Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Invoice.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. namespace App\Filament\Company\Pages\Setting;
  3. use App\Enums\DocumentAmountColumn;
  4. use App\Enums\DocumentItemColumn;
  5. use App\Enums\DocumentPriceColumn;
  6. use App\Enums\DocumentType;
  7. use App\Enums\DocumentUnitColumn;
  8. use App\Enums\Font;
  9. use App\Enums\PaymentTerms;
  10. use App\Enums\Template;
  11. use App\Models\Setting\DocumentDefault as InvoiceModel;
  12. use Filament\Actions\Action;
  13. use Filament\Actions\ActionGroup;
  14. use Filament\Forms\Components\Checkbox;
  15. use Filament\Forms\Components\ColorPicker;
  16. use Filament\Forms\Components\Component;
  17. use Filament\Forms\Components\FileUpload;
  18. use Filament\Forms\Components\Group;
  19. use Filament\Forms\Components\Radio;
  20. use Filament\Forms\Components\Section;
  21. use Filament\Forms\Components\Select;
  22. use Filament\Forms\Components\Textarea;
  23. use Filament\Forms\Components\TextInput;
  24. use Filament\Forms\Components\ViewField;
  25. use Filament\Forms\Form;
  26. use Filament\Forms\Get;
  27. use Filament\Forms\Set;
  28. use Filament\Notifications\Notification;
  29. use Filament\Pages\Concerns\InteractsWithFormActions;
  30. use Filament\Pages\Page;
  31. use Filament\Support\Exceptions\Halt;
  32. use Illuminate\Auth\Access\AuthorizationException;
  33. use Illuminate\Database\Eloquent\Model;
  34. use Livewire\Attributes\Locked;
  35. use function Filament\authorize;
  36. /**
  37. * @property Form $form
  38. */
  39. class Invoice extends Page
  40. {
  41. use InteractsWithFormActions;
  42. protected static ?string $navigationIcon = 'heroicon-o-document-duplicate';
  43. protected static ?string $navigationLabel = 'Invoice';
  44. protected static ?string $navigationGroup = 'Settings';
  45. protected static ?string $slug = 'settings/invoice';
  46. protected ?string $heading = 'Invoice';
  47. protected static string $view = 'filament.company.pages.setting.invoice';
  48. public ?array $data = [];
  49. public ?InvoiceModel $record = null;
  50. public function mount(): void
  51. {
  52. $this->record = InvoiceModel::invoice()
  53. ->firstOrNew([
  54. 'company_id' => auth()->user()->currentCompany->id,
  55. 'type' => DocumentType::Invoice->value,
  56. ]);
  57. abort_unless(static::canView($this->record), 404);
  58. $this->fillForm();
  59. }
  60. public function fillForm(): void
  61. {
  62. $data = $this->record->attributesToArray();
  63. $data = $this->mutateFormDataBeforeFill($data);
  64. $this->form->fill($data);
  65. }
  66. protected function mutateFormDataBeforeFill(array $data): array
  67. {
  68. return $data;
  69. }
  70. protected function mutateFormDataBeforeSave(array $data): array
  71. {
  72. return $data;
  73. }
  74. public function save(): void
  75. {
  76. try {
  77. $data = $this->form->getState();
  78. $data = $this->mutateFormDataBeforeSave($data);
  79. $this->handleRecordUpdate($this->record, $data);
  80. } catch (Halt $exception) {
  81. return;
  82. }
  83. $this->getSavedNotification()?->send();
  84. if ($redirectUrl = $this->getRedirectUrl()) {
  85. $this->redirect($redirectUrl);
  86. }
  87. }
  88. protected function getSavedNotification(): ?Notification
  89. {
  90. $title = $this->getSavedNotificationTitle();
  91. if (blank($title)) {
  92. return null;
  93. }
  94. return Notification::make()
  95. ->success()
  96. ->title($this->getSavedNotificationTitle());
  97. }
  98. protected function getSavedNotificationTitle(): ?string
  99. {
  100. return __('filament-panels::pages/tenancy/edit-tenant-profile.notifications.saved.title');
  101. }
  102. protected function getRedirectUrl(): ?string
  103. {
  104. return null;
  105. }
  106. public function form(Form $form): Form
  107. {
  108. return $form
  109. ->schema([
  110. $this->getGeneralSection(),
  111. $this->getContentSection(),
  112. $this->getTemplateSection(),
  113. ])
  114. ->model($this->record)
  115. ->statePath('data')
  116. ->operation('edit');
  117. }
  118. protected function getGeneralSection(): Component
  119. {
  120. return Section::make('General')
  121. ->schema([
  122. TextInput::make('number_prefix')
  123. ->label('Number Prefix')
  124. ->live()
  125. ->required(),
  126. Select::make('number_digits')
  127. ->label('Number Digits')
  128. ->options(InvoiceModel::availableNumberDigits())
  129. ->native(false)
  130. ->live()
  131. ->required(),
  132. TextInput::make('number_next')
  133. ->label('Next Number')
  134. ->live()
  135. ->maxLength(static fn (Get $get) => $get('number_digits'))
  136. ->suffix(static function (Get $get, $state) {
  137. $number_prefix = $get('number_prefix');
  138. $number_digits = $get('number_digits');
  139. $number_next = $state;
  140. return InvoiceModel::getNumberNext(true, true, $number_prefix, $number_digits, $number_next);
  141. })
  142. ->required(),
  143. Select::make('payment_terms')
  144. ->label('Payment Terms')
  145. ->options(PaymentTerms::class)
  146. ->native(false)
  147. ->live()
  148. ->required(),
  149. ])->columns();
  150. }
  151. protected function getContentSection(): Component
  152. {
  153. return Section::make('Content')
  154. ->schema([
  155. TextInput::make('header')
  156. ->label('Header')
  157. ->live()
  158. ->required(),
  159. TextInput::make('subheader')
  160. ->label('Subheader')
  161. ->live()
  162. ->nullable(),
  163. Textarea::make('terms')
  164. ->label('Terms')
  165. ->live()
  166. ->nullable(),
  167. Textarea::make('footer')
  168. ->label('Footer / Notes')
  169. ->live()
  170. ->nullable(),
  171. ])->columns();
  172. }
  173. protected function getTemplateSection(): Component
  174. {
  175. return Section::make('Template')
  176. ->description('Choose the template and edit the column names.')
  177. ->schema([
  178. Group::make()
  179. ->live()
  180. ->schema([
  181. FileUpload::make('logo')
  182. ->label('Logo')
  183. ->disk('public')
  184. ->directory('logos/documents')
  185. ->imageResizeMode('contain')
  186. ->imagePreviewHeight('250')
  187. ->imageCropAspectRatio('2:1')
  188. ->openable()
  189. ->preserveFilenames()
  190. ->visibility('public')
  191. ->image(),
  192. Checkbox::make('show_logo')
  193. ->label('Show Logo'),
  194. ColorPicker::make('accent_color')
  195. ->label('Accent Color'),
  196. Select::make('font')
  197. ->label('Font')
  198. ->native(false)
  199. ->selectablePlaceholder(false)
  200. ->rule('required')
  201. ->allowHtml()
  202. ->options(collect(Font::cases())
  203. ->mapWithKeys(static fn ($case) => [
  204. $case->value => "<span style='font-family:{$case->getLabel()}'>{$case->getLabel()}</span>"
  205. ]),
  206. ),
  207. Select::make('template')
  208. ->label('Template')
  209. ->options(Template::class)
  210. ->required(),
  211. Select::make('item_name.option')
  212. ->label('Item Name')
  213. ->native(false)
  214. ->required()
  215. ->options(InvoiceModel::getAvailableItemNameOptions()),
  216. TextInput::make('item_name.custom')
  217. ->hiddenLabel()
  218. ->disabled(static fn (callable $get) => $get('item_name.option') !== 'other')
  219. ->nullable(),
  220. Select::make('unit_name.option')
  221. ->label('Unit Name')
  222. ->native(false)
  223. ->required()
  224. ->options(InvoiceModel::getAvailableUnitNameOptions()),
  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. ->label('Price Name')
  231. ->native(false)
  232. ->required()
  233. ->options(InvoiceModel::getAvailablePriceNameOptions()),
  234. TextInput::make('price_name.custom')
  235. ->hiddenLabel()
  236. ->disabled(static fn (callable $get) => $get('price_name.option') !== 'other')
  237. ->nullable(),
  238. Select::make('amount_name.option')
  239. ->label('Amount Name')
  240. ->native(false)
  241. ->required()
  242. ->options(InvoiceModel::getAvailableAmountNameOptions()),
  243. TextInput::make('amount_name.custom')
  244. ->hiddenLabel()
  245. ->disabled(static fn (callable $get) => $get('amount_name.option') !== 'other')
  246. ->nullable(),
  247. ])->columns(1),
  248. Group::make()
  249. ->schema([
  250. ViewField::make('preview.default')
  251. ->label('Preview')
  252. ->visible(static fn (callable $get) => $get('template') === 'default')
  253. ->view('components.invoice-layouts.default'),
  254. ViewField::make('preview.modern')
  255. ->label('Preview')
  256. ->visible(static fn (callable $get) => $get('template') === 'modern')
  257. ->view('components.invoice-layouts.modern'),
  258. ViewField::make('preview.classic')
  259. ->label('Preview')
  260. ->visible(static fn (callable $get) => $get('template') === 'classic')
  261. ->view('components.invoice-layouts.classic'),
  262. ])->columnSpan(2),
  263. ])->columns(3);
  264. }
  265. protected function handleRecordUpdate(InvoiceModel $record, array $data): InvoiceModel
  266. {
  267. $record->update($data);
  268. return $record;
  269. }
  270. /**
  271. * @return array<Action | ActionGroup>
  272. */
  273. protected function getFormActions(): array
  274. {
  275. return [
  276. $this->getSaveFormAction(),
  277. ];
  278. }
  279. protected function getSaveFormAction(): Action
  280. {
  281. return Action::make('save')
  282. ->label(__('filament-panels::pages/tenancy/edit-tenant-profile.form.actions.save.label'))
  283. ->submit('save')
  284. ->keyBindings(['mod+s']);
  285. }
  286. public static function canView(Model $record): bool
  287. {
  288. try {
  289. return authorize('update', $record)->allowed();
  290. } catch (AuthorizationException $exception) {
  291. return $exception->toResponse()->allowed();
  292. }
  293. }
  294. }