feat(billing): receipt capture — outstanding workflow, batch by check, reconciliation
Implements docs/RECEIPT_CAPTURE_SPEC.md §1, the legacy "Editor" replacement, on top of the single-movement capture from plan step 6. No new abilities: batching and resolving are both capturing. - outstanding (legacy NOPAGO): capture flag, ?outstanding= filter, and POST /billing/:id/resolve-outstanding (gated ledger:create, not ledger:void — resolving completes a capture rather than reversing one). Outstanding rows are excluded from every balance aggregate, matching the legacy SALDOS ULTIMO 0 query's HAVING NOPAGO = 0, but still count in the movement browser's filtered totals. - POST /billing/batch: many customers' receipts against one check, in one $transaction. Deliberately not a persisted batch entity — checkNumber is already a column and grouping by it answers every legacy by-check query. - GET /billing/by-check + a cheque-count report, replacing REPORTE CHEQUE COUNT / REPORTE POR CHEQUE / EDITA CHEQUE ALF|COUNT|NUM. Print, PDF, CSV and XLSX come free from the existing /reportes/:slug machinery. - Web: /estado-cuenta/lote (the Editor screen, with live reconciliation against the physical check amount), an "Estado de pago" filter, a "sin fondos" row tag and a Resolver dialog, plus a top-level "Captura" nav entry. Integration seam for the OCR auto-capture module (spec §2), which is required to post through createBatch rather than writing Transaction rows itself: items[i] maps to lines[i] so postedTransactionId can be zipped back on; opts.refs[i] stamps captureRef with a duplicate-post guard that a voided row deliberately does not block; opts.source is service-level only, so an HTTP client cannot label hand-keyed rows as machine-captured. captureSource/captureRef are nullable so the 40,136 migrated rows stay NULL rather than being mislabelled. Fixes two pre-existing bugs found while building this: - statement() filtered legacySourceTable with `notIn`, which compiles to SQL NOT IN — and `NULL NOT IN (...)` is NULL, so every app-captured movement was invisible on the customer statement (438 rows in the movement browser vs 392 on the statement) while showing everywhere else. This would have made the whole capture feature look broken. - The balances count query omitted the void filter its own page query applied, so the total disagreed with the rows. Nav highlighting now resolves by longest match; the previous first-startsWith logic lit up both the parent and any nested entry. Verified end-to-end against the dev DB, API and browser; all test rows removed afterwards. Also corrects RESUME.md, which documented the dev ports as :3001/:3000 — they are :4501/:4500, from the env files. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -949,6 +949,111 @@ const edoCuentaDatos: ReportDef = {
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* REPORTE CHEQUE COUNT — everything captured against one check.
|
||||
*
|
||||
* The reconciliation half of the batch-capture flow (docs/RECEIPT_CAPTURE_SPEC
|
||||
* §1.3): staff key many customers' receipts against one physical check, then
|
||||
* check that what was captured adds up to what the check was cut for. Replaces
|
||||
* `EDITA CHEQUE ALF/COUNT/NUM`, `REPORTE POR CHEQUE` and
|
||||
* `REPORTE POR CHEQUE PARA ALFA` — four legacy objects, one parameterized
|
||||
* report.
|
||||
*
|
||||
* Deliberately mirrors `BillingService.byCheck`'s rules rather than inventing
|
||||
* its own: voided rows are dropped entirely, and outstanding (NOPAGO) rows are
|
||||
* listed but excluded from the total, because the check never funded them.
|
||||
*/
|
||||
const chequeCount: ReportDef = {
|
||||
slug: "cheque-count",
|
||||
title: "Reporte por cheque",
|
||||
description:
|
||||
"Todos los movimientos capturados contra un mismo cheque, con el total " +
|
||||
"para conciliar contra el importe físico del cheque. Los movimientos " +
|
||||
"pendientes de pago (sin fondos) se listan pero no suman al total.",
|
||||
domain: "estado-cuenta",
|
||||
legacyName: "REPORTE CHEQUE COUNT / REPORTE POR CHEQUE / EDITA CHEQUE COUNT",
|
||||
format: "tabular",
|
||||
params: [
|
||||
{
|
||||
key: "checkNumber",
|
||||
label: "Número de cheque",
|
||||
kind: "text",
|
||||
placeholder: "Ej. 10432",
|
||||
},
|
||||
],
|
||||
columns: [
|
||||
{ key: "customerName", label: "Cliente", type: "text" },
|
||||
{ key: "reference", label: "Referencia", type: "text" },
|
||||
{ key: "period", label: "Periodo", type: "text" },
|
||||
{ key: "concept", label: "Concepto", type: "text" },
|
||||
{ key: "transactionDate", label: "Fecha", type: "date" },
|
||||
{ key: "status", label: "Estado", type: "text" },
|
||||
{ key: "amount", label: "Importe", type: "money", align: "right" },
|
||||
],
|
||||
async run(prisma, p) {
|
||||
const checkNumber = p.checkNumber?.trim();
|
||||
if (!checkNumber) {
|
||||
return {
|
||||
rows: [],
|
||||
totals: { movimientos: 0 },
|
||||
subtitle: "Indique un número de cheque",
|
||||
};
|
||||
}
|
||||
|
||||
const rows = await prisma.transaction.findMany({
|
||||
where: { checkNumber, ...NOT_VOIDED },
|
||||
orderBy: [{ transactionDate: "asc" }, { id: "asc" }],
|
||||
select: {
|
||||
transactionDate: true,
|
||||
amount: true,
|
||||
currency: true,
|
||||
reference: true,
|
||||
period: true,
|
||||
outstanding: true,
|
||||
type: { select: { nameEn: true, nameEs: true } },
|
||||
customer: { select: { name: true, nameMissing: true } },
|
||||
},
|
||||
});
|
||||
|
||||
// Per currency, and never collapsed — same rule as the rest of the ledger.
|
||||
const totals = new Map<string, Prisma.Decimal>();
|
||||
let outstandingCount = 0;
|
||||
for (const r of rows) {
|
||||
if (r.outstanding) {
|
||||
outstandingCount++;
|
||||
continue;
|
||||
}
|
||||
totals.set(
|
||||
r.currency,
|
||||
(totals.get(r.currency) ?? new Prisma.Decimal(0)).plus(r.amount),
|
||||
);
|
||||
}
|
||||
|
||||
const totalsOut: Record<string, string | number> = {
|
||||
movimientos: rows.length,
|
||||
};
|
||||
for (const [currency, sum] of totals) {
|
||||
totalsOut[`total ${currency}`] = sum.toFixed(2);
|
||||
}
|
||||
if (outstandingCount) totalsOut["sin fondos"] = outstandingCount;
|
||||
|
||||
return {
|
||||
rows: rows.map((r) => ({
|
||||
customerName: nameOf(r.customer),
|
||||
reference: r.reference ?? "—",
|
||||
period: r.period ?? "—",
|
||||
concept: r.type?.nameEs || r.type?.nameEn || "Sin clasificar",
|
||||
transactionDate: r.transactionDate.toISOString().slice(0, 10),
|
||||
status: r.outstanding ? "Sin fondos" : "Pagado",
|
||||
amount: r.amount.toFixed(2),
|
||||
currency: r.currency,
|
||||
})),
|
||||
totals: totalsOut,
|
||||
subtitle: `Cheque ${checkNumber} · ${rows.length} movimientos`,
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------ export */
|
||||
|
||||
export const REPORTS: ReportDef[] = [
|
||||
@@ -959,6 +1064,7 @@ export const REPORTS: ReportDef[] = [
|
||||
vigente,
|
||||
avisoRenovacion,
|
||||
edoCuentaDatos,
|
||||
chequeCount,
|
||||
];
|
||||
|
||||
export function findReport(slug: string): ReportDef | undefined {
|
||||
|
||||
Reference in New Issue
Block a user