86 lines
2.1 KiB
PHP
86 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Products;
|
|
|
|
use App\Models\Orders\ClientGroup;
|
|
use App\Traits\BelongsToTenant;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
/**
|
|
* @mixin IdeHelperPriceHistory
|
|
*/
|
|
class PriceHistory extends Model
|
|
{
|
|
use BelongsToTenant, SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'tenant_id',
|
|
'item_type_code',
|
|
'item_id',
|
|
'price_type_code',
|
|
'client_group_id',
|
|
'price',
|
|
'started_at',
|
|
'ended_at',
|
|
'created_by',
|
|
'updated_by',
|
|
'deleted_by',
|
|
];
|
|
|
|
protected $casts = [
|
|
'price' => 'decimal:4',
|
|
'started_at' => 'date',
|
|
'ended_at' => 'date',
|
|
];
|
|
|
|
// ClientGroup 관계
|
|
public function clientGroup()
|
|
{
|
|
return $this->belongsTo(ClientGroup::class, 'client_group_id');
|
|
}
|
|
|
|
// Polymorphic 관계 (item_type_code에 따라 Product 또는 Material)
|
|
public function item()
|
|
{
|
|
if ($this->item_type_code === 'PRODUCT') {
|
|
return $this->belongsTo(Product::class, 'item_id');
|
|
} elseif ($this->item_type_code === 'MATERIAL') {
|
|
return $this->belongsTo(\App\Models\Materials\Material::class, 'item_id');
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
// 스코프
|
|
public function scopeForItem($query, string $itemType, int $itemId)
|
|
{
|
|
return $query->where('item_type_code', $itemType)
|
|
->where('item_id', $itemId);
|
|
}
|
|
|
|
public function scopeForClientGroup($query, ?int $clientGroupId)
|
|
{
|
|
return $query->where('client_group_id', $clientGroupId);
|
|
}
|
|
|
|
public function scopeValidAt($query, $date)
|
|
{
|
|
return $query->where('started_at', '<=', $date)
|
|
->where(function ($q) use ($date) {
|
|
$q->whereNull('ended_at')
|
|
->orWhere('ended_at', '>=', $date);
|
|
});
|
|
}
|
|
|
|
public function scopeSalePrice($query)
|
|
{
|
|
return $query->where('price_type_code', 'SALE');
|
|
}
|
|
|
|
public function scopePurchasePrice($query)
|
|
{
|
|
return $query->where('price_type_code', 'PURCHASE');
|
|
}
|
|
}
|