Files
CKB/app/Models/Stock.php

57 lines
1.3 KiB
PHP
Executable File

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Stock extends Model
{
use HasFactory;
protected $fillable = [
'product_id',
'dealer_id',
'quantity'
];
public function product()
{
return $this->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;
}
}