feat: mejorar ux y validaciones del toldo
This commit is contained in:
@@ -3,8 +3,16 @@ import type { Venta, VentaLinea } from '@toldo/core';
|
||||
|
||||
export class VentasService {
|
||||
async registrarVenta(venta: Venta, lineas: VentaLinea[]) {
|
||||
if (!lineas.length || lineas.some((linea) => linea.cantidad <= 0)) {
|
||||
throw new Error('La venta debe tener cantidades mayores que cero');
|
||||
}
|
||||
// Validar stock mínimo conceptual - se puede expandir
|
||||
return db.transaction('rw', db.ventas, db.ventaLineas, async () => {
|
||||
for (const linea of lineas) {
|
||||
const movimientos = await db.movimientosInventario.where('productoId').equals(linea.productoId).toArray();
|
||||
const stock = movimientos.reduce((total, movimiento) => total + movimiento.cantidad, 0);
|
||||
if (stock < linea.cantidad) throw new Error(`Stock insuficiente para ${linea.productoId}. Disponible: ${stock}`);
|
||||
}
|
||||
await db.ventas.add(venta);
|
||||
await db.ventaLineas.bulkAdd(lineas);
|
||||
// Registrar movimientos de venta
|
||||
|
||||
Reference in New Issue
Block a user