feat: mejorar pos movil y cierre de jornada
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
export * from './entities';
|
||||
export * from './rules/cuadreRules';
|
||||
export * from './rules/jornadaRules';
|
||||
export * from './rules/ventaRules';
|
||||
export * from './entities/JornadaToldo';
|
||||
|
||||
8
packages/core/src/rules/ventaRules.ts
Normal file
8
packages/core/src/rules/ventaRules.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export function calcularDevuelta(total: number, recibido: number) {
|
||||
return recibido - total;
|
||||
}
|
||||
|
||||
export function validarPagoEfectivo(total: number, recibido: number) {
|
||||
if (!Number.isFinite(recibido) || recibido < total) return 'El efectivo recibido no alcanza para cubrir la venta';
|
||||
return null;
|
||||
}
|
||||
@@ -60,15 +60,14 @@ export class JornadaService {
|
||||
return gastoId;
|
||||
}
|
||||
|
||||
async cerrarJornada(jornadaId: string, cuadreId?: string): Promise<string> {
|
||||
async cerrarJornada(jornadaId: string, saldoRealEfectivo: number, totalQRBanco?: number): Promise<string> {
|
||||
if (!Number.isFinite(saldoRealEfectivo) || saldoRealEfectivo < 0) throw new Error('El efectivo contado es obligatorio y no puede ser negativo');
|
||||
const jornada: any = await db.jornadas.get(jornadaId);
|
||||
if (!jornada) throw new Error('Jornada no encontrada');
|
||||
if (jornada.estado === 'CERRADA') throw new Error('Jornada ya cerrada');
|
||||
|
||||
let cid = cuadreId;
|
||||
if (!cid) {
|
||||
const cuadreParcial = await this.cuadreService.calcularCuadreDelDia(jornada.fecha, jornada.baseInicial);
|
||||
const cuadre: CuadreCaja = {
|
||||
const cuadreParcial = await this.cuadreService.calcularCuadreDelDia(jornada.fecha, jornada.baseInicial);
|
||||
const cuadre: CuadreCaja = {
|
||||
id: crypto.randomUUID(),
|
||||
fecha: jornada.fecha,
|
||||
baseInicial: jornada.baseInicial,
|
||||
@@ -78,17 +77,19 @@ export class JornadaService {
|
||||
totalGastosQR: cuadreParcial.totalGastosQR || 0,
|
||||
saldoEsperadoEfectivo: cuadreParcial.saldoEsperadoEfectivo || 0,
|
||||
totalVentas: cuadreParcial.totalVentas || 0,
|
||||
createdAt: new Date()
|
||||
} as CuadreCaja;
|
||||
cid = await this.cuadreService.guardarCuadre(cuadre);
|
||||
saldoRealEfectivo,
|
||||
totalQRBanco,
|
||||
createdAt: new Date()
|
||||
} as CuadreCaja;
|
||||
cuadre.descuadreEfectivo = saldoRealEfectivo - cuadre.saldoEsperadoEfectivo;
|
||||
if (totalQRBanco !== undefined) {
|
||||
cuadre.totalQRRegistrado = cuadre.totalVentasQR - cuadre.totalGastosQR;
|
||||
cuadre.descuadreQR = totalQRBanco - cuadre.totalQRRegistrado;
|
||||
}
|
||||
|
||||
await db.jornadas.put({
|
||||
...jornada,
|
||||
estado: 'CERRADA',
|
||||
fechaCierre: new Date(),
|
||||
cuadreId: cid,
|
||||
updatedAt: new Date()
|
||||
const cid = cuadre.id;
|
||||
await db.transaction('rw', db.jornadas, db.cuadres, async () => {
|
||||
await db.cuadres.add(cuadre);
|
||||
await db.jornadas.put({ ...jornada, estado: 'CERRADA', fechaCierre: new Date(), cuadreId: cid, updatedAt: new Date() });
|
||||
});
|
||||
return cid;
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ describe('Caso de uso jornada', () => {
|
||||
expect(resumen?.totalVentas).toBe(30000);
|
||||
expect(resumen?.totalGastos).toBe(5000);
|
||||
|
||||
const cuadreId = await service.cerrarJornada(jid);
|
||||
const cuadreId = await service.cerrarJornada(jid, 125000);
|
||||
expect(cuadreId).toBeTruthy();
|
||||
|
||||
const abierta = await service.getJornadaAbierta();
|
||||
|
||||
Reference in New Issue
Block a user