Andrew Wallo 5 maanden geleden
bovenliggende
commit
195155fa7f

+ 1
- 26
app/Filament/Company/Resources/Purchases/VendorResource/Pages/CreateVendor.php Bestand weergeven

@@ -17,32 +17,7 @@ class CreateVendor extends CreateRecord
17 17
 
18 18
     protected function handleRecordCreation(array $data): Model
19 19
     {
20
-        /** @var Vendor $vendor */
21
-        $vendor = parent::handleRecordCreation($data);
22
-
23
-        if (isset($data['contact'], $data['contact']['first_name'])) {
24
-            $vendor->contact()->create([
25
-                'is_primary' => true,
26
-                'first_name' => $data['contact']['first_name'],
27
-                'last_name' => $data['contact']['last_name'],
28
-                'email' => $data['contact']['email'],
29
-                'phones' => $data['contact']['phones'] ?? [],
30
-            ]);
31
-        }
32
-
33
-        if (isset($data['address'], $data['address']['type'], $data['address']['address_line_1'])) {
34
-            $vendor->address()->create([
35
-                'type' => $data['address']['type'],
36
-                'address_line_1' => $data['address']['address_line_1'],
37
-                'address_line_2' => $data['address']['address_line_2'] ?? null,
38
-                'country_code' => $data['address']['country_code'] ?? null,
39
-                'state_id' => $data['address']['state_id'] ?? null,
40
-                'city' => $data['address']['city'] ?? null,
41
-                'postal_code' => $data['address']['postal_code'] ?? null,
42
-            ]);
43
-        }
44
-
45
-        return $vendor;
20
+        return Vendor::createWithRelations($data);
46 21
     }
47 22
 
48 23
     public function getMaxContentWidth(): MaxWidth | string | null

+ 3
- 29
app/Filament/Company/Resources/Purchases/VendorResource/Pages/EditVendor.php Bestand weergeven

@@ -18,36 +18,10 @@ class EditVendor extends EditRecord
18 18
 
19 19
     protected function handleRecordUpdate(Model $record, array $data): Model
20 20
     {
21
-        /** @var Vendor $vendor */
22
-        $vendor = parent::handleRecordUpdate($record, $data);
21
+        /** @var Vendor $record */
22
+        $record->updateWithRelations($data);
23 23
 
24
-        if (isset($data['contact'], $data['contact']['first_name'])) {
25
-            $vendor->contact()->updateOrCreate(
26
-                ['is_primary' => true],
27
-                [
28
-                    'first_name' => $data['contact']['first_name'],
29
-                    'last_name' => $data['contact']['last_name'],
30
-                    'email' => $data['contact']['email'],
31
-                    'phones' => $data['contact']['phones'] ?? [],
32
-                ]
33
-            );
34
-        }
35
-
36
-        if (isset($data['address'], $data['address']['type'], $data['address']['address_line_1'])) {
37
-            $vendor->address()->updateOrCreate(
38
-                ['type' => $data['address']['type']],
39
-                [
40
-                    'address_line_1' => $data['address']['address_line_1'],
41
-                    'address_line_2' => $data['address']['address_line_2'] ?? null,
42
-                    'country_code' => $data['address']['country_code'] ?? null,
43
-                    'state_id' => $data['address']['state_id'] ?? null,
44
-                    'city' => $data['address']['city'] ?? null,
45
-                    'postal_code' => $data['address']['postal_code'] ?? null,
46
-                ]
47
-            );
48
-        }
49
-
50
-        return $vendor;
24
+        return $record;
51 25
     }
52 26
 
53 27
     protected function getHeaderActions(): array

+ 1
- 79
app/Filament/Company/Resources/Sales/ClientResource/Pages/CreateClient.php Bestand weergeven

@@ -3,7 +3,6 @@
3 3
 namespace App\Filament\Company\Resources\Sales\ClientResource\Pages;
4 4
 
5 5
 use App\Concerns\HandlePageRedirect;
6
-use App\Enums\Common\AddressType;
7 6
 use App\Filament\Company\Resources\Sales\ClientResource;
8 7
 use App\Models\Common\Client;
