You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EditAccount.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Filament\Resources\AccountResource\Pages;
  3. use App\Filament\Resources\AccountResource;
  4. use App\Models\Banking\Account;
  5. use App\Models\Setting\Currency;
  6. use App\Traits\HandlesResourceRecordUpdate;
  7. use Filament\Pages\Actions;
  8. use Filament\Resources\Pages\EditRecord;
  9. use Filament\Support\Exceptions\Halt;
  10. use Illuminate\Database\Eloquent\Model;
  11. use Illuminate\Support\Facades\Auth;
  12. class EditAccount extends EditRecord
  13. {
  14. use HandlesResourceRecordUpdate;
  15. protected static string $resource = AccountResource::class;
  16. protected function getActions(): array
  17. {
  18. return [
  19. Actions\DeleteAction::make(),
  20. ];
  21. }
  22. protected function getRedirectUrl(): string
  23. {
  24. return $this->previousUrl;
  25. }
  26. protected function mutateFormDataBeforeSave(array $data): array
  27. {
  28. $data['enabled'] = (bool)$data['enabled'];
  29. return $data;
  30. }
  31. /**
  32. * @throws Halt
  33. */
  34. protected function handleRecordUpdate(Model|Account $record, array $data): Model|Account
  35. {
  36. $user = Auth::user();
  37. if (!$user) {
  38. throw new Halt('No authenticated user found.');
  39. }
  40. $oldCurrency = $record->currency_code;
  41. $newCurrency = $data['currency_code'];
  42. if ($oldCurrency !== $newCurrency) {
  43. $data['opening_balance'] = Currency::convertBalance(
  44. $data['opening_balance'],
  45. $oldCurrency,
  46. $newCurrency
  47. );
  48. }
  49. $this->handleRecordUpdateWithUniqueField($record, $data, $user);
  50. return parent::handleRecordUpdate($record, $data);
  51. }
  52. }