pdv.backend/app/Models/InventoryMovement.php

77 lines
1.8 KiB
PHP

<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class InventoryMovement extends Model
{
const UPDATED_AT = null;
protected $fillable = [
'inventory_id',
'warehouse_from_id',
'warehouse_to_id',
'movement_type',
'quantity',
'unit_cost',
'supplier_id',
'reference_type',
'reference_id',
'user_id',
'notes',
'invoice_reference',
];
protected $casts = [
'quantity' => 'decimal:3',
'unit_cost' => 'decimal:2',
'created_at' => 'datetime',
];
// Relaciones
public function inventory() {
return $this->belongsTo(Inventory::class);
}
public function supplier() {
return $this->belongsTo(Supplier::class);
}
public function warehouseFrom() {
return $this->belongsTo(Warehouse::class, 'warehouse_from_id');
}
public function warehouseTo() {
return $this->belongsTo(Warehouse::class, 'warehouse_to_id');
}
public function user() {
return $this->belongsTo(User::class);
}
public function serials() {
return $this->hasMany(InventorySerial::class, 'movement_id');
}
// Relación polimórfica para la referencia
public function reference() {
return $this->morphTo();
}
// Scopes
public function scopeByType($query, string $type) {
return $query->where('movement_type', $type);
}
public function scopeEntry($query) {
return $query->where('movement_type', 'entry');
}
public function scopeExit($query) {
return $query->where('movement_type', 'exit');
}
public function scopeTransfer($query) {
return $query->where('movement_type', 'transfer');
}
}