|
@@ -2,10 +2,9 @@
|
2
|
2
|
|
3
|
3
|
namespace App\Filament\Forms\Components;
|
4
|
4
|
|
|
5
|
+use App\Enums\Common\AddressType;
|
5
|
6
|
use App\Filament\Company\Resources\Sales\ClientResource;
|
6
|
|
-use App\Models\Common\Address;
|
7
|
7
|
use App\Models\Common\Client;
|
8
|
|
-use App\Models\Common\Contact;
|
9
|
8
|
use Filament\Forms\Components\Actions\Action;
|
10
|
9
|
use Filament\Forms\Components\Select;
|
11
|
10
|
use Filament\Forms\Form;
|
|
@@ -28,39 +27,86 @@ class CreateClientSelect extends Select
|
28
|
27
|
|
29
|
28
|
$this->createOptionUsing(static function (array $data) {
|
30
|
29
|
return DB::transaction(static function () use ($data) {
|
|
30
|
+ /** @var Client $client */
|
31
|
31
|
$client = Client::create([
|
32
|
32
|
'name' => $data['name'],
|
33
|
33
|
'website' => $data['website'] ?? null,
|
34
|
34
|
'notes' => $data['notes'] ?? null,
|
35
|
35
|
]);
|
36
|
36
|
|
37
|
|
- // Create primary contact
|
38
|
|
- $primaryContact = $client->contacts()->create([
|
39
|
|
- 'first_name' => $data['primary_contact']['first_name'],
|
40
|
|
- 'last_name' => $data['primary_contact']['last_name'],
|
41
|
|
- 'email' => $data['primary_contact']['email'],
|
42
|
|
- 'is_primary' => true,
|
43
|
|
- ]);
|
|
37
|
+ if (isset($data['primaryContact'], $data['primaryContact']['first_name'])) {
|
|
38
|
+ $client->primaryContact()->create([
|
|
39
|
+ 'is_primary' => true,
|
|
40
|
+ 'first_name' => $data['primaryContact']['first_name'],
|
|
41
|
+ 'last_name' => $data['primaryContact']['last_name'],
|
|
42
|
+ 'email' => $data['primaryContact']['email'],
|
|
43
|
+ 'phones' => $data['primaryContact']['phones'] ?? [],
|
|
44
|
+ ]);
|
|
45
|
+ }
|
44
|
46
|
|
45
|
|
- // Add phone number
|
46
|
|
- $primaryContact->phones()->create([
|
47
|
|
- 'type' => 'primary',
|
48
|
|
- 'number' => $data['primary_contact']['phones'][0]['number'],
|
49
|
|
- ]);
|
|
47
|
+ if (isset($data['secondaryContacts'])) {
|
|
48
|
+ foreach ($data['secondaryContacts'] as $contactData) {
|
|
49
|
+ if (isset($contactData['first_name'])) {
|
|
50
|
+ $client->secondaryContacts()->create([
|
|
51
|
+ 'is_primary' => false,
|
|
52
|
+ 'first_name' => $contactData['first_name'],
|
|
53
|
+ 'last_name' => $contactData['last_name'],
|
|
54
|
+ 'email' => $contactData['email'],
|
|
55
|
+ 'phones' => $contactData['phones'] ?? [],
|
|
56
|
+ ]);
|
|
57
|
+ }
|
|
58
|
+ }
|
|
59
|
+ }
|
50
|
60
|
|
51
|
|
- // Create billing address
|
52
|
|
- $client->addresses()->create([
|
53
|
|
- 'type' => 'billing',
|
54
|
|
- ...$data['billing_address'],
|
55
|
|
- ]);
|
|
61
|
+ if (isset($data['billingAddress'], $data['billingAddress']['address_line_1'])) {
|
|
62
|
+ $client->billingAddress()->create([
|
|
63
|
+ 'type' => AddressType::Billing,
|
|
64
|
+ 'address_line_1' => $data['billingAddress']['address_line_1'],
|
|
65
|
+ 'address_line_2' => $data['billingAddress']['address_line_2'] ?? null,
|
|
66
|
+ 'country_code' => $data['billingAddress']['country_code'] ?? null,
|
|
67
|
+ 'state_id' => $data['billingAddress']['state_id'] ?? null,
|
|
68
|
+ 'city' => $data['billingAddress']['city'] ?? null,
|
|
69
|
+ 'postal_code' => $data['billingAddress']['postal_code'] ?? null,
|
|
70
|
+ ]);
|
|
71
|
+ }
|
56
|
72
|
|
57
|
|
- // Create shipping address
|
58
|
|
- $client->addresses()->create([
|
59
|
|
- 'type' => 'shipping',
|
60
|
|
- 'recipient' => $data['shipping_address']['recipient'],
|
61
|
|
- 'phone' => $data['shipping_address']['phone'],
|
62
|
|
- ...$data['shipping_address'],
|
63
|
|
- ]);
|
|
73
|
+ if (isset($data['shippingAddress'])) {
|
|
74
|
+ $shippingData = $data['shippingAddress'];
|
|
75
|
+ $shippingAddress = [
|
|
76
|
+ 'type' => AddressType::Shipping,
|
|
77
|
+ 'recipient' => $shippingData['recipient'] ?? null,
|
|
78
|
+ 'phone' => $shippingData['phone'] ?? null,
|
|
79
|
+ 'notes' => $shippingData['notes'] ?? null,
|
|
80
|
+ ];
|
|
81
|
+
|
|
82
|
+ if ($shippingData['same_as_billing'] ?? false) {
|
|
83
|
+ $billingAddress = $client->billingAddress;
|
|
84
|
+ if ($billingAddress) {
|
|
85
|
+ $shippingAddress = [
|
|
86
|
+ ...$shippingAddress,
|
|
87
|
+ 'parent_address_id' => $billingAddress->id,
|
|
88
|
+ 'address_line_1' => $billingAddress->address_line_1,
|
|
89
|
+ 'address_line_2' => $billingAddress->address_line_2,
|
|
90
|
+ 'country_code' => $billingAddress->country_code,
|
|
91
|
+ 'state_id' => $billingAddress->state_id,
|
|
92
|
+ 'city' => $billingAddress->city,
|
|
93
|
+ 'postal_code' => $billingAddress->postal_code,
|
|
94
|
+ ];
|
|
95
|
+ $client->shippingAddress()->create($shippingAddress);
|
|
96
|
+ }
|
|
97
|
+ } elseif (isset($shippingData['address_line_1'])) {
|
|
98
|
+ $shippingAddress = [
|
|
99
|
+ ...$shippingAddress,
|
|
100
|
+ 'address_line_1' => $shippingData['address_line_1'],
|
|
101
|
+ 'address_line_2' => $shippingData['address_line_2'] ?? null,
|
|
102
|
+ 'country_code' => $shippingData['country_code'] ?? null,
|
|
103
|
+ 'state_id' => $shippingData['state_id'] ?? null,
|
|
104
|
+ 'city' => $shippingData['city'] ?? null,
|
|
105
|
+ 'postal_code' => $shippingData['postal_code'] ?? null,
|
|
106
|
+ ];
|
|
107
|
+ $client->shippingAddress()->create($shippingAddress);
|
|
108
|
+ }
|
|
109
|
+ }
|
64
|
110
|
|
65
|
111
|
return $client->getKey();
|
66
|
112
|
});
|