Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Invoice.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace App\Filament\Company\Pages\Setting;
  3. use App\Enums\DocumentType;
  4. use App\Enums\Font;
  5. use App\Enums\PaymentTerms;
  6. use App\Enums\Template;
  7. use App\Models\Setting\DocumentDefault as InvoiceModel;
  8. use Filament\Actions\Action;
  9. use Filament\Actions\ActionGroup;
  10. use Filament\Facades\Filament;
  11. use Filament\Forms\Components\Checkbox;
  12. use Filament\Forms\Components\ColorPicker;
  13. use Filament\Forms\Components\Component;
  14. use Filament\Forms\Components\FileUpload;
  15. use Filament\Forms\Components\Grid;
  16. use Filament\Forms\Components\MarkdownEditor;
  17. use Filament\Forms\Components\Section;
  18. use Filament\Forms\Components\Select;
  19. use Filament\Forms\Components\Textarea;
  20. use Filament\Forms\Components\TextInput;
  21. use Filament\Forms\Components\ViewField;
  22. use Filament\Forms\Form;
  23. use Filament\Forms\Get;
  24. use Filament\Forms\Set;
  25. use Filament\Notifications\Notification;
  26. use Filament\Pages\Concerns\InteractsWithFormActions;
  27. use Filament\Pages\Page;
  28. use Filament\Support\Exceptions\Halt;
  29. use Illuminate\Auth\Access\AuthorizationException;
  30. use Illuminate\Contracts\Support\Htmlable;
  31. use Illuminate\Database\Eloquent\Model;
  32. use Illuminate\Support\Facades\Auth;
  33. use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
  34. use function Filament\authorize;
  35. /**
  36. * @property Form $form
  37. */
  38. class Invoice extends Page
  39. {
  40. use InteractsWithFormActions;
  41. protected static ?string $title = 'Invoice';
  42. protected static ?string $slug = 'settings/invoice';
  43. protected static string $view = 'filament.company.pages.setting.invoice';
  44. public ?array $data = [];
  45. public ?InvoiceModel $record = null;
  46. public function getTitle(): string | Htmlable
  47. {
  48. return translate(static::$title);
  49. }
  50. public static function getNavigationLabel(): string
  51. {
  52. return translate(static::$title);
  53. }
  54. public static function getNavigationParentItem(): ?string
  55. {
  56. if (Filament::hasTopNavigation()) {
  57. return translate('Personalization');
  58. }
  59. return null;
  60. }
  61. public function mount(): void
  62. {
  63. $this->record = InvoiceModel::invoice()
  64. ->firstOrNew([
  65. 'company_id' => auth()->user()->currentCompany->id,
  66. 'type' => DocumentType::Invoice->value,
  67. ]);
  68. abort_unless(static::canView($this->record), 404);
  69. $this->fillForm();
  70. }
  71. public function fillForm(): void
  72. {
  73. $data = $this->record->attributesToArray();
  74. $this->form->fill($data);
  75. }
  76. public function save(): void
  77. {
  78. try {
  79. $data = $this->form->getState();
  80. $this->handleRecordUpdate($this->record, $data);
  81. } catch (Halt $exception) {
  82. return;
  83. }
  84. $this->getSavedNotification()->send();
  85. }
  86. protected function getSavedNotification(): Notification
  87. {
  88. return Notification::make()
  89. ->success()
  90. ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title'));
  91. }
  92. public function form(Form $form): Form
  93. {
  94. return $form
  95. ->live()
  96. ->schema([
  97. $this->getGeneralSection(),
  98. $this->getContentSection(),
  99. $this->getTemplateSection(),
  100. ])
  101. ->model($this->record)
  102. ->statePath('data')
  103. ->operation('edit');
  104. }
  105. protected function getGeneralSection(): Component
  106. {
  107. return Section::make('General')
  108. ->schema([
  109. TextInput::make('number_prefix')
  110. ->localizeLabel()
  111. ->nullable(),
  112. Select::make('number_digits')
  113. ->softRequired()
  114. ->localizeLabel()
  115. ->options(InvoiceModel::availableNumberDigits()),
  116. TextInput::make('number_next')
  117. ->softRequired()
  118. ->localizeLabel()
  119. ->maxLength(static fn (Get $get) => $get('number_digits'))
  120. ->hint(static function (Get $get, $state) {
  121. $number_prefix = $get('number_prefix');
  122. $number_digits = $get('number_digits');
  123. $number_next = $state;
  124. return InvoiceModel::getNumberNext(true, true, $number_prefix, $number_digits, $number_next);
  125. }),
  126. Select::make('payment_terms')
  127. ->softRequired()
  128. ->localizeLabel()
  129. ->options(PaymentTerms::class),
  130. ])->columns();
  131. }
  132. protected function getContentSection(): Component
  133. {
  134. return Section::make('Content')
  135. ->schema([
  136. TextInput::make('header')
  137. ->localizeLabel()
  138. ->nullable(),
  139. TextInput::make('subheader')
  140. ->localizeLabel()
  141. ->nullable(),
  142. MarkdownEditor::make('terms')
  143. ->nullable(),
  144. Textarea::make('footer')
  145. ->localizeLabel('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. Grid::make(1)
  155. ->schema([
  156. FileUpload::make('logo')
  157. ->openable()
  158. ->maxSize(1024)
  159. ->localizeLabel()
  160. ->visibility('public')
  161. ->disk('public')
  162. ->directory('logos/document')
  163. ->imageResizeMode('contain')
  164. ->imageCropAspectRatio('3:2')
  165. ->panelAspectRatio('3:2')
  166. ->panelLayout('integrated')
  167. ->removeUploadedFileButtonPosition('center bottom')
  168. ->uploadButtonPosition('center bottom')
  169. ->uploadProgressIndicatorPosition('center bottom')
  170. ->getUploadedFileNameForStorageUsing(
  171. static fn (TemporaryUploadedFile $file): string => (string) str($file->getClientOriginalName())
  172. ->prepend(Auth::user()->currentCompany->id . '_'),
  173. )
  174. ->extraAttributes([
  175. 'class' => 'aspect-[3/2] w-[9.375rem] max-w-full',
  176. ])
  177. ->acceptedFileTypes(['image/png', 'image/jpeg', 'image/gif']),
  178. Checkbox::make('show_logo')
  179. ->localizeLabel(),
  180. ColorPicker::make('accent_color')
  181. ->localizeLabel(),
  182. Select::make('font')
  183. ->softRequired()
  184. ->localizeLabel()
  185. ->allowHtml()
  186. ->options(
  187. collect(Font::cases())
  188. ->mapWithKeys(static fn ($case) => [
  189. $case->value => "<span style='font-family:{$case->getLabel()}'>{$case->getLabel()}</span>",
  190. ]),
  191. ),
  192. Select::make('template')
  193. ->softRequired()
  194. ->localizeLabel()
  195. ->options(Template::class),
  196. Select::make('item_name.option')
  197. ->softRequired()
  198. ->localizeLabel('Item Name')
  199. ->options(InvoiceModel::getAvailableItemNameOptions())
  200. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  201. if ($state !== 'other' && $old === 'other' && filled($get('item_name.custom'))) {
  202. $set('item_name.old_custom', $get('item_name.custom'));
  203. $set('item_name.custom', null);
  204. }
  205. if ($state === 'other' && $old !== 'other') {
  206. $set('item_name.custom', $get('item_name.old_custom'));
  207. }
  208. }),
  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. ->softRequired()
  215. ->localizeLabel('Unit Name')
  216. ->options(InvoiceModel::getAvailableUnitNameOptions())
  217. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  218. if ($state !== 'other' && $old === 'other' && filled($get('unit_name.custom'))) {
  219. $set('unit_name.old_custom', $get('unit_name.custom'));
  220. $set('unit_name.custom', null);
  221. }
  222. if ($state === 'other' && $old !== 'other') {
  223. $set('unit_name.custom', $get('unit_name.old_custom'));
  224. }
  225. }),
  226. TextInput::make('unit_name.custom')
  227. ->hiddenLabel()
  228. ->disabled(static fn (callable $get) => $get('unit_name.option') !== 'other')
  229. ->nullable(),
  230. Select::make('price_name.option')
  231. ->softRequired()
  232. ->localizeLabel('Price Name')
  233. ->options(InvoiceModel::getAvailablePriceNameOptions())
  234. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  235. if ($state !== 'other' && $old === 'other' && filled($get('price_name.custom'))) {
  236. $set('price_name.old_custom', $get('price_name.custom'));
  237. $set('price_name.custom', null);
  238. }
  239. if ($state === 'other' && $old !== 'other') {
  240. $set('price_name.custom', $get('price_name.old_custom'));
  241. }
  242. }),
  243. TextInput::make('price_name.custom')
  244. ->hiddenLabel()
  245. ->disabled(static fn (callable $get) => $get('price_name.option') !== 'other')
  246. ->nullable(),
  247. Select::make('amount_name.option')
  248. ->softRequired()
  249. ->localizeLabel('Amount Name')
  250. ->options(InvoiceModel::getAvailableAmountNameOptions())
  251. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  252. if ($state !== 'other' && $old === 'other' && filled($get('amount_name.custom'))) {
  253. $set('amount_name.old_custom', $get('amount_name.custom'));
  254. $set('amount_name.custom', null);
  255. }
  256. if ($state === 'other' && $old !== 'other') {
  257. $set('amount_name.custom', $get('amount_name.old_custom'));
  258. }
  259. }),
  260. TextInput::make('amount_name.custom')
  261. ->hiddenLabel()
  262. ->disabled(static fn (callable $get) => $get('amount_name.option') !== 'other')
  263. ->nullable(),
  264. ])->columnSpan(1),
  265. Grid::make()
  266. ->schema([
  267. ViewField::make('preview.default')
  268. ->columnSpan(2)
  269. ->hiddenLabel()
  270. ->visible(static fn (Get $get) => $get('template') === 'default')
  271. ->view('filament.company.components.invoice-layouts.default'),
  272. ViewField::make('preview.modern')
  273. ->columnSpan(2)
  274. ->hiddenLabel()
  275. ->visible(static fn (Get $get) => $get('template') === 'modern')
  276. ->view('filament.company.components.invoice-layouts.modern'),
  277. ViewField::make('preview.classic')
  278. ->columnSpan(2)
  279. ->hiddenLabel()
  280. ->visible(static fn (Get $get) => $get('template') === 'classic')
  281. ->view('filament.company.components.invoice-layouts.classic'),
  282. ])->columnSpan(2),
  283. ])->columns(3);
  284. }
  285. protected function handleRecordUpdate(InvoiceModel $record, array $data): InvoiceModel
  286. {
  287. $record->update($data);
  288. return $record;
  289. }
  290. /**
  291. * @return array<Action | ActionGroup>
  292. */
  293. protected function getFormActions(): array
  294. {
  295. return [
  296. $this->getSaveFormAction(),
  297. ];
  298. }
  299. protected function getSaveFormAction(): Action
  300. {
  301. return Action::make('save')
  302. ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
  303. ->submit('save')
  304. ->keyBindings(['mod+s']);
  305. }
  306. public static function canView(Model $record): bool
  307. {
  308. try {
  309. return authorize('update', $record)->allowed();
  310. } catch (AuthorizationException $exception) {
  311. return $exception->toResponse()->allowed();
  312. }
  313. }
  314. }