updatesTransactionStatus = $updatesTransactionStatus; $this->updateDoFromVTPortalProcessor = $updateDoFromVTPortalProcessor; $this->updateDoFromYDPortalProcessor = $updateDoFromYDPortalProcessor; $this->createStorageInvoiceDocTransactionProcessor = $createStorageInvoiceDocTransactionProcessor; } /** * @param $packingList * @param $invoice * @param $processCompletedInvoices * @throws \App\Classes\Exceptions\MalformedRequestException */ public function execute($packingList, $invoice = null, $processCompletedInvoices = false) { $result = false; $totalInvoicesAmountPaid = 0.00; $totalInvoicesAmount = 0.00; if($processCompletedInvoices){ //processCompletedInvoices, for manual processing of invoices, e.g. incomplete payment gateway finish with storage invoice $invoiceTransactions = $packingList->transactions()->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->whereIn('type', [TransactionType::SHIPPING_INVOICE, TransactionType::STORAGE_INVOICE])->get(); } else{ $invoiceTransactions = $packingList->transactions()->where('status', ApprovalStatus::APPROVED)->whereIn('type', [TransactionType::SHIPPING_INVOICE, TransactionType::STORAGE_INVOICE])->get(); } //Part 1: Process each single invoice type and get the total of all invoices (STORAGE + SHIPPING) foreach($invoiceTransactions as $invoiceTransaction){ $totalInvoiceAmountPaid = $invoiceTransaction->transactions->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->sum('amount'); if ($invoice && $invoice->type == $invoiceTransaction->type) { if(($invoice->amount - $totalInvoiceAmountPaid) < 0.01){ Log::channel('storage_invoices')->info('ReleaseGoodsToCustomerProcessor updatesTransactionStatus'); $this->updatesTransactionStatus->execute($invoice, ApprovalStatus::COMPLETED); //cief todo: remove the following if block if($invoice->type === TransactionType::STORAGE_INVOICE){ Log::channel('storage_invoices')->info('ReleaseGoodsToCustomerProcessor createStorageInvoiceDocTransactionProcessor'); $this->createStorageInvoiceDocTransactionProcessor->execute($packingList); } } } $totalInvoicesAmount = $totalInvoicesAmount + $invoiceTransaction->amount; $totalInvoicesAmountPaid = $totalInvoicesAmountPaid + $totalInvoiceAmountPaid; } Log::channel('storage_invoices')->info('Total invoice amount paid 1: '.$totalInvoicesAmount); //cief todo: to be removed Log::channel('storage_invoices')->info('Total invoice amount paid 2: '.$totalInvoicesAmountPaid); //cief todo: to be removed //Part 2: Based on the collected info for all the total of all invoices if (($totalInvoicesAmount - $totalInvoicesAmountPaid) < 0.01 && $totalInvoicesAmountPaid > 0.01) { Log::channel('storage_invoices')->info('Total invoice amount paid 3: '.$totalInvoicesAmountPaid); //cief todo: to be removed $packingList->status = ApprovalStatus::APPROVED; $packingList->save(); if (app()->environment('production')) { $this->updateDoFromVTPortalProcessor->execute($packingList); $this->updateDoFromYDPortalProcessor->execute($packingList); } $result = true; } return $result; } }