浏览代码

update TransactionTest.php

3.x
Andrew Wallo 4 个月前
父节点
当前提交
e16fefa2e5
共有 1 个文件被更改,包括 40 次插入36 次删除
  1. 40
    36
      tests/Feature/Accounting/TransactionTest.php

+ 40
- 36
tests/Feature/Accounting/TransactionTest.php 查看文件

9
 use App\Models\Accounting\Account;
9
 use App\Models\Accounting\Account;
10
 use App\Models\Accounting\Transaction;
10
 use App\Models\Accounting\Transaction;
11
 use App\Utilities\Currency\ConfigureCurrencies;
11
 use App\Utilities\Currency\ConfigureCurrencies;
12
+use App\Utilities\Currency\CurrencyConverter;
12
 use Filament\Tables\Actions\DeleteAction;
13
 use Filament\Tables\Actions\DeleteAction;
13
 use Filament\Tables\Actions\DeleteBulkAction;
14
 use Filament\Tables\Actions\DeleteBulkAction;
14
 use Filament\Tables\Actions\ReplicateAction;
15
 use Filament\Tables\Actions\ReplicateAction;
78
         ->create();
79
         ->create();
79
 
80
 
80
     expect($transaction)
81
     expect($transaction)
81
-        ->journalEntries->sumDebits()->getValue()->toEqual($amount)
82
-        ->journalEntries->sumCredits()->getValue()->toEqual($amount);
82
+        ->journalEntries->sumDebits()->getAmount()->toEqual($amount)
83
+        ->journalEntries->sumCredits()->getAmount()->toEqual($amount);
83
 })->with([
84
 })->with([
84
     ['asDeposit', 'forUncategorizedRevenue', 2000],
85
     ['asDeposit', 'forUncategorizedRevenue', 2000],
85
     ['asWithdrawal', 'forUncategorizedExpense', 500],
86
     ['asWithdrawal', 'forUncategorizedExpense', 500],
122
     $expectedUSDValue = 1500;
123
     $expectedUSDValue = 1500;
123
 
124
 
124
     expect($transaction)
125
     expect($transaction)
125
-        ->amount->toEqual('1,500.00')
126
+        ->amount->toBe(1500)
126
         ->journalEntries->count()->toBe(2)
127
         ->journalEntries->count()->toBe(2)
127
-        ->journalEntries->sumDebits()->getValue()->toEqual($expectedUSDValue)
128
-        ->journalEntries->sumCredits()->getValue()->toEqual($expectedUSDValue);
128
+        ->journalEntries->sumDebits()->getAmount()->toEqual($expectedUSDValue)
129
+        ->journalEntries->sumCredits()->getAmount()->toEqual($expectedUSDValue);
129
 });
130
 });
130
 
131
 
131
 it('handles multi-currency transfers correctly', function () {
132
 it('handles multi-currency transfers correctly', function () {
150
     expect($debitAccount->name)->toBe('Destination Bank Account') // Debit: Destination (USD) account
151
     expect($debitAccount->name)->toBe('Destination Bank Account') // Debit: Destination (USD) account
151
         ->and($creditAccount->is($foreignBankAccount))->toBeTrue(); // Credit: Foreign Bank Account (CAD) account
152
         ->and($creditAccount->is($foreignBankAccount))->toBeTrue(); // Credit: Foreign Bank Account (CAD) account
152
 
153
 
153
-    // The 1500 CAD is worth 1102.94 USD (1500 CAD / 1.36)
154
-    $expectedUSDValue = round(1500 / 1.36, 2);
154
+    // The 1500 CAD is worth approximately 1103 USD (1500 CAD / 1.36)
155
+    $expectedUSDValue = CurrencyConverter::convertBalance(1500, 'CAD', 'USD');
155
 
156
 
156
-    // Verify that the debit is 1102.94 USD and the credit is 1500 CAD converted to 1102.94 USD
157
-    // Transaction amount stays in source bank account currency (cast is applied)
157
+    // Verify that the debit and credit are converted to USD cents
158
+    // Transaction amount stays in source bank account currency
158
     expect($transaction)
159
     expect($transaction)
159
-        ->amount->toEqual('1,500.00')
160
+        ->amount->toBe(1500)
160
         ->journalEntries->count()->toBe(2)
161
         ->journalEntries->count()->toBe(2)
161
-        ->journalEntries->sumDebits()->getValue()->toEqual($expectedUSDValue)
162
-        ->journalEntries->sumCredits()->getValue()->toEqual($expectedUSDValue);
162
+        ->journalEntries->sumDebits()->getAmount()->toEqual($expectedUSDValue)
163
+        ->journalEntries->sumCredits()->getAmount()->toEqual($expectedUSDValue);
163
 });
164
 });
