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.

EditTax.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Filament\Resources\TaxResource\Pages;
  3. use App\Filament\Resources\TaxResource;
  4. use App\Models\Setting\Tax;
  5. use Filament\Pages\Actions;
  6. use Filament\Resources\Pages\EditRecord;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\DB;
  10. class EditTax extends EditRecord
  11. {
  12. protected static string $resource = TaxResource::class;
  13. protected function getActions(): array
  14. {
  15. return [
  16. Actions\DeleteAction::make(),
  17. ];
  18. }
  19. protected function getRedirectUrl(): string
  20. {
  21. return $this->getResource()::getUrl('index');
  22. }
  23. protected function mutateFormDataBeforeUpdate(array $data): array
  24. {
  25. $data['company_id'] = Auth::user()->currentCompany->id;
  26. $data['enabled'] = (bool)$data['enabled'];
  27. $data['updated_by'] = Auth::id();
  28. return $data;
  29. }
  30. protected function handleRecordUpdate(Model|Tax $record, array $data): Model|Tax
  31. {
  32. return DB::transaction(function () use ($record, $data) {
  33. $currentCompanyId = auth()->user()->currentCompany->id;
  34. $recordId = $record->id;
  35. $oldType = $record->type;
  36. $newType = $data['type'];
  37. $enabled = (bool)($data['enabled'] ?? false);
  38. // If the record type has changed and it was previously enabled
  39. if ($oldType !== $newType && $record->enabled) {
  40. $this->changeRecordType($currentCompanyId, $recordId, $oldType);
  41. }
  42. if ($enabled === true) {
  43. $this->disableExistingRecord($currentCompanyId, $recordId, $newType);
  44. } elseif ($enabled === false) {
  45. $this->ensureAtLeastOneEnabled($currentCompanyId, $recordId, $newType, $enabled);
  46. }
  47. $data['enabled'] = $enabled;
  48. return parent::handleRecordUpdate($record, $data);
  49. });
  50. }
  51. protected function changeRecordType(int $companyId, int $recordId, string $oldType): void
  52. {
  53. $oldTypeRecord = $this->getCompanyCategoryRecord($companyId, $oldType, $recordId);
  54. if ($oldTypeRecord) {
  55. $oldTypeRecord->enabled = true;
  56. $oldTypeRecord->save();
  57. }
  58. }
  59. protected function disableExistingRecord(int $companyId, int $recordId, string $newType): void
  60. {
  61. $existingEnabledRecord = $this->getCompanyCategoryRecord($companyId, $newType, $recordId);
  62. if ($existingEnabledRecord !== null) {
  63. $existingEnabledRecord->enabled = false;
  64. $existingEnabledRecord->save();
  65. }
  66. }
  67. protected function ensureAtLeastOneEnabled(int $companyId, int $recordId, string $newType, bool &$enabled): void
  68. {
  69. $otherEnabledRecords = Tax::where('company_id', $companyId)
  70. ->where('enabled', true)
  71. ->where('type', $newType)
  72. ->where('id', '!=', $recordId)
  73. ->count();
  74. if ($otherEnabledRecords === 0) {
  75. $enabled = true;
  76. }
  77. }
  78. protected function getCompanyCategoryRecord(int $companyId, string $type, int $recordId): ?Tax
  79. {
  80. return Tax::where('company_id', $companyId)
  81. ->where('type', $type)
  82. ->where('id', '!=', $recordId)
  83. ->first();
  84. }
  85. }