Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/shipping min quantity factor #1961

Open
wants to merge 3 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@
'currency_id' => [
'label' => 'Currency',
],
'min_quantity' => [
'min_spend' => [
'label' => 'Min. Spend',
],
'min_weight' => [
'label' => 'Min. Weight (KG)',
],
'price' => [
'label' => 'Price',
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Awcodes\Shout\Components\Shout;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Resources\Pages\ManageRelatedRecords;
use Filament\Support\Facades\FilamentIcon;
use Filament\Tables;
Expand All @@ -16,6 +17,8 @@
use Lunar\Models\CustomerGroup;
use Lunar\Models\Price;
use Lunar\Shipping\Filament\Resources\ShippingZoneResource;
use Lunar\Shipping\Models\Contracts\ShippingMethod as ShippingMethodContract;
use Lunar\Shipping\Models\ShippingMethod;
use Lunar\Shipping\Models\ShippingRate;

class ManageShippingRates extends ManageRelatedRecords
Expand Down Expand Up @@ -58,6 +61,7 @@ function () {
__('lunarpanel.shipping::relationmanagers.shipping_rates.form.shipping_method_id.label')
)
->required()
->live()
->relationship(name: 'shippingMethod', titleAttribute: 'name')
->columnSpan(2),
Forms\Components\TextInput::make('price')
Expand Down Expand Up @@ -98,28 +102,39 @@ function () {
)->default(
Currency::getDefault()->id
)->required()->preload(),
Forms\Components\TextInput::make('min_quantity')
Forms\Components\TextInput::make('price')
->label(
__('lunarpanel.shipping::relationmanagers.shipping_rates.form.prices.repeater.min_quantity.label')
__('lunarpanel.shipping::relationmanagers.shipping_rates.form.prices.repeater.price.label')
)
->numeric()
->required(),
Forms\Components\TextInput::make('price')
Forms\Components\TextInput::make('min_quantity')
->label(
__('lunarpanel.shipping::relationmanagers.shipping_rates.form.prices.repeater.price.label')
function (Get $get) {
if (static::getShippingChargeBy($get('../../shipping_method_id')) == 'weight') {
return __('lunarpanel.shipping::relationmanagers.shipping_rates.form.prices.repeater.min_weight.label');
}

return __('lunarpanel.shipping::relationmanagers.shipping_rates.form.prices.repeater.min_spend.label');
}
)
->numeric()
->required(),
])->afterStateHydrated(
static function (Forms\Components\Repeater $component, ?Model $record = null): void {
if ($record) {
$chargeBy = static::getShippingChargeBy($record->shippingMethod);
$currencies = Currency::all();

$component->state(
$record->priceBreaks->map(function ($price) {
$record->priceBreaks->map(function ($price) use ($chargeBy, $currencies) {
$currency = $currencies->first(fn ($currency) => $currency->id == $price->currency_id);

return [
'customer_group_id' => $price->customer_group_id,
'price' => $price->price->decimal,
'currency_id' => $price->currency_id,
'min_quantity' => $price->min_quantity / 100,
'min_quantity' => $chargeBy == 'cart_total' ? $price->min_quantity / $currency->factor : $price->min_quantity / 100,
];
})->toArray()
);
Expand Down Expand Up @@ -166,6 +181,19 @@ public function table(Table $table): Table
]);
}

private static function getShippingChargeBy(ShippingMethodContract|int|null $method): string
{
if (blank($method)) {
return 'cart_total';
}

if (! $method instanceof ShippingMethodContract) {
$method = ShippingMethod::find($method);
}

return ($method?->data['charge_by'] ?? null) ?? 'cart_total';
}

protected static function saveShippingRate(?ShippingRate $shippingRate = null, array $data = []): void
{
$currency = Currency::getDefault();
Expand All @@ -182,11 +210,17 @@ protected static function saveShippingRate(?ShippingRate $shippingRate = null, a
$shippingRate->priceBreaks()->delete();

$currencies = Currency::all();
$chargeBy = static::getShippingChargeBy($shippingRate->shippingMethod);

$tiers = collect($data['prices'] ?? [])->map(
function ($price) use ($currencies) {
function ($price) use ($chargeBy, $currencies) {
$currency = $currencies->first(fn ($currency) => $currency->id == $price['currency_id']);

$price['min_quantity'] = (int) ($price['min_quantity'] * $currency->factor);
if ($chargeBy == 'cart_total') {
$price['min_quantity'] = (int) ($price['min_quantity'] * $currency->factor);
} else {
$price['min_quantity'] = (int) ($price['min_quantity'] * 100);
}

$price['price'] = (int) ($price['price'] * $currency->factor);

Expand Down