|
@@ -107,21 +107,35 @@ class TransactionObserver
|
107
|
107
|
return;
|
108
|
108
|
}
|
109
|
109
|
|
110
|
|
- $depositTotal = (int) $invoice->deposits()
|
|
110
|
+ $invoiceCurrency = $invoice->currency_code;
|
|
111
|
+
|
|
112
|
+ $depositTotalInInvoiceCurrencyCents = (int) $invoice->deposits()
|
111
|
113
|
->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
|
112
|
|
- ->sum('amount');
|
|
114
|
+ ->get()
|
|
115
|
+ ->sum(function (Transaction $transaction) use ($invoiceCurrency) {
|
|
116
|
+ $bankAccountCurrency = $transaction->bankAccount->account->currency_code;
|
|
117
|
+ $amountCents = (int) $transaction->getRawOriginal('amount');
|
|
118
|
+
|
|
119
|
+ return CurrencyConverter::convertBalance($amountCents, $bankAccountCurrency, $invoiceCurrency);
|
|
120
|
+ });
|
113
|
121
|
|
114
|
|
- $withdrawalTotal = (int) $invoice->withdrawals()
|
|
122
|
+ $withdrawalTotalInInvoiceCurrencyCents = (int) $invoice->withdrawals()
|
115
|
123
|
->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
|
116
|
|
- ->sum('amount');
|
|
124
|
+ ->get()
|
|
125
|
+ ->sum(function (Transaction $transaction) use ($invoiceCurrency) {
|
|
126
|
+ $bankAccountCurrency = $transaction->bankAccount->account->currency_code;
|
|
127
|
+ $amountCents = (int) $transaction->getRawOriginal('amount');
|
|
128
|
+
|
|
129
|
+ return CurrencyConverter::convertBalance($amountCents, $bankAccountCurrency, $invoiceCurrency);
|
|
130
|
+ });
|
117
|
131
|
|
118
|
|
- $totalPaid = $depositTotal - $withdrawalTotal;
|
|
132
|
+ $totalPaidInInvoiceCurrencyCents = $depositTotalInInvoiceCurrencyCents - $withdrawalTotalInInvoiceCurrencyCents;
|
119
|
133
|
|
120
|
|
- $invoiceTotal = (int) $invoice->getRawOriginal('total');
|
|
134
|
+ $invoiceTotalInInvoiceCurrencyCents = (int) $invoice->getRawOriginal('total');
|
121
|
135
|
|
122
|
136
|
$newStatus = match (true) {
|
123
|
|
- $totalPaid > $invoiceTotal => InvoiceStatus::Overpaid,
|
124
|
|
- $totalPaid === $invoiceTotal => InvoiceStatus::Paid,
|
|
137
|
+ $totalPaidInInvoiceCurrencyCents > $invoiceTotalInInvoiceCurrencyCents => InvoiceStatus::Overpaid,
|
|
138
|
+ $totalPaidInInvoiceCurrencyCents === $invoiceTotalInInvoiceCurrencyCents => InvoiceStatus::Paid,
|
125
|
139
|
default => InvoiceStatus::Partial,
|
126
|
140
|
};
|
127
|
141
|
|
|
@@ -134,7 +148,7 @@ class TransactionObserver
|
134
|
148
|
}
|
135
|
149
|
|
136
|
150
|
$invoice->update([
|
137
|
|
- 'amount_paid' => CurrencyConverter::convertCentsToFloat($totalPaid),
|
|
151
|
+ 'amount_paid' => CurrencyConverter::convertCentsToFormatSimple($totalPaidInInvoiceCurrencyCents, $invoiceCurrency),
|
138
|
152
|
'status' => $newStatus,
|
139
|
153
|
'paid_at' => $paidAt,
|
140
|
154
|
]);
|
|
@@ -146,15 +160,24 @@ class TransactionObserver
|
146
|
160
|
return;
|
147
|
161
|
}
|
148
|
162
|
|
149
|
|
- $withdrawalTotal = (int) $bill->withdrawals()
|
|
163
|
+ $billCurrency = $bill->currency_code;
|
|
164
|
+
|
|
165
|
+ $withdrawalTotalInBillCurrencyCents = (int) $bill->withdrawals()
|
150
|
166
|
->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
|
151
|
|
- ->sum('amount');
|
|
167
|
+ ->get()
|
|
168
|
+ ->sum(function (Transaction $transaction) use ($billCurrency) {
|
|
169
|
+ $bankAccountCurrency = $transaction->bankAccount->account->currency_code;
|
|
170
|
+ $amountCents = (int) $transaction->getRawOriginal('amount');
|
|
171
|
+
|
|
172
|
+ return CurrencyConverter::convertBalance($amountCents, $bankAccountCurrency, $billCurrency);
|
|
173
|
+ });
|
|
174
|
+
|
|
175
|
+ $totalPaidInBillCurrencyCents = $withdrawalTotalInBillCurrencyCents;
|
152
|
176
|
|
153
|
|
- $totalPaid = $withdrawalTotal;
|
154
|
|
- $billTotal = (int) $bill->getRawOriginal('total');
|
|
177
|
+ $billTotalInBillCurrencyCents = (int) $bill->getRawOriginal('total');
|
155
|
178
|
|
156
|
179
|
$newStatus = match (true) {
|
157
|
|
- $totalPaid >= $billTotal => BillStatus::Paid,
|
|
180
|
+ $totalPaidInBillCurrencyCents >= $billTotalInBillCurrencyCents => BillStatus::Paid,
|
158
|
181
|
default => BillStatus::Partial,
|
159
|
182
|
};
|
160
|
183
|
|
|
@@ -167,7 +190,7 @@ class TransactionObserver
|
167
|
190
|
}
|
168
|
191
|
|
169
|
192
|
$bill->update([
|
170
|
|
- 'amount_paid' => CurrencyConverter::convertCentsToFloat($totalPaid),
|
|
193
|
+ 'amount_paid' => CurrencyConverter::convertCentsToFormatSimple($totalPaidInBillCurrencyCents, $billCurrency),
|
171
|
194
|
'status' => $newStatus,
|
172
|
195
|
'paid_at' => $paidAt,
|
173
|
196
|
]);
|