150 lines
3.1 KiB
PHP
150 lines
3.1 KiB
PHP
<?php namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class CatalogItem extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'type',
|
|
'category_id',
|
|
'name',
|
|
'description',
|
|
'sku',
|
|
'barcode',
|
|
'is_stockable',
|
|
'stock',
|
|
'track_serials',
|
|
'attributes',
|
|
'is_active',
|
|
];
|
|
|
|
protected $casts = [
|
|
'is_stockable' => 'boolean',
|
|
'track_serials' => 'boolean',
|
|
'is_active' => 'boolean',
|
|
'attributes' => 'array',
|
|
];
|
|
|
|
protected $appends = ['has_serials'];
|
|
|
|
// RELACIONES
|
|
|
|
public function category()
|
|
{
|
|
return $this->belongsTo(Category::class);
|
|
}
|
|
|
|
public function price()
|
|
{
|
|
return $this->hasOne(Price::class);
|
|
}
|
|
|
|
public function serials()
|
|
{
|
|
return $this->hasMany(InventorySerial::class);
|
|
}
|
|
|
|
public function availableSerials()
|
|
{
|
|
return $this->hasMany(InventorySerial::class)
|
|
->where('status', 'disponible');
|
|
}
|
|
|
|
// HELPERS DE TIPO
|
|
|
|
public function isProduct(): bool
|
|
{
|
|
return $this->type === 'product';
|
|
}
|
|
|
|
public function isService(): bool
|
|
{
|
|
return $this->type === 'service';
|
|
}
|
|
|
|
public function isLaundry(): bool
|
|
{
|
|
return $this->type === 'laundry';
|
|
}
|
|
|
|
// HELPERS DE STOCK
|
|
|
|
public function getAvailableStockAttribute(): int
|
|
{
|
|
if (!$this->is_stockable) {
|
|
return 0;
|
|
}
|
|
|
|
if ($this->track_serials) {
|
|
return $this->availableSerials()->count();
|
|
}
|
|
|
|
return $this->stock ?? 0;
|
|
}
|
|
|
|
public function syncStock(): void
|
|
{
|
|
if ($this->is_stockable && $this->track_serials) {
|
|
$this->update(['stock' => $this->availableSerials()->count()]);
|
|
}
|
|
}
|
|
|
|
public function decrementStock(int $quantity): void
|
|
{
|
|
if ($this->is_stockable && !$this->track_serials) {
|
|
$this->decrement('stock', $quantity);
|
|
}
|
|
}
|
|
|
|
public function incrementStock(int $quantity): void
|
|
{
|
|
if ($this->is_stockable && !$this->track_serials) {
|
|
$this->increment('stock', $quantity);
|
|
}
|
|
}
|
|
|
|
public function getHasSerialsAttribute(): bool
|
|
{
|
|
return $this->track_serials &&
|
|
isset($this->attributes['serials_count']) &&
|
|
$this->attributes['serials_count'] > 0;
|
|
}
|
|
|
|
//HELPERS DE ATRIBUTOS
|
|
|
|
public function getAtributos(string $key, $default = null)
|
|
{
|
|
return $this->attributes[$key] ?? $default;
|
|
}
|
|
|
|
//SCOPES
|
|
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('is_active', true);
|
|
}
|
|
|
|
public function scopeOfType($query, string $type)
|
|
{
|
|
return $query->where('type', $type);
|
|
}
|
|
|
|
public function scopeProducts($query)
|
|
{
|
|
return $query->where('type', 'product');
|
|
}
|
|
|
|
public function scopeServices($query)
|
|
{
|
|
return $query->where('type', 'service');
|
|
}
|
|
|
|
public function scopeStockable($query)
|
|
{
|
|
return $query->where('is_stockable', true);
|
|
}
|
|
}
|