Andrew Wallo 4 months ago
parent
commit
1173ba89c0

+ 3
- 3
app/Filament/Company/Resources/Purchases/BillResource.php View File

624
                                 ->label('Notes'),
624
                                 ->label('Notes'),
625
                         ])
625
                         ])
626
                         ->before(function (Collection $records, Tables\Actions\BulkAction $action, array $data) {
626
                         ->before(function (Collection $records, Tables\Actions\BulkAction $action, array $data) {
627
-                            $totalPaymentAmount = CurrencyConverter::convertToCents($data['amount']);
627
+                            $totalPaymentAmount = $data['amount'] ?? 0;
628
                             $totalAmountDue = $records->sum(fn (Bill $bill) => $bill->getRawOriginal('amount_due'));
628
                             $totalAmountDue = $records->sum(fn (Bill $bill) => $bill->getRawOriginal('amount_due'));
629
 
629
 
630
                             if ($totalPaymentAmount > $totalAmountDue) {
630
                             if ($totalPaymentAmount > $totalAmountDue) {
641
                             }
641
                             }
642
                         })
642
                         })
643
                         ->action(function (Collection $records, Tables\Actions\BulkAction $action, array $data) {
643
                         ->action(function (Collection $records, Tables\Actions\BulkAction $action, array $data) {
644
-                            $totalPaymentAmount = CurrencyConverter::convertToCents($data['amount']);
644
+                            $totalPaymentAmount = $data['amount'] ?? 0;
645
                             $remainingAmount = $totalPaymentAmount;
645
                             $remainingAmount = $totalPaymentAmount;
646
 
646
 
647
                             $records->each(function (Bill $record) use (&$remainingAmount, $data) {
647
                             $records->each(function (Bill $record) use (&$remainingAmount, $data) {
652
                                 }
652
                                 }
653
 
653
 
654
                                 $paymentAmount = min($amountDue, $remainingAmount);
654
                                 $paymentAmount = min($amountDue, $remainingAmount);
655
-                                $data['amount'] = CurrencyConverter::convertCentsToFormatSimple($paymentAmount);
655
+                                $data['amount'] = $paymentAmount;
656
 
656
 
657
                                 $record->recordPayment($data);
657
                                 $record->recordPayment($data);
658
                                 $remainingAmount -= $paymentAmount;
658
                                 $remainingAmount -= $paymentAmount;

+ 3
- 3
app/Filament/Company/Resources/Sales/InvoiceResource.php View File

737
                                 ->label('Notes'),
737
                                 ->label('Notes'),
738
                         ])
738
                         ])
739
                         ->before(function (DocumentCollection $records, Tables\Actions\BulkAction $action, array $data) {
739
                         ->before(function (DocumentCollection $records, Tables\Actions\BulkAction $action, array $data) {
740
-                            $totalPaymentAmount = CurrencyConverter::convertToCents($data['amount']);
740
+                            $totalPaymentAmount = $data['amount'] ?? 0;
741
                             $totalAmountDue = $records->sumMoneyInCents('amount_due');
741
                             $totalAmountDue = $records->sumMoneyInCents('amount_due');
742
 
742
 
743
                             if ($totalPaymentAmount > $totalAmountDue) {
743
                             if ($totalPaymentAmount > $totalAmountDue) {
754
                             }
754
                             }
755
                         })
755
                         })
756
                         ->action(function (DocumentCollection $records, Tables\Actions\BulkAction $action, array $data) {
756
                         ->action(function (DocumentCollection $records, Tables\Actions\BulkAction $action, array $data) {
757
-                            $totalPaymentAmount = CurrencyConverter::convertToCents($data['amount']);
757
+                            $totalPaymentAmount = $data['amount'] ?? 0;
758
 
758
 
759
                             $remainingAmount = $totalPaymentAmount;
759
                             $remainingAmount = $totalPaymentAmount;
760
 
760
 
767
 
767
 
768
                                 $paymentAmount = min($amountDue, $remainingAmount);
768
                                 $paymentAmount = min($amountDue, $remainingAmount);
769
 
769
 
770
-                                $data['amount'] = CurrencyConverter::convertCentsToFormatSimple($paymentAmount);
770
+                                $data['amount'] = $paymentAmount;
771
 
771
 
772
                                 $record->recordPayment($data);
772
                                 $record->recordPayment($data);
773
 
773
 

+ 3
- 6
app/Models/Accounting/Bill.php View File

228
         $requiresConversion = $billCurrency !== $bankAccountCurrency;
228
         $requiresConversion = $billCurrency !== $bankAccountCurrency;
229
 
229
 
230
         // Store the original payment amount in bill currency before any conversion
230
         // Store the original payment amount in bill currency before any conversion
231
-        $amountInBillCurrencyCents = CurrencyConverter::convertToCents($data['amount'], $billCurrency);
231
+        $amountInBillCurrencyCents = $data['amount'];
232
 
232
 
233
         if ($requiresConversion) {
233
         if ($requiresConversion) {
234
             $amountInBankCurrencyCents = CurrencyConverter::convertBalance(
234
             $amountInBankCurrencyCents = CurrencyConverter::convertBalance(
236
                 $billCurrency,
236
                 $billCurrency,
237
                 $bankAccountCurrency
237
                 $bankAccountCurrency
238
             );
238
             );
239
-            $formattedAmountForBankCurrency = CurrencyConverter::convertCentsToFormatSimple(
240
-                $amountInBankCurrencyCents,
241
-                $bankAccountCurrency
242
-            );
239
+            $formattedAmountForBankCurrency = $amountInBankCurrencyCents;
243
         } else {
240
         } else {
244
-            $formattedAmountForBankCurrency = $data['amount']; // Already in simple format
241
+            $formattedAmountForBankCurrency = $amountInBillCurrencyCents;
245
         }
242
         }
246
 
243
 
247
         // Create transaction with converted amount
244
         // Create transaction with converted amount

+ 5
- 5
app/Models/Accounting/Estimate.php View File

471
             'currency_code' => $this->currency_code,
471
             'currency_code' => $this->currency_code,
472
             'discount_method' => $this->discount_method,
472
             'discount_method' => $this->discount_method,
473
             'discount_computation' => $this->discount_computation,
473
             'discount_computation' => $this->discount_computation,
474
-            'discount_rate' => $this->discount_rate,
475
-            'subtotal' => $this->subtotal,
476
-            'tax_total' => $this->tax_total,
477
-            'discount_total' => $this->discount_total,
478
-            'total' => $this->total,
474
+            'discount_rate' => $this->getRawOriginal('discount_rate'),
475
+            'subtotal' => $this->getRawOriginal('subtotal'),
476
+            'tax_total' => $this->getRawOriginal('tax_total'),
477
+            'discount_total' => $this->getRawOriginal('discount_total'),
478
+            'total' => $this->getRawOriginal('total'),
479
             'terms' => $this->terms,
479
             'terms' => $this->terms,
480
             'footer' => $this->footer,
480
             'footer' => $this->footer,
481
             'created_by' => auth()->id(),
481
             'created_by' => auth()->id(),

+ 5
- 5
app/Models/Accounting/RecurringInvoice.php View File

643
             'currency_code' => $this->currency_code,
643
             'currency_code' => $this->currency_code,
644
             'discount_method' => $this->discount_method,
644
             'discount_method' => $this->discount_method,
645
             'discount_computation' => $this->discount_computation,
645
             'discount_computation' => $this->discount_computation,
646
-            'discount_rate' => $this->discount_rate,
647
-            'subtotal' => $this->subtotal,
648
-            'tax_total' => $this->tax_total,
649
-            'discount_total' => $this->discount_total,
650
-            'total' => $this->total,
646
+            'discount_rate' => $this->getRawOriginal('discount_rate'),
647
+            'subtotal' => $this->getRawOriginal('subtotal'),
648
+            'tax_total' => $this->getRawOriginal('tax_total'),
649
+            'discount_total' => $this->getRawOriginal('discount_total'),
650
+            'total' => $this->getRawOriginal('total'),
651
             'terms' => $this->terms,
651
             'terms' => $this->terms,
652
             'footer' => $this->footer,
652
             'footer' => $this->footer,
653
             'created_by' => auth()->id(),
653
             'created_by' => auth()->id(),

+ 4
- 4
app/Services/TransactionService.php View File

250
         });
250
         });
251
     }
251
     }
