diff --git a/app/Classes/Jobs/Commands/V2/ProcessBookingForEInvoiceV2CommandJob.php b/app/Classes/Jobs/Commands/V2/ProcessBookingForEInvoiceV2CommandJob.php index 11d87cef..e69f6923 100644 --- a/app/Classes/Jobs/Commands/V2/ProcessBookingForEInvoiceV2CommandJob.php +++ b/app/Classes/Jobs/Commands/V2/ProcessBookingForEInvoiceV2CommandJob.php @@ -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(); + } + } } diff --git a/app/Classes/Modules/Bookings/Processors/RegenerateInvoiceBookingV2Processor.php b/app/Classes/Modules/Bookings/Processors/RegenerateInvoiceBookingV2Processor.php index 3e143687..2e758c81 100644 --- a/app/Classes/Modules/Bookings/Processors/RegenerateInvoiceBookingV2Processor.php +++ b/app/Classes/Modules/Bookings/Processors/RegenerateInvoiceBookingV2Processor.php @@ -75,28 +75,43 @@ class RegenerateInvoiceBookingV2Processor // get the first bill_no if($firstInvoice){ $firstBillNo = $firstInvoice->bill_no; - if (strpos($firstBillNo, '-deleted') !== false) { - $firstBillNo = substr($firstBillNo, 0, strpos($firstBillNo, '-deleted')); - } + $kvpCopies = null; - // update currentInvoice bill_no to '-deleted-' - $currentInvoice = $booking->transactions()->where('type', TransactionType::INVOICE)->first(); - if($currentInvoice){ - $currentInvoice->bill_no = $currentInvoice->bill_no ."-deleted-" . (string)(Carbon::now()->timestamp); - $currentInvoice->save(); - } - - $transactionWithSameBillNo = Transaction::where('bill_no', $firstBillNo)->withTrashed()->get(); - if ($transactionWithSameBillNo) { - foreach ($transactionWithSameBillNo as $transaction) { - $transaction->bill_no = $transaction->bill_no . "-deleted-" . Str::random(10); - $transaction->save(); + //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){ + $currentInvoice->bill_no = $currentInvoice->bill_no ."-deleted-" . (string)(Carbon::now()->timestamp); + $currentInvoice->save(); } - } - $transaction = $booking->transactions()->whereIn('type', [TransactionType::INVOICE, TransactionType::SUPPLIER_DELIVER])->get(); - foreach ($transaction as $key => $row) { - $this->deletesTransaction->execute($row); + $transactionWithSameBillNo = Transaction::where('bill_no', $firstBillNo)->withTrashed()->get(); + if ($transactionWithSameBillNo) { + foreach ($transactionWithSameBillNo as $transaction) { + $transaction->bill_no = $transaction->bill_no . "-deleted-" . Str::random(10); + $transaction->save(); + } + } + + $transaction = $booking->transactions()->whereIn('type', [TransactionType::INVOICE, TransactionType::SUPPLIER_DELIVER])->get(); + foreach ($transaction as $key => $row) { + $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(); @@ -104,10 +119,6 @@ class RegenerateInvoiceBookingV2Processor $this->deletesDocument->execute($row); } - $kvpCopies = $latestInvoice->attributesKVP->map(function ($kvp) { - return $kvp->replicate(); - }); - $this->createInvoiceTransactionProcessor->execute($booking, $firstBillNo, $kvpCopies, [ 'generateEInvoice' => true, 'generateEInvoiceWithNormalInvoiceTemplate' => $eInvoiceWithNormalInvoiceTemplate,