2025-01-19 12:51:37 -05:00

62 lines
1.9 KiB
PHP

<?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\TaxRate;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
uses(DatabaseMigrations::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