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.

CompanyDefault.php 7.3KB

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