164
 
165
 
165
 it('handles multi-currency deposits correctly', function () {
166
 it('handles multi-currency deposits correctly', function () {
171
 
172
 
172
     ConfigureCurrencies::syncCurrencies();
173
     ConfigureCurrencies::syncCurrencies();
173
 
174
 
174
-    // Create a deposit of 1500 BHD to the foreign bank account
175
+    // Create a deposit of 1500 BHD (in fils - BHD subunits) to the foreign bank account
175
     /** @var Transaction $transaction */
176
     /** @var Transaction $transaction */
176
     $transaction = Transaction::factory()
177
     $transaction = Transaction::factory()
177
         ->forBankAccount($foreignBankAccount->bankAccount)
178
         ->forBankAccount($foreignBankAccount->bankAccount)
184
     expect($debitAccount->is($foreignBankAccount))->toBeTrue() // Debit: Foreign Bank Account (BHD) account
185
     expect($debitAccount->is($foreignBankAccount))->toBeTrue() // Debit: Foreign Bank Account (BHD) account
185
         ->and($creditAccount->name)->toBe('Uncategorized Income'); // Credit: Uncategorized Income (USD) account
186
         ->and($creditAccount->name)->toBe('Uncategorized Income'); // Credit: Uncategorized Income (USD) account
186
 
187
 
187
-    // Convert to USD using the rate 0.38 BHD per USD
188
-    $expectedUSDValue = round(1500 / 0.38, 2);
188
+    $expectedUSDValue = CurrencyConverter::convertBalance(1500, 'BHD', 'USD');
189
 
189
 
190
-    // Verify that the debit is 39473.68 USD and the credit is 1500 BHD converted to 39473.68 USD
190
+    // Verify that journal entries are converted to USD cents
191
     expect($transaction)
191
     expect($transaction)
192
-        ->amount->toEqual('1,500.000')
192
+        ->amount->toBe(1500)
193
         ->journalEntries->count()->toBe(2)
193
         ->journalEntries->count()->toBe(2)
194
-        ->journalEntries->sumDebits()->getValue()->toEqual($expectedUSDValue)
195
-        ->journalEntries->sumCredits()->getValue()->toEqual($expectedUSDValue);
194
+        ->journalEntries->sumDebits()->getAmount()->toEqual($expectedUSDValue)
195
+        ->journalEntries->sumCredits()->getAmount()->toEqual($expectedUSDValue);
196
 });
196
 });
197
 
197
 
198
 it('handles multi-currency withdrawals correctly', function () {
198
 it('handles multi-currency withdrawals correctly', function () {
216
     expect($debitAccount->name)->toBe('Uncategorized Expense')
216
     expect($debitAccount->name)->toBe('Uncategorized Expense')
217
         ->and($creditAccount->is($foreignBankAccount))->toBeTrue();
217
         ->and($creditAccount->is($foreignBankAccount))->toBeTrue();
218
 
218
 
219
-    $expectedUSDValue = round(1500 / 0.76, 2);
219
+    $expectedUSDValue = CurrencyConverter::convertBalance(1500, 'GBP', 'USD');
220
 
220
 
221
     expect($transaction)
221
     expect($transaction)
222
-        ->amount->toEqual('1,500.00')
222
+        ->amount->toBe(1500)
223
         ->journalEntries->count()->toBe(2)
223
         ->journalEntries->count()->toBe(2)
224
-        ->journalEntries->sumDebits()->getValue()->toEqual($expectedUSDValue)
225
-        ->journalEntries->sumCredits()->getValue()->toEqual($expectedUSDValue);
224
+        ->journalEntries->sumDebits()->getAmount()->toEqual($expectedUSDValue)
225
+        ->journalEntries->sumCredits()->getAmount()->toEqual($expectedUSDValue);
226
 });
226
 });
