|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Models\User; |
| 4 | +use App\Modules\Core\Models\Tenant; |
| 5 | +use App\Modules\Finance\Models\BatchPayment; |
| 6 | +use App\Modules\Finance\Models\Contact; |
| 7 | +use App\Modules\Finance\Models\Invoice; |
| 8 | +use App\Modules\Finance\Models\Payment; |
| 9 | +use Database\Seeders\RolePermissionSeeder; |
| 10 | + |
| 11 | +beforeEach(function () { |
| 12 | + $this->seed(RolePermissionSeeder::class); |
| 13 | + $this->tenant = Tenant::create(['name' => 'Pay Co', 'slug' => 'pay-co-' . uniqid()]); |
| 14 | + $this->user = User::factory()->create(['tenant_id' => $this->tenant->id]); |
| 15 | + $this->user->assignRole('super-admin'); |
| 16 | + $this->token = $this->user->createToken('test')->plainTextToken; |
| 17 | + app()->instance('tenant', $this->tenant); |
| 18 | +}); |
| 19 | + |
| 20 | +function makeBatchInvoice(): Invoice |
| 21 | +{ |
| 22 | + $contact = Contact::create([ |
| 23 | + 'tenant_id' => test()->tenant->id, |
| 24 | + 'name' => 'Batch Client ' . uniqid(), |
| 25 | + 'type' => 'customer', |
| 26 | + ]); |
| 27 | + |
| 28 | + return Invoice::create([ |
| 29 | + 'tenant_id' => test()->tenant->id, |
| 30 | + 'contact_id' => $contact->id, |
| 31 | + 'issue_date' => now()->toDateString(), |
| 32 | + 'due_date' => now()->addDays(30)->toDateString(), |
| 33 | + 'status' => 'sent', |
| 34 | + ]); |
| 35 | +} |
| 36 | + |
| 37 | +test('can create a batch payment', function () { |
| 38 | + $inv1 = makeBatchInvoice(); |
| 39 | + $inv2 = makeBatchInvoice(); |
| 40 | + |
| 41 | + $this->withToken($this->token) |
| 42 | + ->postJson('/api/v1/batch-payments', [ |
| 43 | + 'payment_date' => now()->toDateString(), |
| 44 | + 'payment_method' => 'bank_transfer', |
| 45 | + 'type' => 'received', |
| 46 | + 'payments' => [ |
| 47 | + ['invoice_id' => $inv1->id, 'amount' => 500.00], |
| 48 | + ['invoice_id' => $inv2->id, 'amount' => 750.00], |
| 49 | + ], |
| 50 | + ]) |
| 51 | + ->assertStatus(201) |
| 52 | + ->assertJsonPath('data.type', 'received') |
| 53 | + ->assertJsonStructure(['data' => ['reference', 'total_amount', 'payments']]); |
| 54 | + |
| 55 | + expect(Payment::where('invoice_id', $inv1->id)->exists())->toBeTrue(); |
| 56 | + expect(Payment::where('invoice_id', $inv2->id)->exists())->toBeTrue(); |
| 57 | +}); |
| 58 | + |
| 59 | +test('total_amount is sum of individual payments', function () { |
| 60 | + $inv1 = makeBatchInvoice(); |
| 61 | + $inv2 = makeBatchInvoice(); |
| 62 | + |
| 63 | + $response = $this->withToken($this->token) |
| 64 | + ->postJson('/api/v1/batch-payments', [ |
| 65 | + 'payment_date' => now()->toDateString(), |
| 66 | + 'payment_method' => 'cash', |
| 67 | + 'type' => 'received', |
| 68 | + 'payments' => [ |
| 69 | + ['invoice_id' => $inv1->id, 'amount' => 200.00], |
| 70 | + ['invoice_id' => $inv2->id, 'amount' => 300.00], |
| 71 | + ], |
| 72 | + ]) |
| 73 | + ->assertStatus(201); |
| 74 | + |
| 75 | + expect((float) $response->json('data.total_amount'))->toBe(500.0); |
| 76 | +}); |
| 77 | + |
| 78 | +test('can list batch payments', function () { |
| 79 | + $inv = makeBatchInvoice(); |
| 80 | + BatchPayment::create([ |
| 81 | + 'tenant_id' => $this->tenant->id, |
| 82 | + 'reference' => 'BATCH-001', |
| 83 | + 'payment_date' => now()->toDateString(), |
| 84 | + 'payment_method' => 'bank_transfer', |
| 85 | + 'type' => 'received', |
| 86 | + 'total_amount' => 1000.00, |
| 87 | + ]); |
| 88 | + |
| 89 | + $response = $this->withToken($this->token) |
| 90 | + ->getJson('/api/v1/batch-payments') |
| 91 | + ->assertStatus(200); |
| 92 | + |
| 93 | + expect(count($response->json('data')))->toBeGreaterThanOrEqual(1); |
| 94 | +}); |
| 95 | + |
| 96 | +test('can view a batch payment with individual payments', function () { |
| 97 | + $inv = makeBatchInvoice(); |
| 98 | + |
| 99 | + $batch = BatchPayment::create([ |
| 100 | + 'tenant_id' => $this->tenant->id, |
| 101 | + 'reference' => 'BATCH-002', |
| 102 | + 'payment_date' => now()->toDateString(), |
| 103 | + 'payment_method' => 'cheque', |
| 104 | + 'type' => 'made', |
| 105 | + 'total_amount' => 500.00, |
| 106 | + ]); |
| 107 | + |
| 108 | + Payment::create([ |
| 109 | + 'tenant_id' => $this->tenant->id, |
| 110 | + 'invoice_id' => $inv->id, |
| 111 | + 'batch_payment_id' => $batch->id, |
| 112 | + 'amount' => 500.00, |
| 113 | + 'payment_date' => now()->toDateString(), |
| 114 | + 'method' => 'cheque', |
| 115 | + ]); |
| 116 | + |
| 117 | + $this->withToken($this->token) |
| 118 | + ->getJson("/api/v1/batch-payments/{$batch->id}") |
| 119 | + ->assertStatus(200) |
| 120 | + ->assertJsonStructure(['data' => ['reference', 'payments']]); |
| 121 | +}); |
| 122 | + |
| 123 | +test('can filter by type', function () { |
| 124 | + BatchPayment::create([ |
| 125 | + 'tenant_id' => $this->tenant->id, |
| 126 | + 'reference' => 'BATCH-REC', |
| 127 | + 'payment_date' => now()->toDateString(), |
| 128 | + 'payment_method' => 'cash', |
| 129 | + 'type' => 'received', |
| 130 | + 'total_amount' => 100.00, |
| 131 | + ]); |
| 132 | + |
| 133 | + BatchPayment::create([ |
| 134 | + 'tenant_id' => $this->tenant->id, |
| 135 | + 'reference' => 'BATCH-MADE', |
| 136 | + 'payment_date' => now()->toDateString(), |
| 137 | + 'payment_method' => 'cash', |
| 138 | + 'type' => 'made', |
| 139 | + 'total_amount' => 200.00, |
| 140 | + ]); |
| 141 | + |
| 142 | + $response = $this->withToken($this->token) |
| 143 | + ->getJson('/api/v1/batch-payments?type=received') |
| 144 | + ->assertStatus(200); |
| 145 | + |
| 146 | + foreach ($response->json('data') as $item) { |
| 147 | + expect($item['type'])->toBe('received'); |
| 148 | + } |
| 149 | +}); |
| 150 | + |
| 151 | +test('can get batch payment summary', function () { |
| 152 | + BatchPayment::create([ |
| 153 | + 'tenant_id' => $this->tenant->id, |
| 154 | + 'reference' => 'BATCH-SUM-1', |
| 155 | + 'payment_date' => now()->toDateString(), |
| 156 | + 'payment_method' => 'bank_transfer', |
| 157 | + 'type' => 'received', |
| 158 | + 'total_amount' => 1000.00, |
| 159 | + ]); |
| 160 | + |
| 161 | + $response = $this->withToken($this->token) |
| 162 | + ->getJson('/api/v1/batch-payments/summary') |
| 163 | + ->assertStatus(200) |
| 164 | + ->assertJsonStructure(['data' => ['total_received', 'total_made', 'batch_count_received']]); |
| 165 | + |
| 166 | + expect((float) $response->json('data.total_received'))->toBeGreaterThanOrEqual(1000.0); |
| 167 | +}); |
| 168 | + |
| 169 | +test('can delete a batch payment', function () { |
| 170 | + $batch = BatchPayment::create([ |
| 171 | + 'tenant_id' => $this->tenant->id, |
| 172 | + 'reference' => 'BATCH-DEL', |
| 173 | + 'payment_date' => now()->toDateString(), |
| 174 | + 'payment_method' => 'cash', |
| 175 | + 'type' => 'received', |
| 176 | + 'total_amount' => 100.00, |
| 177 | + ]); |
| 178 | + |
| 179 | + $this->withToken($this->token) |
| 180 | + ->deleteJson("/api/v1/batch-payments/{$batch->id}") |
| 181 | + ->assertStatus(200); |
| 182 | + |
| 183 | + expect(BatchPayment::withTrashed()->find($batch->id)?->deleted_at)->not->toBeNull(); |
| 184 | +}); |
| 185 | + |
| 186 | +test('requires authentication', function () { |
| 187 | + $this->getJson('/api/v1/batch-payments')->assertStatus(401); |
| 188 | +}); |
0 commit comments