9 8
 use Filament\Resources\Pages\CreateRecord;
@@ -23,83 +22,6 @@ class CreateClient extends CreateRecord
23 22
 
24 23
     protected function handleRecordCreation(array $data): Model
25 24
     {
26
-        /** @var Client $record */
27
-        $record = parent::handleRecordCreation($data);
28
-
29
-        if (isset($data['primaryContact'], $data['primaryContact']['first_name'])) {
30
-            $record->primaryContact()->create([
31
-                'is_primary' => true,
32
-                'first_name' => $data['primaryContact']['first_name'],
33
-                'last_name' => $data['primaryContact']['last_name'],
34
-                'email' => $data['primaryContact']['email'],
35
-                'phones' => $data['primaryContact']['phones'] ?? [],
36
-            ]);
37
-        }
38
-
39
-        if (isset($data['secondaryContacts'])) {
40
-            foreach ($data['secondaryContacts'] as $contactData) {
41
-                if (isset($contactData['first_name'])) {
42
-                    $record->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
-
53
-        if (isset($data['billingAddress'], $data['billingAddress']['address_line_1'])) {
54
-            $record->billingAddress()->create([
55
-                'type' => AddressType::Billing,
56
-                'address_line_1' => $data['billingAddress']['address_line_1'],
57
-                'address_line_2' => $data['billingAddress']['address_line_2'] ?? null,
58
-                'country_code' => $data['billingAddress']['country_code'] ?? null,
59
-                'state_id' => $data['billingAddress']['state_id'] ?? null,
60
-                'city' => $data['billingAddress']['city'] ?? null,
61
-                'postal_code' => $data['billingAddress']['postal_code'] ?? null,
62
-            ]);
63
-        }
64
-
65
-        if (isset($data['shippingAddress'])) {
66
-            $shippingData = $data['shippingAddress'];
67
-            $shippingAddress = [
68
-                'type' => AddressType::Shipping,
69
-                'recipient' => $shippingData['recipient'] ?? null,
70
-                'phone' => $shippingData['phone'] ?? null,
71
-                'notes' => $shippingData['notes'] ?? null,
72
-            ];
73
-
74
-            if ($shippingData['same_as_billing'] ?? false) {
75
-                $billingAddress = $record->billingAddress;
76
-                if ($billingAddress) {
77
-                    $shippingAddress = [
78
-                        ...$shippingAddress,
79
-                        'parent_address_id' => $billingAddress->id,
80
-                        'address_line_1' => $billingAddress->address_line_1,
81
-                        'address_line_2' => $billingAddress->address_line_2,
82
-                        'country_code' => $billingAddress->country_code,
83
-                        'state_id' => $billingAddress->state_id,
84
-                        'city' => $billingAddress->city,
85
-                        'postal_code' => $billingAddress->postal_code,
86
-                    ];
87
-                    $record->shippingAddress()->create($shippingAddress);
88
-                }
89
-            } elseif (isset($shippingData['address_line_1'])) {
90
-                $shippingAddress = [
91
-                    ...$shippingAddress,
92
-                    'address_line_1' => $shippingData['address_line_1'],
93
-                    'address_line_2' => $shippingData['address_line_2'] ?? null,
94
-                    'country_code' => $shippingData['country_code'] ?? null,
95
-                    'state_id' => $shippingData['state_id'] ?? null,
96
-                    'city' => $shippingData['city'] ?? null,
97
-                    'postal_code' => $shippingData['postal_code'] ?? null,
98
-                ];
99
-                $record->shippingAddress()->create($shippingAddress);
100
-            }
101
-        }
102
-
103
-        return $record;
25
+        return Client::createWithRelations($data);
104 26
     }
105 27
 }

+ 1
- 92
app/Filament/Company/Resources/Sales/ClientResource/Pages/EditClient.php Bestand weergeven

@@ -3,7 +3,6 @@
3 3
 namespace App\Filament\Company\Resources\Sales\ClientResource\Pages;
4 4
 
5 5
 use App\Concerns\HandlePageRedirect;
6
-use App\Enums\Common\AddressType;
7 6
 use App\Filament\Company\Resources\Sales\ClientResource;
8 7
 use App\Models\Common\Client;
9 8
 use Filament\Actions;
@@ -32,97 +31,7 @@ class EditClient extends EditRecord
32 31
     protected function handleRecordUpdate(Model $record, array $data): Model
33 32
     {
34 33
         /** @var Client $record */
35
-        $record = parent::handleRecordUpdate($record, $data);
36
-
37
-        if (isset($data['primaryContact'], $data['primaryContact']['first_name'])) {
38
-            $record->primaryContact()->updateOrCreate(
39
-                ['is_primary' => true],
40
-                [
41
-                    'first_name' => $data['primaryContact']['first_name'],
42
-                    'last_name' => $data['primaryContact']['last_name'],
43
-                    'email' => $data['primaryContact']['email'],
44
-                    'phones' => $data['primaryContact']['phones'] ?? [],
45
-                ]
46
-            );
47
-        }
48
-
49
-        if (isset($data['secondaryContacts'])) {
50
-            // Delete removed contacts
51
-            $existingIds = collect($data['secondaryContacts'])->pluck('id')->filter()->all();
52
-            $record->secondaryContacts()->whereNotIn('id', $existingIds)->delete();
53
-
54
-            // Update or create contacts
55
-            foreach ($data['secondaryContacts'] as $contactData) {
56
-                if (isset($contactData['first_name'])) {
57
-                    $record->secondaryContacts()->updateOrCreate(
58
-                        ['id' => $contactData['id'] ?? null],
59
-                        [
60
-                            'is_primary' => false,
61
-                            'first_name' => $contactData['first_name'],
62
-                            'last_name' => $contactData['last_name'],
63
-                            'email' => $contactData['email'],
64
-                            'phones' => $contactData['phones'] ?? [],
65
-                        ]
66
-                    );
67
-                }
68
-            }
69
-        }
70
-
71
-        if (isset($data['billingAddress'], $data['billingAddress']['address_line_1'])) {
72
-            $record->billingAddress()->updateOrCreate(
73
-                ['type' => AddressType::Billing],
74
-                [
75
-                    'address_line_1' => $data['billingAddress']['address_line_1'],
76
-                    'address_line_2' => $data['billingAddress']['address_line_2'] ?? null,
77
-                    'country_code' => $data['billingAddress']['country_code'] ?? null,
78
-                    'state_id' => $data['billingAddress']['state_id'] ?? null,
79
-                    'city' => $data['billingAddress']['city'] ?? null,
80
-                    'postal_code' => $data['billingAddress']['postal_code'] ?? null,
81
-                ]
82
-            );
83
-        }
84
-
85
-        if (isset($data['shippingAddress'])) {
86
-            $shippingData = $data['shippingAddress'];
87
-            $shippingAddress = [
88
-                'type' => AddressType::Shipping,
89
-                'recipient' => $shippingData['recipient'] ?? null,
90
-                'phone' => $shippingData['phone'] ?? null,
91
-                'notes' => $shippingData['notes'] ?? null,
92
-            ];
93
-
94
-            if ($shippingData['same_as_billing'] ?? false) {
95
-                $billingAddress = $record->billingAddress;
96
-                if ($billingAddress) {
97
-                    $shippingAddress = [
98
-                        ...$shippingAddress,
99
-                        'parent_address_id' => $billingAddress->id,
100
-                        'address_line_1' => $billingAddress->address_line_1,
101
-                        'address_line_2' => $billingAddress->address_line_2,
102
-                        'country_code' => $billingAddress->country_code,
103
-                        'state_id' => $billingAddress->state_id,
104
-                        'city' => $billingAddress->city,
105
-                        'postal_code' => $billingAddress->postal_code,
106
-                    ];
107
-                }
108
-            } elseif (isset($shippingData['address_line_1'])) {
109
-                $shippingAddress = [
110
-                    ...$shippingAddress,
111
-                    'parent_address_id' => null,
112
-                    'address_line_1' => $shippingData['address_line_1'],
113
-                    'address_line_2' => $shippingData['address_line_2'] ?? null,
114
-                    'country_code' => $shippingData['country_code'] ?? null,
115
-                    'state_id' => $shippingData['state_id'] ?? null,
116
-                    'city' => $shippingData['city'] ?? null,
117
-                    'postal_code' => $shippingData['postal_code'] ?? null,
118
-                ];
119
-            }
120
-
121
-            $record->shippingAddress()->updateOrCreate(
122
-                ['type' => AddressType::Shipping],
123
-                $shippingAddress
124
-            );
125
-        }
34
+        $record->updateWithRelations($data);
126 35
 
127 36
         return $record;
128 37
     }

+ 2
- 2
app/Filament/Forms/Components/CreateAdjustmentSelect.php Bestand weergeven

@@ -211,12 +211,12 @@ class CreateAdjustmentSelect extends Select
211 211
     {
212 212
         $categoryLabel = $this->getCategory()?->getLabel() ?? 'Adjustment';
213 213
         $typeLabel = $this->getType()?->getLabel() ?? '';
214
-        $label = trim($typeLabel . ' ' . $categoryLabel);
214
+        $label = strtolower(trim($typeLabel . ' ' . $categoryLabel));
215 215
 
216 216
         return $action
217 217
             ->label('Create ' . $label)
218 218
             ->slideOver()
219 219
             ->modalWidth(MaxWidth::ExtraLarge)
220
-            ->modalHeading('Create a new ' . strtolower($label));
220
+            ->modalHeading('Create a new ' . $label);
221 221
     }
222 222
 }

+ 4
- 83
app/Filament/Forms/Components/CreateClientSelect.php Bestand weergeven

@@ -2,7 +2,6 @@
2 2
 
3 3
 namespace App\Filament\Forms\Components;
4 4
 
5
-use App\Enums\Common\AddressType;
6 5
 use App\Filament\Company\Resources\Sales\ClientResource;
7 6
 use App\Models\Common\Client;
8 7
 use Filament\Forms\Components\Actions\Action;
@@ -27,86 +26,7 @@ class CreateClientSelect extends Select
27 26
 
28 27
         $this->createOptionUsing(static function (array $data) {
29 28
             return DB::transaction(static function () use ($data) {
30
-                /** @var Client $client */
31
-                $client = Client::create([
32
-                    'name' => $data['name'],
33
-                    'website' => $data['website'] ?? null,
34
-                    'notes' => $data['notes'] ?? null,
35
-                ]);
36
-
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
-                }
46
-
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
-                }
60
-
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
-                }
72
-
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
-                }
29
+                $client = Client::createWithRelations($data);
110 30
 
111 31
                 return $client->getKey();
112 32
             });
