pdv.backend/app/Models/Inventory.php
Juan Felipe Zapata Moreno 810aff1b0e WIP: Serials
2026-01-16 17:37:39 -06:00

71 lines
1.4 KiB
PHP

<?php namespace App\Models;
/**
* @copyright (c) 2025 Notsoweb Software (https://notsoweb.com) - All Rights Reserved
*/
use Illuminate\Database\Eloquent\Model;
/**
* Descripción
*
* @author Moisés Cortés C. <moises.cortes@notsoweb.com>
*
* @version 1.0.0
*/
class Inventory extends Model
{
protected $fillable = [
'category_id',
'name',
'sku',
'barcode',
'stock',
'is_active',
];
protected $casts = [
'is_active' => 'boolean',
];
public function category()
{
return $this->belongsTo(Category::class);
}
public function price()
{
return $this->hasOne(Price::class);
}
public function serials()
{
return $this->hasMany(InventorySerial::class);
}
/**
* Obtener seriales disponibles
*/
public function availableSerials()
{
return $this->hasMany(InventorySerial::class)
->where('status', 'disponible');
}
/**
* Calcular stock basado en seriales disponibles
*/
public function getAvailableStockAttribute(): int
{
return $this->availableSerials()->count();
}
/**
* Sincronizar el campo stock con los seriales disponibles
*/
public function syncStock(): void
{
$this->update(['stock' => $this->getAvailableStockAttribute()]);
}
}