227
 
227
 
228
 it('can add an income or expense transaction', function (TransactionType $transactionType, string $actionName) {
228
 it('can add an income or expense transaction', function (TransactionType $transactionType, string $actionName) {
249
 
249
 
250
     expect($transaction)
250
     expect($transaction)
251
         ->not->toBeNull()
251
         ->not->toBeNull()
252
-        ->amount->toEqual('500.00')
252
+        ->amount->toBe(50000) // 500.00 in cents
253
         ->type->toBe($transactionType)
253
         ->type->toBe($transactionType)
254
         ->bankAccount->is($defaultBankAccount)->toBeTrue()
254
         ->bankAccount->is($defaultBankAccount)->toBeTrue()
255
         ->account->is($defaultAccount)->toBeTrue()
255
         ->account->is($defaultAccount)->toBeTrue()
284
 
284
 
285
     expect($transaction)
285
     expect($transaction)
286
         ->not->toBeNull()
286
         ->not->toBeNull()
287
-        ->amount->toEqual('1,500.00')
287
+        ->amount->toBe(150000) // 1,500.00 in cents
288
         ->type->toBe(TransactionType::Transfer)
288
         ->type->toBe(TransactionType::Transfer)
289
         ->bankAccount->is($sourceBankAccount)->toBeTrue()
289
         ->bankAccount->is($sourceBankAccount)->toBeTrue()
290
         ->account->is($destinationBankAccount)->toBeTrue()
290
         ->account->is($destinationBankAccount)->toBeTrue()
323
 
323
 
324
     expect($transaction)
324
     expect($transaction)
325
         ->not->toBeNull()
325
         ->not->toBeNull()
326
-        ->amount->toEqual('1,000.00')
326
+        ->amount->toBe(100000) // 1,000.00 in cents
327
         ->type->isJournal()->toBeTrue()
327
         ->type->isJournal()->toBeTrue()
328
         ->bankAccount->toBeNull()
328
         ->bankAccount->toBeNull()
329
         ->account->toBeNull()
329
         ->account->toBeNull()
330
         ->journalEntries->count()->toBe(2)
330
         ->journalEntries->count()->toBe(2)
331
-        ->journalEntries->sumDebits()->getValue()->toEqual(1000)
332
-        ->journalEntries->sumCredits()->getValue()->toEqual(1000)
331
+        ->journalEntries->sumDebits()->getAmount()->toEqual(100000)
332
+        ->journalEntries->sumCredits()->getAmount()->toEqual(100000)
333
         ->and($debitAccount->is($defaultDebitAccount))->toBeTrue()
333
         ->and($debitAccount->is($defaultDebitAccount))->toBeTrue()
334
         ->and($creditAccount->is($defaultCreditAccount))->toBeTrue();
334
         ->and($creditAccount->is($defaultCreditAccount))->toBeTrue();
335
 });
335
 });
345
 
345
 
346
     $newDescription = 'Updated Description';
346
     $newDescription = 'Updated Description';
347
 
347
 
348
+    $formattedAmount = CurrencyConverter::convertCentsToFormatSimple($transaction->amount);
349
+
348
     livewire(ListTransactions::class)
350
     livewire(ListTransactions::class)
349
         ->mountTableAction(EditTransactionAction::class, $transaction)
351
         ->mountTableAction(EditTransactionAction::class, $transaction)
350
         ->assertTableActionDataSet([
352
         ->assertTableActionDataSet([
351
             'type' => $transactionType->value,
353
             'type' => $transactionType->value,
352
             'description' => $transaction->description,
354
             'description' => $transaction->description,
353
-            'amount' => $transaction->amount,
355
+            'amount' => $formattedAmount,
354
         ])
356
         ])
