belongsTo(ProductCategory::class, 'product_category_id'); } public function opnameDetails(){ return $this->hasMany(OpnameDetail::class); } public function stocks(){ return $this->hasMany(Stock::class); } public function mutationDetails() { return $this->hasMany(MutationDetail::class); } public function dealers() { return $this->belongsToMany(Dealer::class, 'stocks', 'product_id', 'dealer_id') ->withPivot('quantity') ->withTimestamps(); } // Helper method untuk mendapatkan total stock saat ini public function getCurrentTotalStockAttribute() { return $this->stocks()->sum('quantity'); } // Helper method untuk mendapatkan stock di dealer tertentu public function getStockByDealer($dealerId) { return $this->stocks()->where('dealer_id', $dealerId)->first()?->quantity ?? 0; } }