Andrew Wallo 3 月之前
父節點
當前提交
0b1c0baee8
共有 3 個檔案被更改,包括 231 行新增206 行删除
  1. 4
    3
      app/Concerns/HasJournalEntryActions.php
  2. 18
    8
      app/Concerns/HasTransactionAction.php
  3. 209
    195
      composer.lock

+ 4
- 3
app/Concerns/HasJournalEntryActions.php 查看文件

@@ -4,6 +4,7 @@ namespace App\Concerns;
4 4
 
5 5
 use App\Enums\Accounting\JournalEntryType;
6 6
 use App\Utilities\Currency\CurrencyAccessor;
7
+use App\Utilities\Currency\CurrencyConverter;
7 8
 use Filament\Tables\Actions\Action;
8 9
 
9 10
 trait HasJournalEntryActions
@@ -14,7 +15,7 @@ trait HasJournalEntryActions
14 15
 
15 16
     private function formatMoney(int $amount): string
16 17
     {
17
-        return money($amount, CurrencyAccessor::getDefaultCurrency())->format();
18
+        return CurrencyConverter::formatCentsToMoney($amount, CurrencyAccessor::getDefaultCurrency());
18 19
     }
19 20
 
20 21
     /**
@@ -184,7 +185,7 @@ trait HasJournalEntryActions
184 185
             return '0';
185 186
         }
186 187
 
187
-        $currency = currency(CurrencyAccessor::getDefaultCurrency());
188
+        $currency = currency('USD');
188 189
         $decimal = $currency->getDecimalMark();
189 190
 
190 191
         if (substr($amount, -1) === $decimal) {
@@ -201,6 +202,6 @@ trait HasJournalEntryActions
201 202
      */
202 203
     protected function convertAmountToCents(string $amount): int
203 204
     {
204
-        return money($amount, CurrencyAccessor::getDefaultCurrency(), true)->getAmount();
205
+        return CurrencyConverter::convertToCents($amount, 'USD');
205 206
     }
206 207
 }

+ 18
- 8
app/Concerns/HasTransactionAction.php 查看文件

@@ -9,6 +9,7 @@ use App\Models\Accounting\JournalEntry;
9 9
 use App\Models\Accounting\Transaction;
10 10
 use App\Models\Banking\BankAccount;
11 11
 use App\Utilities\Currency\CurrencyAccessor;
12
+use App\Utilities\Currency\CurrencyConverter;
12 13
 use Awcodes\TableRepeater\Header;
13 14
 use Closure;
14 15
 use Filament\Forms;
@@ -251,20 +252,23 @@ trait HasTransactionAction
251 252
 
252 253
                         $hasDebit = false;
253 254
                         $hasCredit = false;
255
+                        $totalDebits = 0;
256
+                        $totalCredits = 0;
254 257
 
255 258
                         foreach ($value as $entry) {
256
-                            if (! isset($entry['type'])) {
259
+                            if (! isset($entry['type']) || ! isset($entry['amount'])) {
257 260
                                 continue;
258 261
                             }
259 262
 
260
-                            if (JournalEntryType::parse($entry['type'])->isDebit()) {
263
+                            $entryType = JournalEntryType::parse($entry['type']);
264
+                            $amount = CurrencyConverter::convertToCents($entry['amount'], 'USD');
265
+
266
+                            if ($entryType->isDebit()) {
261 267
                                 $hasDebit = true;
262
-                            } elseif (JournalEntryType::parse($entry['type'])->isCredit()) {
268
+                                $totalDebits += $amount;
269
+                            } elseif ($entryType->isCredit()) {
263 270
                                 $hasCredit = true;
264
-                            }
265
-
266
-                            if ($hasDebit && $hasCredit) {
267
-                                break;
271
+                                $totalCredits += $amount;
268 272
                             }
269 273
                         }
270 274
 
@@ -275,6 +279,12 @@ trait HasTransactionAction
275 279
                         if (! $hasCredit) {
276 280
                             $fail('At least one credit entry is required.');
277 281
                         }
282
+
283
+                        if ($totalDebits !== $totalCredits) {
284
+                            $debitFormatted = CurrencyConverter::formatCentsToMoney($totalDebits, CurrencyAccessor::getDefaultCurrency());
285
+                            $creditFormatted = CurrencyConverter::formatCentsToMoney($totalCredits, CurrencyAccessor::getDefaultCurrency());
286
+                            $fail("Total debits ({$debitFormatted}) must equal total credits ({$creditFormatted}).");
287
+                        }
278 288
                     };
279 289
                 },
280 290
             ])
@@ -326,7 +336,7 @@ trait HasTransactionAction
326 336
                 ->searchable(),
327 337
             Forms\Components\TextInput::make('amount')
328 338
                 ->label('Amount')
329
-                ->live()
339
+                ->live(onBlur: true)
330 340
                 ->money()
331 341
                 ->afterStateUpdated(function (Forms\Get $get, Forms\Set $set, ?string $state, ?string $old) {
332 342
                     $this->updateJournalEntryAmount(JournalEntryType::parse($get('type')), $state, $old);

+ 209
- 195
composer.lock
文件差異過大導致無法顯示
查看文件


Loading…
取消
儲存