diff --git a/app/Classes/Jobs/Commands/V2/ProcessPaymentReportV2CommandJob.php b/app/Classes/Jobs/Commands/V2/ProcessPaymentReportV2CommandJob.php new file mode 100644 index 00000000..ed54580e --- /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['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("Processing Payment Report:", [ + '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/Jobs/Commands/V2/ProcessSalesInvoiceReportV2CommandJob.php b/app/Classes/Jobs/Commands/V2/ProcessSalesInvoiceReportV2CommandJob.php new file mode 100644 index 00000000..d96fa761 --- /dev/null +++ b/app/Classes/Jobs/Commands/V2/ProcessSalesInvoiceReportV2CommandJob.php @@ -0,0 +1,103 @@ +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['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 for debugging + Log::info("Processing Sales Invoice Report:", [ + '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 f20e652f..377407a2 100644 --- a/app/Classes/Modules/Imports/ControllersLogic/ImportExcelLogic.php +++ b/app/Classes/Modules/Imports/ControllersLogic/ImportExcelLogic.php @@ -5,10 +5,13 @@ 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; 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; @@ -44,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' ]; } @@ -68,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); @@ -159,7 +169,7 @@ class ImportExcelLogic extends AbstractControllerLogic throw new MalformedRequestException('Cannot process report type: ' . $reportType); } } - + */ return $this->response($result); } @@ -213,15 +223,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); } } @@ -250,24 +261,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; + // } } } 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.'); + } + } +}