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

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