Facturas de cliente
This commit is contained in:
parent
4c4afe2b3a
commit
2c695ee8a6
@ -43,7 +43,7 @@ export class IssueProformaUseCase {
|
||||
|
||||
const proformaId = idOrError.data;
|
||||
const presenter = this.presenterRegistry.getPresenter({
|
||||
resource: "proforma",
|
||||
resource: "issued-invoice",
|
||||
projection: "FULL",
|
||||
}) as ProformaFullPresenter;
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@ import {
|
||||
type ValidationErrorDetail,
|
||||
extractOrPushError,
|
||||
maybeFromNullableVO,
|
||||
toNullable,
|
||||
toEmptyString,
|
||||
} from "@repo/rdx-ddd";
|
||||
import { Maybe, Result } from "@repo/rdx-utils";
|
||||
|
||||
@ -120,10 +120,10 @@ export class CustomerInvoiceVerifactuDomainMapper
|
||||
id: UniqueID.generateNewID().toPrimitive(),
|
||||
invoice_id: parent.id.toPrimitive(),
|
||||
estado: VerifactuRecordEstado.createPendiente().toPrimitive(),
|
||||
qr: null,
|
||||
url: null,
|
||||
uuid: null,
|
||||
operacion: null,
|
||||
qr: "",
|
||||
url: "",
|
||||
uuid: "",
|
||||
operacion: "",
|
||||
});
|
||||
}
|
||||
|
||||
@ -133,10 +133,10 @@ export class CustomerInvoiceVerifactuDomainMapper
|
||||
id: verifactu.id.toPrimitive(),
|
||||
invoice_id: parent.id.toPrimitive(),
|
||||
estado: verifactu.estado.toPrimitive(),
|
||||
qr: toNullable(verifactu.qrCode, (v) => v),
|
||||
url: toNullable(verifactu.url, (v) => v.toPrimitive()),
|
||||
uuid: toNullable(verifactu.uuid, (v) => v),
|
||||
operacion: toNullable(verifactu.operacion, (v) => v),
|
||||
qr: toEmptyString(verifactu.qrCode, (v) => v),
|
||||
url: toEmptyString(verifactu.url, (v) => v.toPrimitive()),
|
||||
uuid: toEmptyString(verifactu.uuid, (v) => v),
|
||||
operacion: toEmptyString(verifactu.operacion, (v) => v),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -76,18 +76,47 @@ export class CustomerInvoiceRepository
|
||||
const mapper: ICustomerInvoiceDomainMapper = this._registry.getDomainMapper({
|
||||
resource: "customer-invoice",
|
||||
});
|
||||
const dto = mapper.mapToPersistence(invoice);
|
||||
const dtoResult = mapper.mapToPersistence(invoice);
|
||||
|
||||
if (dto.isFailure) {
|
||||
return Result.fail(dto.error);
|
||||
console.log("DTO to persist:", dtoResult);
|
||||
|
||||
if (dtoResult.isFailure) {
|
||||
return Result.fail(dtoResult.error);
|
||||
}
|
||||
|
||||
const { data } = dto;
|
||||
const dto = dtoResult.data;
|
||||
const { id, items, taxes, verifactu, ...createPayload } = dto;
|
||||
|
||||
await CustomerInvoiceModel.create(data, {
|
||||
include: [{ all: true }],
|
||||
transaction,
|
||||
});
|
||||
// 1. Insertar cabecera
|
||||
await CustomerInvoiceModel.create(
|
||||
{
|
||||
...createPayload,
|
||||
id,
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
|
||||
// 2. Inserta taxes de cabecera
|
||||
if (Array.isArray(taxes) && taxes.length > 0) {
|
||||
await CustomerInvoiceTaxModel.bulkCreate(taxes, { transaction });
|
||||
}
|
||||
|
||||
// 3. Inserta items + sus taxes
|
||||
if (Array.isArray(items) && items.length > 0) {
|
||||
for (const item of items) {
|
||||
const { taxes: itemTaxes, ...itemData } = item;
|
||||
await CustomerInvoiceItemModel.create(itemData, { transaction });
|
||||
|
||||
if (Array.isArray(itemTaxes) && itemTaxes.length > 0) {
|
||||
await CustomerInvoiceItemTaxModel.bulkCreate(itemTaxes, { transaction });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Inserta VerifactuRecord si existe
|
||||
if (verifactu) {
|
||||
await VerifactuRecordModel.create(verifactu, { transaction });
|
||||
}
|
||||
|
||||
return Result.ok();
|
||||
} catch (err: unknown) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user