Tests for paid_to_date

This commit is contained in:
David Bomba 2021-01-24 20:08:57 +11:00
parent 6170dfae12
commit 0f8ee2d101
2 changed files with 40 additions and 23 deletions

View File

@ -38,14 +38,12 @@ class HandleReversal extends AbstractService
public function run() public function run()
{ {
/* Check again!! */ /* Check again!! */
if (! $this->invoice->invoiceReversable($this->invoice)) { if (! $this->invoice->invoiceReversable($this->invoice))
return $this->invoice; return $this->invoice;
}
/* If the invoice has been cancelled - we need to unwind the cancellation before reversing*/ /* If the invoice has been cancelled - we need to unwind the cancellation before reversing*/
if ($this->invoice->status_id == Invoice::STATUS_CANCELLED) { if ($this->invoice->status_id == Invoice::STATUS_CANCELLED)
$this->invoice = $this->invoice->service()->reverseCancellation()->save(); $this->invoice = $this->invoice->service()->reverseCancellation()->save();
}
$balance_remaining = $this->invoice->balance; $balance_remaining = $this->invoice->balance;
@ -57,18 +55,19 @@ class HandleReversal extends AbstractService
->get(); ->get();
$paymentables->each(function ($paymentable) use ($total_paid) { $paymentables->each(function ($paymentable) use ($total_paid) {
$reversable_amount = $paymentable->amount - $paymentable->refunded; $reversable_amount = $paymentable->amount - $paymentable->refunded;
$total_paid -= $reversable_amount; $total_paid -= $reversable_amount;
$paymentable->amount = $paymentable->refunded; $paymentable->amount = $paymentable->refunded;
$paymentable->save(); $paymentable->save();
}); });
/* Generate a credit for the $total_paid amount */ /* Generate a credit for the $total_paid amount */
$notes = 'Credit for reversal of '.$this->invoice->number; $notes = 'Credit for reversal of '.$this->invoice->number;
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;
@ -79,16 +78,13 @@ class HandleReversal extends AbstractService
$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();
} }
@ -116,7 +112,8 @@ class HandleReversal extends AbstractService
} }
$this->invoice->balance = 0; $this->invoice->balance = 0;
$this->invoice->paid_to_date = 0;
/* Set invoice status to reversed... somehow*/ /* Set invoice status to reversed... somehow*/
$this->invoice->service()->setStatus(Invoice::STATUS_REVERSED)->save(); $this->invoice->service()->setStatus(Invoice::STATUS_REVERSED)->save();

View File

@ -63,7 +63,38 @@ class EntityPaidToDateTest extends TestCase
public function testPaidToDateWithMarkPaidAction() public function testPaidToDateWithMarkPaidAction()
{ {
//create new client $invoice = $this->bootNewInvoice();
$this->assertEquals($invoice->balance, 0);
$this->assertEquals($invoice->paid_to_date, 0);
$invoice->service()->markSent()->save();
$this->assertEquals($invoice->balance, 20);
$invoice->service()->markPaid()->save();
$this->assertEquals($invoice->paid_to_date, 20);
}
public function testPaidToDateWithInvoiceCancellation()
{
$invoice = $this->bootNewInvoice();
$invoice->service()->markPaid()->save();
$this->assertEquals(20, $invoice->paid_to_date);
$invoice->service()->handleReversal()->save();
$this->assertEquals(0, $invoice->paid_to_date);
}
private function bootNewInvoice()
{
$data = [ $data = [
'name' => 'A Nice Client', 'name' => 'A Nice Client',
@ -125,17 +156,6 @@ class EntityPaidToDateTest extends TestCase
$invoice_one_hashed_id = $arr['data']['id']; $invoice_one_hashed_id = $arr['data']['id'];
$invoice = Invoice::find($this->decodePrimaryKey($invoice_one_hashed_id)); return Invoice::find($this->decodePrimaryKey($invoice_one_hashed_id));
$this->assertEquals($invoice->balance, 0);
$this->assertEquals($invoice->paid_to_date, 0);
$invoice->service()->markSent()->save();
$this->assertEquals($invoice->balance, 20);
$invoice->service()->markPaid()->save();
$this->assertEquals($invoice->paid_to_date, 20);
} }
} }