1
0
forked from poc/pds

resolved logic invoice report

This commit is contained in:
dhanabalan
2025-07-10 17:46:34 +05:30
parent 6a8978f676
commit b73202e03f
2 changed files with 211 additions and 159 deletions

View File

@@ -16,7 +16,7 @@ class SendInvoiceReport extends Command
* @var string * @var string
*/ */
// protected $signature = 'app:send-invoice-report'; // protected $signature = 'app:send-invoice-report';
protected $signature = 'send:invoice-report'; protected $signature = 'send:invoice-report{schedule_type} {plant}';
/** /**
@@ -29,95 +29,150 @@ class SendInvoiceReport extends Command
/** /**
* Execute the console command. * Execute the console command.
*/ */
// public function handle()
// {
// //
// }
// public function handle() // public function handle()
// { // {
// $mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceValidation') // $schedule = $this->argument('schedule_type');
// ->where('rule_name', 'InvoiceMailAlert') // $plantid = $this->argument('plant');
// ->get();
// $emails = $mailRules->pluck('email')->unique()->toArray(); // $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'); // $plants = InvoiceValidation::select('plant_id')->distinct()->pluck('plant_id');
// $tableData = [];
// $no = 1; // $serialTableData = [];
// $materialTableData = [];
// $bundleTableData = [];
// $noSerial = 1;
// $noMaterial = 1;
// $noBundle = 1;
// foreach ($plants as $plantId) { // foreach ($plants as $plantId) {
// $plant = Plant::find($plantId); // $plant = Plant::find($plantId);
// $plantName = $plant ? $plant->name : $plantId; // $plantName = $plant ? $plant->name : $plantId;
// $totalInvoice = InvoiceValidation::where('plant_id', $plantId) // //..Serial Invoice
// $totalSerialCount = InvoiceValidation::where('plant_id', $plantId)
// ->whereNull('quantity')
// ->whereBetween('created_at', [$startDate, $endDate])
// ->distinct('invoice_number') // ->distinct('invoice_number')
// ->count('invoice_number'); // ->count('invoice_number');
// $startDate = now()->setTime(8, 0, 0); // $scannedSerialCount = InvoiceValidation::select('invoice_number')
// $endDate = now()->copy()->addDay()->setTime(8, 0, 0);
// $scannedInvoice = InvoiceValidation::select('invoice_number')
// ->where('plant_id', $plantId) // ->where('plant_id', $plantId)
// ->whereNull('quantity') // ->whereNull('quantity')
// ->whereBetween('updated_at', [$startDate, $endDate]) // ->whereBetween('updated_at', [$startDate, $endDate])
// ->groupBy('invoice_number') // ->groupBy('invoice_number')
// ->havingRaw("COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)") // ->havingRaw(
// "COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)"
// )
// ->count(); // ->count();
// $tableData[] = [ // $serialTableData[] = [
// 'no' => $no++, // 'no' => $noSerial++,
// 'plant' => $plantName, // 'plant' => $plantName,
// 'totalInvoice' => $totalInvoice, // 'totalInvoice' => $totalSerialCount,
// 'scannedInvoice' => $scannedInvoice, // '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,
// ]; // ];
// } // }
// // Mail::to('jothikumar.padmanaban@cripumps.com')->send( // // Send to SerialInvoiceMail recipients
// // new test($tableData) // if ($mailRules->has('SerialInvoiceMail')) {
// // ); // $emails = $mailRules['SerialInvoiceMail']->pluck('email')->unique()->toArray();
// if (!empty($emails))
// {
// // Mail::to($emails)->send(new test($tableData));
// // $this->info('Invoice report email sent successfully to: ' . implode(', ', $emails));
// foreach ($emails as $email) { // foreach ($emails as $email) {
// Mail::to($email)->send(new test($tableData)); // 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));
// }
// } // }
// else // // Send to InvoiceMail recipients (all three tables)
// { // if ($mailRules->has('InvoiceMail')) {
// $this->info('No recipients found for InvoiceMailAlert.'); // $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() public function handle()
{ {
// Group emails by rule name $schedule = $this->argument('schedule_type');
$plantId = $this->argument('plant');
$mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceValidation')->get()->groupBy('rule_name'); $mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceValidation')->get()->groupBy('rule_name');
$startDate = now()->setTime(8, 0, 0); $startDate = now()->setTime(8, 0, 0);
$endDate = now()->copy()->addDay()->setTime(8, 0, 0); $endDate = now()->copy()->addDay()->setTime(8, 0, 0);
$plants = InvoiceValidation::select('plant_id')->distinct()->pluck('plant_id');
// Build all tables once
$serialTableData = []; $serialTableData = [];
$materialTableData = []; $materialTableData = [];
$bundleTableData = []; $bundleTableData = [];
$noSerial = 1;
$noMaterial = 1;
$noBundle = 1;
foreach ($plants as $plantId) {
$plant = Plant::find($plantId); $plant = Plant::find($plantId);
$plantName = $plant ? $plant->name : $plantId; $plantName = $plant ? $plant->name : $plantId;
//..Serial Invoice // Serial Invoice
$totalSerialCount = InvoiceValidation::where('plant_id', $plantId) $totalSerialCount = InvoiceValidation::where('plant_id', $plantId)
->whereNull('quantity') ->whereNull('quantity')
->whereBetween('created_at', [$startDate, $endDate]) ->whereBetween('created_at', [$startDate, $endDate])
@@ -135,14 +190,13 @@ class SendInvoiceReport extends Command
->count(); ->count();
$serialTableData[] = [ $serialTableData[] = [
'no' => $noSerial++, 'no' => 1,
'plant' => $plantName, 'plant' => $plantName,
'totalInvoice' => $totalSerialCount, 'totalInvoice' => $totalSerialCount,
'scannedInvoice' => $scannedSerialCount, 'scannedInvoice' => $scannedSerialCount,
]; ];
//..Individual Invoice // Individual Material Invoice
$TotalMatCount = InvoiceValidation::where('plant_id', $plantId) $TotalMatCount = InvoiceValidation::where('plant_id', $plantId)
->where('quantity', 1) ->where('quantity', 1)
->whereBetween('created_at', [$startDate, $endDate]) ->whereBetween('created_at', [$startDate, $endDate])
@@ -160,14 +214,13 @@ class SendInvoiceReport extends Command
->count(); ->count();
$materialTableData[] = [ $materialTableData[] = [
'no' => $noMaterial++, 'no' => 1,
'plant' => $plantName, 'plant' => $plantName,
'totalInvoice' => $TotalMatCount, 'totalInvoice' => $TotalMatCount,
'scannedInvoice' => $scannedMatCount, 'scannedInvoice' => $scannedMatCount,
]; ];
//..BUndle Invoice // Bundle Invoice
$totalBundleCount = InvoiceValidation::where('plant_id', $plantId) $totalBundleCount = InvoiceValidation::where('plant_id', $plantId)
->where('quantity', '>', 1) ->where('quantity', '>', 1)
->whereBetween('created_at', [$startDate, $endDate]) ->whereBetween('created_at', [$startDate, $endDate])
@@ -185,12 +238,11 @@ class SendInvoiceReport extends Command
->count(); ->count();
$bundleTableData[] = [ $bundleTableData[] = [
'no' => $noBundle++, 'no' => 1,
'plant' => $plantName, 'plant' => $plantName,
'totalInvoice' => $totalBundleCount, 'totalInvoice' => $totalBundleCount,
'scannedInvoice' => $scannedBundleCount, 'scannedInvoice' => $scannedBundleCount,
]; ];
}
// Send to SerialInvoiceMail recipients // Send to SerialInvoiceMail recipients
if ($mailRules->has('SerialInvoiceMail')) { if ($mailRules->has('SerialInvoiceMail')) {
@@ -216,8 +268,10 @@ class SendInvoiceReport extends Command
} }
} }
//$this->info(json_encode($materialTableData)); // $this->table(
// ['No', 'Plant', 'Total Invoice', 'Scanned Invoice'],
// $serialTableData
// );
} }
} }

