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.

CreateVendorSelect.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Filament\Forms\Components;
  3. use App\Filament\Company\Resources\Purchases\VendorResource;
  4. use App\Models\Common\Vendor;
  5. use Filament\Forms\Components\Actions\Action;
  6. use Filament\Forms\Components\Select;
  7. use Filament\Forms\Form;
  8. use Filament\Support\Enums\MaxWidth;
  9. use Illuminate\Support\Facades\DB;
  10. class CreateVendorSelect extends Select
  11. {
  12. protected function setUp(): void
  13. {
  14. parent::setUp();
  15. $this
  16. ->searchable()
  17. ->preload()
  18. ->createOptionForm(fn (Form $form) => $this->createVendorForm($form))
  19. ->createOptionAction(fn (Action $action) => $this->createVendorAction($action));
  20. $this->relationship('vendor', 'name');
  21. $this->createOptionUsing(static function (array $data) {
  22. return DB::transaction(static function () use ($data) {
  23. /** @var Vendor $vendor */
  24. $vendor = Vendor::create([
  25. 'name' => $data['name'],
  26. 'type' => $data['type'],
  27. 'currency_code' => $data['currency_code'] ?? null,
  28. 'contractor_type' => $data['contractor_type'] ?? null,
  29. 'ssn' => $data['ssn'] ?? null,
  30. 'ein' => $data['ein'] ?? null,
  31. 'account_number' => $data['account_number'] ?? null,
  32. 'website' => $data['website'] ?? null,
  33. 'notes' => $data['notes'] ?? null,
  34. ]);
  35. if (isset($data['contact'], $data['contact']['first_name'])) {
  36. $vendor->contact()->create([
  37. 'is_primary' => true,
  38. 'first_name' => $data['contact']['first_name'],
  39. 'last_name' => $data['contact']['last_name'],
  40. 'email' => $data['contact']['email'],
  41. 'phones' => $data['contact']['phones'] ?? [],
  42. ]);
  43. }
  44. if (isset($data['address'], $data['address']['type'], $data['address']['address_line_1'])) {
  45. $vendor->address()->create([
  46. 'type' => $data['address']['type'],
  47. 'address_line_1' => $data['address']['address_line_1'],
  48. 'address_line_2' => $data['address']['address_line_2'] ?? null,
  49. 'country_code' => $data['address']['country_code'] ?? null,
  50. 'state_id' => $data['address']['state_id'] ?? null,
  51. 'city' => $data['address']['city'] ?? null,
  52. 'postal_code' => $data['address']['postal_code'] ?? null,
  53. ]);
  54. }
  55. return $vendor->getKey();
  56. });
  57. });
  58. }
  59. protected function createVendorForm(Form $form): Form
  60. {
  61. return VendorResource::form($form);
  62. }
  63. protected function createVendorAction(Action $action): Action
  64. {
  65. return $action
  66. ->label('Add vendor')
  67. ->slideOver()
  68. ->modalWidth(MaxWidth::ThreeExtraLarge);
  69. }
  70. }