您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CreateClientSelect.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Filament\Forms\Components;
  3. use App\Enums\Common\AddressType;
  4. use App\Filament\Company\Resources\Sales\ClientResource;
  5. use App\Models\Common\Client;
  6. use Filament\Forms\Components\Actions\Action;
  7. use Filament\Forms\Components\Select;
  8. use Filament\Forms\Form;
  9. use Filament\Support\Enums\MaxWidth;
  10. use Illuminate\Support\Facades\DB;
  11. class CreateClientSelect extends Select
  12. {
  13. protected function setUp(): void
  14. {
  15. parent::setUp();
  16. $this
  17. ->searchable()
  18. ->preload()
  19. ->createOptionForm(fn (Form $form) => $this->createClientForm($form))
  20. ->createOptionAction(fn (Action $action) => $this->createClientAction($action));
  21. $this->relationship('client', 'name');
  22. $this->createOptionUsing(static function (array $data) {
  23. return DB::transaction(static function () use ($data) {
  24. /** @var Client $client */
  25. $client = Client::create([
  26. 'name' => $data['name'],
  27. 'website' => $data['website'] ?? null,
  28. 'notes' => $data['notes'] ?? null,
  29. ]);
  30. if (isset($data['primaryContact'], $data['primaryContact']['first_name'])) {
  31. $client->primaryContact()->create([
  32. 'is_primary' => true,
  33. 'first_name' => $data['primaryContact']['first_name'],
  34. 'last_name' => $data['primaryContact']['last_name'],
  35. 'email' => $data['primaryContact']['email'],
  36. 'phones' => $data['primaryContact']['phones'] ?? [],
  37. ]);
  38. }
  39. if (isset($data['secondaryContacts'])) {
  40. foreach ($data['secondaryContacts'] as $contactData) {
  41. if (isset($contactData['first_name'])) {
  42. $client->secondaryContacts()->create([
  43. 'is_primary' => false,
  44. 'first_name' => $contactData['first_name'],
  45. 'last_name' => $contactData['last_name'],
  46. 'email' => $contactData['email'],
  47. 'phones' => $contactData['phones'] ?? [],
  48. ]);
  49. }
  50. }
  51. }
  52. if (isset($data['billingAddress'], $data['billingAddress']['address_line_1'])) {
  53. $client->billingAddress()->create([
  54. 'type' => AddressType::Billing,
  55. 'address_line_1' => $data['billingAddress']['address_line_1'],
  56. 'address_line_2' => $data['billingAddress']['address_line_2'] ?? null,
  57. 'country_code' => $data['billingAddress']['country_code'] ?? null,
  58. 'state_id' => $data['billingAddress']['state_id'] ?? null,
  59. 'city' => $data['billingAddress']['city'] ?? null,
  60. 'postal_code' => $data['billingAddress']['postal_code'] ?? null,
  61. ]);
  62. }
  63. if (isset($data['shippingAddress'])) {
  64. $shippingData = $data['shippingAddress'];
  65. $shippingAddress = [
  66. 'type' => AddressType::Shipping,
  67. 'recipient' => $shippingData['recipient'] ?? null,
  68. 'phone' => $shippingData['phone'] ?? null,
  69. 'notes' => $shippingData['notes'] ?? null,
  70. ];
  71. if ($shippingData['same_as_billing'] ?? false) {
  72. $billingAddress = $client->billingAddress;
  73. if ($billingAddress) {
  74. $shippingAddress = [
  75. ...$shippingAddress,
  76. 'parent_address_id' => $billingAddress->id,
  77. 'address_line_1' => $billingAddress->address_line_1,
  78. 'address_line_2' => $billingAddress->address_line_2,
  79. 'country_code' => $billingAddress->country_code,
  80. 'state_id' => $billingAddress->state_id,
  81. 'city' => $billingAddress->city,
  82. 'postal_code' => $billingAddress->postal_code,
  83. ];
  84. $client->shippingAddress()->create($shippingAddress);
  85. }
  86. } elseif (isset($shippingData['address_line_1'])) {
  87. $shippingAddress = [
  88. ...$shippingAddress,
  89. 'address_line_1' => $shippingData['address_line_1'],
  90. 'address_line_2' => $shippingData['address_line_2'] ?? null,
  91. 'country_code' => $shippingData['country_code'] ?? null,
  92. 'state_id' => $shippingData['state_id'] ?? null,
  93. 'city' => $shippingData['city'] ?? null,
  94. 'postal_code' => $shippingData['postal_code'] ?? null,
  95. ];
  96. $client->shippingAddress()->create($shippingAddress);
  97. }
  98. }
  99. return $client->getKey();
  100. });
  101. });
  102. }
  103. protected function createClientForm(Form $form): Form
  104. {
  105. return ClientResource::form($form);
  106. }
  107. protected function createClientAction(Action $action): Action
  108. {
  109. return $action
  110. ->label('Add client')
  111. ->slideOver()
  112. ->modalWidth(MaxWidth::ThreeExtraLarge);
  113. }
  114. }