@@ -121,8 +41,9 @@ class CreateClientSelect extends Select
121 41
     protected function createClientAction(Action $action): Action
122 42
     {
123 43
         return $action
124
-            ->label('Add client')
44
+            ->label('Create client')
125 45
             ->slideOver()
126
-            ->modalWidth(MaxWidth::ThreeExtraLarge);
46
+            ->modalWidth(MaxWidth::ThreeExtraLarge)
47
+            ->modalHeading('Create a new client');
127 48
     }
128 49
 }

+ 3
- 2
app/Filament/Forms/Components/CreateCurrencySelect.php Bestand weergeven

@@ -63,8 +63,9 @@ class CreateCurrencySelect extends Select
63 63
     protected function createCurrencyAction(Action $action): Action
64 64
     {
65 65
         return $action
66
-            ->label('Add currency')
66
+            ->label('Create currency')
67 67
             ->slideOver()
68
-            ->modalWidth(MaxWidth::Medium);
68
+            ->modalWidth(MaxWidth::Medium)
69
+            ->modalHeading('Create a new currency');
69 70
     }
70 71
 }

+ 12
- 8
app/Filament/Forms/Components/CreateOfferingSelect.php Bestand weergeven

@@ -43,15 +43,16 @@ class CreateOfferingSelect extends Select
43 43
             ->createOptionAction(fn (Action $action) => $this->createOfferingAction($action));
