Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Invoice.php 14KB

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