Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

CompanyDefault.php 9.3KB

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