|
@@ -3,7 +3,6 @@
|
3
|
3
|
namespace App\Observers;
|
4
|
4
|
|
5
|
5
|
use App\Enums\Accounting\JournalEntryType;
|
6
|
|
-use App\Enums\Accounting\TransactionType;
|
7
|
6
|
use App\Models\Accounting\Account;
|
8
|
7
|
use App\Models\Accounting\JournalEntry;
|
9
|
8
|
use App\Models\Accounting\Transaction;
|
|
@@ -18,30 +17,74 @@ class TransactionObserver
|
18
|
17
|
*/
|
19
|
18
|
public function created(Transaction $transaction): void
|
20
|
19
|
{
|
21
|
|
- if ($transaction->type === TransactionType::Journal) {
|
|
20
|
+ if ($transaction->type->isJournal()) {
|
22
|
21
|
return;
|
23
|
22
|
}
|
24
|
23
|
|
25
|
|
- $chartAccount = $transaction->account;
|
26
|
|
- $bankAccount = $transaction->bankAccount->account;
|
|
24
|
+ [$debitAccount, $creditAccount] = $this->determineAccounts($transaction);
|
27
|
25
|
|
28
|
|
- $debitAccount = $transaction->type === TransactionType::Withdrawal ? $chartAccount : $bankAccount;
|
29
|
|
- $creditAccount = $transaction->type === TransactionType::Withdrawal ? $bankAccount : $chartAccount;
|
|
26
|
+ if ($debitAccount === null || $creditAccount === null) {
|
|
27
|
+ return;
|
|
28
|
+ }
|
30
|
29
|
|
31
|
30
|
$this->createJournalEntries($transaction, $debitAccount, $creditAccount);
|
32
|
31
|
}
|
33
|
32
|
|
34
|
|
- private function createJournalEntries(Transaction $transaction, Account $debitAccount, Account $creditAccount): void
|
|
33
|
+ /**
|
|
34
|
+ * Handle the Transaction "updated" event.
|
|
35
|
+ */
|
|
36
|
+ public function updated(Transaction $transaction): void
|
35
|
37
|
{
|
36
|
|
- $defaultCurrency = CurrencyAccessor::getDefaultCurrency();
|
37
|
|
- $transactionCurrency = $transaction->bankAccount->account->currency_code; // only account which would have a different currency compared to the default currency
|
|
38
|
+ if ($transaction->type->isJournal() || $this->hasRelevantChanges($transaction) === false) {
|
|
39
|
+ return;
|
|
40
|
+ }
|
38
|
41
|
|
39
|
|
- if ($transactionCurrency !== $defaultCurrency) {
|
40
|
|
- $convertedTransactionAmount = $this->convertToDefaultCurrency($transaction->amount, $transactionCurrency, $defaultCurrency);
|
41
|
|
- } else {
|
42
|
|
- $convertedTransactionAmount = $transaction->amount;
|
|
42
|
+ $journalEntries = $transaction->journalEntries;
|
|
43
|
+
|
|
44
|
+ $debitEntry = $journalEntries->where('type', JournalEntryType::Debit)->first();
|
|
45
|
+ $creditEntry = $journalEntries->where('type', JournalEntryType::Credit)->first();
|
|
46
|
+
|
|
47
|
+ if ($debitEntry === null || $creditEntry === null) {
|
|
48
|
+ return;
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ [$debitAccount, $creditAccount] = $this->determineAccounts($transaction);
|
|
52
|
+
|
|
53
|
+ if ($debitAccount === null || $creditAccount === null) {
|
|
54
|
+ return;
|
43
|
55
|
}
|
44
|
56
|
|
|
57
|
+ $convertedTransactionAmount = $this->getConvertedTransactionAmount($transaction);
|
|
58
|
+
|
|
59
|
+ $this->updateJournalEntriesForTransaction($debitEntry, $debitAccount, $convertedTransactionAmount);
|
|
60
|
+ $this->updateJournalEntriesForTransaction($creditEntry, $creditAccount, $convertedTransactionAmount);
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ /**
|
|
64
|
+ * Handle the Transaction "deleting" event.
|
|
65
|
+ */
|
|
66
|
+ public function deleting(Transaction $transaction): void
|
|
67
|
+ {
|
|
68
|
+ DB::transaction(static function () use ($transaction) {
|
|
69
|
+ $transaction->journalEntries()->each(fn (JournalEntry $entry) => $entry->delete());
|
|
70
|
+ });
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ private function determineAccounts(Transaction $transaction): array
|
|
74
|
+ {
|
|
75
|
+ $chartAccount = $transaction->account;
|
|
76
|
+ $bankAccount = $transaction->bankAccount?->account;
|
|
77
|
+
|
|
78
|
+ $debitAccount = $transaction->type->isWithdrawal() ? $chartAccount : $bankAccount;
|
|
79
|
+ $creditAccount = $transaction->type->isWithdrawal() ? $bankAccount : $chartAccount;
|
|
80
|
+
|
|
81
|
+ return [$debitAccount, $creditAccount];
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ private function createJournalEntries(Transaction $transaction, Account $debitAccount, Account $creditAccount): void
|
|
85
|
+ {
|
|
86
|
+ $convertedTransactionAmount = $this->getConvertedTransactionAmount($transaction);
|
|
87
|
+
|
45
|
88
|
$debitAccount->journalEntries()->create([
|
46
|
89
|
'company_id' => $transaction->company_id,
|
47
|
90
|
'transaction_id' => $transaction->id,
|
|
@@ -59,62 +102,33 @@ class TransactionObserver
|
59
|
102
|
]);
|
60
|
103
|
}
|
61
|
104
|
|
62
|
|
- private function convertToDefaultCurrency(string $amount, string $fromCurrency, string $toCurrency): string
|
|
105
|
+ private function getConvertedTransactionAmount(Transaction $transaction): string
|
63
|
106
|
{
|
64
|
|
- $amountInCents = CurrencyConverter::prepareForAccessor($amount, $fromCurrency);
|
65
|
|
-
|
66
|
|
- $convertedAmountInCents = CurrencyConverter::convertBalance($amountInCents, $fromCurrency, $toCurrency);
|
67
|
|
-
|
68
|
|
- return CurrencyConverter::prepareForMutator($convertedAmountInCents, $toCurrency);
|
69
|
|
- }
|
70
|
|
-
|
71
|
|
- /**
|
72
|
|
- * Handle the Transaction "updated" event.
|
73
|
|
- */
|
74
|
|
- public function updated(Transaction $transaction): void
|
75
|
|
- {
|
76
|
|
- if ($transaction->type === TransactionType::Journal || $this->hasRelevantChanges($transaction) === false) {
|
77
|
|
- return;
|
78
|
|
- }
|
79
|
|
-
|
80
|
|
- $chartAccount = $transaction->account;
|
81
|
|
- $bankAccount = $transaction->bankAccount?->account;
|
82
|
|
-
|
83
|
|
- if (! $chartAccount || ! $bankAccount) {
|
84
|
|
- return;
|
85
|
|
- }
|
86
|
|
-
|
87
|
|
- $journalEntries = $transaction->journalEntries;
|
88
|
|
-
|
89
|
|
- $debitEntry = $journalEntries->where('type', JournalEntryType::Debit)->first();
|
90
|
|
- $creditEntry = $journalEntries->where('type', JournalEntryType::Credit)->first();
|
91
|
|
-
|
92
|
|
- if (! $debitEntry || ! $creditEntry) {
|
93
|
|
- return;
|
94
|
|
- }
|
95
|
|
-
|
96
|
107
|
$defaultCurrency = CurrencyAccessor::getDefaultCurrency();
|
97
|
108
|
$transactionCurrency = $transaction->bankAccount->account->currency_code; // only account which would have a different currency compared to the default currency
|
98
|
109
|
|
99
|
110
|
if ($transactionCurrency !== $defaultCurrency) {
|
100
|
|
- $convertedTransactionAmount = $this->convertToDefaultCurrency($transaction->amount, $transactionCurrency, $defaultCurrency);
|
101
|
|
- } else {
|
102
|
|
- $convertedTransactionAmount = $transaction->amount;
|
|
111
|
+ return $this->convertToDefaultCurrency($transaction->amount, $transactionCurrency, $defaultCurrency);
|
103
|
112
|
}
|
104
|
113
|
|
105
|
|
- $debitAccount = $transaction->type === TransactionType::Withdrawal ? $chartAccount : $bankAccount;
|
106
|
|
- $creditAccount = $transaction->type === TransactionType::Withdrawal ? $bankAccount : $chartAccount;
|
|
114
|
+ return $transaction->amount;
|
|
115
|
+ }
|
107
|
116
|
|
108
|
|
- $this->updateJournalEntriesForTransaction($debitEntry, $debitAccount, $convertedTransactionAmount);
|
109
|
|
- $this->updateJournalEntriesForTransaction($creditEntry, $creditAccount, $convertedTransactionAmount);
|
|
117
|
+ private function convertToDefaultCurrency(string $amount, string $fromCurrency, string $toCurrency): string
|
|
118
|
+ {
|
|
119
|
+ $amountInCents = CurrencyConverter::prepareForAccessor($amount, $fromCurrency);
|
|
120
|
+
|
|
121
|
+ $convertedAmountInCents = CurrencyConverter::convertBalance($amountInCents, $fromCurrency, $toCurrency);
|
|
122
|
+
|
|
123
|
+ return CurrencyConverter::prepareForMutator($convertedAmountInCents, $toCurrency);
|
110
|
124
|
}
|
111
|
125
|
|
112
|
|
- protected function hasRelevantChanges(Transaction $transaction): bool
|
|
126
|
+ private function hasRelevantChanges(Transaction $transaction): bool
|
113
|
127
|
{
|
114
|
128
|
return $transaction->wasChanged(['amount', 'account_id', 'bank_account_id', 'type']);
|
115
|
129
|
}
|
116
|
130
|
|
117
|
|
- protected function updateJournalEntriesForTransaction(JournalEntry $journalEntry, Account $account, string $convertedTransactionAmount): void
|
|
131
|
+ private function updateJournalEntriesForTransaction(JournalEntry $journalEntry, Account $account, string $convertedTransactionAmount): void
|
118
|
132
|
{
|
119
|
133
|
DB::transaction(static function () use ($journalEntry, $account, $convertedTransactionAmount) {
|
120
|
134
|
$journalEntry->update([
|
|
@@ -123,38 +137,4 @@ class TransactionObserver
|
123
|
137
|
]);
|
124
|
138
|
});
|
125
|
139
|
}
|
126
|
|
-
|
127
|
|
- /**
|
128
|
|
- * Handle the Transaction "deleting" event.
|
129
|
|
- */
|
130
|
|
- public function deleting(Transaction $transaction): void
|
131
|
|
- {
|
132
|
|
- DB::transaction(static function () use ($transaction) {
|
133
|
|
- $transaction->journalEntries()->each(fn (JournalEntry $entry) => $entry->delete());
|
134
|
|
- });
|
135
|
|
- }
|
136
|
|
-
|
137
|
|
- /**
|
138
|
|
- * Handle the Transaction "deleted" event.
|
139
|
|
- */
|
140
|
|
- public function deleted(Transaction $transaction): void
|
141
|
|
- {
|
142
|
|
- //
|
143
|
|
- }
|
144
|
|
-
|
145
|
|
- /**
|
146
|
|
- * Handle the Transaction "restored" event.
|
147
|
|
- */
|
148
|
|
- public function restored(Transaction $transaction): void
|
149
|
|
- {
|
150
|
|
- //
|
151
|
|
- }
|
152
|
|
-
|
153
|
|
- /**
|
154
|
|
- * Handle the Transaction "force deleted" event.
|
155
|
|
- */
|
156
|
|
- public function forceDeleted(Transaction $transaction): void
|
157
|
|
- {
|
158
|
|
- //
|
159
|
|
- }
|
160
|
140
|
}
|