252
 
252
 
253
-    private function getConvertedTransactionAmount(Transaction $transaction): string
253
+    private function getConvertedTransactionAmount(Transaction $transaction): int
254
     {
254
     {
255
         $defaultCurrency = CurrencyAccessor::getDefaultCurrency();
255
         $defaultCurrency = CurrencyAccessor::getDefaultCurrency();
256
         $bankAccountCurrency = $transaction->bankAccount->account->currency_code;
256
         $bankAccountCurrency = $transaction->bankAccount->account->currency_code;
259
             return $this->convertToDefaultCurrency($transaction->amount, $bankAccountCurrency, $defaultCurrency);
259
             return $this->convertToDefaultCurrency($transaction->amount, $bankAccountCurrency, $defaultCurrency);
260
         }
260
         }
261
 
261
 
262
-        return $transaction->amount;
262
+        return CurrencyConverter::prepareForAccessor($transaction->amount, $defaultCurrency);
263
     }
263
     }
264
 
264
 
265
     private function convertToDefaultCurrency(string $amount, string $fromCurrency, string $toCurrency): string
265
     private function convertToDefaultCurrency(string $amount, string $fromCurrency, string $toCurrency): string
268
 
268
 
269
         $convertedAmountInCents = CurrencyConverter::convertBalance($amountInCents, $fromCurrency, $toCurrency);
