diff --git a/app/Classes/Jobs/Commands/V2/ProcessCreditNoteReportV2CommandJob.php b/app/Classes/Jobs/Commands/V2/ProcessCreditNoteReportV2CommandJob.php new file mode 100644 index 00000000..91dc27bb --- /dev/null +++ b/app/Classes/Jobs/Commands/V2/ProcessCreditNoteReportV2CommandJob.php @@ -0,0 +1,108 @@ +details = $details; + } + + public function handle() + { + Log::info(Carbon::now() . ': Start job - Processing single record for E-Invoice from Credit Note 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; + $description = $this->details['description'] ?? null; + $reason = $this->details['reason'] ?? null; + $deptNo = $this->details['deptno'] ?? null; + $qty = $this->details['qty'] ?? null; + $unitPrice = $this->details['unitprice'] ?? null; + $accNo = $this->details['accno'] ?? null; + $submitEinvoice = $this->details['submiteinvoice'] ?? null; + $einvoiceIssueDateTime = $this->details['einvoiceissuedatetime'] ?? null; + $consolidatedEinvoice = $this->details['consolidatedeinvoice'] ?? null; + $eInvoiceValidationLink = $this->details['einvoicevalidationlink'] ?? null; + + Log::info("Processing Credit Note Report:", [ + 'DocNo' => $docNo, + 'DocDate' => $docDate, + 'DebtorCode' => $debtorCode, + 'Ref' => $ref, + 'Description' => $description, + 'Reason' => $reason, + 'DeptNo' => $deptNo, + 'Qty' => $qty, + 'UnitPrice' => $unitPrice, + 'AccNo' => $accNo, + 'SubmitEinvoice' => $submitEinvoice, + 'EInvoiceIssueDateTime' => $einvoiceIssueDateTime, + 'ConsolidatedEinvoice' => $consolidatedEinvoice, + 'EInvoiceValidationLink' => $eInvoiceValidationLink, + ]); + + $booking = Booking::where('marking', $ref)->first(); + if($booking){ + if($docNo != "" && $docNo != "<>"){ + $payments = $booking->transactions()->payments()->get(); + foreach ($payments as $payment) { + $refundTransaction = $payment->transactions()->refunds()->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->latest()->first(); + if($refundTransaction){ + $this->updateOrCreateKeyValuePair($refundTransaction, KVPKey::AUTOCOUNT_DOCNO_CREDIT_NOTE, $docNo); + if($eInvoiceValidationLink){ + $this->updateOrCreateKeyValuePair($refundTransaction, KVPKey::AUTOCOUNT_EINVOICE_VALIDATION_LINK_CREDIT_NOTE, $eInvoiceValidationLink); + } + break; + } + } + } + } + + $end = new Carbon(); + $elapsedTime = $start->diff($end)->format('%H:%I:%S'); + Log::info(Carbon::now() . ': End job - Processing single record for E-Invoice from Credit Note 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/Services/AutoCountDataImport.php b/app/Classes/Modules/Imports/Services/AutoCountDataImport.php index 17016aeb..85d47a7b 100644 --- a/app/Classes/Modules/Imports/Services/AutoCountDataImport.php +++ b/app/Classes/Modules/Imports/Services/AutoCountDataImport.php @@ -9,6 +9,7 @@ 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 App\Classes\Jobs\Commands\V2\ProcessCreditNoteReportV2CommandJob; use Illuminate\Support\Facades\Log; class AutoCountDataImport implements ToCollection, WithHeadingRow, WithChunkReading @@ -40,10 +41,14 @@ class AutoCountDataImport implements ToCollection, WithHeadingRow, WithChunkRead if ($this->reportType === 'Sales Invoice Report') { ProcessSalesInvoiceReportV2CommandJob::dispatch($row->toArray()); - } elseif ($this->reportType === '01R - RECEIVE PAYMENT (FULL PAYMENT) [AR RECEIVE PAYMENT]') { + } + elseif ($this->reportType === '01R - RECEIVE PAYMENT (FULL PAYMENT) [AR RECEIVE PAYMENT]') { ProcessPaymentReportV2CommandJob::dispatch($row->toArray()); } - else{ + else if ($this->reportType === 'Credit Note Report') { + ProcessCreditNoteReportV2CommandJob::dispatch($row->toArray()); + } + else { throw new MalformedRequestException('Cannot process report type: ' . $this->reportType); } } @@ -77,6 +82,23 @@ class AutoCountDataImport implements ToCollection, WithHeadingRow, WithChunkRead 'knockoffdocno' ]; + $creditNoteReportHeader = [ + 'docno', + 'docdate', + 'debtorcode', + 'ref', + 'description', + 'reason', + 'deptno', + 'qty', + 'unitprice', + 'accno', + 'submiteinvoice', + 'einvoiceissuedatetime', + 'consolidatedeinvoice', + 'einvoicevalidationlink' + ]; + if ($reportType === 'Sales Invoice Report' && $header !== $salesInvoiceHeader && $header !== [...$salesInvoiceHeader, $optionalColumn]) { @@ -88,5 +110,8 @@ class AutoCountDataImport implements ToCollection, WithHeadingRow, WithChunkRead 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.'); } + elseif ($reportType === 'Credit Note Report' && $header !== $creditNoteReportHeader) { + throw new MalformedRequestException('Uploaded Excel file format is incorrect. Column headers do not match expected format.'); + } } }