Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CreateCurrency.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Filament\Resources\CurrencyResource\Pages;
  3. use App\Filament\Resources\CurrencyResource;
  4. use App\Models\Setting\Currency;
  5. use Filament\Pages\Actions;
  6. use Filament\Resources\Pages\CreateRecord;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Support\Facades\DB;
  9. class CreateCurrency extends CreateRecord
  10. {
  11. protected static string $resource = CurrencyResource::class;
  12. protected function getRedirectUrl(): string
  13. {
  14. return $this->getResource()::getUrl('index');
  15. }
  16. protected function mutateFormDataBeforeCreate(array $data): array
  17. {
  18. $data['company_id'] = auth()->user()->currentCompany->id;
  19. return $data;
  20. }
  21. protected function handleRecordCreation(array $data): Model
  22. {
  23. $currentCompanyId = auth()->user()->currentCompany->id;
  24. $currencyId = $data['id'] ?? null;
  25. $enabledCurrenciesCount = Currency::where('company_id', $currentCompanyId)
  26. ->where('enabled', '1')
  27. ->where('id', '!=', $currencyId)
  28. ->count();
  29. if ($data['enabled'] === '1' && $enabledCurrenciesCount > 0) {
  30. $this->disableOtherCurrencies($currentCompanyId, $currencyId);
  31. } elseif ($data['enabled'] === '0' && $enabledCurrenciesCount < 1) {
  32. $data['enabled'] = '1';
  33. }
  34. return parent::handleRecordCreation($data);
  35. }
  36. protected function disableOtherCurrencies($companyId, $currencyId): void
  37. {
  38. DB::table('currencies')
  39. ->where('company_id', $companyId)
  40. ->where('id', '!=', $currencyId)
  41. ->update(['enabled' => '0']);
  42. }
  43. }