Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Invoice.php 14KB

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