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 13KB

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