44 44
 
45 45
         $this->relationship(
46
-            $this->isPurchasable() && ! $this->isSellable() ? 'purchasableOffering' :
47
-            ($this->isSellable() && ! $this->isPurchasable() ? 'sellableOffering' : 'offering'),
48
-            'name'
46
+            name: fn () => $this->isPurchasable() && ! $this->isSellable() ? 'purchasableOffering' : ($this->isSellable() && ! $this->isPurchasable() ? 'sellableOffering' : 'offering'),
47
+            titleAttribute: 'name'
49 48
         );
50 49
 
51
-        $this->createOptionUsing(function (array $data) {
50
+        $this->createOptionUsing(function (array $data, Form $form) {
52 51
             if ($this->isSellableAndPurchasable()) {
53
-                $data['sellable'] = isset($data['attributes']) && in_array('Sellable', $data['attributes'], true);
54
-                $data['purchasable'] = isset($data['attributes']) && in_array('Purchasable', $data['attributes'], true);
52
+                $attributes = array_flip($data['attributes'] ?? []);
53
+
54
+                $data['sellable'] = isset($attributes['Sellable']);
55
+                $data['purchasable'] = isset($attributes['Purchasable']);
55 56
             } else {
56 57
                 $data['sellable'] = $this->isSellable;
57 58
                 $data['purchasable'] = $this->isPurchasable;
@@ -61,6 +62,8 @@ class CreateOfferingSelect extends Select
61 62
 
62 63
             $offering = Offering::create($data);
63 64
 
65
+            $form->model($offering)->saveRelationships();
66
+
64 67
             return $offering->getKey();
65 68
         });
66 69
     }
@@ -85,9 +88,10 @@ class CreateOfferingSelect extends Select
85 88
     protected function createOfferingAction(Action $action): Action
86 89
     {
87 90
         return $action
88
-            ->label('Add offering')
91
+            ->label('Create offering')
89 92
             ->slideOver()
90
-            ->modalWidth(MaxWidth::ThreeExtraLarge);
93
+            ->modalWidth(MaxWidth::ThreeExtraLarge)
94
+            ->modalHeading('Create a new offering');
91 95
     }
92 96
 
93 97
     public function isSellable(): bool

+ 4
- 36
app/Filament/Forms/Components/CreateVendorSelect.php Bestand weergeven

@@ -26,40 +26,7 @@ class CreateVendorSelect extends Select
26 26
 
27 27
         $this->createOptionUsing(static function (array $data) {
28 28
             return DB::transaction(static function () use ($data) {
29
-                /** @var Vendor $vendor */
30
-                $vendor = Vendor::create([
31
-                    'name' => $data['name'],
32
-                    'type' => $data['type'],
33
-                    'currency_code' => $data['currency_code'] ?? null,
34
-                    'contractor_type' => $data['contractor_type'] ?? null,
35
-                    'ssn' => $data['ssn'] ?? null,
36
-                    'ein' => $data['ein'] ?? null,
37
-                    'account_number' => $data['account_number'] ?? null,
38
-                    'website' => $data['website'] ?? null,
39
-                    'notes' => $data['notes'] ?? null,
40
-                ]);
41
-
42
-                if (isset($data['contact'], $data['contact']['first_name'])) {
43
-                    $vendor->contact()->create([
44
-                        'is_primary' => true,
45
-                        'first_name' => $data['contact']['first_name'],
46
-                        'last_name' => $data['contact']['last_name'],
47
-                        'email' => $data['contact']['email'],
48
-                        'phones' => $data['contact']['phones'] ?? [],
49
-                    ]);
50
-                }
51
-
52
-                if (isset($data['address'], $data['address']['type'], $data['address']['address_line_1'])) {
53
-                    $vendor->address()->create([
54
-                        'type' => $data['address']['type'],
55
-                        'address_line_1' => $data['address']['address_line_1'],
56
-                        'address_line_2' => $data['address']['address_line_2'] ?? null,
57
-                        'country_code' => $data['address']['country_code'] ?? null,
58
-                        'state_id' => $data['address']['state_id'] ?? null,
59
-                        'city' => $data['address']['city'] ?? null,
60
-                        'postal_code' => $data['address']['postal_code'] ?? null,
61
-                    ]);
62
-                }
29
+                $vendor = Vendor::createWithRelations($data);
63 30
 
64 31
                 return $vendor->getKey();
65 32
             });
@@ -74,8 +41,9 @@ class CreateVendorSelect extends Select
74 41
     protected function createVendorAction(Action $action): Action
75 42
     {
76 43
         return $action
77
-            ->label('Add vendor')
44
+            ->label('Create vendor')
78 45
             ->slideOver()
79
-            ->modalWidth(MaxWidth::ThreeExtraLarge);
46
+            ->modalWidth(MaxWidth::ThreeExtraLarge)
47
+            ->modalHeading('Create a new vendor');
80 48
     }
81 49
 }

+ 180
- 0
app/Models/Common/Client.php Bestand weergeven

@@ -35,6 +35,186 @@ class Client extends Model
35 35
         'updated_by',
36 36
     ];
37 37
 
38
+    public static function createWithRelations(array $data): self
39
+    {
40
+        /** @var Client $client */
41
+        $client = self::create($data);
42
+
43
+        if (isset($data['primaryContact'], $data['primaryContact']['first_name'])) {
44
+            $client->primaryContact()->create([
45
+                'is_primary' => true,
46
+                'first_name' => $data['primaryContact']['first_name'],
47
+                'last_name' => $data['primaryContact']['last_name'],
48
+                'email' => $data['primaryContact']['email'],
49
+                'phones' => $data['primaryContact']['phones'] ?? [],
50
+            ]);
51
+        }
52
+
53
+        if (isset($data['secondaryContacts'])) {
54
+            foreach ($data['secondaryContacts'] as $contactData) {
55
+                if (isset($contactData['first_name'])) {
56
+                    $client->secondaryContacts()->create([
57
+                        'is_primary' => false,
58
+                        'first_name' => $contactData['first_name'],
59
+                        'last_name' => $contactData['last_name'],
60
+                        'email' => $contactData['email'],
61
+                        'phones' => $contactData['phones'] ?? [],
62
+                    ]);
63
+                }
64
+            }
65
+        }
66
+
67
+        if (isset($data['billingAddress'], $data['billingAddress']['address_line_1'])) {
68
+            $client->billingAddress()->create([
69
+                'type' => AddressType::Billing,
70
+                'address_line_1' => $data['billingAddress']['address_line_1'],
71
+                'address_line_2' => $data['billingAddress']['address_line_2'] ?? null,
72
+                'country_code' => $data['billingAddress']['country_code'] ?? null,
73
+                'state_id' => $data['billingAddress']['state_id'] ?? null,
74
+                'city' => $data['billingAddress']['city'] ?? null,
75
+                'postal_code' => $data['billingAddress']['postal_code'] ?? null,
76
+            ]);
77
+        }
78
+
79
+        if (isset($data['shippingAddress'])) {
80
+            $shippingData = $data['shippingAddress'];
81
+            $shippingAddress = [
82
+                'type' => AddressType::Shipping,
83
+                'recipient' => $shippingData['recipient'] ?? null,
84
+                'phone' => $shippingData['phone'] ?? null,
85
+                'notes' => $shippingData['notes'] ?? null,
86
+            ];
87
+
88
+            if ($shippingData['same_as_billing'] ?? false) {
89
+                $billingAddress = $client->billingAddress;
90
+                if ($billingAddress) {
91
+                    $shippingAddress = [
92
+                        ...$shippingAddress,
93
+                        'parent_address_id' => $billingAddress->id,
94
+                        'address_line_1' => $billingAddress->address_line_1,
95
+                        'address_line_2' => $billingAddress->address_line_2,
96
+                        'country_code' => $billingAddress->country_code,
97
+                        'state_id' => $billingAddress->state_id,
98
+                        'city' => $billingAddress->city,
99
+                        'postal_code' => $billingAddress->postal_code,
100
+                    ];
101
+                    $client->shippingAddress()->create($shippingAddress);
102
+                }
103
+            } elseif (isset($shippingData['address_line_1'])) {
104
+                $shippingAddress = [
105
+                    ...$shippingAddress,
106
+                    'address_line_1' => $shippingData['address_line_1'],
107
+                    'address_line_2' => $shippingData['address_line_2'] ?? null,
108
+                    'country_code' => $shippingData['country_code'] ?? null,
109
+                    'state_id' => $shippingData['state_id'] ?? null,
110
+                    'city' => $shippingData['city'] ?? null,
111
+                    'postal_code' => $shippingData['postal_code'] ?? null,
112
+                ];
113
+
114
+                $client->shippingAddress()->create($shippingAddress);
115
+            }
116
+        }
117
+
118
+        return $client;
119
+    }
120
+
121
+    public function updateWithRelations(array $data): self
122
+    {
123
+        $this->update($data);
124
+
125
+        if (isset($data['primaryContact'], $data['primaryContact']['first_name'])) {
126
+            $this->primaryContact()->updateOrCreate(
127
+                ['is_primary' => true],
128
+                [
129
+                    'first_name' => $data['primaryContact']['first_name'],
130
+                    'last_name' => $data['primaryContact']['last_name'],
131
+                    'email' => $data['primaryContact']['email'],
132
+                    'phones' => $data['primaryContact']['phones'] ?? [],
133
+                ]
134
+            );
135
+        }
136
+
137
+        if (isset($data['secondaryContacts'])) {
138
+            // Delete removed contacts
139
+            $existingIds = collect($data['secondaryContacts'])->pluck('id')->filter()->all();
140
+            $this->secondaryContacts()->whereNotIn('id', $existingIds)->delete();
141
+
142
+            // Update or create contacts
143
+            foreach ($data['secondaryContacts'] as $contactData) {
144
+                if (isset($contactData['first_name'])) {
145
+                    $this->secondaryContacts()->updateOrCreate(
146
+                        ['id' => $contactData['id'] ?? null],
147
+                        [
148
+                            'is_primary' => false,
149
+                            'first_name' => $contactData['first_name'],
150
+                            'last_name' => $contactData['last_name'],
151
+                            'email' => $contactData['email'],
152
+                            'phones' => $contactData['phones'] ?? [],
153
+                        ]
154
+                    );
155
+                }
156
+            }
157
+        }
158
+
159
+        if (isset($data['billingAddress'], $data['billingAddress']['address_line_1'])) {
160
+            $this->billingAddress()->updateOrCreate(
161
+                ['type' => AddressType::Billing],
162
+                [
163
+                    'address_line_1' => $data['billingAddress']['address_line_1'],
164
+                    'address_line_2' => $data['billingAddress']['address_line_2'] ?? null,
165
+                    'country_code' => $data['billingAddress']['country_code'] ?? null,
166
+                    'state_id' => $data['billingAddress']['state_id'] ?? null,
167
+                    'city' => $data['billingAddress']['city'] ?? null,
168
+                    'postal_code' => $data['billingAddress']['postal_code'] ?? null,
169
+                ]
170
+            );
171
+        }
172
+
173
+        if (isset($data['shippingAddress'])) {
174
+            $shippingData = $data['shippingAddress'];
175
+            $shippingAddress = [
176
+                'type' => AddressType::Shipping,
177
+                'recipient' => $shippingData['recipient'] ?? null,
178
+                'phone' => $shippingData['phone'] ?? null,
179
+                'notes' => $shippingData['notes'] ?? null,
180
+            ];
181
+
182
+            if ($shippingData['same_as_billing'] ?? false) {
183
+                $billingAddress = $this->billingAddress;
184
+                if ($billingAddress) {
185
+                    $shippingAddress = [
186
+                        ...$shippingAddress,
187
+                        'parent_address_id' => $billingAddress->id,
188
+                        'address_line_1' => $billingAddress->address_line_1,
189
+                        'address_line_2' => $billingAddress->address_line_2,
190
+                        'country_code' => $billingAddress->country_code,
191
+                        'state_id' => $billingAddress->state_id,
192
+                        'city' => $billingAddress->city,
193
+                        'postal_code' => $billingAddress->postal_code,
194
+                    ];
195
+                }
196
+            } elseif (isset($shippingData['address_line_1'])) {
197
+                $shippingAddress = [
198
+                    ...$shippingAddress,
199
+                    'parent_address_id' => null,
200
+                    'address_line_1' => $shippingData['address_line_1'],
201
+                    'address_line_2' => $shippingData['address_line_2'] ?? null,
202
+                    'country_code' => $shippingData['country_code'] ?? null,
203
+                    'state_id' => $shippingData['state_id'] ?? null,
204
+                    'city' => $shippingData['city'] ?? null,
205
+                    'postal_code' => $shippingData['postal_code'] ?? null,
206
+                ];
207
+            }
208
+
209
+            $this->shippingAddress()->updateOrCreate(
210
+                ['type' => AddressType::Shipping],
211
+                $shippingAddress
212
+            );
213
+        }
214
+
215
+        return $this;
216
+    }
217
+
38 218
     public function contacts(): MorphMany
39 219
     {
40 220
         return $this->morphMany(Contact::class, 'contactable');

+ 63
- 0
app/Models/Common/Vendor.php Bestand weergeven

@@ -44,6 +44,69 @@ class Vendor extends Model
44 44
         'ein' => 'encrypted',
45 45
     ];
46 46
 
47
+    public static function createWithRelations(array $data): self
48
+    {
49
+        /** @var Vendor $vendor */
50
+        $vendor = self::create($data);
51
+
52
+        if (isset($data['contact'], $data['contact']['first_name'])) {
53
+            $vendor->contact()->create([
54
+                'is_primary' => true,
55
+                'first_name' => $data['contact']['first_name'],
56
+                'last_name' => $data['contact']['last_name'],
57
+                'email' => $data['contact']['email'],
58
+                'phones' => $data['contact']['phones'] ?? [],
59
+            ]);
60
+        }
61
+
62
+        if (isset($data['address'], $data['address']['type'], $data['address']['address_line_1'])) {
63
+            $vendor->address()->create([
64
+                'type' => $data['address']['type'],
65
+                'address_line_1' => $data['address']['address_line_1'],
66
+                'address_line_2' => $data['address']['address_line_2'] ?? null,
67
+                'country_code' => $data['address']['country_code'] ?? null,
68
+                'state_id' => $data['address']['state_id'] ?? null,
69
+                'city' => $data['address']['city'] ?? null,
70
+                'postal_code' => $data['address']['postal_code'] ?? null,
71
+            ]);
72
+        }
73
+
74
+        return $vendor;
75
+    }
76
+
77
+    public function updateWithRelations(array $data): self
78
+    {
79
+        $this->update($data);
80
+
81
+        if (isset($data['contact'], $data['contact']['first_name'])) {
82
+            $this->contact()->updateOrCreate(
83
+                ['is_primary' => true],
84
+                [
85
+                    'first_name' => $data['contact']['first_name'],
86
+                    'last_name' => $data['contact']['last_name'],
87
+                    'email' => $data['contact']['email'],
88
+                    'phones' => $data['contact']['phones'] ?? [],
89
+                ]
90
+            );
91
+        }
92
+
93
+        if (isset($data['address'], $data['address']['type'], $data['address']['address_line_1'])) {
94
+            $this->address()->updateOrCreate(
95
+                ['type' => $data['address']['type']],
96
+                [
97
+                    'address_line_1' => $data['address']['address_line_1'],
98
+                    'address_line_2' => $data['address']['address_line_2'] ?? null,
99
+                    'country_code' => $data['address']['country_code'] ?? null,
100
+                    'state_id' => $data['address']['state_id'] ?? null,
101
+                    'city' => $data['address']['city'] ?? null,
102
+                    'postal_code' => $data['address']['postal_code'] ?? null,
103
+                ]
104
+            );
105
+        }
106
+
107
+        return $this;
108
+    }
109
+
47 110
     public function bills(): HasMany
48 111
     {
49 112
         return $this->hasMany(Bill::class);

+ 172
- 172
composer.lock
Diff onderdrukt omdat het te groot bestand
Bestand weergeven


Laden…
Annuleren
Opslaan