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>
223 lines
6.6 KiB
TypeScript
223 lines
6.6 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
Param,
|
|
Post,
|
|
Query,
|
|
Req,
|
|
UseGuards,
|
|
} from "@nestjs/common";
|
|
import { TransactionDomain } from "@jorgecuadros/database";
|
|
import { Request } from "express";
|
|
import { AuthenticatedGuard } from "../auth/authenticated.guard";
|
|
import { AbilityGuard } from "../auth/ability.guard";
|
|
import { RequireAbility } from "../auth/require-ability.decorator";
|
|
import { AuditService } from "../common/audit.service";
|
|
import {
|
|
BalanceFilter,
|
|
BalanceSort,
|
|
BillingService,
|
|
LedgerCurrency,
|
|
LedgerDirection,
|
|
MovementSort,
|
|
} from "./billing.service";
|
|
import {
|
|
BatchCreateDto,
|
|
CreateMovementDto,
|
|
ResolveOutstandingDto,
|
|
} from "./movement.dto";
|
|
|
|
const DOMAINS: TransactionDomain[] = ["UTILITY", "INSURANCE", "TRUST"];
|
|
const CURRENCIES: LedgerCurrency[] = ["MXN", "USD"];
|
|
const DIRECTIONS: LedgerDirection[] = ["charge", "credit"];
|
|
const BALANCES: BalanceFilter[] = ["all", "owing", "credit", "settled"];
|
|
const MOVEMENT_SORTS: MovementSort[] = [
|
|
"date_desc",
|
|
"date_asc",
|
|
"amount_desc",
|
|
"amount_asc",
|
|
"customer",
|
|
];
|
|
const BALANCE_SORTS: BalanceSort[] = [
|
|
"owing_desc",
|
|
"credit_desc",
|
|
"recent",
|
|
"customer",
|
|
];
|
|
|
|
function one<T>(allowed: T[], value: string | undefined): T | undefined {
|
|
return allowed.includes(value as T) ? (value as T) : undefined;
|
|
}
|
|
|
|
/** Tri-state query flag: "true"/"false" filter, anything else means no filter. */
|
|
function flag(v: string | undefined): boolean | undefined {
|
|
return v === "true" ? true : v === "false" ? false : undefined;
|
|
}
|
|
|
|
/** A `YYYY-MM-DD` bound; anything unparseable is treated as absent. */
|
|
function parseDate(v: string | undefined, endOfDay = false): Date | undefined {
|
|
if (!v) return undefined;
|
|
const d = new Date(endOfDay ? `${v}T23:59:59.999Z` : `${v}T00:00:00.000Z`);
|
|
return Number.isNaN(d.getTime()) ? undefined : d;
|
|
}
|
|
|
|
@UseGuards(AuthenticatedGuard, AbilityGuard)
|
|
@Controller("billing")
|
|
export class BillingController {
|
|
constructor(
|
|
private readonly billing: BillingService,
|
|
private readonly audit: AuditService,
|
|
) {}
|
|
|
|
private actingId(req: Request): string {
|
|
return (req.user as { id: string }).id;
|
|
}
|
|
|
|
@Get("stats")
|
|
stats() {
|
|
return this.billing.stats();
|
|
}
|
|
|
|
@Get("facets")
|
|
facets() {
|
|
return this.billing.facets();
|
|
}
|
|
|
|
/** Per-customer balances — the receivables worklist. */
|
|
@Get("balances")
|
|
balances(
|
|
@Query("query") query?: string,
|
|
@Query("page") page?: string,
|
|
@Query("pageSize") pageSize?: string,
|
|
@Query("currency") currency?: string,
|
|
@Query("balance") balance?: string,
|
|
@Query("domain") domain?: string,
|
|
@Query("sort") sort?: string,
|
|
) {
|
|
return this.billing.balances({
|
|
query,
|
|
page: Math.max(1, Number(page) || 1),
|
|
pageSize: Math.min(100, Math.max(1, Number(pageSize) || 25)),
|
|
currency: one(CURRENCIES, currency) ?? "MXN",
|
|
balance: one(BALANCES, balance) ?? "all",
|
|
domain: one(DOMAINS, domain),
|
|
sort: one(BALANCE_SORTS, sort) ?? "owing_desc",
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Every movement cut against one check, with its total — the reconciliation
|
|
* view replacing the legacy REPORTE CHEQUE COUNT. Declared before the
|
|
* `customers/:id` and `:id`-shaped routes so the literal path wins.
|
|
*/
|
|
@Get("by-check")
|
|
byCheck(@Query("checkNumber") checkNumber?: string) {
|
|
const n = checkNumber?.trim();
|
|
if (!n) throw new BadRequestException("checkNumber es obligatorio");
|
|
return this.billing.byCheck(n);
|
|
}
|
|
|
|
/** One customer's full statement across both business lines. */
|
|
@Get("customers/:id")
|
|
statement(@Param("id") id: string) {
|
|
return this.billing.statement(id);
|
|
}
|
|
|
|
/** Cross-customer movement browser. */
|
|
@Get()
|
|
movements(
|
|
@Query("query") query?: string,
|
|
@Query("page") page?: string,
|
|
@Query("pageSize") pageSize?: string,
|
|
@Query("domain") domain?: string,
|
|
@Query("currency") currency?: string,
|
|
@Query("direction") direction?: string,
|
|
@Query("typeId") typeId?: string,
|
|
@Query("source") source?: string,
|
|
@Query("customerId") customerId?: string,
|
|
@Query("outstanding") outstanding?: string,
|
|
@Query("checkNumber") checkNumber?: string,
|
|
@Query("from") from?: string,
|
|
@Query("to") to?: string,
|
|
@Query("sort") sort?: string,
|
|
) {
|
|
return this.billing.movements({
|
|
query,
|
|
page: Math.max(1, Number(page) || 1),
|
|
pageSize: Math.min(100, Math.max(1, Number(pageSize) || 25)),
|
|
domain: one(DOMAINS, domain),
|
|
currency: one(CURRENCIES, currency),
|
|
direction: one(DIRECTIONS, direction),
|
|
typeId: typeId || undefined,
|
|
source: source || undefined,
|
|
customerId: customerId || undefined,
|
|
outstanding: flag(outstanding),
|
|
checkNumber: checkNumber?.trim() || undefined,
|
|
from: parseDate(from),
|
|
to: parseDate(to, true),
|
|
sort: one(MOVEMENT_SORTS, sort) ?? "date_desc",
|
|
});
|
|
}
|
|
|
|
// --- writes ---------------------------------------------------------------
|
|
|
|
@Post()
|
|
@RequireAbility("ledger:create")
|
|
async create(@Body() dto: CreateMovementDto, @Req() req: Request) {
|
|
const tx = await this.billing.createMovement(dto);
|
|
void this.audit.log(this.actingId(req), "ledger.create", {
|
|
transactionId: tx.id,
|
|
customerId: dto.customerId,
|
|
amount: dto.amount,
|
|
currency: tx.currency,
|
|
});
|
|
return tx;
|
|
}
|
|
|
|
/**
|
|
* Batch capture: many customers' receipts against one physical check.
|
|
* Same ability as single capture — batching is still capturing.
|
|
*/
|
|
@Post("batch")
|
|
@RequireAbility("ledger:create")
|
|
async createBatch(@Body() dto: BatchCreateDto, @Req() req: Request) {
|
|
const result = await this.billing.createBatch(dto);
|
|
void this.audit.log(this.actingId(req), "ledger.batch", {
|
|
checkNumber: dto.checkNumber,
|
|
count: result.count,
|
|
total: result.total,
|
|
currency: result.currency,
|
|
});
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Resolve an outstanding (NOPAGO) row — `ledger:create`, not `ledger:void`:
|
|
* resolving completes a capture, it doesn't reverse one.
|
|
*/
|
|
@Post(":id/resolve-outstanding")
|
|
@RequireAbility("ledger:create")
|
|
async resolveOutstanding(
|
|
@Param("id") id: string,
|
|
@Body() dto: ResolveOutstandingDto,
|
|
@Req() req: Request,
|
|
) {
|
|
const tx = await this.billing.resolveOutstanding(id, dto);
|
|
void this.audit.log(this.actingId(req), "ledger.resolve-outstanding", {
|
|
transactionId: id,
|
|
checkNumber: dto.checkNumber,
|
|
});
|
|
return tx;
|
|
}
|
|
|
|
@Post(":id/void")
|
|
@RequireAbility("ledger:void")
|
|
async void(@Param("id") id: string, @Req() req: Request) {
|
|
const tx = await this.billing.voidMovement(id, this.actingId(req));
|
|
void this.audit.log(this.actingId(req), "ledger.void", { transactionId: id });
|
|
return tx;
|
|
}
|
|
}
|