* * @version 1.0.0 */ class SaleDetail extends Model { protected $fillable = [ 'sale_id', 'inventory_id', 'product_name', 'quantity', 'unit_price', 'subtotal', 'discount_percentage', 'discount_amount', ]; protected $casts = [ 'unit_price' => 'decimal:2', 'subtotal' => 'decimal:2', 'discount_percentage' => 'decimal:2', 'discount_amount' => 'decimal:2', ]; public function sale() { return $this->belongsTo(Sale::class); } public function inventory() { return $this->belongsTo(Inventory::class); } public function serials() { return $this->hasMany(InventorySerial::class, 'sale_detail_id'); } /** * Obtener números de serie vendidos */ public function getSerialNumbersAttribute(): array { return $this->serials()->pluck('serial_number')->toArray(); } /** * Devoluciones de este detalle */ public function returnDetails() { return $this->hasMany(ReturnDetail::class); } /** * Cantidad total devuelta */ public function getQuantityReturnedAttribute(): int { return $this->returnDetails()->sum('quantity_returned'); } /** * Cantidad restante (no devuelta) */ public function getQuantityRemainingAttribute(): int { return $this->quantity - $this->getQuantityReturnedAttribute(); } /** * Verificar si se puede devolver más */ public function canReturn(): bool { return $this->getQuantityRemainingAttribute() > 0; } /** * Cantidad máxima que se puede devolver */ public function getMaxReturnableQuantityAttribute(): int { return $this->getQuantityRemainingAttribute(); } }