選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

VendorResource.php 11KB

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