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

DocumentDefaultResource.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace App\Filament\Company\Clusters\Settings\Resources;
  3. use App\Enums\Accounting\DocumentDiscountMethod;
  4. use App\Enums\Accounting\DocumentType;
  5. use App\Enums\Setting\Font;
  6. use App\Enums\Setting\PaymentTerms;
  7. use App\Enums\Setting\Template;
  8. use App\Filament\Company\Clusters\Settings;
  9. use App\Filament\Company\Clusters\Settings\Resources\DocumentDefaultResource\Pages;
  10. use App\Filament\Forms\Components\DocumentPreview;
  11. use App\Models\Setting\DocumentDefault;
  12. use Filament\Forms;
  13. use Filament\Forms\Components\Component;
  14. use Filament\Forms\Form;
  15. use Filament\Forms\Get;
  16. use Filament\Forms\Set;
  17. use Filament\Resources\Resource;
  18. use Filament\Tables;
  19. use Filament\Tables\Table;
  20. use Illuminate\Support\Facades\Auth;
  21. use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
  22. class DocumentDefaultResource extends Resource
  23. {
  24. protected static ?string $model = DocumentDefault::class;
  25. protected static ?string $cluster = Settings::class;
  26. protected static ?string $modelLabel = 'document template';
  27. public static function form(Form $form): Form
  28. {
  29. return $form
  30. ->live()
  31. ->schema([
  32. self::getGeneralSection(),
  33. self::getContentSection(),
  34. self::getTemplateSection(),
  35. self::getBillColumnLabelsSection(),
  36. ]);
  37. }
  38. public static function getGeneralSection(): Forms\Components\Component
  39. {
  40. return Forms\Components\Section::make('General')
  41. ->schema([
  42. Forms\Components\TextInput::make('number_prefix')
  43. ->localizeLabel()
  44. ->nullable(),
  45. Forms\Components\Select::make('payment_terms')
  46. ->softRequired()
  47. ->localizeLabel()
  48. ->options(PaymentTerms::class),
  49. Forms\Components\Select::make('discount_method')
  50. ->softRequired()
  51. ->options(DocumentDiscountMethod::class),
  52. ])->columns();
  53. }
  54. public static function getContentSection(): Forms\Components\Component
  55. {
  56. return Forms\Components\Section::make('Content')
  57. ->hidden(static fn (DocumentDefault $record) => $record->type === DocumentType::Bill)
  58. ->schema([
  59. Forms\Components\TextInput::make('header')
  60. ->localizeLabel()
  61. ->nullable(),
  62. Forms\Components\TextInput::make('subheader')
  63. ->localizeLabel()
  64. ->nullable(),
  65. Forms\Components\Textarea::make('terms')
  66. ->localizeLabel()
  67. ->nullable(),
  68. Forms\Components\Textarea::make('footer')
  69. ->localizeLabel('Footer')
  70. ->nullable(),
  71. ])->columns();
  72. }
  73. public static function getTemplateSection(): Component
  74. {
  75. return Forms\Components\Section::make('Template')
  76. ->description('Choose the template and edit the column names.')
  77. ->hidden(static fn (DocumentDefault $record) => $record->type === DocumentType::Bill)
  78. ->schema([
  79. Forms\Components\Grid::make(1)
  80. ->schema([
  81. Forms\Components\FileUpload::make('logo')
  82. ->openable()
  83. ->maxSize(1024)
  84. ->localizeLabel()
  85. ->visibility('public')
  86. ->disk('public')
  87. ->directory('logos/document')
  88. ->imageResizeMode('contain')
  89. ->imageCropAspectRatio('3:2')
  90. ->panelAspectRatio('3:2')
  91. ->panelLayout('integrated')
  92. ->removeUploadedFileButtonPosition('center bottom')
  93. ->uploadButtonPosition('center bottom')
  94. ->uploadProgressIndicatorPosition('center bottom')
  95. ->getUploadedFileNameForStorageUsing(
  96. static fn (TemporaryUploadedFile $file): string => (string) str($file->getClientOriginalName())
  97. ->prepend(Auth::user()->currentCompany->id . '_'),
  98. )
  99. ->extraAttributes([
  100. 'class' => 'aspect-[3/2] w-[9.375rem] max-w-full',
  101. ])
  102. ->acceptedFileTypes(['image/png', 'image/jpeg', 'image/gif']),
  103. Forms\Components\Checkbox::make('show_logo')
  104. ->localizeLabel(),
  105. Forms\Components\ColorPicker::make('accent_color')
  106. ->localizeLabel(),
  107. Forms\Components\Select::make('font')
  108. ->softRequired()
  109. ->localizeLabel()
  110. ->allowHtml()
  111. ->options(
  112. collect(Font::cases())
  113. ->mapWithKeys(static fn ($case) => [
  114. $case->value => "<span style='font-family:{$case->getLabel()}'>{$case->getLabel()}</span>",
  115. ]),
  116. ),
  117. Forms\Components\Select::make('template')
  118. ->softRequired()
  119. ->localizeLabel()
  120. ->options(Template::class),
  121. ...static::getColumnLabelsSchema(),
  122. ])->columnSpan(1),
  123. DocumentPreview::make()
  124. ->template(static fn (Get $get) => Template::parse($get('template')))
  125. ->preview()
  126. ->columnSpan([
  127. 'lg' => 2,
  128. ]),
  129. ])->columns(3);
  130. }
  131. public static function getBillColumnLabelsSection(): Component
  132. {
  133. return Forms\Components\Section::make('Column Labels')
  134. ->visible(static fn (DocumentDefault $record) => $record->type === DocumentType::Bill)
  135. ->schema(static::getColumnLabelsSchema())->columns();
  136. }
  137. public static function getColumnLabelsSchema(): array
  138. {
  139. return [
  140. Forms\Components\Select::make('item_name.option')
  141. ->softRequired()
  142. ->localizeLabel('Item name')
  143. ->options(DocumentDefault::getAvailableItemNameOptions())
  144. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  145. if ($state !== 'other' && $old === 'other' && filled($get('item_name.custom'))) {
  146. $set('item_name.old_custom', $get('item_name.custom'));
  147. $set('item_name.custom', null);
  148. }
  149. if ($state === 'other' && $old !== 'other') {
  150. $set('item_name.custom', $get('item_name.old_custom'));
  151. }
  152. }),
  153. Forms\Components\TextInput::make('item_name.custom')
  154. ->hiddenLabel()
  155. ->extraFieldWrapperAttributes(static fn (DocumentDefault $record) => [
  156. 'class' => $record->type === DocumentType::Bill ? 'report-hidden-label' : '',
  157. ])
  158. ->disabled(static fn (callable $get) => $get('item_name.option') !== 'other')
  159. ->nullable(),
  160. Forms\Components\Select::make('unit_name.option')
  161. ->softRequired()
  162. ->localizeLabel('Unit name')
  163. ->options(DocumentDefault::getAvailableUnitNameOptions())
  164. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  165. if ($state !== 'other' && $old === 'other' && filled($get('unit_name.custom'))) {
  166. $set('unit_name.old_custom', $get('unit_name.custom'));
  167. $set('unit_name.custom', null);
  168. }
  169. if ($state === 'other' && $old !== 'other') {
  170. $set('unit_name.custom', $get('unit_name.old_custom'));
  171. }
  172. }),
  173. Forms\Components\TextInput::make('unit_name.custom')
  174. ->hiddenLabel()
  175. ->extraFieldWrapperAttributes(static fn (DocumentDefault $record) => [
  176. 'class' => $record->type === DocumentType::Bill ? 'report-hidden-label' : '',
  177. ])
  178. ->disabled(static fn (callable $get) => $get('unit_name.option') !== 'other')
  179. ->nullable(),
  180. Forms\Components\Select::make('price_name.option')
  181. ->softRequired()
  182. ->localizeLabel('Price name')
  183. ->options(DocumentDefault::getAvailablePriceNameOptions())
  184. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  185. if ($state !== 'other' && $old === 'other' && filled($get('price_name.custom'))) {
  186. $set('price_name.old_custom', $get('price_name.custom'));
  187. $set('price_name.custom', null);
  188. }
  189. if ($state === 'other' && $old !== 'other') {
  190. $set('price_name.custom', $get('price_name.old_custom'));
  191. }
  192. }),
  193. Forms\Components\TextInput::make('price_name.custom')
  194. ->hiddenLabel()
  195. ->extraFieldWrapperAttributes(static fn (DocumentDefault $record) => [
  196. 'class' => $record->type === DocumentType::Bill ? 'report-hidden-label' : '',
  197. ])
  198. ->disabled(static fn (callable $get) => $get('price_name.option') !== 'other')
  199. ->nullable(),
  200. Forms\Components\Select::make('amount_name.option')
  201. ->softRequired()
  202. ->localizeLabel('Amount name')
  203. ->options(DocumentDefault::getAvailableAmountNameOptions())
  204. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  205. if ($state !== 'other' && $old === 'other' && filled($get('amount_name.custom'))) {
  206. $set('amount_name.old_custom', $get('amount_name.custom'));
  207. $set('amount_name.custom', null);
  208. }
  209. if ($state === 'other' && $old !== 'other') {
  210. $set('amount_name.custom', $get('amount_name.old_custom'));
  211. }
  212. }),
  213. Forms\Components\TextInput::make('amount_name.custom')
  214. ->hiddenLabel()
  215. ->extraFieldWrapperAttributes(static fn (DocumentDefault $record) => [
  216. 'class' => $record->type === DocumentType::Bill ? 'report-hidden-label' : '',
  217. ])
  218. ->disabled(static fn (callable $get) => $get('amount_name.option') !== 'other')
  219. ->nullable(),
  220. ];
  221. }
  222. public static function table(Table $table): Table
  223. {
  224. return $table
  225. ->columns([
  226. Tables\Columns\TextColumn::make('type')
  227. ->badge(),
  228. Tables\Columns\TextColumn::make('number_prefix'),
  229. Tables\Columns\TextColumn::make('template')
  230. ->badge(),
  231. Tables\Columns\IconColumn::make('show_logo')
  232. ->boolean(),
  233. ])
  234. ->filters([
  235. //
  236. ])
  237. ->actions([
  238. Tables\Actions\EditAction::make(),
  239. ])
  240. ->bulkActions([
  241. //
  242. ]);
  243. }
  244. public static function getRelations(): array
  245. {
  246. return [
  247. //
  248. ];
  249. }
  250. public static function getPages(): array
  251. {
  252. return [
  253. 'index' => Pages\ListDocumentDefaults::route('/'),
  254. 'edit' => Pages\EditDocumentDefault::route('/{record}/edit'),
  255. ];
  256. }
  257. }