|  | @@ -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);
 |