1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

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()
{
/* Check again!! */
if (! $this->invoice->invoiceReversable($this->invoice)) {
if (! $this->invoice->invoiceReversable($this->invoice))
return $this->invoice;
}
/* 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();
}
$balance_remaining = $this->invoice->balance;
@ -57,18 +55,19 @@ class HandleReversal extends AbstractService
->get();
$paymentables->each(function ($paymentable) use ($total_paid) {
$reversable_amount = $paymentable->amount - $paymentable->refunded;
$total_paid -= $reversable_amount;
$paymentable->amount = $paymentable->refunded;
$paymentable->save();
});
/* Generate a credit for the $total_paid amount */
$notes = 'Credit for reversal of '.$this->invoice->number;
if ($total_paid > 0) {
$credit = CreditFactory::create($this->invoice->company_id, $this->invoice->user_id);
$credit->client_id = $this->invoice->client_id;
$credit->invoice_id = $this->invoice->id;
@ -79,16 +78,13 @@ class HandleReversal extends AbstractService
$item->notes = $notes;
$line_items[] = $item;
$credit->line_items = $line_items;
$credit->save();
$credit_calc = new InvoiceSum($credit);
$credit_calc->build();
$credit = $credit_calc->purgeTaxes()->getCredit();
$credit->service()->markSent()->save();
}
@ -116,7 +112,8 @@ class HandleReversal extends AbstractService
}
$this->invoice->balance = 0;
$this->invoice->paid_to_date = 0;
/* Set invoice status to reversed... somehow*/
$this->invoice->service()->setStatus(Invoice::STATUS_REVERSED)->save();

View File

@ -63,7 +63,38 @@ class EntityPaidToDateTest extends TestCase
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 = [
'name' => 'A Nice Client',
@ -125,17 +156,6 @@ class EntityPaidToDateTest extends TestCase
$invoice_one_hashed_id = $arr['data']['id'];
$invoice = 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);
return Invoice::find($this->decodePrimaryKey($invoice_one_hashed_id));
}
}