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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace App\Filament\Company\Pages\Setting;
  3. use App\Enums\{DocumentType, Font, PaymentTerms, Template};
  4. use App\Models\Setting\DocumentDefault as InvoiceModel;
  5. use Filament\Actions\{Action, ActionGroup};
  6. use Filament\Forms\Components\{Checkbox, ColorPicker, Component, FileUpload, Group, Section, Select, TextInput, Textarea, ViewField};
  7. use Filament\Forms\{Form, Get};
  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 Illuminate\Support\Facades\Auth;
  15. use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
  16. use function Filament\authorize;
  17. /**
  18. * @property Form $form
  19. */
  20. class Invoice extends Page
  21. {
  22. use InteractsWithFormActions;
  23. protected static ?string $navigationIcon = 'heroicon-o-document-duplicate';
  24. protected static ?string $navigationLabel = 'Invoice';
  25. protected static ?string $navigationGroup = 'Settings';
  26. protected static ?string $slug = 'settings/invoice';
  27. protected ?string $heading = 'Invoice';
  28. protected static string $view = 'filament.company.pages.setting.invoice';
  29. public ?array $data = [];
  30. public ?InvoiceModel $record = null;
  31. public function mount(): void
  32. {
  33. $this->record = InvoiceModel::invoice()
  34. ->firstOrNew([
  35. 'company_id' => auth()->user()->currentCompany->id,
  36. 'type' => DocumentType::Invoice->value,
  37. ]);
  38. abort_unless(static::canView($this->record), 404);
  39. $this->fillForm();
  40. }
  41. public function fillForm(): void
  42. {
  43. $data = $this->record->attributesToArray();
  44. $data = $this->mutateFormDataBeforeFill($data);
  45. $this->form->fill($data);
  46. }
  47. protected function mutateFormDataBeforeFill(array $data): array
  48. {
  49. return $data;
  50. }
  51. protected function mutateFormDataBeforeSave(array $data): array
  52. {
  53. return $data;
  54. }
  55. public function save(): void
  56. {
  57. try {
  58. $data = $this->form->getState();
  59. $data = $this->mutateFormDataBeforeSave($data);
  60. $this->handleRecordUpdate($this->record, $data);
  61. } catch (Halt $exception) {
  62. return;
  63. }
  64. $this->getSavedNotification()?->send();
  65. if ($redirectUrl = $this->getRedirectUrl()) {
  66. $this->redirect($redirectUrl);
  67. }
  68. }
  69. protected function getSavedNotification(): ?Notification
  70. {
  71. $title = $this->getSavedNotificationTitle();
  72. if (blank($title)) {
  73. return null;
  74. }
  75. return Notification::make()
  76. ->success()
  77. ->title($this->getSavedNotificationTitle());
  78. }
  79. protected function getSavedNotificationTitle(): ?string
  80. {
  81. return __('filament-panels::pages/tenancy/edit-tenant-profile.notifications.saved.title');
  82. }
  83. protected function getRedirectUrl(): ?string
  84. {
  85. return null;
  86. }
  87. public function form(Form $form): Form
  88. {
  89. return $form
  90. ->schema([
  91. $this->getGeneralSection(),
  92. $this->getContentSection(),
  93. $this->getTemplateSection(),
  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. TextInput::make('number_prefix')
  104. ->label('Number Prefix')
  105. ->live()
  106. ->required(),
  107. Select::make('number_digits')
  108. ->label('Number Digits')
  109. ->options(InvoiceModel::availableNumberDigits())
  110. ->native(false)
  111. ->live()
  112. ->required(),
  113. TextInput::make('number_next')
  114. ->label('Next Number')
  115. ->live()
  116. ->maxLength(static fn (Get $get) => $get('number_digits'))
  117. ->suffix(static function (Get $get, $state) {
  118. $number_prefix = $get('number_prefix');
  119. $number_digits = $get('number_digits');
  120. $number_next = $state;
  121. return InvoiceModel::getNumberNext(true, true, $number_prefix, $number_digits, $number_next);
  122. })
  123. ->required(),
  124. Select::make('payment_terms')
  125. ->label('Payment Terms')
  126. ->options(PaymentTerms::class)
  127. ->native(false)
  128. ->live()
  129. ->required(),
  130. ])->columns();
  131. }
  132. protected function getContentSection(): Component
  133. {
  134. return Section::make('Content')
  135. ->schema([
  136. TextInput::make('header')
  137. ->label('Header')
  138. ->live()
  139. ->required(),
  140. TextInput::make('subheader')
  141. ->label('Subheader')
  142. ->live()
  143. ->nullable(),
  144. Textarea::make('terms')
  145. ->label('Terms')
  146. ->live()
  147. ->nullable(),
  148. Textarea::make('footer')
  149. ->label('Footer / Notes')
  150. ->live()
  151. ->nullable(),
  152. ])->columns();
  153. }
  154. protected function getTemplateSection(): Component
  155. {
  156. return Section::make('Template')
  157. ->description('Choose the template and edit the column names.')
  158. ->schema([
  159. Group::make()
  160. ->live()
  161. ->schema([
  162. FileUpload::make('logo')
  163. ->label('Logo')
  164. ->disk('public')
  165. ->directory('logos/document')
  166. ->imageResizeMode('contain')
  167. ->imagePreviewHeight('250')
  168. ->imageCropAspectRatio('2:1')
  169. ->getUploadedFileNameForStorageUsing(
  170. static fn (TemporaryUploadedFile $file): string => (string) str($file->getClientOriginalName())
  171. ->prepend(Auth::user()->currentCompany->id . '_'),
  172. )
  173. ->openable()
  174. ->maxSize(2048)
  175. ->image()
  176. ->visibility('public')
  177. ->acceptedFileTypes(['image/png', 'image/jpeg']),
  178. Checkbox::make('show_logo')
  179. ->label('Show Logo'),
  180. ColorPicker::make('accent_color')
  181. ->label('Accent Color'),
  182. Select::make('font')
  183. ->label('Font')
  184. ->native(false)
  185. ->selectablePlaceholder(false)
  186. ->rule('required')
  187. ->allowHtml()
  188. ->options(
  189. collect(Font::cases())
  190. ->mapWithKeys(static fn ($case) => [
  191. $case->value => "<span style='font-family:{$case->getLabel()}'>{$case->getLabel()}</span>",
  192. ]),
  193. ),
  194. Select::make('template')
  195. ->label('Template')
  196. ->native(false)
  197. ->options(Template::class)
  198. ->required(),
  199. Select::make('item_name.option')
  200. ->label('Item Name')
  201. ->native(false)
  202. ->required()
  203. ->options(InvoiceModel::getAvailableItemNameOptions()),
  204. TextInput::make('item_name.custom')
  205. ->hiddenLabel()
  206. ->disabled(static fn (callable $get) => $get('item_name.option') !== 'other')
  207. ->nullable(),
  208. Select::make('unit_name.option')
  209. ->label('Unit Name')
  210. ->native(false)
  211. ->required()
  212. ->options(InvoiceModel::getAvailableUnitNameOptions()),
  213. TextInput::make('unit_name.custom')
  214. ->hiddenLabel()
  215. ->disabled(static fn (callable $get) => $get('unit_name.option') !== 'other')
  216. ->nullable(),
  217. Select::make('price_name.option')
  218. ->label('Price Name')
  219. ->native(false)
  220. ->required()
  221. ->options(InvoiceModel::getAvailablePriceNameOptions()),
  222. TextInput::make('price_name.custom')
  223. ->hiddenLabel()
  224. ->disabled(static fn (callable $get) => $get('price_name.option') !== 'other')
  225. ->nullable(),
  226. Select::make('amount_name.option')
  227. ->label('Amount Name')
  228. ->native(false)
  229. ->required()
  230. ->options(InvoiceModel::getAvailableAmountNameOptions()),
  231. TextInput::make('amount_name.custom')
  232. ->hiddenLabel()
  233. ->disabled(static fn (callable $get) => $get('amount_name.option') !== 'other')
  234. ->nullable(),
  235. ])->columns(1),
  236. Group::make()
  237. ->schema([
  238. ViewField::make('preview.default')
  239. ->label('Preview')
  240. ->visible(static fn (callable $get) => $get('template') === 'default')
  241. ->view('components.invoice-layouts.default'),
  242. ViewField::make('preview.modern')
  243. ->label('Preview')
  244. ->visible(static fn (callable $get) => $get('template') === 'modern')
  245. ->view('components.invoice-layouts.modern'),
  246. ViewField::make('preview.classic')
  247. ->label('Preview')
  248. ->visible(static fn (callable $get) => $get('template') === 'classic')
  249. ->view('components.invoice-layouts.classic'),
  250. ])->columnSpan(2),
  251. ])->columns(3);
  252. }
  253. protected function handleRecordUpdate(InvoiceModel $record, array $data): InvoiceModel
  254. {
  255. $record->update($data);
  256. return $record;
  257. }
  258. /**
  259. * @return array<Action | ActionGroup>
  260. */
  261. protected function getFormActions(): array
  262. {
  263. return [
  264. $this->getSaveFormAction(),
  265. ];
  266. }
  267. protected function getSaveFormAction(): Action
  268. {
  269. return Action::make('save')
  270. ->label(__('filament-panels::pages/tenancy/edit-tenant-profile.form.actions.save.label'))
  271. ->submit('save')
  272. ->keyBindings(['mod+s']);
  273. }
  274. public static function canView(Model $record): bool
  275. {
  276. try {
  277. return authorize('update', $record)->allowed();
  278. } catch (AuthorizationException $exception) {
  279. return $exception->toResponse()->allowed();
  280. }
  281. }
  282. }