|
@@ -9,6 +9,7 @@ use App\Filament\Tables\Actions\ReplicateBulkAction;
|
9
|
9
|
use App\Models\Accounting\Account;
|
10
|
10
|
use App\Models\Accounting\Transaction;
|
11
|
11
|
use App\Utilities\Currency\ConfigureCurrencies;
|
|
12
|
+use App\Utilities\Currency\CurrencyConverter;
|
12
|
13
|
use Filament\Tables\Actions\DeleteAction;
|
13
|
14
|
use Filament\Tables\Actions\DeleteBulkAction;
|
14
|
15
|
use Filament\Tables\Actions\ReplicateAction;
|
|
@@ -78,8 +79,8 @@ it('stores and sums correct debit and credit amounts for different transaction t
|
78
|
79
|
->create();
|
79
|
80
|
|
80
|
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
|
84
|
})->with([
|
84
|
85
|
['asDeposit', 'forUncategorizedRevenue', 2000],
|
85
|
86
|
['asWithdrawal', 'forUncategorizedExpense', 500],
|
|
@@ -122,10 +123,10 @@ it('handles multi-currency transfers without conversion when the source bank acc
|
122
|
123
|
$expectedUSDValue = 1500;
|
123
|
124
|
|
124
|
125
|
expect($transaction)
|
125
|
|
- ->amount->toEqual('1,500.00')
|
|
126
|
+ ->amount->toBe(1500)
|
126
|
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
|
132
|
it('handles multi-currency transfers correctly', function () {
|
|
@@ -150,16 +151,16 @@ it('handles multi-currency transfers correctly', function () {
|
150
|
151
|
expect($debitAccount->name)->toBe('Destination Bank Account') // Debit: Destination (USD) account
|
151
|
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
|
159
|
expect($transaction)
|
159
|
|
- ->amount->toEqual('1,500.00')
|
|
160
|
+ ->amount->toBe(1500)
|
160
|
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
|
166
|
it('handles multi-currency deposits correctly', function () {
|
|
@@ -171,7 +172,7 @@ it('handles multi-currency deposits correctly', function () {
|
171
|
172
|
|
172
|
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
|
176
|
/** @var Transaction $transaction */
|
176
|
177
|
$transaction = Transaction::factory()
|
177
|
178
|
->forBankAccount($foreignBankAccount->bankAccount)
|
|
@@ -184,15 +185,14 @@ it('handles multi-currency deposits correctly', function () {
|
184
|
185
|
expect($debitAccount->is($foreignBankAccount))->toBeTrue() // Debit: Foreign Bank Account (BHD) account
|
185
|
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
|
191
|
expect($transaction)
|
192
|
|
- ->amount->toEqual('1,500.000')
|
|
192
|
+ ->amount->toBe(1500)
|
193
|
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
|
198
|
it('handles multi-currency withdrawals correctly', function () {
|
|
@@ -216,13 +216,13 @@ it('handles multi-currency withdrawals correctly', function () {
|
216
|
216
|
expect($debitAccount->name)->toBe('Uncategorized Expense')
|
217
|
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
|
221
|
expect($transaction)
|
222
|
|
- ->amount->toEqual('1,500.00')
|
|
222
|
+ ->amount->toBe(1500)
|
223
|
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
|
228
|
it('can add an income or expense transaction', function (TransactionType $transactionType, string $actionName) {
|
|
@@ -249,7 +249,7 @@ it('can add an income or expense transaction', function (TransactionType $transa
|
249
|
249
|
|
250
|
250
|
expect($transaction)
|
251
|
251
|
->not->toBeNull()
|
252
|
|
- ->amount->toEqual('500.00')
|
|
252
|
+ ->amount->toBe(50000) // 500.00 in cents
|
253
|
253
|
->type->toBe($transactionType)
|
254
|
254
|
->bankAccount->is($defaultBankAccount)->toBeTrue()
|
255
|
255
|
->account->is($defaultAccount)->toBeTrue()
|
|
@@ -284,7 +284,7 @@ it('can add a transfer transaction', function () {
|
284
|
284
|
|
285
|
285
|
expect($transaction)
|
286
|
286
|
->not->toBeNull()
|
287
|
|
- ->amount->toEqual('1,500.00')
|
|
287
|
+ ->amount->toBe(150000) // 1,500.00 in cents
|
288
|
288
|
->type->toBe(TransactionType::Transfer)
|
289
|
289
|
->bankAccount->is($sourceBankAccount)->toBeTrue()
|
290
|
290
|
->account->is($destinationBankAccount)->toBeTrue()
|
|
@@ -323,13 +323,13 @@ it('can add a journal transaction', function () {
|
323
|
323
|
|
324
|
324
|
expect($transaction)
|
325
|
325
|
->not->toBeNull()
|
326
|
|
- ->amount->toEqual('1,000.00')
|
|
326
|
+ ->amount->toBe(100000) // 1,000.00 in cents
|
327
|
327
|
->type->isJournal()->toBeTrue()
|
328
|
328
|
->bankAccount->toBeNull()
|
329
|
329
|
->account->toBeNull()
|
330
|
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
|
333
|
->and($debitAccount->is($defaultDebitAccount))->toBeTrue()
|
334
|
334
|
->and($creditAccount->is($defaultCreditAccount))->toBeTrue();
|
335
|
335
|
});
|
|
@@ -345,12 +345,14 @@ it('can update a deposit or withdrawal transaction', function (TransactionType $
|
345
|
345
|
|
346
|
346
|
$newDescription = 'Updated Description';
|
347
|
347
|
|
|
348
|
+ $formattedAmount = CurrencyConverter::convertCentsToFormatSimple($transaction->amount);
|
|
349
|
+
|
348
|
350
|
livewire(ListTransactions::class)
|
349
|
351
|
->mountTableAction(EditTransactionAction::class, $transaction)
|
350
|
352
|
->assertTableActionDataSet([
|
351
|
353
|
'type' => $transactionType->value,
|
352
|
354
|
'description' => $transaction->description,
|
353
|
|
- 'amount' => $transaction->amount,
|
|
355
|
+ 'amount' => $formattedAmount,
|
354
|
356
|
])
|
355
|
357
|
->setTableActionData([
|
356
|
358
|
'description' => $newDescription,
|
|
@@ -362,7 +364,7 @@ it('can update a deposit or withdrawal transaction', function (TransactionType $
|
362
|
364
|
$transaction->refresh();
|
363
|
365
|
|
364
|
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
|
368
|
})->with([
|
367
|
369
|
TransactionType::Deposit,
|
368
|
370
|
TransactionType::Withdrawal,
|
|
@@ -377,12 +379,14 @@ it('can update a transfer transaction', function () {
|
377
|
379
|
|
378
|
380
|
$newDescription = 'Updated Transfer Description';
|
379
|
381
|
|
|
382
|
+ $formattedAmount = CurrencyConverter::convertCentsToFormatSimple($transaction->amount);
|
|
383
|
+
|
380
|
384
|
livewire(ListTransactions::class)
|
381
|
385
|
->mountTableAction(EditTransactionAction::class, $transaction)
|
382
|
386
|
->assertTableActionDataSet([
|
383
|
387
|
'type' => TransactionType::Transfer->value,
|
384
|
388
|
'description' => $transaction->description,
|
385
|
|
- 'amount' => $transaction->amount,
|
|
389
|
+ 'amount' => $formattedAmount,
|
386
|
390
|
])
|
387
|
391
|
->setTableActionData([
|
388
|
392
|
'description' => $newDescription,
|
|
@@ -394,7 +398,7 @@ it('can update a transfer transaction', function () {
|
394
|
398
|
$transaction->refresh();
|
395
|
399
|
|
396
|
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
|
404
|
it('replicates a transaction with correct journal entries', function () {
|
|
@@ -417,8 +421,8 @@ it('replicates a transaction with correct journal entries', function () {
|
417
|
421
|
|
418
|
422
|
expect($replicatedTransaction)
|
419
|
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
|
426
|
->description->toBe('(Copy of) ' . $originalTransaction->description)
|
423
|
427
|
->and($replicatedDebitAccount->name)->toBe($originalDebitAccount->name)
|
424
|
428
|
->and($replicatedCreditAccount->name)->toBe($originalCreditAccount->name);
|
|
@@ -451,8 +455,8 @@ it('bulk replicates transactions with correct journal entries', function () {
|
451
|
455
|
|
452
|
456
|
expect($replicatedTransaction)
|
453
|
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
|
460
|
->and($replicatedDebitAccount->name)->toBe($originalDebitAccount->name)
|
457
|
461
|
->and($replicatedCreditAccount->name)->toBe($originalCreditAccount->name);
|
458
|
462
|
});
|