outputArray = []; $approvalStatusArray = ApprovalStatus::APPROVAL_STATUS_ID; $transactions = Transaction::where('type', TransactionType::PAYMENT) ->where('payment_method', PaymentMethodType::PAYMENT_GATEWAY) ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) ->get(); foreach ($transactions as $payment) { $invoice = $payment->owner ?? null; if (!$invoice) { Log::info('Payment has no owner. Payment ID: ' . $payment->id); continue; } $packingList = $invoice->owner ?? null; if (!$packingList) { Log::info('Invoice has no owner. Invoice ID: ' . $invoice->id); continue; } $order = $packingList->owner; if (!$invoice) { Log::info('PackingList has no owner. PackingList ID: ' . $packingList->id); continue; } if ($invoice->status == ApprovalStatus::APPROVED) { $totalPaidAmount = $invoice->transactions->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->sum('amount'); if(($invoice->amount - $totalPaidAmount) < 0.01) { (App()->make(UpdatesTransactionStatus::class))->execute($invoice, ApprovalStatus::COMPLETED); $packingList->status = ApprovalStatus::APPROVED; $packingList->save(); if(app()->environment('production')){ (App()->make(UpdateDoFromVTPortalProcessor::class))->execute($packingList); (App()->make(UpdateDoFromYDPortalProcessor::class))->execute($packingList); } dump('Updated invoice ' . $invoice->id . '. Order: '. $order->reference); } else { Log::info('Failed Update invoice ' . $invoice->id . ' because payment not enough. Invoice Amount: ' . $invoice->amount . ' . Paid Amount: ' . $totalPaidAmount . ' . Order: '. $order->reference); } } } $end = new Carbon(); $elapsedTime = $start->diff($end)->format('%H:%I:%S'); Log::info(Carbon::now() . ': End job - Success Updated Payment Status. ElapsedTime: ' . $elapsedTime . '.'); } }