Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DocumentDefaultResource.php 13KB

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