116 lines
2.5 KiB
PHP
116 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Observers\InventoryMovementObserver;
|
|
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Notsoweb\LaravelCore\Traits\Models\Extended;
|
|
|
|
#[ObservedBy([InventoryMovementObserver::class])]
|
|
class InventoryMovement extends Model
|
|
{
|
|
use Extended;
|
|
|
|
const UPDATED_AT = null;
|
|
|
|
protected $fillable = [
|
|
'inventory_id',
|
|
'warehouse_from_id',
|
|
'warehouse_to_id',
|
|
'movement_type',
|
|
'quantity',
|
|
'unit_of_measure_id',
|
|
'unit_quantity',
|
|
'unit_cost',
|
|
'unit_cost_original',
|
|
'supplier_id',
|
|
'reference_type',
|
|
'reference_id',
|
|
'user_id',
|
|
'notes',
|
|
'invoice_reference',
|
|
];
|
|
|
|
protected $casts = [
|
|
'quantity' => 'decimal:3',
|
|
'unit_quantity' => 'decimal:3',
|
|
'unit_cost' => 'decimal:2',
|
|
'unit_cost_original' => '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 unitOfMeasure()
|
|
{
|
|
return $this->belongsTo(UnitOfMeasurement::class);
|
|
}
|
|
|
|
public function serials()
|
|
{
|
|
return $this->hasMany(InventorySerial::class, 'movement_id');
|
|
}
|
|
|
|
public function transferredSerials()
|
|
{
|
|
return $this->hasMany(InventorySerial::class, 'transfer_movement_id');
|
|
}
|
|
|
|
public function exitedSerials()
|
|
{
|
|
return $this->hasMany(InventorySerial::class, 'exit_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');
|
|
}
|
|
}
|