diff --git a/app/Console/Commands/SendInvoiceReport.php b/app/Console/Commands/SendInvoiceReport.php index 26a5ef1..24fa87d 100644 --- a/app/Console/Commands/SendInvoiceReport.php +++ b/app/Console/Commands/SendInvoiceReport.php @@ -32,12 +32,12 @@ class SendInvoiceReport extends Command { $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'); + $plantIdArg = (int) $this->argument('plant'); $mailRules = \App\Models\AlertMailRule::where('module', 'InvoiceValidation')->get()->groupBy('rule_name'); + $this->info(print_r($mailRules->toArray(), true)); + // $startDate = now()->setTime(8, 0, 0); // $endDate = now()->copy()->addDay()->setTime(8, 0, 0); @@ -50,6 +50,8 @@ class SendInvoiceReport extends Command ? InvoiceValidation::select('plant_id')->distinct()->pluck('plant_id')->toArray() : [$plantIdArg]; + $isPlantRun = $plantIdArg != 0; + $no = 1; if (strtolower($schedule) == 'daily') { $startDate = now()->subDay()->setTime(8, 0, 0); @@ -178,61 +180,97 @@ class SendInvoiceReport extends Command $no++; } - $mail = new test($serialTableData, $materialTableData, $bundleTableData, $schedule); - $contentVars = $mail->content()->with; + // $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')) { + + // foreach ($mailRules->get('SerialInvoiceMail') as $rule) { + + // $filteredSerialData = []; + + // $toEmails = collect(explode(',', $rule->email)) + // ->map(fn ($e) => trim($e)) + // ->filter() + // ->unique() + // ->values() + // ->toArray(); + + // $ccEmails = collect(explode(',', $rule->cc_emails ?? '')) + // ->map(fn ($e) => trim($e)) + // ->filter() + // ->unique() + // ->values() + // ->toArray(); + + // if (empty($toEmails)) { + // $this->warn("Skipping rule ID {$rule->id} — no valid To emails found."); + + // continue; + // } + + // if ($rule->plant == 0) { + // $filteredSerialData = $serialTableData; + + // } else { + // $filteredSerialData = collect($serialTableData) + // ->where('plant_id', $rule->plant) + // ->values() + // ->toArray(); + // } + + // if (empty($filteredSerialData)) { + // $this->warn("Skipping empty mail for rule {$rule->id}"); + // continue; + // } + + // // \Mail::to($toEmails) + // // ->cc($ccEmails) + // // ->send(new test( + // // $filteredSerialData, + // // [], + // // [], + // // $schedule + // // )); + + // $mail = new test( + // $filteredSerialData, + // [], + // [], + // $schedule + // ); + + // \Mail::to($toEmails) + // ->cc($ccEmails) + // ->send($mail); + + // // \Mail::to($toEmails)->cc($ccEmails)->send(new test($serialTableData, [], [], $schedule)); + + // $this->info("Mail sent for rule ID {$rule->id} → To: ".implode(', ', $toEmails).($ccEmails ? ' | CC: '.implode(', ', $ccEmails) : '')); + // } + // } + if ($mailRules->has('SerialInvoiceMail')) { - // $emails = $mailRules['SerialInvoiceMail']->pluck('email')->unique()->toArray(); - // foreach ($emails as $email) { - // Mail::to($email)->send(new test($serialTableData, [], [], $schedule)); - // } + foreach ($mailRules->get('SerialInvoiceMail') as $rule) { - $toEmails = collect(explode(',', $rule->email)) - ->map(fn ($e) => trim($e)) - ->filter() - ->unique() - ->values() - ->toArray(); - - $ccEmails = collect(explode(',', $rule->cc_emails ?? '')) - ->map(fn ($e) => trim($e)) - ->filter() - ->unique() - ->values() - ->toArray(); - - if (empty($toEmails)) { - $this->warn("Skipping rule ID {$rule->id} — no valid To emails found."); - + if ($isPlantRun && $rule->plant != $plantIdArg && $rule->plant != 0) { continue; } - if ($rule->plant == 0) { - $filteredSerialData = $serialTableData; + $data = collect($serialTableData) + ->when($rule->plant != 0, fn ($q) => $q->where('plant_id', $rule->plant)) + ->values() + ->toArray(); - } else { - $filteredSerialData = collect($serialTableData) - ->where('plant_id', $rule->plant) - ->values() - ->toArray(); - } + if (empty($data)) continue; - \Mail::to($toEmails) - ->cc($ccEmails) - ->send(new test( - $filteredSerialData, - [], - [], - $schedule - )); - - // \Mail::to($toEmails)->cc($ccEmails)->send(new test($serialTableData, [], [], $schedule)); - - $this->info("Mail sent for rule ID {$rule->id} → To: ".implode(', ', $toEmails).($ccEmails ? ' | CC: '.implode(', ', $ccEmails) : '')); + \Mail::to($rule->email) + ->cc($rule->cc_emails ?? []) + ->send(new test($data, [], [], $schedule)); } } @@ -300,12 +338,21 @@ class SendInvoiceReport extends Command // 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, $schedule)); - // $this->info("✅ Sent InvoiceMail to: {$email}"); - // } - foreach ($mailRules->get('InvoiceMail') as $rule) { + + $invoiceRules = $mailRules->get('InvoiceMail'); + $hasGlobalRule = $invoiceRules->contains('plant', 0); + + // foreach ($mailRules->get('InvoiceMail') as $rule) { + foreach ($invoiceRules as $rule) { + + if ($hasGlobalRule && $rule->plant != 0) { + $this->info("Skipping rule ID {$rule->id} (plant={$rule->plant}) — covered by global plant=0 rule."); + continue; + } + + $filteredSerialData = []; + $filteredMaterialData = []; + $filteredBundleData = []; $toEmails = collect(explode(',', $rule->email)) ->map(fn ($e) => trim($e)) @@ -327,7 +374,6 @@ class SendInvoiceReport extends Command continue; } - // FILTER DATA if ($rule->plant == 0) { $filteredSerialData = $serialTableData; @@ -362,18 +408,35 @@ class SendInvoiceReport extends Command continue; } + // \Mail::to($toEmails) + // ->cc($ccEmails) + // ->send(new test( + // $filteredSerialData, + // $filteredMaterialData, + // $filteredBundleData, + // $schedule + // )); + + $mail = new test( + $filteredSerialData, + $filteredMaterialData, + $filteredBundleData, + $schedule + ); + \Mail::to($toEmails) ->cc($ccEmails) - ->send(new test( - $filteredSerialData, - $filteredMaterialData, - $filteredBundleData, - $schedule - )); + ->send($mail); + + // \Mail::raw('Test mail', function ($message) use ($toEmails, $ccEmails) { + // $message->to($toEmails) + // ->cc($ccEmails) + // ->subject('Test'); + // }); // \Mail::to($toEmails)->cc($ccEmails)->send(new test($serialTableData, $materialTableData, $bundleTableData, $schedule)); - $this->info("Mail sent for rule ID {$rule->id} → To: ".implode(', ', $toEmails).($ccEmails ? ' | CC: '.implode(', ', $ccEmails) : '')); + // $this->info("Mail sent for rule ID {$rule->id} → To: ".implode(', ', $toEmails).($ccEmails ? ' | CC: '.implode(', ', $ccEmails) : '')); } } diff --git a/app/Filament/Resources/StickerMasterResource.php b/app/Filament/Resources/StickerMasterResource.php index aa5e7b4..10a9759 100644 --- a/app/Filament/Resources/StickerMasterResource.php +++ b/app/Filament/Resources/StickerMasterResource.php @@ -296,9 +296,10 @@ class StickerMasterResource extends Resource Forms\Components\Select::make('material_type') ->label('Material Type') ->options([ - '1' => 'Individual', - '2' => 'Bundle', - '3' => 'Quantity', + 1 => 'Individual', + 2 => 'Bundle', + 3 => 'Quantity', + 4 => 'Bundle Individual', ]) ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { @@ -307,7 +308,7 @@ class StickerMasterResource extends Resource $set('load_rate', 0); } - if ($state !== '2') { + if ($state != '2' && $state != '4') { $set('bundle_quantity', null); } else { $set('bundle_quantity', 2); @@ -319,12 +320,12 @@ class StickerMasterResource extends Resource Forms\Components\TextInput::make('bundle_quantity') ->label('Bundle Quantity') ->integer() - ->readOnly(fn (callable $get) => $get('material_type') !== '2') + ->readOnly(fn (callable $get) => $get('material_type') != 2 && $get('material_type') != 4) ->nullable() ->minValue(2) ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { - if ($get('material_type') !== '2') { + if ($get('material_type') != 2 && $get('material_type') != 4) { $set('bundle_quantity', null); } elseif ($get('bundle_quantity') < 2) { $set('bundle_quantity', 2); @@ -655,10 +656,6 @@ class StickerMasterResource extends Resource ->label('Load Rate') ->alignCenter() ->sortable(), - Tables\Columns\TextColumn::make('bundle_quantity') - ->label('Bundle Quantity') - ->default('-') - ->alignCenter(), Tables\Columns\TextColumn::make('material_type') ->label('Material Type') ->default('-') @@ -672,9 +669,14 @@ class StickerMasterResource extends Resource 1 => 'Individual', 2 => 'Bundle', 3 => 'Quantity', + 4 => 'Bundle Individual', default => '-', }; }), + Tables\Columns\TextColumn::make('bundle_quantity') + ->label('Bundle Quantity') + ->default('-') + ->alignCenter(), Tables\Columns\TextColumn::make('created_by') ->label('Created By') ->alignCenter() @@ -754,12 +756,13 @@ class StickerMasterResource extends Resource ->searchable() ->reactive(), Select::make('material_type') - ->label('Select Material Type') + ->label('Search by Material Type') ->nullable() ->options([ 1 => 'Individual', 2 => 'Bundle', 3 => 'Quantity', + 4 => 'Bundle Individual', ]) ->searchable() ->reactive(), @@ -870,6 +873,8 @@ class StickerMasterResource extends Resource $indicators[] = 'Material Type: Bundle'; } elseif ($data['material_type'] == 3) { $indicators[] = 'Material Type: Quantity'; + } elseif ($data['material_type'] == 4) { + $indicators[] = 'Material Type: Bundle Individual'; } // $indicators[] = 'Material Type: '.$data['material_type']; }