Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Feb 14, 2024
1 parent 50fb554 commit d3126d7
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 19 deletions.
8 changes: 6 additions & 2 deletions config/bazar.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
'currencies' => [
'default' => strtolower(env('BAZAR_CURRENCY', 'USD')),
'available' => [
'USD',
'EUR',
'USD' => [
'precision' => 2,
],
'EUR' => [
'precision' => 2,
],
],
],

Expand Down
2 changes: 1 addition & 1 deletion src/Bazar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', []));
}

/**
Expand Down
114 changes: 114 additions & 0 deletions src/Fields/VariantProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace Cone\Bazar\Fields;

use Closure;
use Cone\Root\Fields\MorphToMany;
use Cone\Root\Fields\Select;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class VariantProperties extends MorphToMany
{
/**
* The Blade template.
*/
protected string $template = 'root::fields.fieldset';

/**
* The relations to eager load on every query.
*/
protected array $with = [
'property',
];

/**
* Create a new relation field instance.
*/
public function __construct(?string $label = null, Closure|string|null $modelAttribute = null, Closure|string|null $relation = null)
{
parent::__construct($label, $modelAttribute, $relation);

$this->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),
]);
}
}
16 changes: 1 addition & 15 deletions src/Fields/Variants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'),
];
Expand Down
3 changes: 2 additions & 1 deletion src/Support/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit d3126d7

Please sign in to comment.