Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

CompanyDefault.php 8.3KB

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