payment = $payment; } /** * Handle the event. * * @param object $event * @return void */ public function handle() { $invoices = $this->payment->invoices()->get(); $invoices_total = $invoices->sum('balance'); /* Simplest scenario*/ if(strval($invoices_total) === strval($this->payment->amount)) { $invoices->each(function ($invoice){ UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1)); $invoice->clearPartial(); $invoice->updateBalance($invoice->balance*-1); }); } else { $total = 0; foreach($invoices as $invoice) { if($invoice->hasPartial()) $total += $invoice->partial; else $total += $invoice->balance; } /* test if there is a batch of partial invoices that have been paid */ if($this->payment->amount == $total) { $invoices->each(function ($invoice){ if($invoice->hasPartial()) { UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->partial*-1)); $invoice->updateBalance($invoice->partial*-1); $invoice->clearPartial(); $invoice->setDueDate(); //todo do we need to mark it as a partial? } else { UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1)); $invoice->clearPartial(); $invoice->updateBalance($invoice->balance*-1); } }); } else { /* $this->sysLog([ 'payment' => $this->payment, 'invoices' => $invoices, 'invoices_total' => $invoices_total, 'payment_amount' => $this->payment->amount, 'partial_check_amount' => $total, ], SystemLog::GATEWAY_RESPONSE, SystemLog::PAYMENT_RECONCILIATION_FAILURE, $this->payment->client); */ throw new \Exception("payment amount {$this->payment->amount} does not match invoice totals {$invoices_total}"); } } } } /* $this->payment = $event->payment; $invoice = $this->payment->invoice; $adjustment = $this->payment->amount * -1; $partial = max(0, $invoice->partial - $this->payment->amount); $invoice->updateBalances($adjustment, $partial); $invoice->updatePaidStatus(true); // store a backup of the invoice $activity = Activity::wherePaymentId($this->payment->id) ->whereActivityTypeId(ACTIVITY_TYPE_CREATE_PAYMENT) ->first(); $activity->json_backup = $invoice->hidePrivateFields()->toJSON(); $activity->save(); if ($invoice->balance == 0 && $this->payment->account->auto_archive_invoice) { $invoiceRepo = app('App\Ninja\Repositories\InvoiceRepository'); $invoiceRepo->archive($invoice); } */