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

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