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.

CompanyDefault.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace App\Filament\Company\Pages\Setting;
  3. use App\Events\CompanyDefaultUpdated;
  4. use App\Models\Setting\CompanyDefault as CompanyDefaultModel;
  5. use Filament\Actions\Action;
  6. use Filament\Actions\ActionGroup;
  7. use Filament\Forms\Components\Component;
  8. use Filament\Forms\Components\Section;
  9. use Filament\Forms\Components\Select;
  10. use Filament\Forms\Form;
  11. use Filament\Notifications\Notification;
  12. use Filament\Pages\Concerns\InteractsWithFormActions;
  13. use Filament\Pages\Page;
  14. use Filament\Support\Exceptions\Halt;
  15. use Illuminate\Auth\Access\AuthorizationException;
  16. use Illuminate\Database\Eloquent\Model;
  17. use Livewire\Attributes\Locked;
  18. use function Filament\authorize;
  19. /**
  20. * @property Form $form
  21. */
  22. class CompanyDefault extends Page
  23. {
  24. use InteractsWithFormActions;
  25. protected static ?string $navigationIcon = 'heroicon-o-adjustments-vertical';
  26. protected static ?string $navigationLabel = 'Default';
  27. protected static ?string $navigationGroup = 'Settings';
  28. protected static ?string $slug = 'settings/default';
  29. protected ?string $heading = 'Default';
  30. protected static string $view = 'filament.company.pages.setting.company-default';
  31. public ?array $data = [];
  32. #[Locked]
  33. public ?CompanyDefaultModel $record = null;
  34. public function mount(): void
  35. {
  36. $this->record = CompanyDefaultModel::firstOrNew([
  37. 'company_id' => auth()->user()->currentCompany->id,
  38. ]);
  39. abort_unless(static::canView($this->record), 404);
  40. $this->fillForm();
  41. }
  42. public function fillForm(): void
  43. {
  44. $data = $this->record->attributesToArray();
  45. $data = $this->mutateFormDataBeforeFill($data);
  46. $this->form->fill($data);
  47. }
  48. protected function mutateFormDataBeforeFill(array $data): array
  49. {
  50. return $data;
  51. }
  52. protected function mutateFormDataBeforeSave(array $data): array
  53. {
  54. return $data;
  55. }
  56. public function save(): void
  57. {
  58. try {
  59. $data = $this->form->getState();
  60. $data = $this->mutateFormDataBeforeSave($data);
  61. $this->handleRecordUpdate($this->record, $data);
  62. } catch (Halt $exception) {
  63. return;
  64. }
  65. $this->getSavedNotification()?->send();
  66. if ($redirectUrl = $this->getRedirectUrl()) {
  67. $this->redirect($redirectUrl);
  68. }
  69. }
  70. protected function getSavedNotification(): ?Notification
  71. {
  72. $title = $this->getSavedNotificationTitle();
  73. if (blank($title)) {
  74. return null;
  75. }
  76. return Notification::make()
  77. ->success()
  78. ->title($this->getSavedNotificationTitle());
  79. }
  80. protected function getSavedNotificationTitle(): ?string
  81. {
  82. return __('filament-panels::pages/tenancy/edit-tenant-profile.notifications.saved.title');
  83. }
  84. protected function getRedirectUrl(): ?string
  85. {
  86. return null;
  87. }
  88. public function form(Form $form): Form
  89. {
  90. return $form
  91. ->schema([
  92. $this->getGeneralSection(),
  93. $this->getModifiersSection(),
  94. $this->getCategoriesSection(),
  95. ])
  96. ->model($this->record)
  97. ->statePath('data')
  98. ->operation('edit');
  99. }
  100. protected function getGeneralSection(): Component
  101. {
  102. return Section::make('General')
  103. ->schema([
  104. Select::make('account_id')
  105. ->label('Account')
  106. ->relationship('account', 'name')
  107. ->saveRelationshipsUsing(null)
  108. ->selectablePlaceholder(false)
  109. ->searchable()
  110. ->preload(),
  111. Select::make('currency_code')
  112. ->label('Currency')
  113. ->relationship('currency', 'code')
  114. ->saveRelationshipsUsing(null)
  115. ->selectablePlaceholder(false)
  116. ->rule('required')
  117. ->searchable()
  118. ->preload(),
  119. ])->columns();
  120. }
  121. protected function getModifiersSection(): Component
  122. {
  123. return Section::make('Taxes & Discounts')
  124. ->schema([
  125. Select::make('sales_tax_id')
  126. ->label('Sales Tax')
  127. ->relationship('salesTax', 'name')
  128. ->saveRelationshipsUsing(null)
  129. ->selectablePlaceholder(false)
  130. ->rule('required')
  131. ->searchable()
  132. ->preload(),
  133. Select::make('purchase_tax_id')
  134. ->label('Purchase Tax')
  135. ->relationship('purchaseTax', 'name')
  136. ->saveRelationshipsUsing(null)
  137. ->selectablePlaceholder(false)
  138. ->rule('required')
  139. ->searchable()
  140. ->preload(),
  141. Select::make('sales_discount_id')
  142. ->label('Sales Discount')
  143. ->relationship('salesDiscount', 'name')
  144. ->saveRelationshipsUsing(null)
  145. ->selectablePlaceholder(false)
  146. ->rule('required')
  147. ->searchable()
  148. ->preload(),
  149. Select::make('purchase_discount_id')
  150. ->label('Purchase Discount')
  151. ->relationship('purchaseDiscount', 'name')
  152. ->saveRelationshipsUsing(null)
  153. ->selectablePlaceholder(false)
  154. ->rule('required')
  155. ->searchable()
  156. ->preload(),
  157. ])->columns();
  158. }
  159. protected function getCategoriesSection(): Component
  160. {
  161. return Section::make('Categories')
  162. ->schema([
  163. Select::make('income_category_id')
  164. ->label('Income Category')
  165. ->relationship('incomeCategory', 'name')
  166. ->saveRelationshipsUsing(null)
  167. ->selectablePlaceholder(false)
  168. ->rule('required')
  169. ->searchable()
  170. ->preload(),
  171. Select::make('expense_category_id')
  172. ->label('Expense Category')
  173. ->relationship('expenseCategory', 'name')
  174. ->saveRelationshipsUsing(null)
  175. ->selectablePlaceholder(false)
  176. ->rule('required')
  177. ->searchable()
  178. ->preload(),
  179. ])->columns();
  180. }
  181. protected function handleRecordUpdate(CompanyDefaultModel $record, array $data): CompanyDefaultModel
  182. {
  183. CompanyDefaultUpdated::dispatch($record, $data);
  184. $record->update($data);
  185. return $record;
  186. }
  187. /**
  188. * @return array<Action | ActionGroup>
  189. */
  190. protected function getFormActions(): array
  191. {
  192. return [
  193. $this->getSaveFormAction(),
  194. ];
  195. }
  196. protected function getSaveFormAction(): Action
  197. {
  198. return Action::make('save')
  199. ->label(__('filament-panels::pages/tenancy/edit-tenant-profile.form.actions.save.label'))
  200. ->submit('save')
  201. ->keyBindings(['mod+s']);
  202. }
  203. public static function canView(Model $record): bool
  204. {
  205. try {
  206. return authorize('update', $record)->allowed();
  207. } catch (AuthorizationException $exception) {
  208. return $exception->toResponse()->allowed();
  209. }
  210. }
  211. }