Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

CompanyDefault.php 9.0KB

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