From 1d6b7d3b55a2fd7961440ebf800727a03594170a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 17 Nov 2022 17:38:27 +1100 Subject: [PATCH] Late fee tests --- tests/Unit/AutoBillInvoiceTest.php | 23 -------- tests/Unit/LateFeeTest.php | 86 ++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 23 deletions(-) create mode 100644 tests/Unit/LateFeeTest.php diff --git a/tests/Unit/AutoBillInvoiceTest.php b/tests/Unit/AutoBillInvoiceTest.php index df73de11f07a..aa8ef0281e66 100644 --- a/tests/Unit/AutoBillInvoiceTest.php +++ b/tests/Unit/AutoBillInvoiceTest.php @@ -48,27 +48,4 @@ class AutoBillInvoiceTest extends TestCase $this->assertEquals($this->client->fresh()->credit_balance, 0); } - // public function testAutoBillSetOffFunctionality() - // { - - // $settings = $this->company->settings; - // $settings->use_credits_payment = 'off'; - - // $this->company->settings = $settings; - // $this->company->save(); - - // $this->assertEquals($this->client->balance, 10); - // $this->assertEquals($this->client->paid_to_date, 0); - // $this->assertEquals($this->client->credit_balance, 10); - - // $this->invoice->service()->markSent()->autoBill()->save(); - - // $this->assertNotNull($this->invoice->payments()); - // $this->assertEquals(0, $this->invoice->payments()->sum('payments.amount')); - - // $this->assertEquals($this->client->balance, 10); - // $this->assertEquals($this->client->paid_to_date, 0); - // $this->assertEquals($this->client->credit_balance, 10); - - // } } diff --git a/tests/Unit/LateFeeTest.php b/tests/Unit/LateFeeTest.php new file mode 100644 index 000000000000..28ef22a50d4e --- /dev/null +++ b/tests/Unit/LateFeeTest.php @@ -0,0 +1,86 @@ +makeTestData(); + } + + public function testLateFeeBalances() + { + + $this->assertEquals(10, $this->client->balance); + $this->assertEquals(10, $this->invoice->balance); + + $this->invoice = $this->setLateFee($this->invoice, 5, 0); + + $this->assertEquals(15, $this->client->fresh()->balance); + $this->assertEquals(15, $this->invoice->fresh()->balance); + + } + + private function setLateFee($invoice, $amount, $percent) :Invoice + { + + $temp_invoice_balance = $invoice->balance; + + if ($amount <= 0 && $percent <= 0) { + return $invoice; + } + + $fee = $amount; + + if ($invoice->partial > 0) { + $fee += round($invoice->partial * $percent / 100, 2); + } else { + $fee += round($invoice->balance * $percent / 100, 2); + } + + $invoice_item = new InvoiceItem; + $invoice_item->type_id = '5'; + $invoice_item->product_key = trans('texts.fee'); + $invoice_item->notes = ctrans('texts.late_fee_added', ['date' => now()]); + $invoice_item->quantity = 1; + $invoice_item->cost = $fee; + + $invoice_items = $invoice->line_items; + $invoice_items[] = $invoice_item; + + $invoice->line_items = $invoice_items; + + /**Refresh Invoice values*/ + $invoice = $invoice->calc()->getInvoice(); + + $invoice->client->service()->updateBalance($invoice->balance - $temp_invoice_balance)->save(); + $invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}"); + + return $invoice; + } + +}