topnotch_ordersystem/tests/Unit/InvoiceTest.php

117 lines
4.2 KiB
PHP
Raw Normal View History

2025-01-19 12:51:37 -05:00
<?php
use App\Enums\InvoiceStatus;
use App\Filament\Admin\Resources\InvoiceResource\Pages\CreateInvoice;
use App\Models\Customer;
use App\Models\Invoice;
use App\Models\Order;
use App\Models\ProductService;
use App\Models\TaxRate;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('can create an invoice', function () {
$user = User::factory(['is_admin' => true])->create();
$this->actingAs($user);
$customer = Customer::factory()->create(); // Generates a customer
$pst_rate = TaxRate::where('name', 'PST')->value('value') ?? 0;
$gst_rate = TaxRate::where('name', 'GST')->value('value') ?? 0;
$formData = [
'customer_id' => $customer->id,
'date' => now()->toDateString(),
'due_date' => now()->addDays(7)->toDateString(),
'status' => InvoiceStatus::UNPAID->value,
'has_gst' => true,
'has_pst' => true,
];
$this->livewire(CreateInvoice::class)
->fillForm($formData)
->call('create')
->assertHasNoErrors();
$this->assertDatabaseHas('invoices', [
'internal_id' => 'INV400001',
'customer_id' => $formData['customer_id'],
'status' => $formData['status'],
'has_gst' => $formData['has_gst'],
'has_pst' => $formData['has_pst'],
'gst_rate' => $gst_rate,
'pst_rate' => $pst_rate,
]);
$invoice = Invoice::where('internal_id', 'INV400001')->firstOrFail();
$this->assertEquals($invoice->orders->isEmpty(), true);
});
it('can add orders to an invoice', function () {
$customer = Customer::factory()->create();
$invoice = Invoice::factory()->create(['customer_id' => $customer->id]);
$orders = Order::factory()->for($customer)->count(3)->create();
$invoice->orders()->saveMany($orders);
$this->assertEquals($invoice->orders->count(), 3);
});
it('correctly calculates tax amounts', function () {
$customer = Customer::factory()->create();
$order = Order::factory()->for($customer)->create();
$services = ProductService::factory()->count(3)->create(['order_id' => $order->id]);
$invoice = Invoice::factory(['has_gst' => true, 'has_pst' => true])->create(['customer_id' => $customer->id]);
$order->invoice()->associate($invoice);
$order->save();
$subtotal = $order->total_service_price;
$pst_amount = $subtotal * $invoice->pst_rate / 100;
$gst_amount = $subtotal * $invoice->gst_rate / 100;
$total = $subtotal + $pst_amount + $gst_amount;
$this->assertSame($invoice->subtotal, $subtotal);
$this->assertSame($invoice->gst_amount, $gst_amount);
$this->assertSame($invoice->pst_amount, $pst_amount);
$this->assertSame($invoice->total, $total);
});
it('correctly re-calculates tax amounts when an order gets added', function () {
$customer = Customer::factory()->create();
$order = Order::factory()->for($customer)->create();
$services = ProductService::factory()->count(3)->create(['order_id' => $order->id]);
$invoice = Invoice::factory(['has_gst' => true, 'has_pst' => true])->create(['customer_id' => $customer->id]);
$order->invoice()->associate($invoice);
$order->save();
$newService = ProductService::factory()->create(['order_id' => $order->id]);
$subtotal = $order->total_service_price;
$pst_amount = $subtotal * $invoice->pst_rate / 100;
$gst_amount = $subtotal * $invoice->gst_rate / 100;
$total = $subtotal + $pst_amount + $gst_amount;
$invoice->refresh();
$this->assertSame($invoice->subtotal, $subtotal);
$this->assertSame($invoice->gst_amount, $gst_amount);
$this->assertSame($invoice->pst_amount, $pst_amount);
$this->assertSame($invoice->total, $total);
});
it('correctly re-calculates tax amounts when an order gets removed', function () {});
it('correctly re-calculates tax amounts when an order\'s product services get added', function () {});
it('correctly re-calculates tax amounts when an order\'s product service\'s quantity gets changed', function () {});
it('correctly re-calculates tax amounts when an order\'s product service\'s amount gets changed', function () {});
it('correctly re-calculates tax amounts when an order\'s product services get removed', function () {});