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; } /** * Get all works that require this product * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function works() { return $this->belongsToMany(Work::class, 'work_products') ->withPivot('quantity_required', 'notes') ->withTimestamps(); } /** * Get work products pivot records * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function workProducts() { return $this->hasMany(WorkProduct::class); } }