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

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