Andrew Wallo 6 months ago
parent
commit
0270359f26

+ 2
- 2
app/Models/Accounting/Bill.php View File

@@ -257,8 +257,8 @@ class Bill extends Document
257 257
             'description' => $transactionDescription,
258 258
             'notes' => $data['notes'] ?? null,
259 259
             'meta' => [
260
-                'original_bill_currency' => $billCurrency,
261
-                'amount_in_bill_currency_cents' => $amountInBillCurrencyCents,
260
+                'original_document_currency' => $billCurrency,
261
+                'amount_in_document_currency_cents' => $amountInBillCurrencyCents,
262 262
             ],
263 263
         ]);
264 264
     }

+ 7
- 1
app/Models/Accounting/Invoice.php View File

@@ -311,8 +311,10 @@ class Invoice extends Document
311 311
         $invoiceCurrency = $this->currency_code;
312 312
         $requiresConversion = $invoiceCurrency !== $bankAccountCurrency;
313 313
 
314
+        // Store the original payment amount in invoice currency before any conversion
315
+        $amountInInvoiceCurrencyCents = CurrencyConverter::convertToCents($data['amount'], $invoiceCurrency);
316
+
314 317
         if ($requiresConversion) {
315
-            $amountInInvoiceCurrencyCents = CurrencyConverter::convertToCents($data['amount'], $invoiceCurrency);
316 318
             $amountInBankCurrencyCents = CurrencyConverter::convertBalance(
317 319
                 $amountInInvoiceCurrencyCents,
318 320
                 $invoiceCurrency,
@@ -338,6 +340,10 @@ class Invoice extends Document
338 340
             'account_id' => Account::getAccountsReceivableAccount()->id,
339 341
             'description' => $transactionDescription,
340 342
             'notes' => $data['notes'] ?? null,
343
+            'meta' => [
344
+                'original_document_currency' => $invoiceCurrency,
345
+                'amount_in_document_currency_cents' => $amountInInvoiceCurrencyCents,
346
+            ],
341 347
         ]);
342 348
     }
343 349
 

+ 26
- 4
app/Observers/TransactionObserver.php View File

@@ -113,6 +113,16 @@ class TransactionObserver
113 113
             ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
114 114
             ->get()
115 115
             ->sum(function (Transaction $transaction) use ($invoiceCurrency) {
116
+                // If the transaction has stored the original invoice amount in metadata, use that
117
+                if (! empty($transaction->meta) &&
118
+                    isset($transaction->meta['original_document_currency']) &&
119
+                    $transaction->meta['original_document_currency'] === $invoiceCurrency &&
120
+                    isset($transaction->meta['amount_in_document_currency_cents'])) {
121
+
122
+                    return (int) $transaction->meta['amount_in_document_currency_cents'];
123
+                }
124
+
125
+                // Fall back to conversion if metadata is not available
116 126
                 $bankAccountCurrency = $transaction->bankAccount->account->currency_code;
117 127
                 $amountCents = (int) $transaction->getRawOriginal('amount');
118 128
 
@@ -123,6 +133,16 @@ class TransactionObserver
123 133
             ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
124 134
             ->get()
125 135
             ->sum(function (Transaction $transaction) use ($invoiceCurrency) {
136
+                // If the transaction has stored the original invoice amount in metadata, use that
137
+                if (! empty($transaction->meta) &&
138
+                    isset($transaction->meta['original_document_currency']) &&
139
+                    $transaction->meta['original_document_currency'] === $invoiceCurrency &&
140
+                    isset($transaction->meta['amount_in_document_currency_cents'])) {
141
+
142
+                    return (int) $transaction->meta['amount_in_document_currency_cents'];
143
+                }
144
+
145
+                // Fall back to conversion if metadata is not available
126 146
                 $bankAccountCurrency = $transaction->bankAccount->account->currency_code;
127 147
                 $amountCents = (int) $transaction->getRawOriginal('amount');
128 148
 
@@ -136,6 +156,7 @@ class TransactionObserver
136 156
         $newStatus = match (true) {
137 157
             $totalPaidInInvoiceCurrencyCents > $invoiceTotalInInvoiceCurrencyCents => InvoiceStatus::Overpaid,
138 158
             $totalPaidInInvoiceCurrencyCents === $invoiceTotalInInvoiceCurrencyCents => InvoiceStatus::Paid,
159
+            $totalPaidInInvoiceCurrencyCents === 0 => $invoice->last_sent_at ? InvoiceStatus::Sent : InvoiceStatus::Unsent,
139 160
             default => InvoiceStatus::Partial,
140 161
         };
141 162
 
@@ -168,11 +189,11 @@ class TransactionObserver
168 189
             ->sum(function (Transaction $transaction) use ($billCurrency) {
169 190
                 // If the transaction has stored the original bill amount in metadata, use that
170 191
                 if (! empty($transaction->meta) &&
171
-                    isset($transaction->meta['original_bill_currency']) &&
172
-                    $transaction->meta['original_bill_currency'] === $billCurrency &&
173
-                    isset($transaction->meta['amount_in_bill_currency_cents'])) {
192
+                    isset($transaction->meta['original_document_currency']) &&
193
+                    $transaction->meta['original_document_currency'] === $billCurrency &&
194
+                    isset($transaction->meta['amount_in_document_currency_cents'])) {
174 195
 
175
-                    return (int) $transaction->meta['amount_in_bill_currency_cents'];
196
+                    return (int) $transaction->meta['amount_in_document_currency_cents'];
176 197
                 }
177 198
 
178 199
                 // Fall back to conversion if metadata is not available
@@ -188,6 +209,7 @@ class TransactionObserver
188 209
 
189 210
         $newStatus = match (true) {
190 211
             $totalPaidInBillCurrencyCents >= $billTotalInBillCurrencyCents => BillStatus::Paid,
212
+            $totalPaidInBillCurrencyCents === 0 => BillStatus::Open,
191 213
             default => BillStatus::Partial,
192 214
         };
193 215
 

Loading…
Cancel
Save