Andrew Wallo 3 months ago
parent
commit
0b1c0baee8
3 changed files with 231 additions and 206 deletions
  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 View File

4
 
4
 
5
 use App\Enums\Accounting\JournalEntryType;
5
 use App\Enums\Accounting\JournalEntryType;
6
 use App\Utilities\Currency\CurrencyAccessor;
6
 use App\Utilities\Currency\CurrencyAccessor;
7
+use App\Utilities\Currency\CurrencyConverter;
7
 use Filament\Tables\Actions\Action;
8
 use Filament\Tables\Actions\Action;
8
 
9
 
9
 trait HasJournalEntryActions
10
 trait HasJournalEntryActions
14
 
15
 
15
     private function formatMoney(int $amount): string
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
             return '0';
185
             return '0';
185
         }
186
         }
186
 
187
 
187
-        $currency = currency(CurrencyAccessor::getDefaultCurrency());
188
+        $currency = currency('USD');
188
         $decimal = $currency->getDecimalMark();
189
         $decimal = $currency->getDecimalMark();
189
 
190
 
190
         if (substr($amount, -1) === $decimal) {
191
         if (substr($amount, -1) === $decimal) {
201
      */
202
      */
202
     protected function convertAmountToCents(string $amount): int
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 View File

9
 use App\Models\Accounting\Transaction;
9
 use App\Models\Accounting\Transaction;
10
 use App\Models\Banking\BankAccount;
10
 use App\Models\Banking\BankAccount;
11
 use App\Utilities\Currency\CurrencyAccessor;
11
 use App\Utilities\Currency\CurrencyAccessor;
12
+use App\Utilities\Currency\CurrencyConverter;
12
 use Awcodes\TableRepeater\Header;
13
 use Awcodes\TableRepeater\Header;
13
 use Closure;
14
 use Closure;
14
 use Filament\Forms;
15
 use Filament\Forms;
251
 
252
 
252
                         $hasDebit = false;
253
                         $hasDebit = false;
253
                         $hasCredit = false;
254
                         $hasCredit = false;
255
+                        $totalDebits = 0;
256
+                        $totalCredits = 0;
254
 
257
 
255
                         foreach ($value as $entry) {
258
                         foreach ($value as $entry) {
256
-                            if (! isset($entry['type'])) {
259
+                            if (! isset($entry['type']) || ! isset($entry['amount'])) {
257
                                 continue;
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
                                 $hasDebit = true;
267
                                 $hasDebit = true;
262
-                            } elseif (JournalEntryType::parse($entry['type'])->isCredit()) {
268
+                                $totalDebits += $amount;
269
+                            } elseif ($entryType->isCredit()) {
263
                                 $hasCredit = true;
270
                                 $hasCredit = true;
264
-                            }
265
-
266
-                            if ($hasDebit && $hasCredit) {
267
-                                break;
271
+                                $totalCredits += $amount;
268
                             }
272
                             }
269
                         }
273
                         }
270
 
274
 
275
                         if (! $hasCredit) {
279
                         if (! $hasCredit) {
276
                             $fail('At least one credit entry is required.');
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
                 ->searchable(),
336
                 ->searchable(),
327
             Forms\Components\TextInput::make('amount')
337
             Forms\Components\TextInput::make('amount')
328
                 ->label('Amount')
338
                 ->label('Amount')
329
-                ->live()
339
+                ->live(onBlur: true)
330
                 ->money()
340
                 ->money()
331
                 ->afterStateUpdated(function (Forms\Get $get, Forms\Set $set, ?string $state, ?string $old) {
341
                 ->afterStateUpdated(function (Forms\Get $get, Forms\Set $set, ?string $state, ?string $old) {
332
                     $this->updateJournalEntryAmount(JournalEntryType::parse($get('type')), $state, $old);
342
                     $this->updateJournalEntryAmount(JournalEntryType::parse($get('type')), $state, $old);

+ 209
- 195
composer.lock
File diff suppressed because it is too large
View File


Loading…
Cancel
Save