Andrew Wallo 4 달 전
부모
커밋
1ffeea46f7

+ 2
- 10
app/Casts/JournalEntryCast.php 파일 보기

@@ -2,23 +2,15 @@
2 2
 
3 3
 namespace App\Casts;
4 4
 
5
-use App\Utilities\Currency\CurrencyAccessor;
6
-use App\Utilities\Currency\CurrencyConverter;
7 5
 use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
8 6
 use Illuminate\Database\Eloquent\Model;
9 7
 use UnexpectedValueException;
10 8
 
11 9
 class JournalEntryCast implements CastsAttributes
12 10
 {
13
-    public function get(Model $model, string $key, mixed $value, array $attributes): string
11
+    public function get(Model $model, string $key, mixed $value, array $attributes): int
14 12
     {
15
-        $currency_code = CurrencyAccessor::getDefaultCurrency();
16
-
17
-        if ($value !== null) {
18
-            return CurrencyConverter::prepareForMutator($value, $currency_code);
19
-        }
20
-
21
-        return '';
13
+        return (int) $value;
22 14
     }
23 15
 
24 16
     /**

+ 2
- 10
app/Casts/MoneyCast.php 파일 보기

@@ -2,23 +2,15 @@
2 2
 
3 3
 namespace App\Casts;
4 4
 
5
-use App\Utilities\Currency\CurrencyAccessor;
6
-use App\Utilities\Currency\CurrencyConverter;
7 5
 use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
8 6
 use Illuminate\Database\Eloquent\Model;
9 7
 use UnexpectedValueException;
10 8
 
11 9
 class MoneyCast implements CastsAttributes
12 10
 {
13
-    public function get(Model $model, string $key, mixed $value, array $attributes): string
11
+    public function get(Model $model, string $key, mixed $value, array $attributes): int
14 12
     {
15
-        $currency_code = $attributes['currency_code'] ?? CurrencyAccessor::getDefaultCurrency();
16
-
17
-        if ($value !== null) {
18
-            return CurrencyConverter::prepareForMutator($value, $currency_code);
19
-        }
20
-
21
-        return '';
13
+        return (int) $value;
22 14
     }
23 15
 
24 16
     /**

+ 2
- 16
app/Casts/TransactionAmountCast.php 파일 보기

@@ -4,7 +4,6 @@ namespace App\Casts;
4 4
 
5 5
 use App\Models\Banking\BankAccount;
6 6
 use App\Utilities\Currency\CurrencyAccessor;
7
-use App\Utilities\Currency\CurrencyConverter;
8 7
 use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
9 8
 use Illuminate\Database\Eloquent\Model;
10 9
 use UnexpectedValueException;
@@ -37,22 +36,9 @@ class TransactionAmountCast implements CastsAttributes
37 36
         }
38 37
     }
39 38
 
40
-    public function get(Model $model, string $key, mixed $value, array $attributes): string
39
+    public function get(Model $model, string $key, mixed $value, array $attributes): int
41 40
     {
42
-        // Attempt to retrieve the currency code from the related bankAccount->account model
43
-        $bankAccountId = $attributes['bank_account_id'] ?? null;
44
-
45
-        if ($bankAccountId !== null && ! isset(self::$currencyCache[$bankAccountId])) {
46
-            $this->loadMissingBankAccounts([$bankAccountId]);
47
-        }
48
-
49
-        $currencyCode = $this->getCurrencyCodeFromBankAccountId($bankAccountId);
50
-
51
-        if ($value !== null) {
52
-            return CurrencyConverter::prepareForMutator($value, $currencyCode);
53
-        }
54
-
55
-        return '';
41
+        return (int) $value;
56 42
     }
57 43
 
58 44
     /**

+ 2
- 3
app/Collections/Accounting/JournalEntryCollection.php 파일 보기

@@ -4,7 +4,6 @@ namespace App\Collections\Accounting;
4 4
 
5 5
 use App\Models\Accounting\JournalEntry;
6 6
 use App\Utilities\Currency\CurrencyAccessor;
7
-use App\Utilities\Currency\CurrencyConverter;
8 7
 use App\ValueObjects\Money;
9 8
 use Illuminate\Database\Eloquent\Collection;
10 9
 
@@ -14,7 +13,7 @@ class JournalEntryCollection extends Collection
14 13
     {
15 14
         $total = $this->reduce(static function ($carry, JournalEntry $item) {
16 15
             if ($item->type->isDebit()) {
17
-                $amountAsInteger = CurrencyConverter::convertToCents($item->amount);
16
+                $amountAsInteger = $item->amount;
18 17
 
19 18
                 return bcadd($carry, $amountAsInteger, 0);
20 19
             }
@@ -29,7 +28,7 @@ class JournalEntryCollection extends Collection
29 28
     {
30 29
         $total = $this->reduce(static function ($carry, JournalEntry $item) {
31 30
             if ($item->type->isCredit()) {
32
-                $amountAsInteger = CurrencyConverter::convertToCents($item->amount);
31
+                $amountAsInteger = $item->amount;
33 32
 
34 33
                 return bcadd($carry, $amountAsInteger, 0);
35 34
             }

+ 1
- 18
app/Concerns/HasTransactionAction.php 파일 보기

@@ -14,7 +14,6 @@ use Closure;
14 14
 use Filament\Forms;
15 15
 use Filament\Forms\Components\Actions\Action as FormAction;
16 16
 use Filament\Forms\Form;
17
-use Filament\Support\RawJs;
18 17
 use Illuminate\Contracts\View\View;
19 18
 use Illuminate\Support\Str;
20 19
 
@@ -328,23 +327,7 @@ trait HasTransactionAction
328 327
             Forms\Components\TextInput::make('amount')
329 328
                 ->label('Amount')
330 329
                 ->live()
331
-                ->mask(RawJs::make('$money($input)'))
332
-                ->dehydrateStateUsing(function (?string $state): ?int {
333
-                    if (blank($state)) {
334
-                        return null;
335
-                    }
336
-
337
-                    // Remove thousand separators
338
-                    $cleaned = str_replace(',', '', $state);
339
-
340
-                    // If no decimal point, assume it's whole dollars (add .00)
341
-                    if (! str_contains($cleaned, '.')) {
342
-                        $cleaned .= '.00';
343
-                    }
344
-
345
-                    // Convert to float then to cents (integer)
346
-                    return (int) round((float) $cleaned * 100);
347
-                })
330
+                ->money()
348 331
                 ->afterStateUpdated(function (Forms\Get $get, Forms\Set $set, ?string $state, ?string $old) {
349 332
                     $this->updateJournalEntryAmount(JournalEntryType::parse($get('type')), $state, $old);
350 333
                 })

+ 2
- 2
app/DTO/DocumentDTO.php 파일 보기

@@ -94,9 +94,9 @@ readonly class DocumentDTO
94 94
         );
95 95
     }
96 96
 
97
-    protected static function formatToMoney(float | string $value, ?string $currencyCode): string
97
+    protected static function formatToMoney(int $value, ?string $currencyCode): string
98 98
     {
99
-        return CurrencyConverter::formatToMoney($value, $currencyCode);
99
+        return CurrencyConverter::formatCentsToMoney($value, $currencyCode);
100 100
     }
101 101
 
102 102
     public function getFontHtml(): Htmlable

+ 6
- 6
app/DTO/DocumentPreviewDTO.php 파일 보기

@@ -17,7 +17,7 @@ readonly class DocumentPreviewDTO extends DocumentDTO
17 17
         $paymentTerms = PaymentTerms::parse($data['payment_terms']) ?? $settings->payment_terms;
18 18
 
19 19
         $amountDue = $settings->type !== DocumentType::Estimate ?
20
-            self::formatToMoney('950', null) :
20
+            self::formatToMoney(95000, null) :
21 21
             null;
22 22
 
23 23
         return new self(
@@ -31,11 +31,11 @@ readonly class DocumentPreviewDTO extends DocumentDTO
31 31
             date: $company->locale->date_format->getLabel(),
32 32
             dueDate: $paymentTerms->getDueDate($company->locale->date_format->value),
33 33
             currencyCode: CurrencyAccessor::getDefaultCurrency(),
34
-            subtotal: self::formatToMoney('1000', null),
35
-            discount: self::formatToMoney('100', null),
36
-            tax: self::formatToMoney('50', null),
37
-            total: self::formatToMoney('950', null),
38
-            amountDue: $amountDue,
34
+            subtotal: self::formatToMoney(100000, null), // $1000.00
35
+            discount: self::formatToMoney(10000, null), // $100.00
36
+            tax: self::formatToMoney(5000, null), // $50.00
37
+            total: self::formatToMoney(95000, null), // $950.00
38
+            amountDue: $amountDue, // $950.00 or null for estimates
39 39
             company: CompanyDTO::fromModel($company),
40 40
             client: ClientPreviewDTO::fake(),
41 41
             lineItems: LineItemPreviewDTO::fakeItems(),

+ 5
- 1
app/DTO/LineItemDTO.php 파일 보기

@@ -26,8 +26,12 @@ readonly class LineItemDTO
26 26
         );
27 27
     }
28 28
 
29
-    protected static function formatToMoney(float | string $value, ?string $currencyCode): string
29
+    protected static function formatToMoney(float | string | int $value, ?string $currencyCode): string
30 30
     {
31
+        if (is_int($value)) {
32
+            return CurrencyConverter::formatCentsToMoney($value, $currencyCode);
33
+        }
34
+
31 35
         return CurrencyConverter::formatToMoney($value, $currencyCode);
32 36
     }
33 37
 }

+ 12
- 12
app/DTO/LineItemPreviewDTO.php 파일 보기

@@ -8,25 +8,25 @@ readonly class LineItemPreviewDTO extends LineItemDTO
8 8
     {
9 9
         return [
10 10
             new self(
11
-                name: 'Item 1',
12
-                description: 'Sample item description',
11
+                name: 'Professional Services',
12
+                description: 'Consulting and strategic planning',
13 13
                 quantity: 2,
14
-                unitPrice: self::formatToMoney(150.00, null),
15
-                subtotal: self::formatToMoney(300.00, null),
14
+                unitPrice: self::formatToMoney(15000, null), // $150.00
15
+                subtotal: self::formatToMoney(30000, null),  // $300.00
16 16
             ),
17 17
             new self(
18
-                name: 'Item 2',
19
-                description: 'Another sample item description',
18
+                name: 'Software License',
19
+                description: 'Annual subscription and support',
20 20
                 quantity: 3,
21
-                unitPrice: self::formatToMoney(200.00, null),
22
-                subtotal: self::formatToMoney(600.00, null),
21
+                unitPrice: self::formatToMoney(20000, null), // $200.00
22
+                subtotal: self::formatToMoney(60000, null),  // $600.00
23 23
             ),
24 24
             new self(
25
-                name: 'Item 3',
26
-                description: 'Yet another sample item description',
25
+                name: 'Training Session',
26
+                description: 'Team onboarding and documentation',
27 27
                 quantity: 1,
28
-                unitPrice: self::formatToMoney(180.00, null),
29
-                subtotal: self::formatToMoney(180.00, null),
28
+                unitPrice: self::formatToMoney(10000, null), // $100.00
29
+                subtotal: self::formatToMoney(10000, null),  // $100.00
30 30
             ),
31 31
         ];
32 32
     }

+ 4
- 4
app/Filament/Company/Resources/Purchases/BillResource.php 파일 보기

@@ -584,11 +584,11 @@ class BillResource extends Resource
584 584
                             }
585 585
                         })
586 586
                         ->mountUsing(function (Collection $records, Form $form) {
587
-                            $totalAmountDue = $records->sum(fn (Bill $bill) => $bill->getRawOriginal('amount_due'));
587
+                            $totalAmountDue = $records->sum('amount_due');
588 588
 
589 589
                             $form->fill([
590 590
                                 'posted_at' => now(),
591
-                                'amount' => CurrencyConverter::convertCentsToFormatSimple($totalAmountDue),
591
+                                'amount' => $totalAmountDue,
592 592
                             ]);
593 593
                         })
594 594
                         ->form([
@@ -625,7 +625,7 @@ class BillResource extends Resource
625 625
                         ])
626 626
                         ->before(function (Collection $records, Tables\Actions\BulkAction $action, array $data) {
627 627
                             $totalPaymentAmount = $data['amount'] ?? 0;
628
-                            $totalAmountDue = $records->sum(fn (Bill $bill) => $bill->getRawOriginal('amount_due'));
628
+                            $totalAmountDue = $records->sum('amount_due');
629 629
 
630 630
                             if ($totalPaymentAmount > $totalAmountDue) {
631 631
                                 $formattedTotalAmountDue = CurrencyConverter::formatCentsToMoney($totalAmountDue);
@@ -645,7 +645,7 @@ class BillResource extends Resource
645 645
                             $remainingAmount = $totalPaymentAmount;
646 646
 
647 647
                             $records->each(function (Bill $record) use (&$remainingAmount, $data) {
648
-                                $amountDue = $record->getRawOriginal('amount_due');
648
+                                $amountDue = $record->amount_due;
649 649
 
650 650
                                 if ($amountDue <= 0 || $remainingAmount <= 0) {
651 651
                                     return;

+ 1
- 1
app/Filament/Company/Resources/Purchases/BillResource/RelationManagers/PaymentsRelationManager.php 파일 보기

@@ -210,7 +210,7 @@ class PaymentsRelationManager extends RelationManager
210 210
                         }
211 211
                     )
212 212
                     ->sortable()
213
-                    ->currency(static fn (Transaction $transaction) => $transaction->bankAccount?->account->currency_code ?? CurrencyAccessor::getDefaultCurrency(), true),
213
+                    ->currency(static fn (Transaction $transaction) => $transaction->bankAccount?->account->currency_code ?? CurrencyAccessor::getDefaultCurrency()),
214 214
             ])
215 215
             ->filters([
216 216
                 //

+ 4
- 4
app/Filament/Company/Resources/Sales/InvoiceResource.php 파일 보기

@@ -272,7 +272,7 @@ class InvoiceResource extends Resource
272 272
                                                 return;
273 273
                                             }
274 274
 
275
-                                            $unitPrice = CurrencyConverter::convertToFloat($offeringRecord->price, CurrencyAccessor::getDefaultCurrency());
275
+                                            $unitPrice = CurrencyConverter::convertCentsToFloat($offeringRecord->price, CurrencyAccessor::getDefaultCurrency());
276 276
 
277 277
                                             $set('description', $offeringRecord->description);
278 278
                                             $set('unit_price', $unitPrice);
@@ -697,7 +697,7 @@ class InvoiceResource extends Resource
697 697
                             }
698 698
                         })
699 699
                         ->mountUsing(function (DocumentCollection $records, Form $form) {
700
-                            $totalAmountDue = $records->sumMoneyFormattedSimple('amount_due');
700
+                            $totalAmountDue = $records->sum('amount_due');
701 701
 
702 702
                             $form->fill([
703 703
                                 'posted_at' => now(),
@@ -738,7 +738,7 @@ class InvoiceResource extends Resource
738 738
                         ])
739 739
                         ->before(function (DocumentCollection $records, Tables\Actions\BulkAction $action, array $data) {
740 740
                             $totalPaymentAmount = $data['amount'] ?? 0;
741
-                            $totalAmountDue = $records->sumMoneyInCents('amount_due');
741
+                            $totalAmountDue = $records->sum('amount_due');
742 742
 
743 743
                             if ($totalPaymentAmount > $totalAmountDue) {
744 744
                                 $formattedTotalAmountDue = CurrencyConverter::formatCentsToMoney($totalAmountDue);
@@ -759,7 +759,7 @@ class InvoiceResource extends Resource
759 759
                             $remainingAmount = $totalPaymentAmount;
760 760
 
761 761
                             $records->each(function (Invoice $record) use (&$remainingAmount, $data) {
762
-                                $amountDue = $record->getRawOriginal('amount_due');
762
+                                $amountDue = $record->amount_due;
763 763
 
764 764
                                 if ($amountDue <= 0 || $remainingAmount <= 0) {
765 765
                                     return;

+ 1
- 1
app/Filament/Company/Resources/Sales/InvoiceResource/RelationManagers/PaymentsRelationManager.php 파일 보기

@@ -223,7 +223,7 @@ class PaymentsRelationManager extends RelationManager
223 223
                         }
224 224
                     )
225 225
                     ->sortable()
226
-                    ->currency(static fn (Transaction $transaction) => $transaction->bankAccount?->account->currency_code ?? CurrencyAccessor::getDefaultCurrency(), true),
226
+                    ->currency(static fn (Transaction $transaction) => $transaction->bankAccount?->account->currency_code ?? CurrencyAccessor::getDefaultCurrency()),
227 227
             ])
228 228
             ->filters([
229 229
                 //

+ 2
- 2
app/Models/Accounting/Bill.php 파일 보기

@@ -267,7 +267,7 @@ class Bill extends Document
267 267
 
268 268
         $journalEntryData = [];
269 269
 
270
-        $totalInBillCurrency = $this->getRawOriginal('total');
270
+        $totalInBillCurrency = $this->total;
271 271
         $journalEntryData[] = [
272 272
             'type' => JournalEntryType::Credit,
273 273
             'account_id' => Account::getAccountsPayableAccount($this->company_id)->id,
@@ -276,7 +276,7 @@ class Bill extends Document
276 276
         ];
277 277
 
278 278
         $totalLineItemSubtotalInBillCurrency = (int) $this->lineItems()->sum('subtotal');
279
-        $billDiscountTotalInBillCurrency = (int) $this->getRawOriginal('discount_total');
279
+        $billDiscountTotalInBillCurrency = (int) $this->discount_total;
280 280
         $remainingDiscountInBillCurrency = $billDiscountTotalInBillCurrency;
281 281
 
282 282
         foreach ($this->lineItems as $index => $lineItem) {

+ 12
- 3
app/Providers/MacroServiceProvider.php 파일 보기

@@ -66,6 +66,15 @@ class MacroServiceProvider extends ServiceProvider
66 66
             }
67 67
 
68 68
             $this->mask(RawJs::make('$money($input)'))
69
+                ->afterStateHydrated(function (TextInput $component, ?int $state) use ($currency) {
70
+                    if (blank($state)) {
71
+                        return;
72
+                    }
73
+
74
+                    $currencyCode = $component->evaluate($currency);
75
+                    $formatted = CurrencyConverter::convertCentsToFormatSimple($state, $currencyCode);
76
+                    $component->state($formatted);
77
+                })
69 78
                 ->dehydrateStateUsing(function (?string $state): ?int {
70 79
                     if (blank($state)) {
71 80
                         return null;
@@ -187,7 +196,7 @@ class MacroServiceProvider extends ServiceProvider
187 196
 
188 197
         TextColumn::macro('currency', function (string | Closure | null $currency = null, ?bool $convert = null): static {
189 198
             $currency ??= CurrencyAccessor::getDefaultCurrency();
190
-            $convert ??= true;
199
+            $convert ??= false;
191 200
 
192 201
             $this->formatStateUsing(static function (TextColumn $column, $state) use ($currency, $convert): ?string {
193 202
                 if (blank($state)) {
@@ -205,7 +214,7 @@ class MacroServiceProvider extends ServiceProvider
205 214
 
206 215
         TextEntry::macro('currency', function (string | Closure | null $currency = null, ?bool $convert = null): static {
207 216
             $currency ??= CurrencyAccessor::getDefaultCurrency();
208
-            $convert ??= true;
217
+            $convert ??= false;
209 218
 
210 219
             $this->formatStateUsing(static function (TextEntry $entry, $state) use ($currency, $convert): ?string {
211 220
                 if (blank($state)) {
@@ -223,7 +232,7 @@ class MacroServiceProvider extends ServiceProvider
223 232
 
224 233
         TextColumn::macro('currencyWithConversion', function (string | Closure | null $currency = null, ?bool $convertFromCents = null): static {
225 234
             $currency ??= CurrencyAccessor::getDefaultCurrency();
226
-            $convertFromCents ??= false;
235
+            $convertFromCents ??= true;
227 236
 
228 237
             $this->formatStateUsing(static function (TextColumn $column, $state) use ($currency, $convertFromCents): ?string {
229 238
                 if (blank($state)) {

+ 3
- 7
app/Services/TransactionService.php 파일 보기

@@ -259,16 +259,12 @@ class TransactionService
259 259
             return $this->convertToDefaultCurrency($transaction->amount, $bankAccountCurrency, $defaultCurrency);
260 260
         }
261 261
 
262
-        return CurrencyConverter::prepareForAccessor($transaction->amount, $defaultCurrency);
262
+        return $transaction->amount;
263 263
     }
264 264
 
265
-    private function convertToDefaultCurrency(string $amount, string $fromCurrency, string $toCurrency): string
265
+    private function convertToDefaultCurrency(int $amount, string $fromCurrency, string $toCurrency): int
266 266
     {
267
-        $amountInCents = CurrencyConverter::prepareForAccessor($amount, $fromCurrency);
268
-
269
-        $convertedAmountInCents = CurrencyConverter::convertBalance($amountInCents, $fromCurrency, $toCurrency);
270
-
271
-        return $convertedAmountInCents;
267
+        return CurrencyConverter::convertBalance($amount, $fromCurrency, $toCurrency);
272 268
     }
273 269
 
274 270
     private function hasRelevantChanges(Transaction $transaction): bool

Loading…
취소
저장