1
0
forked from poc/pds

Refactor invoice report generation to include quantity metrics and enhance email content structure

This commit is contained in:
dhanabalan
2025-08-26 18:08:06 +05:30
parent e57962f023
commit 19e1f12f5d
5 changed files with 195 additions and 392 deletions

View File

@@ -30,252 +30,11 @@ class SendInvoiceReport extends Command
* Execute the console command.
*/
// public function handle()
// {
// $schedule = $this->argument('schedule_type');
// $plantid = $this->argument('plant');
// $mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceValidation')->get()->groupBy('rule_name');
// $startDate = now()->setTime(8, 0, 0);
// $endDate = now()->copy()->addDay()->setTime(8, 0, 0);
// $plants = InvoiceValidation::select('plant_id')->distinct()->pluck('plant_id');
// $serialTableData = [];
// $materialTableData = [];
// $bundleTableData = [];
// $noSerial = 1;
// $noMaterial = 1;
// $noBundle = 1;
// foreach ($plants as $plantId) {
// $plant = Plant::find($plantId);
// $plantName = $plant ? $plant->name : $plantId;
// //..Serial Invoice
// $totalSerialCount = InvoiceValidation::where('plant_id', $plantId)
// ->whereNull('quantity')
// ->whereBetween('created_at', [$startDate, $endDate])
// ->distinct('invoice_number')
// ->count('invoice_number');
// $scannedSerialCount = InvoiceValidation::select('invoice_number')
// ->where('plant_id', $plantId)
// ->whereNull('quantity')
// ->whereBetween('updated_at', [$startDate, $endDate])
// ->groupBy('invoice_number')
// ->havingRaw(
// "COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)"
// )
// ->count();
// $serialTableData[] = [
// 'no' => $noSerial++,
// 'plant' => $plantName,
// 'totalInvoice' => $totalSerialCount,
// 'scannedInvoice' => $scannedSerialCount,
// ];
// //..Individual Invoice
// $TotalMatCount = InvoiceValidation::where('plant_id', $plantId)
// ->where('quantity', 1)
// ->whereBetween('created_at', [$startDate, $endDate])
// ->distinct('invoice_number')
// ->count('invoice_number');
// $scannedMatCount = InvoiceValidation::select('invoice_number')
// ->where('plant_id', $plantId)
// ->where('quantity', 1)
// ->whereBetween('updated_at', [$startDate, $endDate])
// ->groupBy('invoice_number')
// ->havingRaw(
// "COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"
// )
// ->count();
// $materialTableData[] = [
// 'no' => $noMaterial++,
// 'plant' => $plantName,
// 'totalInvoice' => $TotalMatCount,
// 'scannedInvoice' => $scannedMatCount,
// ];
// //..BUndle Invoice
// $totalBundleCount = InvoiceValidation::where('plant_id', $plantId)
// ->where('quantity', '>', 1)
// ->whereBetween('created_at', [$startDate, $endDate])
// ->distinct('invoice_number')
// ->count('invoice_number');
// $scannedBundleCount = InvoiceValidation::select('invoice_number')
// ->where('plant_id', $plantId)
// ->where('quantity', '>', 1)
// ->whereBetween('updated_at', [$startDate, $endDate])
// ->groupBy('invoice_number')
// ->havingRaw(
// "COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"
// )
// ->count();
// $bundleTableData[] = [
// 'no' => $noBundle++,
// 'plant' => $plantName,
// 'totalInvoice' => $totalBundleCount,
// 'scannedInvoice' => $scannedBundleCount,
// ];
// }
// // Send to SerialInvoiceMail recipients
// if ($mailRules->has('SerialInvoiceMail')) {
// $emails = $mailRules['SerialInvoiceMail']->pluck('email')->unique()->toArray();
// foreach ($emails as $email) {
// Mail::to($email)->send(new test($serialTableData, [], []));
// }
// }
// // Send to MaterialInvoiceMail recipients (material + bundle table)
// if ($mailRules->has('MaterialInvoiceMail')) {
// $emails = $mailRules['MaterialInvoiceMail']->pluck('email')->unique()->toArray();
// foreach ($emails as $email) {
// Mail::to($email)->send(new test([], $materialTableData, $bundleTableData));
// }
// }
// // Send to InvoiceMail recipients (all three tables)
// if ($mailRules->has('InvoiceMail')) {
// $emails = $mailRules['InvoiceMail']->pluck('email')->unique()->toArray();
// foreach ($emails as $email) {
// Mail::to($email)->send(new test($serialTableData, $materialTableData, $bundleTableData));
// }
// }
// //$this->info(json_encode($materialTableData));
// }
// public function handle()
// {
// $schedule = $this->argument('schedule_type');
// $plantId = $this->argument('plant');
// $mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceValidation')->get()->groupBy('rule_name');
// $startDate = now()->setTime(8, 0, 0);
// $endDate = now()->copy()->addDay()->setTime(8, 0, 0);
// $serialTableData = [];
// $materialTableData = [];
// $bundleTableData = [];
// $plant = Plant::find($plantId);
// $plantName = $plant ? $plant->name : $plantId;
// // Serial Invoice
// $totalSerialCount = InvoiceValidation::where('plant_id', $plantId)
// ->whereNull('quantity')
// ->whereBetween('created_at', [$startDate, $endDate])
// ->distinct('invoice_number')
// ->count('invoice_number');
// $scannedSerialCount = InvoiceValidation::select('invoice_number')
// ->where('plant_id', $plantId)
// ->whereNull('quantity')
// ->whereBetween('updated_at', [$startDate, $endDate])
// ->groupBy('invoice_number')
// ->havingRaw(
// "COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)"
// )
// ->count();
// $serialTableData[] = [
// 'no' => 1,
// 'plant' => $plantName,
// 'totalInvoice' => $totalSerialCount,
// 'scannedInvoice' => $scannedSerialCount,
// ];
// // Individual Material Invoice
// $TotalMatCount = InvoiceValidation::where('plant_id', $plantId)
// ->where('quantity', 1)
// ->whereBetween('created_at', [$startDate, $endDate])
// ->distinct('invoice_number')
// ->count('invoice_number');
// $scannedMatCount = InvoiceValidation::select('invoice_number')
// ->where('plant_id', $plantId)
// ->where('quantity', 1)
// ->whereBetween('updated_at', [$startDate, $endDate])
// ->groupBy('invoice_number')
// ->havingRaw(
// "COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"
// )
// ->count();
// $materialTableData[] = [
// 'no' => 1,
// 'plant' => $plantName,
// 'totalInvoice' => $TotalMatCount,
// 'scannedInvoice' => $scannedMatCount,
// ];
// // Bundle Invoice
// $totalBundleCount = InvoiceValidation::where('plant_id', $plantId)
// ->where('quantity', '>', 1)
// ->whereBetween('created_at', [$startDate, $endDate])
// ->distinct('invoice_number')
// ->count('invoice_number');
// $scannedBundleCount = InvoiceValidation::select('invoice_number')
// ->where('plant_id', $plantId)
// ->where('quantity', '>', 1)
// ->whereBetween('updated_at', [$startDate, $endDate])
// ->groupBy('invoice_number')
// ->havingRaw(
// "COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"
// )
// ->count();
// $bundleTableData[] = [
// 'no' => 1,
// 'plant' => $plantName,
// 'totalInvoice' => $totalBundleCount,
// 'scannedInvoice' => $scannedBundleCount,
// ];
// // Send to SerialInvoiceMail recipients
// if ($mailRules->has('SerialInvoiceMail')) {
// $emails = $mailRules['SerialInvoiceMail']->pluck('email')->unique()->toArray();
// foreach ($emails as $email) {
// Mail::to($email)->send(new test($serialTableData, [], []));
// }
// }
// // Send to MaterialInvoiceMail recipients (material + bundle table)
// if ($mailRules->has('MaterialInvoiceMail')) {
// $emails = $mailRules['MaterialInvoiceMail']->pluck('email')->unique()->toArray();
// foreach ($emails as $email) {
// Mail::to($email)->send(new test([], $materialTableData, $bundleTableData));
// }
// }
// // Send to InvoiceMail recipients (all three tables)
// if ($mailRules->has('InvoiceMail')) {
// $emails = $mailRules['InvoiceMail']->pluck('email')->unique()->toArray();
// foreach ($emails as $email) {
// Mail::to($email)->send(new test($serialTableData, $materialTableData, $bundleTableData));
// }
// }
// // $this->table(
// // ['No', 'Plant', 'Total Invoice', 'Scanned Invoice'],
// // $serialTableData
// // );
// }
public function handle()
{
$schedule = $this->argument('schedule_type');
//$scheduleType = $this->argument('scheduleType');
$plantIdArg = (int) $this->argument('plant'); // can be 0 for all plants
$mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceValidation')->get()->groupBy('rule_name');
@@ -303,7 +62,8 @@ class SendInvoiceReport extends Command
$startDate = now()->setTime(8, 0, 0);
$endDate = now()->copy()->addDay()->setTime(8, 0, 0);
}
foreach ($plantIds as $plantId) {
foreach ($plantIds as $plantId)
{
$plant = Plant::find($plantId);
$plantName = $plant ? $plant->name : $plantId;
@@ -322,11 +82,27 @@ class SendInvoiceReport extends Command
->havingRaw("COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)")
->count();
$serialInvoiceQuan = InvoiceValidation::where('plant_id', $plantId)
->where('quantity', null)
->whereBetween('created_at', [$startDate, $endDate])
->count();
$scannedInvoiceQuan = InvoiceValidation::where('plant_id', $plantId)
->where('scanned_status', 'Scanned')
->where(function($query) {
$query->whereNull('quantity')
->orWhere('quantity', 0);
})
->whereBetween('updated_at', [$startDate, $endDate])
->count();
$serialTableData[] = [
'no' => $no,
'plant' => $plantName,
'totalInvoice' => $totalSerialCount,
'scannedInvoice' => $scannedSerialCount,
'totalInvoiceQuan' => $serialInvoiceQuan,
'scannedInvoiceQuan' => $scannedInvoiceQuan,
];
// Material Invoice
@@ -344,11 +120,25 @@ class SendInvoiceReport extends Command
->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)")
->count();
$totalMatInvoiceQuan = InvoiceValidation::where('plant_id', $plantId)
->where('quantity', 1)
->whereBetween('created_at', [$startDate, $endDate])
->count();
$scannedMatInvoiceQuan = InvoiceValidation::where('plant_id', $plantId)
->where('quantity', 1)
->whereNotNull('serial_number')
->where('serial_number','!=', '')
->whereBetween('updated_at', [$startDate, $endDate])
->count();
$materialTableData[] = [
'no' => $no,
'plant' => $plantName,
'totalInvoice' => $totalMatCount,
'scannedInvoice' => $scannedMatCount,
'totalInvoiceQuan' => $totalMatInvoiceQuan,
'scannedInvoiceQuan' => $scannedMatInvoiceQuan,
];
// Bundle Invoice
@@ -366,29 +156,51 @@ class SendInvoiceReport extends Command
->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)")
->count();
$totalBundleInvoiceQuan = InvoiceValidation::where('plant_id', $plantId)
->where('quantity', null)
->whereBetween('created_at', [$startDate, $endDate])
->count();
$scannedBundleInvoiceQuan = InvoiceValidation::where('plant_id', $plantId)
->where('scanned_status', 'Scanned')
->where(function($query) {
$query->whereNull('quantity')
->orWhere('quantity', 0);
})
->whereBetween('updated_at', [$startDate, $endDate])
->count();
$bundleTableData[] = [
'no' => $no,
'plant' => $plantName,
'totalInvoice' => $totalBundleCount,
'scannedInvoice' => $scannedBundleCount,
'totalInvoiceQuan' => $totalBundleInvoiceQuan,
'scannedInvoiceQuan' => $scannedBundleInvoiceQuan,
];
$no++;
}
$mail = new test($serialTableData, $materialTableData, $bundleTableData, $schedule);
$contentVars = $mail->content()->with;
$this->info($contentVars['greeting'] ?? 'Invoice Report');
// Send to SerialInvoiceMail recipients
if ($mailRules->has('SerialInvoiceMail')) {
$emails = $mailRules['SerialInvoiceMail']->pluck('email')->unique()->toArray();
foreach ($emails as $email) {
Mail::to($email)->send(new test($serialTableData, [], []));
Mail::to($email)->send(new test($serialTableData, [], [], $schedule));
}
}
// Send to MaterialInvoiceMail recipients (material + bundle table)
if ($mailRules->has('MaterialInvoiceMail')) {
$emails = $mailRules['MaterialInvoiceMail']->pluck('email')->unique()->toArray();
foreach ($emails as $email) {
Mail::to($email)->send(new test([], $materialTableData, $bundleTableData));
Mail::to($email)->send(new test([], $materialTableData, $bundleTableData, $schedule));
}
}
@@ -396,19 +208,21 @@ class SendInvoiceReport extends Command
if ($mailRules->has('InvoiceMail')) {
$emails = $mailRules['InvoiceMail']->pluck('email')->unique()->toArray();
foreach ($emails as $email) {
Mail::to($email)->send(new test($serialTableData, $materialTableData, $bundleTableData));
Mail::to($email)->send(new test($serialTableData, $materialTableData, $bundleTableData, $schedule));
}
}
// Show preview in console
$this->info('--- Serial Invoice Table ---');
$this->table(['#', 'Plant', 'Total Invoice', 'Scanned Invoice'], $serialTableData);
$this->table(['#', 'Plant', 'Total Invoice', 'Scanned Invoice','TotalInvoice Quantity', 'ScannedInvoice Quantity'], $serialTableData);
$this->info('--- Material Invoice Table ---');
$this->table(['#', 'Plant', 'Total Invoice', 'Scanned Invoice'], $materialTableData);
$this->table(['#', 'Plant', 'Total Invoice', 'Scanned Invoice','TotalInvoice Quantity', 'ScannedInvoice Quantity'], $materialTableData);
$this->info('--- Bundle Invoice Table ---');
$this->table(['#', 'Plant', 'Total Invoice', 'Scanned Invoice'], $bundleTableData);
$this->table(['#', 'Plant', 'Total Invoice', 'Scanned Invoice','TotalInvoice Quantity', 'ScannedInvoice Quantity'], $bundleTableData);
$this->info($contentVars['wishes'] ?? '');
}