Andrew Wallo преди 5 месеца
родител
ревизия
0772702ded

+ 3
- 4
app/Filament/Company/Resources/Purchases/BillResource.php Целия файл

@@ -14,6 +14,7 @@ use App\Filament\Company\Resources\Purchases\BillResource\Pages;
14 14
 use App\Filament\Company\Resources\Purchases\VendorResource\RelationManagers\BillsRelationManager;
15 15
 use App\Filament\Forms\Components\CreateAdjustmentSelect;
16 16
 use App\Filament\Forms\Components\CreateCurrencySelect;
17
+use App\Filament\Forms\Components\CreateVendorSelect;
17 18
 use App\Filament\Forms\Components\DocumentTotals;
18 19
 use App\Filament\Tables\Actions\ReplicateBulkAction;
19 20
 use App\Filament\Tables\Columns;
@@ -60,10 +61,8 @@ class BillResource extends Resource
60 61
                     ->schema([
61 62
                         Forms\Components\Split::make([
62 63
                             Forms\Components\Group::make([
63
-                                Forms\Components\Select::make('vendor_id')
64
-                                    ->relationship('vendor', 'name')
65
-                                    ->preload()
66
-                                    ->searchable()
64
+                                CreateVendorSelect::make('vendor_id')
65
+                                    ->label('Vendor')
67 66
                                     ->required()
68 67
                                     ->live()
69 68
                                     ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {

+ 1
- 1
app/Filament/Company/Resources/Purchases/VendorResource.php Целия файл

@@ -45,7 +45,7 @@ class VendorResource extends Resource
45 45
                                     ->default(VendorType::Regular)
46 46
                                     ->columnSpanFull(),
47 47
                                 CreateCurrencySelect::make('currency_code')
48
-                                    ->nullable()
48
+                                    ->softRequired()
49 49
                                     ->visible(static fn (Forms\Get $get) => VendorType::parse($get('type')) === VendorType::Regular),
50 50
                                 Forms\Components\Select::make('contractor_type')
51 51
                                     ->label('Contractor type')

+ 1
- 2
app/Filament/Company/Resources/Sales/ClientResource.php Целия файл

@@ -171,8 +171,7 @@ class ClientResource extends Resource
171 171
                 Forms\Components\Section::make('Billing')
172 172
                     ->schema([
173 173
                         CreateCurrencySelect::make('currency_code')
174
-                            ->required(false)
175
-                            ->selectablePlaceholder(false),
174
+                            ->softRequired(),
176 175
                         CustomSection::make('Billing Address')
177 176
                             ->relationship('billingAddress')
178 177
                             ->saveRelationshipsUsing(null)

+ 3
- 4
app/Filament/Company/Resources/Sales/EstimateResource.php Целия файл

@@ -13,6 +13,7 @@ use App\Filament\Company\Resources\Sales\ClientResource\RelationManagers\Estimat
13 13
 use App\Filament\Company\Resources\Sales\EstimateResource\Pages;
14 14
 use App\Filament\Company\Resources\Sales\EstimateResource\Widgets;
15 15
 use App\Filament\Forms\Components\CreateAdjustmentSelect;
16
+use App\Filament\Forms\Components\CreateClientSelect;
16 17
 use App\Filament\Forms\Components\CreateCurrencySelect;
17 18
 use App\Filament\Forms\Components\DocumentFooterSection;
18 19
 use App\Filament\Forms\Components\DocumentHeaderSection;
@@ -61,10 +62,8 @@ class EstimateResource extends Resource
61 62
                     ->schema([
62 63
                         Forms\Components\Split::make([
63 64
                             Forms\Components\Group::make([
64
-                                Forms\Components\Select::make('client_id')
65
-                                    ->relationship('client', 'name')
66
-                                    ->preload()
67
-                                    ->searchable()
65
+                                CreateClientSelect::make('client_id')
66
+                                    ->label('Client')
68 67
                                     ->required()
69 68
                                     ->live()
70 69
                                     ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {

+ 3
- 4
app/Filament/Company/Resources/Sales/RecurringInvoiceResource.php Целия файл

@@ -12,6 +12,7 @@ use App\Enums\Setting\PaymentTerms;
12 12
 use App\Filament\Company\Resources\Sales\ClientResource\RelationManagers\RecurringInvoicesRelationManager;
13 13
 use App\Filament\Company\Resources\Sales\RecurringInvoiceResource\Pages;
14 14
 use App\Filament\Forms\Components\CreateAdjustmentSelect;
15
+use App\Filament\Forms\Components\CreateClientSelect;
15 16
 use App\Filament\Forms\Components\CreateCurrencySelect;
16 17
 use App\Filament\Forms\Components\DocumentFooterSection;
17 18
 use App\Filament\Forms\Components\DocumentHeaderSection;
@@ -53,10 +54,8 @@ class RecurringInvoiceResource extends Resource
53 54
                     ->schema([
54 55
                         Forms\Components\Split::make([
55 56
                             Forms\Components\Group::make([
56
-                                Forms\Components\Select::make('client_id')
57
-                                    ->relationship('client', 'name')
58
-                                    ->preload()
59
-                                    ->searchable()
57
+                                CreateClientSelect::make('client_id')
58
+                                    ->label('Client')
60 59
                                     ->required()
61 60
                                     ->live()
62 61
                                     ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {

+ 72
- 26
app/Filament/Forms/Components/CreateClientSelect.php Целия файл

@@ -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
             });

+ 81
- 0
app/Filament/Forms/Components/CreateVendorSelect.php Целия файл

@@ -0,0 +1,81 @@
1
+<?php
2
+
3
+namespace App\Filament\Forms\Components;
4
+
5
+use App\Filament\Company\Resources\Purchases\VendorResource;
6
+use App\Models\Common\Vendor;
7
+use Filament\Forms\Components\Actions\Action;
8
+use Filament\Forms\Components\Select;
9
+use Filament\Forms\Form;
10
+use Filament\Support\Enums\MaxWidth;
11
+use Illuminate\Support\Facades\DB;
12
+
13
+class CreateVendorSelect extends Select
14
+{
15
+    protected function setUp(): void
16
+    {
17
+        parent::setUp();
18
+
19
+        $this
20
+            ->searchable()
21
+            ->preload()
22
+            ->createOptionForm(fn (Form $form) => $this->createVendorForm($form))
23
+            ->createOptionAction(fn (Action $action) => $this->createVendorAction($action));
24
+
25
+        $this->relationship('vendor', 'name');
26
+
27
+        $this->createOptionUsing(static function (array $data) {
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
+                }
63
+
64
+                return $vendor->getKey();
65
+            });
66
+        });
67
+    }
68
+
69
+    protected function createVendorForm(Form $form): Form
70
+    {
71
+        return VendorResource::form($form);
72
+    }
73
+
74
+    protected function createVendorAction(Action $action): Action
75
+    {
76
+        return $action
77
+            ->label('Add vendor')
78
+            ->slideOver()
79
+            ->modalWidth(MaxWidth::ThreeExtraLarge);
80
+    }
81
+}

Loading…
Отказ
Запис