61 lines
2.1 KiB
TypeScript
61 lines
2.1 KiB
TypeScript
import { db } from '@toldo/data';
|
|
import type { CuadreCaja } from '@toldo/core';
|
|
import { calcularSaldoEsperadoEfectivo, calcularDescuadreEfectivo, calcularTotalVentas } from '@toldo/core';
|
|
|
|
export class CuadreService {
|
|
async calcularCuadreDelDia(fecha: Date, baseInicial: number): Promise<Partial<CuadreCaja>> {
|
|
const start = new Date(fecha);
|
|
start.setHours(0, 0, 0, 0);
|
|
const end = new Date(fecha);
|
|
end.setHours(23, 59, 59, 999);
|
|
|
|
const ventas = await db.ventas.where('fecha').between(start, end).toArray();
|
|
const lineas = await db.ventaLineas.toArray();
|
|
const gastos = await db.gastos.where('fecha').between(start, end).toArray();
|
|
|
|
let totalVentasEfectivo = 0;
|
|
let totalVentasQR = 0;
|
|
|
|
for (const venta of ventas) {
|
|
const vLineas = lineas.filter(l => l.ventaId === venta.id);
|
|
for (const l of vLineas) {
|
|
if (l.formaPago === 'EFECTIVO') totalVentasEfectivo += l.subtotal;
|
|
else totalVentasQR += l.subtotal;
|
|
}
|
|
}
|
|
|
|
let totalGastosEfectivo = 0;
|
|
let totalGastosQR = 0;
|
|
for (const g of gastos) {
|
|
if (g.formaPago === 'EFECTIVO') totalGastosEfectivo += g.monto;
|
|
else if (g.formaPago === 'QR') totalGastosQR += g.monto;
|
|
}
|
|
|
|
const saldoEsperadoEfectivo = calcularSaldoEsperadoEfectivo(baseInicial, totalVentasEfectivo, totalGastosEfectivo);
|
|
const totalVentas = calcularTotalVentas(totalVentasEfectivo, totalVentasQR);
|
|
|
|
return {
|
|
fecha,
|
|
baseInicial,
|
|
totalVentasEfectivo,
|
|
totalVentasQR,
|
|
totalGastosEfectivo,
|
|
totalGastosQR,
|
|
saldoEsperadoEfectivo,
|
|
totalVentas,
|
|
createdAt: new Date()
|
|
};
|
|
}
|
|
|
|
async guardarCuadre(cuadre: CuadreCaja) {
|
|
if (cuadre.saldoRealEfectivo !== undefined && cuadre.saldoEsperadoEfectivo !== undefined) {
|
|
cuadre.descuadreEfectivo = calcularDescuadreEfectivo(cuadre.saldoEsperadoEfectivo, cuadre.saldoRealEfectivo);
|
|
}
|
|
if (cuadre.totalQRBanco !== undefined) {
|
|
cuadre.totalQRRegistrado = cuadre.totalVentasQR - cuadre.totalGastosQR;
|
|
cuadre.descuadreQR = cuadre.totalQRBanco - cuadre.totalQRRegistrado;
|
|
}
|
|
return db.cuadres.add(cuadre);
|
|
}
|
|
}
|