feat: repositorios, servicios, UI completa con rutas, Capacitor Android sync
This commit is contained in:
37
packages/services/src/ventas/VentasService.ts
Normal file
37
packages/services/src/ventas/VentasService.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { db } from '@toldo/data';
|
||||
import type { Venta, VentaLinea } from '@toldo/core';
|
||||
|
||||
export class VentasService {
|
||||
async registrarVenta(venta: Venta, lineas: VentaLinea[]) {
|
||||
// Validar stock mínimo conceptual - se puede expandir
|
||||
return db.transaction('rw', db.ventas, db.ventaLineas, async () => {
|
||||
await db.ventas.add(venta);
|
||||
await db.ventaLineas.bulkAdd(lineas);
|
||||
// Registrar movimientos de venta
|
||||
for (const linea of lineas) {
|
||||
await db.movimientosInventario.add({
|
||||
id: crypto.randomUUID(),
|
||||
fecha: venta.fecha,
|
||||
tipo: 'VENTA',
|
||||
productoId: linea.productoId,
|
||||
cantidad: -Math.abs(linea.cantidad),
|
||||
referenciaId: venta.id,
|
||||
createdAt: new Date()
|
||||
});
|
||||
}
|
||||
return venta.id;
|
||||
});
|
||||
}
|
||||
|
||||
async getVentasDelDia(fecha: Date): Promise<Venta[]> {
|
||||
const start = new Date(fecha);
|
||||
start.setHours(0, 0, 0, 0);
|
||||
const end = new Date(fecha);
|
||||
end.setHours(23, 59, 59, 999);
|
||||
return db.ventas.where('fecha').between(start, end).toArray();
|
||||
}
|
||||
|
||||
async getLineasPorVenta(ventaId: string): Promise<VentaLinea[]> {
|
||||
return db.ventaLineas.where('ventaId').equals(ventaId).toArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user