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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. ->maxSize(1024)
  81. ->localizeLabel()
  82. ->openable()
  83. ->directory('logos/document')
  84. ->image()
  85. ->imageCropAspectRatio('3:2')
  86. ->panelAspectRatio('3:2')
  87. ->panelLayout('compact')
  88. ->extraAttributes([
  89. 'class' => 'es-file-upload document-logo-preview',
  90. ])
  91. ->loadingIndicatorPosition('left')
  92. ->removeUploadedFileButtonPosition('right'),
  93. Forms\Components\Checkbox::make('show_logo')
  94. ->localizeLabel()
  95. ->hidden(is_demo_environment()),
  96. Forms\Components\ColorPicker::make('accent_color')
  97. ->localizeLabel(),
  98. Forms\Components\Select::make('font')
  99. ->softRequired()
  100. ->localizeLabel()
  101. ->allowHtml()
  102. ->options(
  103. collect(Font::cases())
  104. ->mapWithKeys(static fn ($case) => [
  105. $case->value => "<span style='font-family:{$case->getLabel()}'>{$case->getLabel()}</span>",
  106. ]),
  107. ),
  108. Forms\Components\Select::make('template')
  109. ->softRequired()
  110. ->localizeLabel()
  111. ->options(Template::class),
  112. ...static::getColumnLabelsSchema(),
  113. ])->columnSpan(1),
  114. DocumentPreview::make()
  115. ->template(static fn (Get $get) => Template::parse($get('template')))
  116. ->preview()
  117. ->columnSpan([
  118. 'lg' => 2,
  119. ]),
  120. ])->columns(3);
  121. }
  122. public static function getBillColumnLabelsSection(): Component
  123. {
  124. return Forms\Components\Section::make('Column Labels')
  125. ->visible(static fn (DocumentDefault $record) => $record->type === DocumentType::Bill)
  126. ->schema(static::getColumnLabelsSchema())->columns();
  127. }
  128. public static function getColumnLabelsSchema(): array
  129. {
  130. return [
  131. Forms\Components\Select::make('item_name.option')
  132. ->softRequired()
  133. ->localizeLabel('Item name')
  134. ->options(DocumentDefault::getAvailableItemNameOptions())
  135. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  136. if ($state !== 'other' && $old === 'other' && filled($get('item_name.custom'))) {
  137. $set('item_name.old_custom', $get('item_name.custom'));
  138. $set('item_name.custom', null);
  139. }
  140. if ($state === 'other' && $old !== 'other') {
  141. $set('item_name.custom', $get('item_name.old_custom'));
  142. }
  143. }),
  144. Forms\Components\TextInput::make('item_name.custom')
  145. ->hiddenLabel()
  146. ->extraFieldWrapperAttributes(static fn (DocumentDefault $record) => [
  147. 'class' => $record->type === DocumentType::Bill ? 'report-hidden-label' : '',
  148. ])
  149. ->disabled(static fn (callable $get) => $get('item_name.option') !== 'other')
  150. ->nullable(),
  151. Forms\Components\Select::make('unit_name.option')
  152. ->softRequired()
  153. ->localizeLabel('Unit name')
  154. ->options(DocumentDefault::getAvailableUnitNameOptions())
  155. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  156. if ($state !== 'other' && $old === 'other' && filled($get('unit_name.custom'))) {
  157. $set('unit_name.old_custom', $get('unit_name.custom'));
  158. $set('unit_name.custom', null);
  159. }
  160. if ($state === 'other' && $old !== 'other') {
  161. $set('unit_name.custom', $get('unit_name.old_custom'));
  162. }
  163. }),
  164. Forms\Components\TextInput::make('unit_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('unit_name.option') !== 'other')
  170. ->nullable(),
  171. Forms\Components\Select::make('price_name.option')
  172. ->softRequired()
  173. ->localizeLabel('Price name')
  174. ->options(DocumentDefault::getAvailablePriceNameOptions())
  175. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  176. if ($state !== 'other' && $old === 'other' && filled($get('price_name.custom'))) {
  177. $set('price_name.old_custom', $get('price_name.custom'));
  178. $set('price_name.custom', null);
  179. }
  180. if ($state === 'other' && $old !== 'other') {
  181. $set('price_name.custom', $get('price_name.old_custom'));
  182. }
  183. }),
  184. Forms\Components\TextInput::make('price_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('price_name.option') !== 'other')
  190. ->nullable(),
  191. Forms\Components\Select::make('amount_name.option')
  192. ->softRequired()
  193. ->localizeLabel('Amount name')
  194. ->options(DocumentDefault::getAvailableAmountNameOptions())
  195. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  196. if ($state !== 'other' && $old === 'other' && filled($get('amount_name.custom'))) {
  197. $set('amount_name.old_custom', $get('amount_name.custom'));
  198. $set('amount_name.custom', null);
  199. }
  200. if ($state === 'other' && $old !== 'other') {
  201. $set('amount_name.custom', $get('amount_name.old_custom'));
  202. }
  203. }),
  204. Forms\Components\TextInput::make('amount_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('amount_name.option') !== 'other')
  210. ->nullable(),
  211. ];
  212. }
  213. public static function table(Table $table): Table
  214. {
  215. return $table
  216. ->columns([
  217. Tables\Columns\TextColumn::make('type')
  218. ->badge(),
  219. Tables\Columns\TextColumn::make('number_prefix'),
  220. Tables\Columns\TextColumn::make('template')
  221. ->badge(),
  222. Tables\Columns\IconColumn::make('show_logo')
  223. ->boolean(),
  224. ])
  225. ->filters([
  226. //
  227. ])
  228. ->actions([
  229. Tables\Actions\EditAction::make(),
  230. ])
  231. ->bulkActions([
  232. //
  233. ]);
  234. }
  235. public static function getRelations(): array
  236. {
  237. return [
  238. //
  239. ];
  240. }
  241. public static function getPages(): array
  242. {
  243. return [
  244. 'index' => Pages\ListDocumentDefaults::route('/'),
  245. 'edit' => Pages\EditDocumentDefault::route('/{record}/edit'),
  246. ];
  247. }
  248. }