You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DocumentDefaultResource.php 11KB

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