2026-07-19 00:50:51 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Modules\Core\Shipping\Filament\Pages;
|
|
|
|
|
|
|
|
|
|
use Filament\Forms\Form;
|
|
|
|
|
use Filament\Forms\Get;
|
|
|
|
|
use Filament\Tables\Columns\TextColumn;
|
|
|
|
|
use Filament\Tables\Table;
|
2026-07-19 02:24:10 +03:00
|
|
|
use Lunar\Shipping\Filament\Resources\ShippingZoneResource\Pages\ManageShippingRates as BaseManageShippingRates;
|
2026-07-19 00:50:51 +03:00
|
|
|
use Lunar\Shipping\Models\ShippingMethod;
|
|
|
|
|
use Lunar\Shipping\Models\ShippingRate;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Bound in place of the vendor ManageShippingRates page via the container
|
|
|
|
|
* (see ShippingServiceProvider), since that page has no extension hook of
|
|
|
|
|
* its own. Every reference to the vendor class name — routes, sub-nav,
|
|
|
|
|
* ShippingZoneResource::getPages() — is untouched; the container simply
|
|
|
|
|
* hands back this subclass whenever the vendor class is resolved.
|
|
|
|
|
*
|
2026-07-19 02:24:10 +03:00
|
|
|
* Hides the price / price-break fields for a rate whose method has
|
|
|
|
|
* charge_by = "live" (see ShippingMethodResourceExtension, which adds that
|
|
|
|
|
* option to methods whose driver supports live pricing) — those fields
|
|
|
|
|
* would otherwise be dead configuration the driver never reads. Pricing
|
|
|
|
|
* strategy (cart_total / weight / live) stays entirely on the Shipping
|
|
|
|
|
* Method, matching Lunar's own existing charge_by convention; nothing new
|
|
|
|
|
* is stored on the rate itself.
|
2026-07-19 00:50:51 +03:00
|
|
|
*/
|
|
|
|
|
class ManageShippingRates extends BaseManageShippingRates
|
|
|
|
|
{
|
|
|
|
|
public function form(Form $form): Form
|
|
|
|
|
{
|
|
|
|
|
$form = parent::form($form);
|
|
|
|
|
|
|
|
|
|
return $form->schema(
|
2026-07-19 02:24:10 +03:00
|
|
|
$this->hidePriceFieldsWhenLive($form->getComponents())
|
2026-07-19 00:50:51 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function hidePriceFieldsWhenLive(array $components): array
|
|
|
|
|
{
|
2026-07-19 02:24:10 +03:00
|
|
|
$isNotLive = fn (Get $get) => static::methodChargeBy($get('shipping_method_id')) !== 'live';
|
2026-07-19 00:50:51 +03:00
|
|
|
|
|
|
|
|
foreach ($components as $component) {
|
|
|
|
|
if (! method_exists($component, 'getName')) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($component->getName() === 'price') {
|
2026-07-19 02:24:10 +03:00
|
|
|
$component->visible($isNotLive)->required($isNotLive)->dehydrated(true);
|
2026-07-19 00:50:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($component->getName() === 'prices') {
|
2026-07-19 02:24:10 +03:00
|
|
|
$component->visible($isNotLive)->dehydrated(true);
|
2026-07-19 00:50:51 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $components;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function table(Table $table): Table
|
|
|
|
|
{
|
|
|
|
|
$table = parent::table($table);
|
|
|
|
|
|
|
|
|
|
return $table->columns(
|
|
|
|
|
array_map(function ($column) {
|
|
|
|
|
if (method_exists($column, 'getName') && $column->getName() === 'basePrices.0') {
|
|
|
|
|
return TextColumn::make('basePrices.0')
|
|
|
|
|
->label(__('lunarpanel.shipping::relationmanagers.shipping_rates.table.price.label'))
|
|
|
|
|
->formatStateUsing(function ($state, ShippingRate $record) {
|
2026-07-19 02:24:10 +03:00
|
|
|
if (static::methodChargeBy($record->shipping_method_id) === 'live') {
|
2026-07-19 00:50:51 +03:00
|
|
|
return 'Live API pricing';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $state?->price->formatted;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $column;
|
|
|
|
|
}, $table->getColumns())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static function saveShippingRate(?ShippingRate $shippingRate = null, array $data = []): void
|
|
|
|
|
{
|
2026-07-19 02:24:10 +03:00
|
|
|
if (static::methodChargeBy($data['shipping_method_id'] ?? $shippingRate?->shipping_method_id) === 'live') {
|
2026-07-19 00:50:51 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parent::saveShippingRate($shippingRate, $data);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-19 02:24:10 +03:00
|
|
|
protected static function methodChargeBy(ShippingMethod|int|string|null $method): ?string
|
2026-07-19 00:50:51 +03:00
|
|
|
{
|
|
|
|
|
if (blank($method)) {
|
2026-07-19 02:24:10 +03:00
|
|
|
return null;
|
2026-07-19 00:50:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! $method instanceof ShippingMethod) {
|
|
|
|
|
$method = ShippingMethod::find($method);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-19 02:24:10 +03:00
|
|
|
return $method?->data['charge_by'] ?? null;
|
2026-07-19 00:50:51 +03:00
|
|
|
}
|
|
|
|
|
}
|