Filament ProductResource (/admin/products): lista keresővel és szűrőkkel, csak olvasható adatlap. Profitcenternél szerver oldalon csak a saját, rendelhető beszállítók termékei látszanak; mindenkinél csak a beszállító jelenleg érvényes árlistáján szereplők (Product::scopeInActivePriceList). ProfitCenterProductSearch feature flag mögött, menüpont mindkét nav barban. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
161 lines
5.3 KiB
PHP
161 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\StockStatusEnum;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class Product extends BaseAuditable
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $guarded = ['id'];
|
|
// protected $fillable=['name'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'stock_status' => StockStatusEnum::class,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Beszállítónként az érvényes árlista: a legfrissebb, a megadott napig már hatályos
|
|
* (available <= dátum), törlés nélküli lista; azonos dátumnál a kisebb id - ugyanaz a
|
|
* sorrend, mint a Price()-ban. A termék akkor "érvényes árlistás", ha ebben szerepel.
|
|
*/
|
|
public function scopeInActivePriceList(Builder $query, ?string $date = null): Builder
|
|
{
|
|
$date ??= Carbon::today()->format('Y-m-d');
|
|
|
|
return $query->whereExists(function ($sub) use ($date) {
|
|
$sub->from('price_list_prices as plp')
|
|
->join('price_lists as pl', 'pl.id', '=', 'plp.price_list_id')
|
|
->whereColumn('plp.product_id', 'products.id')
|
|
->whereRaw('pl.id = ('.self::activePriceListIdSql().')', [$date])
|
|
->select(DB::raw(1));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hozzáadja a current_price oszlopot: a termék ára az érvényes árlistán
|
|
* (lásd scopeInActivePriceList), NULL ha nem szerepel rajta.
|
|
*/
|
|
public function scopeWithActivePrice(Builder $query, ?string $date = null): Builder
|
|
{
|
|
$date ??= Carbon::today()->format('Y-m-d');
|
|
|
|
return $query->addSelect([
|
|
'products.*',
|
|
'current_price' => DB::table('price_list_prices as plp')
|
|
->join('price_lists as pl', 'pl.id', '=', 'plp.price_list_id')
|
|
->whereColumn('plp.product_id', 'products.id')
|
|
->whereRaw('pl.id = ('.self::activePriceListIdSql().')', [$date])
|
|
->select('plp.price')
|
|
->limit(1),
|
|
]);
|
|
}
|
|
|
|
private static function activePriceListIdSql(): string
|
|
{
|
|
return 'select pl2.id from price_lists pl2 where pl2.supplier_id = pl.supplier_id '
|
|
.'and pl2.deleted_at is null and pl2.available <= ? order by pl2.available desc, pl2.id asc limit 1';
|
|
}
|
|
|
|
public function ProductGroup(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ProductGroup::class);
|
|
}
|
|
|
|
public function Supplier(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Supplier::class);
|
|
}
|
|
|
|
public function Producer(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Producer::class);
|
|
}
|
|
|
|
public function Price(?string $deliveryDate = null)
|
|
{
|
|
$query = $this->belongsToMany(PriceList::class, 'price_list_prices', 'product_id', 'price_list_id')->select('id')
|
|
// ->withTimestamps()
|
|
->withPivot(['price']);
|
|
|
|
if (! $deliveryDate) {
|
|
$deliveryDate = Carbon::today()->format('Y-m-d');
|
|
}
|
|
$query->where('available', '<=', $deliveryDate);
|
|
$query->orderBy('available', 'desc')->orderBy('id', 'asc');
|
|
|
|
if ($priceRecord = $query->first()) {
|
|
return $priceRecord->pivot->price;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function setAttributePrice(?string $deliveryDate = null)
|
|
{
|
|
$this->setAttribute('price', $this->price($deliveryDate));
|
|
}
|
|
|
|
public function LastPrice(): BelongsToMany
|
|
{
|
|
|
|
return $this->belongsToMany(PriceList::class, 'price_list_prices', 'product_id', 'price_list_id')->select('id')
|
|
->withTimestamps()
|
|
->withPivot(['price'])
|
|
->where('available', '<=', Carbon::today()->format('Y-m-d'))
|
|
->orderBy('available', 'desc')->orderBy('id', 'desc');
|
|
// ->limit(1)
|
|
// ->as('priceListPrice')
|
|
}
|
|
|
|
public function PriceList(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(PriceList::class, 'price_list_prices', 'product_id', 'price_list_id')
|
|
->withTimestamps()
|
|
->withPivot(['price']);
|
|
// ->as('priceListPrice')
|
|
}
|
|
|
|
public function ProfitCenter(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(ProfitCenter::class, 'profit_center_favorites')->withTimestamps();
|
|
}
|
|
|
|
public function picture(): MorphOne
|
|
{
|
|
return $this->morphOne(Attachment::class, 'attachable')->where('attachType', '=', 'picture');
|
|
}
|
|
|
|
public function specification(): MorphOne
|
|
{
|
|
return $this->morphOne(Attachment::class, 'attachable')->where('attachType', '=', 'specification');
|
|
}
|
|
|
|
public function getActualPriceList(?string $deliveryDate = null): ?PriceList
|
|
{
|
|
$price = $this->belongsToMany(PriceList::class, 'price_list_prices', 'product_id', 'price_list_id')->select('id')
|
|
// ->withTimestamps()
|
|
->withPivot(['price']);
|
|
|
|
if (! $deliveryDate) {
|
|
$deliveryDate = Carbon::today()->format('Y-m-d');
|
|
}
|
|
$price->where('available', '<=', $deliveryDate);
|
|
$price->orderBy('available', 'desc')->orderBy('id', 'asc');
|
|
|
|
// $price->first();
|
|
return $price->get()->first();
|
|
}
|
|
}
|