Fixes for reversals

This commit is contained in:
David Bomba 2021-11-06 17:07:00 +11:00
parent ecdd73fbcc
commit bbf2168a31

View File

@ -56,8 +56,15 @@ class HandleReversal extends AbstractService
$paymentables->each(function ($paymentable) use ($total_paid) { $paymentables->each(function ($paymentable) use ($total_paid) {
//new concept - when reversing, we unwind the payments
$payment = Payment::find($paymentable->payment_id);
$reversable_amount = $paymentable->amount - $paymentable->refunded; $reversable_amount = $paymentable->amount - $paymentable->refunded;
$total_paid -= $reversable_amount; $total_paid -= $reversable_amount;
$payment->applied -= $reversable_amount;
$payment->save();
$paymentable->amount = $paymentable->refunded; $paymentable->amount = $paymentable->refunded;
$paymentable->save(); $paymentable->save();
@ -67,45 +74,45 @@ class HandleReversal extends AbstractService
$notes = 'Credit for reversal of '.$this->invoice->number; $notes = 'Credit for reversal of '.$this->invoice->number;
$credit = false; $credit = false;
if ($total_paid > 0) { // if ($total_paid > 0) {
$credit = CreditFactory::create($this->invoice->company_id, $this->invoice->user_id); // $credit = CreditFactory::create($this->invoice->company_id, $this->invoice->user_id);
$credit->client_id = $this->invoice->client_id; // $credit->client_id = $this->invoice->client_id;
$credit->invoice_id = $this->invoice->id; // $credit->invoice_id = $this->invoice->id;
$credit->date = now(); // $credit->date = now();
$item = InvoiceItemFactory::create(); // $item = InvoiceItemFactory::create();
$item->quantity = 1; // $item->quantity = 1;
$item->cost = (float) $total_paid; // $item->cost = (float) $total_paid;
$item->notes = $notes; // $item->notes = $notes;
$line_items[] = $item; // $line_items[] = $item;
$credit->line_items = $line_items; // $credit->line_items = $line_items;
$credit->save(); // $credit->save();
$credit_calc = new InvoiceSum($credit); // $credit_calc = new InvoiceSum($credit);
$credit_calc->build(); // $credit_calc->build();
$credit = $credit_calc->purgeTaxes()->getCredit(); // $credit = $credit_calc->purgeTaxes()->getCredit();
$credit->service()->markSent()->save(); // $credit->service()->markSent()->save();
} // }
/*If there is a payment linked, then the credit needs to be linked back to that payment in case of refund*/ /*If there is a payment linked, then the credit needs to be linked back to that payment in case of refund*/
if ($paymentables->count() > 0 && $credit) { if ($paymentables->count() > 0 && $credit) {
$payment = $paymentables->first()->payment; // $payment = $paymentables->first()->payment;
$payment->credits()->save($credit); // $payment->credits()->save($credit);
$paymentable_credit = $payment->credits() // $paymentable_credit = $payment->credits()
->wherePaymentableType(Credit::class) // ->wherePaymentableType(Credit::class)
->wherePaymentableId($credit->id) // ->wherePaymentableId($credit->id)
->first(); // ->first();
//harvest the credit record and add in the amount for the credit. // //harvest the credit record and add in the amount for the credit.
$paymentable_credit->pivot->amount = $total_paid; // $paymentable_credit->pivot->amount = $total_paid;
$paymentable_credit->pivot->save(); // $paymentable_credit->pivot->save();
$paymentable_credit->paid_to_date += $total_paid; // $paymentable_credit->paid_to_date += $total_paid;
$paymentable_credit->save(); // $paymentable_credit->save();
} }
/* Set invoice balance to 0 */ /* Set invoice balance to 0 */
@ -124,7 +131,7 @@ class HandleReversal extends AbstractService
$this->invoice->client->service() $this->invoice->client->service()
->updateBalance($balance_remaining * -1) ->updateBalance($balance_remaining * -1)
->updatePaidToDate($total_paid * -1) // ->updatePaidToDate($total_paid * -1)
->save(); ->save();
event(new InvoiceWasReversed($this->invoice, $this->invoice->company, Ninja::eventVars())); event(new InvoiceWasReversed($this->invoice, $this->invoice->company, Ninja::eventVars()));