|
@@ -1,8 +1,18 @@
|
1
|
1
|
<?php
|
2
|
2
|
|
|
3
|
+use App\Enums\Accounting\JournalEntryType;
|
|
4
|
+use App\Enums\Accounting\TransactionType;
|
|
5
|
+use App\Filament\Company\Pages\Accounting\Transactions;
|
|
6
|
+use App\Filament\Forms\Components\JournalEntryRepeater;
|
|
7
|
+use App\Filament\Tables\Actions\ReplicateBulkAction;
|
3
|
8
|
use App\Models\Accounting\Account;
|
4
|
9
|
use App\Models\Accounting\Transaction;
|
5
|
10
|
use App\Utilities\Currency\ConfigureCurrencies;
|
|
11
|
+use Filament\Tables\Actions\DeleteAction;
|
|
12
|
+use Filament\Tables\Actions\DeleteBulkAction;
|
|
13
|
+use Filament\Tables\Actions\ReplicateAction;
|
|
14
|
+
|
|
15
|
+use function Pest\Livewire\livewire;
|
6
|
16
|
|
7
|
17
|
it('creates correct journal entries for a deposit transaction', function () {
|
8
|
18
|
$transaction = Transaction::factory()
|
|
@@ -59,14 +69,16 @@ it('does not create journal entries for a journal transaction', function () {
|
59
|
69
|
});
|
60
|
70
|
|
61
|
71
|
it('stores and sums correct debit and credit amounts for different transaction types', function ($method, $setupMethod, $amount) {
|
|
72
|
+ /** @var Transaction $transaction */
|
62
|
73
|
$transaction = Transaction::factory()
|
63
|
74
|
->forDefaultBankAccount()
|
64
|
75
|
->{$setupMethod}()
|
65
|
76
|
->{$method}($amount)
|
66
|
77
|
->create();
|
67
|
78
|
|
68
|
|
- expect($transaction->journalEntries->sumDebits()->getValue())->toEqual($amount)
|
69
|
|
- ->and($transaction->journalEntries->sumCredits()->getValue())->toEqual($amount);
|
|
79
|
+ expect($transaction)
|
|
80
|
+ ->journalEntries->sumDebits()->getValue()->toEqual($amount)
|
|
81
|
+ ->journalEntries->sumCredits()->getValue()->toEqual($amount);
|
70
|
82
|
})->with([
|
71
|
83
|
['asDeposit', 'forUncategorizedRevenue', 2000],
|
72
|
84
|
['asWithdrawal', 'forUncategorizedExpense', 500],
|
|
@@ -94,6 +106,7 @@ it('handles multi-currency transfers without conversion when the source bank acc
|
94
|
106
|
->withForeignBankAccount('Foreign Bank Account', 'EUR', 0.92)
|
95
|
107
|
->create();
|
96
|
108
|
|
|
109
|
+ /** @var Transaction $transaction */
|
97
|
110
|
$transaction = Transaction::factory()
|
98
|
111
|
->forDefaultBankAccount()
|
99
|
112
|
->forDestinationBankAccount($foreignBankAccount)
|
|
@@ -102,12 +115,16 @@ it('handles multi-currency transfers without conversion when the source bank acc
|
102
|
115
|
|
103
|
116
|
[$debitAccount, $creditAccount] = getTransactionDebitAndCreditAccounts($transaction);
|
104
|
117
|
|
105
|
|
- expect($transaction->journalEntries->count())->toBe(2)
|
106
|
|
- ->and($debitAccount->name)->toBe('Foreign Bank Account')
|
107
|
|
- ->and($creditAccount->name)->toBe('Cash on Hand')
|
108
|
|
- ->and($transaction->journalEntries->sumDebits()->getValue())->toEqual(1500)
|
109
|
|
- ->and($transaction->journalEntries->sumCredits()->getValue())->toEqual(1500)
|
110
|
|
- ->and($transaction->amount)->toEqual('1,500.00');
|
|
118
|
+ expect($debitAccount->is($foreignBankAccount))->toBeTrue()
|
|
119
|
+ ->and($creditAccount->name)->toBe('Cash on Hand');
|
|
120
|
+
|
|
121
|
+ $expectedUSDValue = 1500;
|
|
122
|
+
|
|
123
|
+ expect($transaction)
|
|
124
|
+ ->amount->toEqual('1,500.00')
|
|
125
|
+ ->journalEntries->count()->toBe(2)
|
|
126
|
+ ->journalEntries->sumDebits()->getValue()->toEqual($expectedUSDValue)
|
|
127
|
+ ->journalEntries->sumCredits()->getValue()->toEqual($expectedUSDValue);
|
111
|
128
|
});
|
112
|
129
|
|
113
|
130
|
it('handles multi-currency transfers correctly', function () {
|
|
@@ -118,6 +135,7 @@ it('handles multi-currency transfers correctly', function () {
|
118
|
135
|
ConfigureCurrencies::syncCurrencies();
|
119
|
136
|
|
120
|
137
|
// Create a transfer of 1500 CAD from the foreign bank account to USD bank account
|
|
138
|
+ /** @var Transaction $transaction */
|
121
|
139
|
$transaction = Transaction::factory()
|
122
|
140
|
->forBankAccount($foreignBankAccount->bankAccount)
|
123
|
141
|
->forDestinationBankAccount()
|
|
@@ -126,18 +144,19 @@ it('handles multi-currency transfers correctly', function () {
|
126
|
144
|
|
127
|
145
|
[$debitAccount, $creditAccount] = getTransactionDebitAndCreditAccounts($transaction);
|
128
|
146
|
|
129
|
|
- expect($transaction->journalEntries->count())->toBe(2)
|
130
|
|
- ->and($debitAccount->name)->toBe('Destination Bank Account') // Debit: Destination (USD) account
|
131
|
|
- ->and($creditAccount->name)->toBe('CAD Bank Account'); // Credit: Foreign (CAD) account
|
|
147
|
+ expect($debitAccount->name)->toBe('Destination Bank Account') // Debit: Destination (USD) account
|
|
148
|
+ ->and($creditAccount->is($foreignBankAccount))->toBeTrue(); // Credit: Foreign Bank Account (CAD) account
|
132
|
149
|
|
133
|
150
|
// The 1500 CAD is worth 1102.94 USD (1500 CAD / 1.36)
|
134
|
151
|
$expectedUSDValue = round(1500 / 1.36, 2);
|
135
|
152
|
|
136
|
153
|
// Verify that the debit is 1102.94 USD and the credit is 1500 CAD converted to 1102.94 USD
|
137
|
154
|
// Transaction amount stays in source bank account currency (cast is applied)
|
138
|
|
- expect($transaction->journalEntries->sumDebits()->getValue())->toEqual($expectedUSDValue)
|
139
|
|
- ->and($transaction->journalEntries->sumCredits()->getValue())->toEqual($expectedUSDValue)
|
140
|
|
- ->and($transaction->amount)->toEqual('1,500.00');
|
|
155
|
+ expect($transaction)
|
|
156
|
+ ->amount->toEqual('1,500.00')
|
|
157
|
+ ->journalEntries->count()->toBe(2)
|
|
158
|
+ ->journalEntries->sumDebits()->getValue()->toEqual($expectedUSDValue)
|
|
159
|
+ ->journalEntries->sumCredits()->getValue()->toEqual($expectedUSDValue);
|
141
|
160
|
});
|
142
|
161
|
|
143
|
162
|
it('handles multi-currency deposits correctly', function () {
|
|
@@ -148,6 +167,7 @@ it('handles multi-currency deposits correctly', function () {
|
148
|
167
|
ConfigureCurrencies::syncCurrencies();
|
149
|
168
|
|
150
|
169
|
// Create a deposit of 1500 BHD to the foreign bank account
|
|
170
|
+ /** @var Transaction $transaction */
|
151
|
171
|
$transaction = Transaction::factory()
|
152
|
172
|
->forBankAccount($foreignBankAccount->bankAccount)
|
153
|
173
|
->forUncategorizedRevenue()
|
|
@@ -156,17 +176,18 @@ it('handles multi-currency deposits correctly', function () {
|
156
|
176
|
|
157
|
177
|
[$debitAccount, $creditAccount] = getTransactionDebitAndCreditAccounts($transaction);
|
158
|
178
|
|
159
|
|
- expect($transaction->journalEntries->count())->toBe(2)
|
160
|
|
- ->and($debitAccount->name)->toBe('BHD Bank Account') // Debit: Foreign (BHD) account
|
|
179
|
+ expect($debitAccount->is($foreignBankAccount))->toBeTrue() // Debit: Foreign Bank Account (BHD) account
|
161
|
180
|
->and($creditAccount->name)->toBe('Uncategorized Income'); // Credit: Uncategorized Income (USD) account
|
162
|
181
|
|
163
|
182
|
// Convert to USD using the rate 0.38 BHD per USD
|
164
|
183
|
$expectedUSDValue = round(1500 / 0.38, 2);
|
165
|
184
|
|
166
|
185
|
// Verify that the debit is 39473.68 USD and the credit is 1500 BHD converted to 39473.68 USD
|
167
|
|
- expect($transaction->journalEntries->sumDebits()->getValue())->toEqual($expectedUSDValue)
|
168
|
|
- ->and($transaction->journalEntries->sumCredits()->getValue())->toEqual($expectedUSDValue)
|
169
|
|
- ->and($transaction->amount)->toEqual('1,500.000'); // Original amount in BHD (3 decimal precision)
|
|
186
|
+ expect($transaction)
|
|
187
|
+ ->amount->toEqual('1,500.000')
|
|
188
|
+ ->journalEntries->count()->toBe(2)
|
|
189
|
+ ->journalEntries->sumDebits()->getValue()->toEqual($expectedUSDValue)
|
|
190
|
+ ->journalEntries->sumCredits()->getValue()->toEqual($expectedUSDValue);
|
170
|
191
|
});
|
171
|
192
|
|
172
|
193
|
it('handles multi-currency withdrawals correctly', function () {
|
|
@@ -176,6 +197,7 @@ it('handles multi-currency withdrawals correctly', function () {
|
176
|
197
|
|
177
|
198
|
ConfigureCurrencies::syncCurrencies();
|
178
|
199
|
|
|
200
|
+ /** @var Transaction $transaction */
|
179
|
201
|
$transaction = Transaction::factory()
|
180
|
202
|
->forBankAccount($foreignBankAccount->bankAccount)
|
181
|
203
|
->forUncategorizedExpense()
|
|
@@ -184,13 +206,310 @@ it('handles multi-currency withdrawals correctly', function () {
|
184
|
206
|
|
185
|
207
|
[$debitAccount, $creditAccount] = getTransactionDebitAndCreditAccounts($transaction);
|
186
|
208
|
|
187
|
|
- expect($transaction->journalEntries->count())->toBe(2)
|
188
|
|
- ->and($debitAccount->name)->toBe('Uncategorized Expense')
|
189
|
|
- ->and($creditAccount->name)->toBe('Foreign Bank Account');
|
|
209
|
+ expect($debitAccount->name)->toBe('Uncategorized Expense')
|
|
210
|
+ ->and($creditAccount->is($foreignBankAccount))->toBeTrue();
|
190
|
211
|
|
191
|
212
|
$expectedUSDValue = round(1500 / 0.76, 2);
|
192
|
213
|
|
193
|
|
- expect($transaction->journalEntries->sumDebits()->getValue())->toEqual($expectedUSDValue)
|
194
|
|
- ->and($transaction->journalEntries->sumCredits()->getValue())->toEqual($expectedUSDValue)
|
|
214
|
+ expect($transaction)
|
|
215
|
+ ->amount->toEqual('1,500.00')
|
|
216
|
+ ->journalEntries->count()->toBe(2)
|
|
217
|
+ ->journalEntries->sumDebits()->getValue()->toEqual($expectedUSDValue)
|
|
218
|
+ ->journalEntries->sumCredits()->getValue()->toEqual($expectedUSDValue);
|
|
219
|
+});
|
|
220
|
+
|
|
221
|
+it('can add an income or expense transaction', function (TransactionType $transactionType, string $actionName) {
|
|
222
|
+ $testCompany = $this->testCompany;
|
|
223
|
+ $defaultBankAccount = $testCompany->default->bankAccount;
|
|
224
|
+ $defaultAccount = Transactions::getUncategorizedAccountByType($transactionType);
|
|
225
|
+
|
|
226
|
+ livewire(Transactions::class)
|
|
227
|
+ ->mountAction($actionName)
|
|
228
|
+ ->assertActionDataSet([
|
|
229
|
+ 'posted_at' => now()->toDateTimeString(),
|
|
230
|
+ 'type' => $transactionType,
|
|
231
|
+ 'bank_account_id' => $defaultBankAccount->id,
|
|
232
|
+ 'amount' => '0.00',
|
|
233
|
+ 'account_id' => $defaultAccount->id,
|
|
234
|
+ ])
|
|
235
|
+ ->setActionData([
|
|
236
|
+ 'amount' => '500.00',
|
|
237
|
+ ])
|
|
238
|
+ ->callMountedAction()
|
|
239
|
+ ->assertHasNoActionErrors();
|
|
240
|
+
|
|
241
|
+ $transaction = Transaction::first();
|
|
242
|
+
|
|
243
|
+ expect($transaction)
|
|
244
|
+ ->not->toBeNull()
|
|
245
|
+ ->amount->toEqual('500.00')
|
|
246
|
+ ->type->toBe($transactionType)
|
|
247
|
+ ->bankAccount->is($defaultBankAccount)->toBeTrue()
|
|
248
|
+ ->account->is($defaultAccount)->toBeTrue()
|
|
249
|
+ ->journalEntries->count()->toBe(2);
|
|
250
|
+})->with([
|
|
251
|
+ [TransactionType::Deposit, 'addIncome'],
|
|
252
|
+ [TransactionType::Withdrawal, 'addExpense'],
|
|
253
|
+]);
|
|
254
|
+
|
|
255
|
+it('can add a transfer transaction', function () {
|
|
256
|
+ $testCompany = $this->testCompany;
|
|
257
|
+ $sourceBankAccount = $testCompany->default->bankAccount;
|
|
258
|
+ $destinationBankAccount = Account::factory()->withBankAccount('Destination Bank Account')->create();
|
|
259
|
+
|
|
260
|
+ livewire(Transactions::class)
|
|
261
|
+ ->mountAction('addTransfer')
|
|
262
|
+ ->assertActionDataSet([
|
|
263
|
+ 'posted_at' => now()->toDateTimeString(),
|
|
264
|
+ 'type' => TransactionType::Transfer,
|
|
265
|
+ 'bank_account_id' => $sourceBankAccount->id,
|
|
266
|
+ 'amount' => '0.00',
|
|
267
|
+ 'account_id' => null,
|
|
268
|
+ ])
|
|
269
|
+ ->setActionData([
|
|
270
|
+ 'account_id' => $destinationBankAccount->id,
|
|
271
|
+ 'amount' => '1,500.00',
|
|
272
|
+ ])
|
|
273
|
+ ->callMountedAction()
|
|
274
|
+ ->assertHasNoActionErrors();
|
|
275
|
+
|
|
276
|
+ $transaction = Transaction::first();
|
|
277
|
+
|
|
278
|
+ expect($transaction)
|
|
279
|
+ ->not->toBeNull()
|
|
280
|
+ ->amount->toEqual('1,500.00')
|
|
281
|
+ ->type->toBe(TransactionType::Transfer)
|
|
282
|
+ ->bankAccount->is($sourceBankAccount)->toBeTrue()
|
|
283
|
+ ->account->is($destinationBankAccount)->toBeTrue()
|
|
284
|
+ ->journalEntries->count()->toBe(2);
|
|
285
|
+});
|
|
286
|
+
|
|
287
|
+it('can add a journal transaction', function () {
|
|
288
|
+ $defaultDebitAccount = Transactions::getUncategorizedAccountByType(TransactionType::Withdrawal);
|
|
289
|
+ $defaultCreditAccount = Transactions::getUncategorizedAccountByType(TransactionType::Deposit);
|
|
290
|
+
|
|
291
|
+ $undoRepeaterFake = JournalEntryRepeater::fake();
|
|
292
|
+
|
|
293
|
+ livewire(Transactions::class)
|
|
294
|
+ ->mountAction('addJournalTransaction')
|
|
295
|
+ ->assertActionDataSet([
|
|
296
|
+ 'posted_at' => now()->toDateTimeString(),
|
|
297
|
+ 'journalEntries' => [
|
|
298
|
+ ['type' => JournalEntryType::Debit, 'account_id' => $defaultDebitAccount->id, 'amount' => '0.00'],
|
|
299
|
+ ['type' => JournalEntryType::Credit, 'account_id' => $defaultCreditAccount->id, 'amount' => '0.00'],
|
|
300
|
+ ],
|
|
301
|
+ ])
|
|
302
|
+ ->setActionData([
|
|
303
|
+ 'journalEntries' => [
|
|
304
|
+ ['amount' => '1,000.00'],
|
|
305
|
+ ['amount' => '1,000.00'],
|
|
306
|
+ ],
|
|
307
|
+ ])
|
|
308
|
+ ->callMountedAction()
|
|
309
|
+ ->assertHasNoActionErrors();
|
|
310
|
+
|
|
311
|
+ $undoRepeaterFake();
|
|
312
|
+
|
|
313
|
+ $transaction = Transaction::first();
|
|
314
|
+
|
|
315
|
+ [$debitAccount, $creditAccount] = getTransactionDebitAndCreditAccounts($transaction);
|
|
316
|
+
|
|
317
|
+ expect($transaction)
|
|
318
|
+ ->not->toBeNull()
|
|
319
|
+ ->amount->toEqual('1,000.00')
|
|
320
|
+ ->type->isJournal()->toBeTrue()
|
|
321
|
+ ->bankAccount->toBeNull()
|
|
322
|
+ ->account->toBeNull()
|
|
323
|
+ ->journalEntries->count()->toBe(2)
|
|
324
|
+ ->journalEntries->sumDebits()->getValue()->toEqual(1000)
|
|
325
|
+ ->journalEntries->sumCredits()->getValue()->toEqual(1000)
|
|
326
|
+ ->and($debitAccount->is($defaultDebitAccount))->toBeTrue()
|
|
327
|
+ ->and($creditAccount->is($defaultCreditAccount))->toBeTrue();
|
|
328
|
+});
|
|
329
|
+
|
|
330
|
+it('can update a deposit or withdrawal transaction', function (TransactionType $transactionType) {
|
|
331
|
+ $defaultAccount = Transactions::getUncategorizedAccountByType($transactionType);
|
|
332
|
+
|
|
333
|
+ $transaction = Transaction::factory()
|
|
334
|
+ ->forDefaultBankAccount()
|
|
335
|
+ ->forAccount($defaultAccount)
|
|
336
|
+ ->forType($transactionType, 1000)
|
|
337
|
+ ->create();
|
|
338
|
+
|
|
339
|
+ $newDescription = 'Updated Description';
|
|
340
|
+
|
|
341
|
+ livewire(Transactions::class)
|
|
342
|
+ ->mountTableAction('updateTransaction', $transaction)
|
|
343
|
+ ->assertTableActionDataSet([
|
|
344
|
+ 'type' => $transactionType->value,
|
|
345
|
+ 'description' => $transaction->description,
|
|
346
|
+ 'amount' => $transaction->amount,
|
|
347
|
+ ])
|
|
348
|
+ ->setTableActionData([
|
|
349
|
+ 'description' => $newDescription,
|
|
350
|
+ 'amount' => '1,500.00',
|
|
351
|
+ ])
|
|
352
|
+ ->callMountedTableAction()
|
|
353
|
+ ->assertHasNoTableActionErrors();
|
|
354
|
+
|
|
355
|
+ $transaction->refresh();
|
|
356
|
+
|
|
357
|
+ expect($transaction->description)->toBe($newDescription)
|
195
|
358
|
->and($transaction->amount)->toEqual('1,500.00');
|
|
359
|
+})->with([
|
|
360
|
+ TransactionType::Deposit,
|
|
361
|
+ TransactionType::Withdrawal,
|
|
362
|
+]);
|
|
363
|
+
|
|
364
|
+it('does not show Edit Transfer or Edit Journal Transaction for deposit or withdrawal transactions', function (TransactionType $transactionType) {
|
|
365
|
+ $defaultAccount = Transactions::getUncategorizedAccountByType($transactionType);
|
|
366
|
+
|
|
367
|
+ $transaction = Transaction::factory()
|
|
368
|
+ ->forDefaultBankAccount()
|
|
369
|
+ ->forAccount($defaultAccount)
|
|
370
|
+ ->forType($transactionType, 1000)
|
|
371
|
+ ->create();
|
|
372
|
+
|
|
373
|
+ livewire(Transactions::class)
|
|
374
|
+ ->assertTableActionHidden('updateTransfer', $transaction)
|
|
375
|
+ ->assertTableActionHidden('updateJournalTransaction', $transaction);
|
|
376
|
+})->with([
|
|
377
|
+ TransactionType::Deposit,
|
|
378
|
+ TransactionType::Withdrawal,
|
|
379
|
+]);
|
|
380
|
+
|
|
381
|
+it('can update a transfer transaction', function () {
|
|
382
|
+ $transaction = Transaction::factory()
|
|
383
|
+ ->forDefaultBankAccount()
|
|
384
|
+ ->forDestinationBankAccount()
|
|
385
|
+ ->asTransfer(1500)
|
|
386
|
+ ->create();
|
|
387
|
+
|
|
388
|
+ $newDescription = 'Updated Transfer Description';
|
|
389
|
+
|
|
390
|
+ livewire(Transactions::class)
|
|
391
|
+ ->mountTableAction('updateTransfer', $transaction)
|
|
392
|
+ ->assertTableActionDataSet([
|
|
393
|
+ 'type' => TransactionType::Transfer->value,
|
|
394
|
+ 'description' => $transaction->description,
|
|
395
|
+ 'amount' => $transaction->amount,
|
|
396
|
+ ])
|
|
397
|
+ ->setTableActionData([
|
|
398
|
+ 'description' => $newDescription,
|
|
399
|
+ 'amount' => '2,000.00',
|
|
400
|
+ ])
|
|
401
|
+ ->callMountedTableAction()
|
|
402
|
+ ->assertHasNoTableActionErrors();
|
|
403
|
+
|
|
404
|
+ $transaction->refresh();
|
|
405
|
+
|
|
406
|
+ expect($transaction->description)->toBe($newDescription)
|
|
407
|
+ ->and($transaction->amount)->toEqual('2,000.00');
|
|
408
|
+});
|
|
409
|
+
|
|
410
|
+it('does not show Edit Transaction or Edit Journal Transaction for transfer transactions', function () {
|
|
411
|
+ $transaction = Transaction::factory()
|
|
412
|
+ ->forDefaultBankAccount()
|
|
413
|
+ ->forDestinationBankAccount()
|
|
414
|
+ ->asTransfer(1500)
|
|
415
|
+ ->create();
|
|
416
|
+
|
|
417
|
+ livewire(Transactions::class)
|
|
418
|
+ ->assertTableActionHidden('updateTransaction', $transaction)
|
|
419
|
+ ->assertTableActionHidden('updateJournalTransaction', $transaction);
|
|
420
|
+});
|
|
421
|
+
|
|
422
|
+it('replicates a transaction with correct journal entries', function () {
|
|
423
|
+ $originalTransaction = Transaction::factory()
|
|
424
|
+ ->forDefaultBankAccount()
|
|
425
|
+ ->forUncategorizedRevenue()
|
|
426
|
+ ->asDeposit(1000)
|
|
427
|
+ ->create();
|
|
428
|
+
|
|
429
|
+ livewire(Transactions::class)
|
|
430
|
+ ->callTableAction(ReplicateAction::class, $originalTransaction);
|
|
431
|
+
|
|
432
|
+ $replicatedTransaction = Transaction::whereKeyNot($originalTransaction->getKey())->first();
|
|
433
|
+
|
|
434
|
+ expect($replicatedTransaction)->not->toBeNull();
|
|
435
|
+
|
|
436
|
+ [$originalDebitAccount, $originalCreditAccount] = getTransactionDebitAndCreditAccounts($originalTransaction);
|
|
437
|
+
|
|
438
|
+ [$replicatedDebitAccount, $replicatedCreditAccount] = getTransactionDebitAndCreditAccounts($replicatedTransaction);
|
|
439
|
+
|
|
440
|
+ expect($replicatedTransaction)
|
|
441
|
+ ->journalEntries->count()->toBe(2)
|
|
442
|
+ ->journalEntries->sumDebits()->getValue()->toEqual(1000)
|
|
443
|
+ ->journalEntries->sumCredits()->getValue()->toEqual(1000)
|
|
444
|
+ ->description->toBe('(Copy of) ' . $originalTransaction->description)
|
|
445
|
+ ->and($replicatedDebitAccount->name)->toBe($originalDebitAccount->name)
|
|
446
|
+ ->and($replicatedCreditAccount->name)->toBe($originalCreditAccount->name);
|
|
447
|
+});
|
|
448
|
+
|
|
449
|
+it('bulk replicates transactions with correct journal entries', function () {
|
|
450
|
+ $originalTransactions = Transaction::factory()
|
|
451
|
+ ->forDefaultBankAccount()
|
|
452
|
+ ->forUncategorizedRevenue()
|
|
453
|
+ ->asDeposit(1000)
|
|
454
|
+ ->count(3)
|
|
455
|
+ ->create();
|
|
456
|
+
|
|
457
|
+ livewire(Transactions::class)
|
|
458
|
+ ->callTableBulkAction(ReplicateBulkAction::class, $originalTransactions);
|
|
459
|
+
|
|
460
|
+ $replicatedTransactions = Transaction::whereKeyNot($originalTransactions->modelKeys())->get();
|
|
461
|
+
|
|
462
|
+ expect($replicatedTransactions->count())->toBe(3);
|
|
463
|
+
|
|
464
|
+ $originalTransactions->each(function (Transaction $originalTransaction) use ($replicatedTransactions) {
|
|
465
|
+ /** @var Transaction $replicatedTransaction */
|
|
466
|
+ $replicatedTransaction = $replicatedTransactions->firstWhere('description', '(Copy of) ' . $originalTransaction->description);
|
|
467
|
+
|
|
468
|
+ expect($replicatedTransaction)->not->toBeNull();
|
|
469
|
+
|
|
470
|
+ [$originalDebitAccount, $originalCreditAccount] = getTransactionDebitAndCreditAccounts($originalTransaction);
|
|
471
|
+
|
|
472
|
+ [$replicatedDebitAccount, $replicatedCreditAccount] = getTransactionDebitAndCreditAccounts($replicatedTransaction);
|
|
473
|
+
|
|
474
|
+ expect($replicatedTransaction)
|
|
475
|
+ ->journalEntries->count()->toBe(2)
|
|
476
|
+ ->journalEntries->sumDebits()->getValue()->toEqual(1000)
|
|
477
|
+ ->journalEntries->sumCredits()->getValue()->toEqual(1000)
|
|
478
|
+ ->and($replicatedDebitAccount->name)->toBe($originalDebitAccount->name)
|
|
479
|
+ ->and($replicatedCreditAccount->name)->toBe($originalCreditAccount->name);
|
|
480
|
+ });
|
|
481
|
+});
|
|
482
|
+
|
|
483
|
+it('can delete a transaction with journal entries', function () {
|
|
484
|
+ $transaction = Transaction::factory()
|
|
485
|
+ ->forDefaultBankAccount()
|
|
486
|
+ ->forUncategorizedRevenue()
|
|
487
|
+ ->asDeposit(1000)
|
|
488
|
+ ->create();
|
|
489
|
+
|
|
490
|
+ expect($transaction->journalEntries()->count())->toBe(2);
|
|
491
|
+
|
|
492
|
+ livewire(Transactions::class)
|
|
493
|
+ ->callTableAction(DeleteAction::class, $transaction);
|
|
494
|
+
|
|
495
|
+ $this->assertModelMissing($transaction);
|
|
496
|
+
|
|
497
|
+ $this->assertDatabaseEmpty('journal_entries');
|
|
498
|
+});
|
|
499
|
+
|
|
500
|
+it('can bulk delete transactions with journal entries', function () {
|
|
501
|
+ $transactions = Transaction::factory()
|
|
502
|
+ ->forDefaultBankAccount()
|
|
503
|
+ ->forUncategorizedRevenue()
|
|
504
|
+ ->asDeposit(1000)
|
|
505
|
+ ->count(3)
|
|
506
|
+ ->create();
|
|
507
|
+
|
|
508
|
+ expect($transactions->count())->toBe(3);
|
|
509
|
+
|
|
510
|
+ livewire(Transactions::class)
|
|
511
|
+ ->callTableBulkAction(DeleteBulkAction::class, $transactions);
|
|
512
|
+
|
|
513
|
+ $this->assertDatabaseEmpty('transactions');
|
|
514
|
+ $this->assertDatabaseEmpty('journal_entries');
|
196
|
515
|
});
|