|
18 | 18 | use Illuminate\Validation\Rules\Unique; |
19 | 19 | use Lunar\Admin\Events\ProductPricingUpdated; |
20 | 20 | use Lunar\Admin\Support\RelationManagers\BaseRelationManager; |
| 21 | +use Lunar\DataTypes\Price as PriceDataType; |
21 | 22 | use Lunar\Facades\DB; |
22 | 23 | use Lunar\Models\Currency; |
23 | 24 | use Lunar\Models\CustomerGroup; |
@@ -76,14 +77,10 @@ public function getDefaultForm(Schema $schema): Schema |
76 | 77 | ])->columns(2), |
77 | 78 |
|
78 | 79 | Group::make([ |
79 | | - TextInput::make('price')->formatStateUsing( |
80 | | - fn ($state) => $state?->decimal(rounding: false) |
81 | | - )->numeric()->helperText( |
| 80 | + TextInput::make('price')->numeric()->helperText( |
82 | 81 | __('lunarpanel::relationmanagers.pricing.form.price.helper_text') |
83 | 82 | )->required(), |
84 | | - TextInput::make('compare_price')->formatStateUsing( |
85 | | - fn ($state) => $state?->decimal(rounding: false) |
86 | | - )->label( |
| 83 | + TextInput::make('compare_price')->label( |
87 | 84 | __('lunarpanel::relationmanagers.pricing.form.compare_price.label') |
88 | 85 | )->helperText( |
89 | 86 | __('lunarpanel::relationmanagers.pricing.form.compare_price.helper_text') |
@@ -152,18 +149,31 @@ public function getDefaultTable(Table $table): Table |
152 | 149 | ), |
153 | 150 | ]) |
154 | 151 | ->recordActions([ |
155 | | - EditAction::make()->mutateDataUsing(function (array $data): array { |
156 | | - $currencyModel = Currency::find($data['currency_id']); |
| 152 | + EditAction::make() |
| 153 | + ->mutateRecordDataUsing(fn (array $data): array => $this->unwrapPriceData($data)) |
| 154 | + ->mutateDataUsing(function (array $data): array { |
| 155 | + $currencyModel = Currency::find($data['currency_id']); |
157 | 156 |
|
158 | | - $data['min_quantity'] = 1; |
159 | | - $data['price'] = (int) ($data['price'] * $currencyModel->factor); |
160 | | - $data['compare_price'] = (int) ($data['compare_price'] * $currencyModel->factor); |
| 157 | + $data['min_quantity'] = 1; |
| 158 | + $data['price'] = (int) ($data['price'] * $currencyModel->factor); |
| 159 | + $data['compare_price'] = (int) ($data['compare_price'] * $currencyModel->factor); |
161 | 160 |
|
162 | | - return $data; |
163 | | - })->after( |
164 | | - fn () => ProductPricingUpdated::dispatch($this->getOwnerRecord()) |
165 | | - ), |
| 161 | + return $data; |
| 162 | + })->after( |
| 163 | + fn () => ProductPricingUpdated::dispatch($this->getOwnerRecord()) |
| 164 | + ), |
166 | 165 | DeleteAction::make(), |
167 | 166 | ]); |
168 | 167 | } |
| 168 | + |
| 169 | + protected function unwrapPriceData(array $data): array |
| 170 | + { |
| 171 | + foreach (['price', 'compare_price'] as $key) { |
| 172 | + if (($data[$key] ?? null) instanceof PriceDataType) { |
| 173 | + $data[$key] = $data[$key]->decimal(rounding: false); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return $data; |
| 178 | + } |
169 | 179 | } |
0 commit comments