選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DocumentDefaultResource.php 13KB

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