E-Invoice - Fix some issues reported by Mira on E-Invoice

This commit is contained in:
Dillon Ngo
2026-03-05 03:41:26 +08:00
parent 92981609e6
commit 5f1a5da363
2 changed files with 80 additions and 25 deletions
@@ -3,6 +3,7 @@
namespace App\Classes\Jobs\Commands\V2;
use App\Classes\Modules\Bookings\Processors\RegenerateInvoiceBookingV2Processor;
use App\Classes\Modules\Transactions\Services\DeletesTransaction;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Classes\ValueObjects\Constants\KVPKey;
use App\Classes\ValueObjects\Constants\TransactionType;
@@ -13,8 +14,9 @@ use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\Booking;
use App\Models\Transaction;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class ProcessBookingForEInvoiceV2CommandJob implements ShouldQueue
{
@@ -55,11 +57,53 @@ class ProcessBookingForEInvoiceV2CommandJob implements ShouldQueue
(App()->make(RegenerateInvoiceBookingV2Processor::class))->execute($this->booking);
}
else{
Log::info("NO Processing for E-Invoice, booking id : " . $this->booking->marking);
$firstInvoiceTrx = $this->booking->transactions()
->whereIn('type', [TransactionType::INVOICE])
->withTrashed()
->orderBy('created_at', 'asc')
->first();
if (Str::startsWith($firstInvoiceTrx->bill_no, 'EINV')) {
Log::info("Reset and reprocess for E-Invoice, booking id : " . $this->booking->marking);
$this->resetAndReprocess($firstInvoiceTrx);
(App()->make(RegenerateInvoiceBookingV2Processor::class))->execute($this->booking);
}
else{
Log::info("NO Processing for NORMAL Invoice, booking id : " . $this->booking->marking);
}
}
$end = new Carbon();
$elapsedTime = $start->diff($end)->format('%H:%I:%S');
Log::info(Carbon::now() . ': End job - Processing single booking for E-Invoice. ElapsedTime: ' . $elapsedTime . '.');
}
private function resetAndReprocess($firstInvoiceTrx){
//Get the first bill number for the first invoice transactions
$firstInvoiceTrxBillNo = '';
if ($firstInvoiceTrx && str_contains($firstInvoiceTrx->bill_no, '-deleted-')) {
$firstInvoiceTrxBillNo = explode('-deleted-', $firstInvoiceTrx->bill_no)[0];
}
//Rename all other invoice transactions bill number
$transactionWithSameBillNo = Transaction::where('bill_no', $firstInvoiceTrxBillNo)->withTrashed()->get();
if ($transactionWithSameBillNo) {
foreach ($transactionWithSameBillNo as $transaction) {
$transaction->bill_no = $transaction->bill_no . "-deleted-" . (string)(Carbon::now()->timestamp);
$transaction->save();
}
}
//Then delete all transactions of type invoice and supplier_deliver
$transaction = $this->booking->transactions()->whereIn('type', [TransactionType::INVOICE, TransactionType::SUPPLIER_DELIVER])->get();
foreach ($transaction as $key => $row) {
(App()->make(DeletesTransaction::class))->execute($row);
}
//Reinstate the first invoice transaction
if ($firstInvoiceTrx && $firstInvoiceTrx->trashed()) {
$firstInvoiceTrx->restore();
$firstInvoiceTrx->bill_no = explode('-deleted-', $firstInvoiceTrx->bill_no)[0];
$firstInvoiceTrx->save();
}
}
}
@@ -75,10 +75,13 @@ class RegenerateInvoiceBookingV2Processor
// get the first bill_no
if($firstInvoice){
$firstBillNo = $firstInvoice->bill_no;
$kvpCopies = null;
//for NORMAL INVOICE
if (!Str::startsWith($firstBillNo, 'EINV')) {
if (strpos($firstBillNo, '-deleted') !== false) {
$firstBillNo = substr($firstBillNo, 0, strpos($firstBillNo, '-deleted'));
}
// update currentInvoice bill_no to '-deleted-'
$currentInvoice = $booking->transactions()->where('type', TransactionType::INVOICE)->first();
if($currentInvoice){
@@ -99,15 +102,23 @@ class RegenerateInvoiceBookingV2Processor
$this->deletesTransaction->execute($row);
}
$kvpCopies = $latestInvoice->attributesKVP->map(function ($kvp) {
return $kvp->replicate();
});
}
//for E-INVOICE
else {
$transaction = $booking->transactions()->whereIn('type', [TransactionType::SUPPLIER_DELIVER])->get();
foreach ($transaction as $key => $row) {
$this->deletesTransaction->execute($row);
}
}
$document = $booking->documents()->whereIn('document_type', [DocumentType::PURCHASE_ORDER, DocumentType::INVOICE, DocumentType::EINVOICE, DocumentType::DELIVER_ORDER, DocumentType::SUPPLIER_DELIVER_ORDER])->get();
foreach ($document as $key => $row) {
$this->deletesDocument->execute($row);
}
$kvpCopies = $latestInvoice->attributesKVP->map(function ($kvp) {
return $kvp->replicate();
});
$this->createInvoiceTransactionProcessor->execute($booking, $firstBillNo, $kvpCopies, [
'generateEInvoice' => true,
'generateEInvoiceWithNormalInvoiceTemplate' => $eInvoiceWithNormalInvoiceTemplate,