您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Invoice.php 12KB

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