'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'); } }