booking = $booking; } public function handle() { Log::info(Carbon::now() . ': Start job - Processing single booking for E-Invoice.'); $start = new Carbon(); $isAutocountDataExist = $this->booking->transactions() ->whereIn('transactions.type', [TransactionType::INVOICE]) ->whereHas('attributesKVP', function ($q) { $q->where('key', KVPKey::AUTOCOUNT_DOCNO_INVOICE); }) ->whereHas('attributesKVP', function ($q) { $q->where('key', KVPKey::AUTOCOUNT_EINVOICE_VALIDATION_LINK); }) ->first(); // $documents = $this->booking->documents()->whereIn('document_type', [DocumentType::INVOICE, DocumentType::DELIVER_ORDER, DocumentType::SUPPLIER_DELIVER_ORDER])->get(); $documents = $this->booking->documents()->whereIn('document_type', [DocumentType::EINVOICE, DocumentType::INVOICE])->get(); if ($documents->isEmpty() && $isAutocountDataExist) { Log::info("Processing for E-Invoice, booking id : " . $this->booking->marking); (App()->make(RegenerateInvoiceBookingV2Processor::class))->execute($this->booking); } else{ $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(); } } }