51 lines
1.3 KiB
PHP
51 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Product extends Model
|
|
{
|
|
use HasFactory, SoftDeletes;
|
|
|
|
protected $fillable = ['code','name','description','unit','active','product_category_id'];
|
|
|
|
public function category(){
|
|
return $this->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;
|
|
}
|
|
}
|