|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+<?php
|
|
|
2
|
+
|
|
|
3
|
+use App\Models\Accounting\Account;
|
|
|
4
|
+use App\Models\Accounting\Transaction;
|
|
|
5
|
+use App\Utilities\Currency\ConfigureCurrencies;
|
|
|
6
|
+
|
|
|
7
|
+it('creates correct journal entries for a deposit transaction', function () {
|
|
|
8
|
+ $transaction = Transaction::factory()
|
|
|
9
|
+ ->forDefaultBankAccount()
|
|
|
10
|
+ ->forUncategorizedRevenue()
|
|
|
11
|
+ ->asDeposit(1000)
|
|
|
12
|
+ ->create();
|
|
|
13
|
+
|
|
|
14
|
+ [$debitAccount, $creditAccount] = getTransactionDebitAndCreditAccounts($transaction);
|
|
|
15
|
+
|
|
|
16
|
+ expect($transaction->journalEntries->count())->toBe(2)
|
|
|
17
|
+ ->and($debitAccount->name)->toBe('Cash on Hand')
|
|
|
18
|
+ ->and($creditAccount->name)->toBe('Uncategorized Income');
|
|
|
19
|
+});
|
|
|
20
|
+
|
|
|
21
|
+it('creates correct journal entries for a withdrawal transaction', function () {
|
|
|
22
|
+ $transaction = Transaction::factory()
|
|
|
23
|
+ ->forDefaultBankAccount()
|
|
|
24
|
+ ->forUncategorizedExpense()
|
|
|
25
|
+ ->asWithdrawal(500)
|
|
|
26
|
+ ->create();
|
|
|
27
|
+
|
|
|
28
|
+ [$debitAccount, $creditAccount] = getTransactionDebitAndCreditAccounts($transaction);
|
|
|
29
|
+
|
|
|
30
|
+ expect($transaction->journalEntries->count())->toBe(2)
|
|
|
31
|
+ ->and($debitAccount->name)->toBe('Uncategorized Expense')
|
|
|
32
|
+ ->and($creditAccount->name)->toBe('Cash on Hand');
|
|
|
33
|
+});
|
|
|
34
|
+
|
|
|
35
|
+it('creates correct journal entries for a transfer transaction', function () {
|
|
|
36
|
+ $transaction = Transaction::factory()
|
|
|
37
|
+ ->forDefaultBankAccount()
|
|
|
38
|
+ ->forDestinationBankAccount()
|
|
|
39
|
+ ->asTransfer(1500)
|
|
|
40
|
+ ->create();
|
|
|
41
|
+
|
|
|
42
|
+ [$debitAccount, $creditAccount] = getTransactionDebitAndCreditAccounts($transaction);
|
|
|
43
|
+
|
|
|
44
|
+ // Acts as a withdrawal transaction for the source account
|
|
|
45
|
+ expect($transaction->journalEntries->count())->toBe(2)
|
|
|
46
|
+ ->and($debitAccount->name)->toBe('Destination Bank Account')
|
|
|
47
|
+ ->and($creditAccount->name)->toBe('Cash on Hand');
|
|
|
48
|
+});
|
|
|
49
|
+
|
|
|
50
|
+it('does not create journal entries for a journal transaction', function () {
|
|
|
51
|
+ $transaction = Transaction::factory()
|
|
|
52
|
+ ->forDefaultBankAccount()
|
|
|
53
|
+ ->forUncategorizedRevenue()
|
|
|
54
|
+ ->asJournal(1000)
|
|
|
55
|
+ ->create();
|
|
|
56
|
+
|
|
|
57
|
+ // Journal entries for a journal transaction are created manually
|
|
|
58
|
+ expect($transaction->journalEntries->count())->toBe(0);
|
|
|
59
|
+});
|
|
|
60
|
+
|
|
|
61
|
+it('stores and sums correct debit and credit amounts for different transaction types', function ($method, $setupMethod, $amount) {
|
|
|
62
|
+ $transaction = Transaction::factory()
|
|
|
63
|
+ ->forDefaultBankAccount()
|
|
|
64
|
+ ->{$setupMethod}()
|
|
|
65
|
+ ->{$method}($amount)
|
|
|
66
|
+ ->create();
|
|
|
67
|
+
|
|
|
68
|
+ expect($transaction->journalEntries->sumDebits()->getValue())->toEqual($amount)
|
|
|
69
|
+ ->and($transaction->journalEntries->sumCredits()->getValue())->toEqual($amount);
|
|
|
70
|
+})->with([
|
|
|
71
|
+ ['asDeposit', 'forUncategorizedRevenue', 2000],
|
|
|
72
|
+ ['asWithdrawal', 'forUncategorizedExpense', 500],
|
|
|
73
|
+ ['asTransfer', 'forDestinationBankAccount', 1500],
|
|
|
74
|
+]);
|
|
|
75
|
+
|
|
|
76
|
+it('deletes associated journal entries when transaction is deleted', function () {
|
|
|
77
|
+ $transaction = Transaction::factory()
|
|
|
78
|
+ ->forDefaultBankAccount()
|
|
|
79
|
+ ->forUncategorizedRevenue()
|
|
|
80
|
+ ->asDeposit(1000)
|
|
|
81
|
+ ->create();
|
|
|
82
|
+
|
|
|
83
|
+ expect($transaction->journalEntries()->count())->toBe(2);
|
|
|
84
|
+
|
|
|
85
|
+ $transaction->delete();
|
|
|
86
|
+
|
|
|
87
|
+ $this->assertModelMissing($transaction);
|
|
|
88
|
+
|
|
|
89
|
+ $this->assertDatabaseCount('journal_entries', 0);
|
|
|
90
|
+});
|
|
|
91
|
+
|
|
|
92
|
+it('handles multi-currency transfers without conversion when the source bank account is in the default currency', function () {
|
|
|
93
|
+ $foreignBankAccount = Account::factory()
|
|
|
94
|
+ ->withForeignBankAccount('Foreign Bank Account', 'EUR', 0.92)
|
|
|
95
|
+ ->create();
|
|
|
96
|
+
|
|
|
97
|
+ $transaction = Transaction::factory()
|
|
|
98
|
+ ->forDefaultBankAccount()
|
|
|
99
|
+ ->forDestinationBankAccount($foreignBankAccount)
|
|
|
100
|
+ ->asTransfer(1500)
|
|
|
101
|
+ ->create();
|
|
|
102
|
+
|
|
|
103
|
+ [$debitAccount, $creditAccount] = getTransactionDebitAndCreditAccounts($transaction);
|
|
|
104
|
+
|
|
|
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');
|
|
|
111
|
+});
|
|
|
112
|
+
|
|
|
113
|
+it('handles multi-currency transfers correctly', function () {
|
|
|
114
|
+ $foreignBankAccount = Account::factory()
|
|
|
115
|
+ ->withForeignBankAccount('CAD Bank Account', 'CAD', 1.36)
|
|
|
116
|
+ ->create();
|
|
|
117
|
+
|
|
|
118
|
+ ConfigureCurrencies::syncCurrencies();
|
|
|
119
|
+
|
|
|
120
|
+ // Create a transfer of 1500 CAD from the foreign bank account to USD bank account
|
|
|
121
|
+ $transaction = Transaction::factory()
|
|
|
122
|
+ ->forBankAccount($foreignBankAccount->bankAccount)
|
|
|
123
|
+ ->forDestinationBankAccount()
|
|
|
124
|
+ ->asTransfer(1500)
|
|
|
125
|
+ ->create();
|
|
|
126
|
+
|
|
|
127
|
+ [$debitAccount, $creditAccount] = getTransactionDebitAndCreditAccounts($transaction);
|
|
|
128
|
+
|
|
|
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
|
|
|
132
|
+
|
|
|
133
|
+ // The 1500 CAD is worth 1102.94 USD (1500 CAD / 1.36)
|
|
|
134
|
+ $expectedUSDValue = round(1500 / 1.36, 2);
|
|
|
135
|
+
|
|
|
136
|
+ // Verify that the debit is 1102.94 USD and the credit is 1500 CAD converted to 1102.94 USD
|
|
|
137
|
+ // 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');
|
|
|
141
|
+});
|
|
|
142
|
+
|
|
|
143
|
+it('handles multi-currency deposits correctly', function () {
|
|
|
144
|
+ $foreignBankAccount = Account::factory()
|
|
|
145
|
+ ->withForeignBankAccount('BHD Bank Account', 'BHD', 0.38)
|
|
|
146
|
+ ->create();
|
|
|
147
|
+
|
|
|
148
|
+ ConfigureCurrencies::syncCurrencies();
|
|
|
149
|
+
|
|
|
150
|
+ // Create a deposit of 1500 BHD to the foreign bank account
|
|
|
151
|
+ $transaction = Transaction::factory()
|
|
|
152
|
+ ->forBankAccount($foreignBankAccount->bankAccount)
|
|
|
153
|
+ ->forUncategorizedRevenue()
|
|
|
154
|
+ ->asDeposit(1500)
|
|
|
155
|
+ ->create();
|
|
|
156
|
+
|
|
|
157
|
+ [$debitAccount, $creditAccount] = getTransactionDebitAndCreditAccounts($transaction);
|
|
|
158
|
+
|
|
|
159
|
+ expect($transaction->journalEntries->count())->toBe(2)
|
|
|
160
|
+ ->and($debitAccount->name)->toBe('BHD Bank Account') // Debit: Foreign (BHD) account
|
|
|
161
|
+ ->and($creditAccount->name)->toBe('Uncategorized Income'); // Credit: Uncategorized Income (USD) account
|
|
|
162
|
+
|
|
|
163
|
+ // Convert to USD using the rate 0.38 BHD per USD
|
|
|
164
|
+ $expectedUSDValue = round(1500 / 0.38, 2);
|
|
|
165
|
+
|
|
|
166
|
+ // 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)
|
|
|
170
|
+});
|
|
|
171
|
+
|
|
|
172
|
+it('handles multi-currency withdrawals correctly', function () {
|
|
|
173
|
+ $foreignBankAccount = Account::factory()
|
|
|
174
|
+ ->withForeignBankAccount('Foreign Bank Account', 'GBP', 0.76) // GBP account
|
|
|
175
|
+ ->create();
|
|
|
176
|
+
|
|
|
177
|
+ ConfigureCurrencies::syncCurrencies();
|
|
|
178
|
+
|
|
|
179
|
+ $transaction = Transaction::factory()
|
|
|
180
|
+ ->forBankAccount($foreignBankAccount->bankAccount)
|
|
|
181
|
+ ->forUncategorizedExpense()
|
|
|
182
|
+ ->asWithdrawal(1500)
|
|
|
183
|
+ ->create();
|
|
|
184
|
+
|
|
|
185
|
+ [$debitAccount, $creditAccount] = getTransactionDebitAndCreditAccounts($transaction);
|
|
|
186
|
+
|
|
|
187
|
+ expect($transaction->journalEntries->count())->toBe(2)
|
|
|
188
|
+ ->and($debitAccount->name)->toBe('Uncategorized Expense')
|
|
|
189
|
+ ->and($creditAccount->name)->toBe('Foreign Bank Account');
|
|
|
190
|
+
|
|
|
191
|
+ $expectedUSDValue = round(1500 / 0.76, 2);
|
|
|
192
|
+
|
|
|
193
|
+ expect($transaction->journalEntries->sumDebits()->getValue())->toEqual($expectedUSDValue)
|
|
|
194
|
+ ->and($transaction->journalEntries->sumCredits()->getValue())->toEqual($expectedUSDValue)
|
|
|
195
|
+ ->and($transaction->amount)->toEqual('1,500.00');
|
|
|
196
|
+});
|