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ů.

VendorResource.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace App\Filament\Company\Resources\Common;
  3. use App\Enums\Common\ContractorType;
  4. use App\Enums\Common\VendorType;
  5. use App\Filament\Company\Resources\Common\VendorResource\Pages;
  6. use App\Filament\Forms\Components\CreateCurrencySelect;
  7. use App\Filament\Forms\Components\CustomSection;
  8. use App\Filament\Forms\Components\PhoneBuilder;
  9. use App\Models\Common\Vendor;
  10. use Filament\Forms;
  11. use Filament\Forms\Form;
  12. use Filament\Resources\Resource;
  13. use Filament\Tables;
  14. use Filament\Tables\Table;
  15. class VendorResource extends Resource
  16. {
  17. protected static ?string $model = Vendor::class;
  18. protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
  19. public static function form(Form $form): Form
  20. {
  21. return $form
  22. ->schema([
  23. Forms\Components\Section::make('General Information')
  24. ->schema([
  25. Forms\Components\Group::make()
  26. ->columns(2)
  27. ->schema([
  28. Forms\Components\TextInput::make('name')
  29. ->label('Vendor Name')
  30. ->required()
  31. ->maxLength(255),
  32. Forms\Components\Radio::make('type')
  33. ->label('Vendor Type')
  34. ->required()
  35. ->live()
  36. ->options(VendorType::class)
  37. ->default(VendorType::Regular)
  38. ->columnSpanFull(),
  39. CreateCurrencySelect::make('currency_code')
  40. ->relationship('currency', 'name')
  41. ->nullable()
  42. ->visible(fn (Forms\Get $get) => VendorType::parse($get('type')) === VendorType::Regular),
  43. Forms\Components\Select::make('contractor_type')
  44. ->label('Contractor Type')
  45. ->required()
  46. ->live()
  47. ->visible(fn (Forms\Get $get) => VendorType::parse($get('type')) === VendorType::Contractor)
  48. ->options(ContractorType::class),
  49. Forms\Components\TextInput::make('ssn')
  50. ->label('Social Security Number')
  51. ->required()
  52. ->live()
  53. ->mask('999-99-9999')
  54. ->stripCharacters('-')
  55. ->maxLength(11)
  56. ->visible(fn (Forms\Get $get) => ContractorType::parse($get('contractor_type')) === ContractorType::Individual)
  57. ->maxLength(255),
  58. Forms\Components\TextInput::make('ein')
  59. ->label('Employer Identification Number')
  60. ->required()
  61. ->live()
  62. ->mask('99-9999999')
  63. ->stripCharacters('-')
  64. ->maxLength(10)
  65. ->visible(fn (Forms\Get $get) => ContractorType::parse($get('contractor_type')) === ContractorType::Business)
  66. ->maxLength(255),
  67. Forms\Components\TextInput::make('account_number')
  68. ->maxLength(255),
  69. Forms\Components\TextInput::make('website')
  70. ->maxLength(255),
  71. Forms\Components\Textarea::make('notes')
  72. ->columnSpanFull(),
  73. ]),
  74. CustomSection::make('Primary Contact')
  75. ->relationship('contact')
  76. ->contained(false)
  77. ->schema([
  78. Forms\Components\Hidden::make('is_primary')
  79. ->default(true),
  80. Forms\Components\TextInput::make('first_name')
  81. ->label('First Name')
  82. ->required()
  83. ->maxLength(255),
  84. Forms\Components\TextInput::make('last_name')
  85. ->label('Last Name')
  86. ->required()
  87. ->maxLength(255),
  88. Forms\Components\TextInput::make('email')
  89. ->label('Email')
  90. ->required()
  91. ->email()
  92. ->columnSpanFull()
  93. ->maxLength(255),
  94. PhoneBuilder::make('phones')
  95. ->hiddenLabel()
  96. ->blockLabels(false)
  97. ->default([
  98. ['type' => 'primary'],
  99. ])
  100. ->columnSpanFull()
  101. ->blocks([
  102. Forms\Components\Builder\Block::make('primary')
  103. ->schema([
  104. Forms\Components\TextInput::make('number')
  105. ->label('Phone')
  106. ->required()
  107. ->maxLength(15),
  108. ])->maxItems(1),
  109. Forms\Components\Builder\Block::make('mobile')
  110. ->schema([
  111. Forms\Components\TextInput::make('number')
  112. ->label('Mobile')
  113. ->required()
  114. ->maxLength(15),
  115. ])->maxItems(1),
  116. Forms\Components\Builder\Block::make('toll_free')
  117. ->schema([
  118. Forms\Components\TextInput::make('number')
  119. ->label('Toll Free')
  120. ->required()
  121. ->maxLength(15),
  122. ])->maxItems(1),
  123. Forms\Components\Builder\Block::make('fax')
  124. ->schema([
  125. Forms\Components\TextInput::make('number')
  126. ->label('Fax')
  127. ->live()
  128. ->maxLength(15),
  129. ])->maxItems(1),
  130. ])
  131. ->deletable(fn (PhoneBuilder $builder) => $builder->getItemsCount() > 1)
  132. ->reorderable(false)
  133. ->blockNumbers(false)
  134. ->addActionLabel('Add Phone'),
  135. ])->columns(),
  136. ])->columns(1),
  137. Forms\Components\Section::make('Address Information')
  138. ->relationship('address')
  139. ->schema([
  140. Forms\Components\Hidden::make('type')
  141. ->default('general'),
  142. Forms\Components\TextInput::make('address_line_1')
  143. ->label('Address Line 1')
  144. ->required()
  145. ->maxLength(255),
  146. Forms\Components\TextInput::make('address_line_2')
  147. ->label('Address Line 2')
  148. ->maxLength(255),
  149. Forms\Components\TextInput::make('city')
  150. ->label('City')
  151. ->required()
  152. ->maxLength(255),
  153. Forms\Components\TextInput::make('state')
  154. ->label('State')
  155. ->required()
  156. ->maxLength(255),
  157. Forms\Components\TextInput::make('postal_code')
  158. ->label('Postal Code / Zip Code')
  159. ->required()
  160. ->maxLength(255),
  161. Forms\Components\TextInput::make('country')
  162. ->label('Country')
  163. ->required()
  164. ->maxLength(255),
  165. ])
  166. ->columns(2),
  167. ]);
  168. }
  169. public static function table(Table $table): Table
  170. {
  171. return $table
  172. ->columns([
  173. Tables\Columns\TextColumn::make('type')
  174. ->badge()
  175. ->searchable(),
  176. Tables\Columns\TextColumn::make('name')
  177. ->searchable()
  178. ->description(fn (Vendor $vendor) => $vendor->contact?->full_name),
  179. Tables\Columns\TextColumn::make('contact.email')
  180. ->label('Email')
  181. ->searchable(),
  182. Tables\Columns\TextColumn::make('primaryContact.phones')
  183. ->label('Phone')
  184. ->state(fn (Vendor $vendor) => $vendor->contact?->first_available_phone),
  185. ])
  186. ->filters([
  187. //
  188. ])
  189. ->actions([
  190. Tables\Actions\EditAction::make(),
  191. ])
  192. ->bulkActions([
  193. Tables\Actions\BulkActionGroup::make([
  194. Tables\Actions\DeleteBulkAction::make(),
  195. ]),
  196. ]);
  197. }
  198. public static function getRelations(): array
  199. {
  200. return [
  201. //
  202. ];
  203. }
  204. public static function getPages(): array
  205. {
  206. return [
  207. 'index' => Pages\ListVendors::route('/'),
  208. 'create' => Pages\CreateVendor::route('/create'),
  209. 'edit' => Pages\EditVendor::route('/{record}/edit'),
  210. ];
  211. }
  212. }