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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace App\Filament\Company\Resources\Purchases;
  3. use App\Enums\Accounting\BillStatus;
  4. use App\Enums\Common\ContractorType;
  5. use App\Enums\Common\VendorType;
  6. use App\Filament\Company\Resources\Purchases\VendorResource\Pages;
  7. use App\Filament\Forms\Components\AddressFields;
  8. use App\Filament\Forms\Components\CreateCurrencySelect;
  9. use App\Filament\Forms\Components\CustomSection;
  10. use App\Filament\Forms\Components\PhoneBuilder;
  11. use App\Filament\Tables\Columns;
  12. use App\Models\Common\Vendor;
  13. use App\Utilities\Currency\CurrencyConverter;
  14. use Filament\Forms;
  15. use Filament\Forms\Form;
  16. use Filament\Resources\Resource;
  17. use Filament\Tables;
  18. use Filament\Tables\Table;
  19. use Illuminate\Database\Eloquent\Builder;
  20. class VendorResource extends Resource
  21. {
  22. protected static ?string $model = Vendor::class;
  23. public static function form(Form $form): Form
  24. {
  25. return $form
  26. ->schema([
  27. Forms\Components\Section::make('General Information')
  28. ->schema([
  29. Forms\Components\Group::make()
  30. ->columns(2)
  31. ->schema([
  32. Forms\Components\TextInput::make('name')
  33. ->label('Vendor name')
  34. ->required()
  35. ->maxLength(255),
  36. Forms\Components\Radio::make('type')
  37. ->label('Vendor type')
  38. ->required()
  39. ->live()
  40. ->options(VendorType::class)
  41. ->default(VendorType::Regular)
  42. ->columnSpanFull(),
  43. CreateCurrencySelect::make('currency_code')
  44. ->softRequired()
  45. ->visible(static fn (Forms\Get $get) => VendorType::parse($get('type')) === VendorType::Regular),
  46. Forms\Components\Select::make('contractor_type')
  47. ->label('Contractor type')
  48. ->required()
  49. ->live()
  50. ->visible(static fn (Forms\Get $get) => VendorType::parse($get('type')) === VendorType::Contractor)
  51. ->options(ContractorType::class),
  52. Forms\Components\TextInput::make('ssn')
  53. ->label('Social security number')
  54. ->required()
  55. ->live()
  56. ->mask('999-99-9999')
  57. ->stripCharacters('-')
  58. ->maxLength(11)
  59. ->visible(static fn (Forms\Get $get) => ContractorType::parse($get('contractor_type')) === ContractorType::Individual)
  60. ->maxLength(255),
  61. Forms\Components\TextInput::make('ein')
  62. ->label('Employer identification number')
  63. ->required()
  64. ->live()
  65. ->mask('99-9999999')
  66. ->stripCharacters('-')
  67. ->maxLength(10)
  68. ->visible(static fn (Forms\Get $get) => ContractorType::parse($get('contractor_type')) === ContractorType::Business)
  69. ->maxLength(255),
  70. Forms\Components\TextInput::make('account_number')
  71. ->maxLength(255),
  72. Forms\Components\TextInput::make('website')
  73. ->maxLength(255),
  74. Forms\Components\Textarea::make('notes')
  75. ->columnSpanFull(),
  76. ]),
  77. CustomSection::make('Primary Contact')
  78. ->relationship('contact')
  79. ->saveRelationshipsUsing(null)
  80. ->saveRelationshipsBeforeChildrenUsing(null)
  81. ->dehydrated(true)
  82. ->contained(false)
  83. ->schema([
  84. Forms\Components\Hidden::make('is_primary')
  85. ->default(true),
  86. Forms\Components\TextInput::make('first_name')
  87. ->label('First name')
  88. ->maxLength(255),
  89. Forms\Components\TextInput::make('last_name')
  90. ->label('Last name')
  91. ->maxLength(255),
  92. Forms\Components\TextInput::make('email')
  93. ->label('Email')
  94. ->email()
  95. ->columnSpanFull()
  96. ->maxLength(255),
  97. PhoneBuilder::make('phones')
  98. ->hiddenLabel()
  99. ->blockLabels(false)
  100. ->default([
  101. ['type' => 'primary'],
  102. ])
  103. ->columnSpanFull()
  104. ->blocks([
  105. Forms\Components\Builder\Block::make('primary')
  106. ->schema([
  107. Forms\Components\TextInput::make('number')
  108. ->label('Phone')
  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. ->maxLength(15),
  116. ])->maxItems(1),
  117. Forms\Components\Builder\Block::make('toll_free')
  118. ->schema([
  119. Forms\Components\TextInput::make('number')
  120. ->label('Toll free')
  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. ->saveRelationshipsUsing(null)
  140. ->saveRelationshipsBeforeChildrenUsing(null)
  141. ->dehydrated(true)
  142. ->schema([
  143. Forms\Components\Hidden::make('type')
  144. ->default('general'),
  145. AddressFields::make(),
  146. ])
  147. ->columns(2),
  148. ]);
  149. }
  150. public static function table(Table $table): Table
  151. {
  152. return $table
  153. ->columns([
  154. Columns::id(),
  155. Tables\Columns\TextColumn::make('type')
  156. ->badge()
  157. ->searchable()
  158. ->sortable(),
  159. Tables\Columns\TextColumn::make('name')
  160. ->searchable()
  161. ->sortable()
  162. ->description(static fn (Vendor $vendor) => $vendor->contact?->full_name),
  163. Tables\Columns\TextColumn::make('contact.email')
  164. ->label('Email')
  165. ->searchable(),
  166. Tables\Columns\TextColumn::make('contact.first_available_phone')
  167. ->label('Phone')
  168. ->state(static fn (Vendor $vendor) => $vendor->contact?->first_available_phone),
  169. Tables\Columns\TextColumn::make('address.address_string')
  170. ->label('Address')
  171. ->searchable()
  172. ->toggleable(isToggledHiddenByDefault: true)
  173. ->listWithLineBreaks(),
  174. Tables\Columns\TextColumn::make('payable_balance')
  175. ->label('Payable balance')
  176. ->getStateUsing(function (Vendor $vendor) {
  177. return $vendor->bills()
  178. ->unpaid()
  179. ->get()
  180. ->sumMoneyInDefaultCurrency('amount_due');
  181. })
  182. ->coloredDescription(function (Vendor $vendor) {
  183. $overdue = $vendor->bills()
  184. ->where('status', BillStatus::Overdue)
  185. ->get()
  186. ->sumMoneyInDefaultCurrency('amount_due');
  187. if ($overdue <= 0) {
  188. return null;
  189. }
  190. $formattedOverdue = CurrencyConverter::formatCentsToMoney($overdue);
  191. return "Overdue: {$formattedOverdue}";
  192. })
  193. ->sortable(query: function (Builder $query, string $direction) {
  194. return $query
  195. ->withSum(['bills' => fn (Builder $query) => $query->unpaid()], 'amount_due')
  196. ->orderBy('bills_sum_amount_due', $direction);
  197. })
  198. ->currency(convert: false)
  199. ->alignEnd(),
  200. ])
  201. ->filters([
  202. //
  203. ])
  204. ->actions([
  205. Tables\Actions\ActionGroup::make([
  206. Tables\Actions\ActionGroup::make([
  207. Tables\Actions\EditAction::make(),
  208. Tables\Actions\ViewAction::make(),
  209. ])->dropdown(false),
  210. Tables\Actions\DeleteAction::make(),
  211. ]),
  212. ])
  213. ->bulkActions([
  214. //
  215. ]);
  216. }
  217. public static function getRelations(): array
  218. {
  219. return [
  220. //
  221. ];
  222. }
  223. public static function getPages(): array
  224. {
  225. return [
  226. 'index' => Pages\ListVendors::route('/'),
  227. 'create' => Pages\CreateVendor::route('/create'),
  228. 'view' => Pages\ViewVendor::route('/{record}'),
  229. 'edit' => Pages\EditVendor::route('/{record}/edit'),
  230. ];
  231. }
  232. }