deletesTransaction = $deletesTransaction; $this->updatesBookingStatus = $updatesBookingStatus; $this->deletesDocument = $deletesDocument; $this->createInvoiceTransactionProcessor = $createInvoiceTransactionProcessor; } public function execute(Booking $booking, bool $isAllowNormalInvoice = false) { $this->updatesBookingStatus->execute($booking, ApprovalStatus::APPROVED); $firstInvoice = $booking->transactions() ->whereIn('type', [TransactionType::INVOICE]) ->withTrashed() ->orderBy('created_at', 'asc') ->first(); Log::info('RegenerateInvoiceBookingProcessor booking: ' . json_encode($booking->marking)); // get the first bill_no if($firstInvoice){ $firstBillNo = $firstInvoice->bill_no; 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(); } $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); } $document = $booking->documents()->whereIn('document_type', [DocumentType::PURCHASE_ORDER, DocumentType::INVOICE, DocumentType::DELIVER_ORDER, DocumentType::SUPPLIER_DELIVER_ORDER])->get(); foreach ($document as $key => $row) { $this->deletesDocument->execute($row); } $kvpCopies = $currentInvoice->attributesKVP->map(function ($kvp) { return $kvp->replicate(); }); $this->createInvoiceTransactionProcessor->execute($booking, $firstBillNo, $kvpCopies, true, $isAllowNormalInvoice); } else { $this->createInvoiceTransactionProcessor->execute($booking, "", null, false, $isAllowNormalInvoice); } } }