269
         $convertedAmountInCents = CurrencyConverter::convertBalance($amountInCents, $fromCurrency, $toCurrency);
270
 
270
 
271
-        return CurrencyConverter::prepareForMutator($convertedAmountInCents, $toCurrency);
271
+        return $convertedAmountInCents;
272
     }
272
     }
273
 
273
 
274
     private function hasRelevantChanges(Transaction $transaction): bool
274
     private function hasRelevantChanges(Transaction $transaction): bool
276
         return $transaction->wasChanged(['amount', 'account_id', 'bank_account_id', 'type']);
276
         return $transaction->wasChanged(['amount', 'account_id', 'bank_account_id', 'type']);
277
     }
277
     }
278
 
278
 
279
-    private function updateJournalEntryForTransaction(JournalEntry $journalEntry, Account $account, string $convertedTransactionAmount): void
279
+    private function updateJournalEntryForTransaction(JournalEntry $journalEntry, Account $account, int $convertedTransactionAmount): void
280
     {
280
     {
281
         DB::transaction(static function () use ($journalEntry, $account, $convertedTransactionAmount) {
281
         DB::transaction(static function () use ($journalEntry, $account, $convertedTransactionAmount) {
282
             $journalEntry->update([
282
             $journalEntry->update([

+ 6
- 7
database/factories/Accounting/BillFactory.php View File

12
 use App\Models\Common\Vendor;
12
 use App\Models\Common\Vendor;
13
 use App\Models\Company;
13
 use App\Models\Company;
14
 use App\Models\Setting\DocumentDefault;
14
 use App\Models\Setting\DocumentDefault;
15
-use App\Utilities\Currency\CurrencyConverter;
16
 use App\Utilities\RateCalculator;
15
 use App\Utilities\RateCalculator;
17
 use Illuminate\Database\Eloquent\Factories\Factory;
16
 use Illuminate\Database\Eloquent\Factories\Factory;
18
 use Illuminate\Support\Carbon;
17
 use Illuminate\Support\Carbon;
174
 
173
 
175
                 $data = [
174
                 $data = [
176
                     'posted_at' => $postedAt,
175
                     'posted_at' => $postedAt,
177
-                    'amount' => CurrencyConverter::convertCentsToFormatSimple($amount, $bill->currency_code),
176
+                    'amount' => $amount,
178
                     'payment_method' => $this->faker->randomElement(PaymentMethod::class),
177
                     'payment_method' => $this->faker->randomElement(PaymentMethod::class),
179
                     'bank_account_id' => BankAccount::where('company_id', $bill->company_id)->inRandomOrder()->value('id'),
178
                     'bank_account_id' => BankAccount::where('company_id', $bill->company_id)->inRandomOrder()->value('id'),
180
                     'notes' => $this->faker->sentence,
179
                     'notes' => $this->faker->sentence,
250
                 $scaledRate = RateCalculator::parseLocalizedRate($bill->discount_rate);
249
                 $scaledRate = RateCalculator::parseLocalizedRate($bill->discount_rate);
251
                 $discountTotalCents = RateCalculator::calculatePercentage($subtotalCents, $scaledRate);
250
                 $discountTotalCents = RateCalculator::calculatePercentage($subtotalCents, $scaledRate);
252
             } else {
251
             } else {
253
-                $discountTotalCents = CurrencyConverter::convertToCents($bill->discount_rate, $bill->currency_code);
252
+                $discountTotalCents = $bill->getRawOriginal('discount_rate');
254
             }
253
             }
255
         }
254
         }
256
 
255
 
258
         $currencyCode = $bill->currency_code;
257
         $currencyCode = $bill->currency_code;
259
 
258
 
260
         $bill->update([
259
         $bill->update([
261
-            'subtotal' => CurrencyConverter::convertCentsToFormatSimple($subtotalCents, $currencyCode),
262
-            'tax_total' => CurrencyConverter::convertCentsToFormatSimple($taxTotalCents, $currencyCode),
263
-            'discount_total' => CurrencyConverter::convertCentsToFormatSimple($discountTotalCents, $currencyCode),
264
-            'total' => CurrencyConverter::convertCentsToFormatSimple($grandTotalCents, $currencyCode),
260
+            'subtotal' => $subtotalCents,
261
+            'tax_total' => $taxTotalCents,
262
+            'discount_total' => $discountTotalCents,
263
+            'total' => $grandTotalCents,
265
         ]);
264
         ]);
266
     }
265
     }
267
 }
266
 }

+ 5
- 6
database/factories/Accounting/EstimateFactory.php View File

10
 use App\Models\Common\Client;
10
 use App\Models\Common\Client;
11
 use App\Models\Company;
11
 use App\Models\Company;
12
 use App\Models\Setting\DocumentDefault;
12
 use App\Models\Setting\DocumentDefault;
13
-use App\Utilities\Currency\CurrencyConverter;
14
 use App\Utilities\RateCalculator;
13
 use App\Utilities\RateCalculator;
15
 use Illuminate\Database\Eloquent\Factories\Factory;
14
 use Illuminate\Database\Eloquent\Factories\Factory;
16
 use Illuminate\Support\Carbon;
15
 use Illuminate\Support\Carbon;
231
                 $scaledRate = RateCalculator::parseLocalizedRate($estimate->discount_rate);
230
                 $scaledRate = RateCalculator::parseLocalizedRate($estimate->discount_rate);
232
                 $discountTotalCents = RateCalculator::calculatePercentage($subtotalCents, $scaledRate);
231
                 $discountTotalCents = RateCalculator::calculatePercentage($subtotalCents, $scaledRate);
233
             } else {
232
             } else {
234
-                $discountTotalCents = CurrencyConverter::convertToCents($estimate->discount_rate, $estimate->currency_code);
233
+                $discountTotalCents = $estimate->getRawOriginal('discount_rate');
235
             }
234
             }
236
         }
235
         }
