E-Invoice - Fix a timeout problem when processing import for E-Invoice

This commit is contained in:
Dillon Ngo
2025-08-27 10:51:43 +08:00
parent dc6031ef1a
commit 3b295df9fc
4 changed files with 133 additions and 32 deletions
@@ -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,
@@ -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,
@@ -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);
}
@@ -0,0 +1,92 @@
<?php
namespace App\Classes\Modules\Imports\Services;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\Jobs\Commands\V2\ProcessPaymentReportV2CommandJob;
use App\Classes\Jobs\Commands\V2\ProcessSalesInvoiceReportV2CommandJob;
use Illuminate\Support\Facades\Log;
class AutoCountDataImport implements ToCollection, WithHeadingRow, WithChunkReading
{
protected $reportType;
public function __construct($reportType)
{
$this->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.');
}
}
}