View File

@@ -12,39 +12,15 @@ Artisan::command('inspire', function () {
// Schedule::command('send:invoice-report'); // Schedule::command('send:invoice-report');
// Schedule::command('send:production-report'); // Schedule::command('send:production-report');
// app()->booted(function () {
// $schedule = app(Schedule::class);
// $productionRules = AlertMailRule::where('module', 'ProductionQuantities')
// ->where('rule_name', 'ProductionMail')
// ->get();
// $types = $productionRules->pluck('schedule_type')->unique();
// foreach ($types as $type) {
// $command = $schedule->command("send:production-report", [$type]);
// switch ($type) {
// case 'Live':
// $command->everyMinute();
// break;
// case 'Hourly':
// $command->hourly();
// break;
// case 'Daily':
// $command->dailyAt('07:59');
// break;
// }
// }
// });
app()->booted(function () { app()->booted(function () {
$schedule = app(Schedule::class); $schedule = app(Schedule::class);
//Production report scheduling
$productionRules = AlertMailRule::where('module', 'ProductionQuantities') $productionRules = AlertMailRule::where('module', 'ProductionQuantities')
->where('rule_name', 'ProductionMail') ->where('rule_name', 'ProductionMail')
->get(); ->get();
// Loop each mail rule individually (plant + schedule_type)
foreach ($productionRules as $rule) { foreach ($productionRules as $rule) {
$type = $rule->schedule_type; $type = $rule->schedule_type;
$plantId = $rule->plant; $plantId = $rule->plant;
@@ -63,6 +39,28 @@ app()->booted(function () {
break; break;
} }
} }
//Invoice report scheduling
$invoiceRules = AlertMailRule::where('module', 'InvoiceValidation')->get();
foreach ($invoiceRules as $rule) {
$type = $rule->schedule_type;
$plantId = $rule->plant;
$command = $schedule->command('send:invoice-report', [$type, $plantId]);
switch ($type) {
case 'Live':
$command->everyMinute();
break;
case 'Hourly':
$command->hourly();
break;
case 'Daily':
$command->dailyAt('07:59');
break;
}
}
}); });