56 lines
1.4 KiB
PHP
56 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Inventory extends Model
|
|
{
|
|
/** @use HasFactory<\Database\Factories\InventoryFactory> */
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'warehouse_id',
|
|
'product_id',
|
|
'quantity',
|
|
'safety_stock',
|
|
'location',
|
|
];
|
|
|
|
public function warehouse(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Warehouse::class);
|
|
}
|
|
|
|
public function product(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Product::class);
|
|
}
|
|
|
|
public function transactions(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(InventoryTransaction::class);
|
|
}
|
|
|
|
public function lastOutgoingTransaction()
|
|
{
|
|
return $this->hasOne(InventoryTransaction::class)->ofMany([
|
|
'actual_time' => 'max',
|
|
'id' => 'max',
|
|
], function ($query) {
|
|
$query->where('quantity', '<', 0);
|
|
});
|
|
}
|
|
|
|
public function lastIncomingTransaction()
|
|
{
|
|
return $this->hasOne(InventoryTransaction::class)->ofMany([
|
|
'actual_time' => 'max',
|
|
'id' => 'max',
|
|
], function ($query) {
|
|
$query->where('quantity', '>', 0);
|
|
});
|
|
}
|
|
}
|