Added logic for all plants in invoice report
This commit is contained in:
@@ -155,10 +155,128 @@ class SendInvoiceReport extends Command
|
|||||||
// //$this->info(json_encode($materialTableData));
|
// //$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()
|
public function handle()
|
||||||
{
|
{
|
||||||
$schedule = $this->argument('schedule_type');
|
$schedule = $this->argument('schedule_type');
|
||||||
$plantId = $this->argument('plant');
|
$plantIdArg = (int) $this->argument('plant'); // can be 0 for all plants
|
||||||
|
|
||||||
$mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceValidation')->get()->groupBy('rule_name');
|
$mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceValidation')->get()->groupBy('rule_name');
|
||||||
|
|
||||||
@@ -169,80 +287,84 @@ class SendInvoiceReport extends Command
|
|||||||
$materialTableData = [];
|
$materialTableData = [];
|
||||||
$bundleTableData = [];
|
$bundleTableData = [];
|
||||||
|
|
||||||
$plant = Plant::find($plantId);
|
// Get plant IDs: either one plant or all
|
||||||
$plantName = $plant ? $plant->name : $plantId;
|
$plantIds = $plantIdArg == 0
|
||||||
|
? InvoiceValidation::select('plant_id')->distinct()->pluck('plant_id')->toArray()
|
||||||
|
: [$plantIdArg];
|
||||||
|
|
||||||
// Serial Invoice
|
$no = 1;
|
||||||
$totalSerialCount = InvoiceValidation::where('plant_id', $plantId)
|
foreach ($plantIds as $plantId) {
|
||||||
->whereNull('quantity')
|
$plant = Plant::find($plantId);
|
||||||
->whereBetween('created_at', [$startDate, $endDate])
|
$plantName = $plant ? $plant->name : $plantId;
|
||||||
->distinct('invoice_number')
|
|
||||||
->count('invoice_number');
|
|
||||||
|
|
||||||
$scannedSerialCount = InvoiceValidation::select('invoice_number')
|
// Serial Invoice
|
||||||
->where('plant_id', $plantId)
|
$totalSerialCount = InvoiceValidation::where('plant_id', $plantId)
|
||||||
->whereNull('quantity')
|
->whereNull('quantity')
|
||||||
->whereBetween('updated_at', [$startDate, $endDate])
|
->whereBetween('created_at', [$startDate, $endDate])
|
||||||
->groupBy('invoice_number')
|
->distinct('invoice_number')
|
||||||
->havingRaw(
|
->count('invoice_number');
|
||||||
"COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)"
|
|
||||||
)
|
|
||||||
->count();
|
|
||||||
|
|
||||||
$serialTableData[] = [
|
$scannedSerialCount = InvoiceValidation::select('invoice_number')
|
||||||
'no' => 1,
|
->where('plant_id', $plantId)
|
||||||
'plant' => $plantName,
|
->whereNull('quantity')
|
||||||
'totalInvoice' => $totalSerialCount,
|
->whereBetween('updated_at', [$startDate, $endDate])
|
||||||
'scannedInvoice' => $scannedSerialCount,
|
->groupBy('invoice_number')
|
||||||
];
|
->havingRaw("COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)")
|
||||||
|
->count();
|
||||||
|
|
||||||
// Individual Material Invoice
|
$serialTableData[] = [
|
||||||
$TotalMatCount = InvoiceValidation::where('plant_id', $plantId)
|
'no' => $no,
|
||||||
->where('quantity', 1)
|
'plant' => $plantName,
|
||||||
->whereBetween('created_at', [$startDate, $endDate])
|
'totalInvoice' => $totalSerialCount,
|
||||||
->distinct('invoice_number')
|
'scannedInvoice' => $scannedSerialCount,
|
||||||
->count('invoice_number');
|
];
|
||||||
|
|
||||||
$scannedMatCount = InvoiceValidation::select('invoice_number')
|
// Material Invoice
|
||||||
->where('plant_id', $plantId)
|
$totalMatCount = InvoiceValidation::where('plant_id', $plantId)
|
||||||
->where('quantity', 1)
|
->where('quantity', 1)
|
||||||
->whereBetween('updated_at', [$startDate, $endDate])
|
->whereBetween('created_at', [$startDate, $endDate])
|
||||||
->groupBy('invoice_number')
|
->distinct('invoice_number')
|
||||||
->havingRaw(
|
->count('invoice_number');
|
||||||
"COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"
|
|
||||||
)
|
|
||||||
->count();
|
|
||||||
|
|
||||||
$materialTableData[] = [
|
$scannedMatCount = InvoiceValidation::select('invoice_number')
|
||||||
'no' => 1,
|
->where('plant_id', $plantId)
|
||||||
'plant' => $plantName,
|
->where('quantity', 1)
|
||||||
'totalInvoice' => $TotalMatCount,
|
->whereBetween('updated_at', [$startDate, $endDate])
|
||||||
'scannedInvoice' => $scannedMatCount,
|
->groupBy('invoice_number')
|
||||||
];
|
->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)")
|
||||||
|
->count();
|
||||||
|
|
||||||
// Bundle Invoice
|
$materialTableData[] = [
|
||||||
$totalBundleCount = InvoiceValidation::where('plant_id', $plantId)
|
'no' => $no,
|
||||||
->where('quantity', '>', 1)
|
'plant' => $plantName,
|
||||||
->whereBetween('created_at', [$startDate, $endDate])
|
'totalInvoice' => $totalMatCount,
|
||||||
->distinct('invoice_number')
|
'scannedInvoice' => $scannedMatCount,
|
||||||
->count('invoice_number');
|
];
|
||||||
|
|
||||||
$scannedBundleCount = InvoiceValidation::select('invoice_number')
|
// Bundle Invoice
|
||||||
->where('plant_id', $plantId)
|
$totalBundleCount = InvoiceValidation::where('plant_id', $plantId)
|
||||||
->where('quantity', '>', 1)
|
->where('quantity', '>', 1)
|
||||||
->whereBetween('updated_at', [$startDate, $endDate])
|
->whereBetween('created_at', [$startDate, $endDate])
|
||||||
->groupBy('invoice_number')
|
->distinct('invoice_number')
|
||||||
->havingRaw(
|
->count('invoice_number');
|
||||||
"COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"
|
|
||||||
)
|
|
||||||
->count();
|
|
||||||
|
|
||||||
$bundleTableData[] = [
|
$scannedBundleCount = InvoiceValidation::select('invoice_number')
|
||||||
'no' => 1,
|
->where('plant_id', $plantId)
|
||||||
'plant' => $plantName,
|
->where('quantity', '>', 1)
|
||||||
'totalInvoice' => $totalBundleCount,
|
->whereBetween('updated_at', [$startDate, $endDate])
|
||||||
'scannedInvoice' => $scannedBundleCount,
|
->groupBy('invoice_number')
|
||||||
];
|
->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)")
|
||||||
|
->count();
|
||||||
|
|
||||||
|
$bundleTableData[] = [
|
||||||
|
'no' => $no,
|
||||||
|
'plant' => $plantName,
|
||||||
|
'totalInvoice' => $totalBundleCount,
|
||||||
|
'scannedInvoice' => $scannedBundleCount,
|
||||||
|
];
|
||||||
|
|
||||||
|
$no++;
|
||||||
|
}
|
||||||
|
|
||||||
// Send to SerialInvoiceMail recipients
|
// Send to SerialInvoiceMail recipients
|
||||||
if ($mailRules->has('SerialInvoiceMail')) {
|
if ($mailRules->has('SerialInvoiceMail')) {
|
||||||
@@ -268,10 +390,16 @@ class SendInvoiceReport extends Command
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// $this->table(
|
// Show preview in console
|
||||||
// ['No', 'Plant', 'Total Invoice', 'Scanned Invoice'],
|
$this->info('--- Serial Invoice Table ---');
|
||||||
// $serialTableData
|
$this->table(['#', 'Plant', 'Total Invoice', 'Scanned Invoice'], $serialTableData);
|
||||||
// );
|
|
||||||
|
$this->info('--- Material Invoice Table ---');
|
||||||
|
$this->table(['#', 'Plant', 'Total Invoice', 'Scanned Invoice'], $materialTableData);
|
||||||
|
|
||||||
|
$this->info('--- Bundle Invoice Table ---');
|
||||||
|
$this->table(['#', 'Plant', 'Total Invoice', 'Scanned Invoice'], $bundleTableData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user