You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Invoice.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. namespace App\Filament\Company\Clusters\Settings\Pages;
  3. use App\Enums\Accounting\DocumentType;
  4. use App\Enums\Setting\Font;
  5. use App\Enums\Setting\PaymentTerms;
  6. use App\Enums\Setting\Template;
  7. use App\Filament\Company\Clusters\Settings;
  8. use App\Models\Setting\DocumentDefault as InvoiceModel;
  9. use Filament\Actions\Action;
  10. use Filament\Actions\ActionGroup;
  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\Section;
  17. use Filament\Forms\Components\Select;
  18. use Filament\Forms\Components\Textarea;
  19. use Filament\Forms\Components\TextInput;
  20. use Filament\Forms\Components\ViewField;
  21. use Filament\Forms\Form;
  22. use Filament\Forms\Get;
  23. use Filament\Forms\Set;
  24. use Filament\Notifications\Notification;
  25. use Filament\Pages\Concerns\InteractsWithFormActions;
  26. use Filament\Pages\Page;
  27. use Filament\Support\Enums\MaxWidth;
  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 $view = 'filament.company.pages.setting.invoice';
  43. protected static ?string $cluster = Settings::class;
  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 function getMaxContentWidth(): MaxWidth | string | null
  55. {
  56. return MaxWidth::ScreenTwoExtraLarge;
  57. }
  58. public function mount(): void
  59. {
  60. $this->record = InvoiceModel::invoice()
  61. ->firstOrNew([
  62. 'company_id' => auth()->user()->currentCompany->id,
  63. 'type' => DocumentType::Invoice->value,
  64. ]);
  65. abort_unless(static::canView($this->record), 404);
  66. $this->fillForm();
  67. }
  68. public function fillForm(): void
  69. {
  70. $data = $this->record->attributesToArray();
  71. $this->form->fill($data);
  72. }
  73. public function save(): void
  74. {
  75. try {
  76. $data = $this->form->getState();
  77. $this->handleRecordUpdate($this->record, $data);
  78. } catch (Halt $exception) {
  79. return;
  80. }
  81. $this->getSavedNotification()->send();
  82. }
  83. protected function getSavedNotification(): Notification
  84. {
  85. return Notification::make()
  86. ->success()
  87. ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title'));
  88. }
  89. public function form(Form $form): Form
  90. {
  91. return $form
  92. ->live()
  93. ->schema([
  94. $this->getGeneralSection(),
  95. $this->getContentSection(),
  96. $this->getTemplateSection(),
  97. ])
  98. ->model($this->record)
  99. ->statePath('data')
  100. ->operation('edit');
  101. }
  102. protected function getGeneralSection(): Component
  103. {
  104. return Section::make('General')
  105. ->schema([
  106. TextInput::make('number_prefix')
  107. ->localizeLabel()
  108. ->nullable(),
  109. Select::make('number_digits')
  110. ->softRequired()
  111. ->localizeLabel()
  112. ->options(InvoiceModel::availableNumberDigits()),
  113. TextInput::make('number_next')
  114. ->softRequired()
  115. ->localizeLabel()
  116. ->mask(static function (Get $get) {
  117. return str_repeat('9', $get('number_digits'));
  118. })
  119. ->hint(function (Get $get, $state) {
  120. $number_prefix = $get('number_prefix');
  121. $number_digits = $get('number_digits');
  122. $number_next = $state;
  123. return $this->record->getNumberNext(true, true, $number_prefix, $number_digits, $number_next);
  124. }),
  125. Select::make('payment_terms')
  126. ->softRequired()
  127. ->localizeLabel()
  128. ->options(PaymentTerms::class),
  129. ])->columns();
  130. }
  131. protected function getContentSection(): Component
  132. {
  133. return Section::make('Content')
  134. ->schema([
  135. TextInput::make('header')
  136. ->localizeLabel()
  137. ->nullable(),
  138. TextInput::make('subheader')
  139. ->localizeLabel()
  140. ->nullable(),
  141. Textarea::make('terms')
  142. ->localizeLabel()
  143. ->nullable(),
  144. Textarea::make('footer')
  145. ->localizeLabel('Footer')
  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. }