feat(clientes): alta de cliente in one screen, opening balance as a payment
The 2026-10-08 ask: open an account without four screens — cliente, propiedad, servicios and the saldo inicial, taken as the first payment. POST /customers/onboard writes it all in one Prisma transaction; a bad service or a taken folder number leaves nothing behind (rolled back on dev). A cash opening balance goes through the EFE receipt book inside that same transaction, so it gets its folio and its C<folio> credit; a check posts a CHECK DEPOSIT against the number given. /clientes/alta pre-fills the next free folder number and links the receipt for printing. The old customer-only form stays as "Solo cliente". Built without the screen sketch from the meeting (it never arrived); the fields are the legacy ones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ca76f4d57a
commit
6468811955
11 files changed
+568
-8
No files matched your search
@@ -1,8 +1,15 @@
|
||||
import { ConflictException, Injectable, NotFoundException } from "@nestjs/common";
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from "@nestjs/common";
|
||||
import { Prisma } from "@jorgecuadros/database";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { CreateCustomerDto } from "./create-customer.dto";
|
||||
import { UpdateCustomerDto } from "./update-customer.dto";
|
||||
import type { OnboardCustomerDto } from "./onboard-customer.dto";
|
||||
import { CashReceiptsService } from "../cash-receipts/cash-receipts.service";
|
||||
import {
|
||||
BALANCE_FORWARD_TYPE,
|
||||
notCashJournal,
|
||||
@@ -51,7 +58,10 @@ function toDate(v?: string): Date | null | undefined {
|
||||
|
||||
@Injectable()
|
||||
export class CustomersService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly cashReceipts: CashReceiptsService,
|
||||
) {}
|
||||
|
||||
/** Unified customer list with search + business-line filter, paginated. */
|
||||
async list({ query, page, pageSize, line, includeArchived }: ListParams) {
|
||||
@@ -259,6 +269,81 @@ export class CustomersService {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Open an account from one screen: customer, property, services and the
|
||||
* opening balance taken as the first payment. One database transaction — a
|
||||
* half-created account (a customer with no property, a payment with no
|
||||
* customer) is worse than none, because the office would key it again.
|
||||
*/
|
||||
async onboard(dto: OnboardCustomerDto, userId: string) {
|
||||
const pay = dto.openingPayment;
|
||||
if (pay) {
|
||||
if (!(pay.amount > 0)) throw new BadRequestException("El saldo inicial debe ser mayor a cero.");
|
||||
if (pay.method === "CHECK" && !pay.checkNumber?.trim()) {
|
||||
throw new BadRequestException("Falta el número de cheque del saldo inicial.");
|
||||
}
|
||||
}
|
||||
if (dto.services?.length && !dto.property) {
|
||||
throw new BadRequestException("Los servicios necesitan una propiedad.");
|
||||
}
|
||||
await this.assertFolderFree(dto.customer.folderNumber);
|
||||
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
const customer = await tx.customer.create({
|
||||
data: { ...this.toData(dto.customer), name: dto.customer.name, nameMissing: false },
|
||||
});
|
||||
|
||||
const property = dto.property
|
||||
? await tx.property.create({ data: { ...dto.property, customerId: customer.id } })
|
||||
: null;
|
||||
if (property && dto.services?.length) {
|
||||
await tx.propertyService.createMany({
|
||||
data: dto.services.map((s) => ({ ...s, propertyId: property.id })),
|
||||
});
|
||||
}
|
||||
|
||||
const concept = pay?.concept?.trim() || "SALDO INICIAL";
|
||||
let receipt = null;
|
||||
let payment = null;
|
||||
if (pay?.method === "CASH") {
|
||||
receipt = await this.cashReceipts.create(
|
||||
{
|
||||
customerId: customer.id,
|
||||
receivedAt: pay.receivedAt,
|
||||
amount: pay.amount,
|
||||
currency: pay.currency,
|
||||
exchangeRate: pay.exchangeRate,
|
||||
concept,
|
||||
},
|
||||
userId,
|
||||
tx,
|
||||
);
|
||||
} else if (pay?.method === "CHECK") {
|
||||
const date = new Date(pay.receivedAt);
|
||||
if (isNaN(date.getTime())) throw new BadRequestException("Fecha del saldo inicial inválida.");
|
||||
const type = await tx.typeTransaction.findFirst({
|
||||
where: { nameEn: "CHECK DEPOSIT" },
|
||||
select: { id: true },
|
||||
});
|
||||
payment = await tx.transaction.create({
|
||||
data: {
|
||||
customerId: customer.id,
|
||||
domain: "UTILITY",
|
||||
typeId: type?.id,
|
||||
transactionDate: date,
|
||||
amount: new Prisma.Decimal(pay.amount.toFixed(2)),
|
||||
currency: pay.currency,
|
||||
checkNumber: pay.checkNumber!.trim(),
|
||||
message: concept,
|
||||
captureSource: "MANUAL",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return { customer, property, receipt, payment };
|
||||
});
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateCustomerDto) {
|
||||
const current = await this.ensureExists(id);
|
||||
// An archived customer's number is history, not a claim on the shelf.
|
||||
|
||||
Reference in new issue
Block a user