diff --git a/app/Console/Commands/SendImportTransit.php b/app/Console/Commands/SendImportTransit.php index 7ace83e..d513677 100644 --- a/app/Console/Commands/SendImportTransit.php +++ b/app/Console/Commands/SendImportTransit.php @@ -7,6 +7,8 @@ use App\Models\AlertMailRule; use App\Models\ImportTransit; use App\Models\Plant; use Illuminate\Console\Command; +use Maatwebsite\Excel\Facades\Excel; +use App\Exports\ImportTransitReportExport; class SendImportTransit extends Command { @@ -58,38 +60,41 @@ class SendImportTransit extends Command // return; // } - $tableData = ImportTransit::select([ - 'cri_rfq_number', - 'mail_received_date', - 'pricol_ref_number', - 'requester', - 'shipper', - 'shipper_location', - 'shipper_invoice', - 'shipper_invoice_date', - 'customs_agent_name', - 'eta_date', - 'status', - 'delivery_location', - 'etd_date', - 'mode', - 'inco_terms', - 'port_of_loading', - 'port_of_discharge', - 'delivery_city', - 'packages', - 'type_of_package', - 'gross_weight', - 'volume', - 'bill_number', - 'bill_received_date', - 'vessel_number', - 'remark', - 'is_transit_identified', - ]) - ->where('status', '!=', 'Delivered') - ->get(); + // $tableData = ImportTransit::select([ + // 'cri_rfq_number', + // 'mail_received_date', + // 'pricol_ref_number', + // 'requester', + // 'shipper', + // 'shipper_location', + // 'shipper_invoice', + // 'shipper_invoice_date', + // 'customs_agent_name', + // 'eta_date', + // 'status', + // 'delivery_location', + // 'etd_date', + // 'mode', + // 'inco_terms', + // 'port_of_loading', + // 'port_of_discharge', + // 'delivery_city', + // 'packages', + // 'type_of_package', + // 'gross_weight', + // 'volume', + // 'bill_number', + // 'bill_received_date', + // 'vessel_number', + // 'remark', + // 'is_transit_identified', + // ]) + // ->where('status', '!=', 'Delivered') + // ->get(); + $tableData = ImportTransit::where('status', '!=', 'Delivered')->get(); + + if ($tableData->isEmpty()) { $this->info('No pending Import Transit records found. Mail skipped.'); return; @@ -101,10 +106,19 @@ class SendImportTransit extends Command $mailSubject = 'Daily Import Transit Report'; + $fileName = 'reports/pending_import_shipment_' . now()->format('Ymd_His') . '.xlsx'; + + Excel::store( + new ImportTransitReportExport($tableData), + $fileName, + 'local' + ); + $mail = new ImportTransitMail( $scheduleType, $tableData, - $mailSubject + $mailSubject, + $fileName ); $toEmails = collect(explode(',', $rule->email)) diff --git a/app/Mail/ImportTransitMail.php b/app/Mail/ImportTransitMail.php index e849d86..b18cb7f 100644 --- a/app/Mail/ImportTransitMail.php +++ b/app/Mail/ImportTransitMail.php @@ -9,6 +9,7 @@ use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use Illuminate\Queue\SerializesModels; +use Illuminate\Mail\Mailables\Attachment; class ImportTransitMail extends Mailable { @@ -20,14 +21,17 @@ class ImportTransitMail extends Mailable public $mailSubject; + public $excelPath; + /** * Create a new message instance. */ - public function __construct($scheduleType, $tableData, $mailSubject) + public function __construct($scheduleType, $tableData, $mailSubject, $excelPath) { $this->scheduleType = $scheduleType; $this->tableData = $tableData ?? []; $this->mailSubject = $mailSubject ?? 'Import Transit'; + $this->excelPath = $excelPath; } /** @@ -90,6 +94,14 @@ class ImportTransitMail extends Mailable */ public function attachments(): array { - return []; + return [ + Attachment::fromStorageDisk( + 'local', + $this->excelPath + )->as('Import_Transit_Report.xlsx') + ->withMime( + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' + ), + ]; } }