From 730e0a17acdfe3ab1046dcc26aa2900147064bb2 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 18 Feb 2024 15:06:20 +1100 Subject: [PATCH] Tests for using Unapplied Payments --- app/Services/Invoice/AutoBillInvoice.php | 16 +- .../Payments/AutoUnappliedPaymentTest.php | 180 ++++++++++++++++++ 2 files changed, 184 insertions(+), 12 deletions(-) create mode 100644 tests/Feature/Payments/AutoUnappliedPaymentTest.php diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 22eacad77639..549efee15b1d 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -68,7 +68,9 @@ class AutoBillInvoice extends AbstractService $this->applyCreditPayment(); } + nlog($this->client->getSetting('use_unapplied_payment')); if($this->client->getSetting('use_unapplied_payment') != 'off') { + nlog("meeeeeeerp"); $this->applyUnappliedPayment(); } @@ -182,9 +184,6 @@ class AutoBillInvoice extends AbstractService $payment->amount = 0; $payment->applied = 0; - - // $payment->amount = $amount; - // $payment->applied = $amount; $payment->client_id = $this->invoice->client_id; $payment->currency_id = $this->invoice->client->getSetting('currency_id'); $payment->date = now()->addSeconds($this->invoice->company->utc_offset())->format('Y-m-d'); @@ -259,7 +258,8 @@ class AutoBillInvoice extends AbstractService */ private function applyUnappliedPayment(): self { - $unapplied_payments = Payment::query()->where('client_id', $this->client->id) + $unapplied_payments = Payment::query() + ->where('client_id', $this->client->id) ->where('status_id', Payment::STATUS_COMPLETED) ->where('is_deleted', false) ->where('amount', '>', 'applied') @@ -408,14 +408,6 @@ class AutoBillInvoice extends AbstractService })->orderBy('is_default', 'DESC') ->get(); - // $gateway_tokens = $this->client - // ->gateway_tokens() - // ->whereHas('gateway', function ($query) { - // $query->where('is_deleted', 0) - // ->where('deleted_at', null); - // })->orderBy('is_default', 'DESC') - // ->get(); - $filtered_gateways = $gateway_tokens->filter(function ($gateway_token) use ($amount) { $company_gateway = $gateway_token->gateway; diff --git a/tests/Feature/Payments/AutoUnappliedPaymentTest.php b/tests/Feature/Payments/AutoUnappliedPaymentTest.php new file mode 100644 index 000000000000..96e3a967a22f --- /dev/null +++ b/tests/Feature/Payments/AutoUnappliedPaymentTest.php @@ -0,0 +1,180 @@ +faker = \Faker\Factory::create(); + + Model::reguard(); + + $this->makeTestData(); + // $this->withoutExceptionHandling(); + + $this->withoutMiddleware( + ThrottleRequests::class + ); + } + + public function testUnappliedPaymentsAreEnabled() + { + + $settings = ClientSettings::defaults(); + $settings->use_unapplied_payment = 'always'; + + $client = Client::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'settings' => $settings, + ]); + + $this->assertEquals('always', $client->settings->use_unapplied_payment); + + $invoice = Invoice::factory()->for($client)->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'auto_bill_enabled' => true, + 'client_id' => $client->id, + ]); + + $invoice = $invoice->calc()->getInvoice(); + + $payment = Payment::factory()->for($client)->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'amount' => 100, + 'applied' => 0, + 'refunded' => 0, + 'status_id' => Payment::STATUS_COMPLETED, + 'is_deleted' => 0, + ]); + + $invoice->service()->markSent()->save(); + + $this->assertGreaterThan(0, $invoice->balance); + + nlog($invoice->balance); + + try{ + $invoice->service()->autoBill()->save(); + } + catch(\Exception $e){ + + } + + $invoice = $invoice->fresh(); + $payment = $payment->fresh(); + + nlog($invoice->toArray()); + nlog($payment->toArray()); + + $this->assertEquals($payment->applied, $invoice->paid_to_date); + $this->assertGreaterThan(2, $invoice->status_id); + $this->assertGreaterThan(0, $payment->applied); + + // $this->assertEquals(Invoice::STATUS_PAID, $invoice->status_id); + // $this->assertEquals(0, $invoice->balance); + + } + + + public function testUnappliedPaymentsAreDisabled() + { + + $settings = ClientSettings::defaults(); + $settings->use_unapplied_payment = 'off'; + + $client = Client::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'settings' => $settings, + ]); + + $this->assertEquals('off', $client->settings->use_unapplied_payment); + + $invoice = Invoice::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'auto_bill_enabled' => true, + 'status_id' => 2 + ]); + $invoice = $invoice->calc()->getInvoice(); + $invoice_balance = $invoice->balance; + + $payment = Payment::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'client_id' => $client->id, + 'amount' => 100, + 'applied' => 0, + 'refunded' => 0, + 'status_id' => Payment::STATUS_COMPLETED + ]); + + $invoice->service()->markSent()->save(); + + $this->assertGreaterThan(0, $invoice->balance); + + try { + $invoice->service()->autoBill()->save(); + } + catch(\Exception $e) { + + } + + $invoice = $invoice->fresh(); + $payment = $payment->fresh(); + + $this->assertEquals($invoice_balance, $invoice->balance); + $this->assertEquals(0, $payment->applied); + $this->assertEquals(2, $invoice->status_id); + $this->assertEquals(0, $invoice->paid_to_date); + $this->assertEquals($invoice->amount, $invoice->balance); + + // $this->assertEquals($payment->applied, $invoice->paid_to_date); + // $this->assertEquals(2, $invoice->status_id); + + + } + +} \ No newline at end of file