Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DocumentDefaultResource.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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([
  140. 'lg' => 2,
  141. ]),
  142. ])->columns(3);
  143. }
  144. public static function getBillColumnLabelsSection(): Component
  145. {
  146. return Forms\Components\Section::make('Column Labels')
  147. ->visible(static fn (DocumentDefault $record) => $record->type === DocumentType::Bill)
  148. ->schema(static::getColumnLabelsSchema())->columns();
  149. }
  150. public static function getColumnLabelsSchema(): array
  151. {
  152. return [
  153. Forms\Components\Select::make('item_name.option')
  154. ->softRequired()
  155. ->localizeLabel('Item name')
  156. ->options(DocumentDefault::getAvailableItemNameOptions())
  157. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  158. if ($state !== 'other' && $old === 'other' && filled($get('item_name.custom'))) {
  159. $set('item_name.old_custom', $get('item_name.custom'));
  160. $set('item_name.custom', null);
  161. }
  162. if ($state === 'other' && $old !== 'other') {
  163. $set('item_name.custom', $get('item_name.old_custom'));
  164. }
  165. }),
  166. Forms\Components\TextInput::make('item_name.custom')
  167. ->hiddenLabel()
  168. ->extraFieldWrapperAttributes(static fn (DocumentDefault $record) => [
  169. 'class' => $record->type === DocumentType::Bill ? 'report-hidden-label' : '',
  170. ])
  171. ->disabled(static fn (callable $get) => $get('item_name.option') !== 'other')
  172. ->nullable(),
  173. Forms\Components\Select::make('unit_name.option')
  174. ->softRequired()
  175. ->localizeLabel('Unit name')
  176. ->options(DocumentDefault::getAvailableUnitNameOptions())
  177. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  178. if ($state !== 'other' && $old === 'other' && filled($get('unit_name.custom'))) {
  179. $set('unit_name.old_custom', $get('unit_name.custom'));
  180. $set('unit_name.custom', null);
  181. }
  182. if ($state === 'other' && $old !== 'other') {
  183. $set('unit_name.custom', $get('unit_name.old_custom'));
  184. }
  185. }),
  186. Forms\Components\TextInput::make('unit_name.custom')
  187. ->hiddenLabel()
  188. ->extraFieldWrapperAttributes(static fn (DocumentDefault $record) => [
  189. 'class' => $record->type === DocumentType::Bill ? 'report-hidden-label' : '',
  190. ])
  191. ->disabled(static fn (callable $get) => $get('unit_name.option') !== 'other')
  192. ->nullable(),
  193. Forms\Components\Select::make('price_name.option')
  194. ->softRequired()
  195. ->localizeLabel('Price name')
  196. ->options(DocumentDefault::getAvailablePriceNameOptions())
  197. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  198. if ($state !== 'other' && $old === 'other' && filled($get('price_name.custom'))) {
  199. $set('price_name.old_custom', $get('price_name.custom'));
  200. $set('price_name.custom', null);
  201. }
  202. if ($state === 'other' && $old !== 'other') {
  203. $set('price_name.custom', $get('price_name.old_custom'));
  204. }
  205. }),
  206. Forms\Components\TextInput::make('price_name.custom')
  207. ->hiddenLabel()
  208. ->extraFieldWrapperAttributes(static fn (DocumentDefault $record) => [
  209. 'class' => $record->type === DocumentType::Bill ? 'report-hidden-label' : '',
  210. ])
  211. ->disabled(static fn (callable $get) => $get('price_name.option') !== 'other')
  212. ->nullable(),
  213. Forms\Components\Select::make('amount_name.option')
  214. ->softRequired()
  215. ->localizeLabel('Amount name')
  216. ->options(DocumentDefault::getAvailableAmountNameOptions())
  217. ->afterStateUpdated(static function (Get $get, Set $set, $state, $old) {
  218. if ($state !== 'other' && $old === 'other' && filled($get('amount_name.custom'))) {
  219. $set('amount_name.old_custom', $get('amount_name.custom'));
  220. $set('amount_name.custom', null);
  221. }
  222. if ($state === 'other' && $old !== 'other') {
  223. $set('amount_name.custom', $get('amount_name.old_custom'));
  224. }
  225. }),
  226. Forms\Components\TextInput::make('amount_name.custom')
  227. ->hiddenLabel()
  228. ->extraFieldWrapperAttributes(static fn (DocumentDefault $record) => [
  229. 'class' => $record->type === DocumentType::Bill ? 'report-hidden-label' : '',
  230. ])
  231. ->disabled(static fn (callable $get) => $get('amount_name.option') !== 'other')
  232. ->nullable(),
  233. ];
  234. }
  235. public static function table(Table $table): Table
  236. {
  237. return $table
  238. ->columns([
  239. Tables\Columns\TextColumn::make('type')
  240. ->badge(),
  241. Tables\Columns\TextColumn::make('number_prefix'),
  242. Tables\Columns\TextColumn::make('template')
  243. ->badge(),
  244. Tables\Columns\IconColumn::make('show_logo')
  245. ->boolean(),
  246. ])
  247. ->filters([
  248. //
  249. ])
  250. ->actions([
  251. Tables\Actions\EditAction::make(),
  252. ])
  253. ->bulkActions([
  254. //
  255. ]);
  256. }
  257. public static function getRelations(): array
  258. {
  259. return [
  260. //
  261. ];
  262. }
  263. public static function getPages(): array
  264. {
  265. return [
  266. 'index' => Pages\ListDocumentDefaults::route('/'),
  267. 'edit' => Pages\EditDocumentDefault::route('/{record}/edit'),
  268. ];
  269. }
  270. }