From 5e22f0e405520dff32693fd568a6b507e709e685 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Tue, 26 Aug 2025 16:31:56 +0800 Subject: [PATCH 1/3] E-Invoice - Fix a timeout problem when processing import for E-Invoice --- .../ProcessSalesInvoiceReportV2CommandJob.php | 102 ++++++++++++++++++ .../ControllersLogic/ImportExcelLogic.php | 20 ++-- 2 files changed, 113 insertions(+), 9 deletions(-) create mode 100644 app/Classes/Jobs/Commands/V2/ProcessSalesInvoiceReportV2CommandJob.php diff --git a/app/Classes/Jobs/Commands/V2/ProcessSalesInvoiceReportV2CommandJob.php b/app/Classes/Jobs/Commands/V2/ProcessSalesInvoiceReportV2CommandJob.php new file mode 100644 index 00000000..8c0ab335 --- /dev/null +++ b/app/Classes/Jobs/Commands/V2/ProcessSalesInvoiceReportV2CommandJob.php @@ -0,0 +1,102 @@ +details = $details; + } + + public function handle() + { + Log::info(Carbon::now() . ': Start job - Processing single record for E-Invoice from Sales Invoice Report Import.'); + $start = new Carbon(); + + $docNo = $this->details[0] ?? null; + $docDate = $this->details[1] ?? null; + $debtorCode = $this->details[2] ?? null; + $ref = $this->details[3] ?? null; + $shipInfo = $this->details[4] ?? null; + $accNo = $this->details[5] ?? null; + $detailDescription = $this->details[6] ?? null; + $furtherDescription = $this->details[7] ?? null; + $classification = $this->details[8] ?? null; + $deptNo = $this->details[9] ?? null; + $qty = $this->details[10] ?? null; + $unitPrice = $this->details[11] ?? null; + $submitEinvoice = $this->details[12] ?? null; + $consolidatedEinvoice = $this->details[13] ?? null; + $eInvoiceValidationLink = $this->details[14] ?? null; // Safe access for the new column + + Log::info("ProcessSalesInvoiceReportV2CommandJob Details:", [ + 'DocNo' => $docNo, + 'DocDate' => $docDate, + 'DebtorCode' => $debtorCode, + 'Ref' => $ref, + 'ShipInfo' => $shipInfo, + 'AccNo' => $accNo, + 'DetailDescription' => $detailDescription, + 'FurtherDescription' => $furtherDescription, + 'Classification' => $classification, + 'DeptNo' => $deptNo, + 'Qty' => $qty, + 'UnitPrice' => $unitPrice, + 'SubmitEinvoice' => $submitEinvoice, + 'ConsolidatedEinvoice' => $consolidatedEinvoice, + 'EInvoiceValidationLink' => $eInvoiceValidationLink, + ]); + + $booking = Booking::where('marking', $ref)->first(); + if($booking){ + if($docNo != "" && $docNo != "<>"){ + $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_DOCNO_INVOICE, $docNo); + } + if($eInvoiceValidationLink){ + $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_EINVOICE_VALIDATION_LINK, $eInvoiceValidationLink); + } + } + + $end = new Carbon(); + $elapsedTime = $start->diff($end)->format('%H:%I:%S'); + Log::info(Carbon::now() . ': End job - Processing single record for E-Invoice from Sales Invoice Report Import. ElapsedTime: ' . $elapsedTime . '.'); + } + + + private function updateOrCreateKeyValuePair($booking, $key, $value) + { + $keyValuePairObject = new KeyValuePairObject($key, $value); + $metadata = $booking->attributesKVP()->where('key', $key)->first(); + + if ($metadata) { + (App()->make(UpdatesKeyValuePair::class))->execute($metadata, $keyValuePairObject); + } else { + (App()->make(CreatesKeyValuePair::class))->execute($booking, $keyValuePairObject); + } + } +} diff --git a/app/Classes/Modules/Imports/ControllersLogic/ImportExcelLogic.php b/app/Classes/Modules/Imports/ControllersLogic/ImportExcelLogic.php index 27ecc768..8793474e 100644 --- a/app/Classes/Modules/Imports/ControllersLogic/ImportExcelLogic.php +++ b/app/Classes/Modules/Imports/ControllersLogic/ImportExcelLogic.php @@ -5,6 +5,7 @@ namespace App\Classes\Modules\Imports\ControllersLogic; use App\Classes\Exceptions\MalformedRequestException; use App\Classes\General\Abstracts\AbstractControllerLogic; +use App\Classes\Jobs\Commands\V2\ProcessSalesInvoiceReportV2CommandJob; use App\Classes\Modules\Accounts\Services\CreatesKeyValuePair; use App\Classes\Modules\Accounts\Services\UpdatesKeyValuePair; use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject; @@ -182,15 +183,16 @@ class ImportExcelLogic extends AbstractControllerLogic 'EInvoiceValidationLink' => $eInvoiceValidationLink, ]); - $booking = Booking::where('marking', $ref)->first(); - if($booking){ - if($docNo != "" && $docNo != "<>"){ - $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_DOCNO_INVOICE, $docNo); - } - if($eInvoiceValidationLink){ - $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_EINVOICE_VALIDATION_LINK, $eInvoiceValidationLink); - } - } + // $booking = Booking::where('marking', $ref)->first(); + // if($booking){ + // if($docNo != "" && $docNo != "<>"){ + // $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_DOCNO_INVOICE, $docNo); + // } + // if($eInvoiceValidationLink){ + // $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_EINVOICE_VALIDATION_LINK, $eInvoiceValidationLink); + // } + // } + ProcessSalesInvoiceReportV2CommandJob::dispatch($details); } } From dc6031ef1a06437580e02b0360d1554d4efe480c Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Tue, 26 Aug 2025 17:03:10 +0800 Subject: [PATCH 2/3] E-Invoice - Fix a timeout problem when processing import for E-Invoice --- .../V2/ProcessPaymentReportV2CommandJob.php | 92 +++++++++++++++++++ .../ControllersLogic/ImportExcelLogic.php | 38 ++++---- 2 files changed, 112 insertions(+), 18 deletions(-) create mode 100644 app/Classes/Jobs/Commands/V2/ProcessPaymentReportV2CommandJob.php diff --git a/app/Classes/Jobs/Commands/V2/ProcessPaymentReportV2CommandJob.php b/app/Classes/Jobs/Commands/V2/ProcessPaymentReportV2CommandJob.php new file mode 100644 index 00000000..b390a0a6 --- /dev/null +++ b/app/Classes/Jobs/Commands/V2/ProcessPaymentReportV2CommandJob.php @@ -0,0 +1,92 @@ +details = $details; + } + + public function handle() + { + Log::info(Carbon::now() . ': Start job - Processing single record from 01R - RECEIVE PAYMENT (FULL PAYMENT) [AR RECEIVE PAYMENT] Import.'); + $start = new Carbon(); + + $docNo = $this->details[0] ?? null; + $docDate = $this->details[1] ?? null; + $debtorCode = $this->details[2] ?? null; + $description = $this->details[3] ?? null; + $paymentMethod = $this->details[4] ?? null; + $paymentAmt = $this->details[5] ?? null; + $knockOffDocNo = $this->details[6] ?? null; + + Log::info("Row Payment Details:", [ + 'DocNo' => $docNo, + 'DocDate' => $docDate, + 'DebtorCode' => $debtorCode, + 'Description' => $description, + 'PaymentMethod' => $paymentMethod, + 'PaymentAmt' => $paymentAmt, + 'KnockOffDocNo' => $knockOffDocNo, + ]); + + if($knockOffDocNo){ + $kvp = KeyValuePair::where('key', KVPKey::AUTOCOUNT_DOCNO_INVOICE)->where('value', $knockOffDocNo)->first(); + if($kvp){ + $booking = $kvp->owner; + if($booking){ + if($docNo != "" && $docNo != "<>"){ + $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_DOCNO_OFFICIAL_RECEIPT, $docNo); + } + // if($eInvoiceValidationLink){ + // $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_EINVOICE_VALIDATION_LINK, $eInvoiceValidationLink); + // } + } + } + } + + $end = new Carbon(); + $elapsedTime = $start->diff($end)->format('%H:%I:%S'); + Log::info(Carbon::now() . ': End job - Processing single record from 01R - RECEIVE PAYMENT (FULL PAYMENT) [AR RECEIVE PAYMENT] Import. ElapsedTime: ' . $elapsedTime . '.'); + } + + + private function updateOrCreateKeyValuePair($booking, $key, $value) + { + $keyValuePairObject = new KeyValuePairObject($key, $value); + $metadata = $booking->attributesKVP()->where('key', $key)->first(); + + if ($metadata) { + (App()->make(UpdatesKeyValuePair::class))->execute($metadata, $keyValuePairObject); + } else { + (App()->make(CreatesKeyValuePair::class))->execute($booking, $keyValuePairObject); + } + } +} diff --git a/app/Classes/Modules/Imports/ControllersLogic/ImportExcelLogic.php b/app/Classes/Modules/Imports/ControllersLogic/ImportExcelLogic.php index 8793474e..1a57aec9 100644 --- a/app/Classes/Modules/Imports/ControllersLogic/ImportExcelLogic.php +++ b/app/Classes/Modules/Imports/ControllersLogic/ImportExcelLogic.php @@ -5,6 +5,7 @@ namespace App\Classes\Modules\Imports\ControllersLogic; use App\Classes\Exceptions\MalformedRequestException; use App\Classes\General\Abstracts\AbstractControllerLogic; +use App\Classes\Jobs\Commands\V2\ProcessPaymentReportV2CommandJob; use App\Classes\Jobs\Commands\V2\ProcessSalesInvoiceReportV2CommandJob; use App\Classes\Modules\Accounts\Services\CreatesKeyValuePair; use App\Classes\Modules\Accounts\Services\UpdatesKeyValuePair; @@ -221,24 +222,25 @@ class ImportExcelLogic extends AbstractControllerLogic if($knockOffDocNo) { - $kvp = KeyValuePair::where('key', KVPKey::AUTOCOUNT_DOCNO_INVOICE)->where('value', $knockOffDocNo)->first(); - if($kvp){ - $booking = $kvp->owner; - if($booking){ - if($docNo != "" && $docNo != "<>"){ - $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_DOCNO_OFFICIAL_RECEIPT, $docNo); - } - // if($eInvoiceValidationLink){ - // $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_EINVOICE_VALIDATION_LINK, $eInvoiceValidationLink); - // } - } - else{ - $unprocessedKnockOffs[] = $knockOffDocNo; - } - } - else{ - $unprocessedKnockOffs[] = $knockOffDocNo; - } + ProcessPaymentReportV2CommandJob::dispatch($details); + // $kvp = KeyValuePair::where('key', KVPKey::AUTOCOUNT_DOCNO_INVOICE)->where('value', $knockOffDocNo)->first(); + // if($kvp){ + // $booking = $kvp->owner; + // if($booking){ + // if($docNo != "" && $docNo != "<>"){ + // $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_DOCNO_OFFICIAL_RECEIPT, $docNo); + // } + // // if($eInvoiceValidationLink){ + // // $this->updateOrCreateKeyValuePair($booking, KVPKey::AUTOCOUNT_EINVOICE_VALIDATION_LINK, $eInvoiceValidationLink); + // // } + // } + // else{ + // $unprocessedKnockOffs[] = $knockOffDocNo; + // } + // } + // else{ + // $unprocessedKnockOffs[] = $knockOffDocNo; + // } } } From 3b295df9fcc56f3483894da7f2b7f6aa13ee4c1c Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Wed, 27 Aug 2025 10:51:43 +0800 Subject: [PATCH 3/3] E-Invoice - Fix a timeout problem when processing import for E-Invoice --- .../V2/ProcessPaymentReportV2CommandJob.php | 22 ++--- .../ProcessSalesInvoiceReportV2CommandJob.php | 39 ++++---- .../ControllersLogic/ImportExcelLogic.php | 12 ++- .../Imports/Services/AutoCountDataImport.php | 92 +++++++++++++++++++ 4 files changed, 133 insertions(+), 32 deletions(-) create mode 100644 app/Classes/Modules/Imports/Services/AutoCountDataImport.php diff --git a/app/Classes/Jobs/Commands/V2/ProcessPaymentReportV2CommandJob.php b/app/Classes/Jobs/Commands/V2/ProcessPaymentReportV2CommandJob.php index b390a0a6..ed54580e 100644 --- a/app/Classes/Jobs/Commands/V2/ProcessPaymentReportV2CommandJob.php +++ b/app/Classes/Jobs/Commands/V2/ProcessPaymentReportV2CommandJob.php @@ -22,14 +22,14 @@ class ProcessPaymentReportV2CommandJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - /** @var object */ + /** @var array */ private $details; /** * ProcessPaymentReportV2CommandJob constructor. - * @param object $details + * @param array $details */ - public function __construct(object $details) + public function __construct(array $details) { $this->details = $details; } @@ -39,15 +39,15 @@ class ProcessPaymentReportV2CommandJob implements ShouldQueue Log::info(Carbon::now() . ': Start job - Processing single record from 01R - RECEIVE PAYMENT (FULL PAYMENT) [AR RECEIVE PAYMENT] Import.'); $start = new Carbon(); - $docNo = $this->details[0] ?? null; - $docDate = $this->details[1] ?? null; - $debtorCode = $this->details[2] ?? null; - $description = $this->details[3] ?? null; - $paymentMethod = $this->details[4] ?? null; - $paymentAmt = $this->details[5] ?? null; - $knockOffDocNo = $this->details[6] ?? null; + $docNo = $this->details['docno'] ?? null; + $docDate = $this->details['docdate'] ?? null; + $debtorCode = $this->details['debtorcode'] ?? null; + $description = $this->details['description'] ?? null; + $paymentMethod = $this->details['paymentmethod'] ?? null; + $paymentAmt = $this->details['paymentamt'] ?? null; + $knockOffDocNo = $this->details['knockoffdocno'] ?? null; - Log::info("Row Payment Details:", [ + Log::info("Processing Payment Report:", [ 'DocNo' => $docNo, 'DocDate' => $docDate, 'DebtorCode' => $debtorCode, diff --git a/app/Classes/Jobs/Commands/V2/ProcessSalesInvoiceReportV2CommandJob.php b/app/Classes/Jobs/Commands/V2/ProcessSalesInvoiceReportV2CommandJob.php index 8c0ab335..d96fa761 100644 --- a/app/Classes/Jobs/Commands/V2/ProcessSalesInvoiceReportV2CommandJob.php +++ b/app/Classes/Jobs/Commands/V2/ProcessSalesInvoiceReportV2CommandJob.php @@ -21,14 +21,14 @@ class ProcessSalesInvoiceReportV2CommandJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - /** @var object */ + /** @var array */ private $details; /** * ProcessSalesInvoiceReportV2CommandJob constructor. - * @param object $details + * @param array $details */ - public function __construct(object $details) + public function __construct(array $details) { $this->details = $details; } @@ -38,23 +38,24 @@ class ProcessSalesInvoiceReportV2CommandJob implements ShouldQueue Log::info(Carbon::now() . ': Start job - Processing single record for E-Invoice from Sales Invoice Report Import.'); $start = new Carbon(); - $docNo = $this->details[0] ?? null; - $docDate = $this->details[1] ?? null; - $debtorCode = $this->details[2] ?? null; - $ref = $this->details[3] ?? null; - $shipInfo = $this->details[4] ?? null; - $accNo = $this->details[5] ?? null; - $detailDescription = $this->details[6] ?? null; - $furtherDescription = $this->details[7] ?? null; - $classification = $this->details[8] ?? null; - $deptNo = $this->details[9] ?? null; - $qty = $this->details[10] ?? null; - $unitPrice = $this->details[11] ?? null; - $submitEinvoice = $this->details[12] ?? null; - $consolidatedEinvoice = $this->details[13] ?? null; - $eInvoiceValidationLink = $this->details[14] ?? null; // Safe access for the new column + $docNo = $this->details['docno'] ?? null; + $docDate = $this->details['docdate'] ?? null; + $debtorCode = $this->details['debtorcode'] ?? null; + $ref = $this->details['ref'] ?? null; + $shipInfo = $this->details['shipinfo'] ?? null; + $accNo = $this->details['accno'] ?? null; + $detailDescription = $this->details['detaildescription'] ?? null; + $furtherDescription = $this->details['furtherdescription'] ?? null; + $classification = $this->details['classification'] ?? null; + $deptNo = $this->details['deptno'] ?? null; + $qty = $this->details['qty'] ?? null; + $unitPrice = $this->details['unitprice'] ?? null; + $submitEinvoice = $this->details['submiteinvoice'] ?? null; + $consolidatedEinvoice = $this->details['consolidatedeinvoice'] ?? null; + $eInvoiceValidationLink = $this->details['einvoicevalidationlink'] ?? null; - Log::info("ProcessSalesInvoiceReportV2CommandJob Details:", [ + // Log for debugging + Log::info("Processing Sales Invoice Report:", [ 'DocNo' => $docNo, 'DocDate' => $docDate, 'DebtorCode' => $debtorCode, diff --git a/app/Classes/Modules/Imports/ControllersLogic/ImportExcelLogic.php b/app/Classes/Modules/Imports/ControllersLogic/ImportExcelLogic.php index 1a57aec9..d06f12ce 100644 --- a/app/Classes/Modules/Imports/ControllersLogic/ImportExcelLogic.php +++ b/app/Classes/Modules/Imports/ControllersLogic/ImportExcelLogic.php @@ -11,6 +11,7 @@ use App\Classes\Modules\Accounts\Services\CreatesKeyValuePair; use App\Classes\Modules\Accounts\Services\UpdatesKeyValuePair; use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject; use App\Classes\Modules\Accounts\DataTransferObjects\KeyValuePairObject; +use App\Classes\Modules\Imports\Services\AutoCountDataImport; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Classes\ValueObjects\Constants\KVPKey; use App\Models\Booking; @@ -46,7 +47,7 @@ class ImportExcelLogic extends AbstractControllerLogic protected function notification():array { return [ 'title' => 'Import Excel', - 'message' => 'You have successfully imported and updated booking details' + 'message' => 'You have successfully imported data from excel file' ]; } @@ -70,6 +71,13 @@ class ImportExcelLogic extends AbstractControllerLogic throw new MalformedRequestException('Import function can only process one file at a time.'); } + foreach ($files as $file) { + $filePath = json_decode($file)->file_info->original->file; + $import = new AutoCountDataImport($reportType); + Excel::import($import, $filePath); + } + + /* foreach ($files as $file) { $collection = Excel::toCollection(null, json_decode($file)->file_info->original->file, null, null, true); @@ -130,7 +138,7 @@ class ImportExcelLogic extends AbstractControllerLogic throw new MalformedRequestException('Cannot process report type: ' . $reportType); } } - + */ return $this->response($result); } diff --git a/app/Classes/Modules/Imports/Services/AutoCountDataImport.php b/app/Classes/Modules/Imports/Services/AutoCountDataImport.php new file mode 100644 index 00000000..17016aeb --- /dev/null +++ b/app/Classes/Modules/Imports/Services/AutoCountDataImport.php @@ -0,0 +1,92 @@ +reportType = $reportType; + } + + public function headingRow(): int + { + return 1; + } + + /** + * @param Collection $collection + */ + public function collection(Collection $collection) + { + static $headerProcessed = false; + foreach ($collection as $row) { + if (!$headerProcessed) { + $header = $row->keys()->map(fn($h) => strtolower(trim($h)))->toArray(); + $this->validateHeader($header, $this->reportType); + $headerProcessed = true; + } + + if ($this->reportType === 'Sales Invoice Report') { + ProcessSalesInvoiceReportV2CommandJob::dispatch($row->toArray()); + } elseif ($this->reportType === '01R - RECEIVE PAYMENT (FULL PAYMENT) [AR RECEIVE PAYMENT]') { + ProcessPaymentReportV2CommandJob::dispatch($row->toArray()); + } + else{ + throw new MalformedRequestException('Cannot process report type: ' . $this->reportType); + } + } + } + + public function chunkSize(): int + { + return 1000; + } + + private function validateHeader(array $header, String $reportType) + { + $optionalColumn = 'einvoicevalidationlink'; + $salesInvoiceHeader = [ + 'docno', 'docdate', 'debtorcode', 'ref', 'shipinfo', 'accno', + 'detaildescription', 'furtherdescription', 'classification', + 'deptno', 'qty', 'unitprice', 'submiteinvoice', 'consolidatedeinvoice' + ]; + $customersReportHeader = [ + 'tin', 'identityno', 'name', 'identitytype', 'taxclassification', 'msiccode', + 'businessactivitydesc', 'debtorcode', 'tradename', 'address', 'postcode', + 'phone', 'emailaddress', 'city', 'countrycode', 'statecode' + ]; + $paymentReportHeader = [ + 'docno', + 'docdate', + 'debtorcode', + 'description', + 'paymentmethod', + 'paymentamt', + 'knockoffdocno' + ]; + + if ($reportType === 'Sales Invoice Report' && + $header !== $salesInvoiceHeader && + $header !== [...$salesInvoiceHeader, $optionalColumn]) { + throw new MalformedRequestException('Uploaded Excel file format is incorrect. Column headers do not match expected format.'); + } + elseif ($reportType === 'Customers Report' && $header !== $customersReportHeader) { + throw new MalformedRequestException('Uploaded Excel file format is incorrect. Column headers do not match expected format.'); + } + elseif ($reportType === '01R - RECEIVE PAYMENT (FULL PAYMENT) [AR RECEIVE PAYMENT]' && $header !== $paymentReportHeader) { + throw new MalformedRequestException('Uploaded Excel file format is incorrect. Column headers do not match expected format.'); + } + } +}