Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

EditAccount.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Filament\Resources\AccountResource\Pages;
  3. use App\Filament\Resources\AccountResource;
  4. use App\Models\Banking\Account;
  5. use Filament\Notifications\Notification;
  6. use Filament\Pages\Actions;
  7. use Filament\Resources\Pages\EditRecord;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Support\Facades\DB;
  11. class EditAccount extends EditRecord
  12. {
  13. protected static string $resource = AccountResource::class;
  14. protected function getActions(): array
  15. {
  16. return [
  17. Actions\DeleteAction::make(),
  18. ];
  19. }
  20. protected function getRedirectUrl(): string
  21. {
  22. return $this->getResource()::getUrl('index');
  23. }
  24. protected function mutateFormDataBeforeSave(array $data): array
  25. {
  26. $data['company_id'] = Auth::user()->currentCompany->id;
  27. $data['enabled'] = (bool)$data['enabled'];
  28. $data['updated_by'] = Auth::id();
  29. return $data;
  30. }
  31. protected function handleRecordUpdate(Model|Account $record, array $data): Model|Account
  32. {
  33. return DB::transaction(function () use ($record, $data) {
  34. $currentCompanyId = auth()->user()->currentCompany->id;
  35. $recordId = $record->id;
  36. $enabled = (bool)($data['enabled'] ?? false);
  37. // If the record is enabled, disable all other records for the same company
  38. if ($enabled === true) {
  39. $this->disableExistingRecord($currentCompanyId, $recordId);
  40. }
  41. // If the record is disabled, ensure at least one record remains enabled
  42. elseif ($enabled === false) {
  43. $this->ensureAtLeastOneEnabled($currentCompanyId, $recordId, $enabled);
  44. }
  45. $data['enabled'] = $enabled;
  46. return parent::handleRecordUpdate($record, $data);
  47. });
  48. }
  49. protected function disableExistingRecord(int $companyId, int $recordId): void
  50. {
  51. $existingEnabledAccount = Account::where('company_id', $companyId)
  52. ->where('enabled', true)
  53. ->where('id', '!=', $recordId)
  54. ->first();
  55. if ($existingEnabledAccount !== null) {
  56. $existingEnabledAccount->enabled = false;
  57. $existingEnabledAccount->save();
  58. $this->defaultAccountChanged();
  59. }
  60. }
  61. protected function ensureAtLeastOneEnabled(int $companyId, int $recordId, bool &$enabled): void
  62. {
  63. $enabledAccountsCount = Account::where('company_id', $companyId)
  64. ->where('enabled', true)
  65. ->where('id', '!=', $recordId)
  66. ->count();
  67. if ($enabledAccountsCount === 0) {
  68. $enabled = true;
  69. }
  70. }
  71. protected function defaultAccountChanged(): void
  72. {
  73. Notification::make()
  74. ->warning()
  75. ->title('Default account updated')
  76. ->body('Your default account has been updated. Please check your account settings to review this change and ensure it is correct.')
  77. ->persistent()
  78. ->send();
  79. }
  80. }