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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. MarkdownEditor::make('terms')
  141. ->nullable(),
  142. Textarea::make('footer')
  143. ->localizeLabel('Footer / Notes')
  144. ->nullable(),
  145. ])->columns();
  146. }
  147. protected function getTemplateSection(): Component
  148. {
  149. return Section::make('Template')
  150. ->description('Choose the template and edit the column names.')
  151. ->schema([
  152. Grid::make(1)
  153. ->schema([
  154. FileUpload::make('logo')
  155. ->openable()
  156. ->maxSize(1024)
  157. ->localizeLabel()
  158. ->visibility('public')
  159. ->disk('public')
  160. ->directory('logos/document')
  161. ->imageResizeMode('contain')
  162. ->imageCropAspectRatio('3:2')
  163. ->panelAspectRatio('3:2')
  164. ->panelLayout('integrated')
  165. ->removeUploadedFileButtonPosition('center bottom')
  166. ->uploadButtonPosition('center bottom')
  167. ->uploadProgressIndicatorPosition('center bottom')
  168. ->getUploadedFileNameForStorageUsing(
  169. static fn (TemporaryUploadedFile $file): string => (string) str($file->getClientOriginalName())
  170. ->prepend(Auth::user()->currentCompany->id . '_'),
  171. )
  172. ->extraAttributes([
  173. 'class' => 'aspect-[3/2] w-[9.375rem] max-w-full',
  174. ])
  175. ->acceptedFileTypes(['image/png', 'image/jpeg', 'image/gif']),
  176. Checkbox::make('show_logo')
  177. ->localizeLabel(),
  178. ColorPicker::make('accent_color')
  179. ->localizeLabel(),
  180. Select::make('font')
  181. ->softRequired()
  182. ->localizeLabel()
  183. ->allowHtml()
  184. ->options(
  185. collect(Font::cases())
  186. ->mapWithKeys(static fn ($case) => [
  187. $case->value => "<span style='font-family:{$case->getLabel()}'>{$case->getLabel()}</span>",
  188. ]),
  189. ),
  190. Select::make('template')
  191. ->softRequired()
  192. ->localizeLabel()
  193. ->options(Template::class),
  194. Select::make('item_name.option')
  195. ->softRequired()
  196. ->localizeLabel('Item Name')
  197. ->options(InvoiceModel::getAvailableItemNameOptions())
  198. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  199. if ($state !== 'other' && $old === 'other' && filled($get('item_name.custom'))) {
  200. $set('item_name.old_custom', $get('item_name.custom'));
  201. $set('item_name.custom', null);
  202. }
  203. if ($state === 'other' && $old !== 'other') {
  204. $set('item_name.custom', $get('item_name.old_custom'));
  205. }
  206. }),
  207. TextInput::make('item_name.custom')
  208. ->hiddenLabel()
  209. ->disabled(static fn (callable $get) => $get('item_name.option') !== 'other')
  210. ->nullable(),
  211. Select::make('unit_name.option')
  212. ->softRequired()
  213. ->localizeLabel('Unit Name')
  214. ->options(InvoiceModel::getAvailableUnitNameOptions())
  215. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  216. if ($state !== 'other' && $old === 'other' && filled($get('unit_name.custom'))) {
  217. $set('unit_name.old_custom', $get('unit_name.custom'));
  218. $set('unit_name.custom', null);
  219. }
  220. if ($state === 'other' && $old !== 'other') {
  221. $set('unit_name.custom', $get('unit_name.old_custom'));
  222. }
  223. }),
  224. TextInput::make('unit_name.custom')
  225. ->hiddenLabel()
  226. ->disabled(static fn (callable $get) => $get('unit_name.option') !== 'other')
  227. ->nullable(),
  228. Select::make('price_name.option')
  229. ->softRequired()
  230. ->localizeLabel('Price Name')
  231. ->options(InvoiceModel::getAvailablePriceNameOptions())
  232. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  233. if ($state !== 'other' && $old === 'other' && filled($get('price_name.custom'))) {
  234. $set('price_name.old_custom', $get('price_name.custom'));
  235. $set('price_name.custom', null);
  236. }
  237. if ($state === 'other' && $old !== 'other') {
  238. $set('price_name.custom', $get('price_name.old_custom'));
  239. }
  240. }),
  241. TextInput::make('price_name.custom')
  242. ->hiddenLabel()
  243. ->disabled(static fn (callable $get) => $get('price_name.option') !== 'other')
  244. ->nullable(),
  245. Select::make('amount_name.option')
  246. ->softRequired()
  247. ->localizeLabel('Amount Name')
  248. ->options(InvoiceModel::getAvailableAmountNameOptions())
  249. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  250. if ($state !== 'other' && $old === 'other' && filled($get('amount_name.custom'))) {
  251. $set('amount_name.old_custom', $get('amount_name.custom'));
  252. $set('amount_name.custom', null);
  253. }
  254. if ($state === 'other' && $old !== 'other') {
  255. $set('amount_name.custom', $get('amount_name.old_custom'));
  256. }
  257. }),
  258. TextInput::make('amount_name.custom')
  259. ->hiddenLabel()
  260. ->disabled(static fn (callable $get) => $get('amount_name.option') !== 'other')
  261. ->nullable(),
  262. ])->columnSpan(1),
  263. Grid::make()
  264. ->schema([
  265. ViewField::make('preview.default')
  266. ->columnSpan(2)
  267. ->hiddenLabel()
  268. ->visible(static fn (Get $get) => $get('template') === 'default')
  269. ->view('filament.company.components.invoice-layouts.default'),
  270. ViewField::make('preview.modern')
  271. ->columnSpan(2)
  272. ->hiddenLabel()
  273. ->visible(static fn (Get $get) => $get('template') === 'modern')
  274. ->view('filament.company.components.invoice-layouts.modern'),
  275. ViewField::make('preview.classic')
  276. ->columnSpan(2)
  277. ->hiddenLabel()
  278. ->visible(static fn (Get $get) => $get('template') === 'classic')
  279. ->view('filament.company.components.invoice-layouts.classic'),
  280. ])->columnSpan(2),
  281. ])->columns(3);
  282. }
  283. protected function handleRecordUpdate(InvoiceModel $record, array $data): InvoiceModel
  284. {
  285. $record->update($data);
  286. return $record;
  287. }
  288. /**
  289. * @return array<Action | ActionGroup>
  290. */
  291. protected function getFormActions(): array
  292. {
  293. return [
  294. $this->getSaveFormAction(),
  295. ];
  296. }
  297. protected function getSaveFormAction(): Action
  298. {
  299. return Action::make('save')
  300. ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
  301. ->submit('save')
  302. ->keyBindings(['mod+s']);
  303. }
  304. public static function canView(Model $record): bool
  305. {
  306. try {
  307. return authorize('update', $record)->allowed();
  308. } catch (AuthorizationException $exception) {
  309. return $exception->toResponse()->allowed();
  310. }
  311. }
  312. }