belongsTo(Product::class); } public function dealer() { return $this->belongsTo(Dealer::class); } public function stockLogs() { return $this->hasMany(StockLog::class); } // Method untuk mengupdate stock public function updateStock($newQuantity, $source, $description = null) { $previousQuantity = $this->quantity; $quantityChange = $newQuantity - $previousQuantity; $this->quantity = $newQuantity; $this->save(); // Buat log perubahan StockLog::create([ 'stock_id' => $this->id, 'source_type' => get_class($source), 'source_id' => $source->id, 'previous_quantity' => $previousQuantity, 'new_quantity' => $newQuantity, 'quantity_change' => $quantityChange, 'description' => $description, 'user_id' => auth()->id() ]); return $this; } }