Andrew Wallo 6 miesięcy temu
rodzic
commit
0270359f26

+ 2
- 2
app/Models/Accounting/Bill.php Wyświetl plik

257
             'description' => $transactionDescription,
257
             'description' => $transactionDescription,
258
             'notes' => $data['notes'] ?? null,
258
             'notes' => $data['notes'] ?? null,
259
             'meta' => [
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 Wyświetl plik

311
         $invoiceCurrency = $this->currency_code;
311
         $invoiceCurrency = $this->currency_code;
312
         $requiresConversion = $invoiceCurrency !== $bankAccountCurrency;
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
         if ($requiresConversion) {
317
         if ($requiresConversion) {
315
-            $amountInInvoiceCurrencyCents = CurrencyConverter::convertToCents($data['amount'], $invoiceCurrency);
316
             $amountInBankCurrencyCents = CurrencyConverter::convertBalance(
318
             $amountInBankCurrencyCents = CurrencyConverter::convertBalance(
317
                 $amountInInvoiceCurrencyCents,
319
                 $amountInInvoiceCurrencyCents,
318
                 $invoiceCurrency,
320
                 $invoiceCurrency,
338
             'account_id' => Account::getAccountsReceivableAccount()->id,
340
             'account_id' => Account::getAccountsReceivableAccount()->id,
339
             'description' => $transactionDescription,
341
             'description' => $transactionDescription,
340
             'notes' => $data['notes'] ?? null,
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 Wyświetl plik

113
             ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
113
             ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
114
             ->get()
114
             ->get()
115
             ->sum(function (Transaction $transaction) use ($invoiceCurrency) {
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
                 $bankAccountCurrency = $transaction->bankAccount->account->currency_code;
126
                 $bankAccountCurrency = $transaction->bankAccount->account->currency_code;
117
                 $amountCents = (int) $transaction->getRawOriginal('amount');
127
                 $amountCents = (int) $transaction->getRawOriginal('amount');
118
 
128
 
123
             ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
133
             ->when($excludedTransaction, fn (Builder $query) => $query->whereKeyNot($excludedTransaction->getKey()))
124
             ->get()
134
             ->get()
125
             ->sum(function (Transaction $transaction) use ($invoiceCurrency) {
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
                 $bankAccountCurrency = $transaction->bankAccount->account->currency_code;
146
                 $bankAccountCurrency = $transaction->bankAccount->account->currency_code;
127
                 $amountCents = (int) $transaction->getRawOriginal('amount');
147
                 $amountCents = (int) $transaction->getRawOriginal('amount');
128
 
148
 
136
         $newStatus = match (true) {
156
         $newStatus = match (true) {
137
             $totalPaidInInvoiceCurrencyCents > $invoiceTotalInInvoiceCurrencyCents => InvoiceStatus::Overpaid,
157
             $totalPaidInInvoiceCurrencyCents > $invoiceTotalInInvoiceCurrencyCents => InvoiceStatus::Overpaid,
138
             $totalPaidInInvoiceCurrencyCents === $invoiceTotalInInvoiceCurrencyCents => InvoiceStatus::Paid,
158
             $totalPaidInInvoiceCurrencyCents === $invoiceTotalInInvoiceCurrencyCents => InvoiceStatus::Paid,
159
+            $totalPaidInInvoiceCurrencyCents === 0 => $invoice->last_sent_at ? InvoiceStatus::Sent : InvoiceStatus::Unsent,
139
             default => InvoiceStatus::Partial,
160
             default => InvoiceStatus::Partial,
140
         };
161
         };
141
 
162
 
168
             ->sum(function (Transaction $transaction) use ($billCurrency) {
189
             ->sum(function (Transaction $transaction) use ($billCurrency) {
169
                 // If the transaction has stored the original bill amount in metadata, use that
190
                 // If the transaction has stored the original bill amount in metadata, use that
170
                 if (! empty($transaction->meta) &&
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
                 // Fall back to conversion if metadata is not available
199
                 // Fall back to conversion if metadata is not available
188
 
209
 
189
         $newStatus = match (true) {
210
         $newStatus = match (true) {
190
             $totalPaidInBillCurrencyCents >= $billTotalInBillCurrencyCents => BillStatus::Paid,
211
             $totalPaidInBillCurrencyCents >= $billTotalInBillCurrencyCents => BillStatus::Paid,
212
+            $totalPaidInBillCurrencyCents === 0 => BillStatus::Open,
191
             default => BillStatus::Partial,
213
             default => BillStatus::Partial,
192
         };
214
         };
193
 
215
 

Ładowanie…
Anuluj
Zapisz