From 32b1ca8cb8ee709be977b155e5c287912f47d5b0 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 26 Nov 2025 16:03:22 +1100 Subject: [PATCH] Handle edge cases --- .../StorePurchaseOrderRequest.php | 3 +- tests/Feature/CreditTest.php | 79 +++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/app/Http/Requests/PurchaseOrder/StorePurchaseOrderRequest.php b/app/Http/Requests/PurchaseOrder/StorePurchaseOrderRequest.php index 7dc3aaf271..ca23535a62 100644 --- a/app/Http/Requests/PurchaseOrder/StorePurchaseOrderRequest.php +++ b/app/Http/Requests/PurchaseOrder/StorePurchaseOrderRequest.php @@ -84,7 +84,8 @@ class StorePurchaseOrderRequest extends Request $input['amount'] = 0; $input['balance'] = 0; - + $input['total_taxes'] = 0; + if ($this->file('documents') instanceof \Illuminate\Http\UploadedFile) { $this->files->set('documents', [$this->file('documents')]); } diff --git a/tests/Feature/CreditTest.php b/tests/Feature/CreditTest.php index 22ebeb77f7..5cf4ecfe22 100644 --- a/tests/Feature/CreditTest.php +++ b/tests/Feature/CreditTest.php @@ -46,6 +46,85 @@ class CreditTest extends TestCase $this->makeTestData(); } + + public function testClientPaidToDateStateAfterCreditCreatedForPaidInvoice() + { + $c = Client::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'balance' => 0, + 'paid_to_date' => 0, + ]); + + $ii = new InvoiceItem(); + $ii->cost = 100; + $ii->quantity = 1; + $ii->product_key = 'xx'; + $ii->notes = 'yy'; + + $i = \App\Models\Invoice::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'client_id' => $c->id, + 'tax_name1' => '', + 'tax_name2' => '', + 'tax_name3' => '', + 'tax_rate1' => 0, + 'tax_rate2' => 0, + 'tax_rate3' => 0, + 'discount' => 0, + 'line_items' => [ + $ii + ], + 'status_id' => 1, + ]); + + $repo = new InvoiceRepository(); + $repo->save([], $i); + + $i = $i->calc()->getInvoice(); + $i = $i->service()->markPaid()->save(); //paid + + $payment = $i->payments()->first(); + + $this->assertNotNull($payment); + + $this->assertEquals(0, $i->balance); + $this->assertEquals(100, $i->amount); + + $credit_array = $i->withoutRelations()->toArray(); + $credit_array['invoice_id'] = $i->hashed_id; + $credit_array['client_id'] = $c->hashed_id; + unset($credit_array['backup']); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/credits', $credit_array); + + $response->assertStatus(200); //reversal - credit created. + + $arr = $response->json(); + $credit = \App\Models\Credit::find($this->decodePrimaryKey($arr['data']['id'])); + + $this->assertNotNull($credit); + $payment = $payment->fresh(); + + $i = $i->fresh(); + + $this->assertEquals(\App\Models\Invoice::STATUS_REVERSED, $i->status_id); + // $this->assertTrue($payment->credits()->exists()); + + $client = $i->client; + + $this->assertEquals(100, $client->credit_balance); + $this->assertEquals(0, $client->paid_to_date); + $this->assertEquals(0, $client->balance); + + } + + + public function testNewCreditDeletionAfterInvoiceReversalAndPaymentRefund() { $c = Client::factory()->create([