Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Invoice.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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, Set};
  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. ->live()
  91. ->schema([
  92. $this->getGeneralSection(),
  93. $this->getContentSection(),
  94. $this->getTemplateSection(),
  95. ])
  96. ->model($this->record)
  97. ->statePath('data')
  98. ->operation('edit');
  99. }
  100. protected function getGeneralSection(): Component
  101. {
  102. return Section::make('General')
  103. ->schema([
  104. TextInput::make('number_prefix')
  105. ->label('Number Prefix')
  106. ->nullable(),
  107. Select::make('number_digits')
  108. ->label('Number Digits')
  109. ->options(InvoiceModel::availableNumberDigits())
  110. ->selectablePlaceholder(false)
  111. ->native(false)
  112. ->rule('required'),
  113. TextInput::make('number_next')
  114. ->label('Next Number')
  115. ->maxLength(static fn (Get $get) => $get('number_digits'))
  116. ->suffix(static function (Get $get, $state) {
  117. $number_prefix = $get('number_prefix');
  118. $number_digits = $get('number_digits');
  119. $number_next = $state;
  120. return InvoiceModel::getNumberNext(true, true, $number_prefix, $number_digits, $number_next);
  121. })
  122. ->rule('required'),
  123. Select::make('payment_terms')
  124. ->label('Payment Terms')
  125. ->options(PaymentTerms::class)
  126. ->selectablePlaceholder(false)
  127. ->native(false)
  128. ->rule('required'),
  129. ])->columns();
  130. }
  131. protected function getContentSection(): Component
  132. {
  133. return Section::make('Content')
  134. ->schema([
  135. TextInput::make('header')
  136. ->label('Header')
  137. ->nullable(),
  138. TextInput::make('subheader')
  139. ->label('Subheader')
  140. ->nullable(),
  141. Textarea::make('terms')
  142. ->label('Terms')
  143. ->nullable(),
  144. Textarea::make('footer')
  145. ->label('Footer / Notes')
  146. ->nullable(),
  147. ])->columns();
  148. }
  149. protected function getTemplateSection(): Component
  150. {
  151. return Section::make('Template')
  152. ->description('Choose the template and edit the column names.')
  153. ->schema([
  154. Group::make()
  155. ->schema([
  156. FileUpload::make('logo')
  157. ->label('Logo')
  158. ->disk('public')
  159. ->directory('logos/document')
  160. ->imageResizeMode('contain')
  161. ->imagePreviewHeight('250')
  162. ->imageCropAspectRatio('2:1')
  163. ->getUploadedFileNameForStorageUsing(
  164. static fn (TemporaryUploadedFile $file): string => (string) str($file->getClientOriginalName())
  165. ->prepend(Auth::user()->currentCompany->id . '_'),
  166. )
  167. ->openable()
  168. ->maxSize(2048)
  169. ->image()
  170. ->visibility('public')
  171. ->acceptedFileTypes(['image/png', 'image/jpeg']),
  172. Checkbox::make('show_logo')
  173. ->label('Show Logo'),
  174. ColorPicker::make('accent_color')
  175. ->label('Accent Color'),
  176. Select::make('font')
  177. ->label('Font')
  178. ->native(false)
  179. ->selectablePlaceholder(false)
  180. ->rule('required')
  181. ->allowHtml()
  182. ->options(
  183. collect(Font::cases())
  184. ->mapWithKeys(static fn ($case) => [
  185. $case->value => "<span style='font-family:{$case->getLabel()}'>{$case->getLabel()}</span>",
  186. ]),
  187. ),
  188. Select::make('template')
  189. ->label('Template')
  190. ->native(false)
  191. ->options(Template::class)
  192. ->selectablePlaceholder(false)
  193. ->rule('required'),
  194. Select::make('item_name.option')
  195. ->label('Item Name')
  196. ->native(false)
  197. ->options(InvoiceModel::getAvailableItemNameOptions())
  198. ->selectablePlaceholder(false)
  199. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  200. if ($state !== 'other' && $old === 'other' && filled($get('item_name.custom'))) {
  201. $set('item_name.old_custom', $get('item_name.custom'));
  202. $set('item_name.custom', null);
  203. }
  204. if ($state === 'other' && $old !== 'other') {
  205. $set('item_name.custom', $get('item_name.old_custom'));
  206. }
  207. })
  208. ->rule('required'),
  209. TextInput::make('item_name.custom')
  210. ->hiddenLabel()
  211. ->disabled(static fn (callable $get) => $get('item_name.option') !== 'other')
  212. ->nullable(),
  213. Select::make('unit_name.option')
  214. ->label('Unit Name')
  215. ->native(false)
  216. ->options(InvoiceModel::getAvailableUnitNameOptions())
  217. ->selectablePlaceholder(false)
  218. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  219. if ($state !== 'other' && $old === 'other' && filled($get('unit_name.custom'))) {
  220. $set('unit_name.old_custom', $get('unit_name.custom'));
  221. $set('unit_name.custom', null);
  222. }
  223. if ($state === 'other' && $old !== 'other') {
  224. $set('unit_name.custom', $get('unit_name.old_custom'));
  225. }
  226. })
  227. ->rule('required'),
  228. TextInput::make('unit_name.custom')
  229. ->hiddenLabel()
  230. ->disabled(static fn (callable $get) => $get('unit_name.option') !== 'other')
  231. ->nullable(),
  232. Select::make('price_name.option')
  233. ->label('Price Name')
  234. ->native(false)
  235. ->options(InvoiceModel::getAvailablePriceNameOptions())
  236. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  237. if ($state !== 'other' && $old === 'other' && filled($get('price_name.custom'))) {
  238. $set('price_name.old_custom', $get('price_name.custom'));
  239. $set('price_name.custom', null);
  240. }
  241. if ($state === 'other' && $old !== 'other') {
  242. $set('price_name.custom', $get('price_name.old_custom'));
  243. }
  244. })
  245. ->selectablePlaceholder(false)
  246. ->rule('required'),
  247. TextInput::make('price_name.custom')
  248. ->hiddenLabel()
  249. ->disabled(static fn (callable $get) => $get('price_name.option') !== 'other')
  250. ->nullable(),
  251. Select::make('amount_name.option')
  252. ->label('Amount Name')
  253. ->native(false)
  254. ->options(InvoiceModel::getAvailableAmountNameOptions())
  255. ->selectablePlaceholder(false)
  256. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  257. if ($state !== 'other' && $old === 'other' && filled($get('amount_name.custom'))) {
  258. $set('amount_name.old_custom', $get('amount_name.custom'));
  259. $set('amount_name.custom', null);
  260. }
  261. if ($state === 'other' && $old !== 'other') {
  262. $set('amount_name.custom', $get('amount_name.old_custom'));
  263. }
  264. })
  265. ->rule('required'),
  266. TextInput::make('amount_name.custom')
  267. ->hiddenLabel()
  268. ->disabled(static fn (callable $get) => $get('amount_name.option') !== 'other')
  269. ->nullable(),
  270. ])->columns(1),
  271. Group::make()
  272. ->schema([
  273. ViewField::make('preview.default')
  274. ->label('Preview')
  275. ->visible(static fn (callable $get) => $get('template') === 'default')
  276. ->view('components.invoice-layouts.default'),
  277. ViewField::make('preview.modern')
  278. ->label('Preview')
  279. ->visible(static fn (callable $get) => $get('template') === 'modern')
  280. ->view('components.invoice-layouts.modern'),
  281. ViewField::make('preview.classic')
  282. ->label('Preview')
  283. ->visible(static fn (callable $get) => $get('template') === 'classic')
  284. ->view('components.invoice-layouts.classic'),
  285. ])->columnSpan(2),
  286. ])->columns(3);
  287. }
  288. protected function handleRecordUpdate(InvoiceModel $record, array $data): InvoiceModel
  289. {
  290. $record->update($data);
  291. return $record;
  292. }
  293. /**
  294. * @return array<Action | ActionGroup>
  295. */
  296. protected function getFormActions(): array
  297. {
  298. return [
  299. $this->getSaveFormAction(),
  300. ];
  301. }
  302. protected function getSaveFormAction(): Action
  303. {
  304. return Action::make('save')
  305. ->label(__('filament-panels::pages/tenancy/edit-tenant-profile.form.actions.save.label'))
  306. ->submit('save')
  307. ->keyBindings(['mod+s']);
  308. }
  309. public static function canView(Model $record): bool
  310. {
  311. try {
  312. return authorize('update', $record)->allowed();
  313. } catch (AuthorizationException $exception) {
  314. return $exception->toResponse()->allowed();
  315. }
  316. }
  317. }