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

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