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.

VendorResource.php 11KB

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