237
 
236
 
239
         $currencyCode = $estimate->currency_code;
238
         $currencyCode = $estimate->currency_code;
240
 
239
 
241
         $estimate->update([
240
         $estimate->update([
242
-            'subtotal' => CurrencyConverter::convertCentsToFormatSimple($subtotalCents, $currencyCode),
243
-            'tax_total' => CurrencyConverter::convertCentsToFormatSimple($taxTotalCents, $currencyCode),
244
-            'discount_total' => CurrencyConverter::convertCentsToFormatSimple($discountTotalCents, $currencyCode),
245
-            'total' => CurrencyConverter::convertCentsToFormatSimple($grandTotalCents, $currencyCode),
241
+            'subtotal' => $subtotalCents,
242
+            'tax_total' => $taxTotalCents,
243
+            'discount_total' => $discountTotalCents,
244
+            'total' => $grandTotalCents,
246
         ]);
245
         ]);
247
     }
246
     }
248
 }
247
 }

+ 6
- 7
database/factories/Accounting/InvoiceFactory.php View File

12
 use App\Models\Common\Client;
12
 use App\Models\Common\Client;
13
 use App\Models\Company;
13
 use App\Models\Company;
14
 use App\Models\Setting\DocumentDefault;
14
 use App\Models\Setting\DocumentDefault;
15
-use App\Utilities\Currency\CurrencyConverter;
16
 use App\Utilities\RateCalculator;
15
 use App\Utilities\RateCalculator;
17
 use Illuminate\Database\Eloquent\Factories\Factory;
16
 use Illuminate\Database\Eloquent\Factories\Factory;
18
 use Illuminate\Support\Carbon;
17
 use Illuminate\Support\Carbon;
192
 
191
 
