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 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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\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\Placeholder;
  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 | string | null
  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. Placeholder::make('currency_code')
  112. ->label(translate('Currency'))
  113. ->hintIcon('heroicon-o-question-mark-circle', 'You cannot change this after your company has been created. You can still use other currencies for transactions.')
  114. ->content(static fn (CompanyDefaultModel $record) => "{$record->currency->code} {$record->currency->symbol} - {$record->currency->name}"),
  115. ])->columns();
  116. }
  117. protected function getModifiersSection(): Component
  118. {
  119. return Section::make('Taxes & Discounts')
  120. ->schema([
  121. Select::make('sales_tax_id')
  122. ->softRequired()
  123. ->localizeLabel()
  124. ->relationship('salesTax', 'name')
  125. ->getOptionLabelFromRecordUsing(function (Tax $record) {
  126. $currencyCode = $this->record->currency_code;
  127. $rate = rateFormat($record->rate, $record->computation->value, $currencyCode);
  128. $rateBadge = $this->renderBadgeOptionLabel($rate);
  129. return "{$record->name} ⁓ {$rateBadge}";
  130. })
  131. ->allowHtml()
  132. ->saveRelationshipsUsing(null)
  133. ->searchable(),
  134. Select::make('purchase_tax_id')
  135. ->softRequired()
  136. ->localizeLabel()
  137. ->relationship('purchaseTax', 'name')
  138. ->getOptionLabelFromRecordUsing(function (Tax $record) {
  139. $currencyCode = $this->record->currency_code;
  140. $rate = rateFormat($record->rate, $record->computation->value, $currencyCode);
  141. $rateBadge = $this->renderBadgeOptionLabel($rate);
  142. return "{$record->name} ⁓ {$rateBadge}";
  143. })
  144. ->allowHtml()
  145. ->saveRelationshipsUsing(null)
  146. ->searchable(),
  147. Select::make('sales_discount_id')
  148. ->softRequired()
  149. ->localizeLabel()
  150. ->relationship('salesDiscount', 'name')
  151. ->getOptionLabelFromRecordUsing(function (Discount $record) {
  152. $currencyCode = $this->record->currency_code;
  153. $rate = rateFormat($record->rate, $record->computation->value, $currencyCode);
  154. $rateBadge = $this->renderBadgeOptionLabel($rate);
  155. return "{$record->name} ⁓ {$rateBadge}";
  156. })
  157. ->saveRelationshipsUsing(null)
  158. ->allowHtml()
  159. ->searchable(),
  160. Select::make('purchase_discount_id')
  161. ->softRequired()
  162. ->localizeLabel()
  163. ->relationship('purchaseDiscount', 'name')
  164. ->getOptionLabelFromRecordUsing(function (Discount $record) {
  165. $currencyCode = $this->record->currency_code;
  166. $rate = rateFormat($record->rate, $record->computation->value, $currencyCode);
  167. $rateBadge = $this->renderBadgeOptionLabel($rate);
  168. return "{$record->name} ⁓ {$rateBadge}";
  169. })
  170. ->allowHtml()
  171. ->saveRelationshipsUsing(null)
  172. ->searchable(),
  173. ])->columns();
  174. }
  175. public function renderBadgeOptionLabel(string $label): string
  176. {
  177. return Blade::render('filament::components.badge', [
  178. 'color' => 'primary',
  179. 'size' => 'sm',
  180. 'slot' => $label,
  181. ]);
  182. }
  183. protected function handleRecordUpdate(CompanyDefaultModel $record, array $data): CompanyDefaultModel
  184. {
  185. CompanyDefaultUpdated::dispatch($record, $data);
  186. $record->update($data);
  187. return $record;
  188. }
  189. /**
  190. * @return array<Action | ActionGroup>
  191. */
  192. protected function getFormActions(): array
  193. {
  194. return [
  195. $this->getSaveFormAction(),
  196. ];
  197. }
  198. protected function getSaveFormAction(): Action
  199. {
  200. return Action::make('save')
  201. ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
  202. ->submit('save')
  203. ->keyBindings(['mod+s']);
  204. }
  205. public static function canView(Model $record): bool
  206. {
  207. try {
  208. return authorize('update', $record)->allowed();
  209. } catch (AuthorizationException $exception) {
  210. return $exception->toResponse()->allowed();
  211. }
  212. }
  213. }