355
         ->setTableActionData([
357
         ->setTableActionData([
356
             'description' => $newDescription,
358
             'description' => $newDescription,
362
     $transaction->refresh();
364
     $transaction->refresh();
363
 
365
 
364
     expect($transaction->description)->toBe($newDescription)
366
     expect($transaction->description)->toBe($newDescription)
365
-        ->and($transaction->amount)->toEqual('1,500.00');
367
+        ->and($transaction->amount)->toBe(150000); // 1,500.00 in cents
366
 })->with([
368
 })->with([
367
     TransactionType::Deposit,
369
     TransactionType::Deposit,
368
     TransactionType::Withdrawal,
370
     TransactionType::Withdrawal,
377
 
379
 
378
     $newDescription = 'Updated Transfer Description';
380
     $newDescription = 'Updated Transfer Description';
379
 
381
 
382
+    $formattedAmount = CurrencyConverter::convertCentsToFormatSimple($transaction->amount);
383
+
380
     livewire(ListTransactions::class)
384
     livewire(ListTransactions::class)
381
         ->mountTableAction(EditTransactionAction::class, $transaction)
385
         ->mountTableAction(EditTransactionAction::class, $transaction)
382
         ->assertTableActionDataSet([
386
         ->assertTableActionDataSet([
383
             'type' => TransactionType::Transfer->value,
387
             'type' => TransactionType::Transfer->value,
384
             'description' => $transaction->description,
388
             'description' => $transaction->description,
385
-            'amount' => $transaction->amount,
389
+            'amount' => $formattedAmount,
386
         ])
390
         ])
387
         ->setTableActionData([
391
         ->setTableActionData([
388
             'description' => $newDescription,
392
             'description' => $newDescription,
394
     $transaction->refresh();
398
     $transaction->refresh();
395
 
399
 
396
     expect($transaction->description)->toBe($newDescription)
400
     expect($transaction->description)->toBe($newDescription)
397
-        ->and($transaction->amount)->toEqual('2,000.00');
401
+        ->and($transaction->amount)->toBe(200000); // 2,000.00 in cents
398
 });
402
 });
399
 
403
 
400
 it('replicates a transaction with correct journal entries', function () {
404
 it('replicates a transaction with correct journal entries', function () {
417
 
421
 
418
     expect($replicatedTransaction)
422
     expect($replicatedTransaction)
419
         ->journalEntries->count()->toBe(2)
423
         ->journalEntries->count()->toBe(2)
420
-        ->journalEntries->sumDebits()->getValue()->toEqual(1000)
421
-        ->journalEntries->sumCredits()->getValue()->toEqual(1000)
424
+        ->journalEntries->sumDebits()->getAmount()->toEqual(1000)
425
+        ->journalEntries->sumCredits()->getAmount()->toEqual(1000)
422
         ->description->toBe('(Copy of) ' . $originalTransaction->description)
426
         ->description->toBe('(Copy of) ' . $originalTransaction->description)
423
         ->and($replicatedDebitAccount->name)->toBe($originalDebitAccount->name)
427
         ->and($replicatedDebitAccount->name)->toBe($originalDebitAccount->name)
424
         ->and($replicatedCreditAccount->name)->toBe($originalCreditAccount->name);
428
         ->and($replicatedCreditAccount->name)->toBe($originalCreditAccount->name);
451
 
455
 
452
         expect($replicatedTransaction)
456
         expect($replicatedTransaction)
453
             ->journalEntries->count()->toBe(2)
457
             ->journalEntries->count()->toBe(2)
454
-            ->journalEntries->sumDebits()->getValue()->toEqual(1000)
455
-            ->journalEntries->sumCredits()->getValue()->toEqual(1000)
458
+            ->journalEntries->sumDebits()->getAmount()->toEqual(1000)
459
+            ->journalEntries->sumCredits()->getAmount()->toEqual(1000)
456
             ->and($replicatedDebitAccount->name)->toBe($originalDebitAccount->name)
460
             ->and($replicatedDebitAccount->name)->toBe($originalDebitAccount->name)
457
             ->and($replicatedCreditAccount->name)->toBe($originalCreditAccount->name);
461
             ->and($replicatedCreditAccount->name)->toBe($originalCreditAccount->name);
458
     });
462
     });

正在加载...
取消
保存