193
                 $data = [
192
                 $data = [
194
                     'posted_at' => $postedAt,
193
                     'posted_at' => $postedAt,
195
-                    'amount' => CurrencyConverter::convertCentsToFormatSimple($amount, $invoice->currency_code),
194
+                    'amount' => $amount,
196
                     'payment_method' => $this->faker->randomElement(PaymentMethod::class),
195
                     'payment_method' => $this->faker->randomElement(PaymentMethod::class),
197
                     'bank_account_id' => BankAccount::where('company_id', $invoice->company_id)->inRandomOrder()->value('id'),
196
                     'bank_account_id' => BankAccount::where('company_id', $invoice->company_id)->inRandomOrder()->value('id'),
198
                     'notes' => $this->faker->sentence,
197
                     'notes' => $this->faker->sentence,
275
                 $scaledRate = RateCalculator::parseLocalizedRate($invoice->discount_rate);
274
                 $scaledRate = RateCalculator::parseLocalizedRate($invoice->discount_rate);
276
                 $discountTotalCents = RateCalculator::calculatePercentage($subtotalCents, $scaledRate);
275
                 $discountTotalCents = RateCalculator::calculatePercentage($subtotalCents, $scaledRate);
277
             } else {
276
             } else {
278
-                $discountTotalCents = CurrencyConverter::convertToCents($invoice->discount_rate, $invoice->currency_code);
277
+                $discountTotalCents = $invoice->getRawOriginal('discount_rate');
279
             }
278
             }
280
         }
279
         }
281
 
280
 
283
         $currencyCode = $invoice->currency_code;
282
         $currencyCode = $invoice->currency_code;
284
 
283
 
285
         $invoice->update([
284
         $invoice->update([
286
-            'subtotal' => CurrencyConverter::convertCentsToFormatSimple($subtotalCents, $currencyCode),
287
-            'tax_total' => CurrencyConverter::convertCentsToFormatSimple($taxTotalCents, $currencyCode),
288
-            'discount_total' => CurrencyConverter::convertCentsToFormatSimple($discountTotalCents, $currencyCode),
289
-            'total' => CurrencyConverter::convertCentsToFormatSimple($grandTotalCents, $currencyCode),
285
+            'subtotal' => $subtotalCents,
286
+            'tax_total' => $taxTotalCents,
287
+            'discount_total' => $discountTotalCents,
288
+            'total' => $grandTotalCents,
290
         ]);
289
         ]);
291
     }
290
     }
292
 }
291
 }

+ 5
- 6
database/factories/Accounting/RecurringInvoiceFactory.php View File

16
 use App\Models\Accounting\RecurringInvoice;
16
 use App\Models\Accounting\RecurringInvoice;
17
 use App\Models\Common\Client;
17
 use App\Models\Common\Client;
18
 use App\Models\Company;
18
 use App\Models\Company;
19
-use App\Utilities\Currency\CurrencyConverter;
20
 use App\Utilities\RateCalculator;
19
 use App\Utilities\RateCalculator;
21
 use Illuminate\Database\Eloquent\Factories\Factory;
20
 use Illuminate\Database\Eloquent\Factories\Factory;
22
 use Illuminate\Support\Carbon;
21
 use Illuminate\Support\Carbon;
325
                 $scaledRate = RateCalculator::parseLocalizedRate($recurringInvoice->discount_rate);
324
                 $scaledRate = RateCalculator::parseLocalizedRate($recurringInvoice->discount_rate);
326
                 $discountTotalCents = RateCalculator::calculatePercentage($subtotalCents, $scaledRate);
325
                 $discountTotalCents = RateCalculator::calculatePercentage($subtotalCents, $scaledRate);
327
             } else {
326
             } else {
328
-                $discountTotalCents = CurrencyConverter::convertToCents($recurringInvoice->discount_rate, $recurringInvoice->currency_code);
327
+                $discountTotalCents = $recurringInvoice->getRawOriginal('discount_rate');
329
             }
328
             }
330
         }
329
         }
331
 
330
 
333
         $currencyCode = $recurringInvoice->currency_code;
332
         $currencyCode = $recurringInvoice->currency_code;
334
 
333
 
335
         $recurringInvoice->update([
334
         $recurringInvoice->update([
336
-            'subtotal' => CurrencyConverter::convertCentsToFormatSimple($subtotalCents, $currencyCode),
337
-            'tax_total' => CurrencyConverter::convertCentsToFormatSimple($taxTotalCents, $currencyCode),
338
-            'discount_total' => CurrencyConverter::convertCentsToFormatSimple($discountTotalCents, $currencyCode),
339
-            'total' => CurrencyConverter::convertCentsToFormatSimple($grandTotalCents, $currencyCode),
335
+            'subtotal' => $subtotalCents,
336
+            'tax_total' => $taxTotalCents,
337
+            'discount_total' => $discountTotalCents,
338
+            'total' => $grandTotalCents,
340
         ]);
339
         ]);
341
     }
340
     }
342
 }
341
 }

Loading…
Cancel
Save