From d3126d768ed37d03ef340085c9f9809dd48baac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Wed, 14 Feb 2024 11:35:14 +0100 Subject: [PATCH] wip --- config/bazar.php | 8 ++- src/Bazar.php | 2 +- src/Fields/VariantProperties.php | 114 +++++++++++++++++++++++++++++++ src/Fields/Variants.php | 16 +---- src/Support/Currency.php | 3 +- 5 files changed, 124 insertions(+), 19 deletions(-) create mode 100644 src/Fields/VariantProperties.php diff --git a/config/bazar.php b/config/bazar.php index 1104e546..fd4186c3 100644 --- a/config/bazar.php +++ b/config/bazar.php @@ -16,8 +16,12 @@ 'currencies' => [ 'default' => strtolower(env('BAZAR_CURRENCY', 'USD')), 'available' => [ - 'USD', - 'EUR', + 'USD' => [ + 'precision' => 2, + ], + 'EUR' => [ + 'precision' => 2, + ], ], ], diff --git a/src/Bazar.php b/src/Bazar.php index 121aa6cb..b1db8df6 100644 --- a/src/Bazar.php +++ b/src/Bazar.php @@ -24,7 +24,7 @@ abstract class Bazar */ public static function getCurrencies(): array { - return Config::get('bazar.currencies.available', []); + return array_keys(Config::get('bazar.currencies.available', [])); } /** diff --git a/src/Fields/VariantProperties.php b/src/Fields/VariantProperties.php new file mode 100644 index 00000000..26fac99d --- /dev/null +++ b/src/Fields/VariantProperties.php @@ -0,0 +1,114 @@ +display('name'); + } + + /** + * {@inheritdoc} + */ + public function fields(Request $request): array + { + return []; + } + + /** + * {@inheritdoc} + */ + public function resolveOptions(Request $request, Model $model): array + { + return []; + } + + /** + * {@inheritdoc} + */ + public function getValueForHydrate(Request $request): mixed + { + $value = (array) $request->input($this->getRequestKey()); + + $value = array_filter($value, static function (mixed $value): bool { + return ! is_null($value); + }); + + return $this->mergePivotValues(array_values($value)); + } + + /** + * Resolve the property fields. + */ + public function resolvePropertyFields(Request $request, Model $model): array + { + $values = $this->resolveRelatableQuery($request, $model)->get(); + + $value = $this->resolveValue($request, $model); + + return $values->groupBy('property_id') + ->map(function (Collection $group) use ($request, $model, $value): array { + return Select::make($group->first()->property->name, $this->modelAttribute.'.'.$group->first()->property->slug) + ->value(static function () use ($value, $group): ?int { + return $value->firstWhere('property_id', $group->first()->property_id)?->getKey(); + }) + ->options($group->pluck('name', 'id')->toArray()) + ->nullable() + ->toInput($request, $model); + })->toArray(); + } + + /** + * {@inheritdoc} + */ + public function resolveRelatableQuery(Request $request, Model $model): Builder + { + $query = parent::resolveRelatableQuery($request, $model); + + $product = $model->relationLoaded('product') + ? $model->product + : $model->product()->make()->forceFill(['id' => $model->product_id]); + + return $query->whereIn( + $query->qualifyColumn('id'), + $product->propertyValues()->select('bazar_property_values.id') + ); + } + + /** + * {@inheritdoc} + */ + public function toInput(Request $request, Model $model): array + { + return array_merge(parent::toInput($request, $model), [ + 'fields' => $this->resolvePropertyFields($request, $model), + ]); + } +} diff --git a/src/Fields/Variants.php b/src/Fields/Variants.php index dbd4edc1..96e3ccd3 100644 --- a/src/Fields/Variants.php +++ b/src/Fields/Variants.php @@ -6,7 +6,6 @@ use Cone\Bazar\Bazar; use Cone\Bazar\Fields\Price; use Cone\Bazar\Models\Variant; -use Cone\Root\Fields\BelongsToMany; use Cone\Root\Fields\Editor; use Cone\Root\Fields\HasMany; use Cone\Root\Fields\ID; @@ -48,20 +47,7 @@ public function fields(Request $request): array Price::make(__('Price'), Bazar::getCurrency()), - BelongsToMany::make(__('Property Values'), 'propertyValues') - ->withRelatableQuery(static function (Request $request, Builder $query, Variant $model): Builder { - $product = $model->relationLoaded('product') - ? $model->product - : $model->product()->make()->forceFill(['id' => $model->product_id]); - - return $query->whereIn( - $query->qualifyColumn('id'), - $product->propertyValues()->select('bazar_property_values.id') - ); - }) - ->with(['property']) - ->display('name') - ->groupOptionsBy('property.name'), + VariantProperties::make(__('Properties'), 'propertyValues'), Editor::make(__('Description'), 'description'), ]; diff --git a/src/Support/Currency.php b/src/Support/Currency.php index 51097c83..c0bac096 100644 --- a/src/Support/Currency.php +++ b/src/Support/Currency.php @@ -5,6 +5,7 @@ use Closure; use Cone\Bazar\Bazar; use Illuminate\Support\Facades\App; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Traits\Macroable; use JsonSerializable; use NumberFormatter; @@ -46,7 +47,7 @@ public function __construct(int|float $value, ?string $currency = null, ?int $pr { $this->value = $value; $this->currency = $currency ?: Bazar::getCurrency(); - $this->precision = $precision; + $this->precision = $precision ?: Config::get('bazar.currencies.available.'.$currency.'.precision', 2); $this->locale = $locale ?: App::getLocale(); }