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

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