diff --git a/app/Jobs/Invoice/MarkInvoicePaid.php b/app/Jobs/Invoice/MarkInvoicePaid.php index e49541258b4f..59ae937ec5c3 100644 --- a/app/Jobs/Invoice/MarkInvoicePaid.php +++ b/app/Jobs/Invoice/MarkInvoicePaid.php @@ -63,11 +63,9 @@ class MarkInvoicePaid implements ShouldQueue $payment->save(); /* Update Invoice balance */ - //$invoice = ApplyPaymentToInvoice::dispatchNow($payment, $this->invoice); event(new PaymentWasCreated($payment)); UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment->amount*-1)); - $this->invoice->updateBalance(($payment->amount*-1)); return $this->invoice; } diff --git a/app/Jobs/Invoice/UpdateInvoicePayment.php b/app/Jobs/Invoice/UpdateInvoicePayment.php new file mode 100644 index 000000000000..e486e5fbce73 --- /dev/null +++ b/app/Jobs/Invoice/UpdateInvoicePayment.php @@ -0,0 +1,142 @@ +payment = $payment; + } + + /** + * Handle the event. + * + * @param object $event + * @return void + */ + public function handle() + { + + $invoices = $this->payment->invoices(); + + $invoices_total = $invoices->sum('balance'); + + /* Simplest scenario*/ + if($invoices_total == $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($invoice as $invoice) + { + + if($invoice->hasPartial()) + $total += $invoice->partial; + else + $total += $invoice->balance; + + Log::error("total = {$total}"); + } + + /* 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); + + throw new Exception('payment amount does not match invoice totals'); + } + + + } + } +} + +/* + $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); + } +*/ \ No newline at end of file diff --git a/app/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/PaymentDrivers/PayPalExpressPaymentDriver.php index 176aedc82724..986e26e59b17 100644 --- a/app/PaymentDrivers/PayPalExpressPaymentDriver.php +++ b/app/PaymentDrivers/PayPalExpressPaymentDriver.php @@ -12,6 +12,7 @@ namespace App\PaymentDrivers; use App\Events\Payment\PaymentWasCreated; +use App\Jobs\Invoice\UpdateInvoicePayment; use App\Models\ClientGatewayToken; use App\Models\GatewayType; use App\Models\Payment; @@ -138,6 +139,8 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver $this->attachInvoices($payment, $request->input('hashed_ids')); event(new PaymentWasCreated($payment)); + + UpdateInvoicePayment::dispatchNow($payment); return redirect()->route('client.payments.show', ['payment'=>$this->encodePrimaryKey($payment->id)]); diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 246c37893641..21e252b34f53 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -13,6 +13,7 @@ namespace App\PaymentDrivers; use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; +use App\Jobs\Invoice\UpdateInvoicePayment; use App\Models\ClientGatewayToken; use App\Models\GatewayType; use App\Models\Invoice; @@ -362,6 +363,8 @@ class StripePaymentDriver extends BasePaymentDriver event(new PaymentWasCreated($payment)); + UpdateInvoicePayment::dispatchNow($payment); + return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); } @@ -369,12 +372,10 @@ class StripePaymentDriver extends BasePaymentDriver { /*Fail and log*/ - $log = [ + $this->sysLog([ 'server_response' => $server_response, 'invoices' => $invoices, - ]; - - $this->sysLog($log); + ]); throw new Exception("Failed to process payment", 1);