Handle edge cases

This commit is contained in:
David Bomba 2025-11-26 16:03:22 +11:00
parent c5c3271a4a
commit 32b1ca8cb8
2 changed files with 81 additions and 1 deletions

View File

@ -84,7 +84,8 @@ class StorePurchaseOrderRequest extends Request
$input['amount'] = 0; $input['amount'] = 0;
$input['balance'] = 0; $input['balance'] = 0;
$input['total_taxes'] = 0;
if ($this->file('documents') instanceof \Illuminate\Http\UploadedFile) { if ($this->file('documents') instanceof \Illuminate\Http\UploadedFile) {
$this->files->set('documents', [$this->file('documents')]); $this->files->set('documents', [$this->file('documents')]);
} }

View File

@ -46,6 +46,85 @@ class CreditTest extends TestCase
$this->makeTestData(); $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() public function testNewCreditDeletionAfterInvoiceReversalAndPaymentRefund()
{ {
$c = Client::factory()->create([ $c = Client::factory()->create([