Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VendorResource.php 11KB

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