From 2157ddeaa371e86a4537c437ec92e6e76f2e7d0e Mon Sep 17 00:00:00 2001 From: david Date: Tue, 17 Feb 2026 11:35:07 +0100 Subject: [PATCH] . --- .../dtos/issued-invoice-list.dto.ts | 6 +- .../issued-invoice-repository.interface.ts | 7 ++ .../services/issued-invoice-finder.ts | 24 +++--- ...proforma-to-issued-invoice-materializer.ts | 2 +- .../issued-invoice-full-snapshot-builder.ts | 14 ++-- .../issued-invoice-full-snapshot.interface.ts | 14 ++-- ...ed-invoice-item-full-snapshot.interface.ts | 2 +- ...ued-invoice-items-full-snapshot-builder.ts | 31 ++++---- ...invoice-recipient-full-snapshot-builder.ts | 9 ++- ...nvoice-recipient-full-snapshot.interfce.ts | 2 +- ...invoice-verifactu-full-snapshot-builder.ts | 9 ++- ...voice-verifactu-full-snapshot.interface.ts | 2 +- ...sued-invoice-list-item-snapshot-builder.ts | 12 +-- ...ed-invoice-list-item-snapshot.interface.ts | 5 +- .../proformas/di/proforma-creator.di.ts | 10 +-- .../proformas/dtos/proforma-list.dto.ts | 6 +- .../application/proformas/mappers/index.ts | 2 +- .../full/proforma-full-snapshot-builder.ts | 2 +- .../proforma-list-item-snapshot.interface.ts | 2 +- .../snapshot-builders/report/index.ts | 1 + .../proformas/proforma.full.presenter.ts | 2 +- .../issued-invoices/aggregates/index.ts | 1 + .../issued-invoice.aggregate.ts | 36 ++++++++- .../domain/issued-invoices/entities/index.ts | 4 +- .../issued-invoice-tax-group.entity.ts | 35 --------- .../entities/issued-invoice-tax.entity.ts | 73 +++++++++++++++++++ .../issued-invoice-taxes.collection.ts | 13 ++++ .../entities/issued-invoice-taxes.entity.ts | 13 ---- .../domain/issued-invoices/errors/index.ts | 0 .../src/api/domain/issued-invoices/index.ts | 3 +- .../issued-invoices/value-objects/index.ts | 1 - ...e-tax-group.vo.ts => invoice-tax-group.vo} | 0 .../api/domain/proformas/aggregates/index.ts | 1 + .../{ => aggregates}/proforma.aggregate.ts | 19 ++--- .../api/domain/proformas/entities/index.ts | 1 + .../{ => entities}/proforma-items/index.ts | 0 .../proforma-items/proforma-item.entity.ts | 2 +- .../proforma-items.collection.ts | 2 +- .../src/api/domain/proformas/errors/index.ts | 0 .../src/api/domain/proformas/index.ts | 5 +- modules/customer-invoices/src/api/index.ts | 2 +- .../mappers/domain/customer-invoice.mapper.ts | 4 +- .../models/customer-invoice.model.ts | 72 +++++++++++++----- .../issued-invoices/controllers/index.ts | 3 - .../express/issued-invoices/index.ts | 3 +- .../issued-invoices/issued-invoices.routes.ts | 9 ++- .../express/proformas/controllers/index.ts | 8 -- .../infrastructure/express/proformas/index.ts | 3 +- .../proformas/proformas-api-error-mapper.ts | 2 +- .../express/proformas/proformas.routes.ts | 3 +- .../di/issued-invoice-repositories.di.ts | 28 +++---- .../get-issued-invoice-by-id.controller.ts | 6 +- .../issued-invoices/express/index.ts | 3 + .../list-issued-invoices.controller.ts | 6 +- .../report-issued-invoice.controller.ts | 4 +- .../infrastructure/issued-invoices/index.ts | 1 + .../persistence/sequelize/index.ts | 1 - .../sequelize-issued-invoice-domain.mapper.ts | 38 +++++----- ...ze-issued-invoice-recipient.list.mapper.ts | 5 +- .../sequelize-issued-invoice.list.mapper.ts | 25 ++----- .../repositories/issued-invoice.repository.ts | 38 +++++++++- .../proformas/di/proformas.di.ts | 51 +++---------- .../proforma-document-pipeline-factory.ts | 3 +- .../documents/post-processors/index.ts | 1 - .../change-status-proforma.controller.ts | 4 +- .../express}/create-proforma.controller.ts | 6 +- .../express}/delete-proforma.controller.ts | 4 +- .../express}/get-proforma.controller.ts | 4 +- .../infrastructure/proformas/express/index.ts | 8 ++ .../express}/issue-proforma.controller.ts | 4 +- .../express}/list-proformas.controller.ts | 4 +- .../express}/report-proforma.controller.ts | 4 +- .../express}/update-proforma.controller.ts | 6 +- .../src/api/infrastructure/proformas/index.ts | 1 + .../sequelize-proforma-domain.mapper.ts | 16 ++-- .../sequelize-proforma-taxes-domain.mapper.ts | 4 +- .../list/sequelize-proforma.list.mapper.ts | 23 ++---- 77 files changed, 427 insertions(+), 353 deletions(-) create mode 100644 modules/customer-invoices/src/api/domain/issued-invoices/aggregates/index.ts rename modules/customer-invoices/src/api/domain/issued-invoices/{ => aggregates}/issued-invoice.aggregate.ts (83%) delete mode 100644 modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-tax-group.entity.ts create mode 100644 modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-tax.entity.ts create mode 100644 modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-taxes.collection.ts delete mode 100644 modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-taxes.entity.ts create mode 100644 modules/customer-invoices/src/api/domain/issued-invoices/errors/index.ts rename modules/customer-invoices/src/api/domain/issued-invoices/value-objects/{invoice-tax-group.vo.ts => invoice-tax-group.vo} (100%) create mode 100644 modules/customer-invoices/src/api/domain/proformas/aggregates/index.ts rename modules/customer-invoices/src/api/domain/proformas/{ => aggregates}/proforma.aggregate.ts (93%) create mode 100644 modules/customer-invoices/src/api/domain/proformas/entities/index.ts rename modules/customer-invoices/src/api/domain/proformas/{ => entities}/proforma-items/index.ts (100%) rename modules/customer-invoices/src/api/domain/proformas/{ => entities}/proforma-items/proforma-item.entity.ts (99%) rename modules/customer-invoices/src/api/domain/proformas/{ => entities}/proforma-items/proforma-items.collection.ts (99%) create mode 100644 modules/customer-invoices/src/api/domain/proformas/errors/index.ts delete mode 100644 modules/customer-invoices/src/api/infrastructure/express/issued-invoices/controllers/index.ts delete mode 100644 modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/index.ts rename modules/customer-invoices/src/api/infrastructure/{express/issued-invoices/controllers => issued-invoices/express}/get-issued-invoice-by-id.controller.ts (83%) create mode 100644 modules/customer-invoices/src/api/infrastructure/issued-invoices/express/index.ts rename modules/customer-invoices/src/api/infrastructure/{express/issued-invoices/controllers => issued-invoices/express}/list-issued-invoices.controller.ts (85%) rename modules/customer-invoices/src/api/infrastructure/{express/issued-invoices/controllers => issued-invoices/express}/report-issued-invoice.controller.ts (89%) delete mode 100644 modules/customer-invoices/src/api/infrastructure/proformas/documents/post-processors/index.ts rename modules/customer-invoices/src/api/infrastructure/{express/proformas/controllers => proformas/express}/change-status-proforma.controller.ts (87%) rename modules/customer-invoices/src/api/infrastructure/{express/proformas/controllers => proformas/express}/create-proforma.controller.ts (78%) rename modules/customer-invoices/src/api/infrastructure/{express/proformas/controllers => proformas/express}/delete-proforma.controller.ts (85%) rename modules/customer-invoices/src/api/infrastructure/{express/proformas/controllers => proformas/express}/get-proforma.controller.ts (84%) create mode 100644 modules/customer-invoices/src/api/infrastructure/proformas/express/index.ts rename modules/customer-invoices/src/api/infrastructure/{express/proformas/controllers => proformas/express}/issue-proforma.controller.ts (85%) rename modules/customer-invoices/src/api/infrastructure/{express/proformas/controllers => proformas/express}/list-proformas.controller.ts (89%) rename modules/customer-invoices/src/api/infrastructure/{express/proformas/controllers => proformas/express}/report-proforma.controller.ts (86%) rename modules/customer-invoices/src/api/infrastructure/{express/proformas/controllers => proformas/express}/update-proforma.controller.ts (80%) diff --git a/modules/customer-invoices/src/api/application/issued-invoices/dtos/issued-invoice-list.dto.ts b/modules/customer-invoices/src/api/application/issued-invoices/dtos/issued-invoice-list.dto.ts index ef7dbf2f..1a9fc20f 100644 --- a/modules/customer-invoices/src/api/application/issued-invoices/dtos/issued-invoice-list.dto.ts +++ b/modules/customer-invoices/src/api/application/issued-invoices/dtos/issued-invoice-list.dto.ts @@ -1,4 +1,4 @@ -import type { CurrencyCode, LanguageCode, Percentage, UniqueID, UtcDate } from "@repo/rdx-ddd"; +import type { CurrencyCode, LanguageCode, UniqueID, UtcDate } from "@repo/rdx-ddd"; import type { Maybe } from "@repo/rdx-utils"; import type { @@ -31,10 +31,8 @@ export type IssuedInvoiceListDTO = { languageCode: LanguageCode; currencyCode: CurrencyCode; - discountPercentage: Percentage; - subtotalAmount: InvoiceAmount; - discountAmount: InvoiceAmount; + totalDiscountAmount: InvoiceAmount; taxableAmount: InvoiceAmount; taxesAmount: InvoiceAmount; totalAmount: InvoiceAmount; diff --git a/modules/customer-invoices/src/api/application/issued-invoices/repositories/issued-invoice-repository.interface.ts b/modules/customer-invoices/src/api/application/issued-invoices/repositories/issued-invoice-repository.interface.ts index 5d1d314a..c117ccf3 100644 --- a/modules/customer-invoices/src/api/application/issued-invoices/repositories/issued-invoice-repository.interface.ts +++ b/modules/customer-invoices/src/api/application/issued-invoices/repositories/issued-invoice-repository.interface.ts @@ -14,6 +14,13 @@ export interface IIssuedInvoiceRepository { transaction: unknown ): Promise>; + existsByIdInCompany( + companyId: UniqueID, + id: UniqueID, + transaction: unknown, + options: unknown + ): Promise>; + findByCriteriaInCompany( companyId: UniqueID, criteria: Criteria, diff --git a/modules/customer-invoices/src/api/application/issued-invoices/services/issued-invoice-finder.ts b/modules/customer-invoices/src/api/application/issued-invoices/services/issued-invoice-finder.ts index 44e0bc98..64027bba 100644 --- a/modules/customer-invoices/src/api/application/issued-invoices/services/issued-invoice-finder.ts +++ b/modules/customer-invoices/src/api/application/issued-invoices/services/issued-invoice-finder.ts @@ -1,17 +1,18 @@ -import type { CustomerInvoiceListDTO } from "@erp/customer-invoices/api/infrastructure"; import type { Criteria } from "@repo/rdx-criteria/server"; import type { UniqueID } from "@repo/rdx-ddd"; import type { Collection, Result } from "@repo/rdx-utils"; import type { Transaction } from "sequelize"; -import type { ICustomerInvoiceRepository, Proforma } from "../../../domain"; +import type { IssuedInvoice } from "../../../domain"; +import type { IssuedInvoiceListDTO } from "../dtos"; +import type { IIssuedInvoiceRepository } from "../repositories"; export interface IIssuedInvoiceFinder { findIssuedInvoiceById( companyId: UniqueID, invoiceId: UniqueID, transaction?: Transaction - ): Promise>; + ): Promise>; issuedInvoiceExists( companyId: UniqueID, @@ -23,18 +24,18 @@ export interface IIssuedInvoiceFinder { companyId: UniqueID, criteria: Criteria, transaction?: Transaction - ): Promise, Error>>; + ): Promise, Error>>; } export class IssuedInvoiceFinder implements IIssuedInvoiceFinder { - constructor(private readonly repository: ICustomerInvoiceRepository) {} + constructor(private readonly repository: IIssuedInvoiceRepository) {} async findIssuedInvoiceById( companyId: UniqueID, invoiceId: UniqueID, transaction?: Transaction - ): Promise> { - return this.repository.getIssuedInvoiceByIdInCompany(companyId, invoiceId, transaction, {}); + ): Promise> { + return this.repository.getByIdInCompany(companyId, invoiceId, transaction); } async issuedInvoiceExists( @@ -51,12 +52,7 @@ export class IssuedInvoiceFinder implements IIssuedInvoiceFinder { companyId: UniqueID, criteria: Criteria, transaction?: Transaction - ): Promise, Error>> { - return this.repository.findIssuedInvoicesByCriteriaInCompany( - companyId, - criteria, - transaction, - {} - ); + ): Promise, Error>> { + return this.repository.findByCriteriaInCompany(companyId, criteria, transaction); } } diff --git a/modules/customer-invoices/src/api/application/issued-invoices/services/proforma-to-issued-invoice-materializer.ts b/modules/customer-invoices/src/api/application/issued-invoices/services/proforma-to-issued-invoice-materializer.ts index 6f428db3..e7541eca 100644 --- a/modules/customer-invoices/src/api/application/issued-invoices/services/proforma-to-issued-invoice-materializer.ts +++ b/modules/customer-invoices/src/api/application/issued-invoices/services/proforma-to-issued-invoice-materializer.ts @@ -44,7 +44,7 @@ export class ProformaToIssuedInvoiceMaterializer implements IProformaToIssuedInv languageCode: proforma.languageCode, currencyCode: proforma.currencyCode, paymentMethod: proforma.paymentMethod, - discountPercentage: proforma.discountPercentage, + discountPercentage: proforma.globalDiscountPercentage, items: new Collection(issuedItems), taxes: new Collection(issuedTaxes), diff --git a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-full-snapshot-builder.ts b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-full-snapshot-builder.ts index 07fb7784..c13a9221 100644 --- a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-full-snapshot-builder.ts +++ b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-full-snapshot-builder.ts @@ -1,15 +1,15 @@ import type { ISnapshotBuilder } from "@erp/core/api"; import { toEmptyString } from "@repo/rdx-ddd"; -import { InvoiceAmount, type Proforma } from "../../../../domain"; +import { InvoiceAmount, type IssuedInvoice } from "../../../../domain"; -import type { IssuedInvoiceFullSnapshot } from "./issued-invoice-full-snapshot.interface"; +import type { IIssuedInvoiceFullSnapshot } from "./issued-invoice-full-snapshot.interface"; import type { IIssuedInvoiceItemsFullSnapshotBuilder } from "./issued-invoice-items-full-snapshot-builder"; import type { IIssuedInvoiceRecipientFullSnapshotBuilder } from "./issued-invoice-recipient-full-snapshot-builder"; import type { IIssuedInvoiceVerifactuFullSnapshotBuilder } from "./issued-invoice-verifactu-full-snapshot-builder"; export interface IIssuedInvoiceFullSnapshotBuilder - extends ISnapshotBuilder {} + extends ISnapshotBuilder {} export class IssuedInvoiceFullSnapshotBuilder implements IIssuedInvoiceFullSnapshotBuilder { constructor( @@ -18,13 +18,11 @@ export class IssuedInvoiceFullSnapshotBuilder implements IIssuedInvoiceFullSnaps private readonly verifactuBuilder: IIssuedInvoiceVerifactuFullSnapshotBuilder ) {} - toOutput(invoice: Proforma): IssuedInvoiceFullSnapshot { + toOutput(invoice: IssuedInvoice): IIssuedInvoiceFullSnapshot { const items = this.itemsBuilder.toOutput(invoice.items); const recipient = this.recipientBuilder.toOutput(invoice); const verifactu = this.verifactuBuilder.toOutput(invoice); - const allAmounts = invoice.calculateAllAmounts(); - const payment = invoice.paymentMethod.match( (payment) => { const { id, payment_description } = payment.toObjectString(); @@ -40,7 +38,7 @@ export class IssuedInvoiceFullSnapshotBuilder implements IIssuedInvoiceFullSnaps let totalRecAmount = InvoiceAmount.zero(invoice.currencyCode.code); let totalRetentionAmount = InvoiceAmount.zero(invoice.currencyCode.code); - const invoiceTaxes = invoice.getTaxes().map((taxGroup) => { + const invoiceTaxes = invoice.taxes().map((taxGroup) => { const { ivaAmount, recAmount, retentionAmount, totalAmount } = taxGroup.calculateAmounts(); totalIvaAmount = totalIvaAmount.add(ivaAmount); @@ -109,7 +107,7 @@ export class IssuedInvoiceFullSnapshotBuilder implements IIssuedInvoiceFullSnaps subtotal_amount: allAmounts.subtotalAmount.toObjectString(), items_discount_amount: allAmounts.itemDiscountAmount.toObjectString(), - discount_percentage: invoice.discountPercentage.toObjectString(), + discount_percentage: invoice.globalDiscountPercentage.toObjectString(), discount_amount: allAmounts.globalDiscountAmount.toObjectString(), taxable_amount: allAmounts.taxableAmount.toObjectString(), diff --git a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-full-snapshot.interface.ts b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-full-snapshot.interface.ts index e7689aef..29e5ce99 100644 --- a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-full-snapshot.interface.ts +++ b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-full-snapshot.interface.ts @@ -1,8 +1,8 @@ -import type { IssuedInvoiceItemFullSnapshot } from "./issued-invoice-item-full-snapshot.interface"; -import type { IssuedInvoiceRecipientFullSnapshot } from "./issued-invoice-recipient-full-snapshot.interfce"; -import type { IssuedInvoiceVerifactuFullSnapshot } from "./issued-invoice-verifactu-full-snapshot.interface"; +import type { IIssuedInvoiceItemFullSnapshot } from "./issued-invoice-item-full-snapshot.interface"; +import type { IIssuedInvoiceRecipientFullSnapshot } from "./issued-invoice-recipient-full-snapshot.interfce"; +import type { IIssuedInvoiceVerifactuFullSnapshot } from "./issued-invoice-verifactu-full-snapshot.interface"; -export interface IssuedInvoiceFullSnapshot { +export interface IIssuedInvoiceFullSnapshot { id: string; company_id: string; @@ -22,7 +22,7 @@ export interface IssuedInvoiceFullSnapshot { currency_code: string; customer_id: string; - recipient: IssuedInvoiceRecipientFullSnapshot; + recipient: IIssuedInvoiceRecipientFullSnapshot; payment_method?: { payment_id: string; @@ -62,8 +62,8 @@ export interface IssuedInvoiceFullSnapshot { taxes_amount: { value: string; scale: string; currency_code: string }; }>; - verifactu: IssuedInvoiceVerifactuFullSnapshot; - items: IssuedInvoiceItemFullSnapshot[]; + verifactu: IIssuedInvoiceVerifactuFullSnapshot; + items: IIssuedInvoiceItemFullSnapshot[]; metadata?: Record; } diff --git a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-item-full-snapshot.interface.ts b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-item-full-snapshot.interface.ts index f8240fe1..f12bc9e7 100644 --- a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-item-full-snapshot.interface.ts +++ b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-item-full-snapshot.interface.ts @@ -1,4 +1,4 @@ -export interface IssuedInvoiceItemFullSnapshot { +export interface IIssuedInvoiceItemFullSnapshot { id: string; is_valued: string; position: string; diff --git a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-items-full-snapshot-builder.ts b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-items-full-snapshot-builder.ts index 02ff3f5c..e939ea8b 100644 --- a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-items-full-snapshot-builder.ts +++ b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-items-full-snapshot-builder.ts @@ -1,18 +1,17 @@ import type { ISnapshotBuilder } from "@erp/core/api"; import { toEmptyString } from "@repo/rdx-ddd"; -import type { CustomerInvoiceItems, IssuedInvoiceItem } from "../../../../domain"; -import type { IssuedInvoiceItemFullSnapshot } from "../../application-models"; +import type { IssuedInvoiceItem, IssuedInvoiceItems } from "../../../../domain"; + +import type { IIssuedInvoiceItemFullSnapshot } from "./issued-invoice-item-full-snapshot.interface"; export interface IIssuedInvoiceItemsFullSnapshotBuilder - extends ISnapshotBuilder {} + extends ISnapshotBuilder {} export class IssuedInvoiceItemsFullSnapshotBuilder implements IIssuedInvoiceItemsFullSnapshotBuilder { - private mapItem(invoiceItem: IssuedInvoiceItem, index: number): IssuedInvoiceItemFullSnapshot { - const allAmounts = invoiceItem.calculateAllAmounts(); - + private mapItem(invoiceItem: IssuedInvoiceItem, index: number): IIssuedInvoiceItemFullSnapshot { return { id: invoiceItem.id.toPrimitive(), is_valued: String(invoiceItem.isValued), @@ -29,23 +28,23 @@ export class IssuedInvoiceItemsFullSnapshotBuilder () => ({ value: "", scale: "", currency_code: "" }) ), - subtotal_amount: allAmounts.subtotalAmount.toObjectString(), + subtotal_amount: invoiceItem.subtotalAmount.toObjectString(), discount_percentage: invoiceItem.itemDiscountPercentage.match( (discountPercentage) => discountPercentage.toObjectString(), () => ({ value: "", scale: "" }) ), - discount_amount: allAmounts.itemDiscountAmount.toObjectString(), + discount_amount: invoiceItem.itemDiscountAmount.toObjectString(), global_discount_percentage: invoiceItem.globalDiscountPercentage.match( (discountPercentage) => discountPercentage.toObjectString(), () => ({ value: "", scale: "" }) ), - global_discount_amount: allAmounts.globalDiscountAmount.toObjectString(), + global_discount_amount: invoiceItem.globalDiscountAmount.toObjectString(), - taxable_amount: allAmounts.taxableAmount.toObjectString(), + taxable_amount: invoiceItem.taxableAmount.toObjectString(), iva_code: invoiceItem.taxes.iva.match( (iva) => iva.code, @@ -57,7 +56,7 @@ export class IssuedInvoiceItemsFullSnapshotBuilder () => ({ value: "", scale: "" }) ), - iva_amount: allAmounts.ivaAmount.toObjectString(), + iva_amount: invoiceItem.ivaAmount.toObjectString(), rec_code: invoiceItem.taxes.rec.match( (rec) => rec.code, @@ -69,7 +68,7 @@ export class IssuedInvoiceItemsFullSnapshotBuilder () => ({ value: "", scale: "" }) ), - rec_amount: allAmounts.recAmount.toObjectString(), + rec_amount: invoiceItem.recAmount.toObjectString(), retention_code: invoiceItem.taxes.retention.match( (retention) => retention.code, @@ -81,15 +80,15 @@ export class IssuedInvoiceItemsFullSnapshotBuilder () => ({ value: "", scale: "" }) ), - retention_amount: allAmounts.retentionAmount.toObjectString(), + retention_amount: invoiceItem.retentionAmount.toObjectString(), - taxes_amount: allAmounts.taxesAmount.toObjectString(), + taxes_amount: invoiceItem.taxesAmount.toObjectString(), - total_amount: allAmounts.totalAmount.toObjectString(), + total_amount: invoiceItem.totalAmount.toObjectString(), }; } - toOutput(invoiceItems: CustomerInvoiceItems): IssuedInvoiceItemFullSnapshot[] { + toOutput(invoiceItems: IssuedInvoiceItems): IIssuedInvoiceItemFullSnapshot[] { return invoiceItems.map((item, index) => this.mapItem(item, index)); } } diff --git a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-recipient-full-snapshot-builder.ts b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-recipient-full-snapshot-builder.ts index 468a1407..b269d5b3 100644 --- a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-recipient-full-snapshot-builder.ts +++ b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-recipient-full-snapshot-builder.ts @@ -1,16 +1,17 @@ import type { ISnapshotBuilder } from "@erp/core/api"; import { DomainValidationError, toEmptyString } from "@repo/rdx-ddd"; -import type { InvoiceRecipient, Proforma } from "../../../../domain"; -import type { IssuedInvoiceRecipientFullSnapshot } from "../../application-models"; +import type { InvoiceRecipient, IssuedInvoice } from "../../../../domain"; + +import type { IIssuedInvoiceRecipientFullSnapshot } from "./issued-invoice-recipient-full-snapshot.interfce"; export interface IIssuedInvoiceRecipientFullSnapshotBuilder - extends ISnapshotBuilder {} + extends ISnapshotBuilder {} export class IssuedInvoiceRecipientFullSnapshotBuilder implements IIssuedInvoiceRecipientFullSnapshotBuilder { - toOutput(invoice: Proforma): IssuedInvoiceRecipientFullSnapshot { + toOutput(invoice: IssuedInvoice): IIssuedInvoiceRecipientFullSnapshot { if (!invoice.recipient) { throw DomainValidationError.requiredValue("recipient", { cause: invoice, diff --git a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-recipient-full-snapshot.interfce.ts b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-recipient-full-snapshot.interfce.ts index 8c6c5d9d..c56cbb72 100644 --- a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-recipient-full-snapshot.interfce.ts +++ b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-recipient-full-snapshot.interfce.ts @@ -1,4 +1,4 @@ -export interface IssuedInvoiceRecipientFullSnapshot { +export interface IIssuedInvoiceRecipientFullSnapshot { id: string; name: string; tin: string; diff --git a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-verifactu-full-snapshot-builder.ts b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-verifactu-full-snapshot-builder.ts index ba8d499c..cc37ddda 100644 --- a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-verifactu-full-snapshot-builder.ts +++ b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-verifactu-full-snapshot-builder.ts @@ -1,16 +1,17 @@ import type { ISnapshotBuilder } from "@erp/core/api"; import { DomainValidationError } from "@repo/rdx-ddd"; -import type { Proforma } from "../../../../domain"; -import type { IssuedInvoiceVerifactuFullSnapshot } from "../../application-models"; +import type { IssuedInvoice } from "../../../../domain"; + +import type { IIssuedInvoiceVerifactuFullSnapshot } from "./issued-invoice-verifactu-full-snapshot.interface"; export interface IIssuedInvoiceVerifactuFullSnapshotBuilder - extends ISnapshotBuilder {} + extends ISnapshotBuilder {} export class IssuedInvoiceVerifactuFullSnapshotBuilder implements IIssuedInvoiceVerifactuFullSnapshotBuilder { - toOutput(invoice: Proforma): IssuedInvoiceVerifactuFullSnapshot { + toOutput(invoice: IssuedInvoice): IIssuedInvoiceVerifactuFullSnapshot { if (!invoice.verifactu) { throw DomainValidationError.requiredValue("verifactu", { cause: invoice, diff --git a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-verifactu-full-snapshot.interface.ts b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-verifactu-full-snapshot.interface.ts index cf656ff3..2209979a 100644 --- a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-verifactu-full-snapshot.interface.ts +++ b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/full/issued-invoice-verifactu-full-snapshot.interface.ts @@ -1,4 +1,4 @@ -export interface IssuedInvoiceVerifactuFullSnapshot { +export interface IIssuedInvoiceVerifactuFullSnapshot { id: string; status: string; url: string; diff --git a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/list/issued-invoice-list-item-snapshot-builder.ts b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/list/issued-invoice-list-item-snapshot-builder.ts index b3ba47c4..4507443c 100644 --- a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/list/issued-invoice-list-item-snapshot-builder.ts +++ b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/list/issued-invoice-list-item-snapshot-builder.ts @@ -1,14 +1,15 @@ import type { ISnapshotBuilder } from "@erp/core/api"; import { toEmptyString } from "@repo/rdx-ddd"; -import type { CustomerInvoiceListDTO } from "../../../../infrastructure"; -import type { IssuedInvoiceListItemSnapshot } from "../../application-models"; +import type { IssuedInvoiceListDTO } from "../../dtos"; + +import type { IIssuedInvoiceListItemSnapshot } from "./issued-invoice-list-item-snapshot.interface"; export interface IIssuedInvoiceListItemSnapshotBuilder - extends ISnapshotBuilder {} + extends ISnapshotBuilder {} export class IssuedInvoiceListItemSnapshotBuilder implements IIssuedInvoiceListItemSnapshotBuilder { - toOutput(invoice: CustomerInvoiceListDTO): IssuedInvoiceListItemSnapshot { + toOutput(invoice: IssuedInvoiceListDTO): IIssuedInvoiceListItemSnapshot { const recipient = invoice.recipient.toObjectString(); const verifactu = invoice.verifactu.match( @@ -42,8 +43,7 @@ export class IssuedInvoiceListItemSnapshotBuilder implements IIssuedInvoiceListI currency_code: invoice.currencyCode.code, subtotal_amount: invoice.subtotalAmount.toObjectString(), - discount_percentage: invoice.discountPercentage.toObjectString(), - discount_amount: invoice.discountAmount.toObjectString(), + total_discount_amount: invoice.totalDiscountAmount.toObjectString(), taxable_amount: invoice.taxableAmount.toObjectString(), taxes_amount: invoice.taxesAmount.toObjectString(), total_amount: invoice.totalAmount.toObjectString(), diff --git a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/list/issued-invoice-list-item-snapshot.interface.ts b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/list/issued-invoice-list-item-snapshot.interface.ts index c38d50fa..45fda903 100644 --- a/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/list/issued-invoice-list-item-snapshot.interface.ts +++ b/modules/customer-invoices/src/api/application/issued-invoices/snapshot-builders/list/issued-invoice-list-item-snapshot.interface.ts @@ -1,4 +1,4 @@ -export interface IssuedInvoiceListItemSnapshot { +export interface IIssuedInvoiceListItemSnapshot { id: string; company_id: string; is_proforma: boolean; @@ -30,8 +30,7 @@ export interface IssuedInvoiceListItemSnapshot { }; subtotal_amount: { value: string; scale: string; currency_code: string }; - discount_percentage: { value: string; scale: string }; - discount_amount: { value: string; scale: string; currency_code: string }; + total_discount_amount: { value: string; scale: string; currency_code: string }; taxable_amount: { value: string; scale: string; currency_code: string }; taxes_amount: { value: string; scale: string; currency_code: string }; total_amount: { value: string; scale: string; currency_code: string }; diff --git a/modules/customer-invoices/src/api/application/proformas/di/proforma-creator.di.ts b/modules/customer-invoices/src/api/application/proformas/di/proforma-creator.di.ts index d26ddf85..d1ad7238 100644 --- a/modules/customer-invoices/src/api/application/proformas/di/proforma-creator.di.ts +++ b/modules/customer-invoices/src/api/application/proformas/di/proforma-creator.di.ts @@ -1,14 +1,14 @@ -import type { ICustomerInvoiceRepository } from "../../../domain/repositories"; import { ProformaFactory } from "../factories"; +import type { IProformaRepository } from "../repositories"; import { type IProformaCreator, type IProformaNumberGenerator, ProformaCreator } from "../services"; -export function buildProformaCreator( +export const buildProformaCreator = ( numberService: IProformaNumberGenerator, - repository: ICustomerInvoiceRepository -): IProformaCreator { + repository: IProformaRepository +): IProformaCreator => { return new ProformaCreator({ numberService, factory: new ProformaFactory(), repository, }); -} +}; diff --git a/modules/customer-invoices/src/api/application/proformas/dtos/proforma-list.dto.ts b/modules/customer-invoices/src/api/application/proformas/dtos/proforma-list.dto.ts index 41eb8ef8..dc1bf449 100644 --- a/modules/customer-invoices/src/api/application/proformas/dtos/proforma-list.dto.ts +++ b/modules/customer-invoices/src/api/application/proformas/dtos/proforma-list.dto.ts @@ -1,4 +1,4 @@ -import type { CurrencyCode, LanguageCode, Percentage, UniqueID, UtcDate } from "@repo/rdx-ddd"; +import type { CurrencyCode, LanguageCode, UniqueID, UtcDate } from "@repo/rdx-ddd"; import type { Maybe } from "@repo/rdx-utils"; import type { @@ -30,10 +30,8 @@ export type ProformaListDTO = { languageCode: LanguageCode; currencyCode: CurrencyCode; - discountPercentage: Percentage; - subtotalAmount: InvoiceAmount; - discountAmount: InvoiceAmount; + totalDiscountAmount: InvoiceAmount; taxableAmount: InvoiceAmount; taxesAmount: InvoiceAmount; totalAmount: InvoiceAmount; diff --git a/modules/customer-invoices/src/api/application/proformas/mappers/index.ts b/modules/customer-invoices/src/api/application/proformas/mappers/index.ts index 8e6fc996..472cff61 100644 --- a/modules/customer-invoices/src/api/application/proformas/mappers/index.ts +++ b/modules/customer-invoices/src/api/application/proformas/mappers/index.ts @@ -1,4 +1,4 @@ export * from "./create-proforma-props.mapper"; export * from "./proforma-domain-mapper.interface"; export * from "./proforma-list-mapper.interface"; -export * from "./update-proforma-props.mapper"; +//export * from "./update-proforma-props.mapper"; diff --git a/modules/customer-invoices/src/api/application/proformas/snapshot-builders/full/proforma-full-snapshot-builder.ts b/modules/customer-invoices/src/api/application/proformas/snapshot-builders/full/proforma-full-snapshot-builder.ts index af718ef1..fa1a267f 100644 --- a/modules/customer-invoices/src/api/application/proformas/snapshot-builders/full/proforma-full-snapshot-builder.ts +++ b/modules/customer-invoices/src/api/application/proformas/snapshot-builders/full/proforma-full-snapshot-builder.ts @@ -106,7 +106,7 @@ export class ProformaFullSnapshotBuilder implements IProformaFullSnapshotBuilder subtotal_amount: allAmounts.subtotalAmount.toObjectString(), items_discount_amount: allAmounts.itemDiscountAmount.toObjectString(), - discount_percentage: invoice.discountPercentage.toObjectString(), + discount_percentage: invoice.globalDiscountPercentage.toObjectString(), discount_amount: allAmounts.globalDiscountAmount.toObjectString(), taxable_amount: allAmounts.taxableAmount.toObjectString(), diff --git a/modules/customer-invoices/src/api/application/proformas/snapshot-builders/list/proforma-list-item-snapshot.interface.ts b/modules/customer-invoices/src/api/application/proformas/snapshot-builders/list/proforma-list-item-snapshot.interface.ts index 02a5050a..c49a167f 100644 --- a/modules/customer-invoices/src/api/application/proformas/snapshot-builders/list/proforma-list-item-snapshot.interface.ts +++ b/modules/customer-invoices/src/api/application/proformas/snapshot-builders/list/proforma-list-item-snapshot.interface.ts @@ -1,4 +1,4 @@ -export interface ProformaListItemSnapshot { +export interface IProformaListItemSnapshot { id: string; company_id: string; is_proforma: boolean; diff --git a/modules/customer-invoices/src/api/application/proformas/snapshot-builders/report/index.ts b/modules/customer-invoices/src/api/application/proformas/snapshot-builders/report/index.ts index d1b38528..fc118ac6 100644 --- a/modules/customer-invoices/src/api/application/proformas/snapshot-builders/report/index.ts +++ b/modules/customer-invoices/src/api/application/proformas/snapshot-builders/report/index.ts @@ -2,5 +2,6 @@ export * from "./proforma-items-report-snapshot-builder"; export * from "./proforma-report-item-snapshot.interface"; export * from "./proforma-report-snapshot.interface"; export * from "./proforma-report-snapshot-builder"; +export * from "./proforma-report-snapshot-builder"; export * from "./proforma-report-tax-snapshot.interface"; export * from "./proforma-tax-report-snapshot-builder"; diff --git a/modules/customer-invoices/src/api/application/snapshot-builders/domain/proformas/proforma.full.presenter.ts b/modules/customer-invoices/src/api/application/snapshot-builders/domain/proformas/proforma.full.presenter.ts index 3e50d7d6..734af9c9 100644 --- a/modules/customer-invoices/src/api/application/snapshot-builders/domain/proformas/proforma.full.presenter.ts +++ b/modules/customer-invoices/src/api/application/snapshot-builders/domain/proformas/proforma.full.presenter.ts @@ -109,7 +109,7 @@ export class ProformaFullPresenter extends Presenter { IssuedInvoiceItems.create({ languageCode: props.languageCode, currencyCode: props.currencyCode, - globalDiscountPercentage: props.discountPercentage, + globalDiscountPercentage: props.globalDiscountPercentage, }); } @@ -170,8 +170,36 @@ export class IssuedInvoice extends AggregateRoot { return this.props.verifactu; } - public get discountPercentage(): Percentage { - return this.props.discountPercentage; + public get subtotalAmount(): InvoiceAmount { + return this.props.subtotalAmount; + } + + public get itemDiscountAmount(): InvoiceAmount { + return this.props.itemDiscountAmount; + } + + public get globalDiscountPercentage(): Percentage { + return this.props.globalDiscountPercentage; + } + + public get globalDiscountAmount(): InvoiceAmount { + return this.props.globalDiscountAmount; + } + + public get totalDiscountAmount(): InvoiceAmount { + return this.props.totalDiscountAmount; + } + + public get taxableAmount(): InvoiceAmount { + return this.props.taxableAmount; + } + + public get taxesAmount(): InvoiceAmount { + return this.props.taxesAmount; + } + + public get totalAmount(): InvoiceAmount { + return this.props.totalAmount; } public get taxes(): IssuedInvoiceTaxes { diff --git a/modules/customer-invoices/src/api/domain/issued-invoices/entities/index.ts b/modules/customer-invoices/src/api/domain/issued-invoices/entities/index.ts index 655ac636..cfbc7c8b 100644 --- a/modules/customer-invoices/src/api/domain/issued-invoices/entities/index.ts +++ b/modules/customer-invoices/src/api/domain/issued-invoices/entities/index.ts @@ -1,4 +1,4 @@ export * from "./issued-invoice-items"; -export * from "./issued-invoice-tax-group.entity"; -export * from "./issued-invoice-taxes.entity"; +export * from "./issued-invoice-tax.entity"; +export * from "./issued-invoice-taxes.collection"; export * from "./verifactu-record.entity"; diff --git a/modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-tax-group.entity.ts b/modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-tax-group.entity.ts deleted file mode 100644 index 382ed87f..00000000 --- a/modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-tax-group.entity.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { DomainEntity, type Percentage, type UniqueID } from "@repo/rdx-ddd"; -import { type Maybe, Result } from "@repo/rdx-utils"; - -import type { InvoiceAmount } from "../../common"; - -export type IssuedInvoiceTaxGroupProps = { - taxableAmount: InvoiceAmount; - - ivaCode: string; - ivaPercentage: Percentage; - ivaAmount: InvoiceAmount; - - recCode: Maybe; - recPercentage: Maybe; - recAmount: InvoiceAmount; - - retentionCode: Maybe; - retentionPercentage: Maybe; - retentionAmount: InvoiceAmount; - - totalAmount: InvoiceAmount; -}; - -export class IssuedInvoiceTaxGroup extends DomainEntity { - public static create( - props: IssuedInvoiceTaxGroupProps, - id?: UniqueID - ): Result { - return Result.ok(new IssuedInvoiceTaxGroup(props, id)); - } - - public getProps(): IssuedInvoiceTaxGroupProps { - return this.props; - } -} diff --git a/modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-tax.entity.ts b/modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-tax.entity.ts new file mode 100644 index 00000000..fa2683bc --- /dev/null +++ b/modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-tax.entity.ts @@ -0,0 +1,73 @@ +import { DomainEntity, type Percentage, type UniqueID } from "@repo/rdx-ddd"; +import { type Maybe, Result } from "@repo/rdx-utils"; + +import type { InvoiceAmount } from "../../common"; + +export type IssuedInvoiceTaxProps = { + taxableAmount: InvoiceAmount; + + ivaCode: string; + ivaPercentage: Percentage; + ivaAmount: InvoiceAmount; + + recCode: Maybe; + recPercentage: Maybe; + recAmount: InvoiceAmount; + + retentionCode: Maybe; + retentionPercentage: Maybe; + retentionAmount: InvoiceAmount; + + taxesAmount: InvoiceAmount; +}; + +export class IssuedInvoiceTax extends DomainEntity { + public static create( + props: IssuedInvoiceTaxProps, + id?: UniqueID + ): Result { + return Result.ok(new IssuedInvoiceTax(props, id)); + } + + public get taxableAmount(): InvoiceAmount { + return this.props.taxableAmount; + } + + public get ivaCode(): string { + return this.props.ivaCode; + } + public get ivaPercentage(): Percentage { + return this.props.ivaPercentage; + } + public get ivaAmount(): InvoiceAmount { + return this.props.ivaAmount; + } + + public get recCode(): Maybe { + return this.props.recCode; + } + public get recPercentage(): Maybe { + return this.props.recPercentage; + } + public get recAmount(): InvoiceAmount { + return this.props.recAmount; + } + + public get retentionCode(): Maybe { + return this.props.retentionCode; + } + public get retentionPercentage(): Maybe { + return this.props.retentionPercentage; + } + public get retentionAmount(): InvoiceAmount { + return this.props.retentionAmount; + } + + public get taxesAmount(): InvoiceAmount { + return this.props.taxesAmount; + } + + public getProps(): IssuedInvoiceTaxProps { + return this.props; + } +} diff --git a/modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-taxes.collection.ts b/modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-taxes.collection.ts new file mode 100644 index 00000000..ade4ce6d --- /dev/null +++ b/modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-taxes.collection.ts @@ -0,0 +1,13 @@ +import { Collection } from "@repo/rdx-utils"; + +import type { IssuedInvoiceTax } from "./issued-invoice-tax.entity"; + +export class IssuedInvoiceTaxes extends Collection { + constructor(items: IssuedInvoiceTax[] = []) { + super(items); + } + + public static create(items: IssuedInvoiceTax[] = []): IssuedInvoiceTaxes { + return new IssuedInvoiceTaxes(items); + } +} diff --git a/modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-taxes.entity.ts b/modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-taxes.entity.ts deleted file mode 100644 index a93d2d6a..00000000 --- a/modules/customer-invoices/src/api/domain/issued-invoices/entities/issued-invoice-taxes.entity.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Collection } from "@repo/rdx-utils"; - -import type { IssuedInvoiceTaxGroup } from "./issued-invoice-tax-group.entity"; - -export class IssuedInvoiceTaxes extends Collection { - constructor(items: IssuedInvoiceTaxGroup[] = []) { - super(items); - } - - public static create(items: IssuedInvoiceTaxGroup[] = []): IssuedInvoiceTaxes { - return new IssuedInvoiceTaxes(items); - } -} diff --git a/modules/customer-invoices/src/api/domain/issued-invoices/errors/index.ts b/modules/customer-invoices/src/api/domain/issued-invoices/errors/index.ts new file mode 100644 index 00000000..e69de29b diff --git a/modules/customer-invoices/src/api/domain/issued-invoices/index.ts b/modules/customer-invoices/src/api/domain/issued-invoices/index.ts index 867a6457..38d1c552 100644 --- a/modules/customer-invoices/src/api/domain/issued-invoices/index.ts +++ b/modules/customer-invoices/src/api/domain/issued-invoices/index.ts @@ -1,3 +1,4 @@ +export * from "./aggregates"; export * from "./entities"; -export * from "./issued-invoice.aggregate"; +export * from "./errors"; export * from "./value-objects"; diff --git a/modules/customer-invoices/src/api/domain/issued-invoices/value-objects/index.ts b/modules/customer-invoices/src/api/domain/issued-invoices/value-objects/index.ts index 3afe84c7..edb91e86 100644 --- a/modules/customer-invoices/src/api/domain/issued-invoices/value-objects/index.ts +++ b/modules/customer-invoices/src/api/domain/issued-invoices/value-objects/index.ts @@ -1,2 +1 @@ -export * from "./invoice-tax-group.vo"; export * from "./verifactu-status.vo"; diff --git a/modules/customer-invoices/src/api/domain/issued-invoices/value-objects/invoice-tax-group.vo.ts b/modules/customer-invoices/src/api/domain/issued-invoices/value-objects/invoice-tax-group.vo similarity index 100% rename from modules/customer-invoices/src/api/domain/issued-invoices/value-objects/invoice-tax-group.vo.ts rename to modules/customer-invoices/src/api/domain/issued-invoices/value-objects/invoice-tax-group.vo diff --git a/modules/customer-invoices/src/api/domain/proformas/aggregates/index.ts b/modules/customer-invoices/src/api/domain/proformas/aggregates/index.ts new file mode 100644 index 00000000..c8dae297 --- /dev/null +++ b/modules/customer-invoices/src/api/domain/proformas/aggregates/index.ts @@ -0,0 +1 @@ +export * from "./proforma.aggregate"; diff --git a/modules/customer-invoices/src/api/domain/proformas/proforma.aggregate.ts b/modules/customer-invoices/src/api/domain/proformas/aggregates/proforma.aggregate.ts similarity index 93% rename from modules/customer-invoices/src/api/domain/proformas/proforma.aggregate.ts rename to modules/customer-invoices/src/api/domain/proformas/aggregates/proforma.aggregate.ts index b7c7c1c0..291d7f38 100644 --- a/modules/customer-invoices/src/api/domain/proformas/proforma.aggregate.ts +++ b/modules/customer-invoices/src/api/domain/proformas/aggregates/proforma.aggregate.ts @@ -10,7 +10,7 @@ import { } from "@repo/rdx-ddd"; import { Collection, type Maybe, Result } from "@repo/rdx-utils"; -import type { InvoicePaymentMethod } from "../common/entities"; +import type { InvoicePaymentMethod } from "../../common/entities"; import { InvoiceAmount, type InvoiceNumber, @@ -19,9 +19,8 @@ import { type InvoiceStatus, InvoiceTaxGroup, type ItemAmount, -} from "../common/value-objects"; - -import { ProformaItems } from "./proforma-items"; +} from "../../common/value-objects"; +import { ProformaItems } from "../entities/proforma-items"; export type ProformaProps = { companyId: UniqueID; @@ -49,7 +48,7 @@ export type ProformaProps = { paymentMethod: Maybe; - discountPercentage: Percentage; + globalDiscountPercentage: Percentage; }; export type ProformaPatchProps = Partial> & { @@ -66,7 +65,7 @@ export class Proforma extends AggregateRoot { ProformaItems.create({ languageCode: props.languageCode, currencyCode: props.currencyCode, - globalDiscountPercentage: props.discountPercentage, + globalDiscountPercentage: props.globalDiscountPercentage, }); } @@ -177,8 +176,8 @@ export class Proforma extends AggregateRoot { return this.props.currencyCode; } - public get discountPercentage(): Percentage { - return this.props.discountPercentage; + public get globalDiscountPercentage(): Percentage { + return this.props.globalDiscountPercentage; } // Method to get the complete list of line items @@ -273,10 +272,6 @@ export class Proforma extends AggregateRoot { return this.calculateAllAmounts().totalAmount; } - /** - * @summary Agrupa impuestos a nivel factura usando el trío (iva|rec|ret), - * construyendo InvoiceTaxGroup desde los datos de los ítems. - */ /** * @summary Agrupa impuestos a nivel factura usando el trío (iva|rec|ret), * construyendo InvoiceTaxGroup desde los datos de los ítems. diff --git a/modules/customer-invoices/src/api/domain/proformas/entities/index.ts b/modules/customer-invoices/src/api/domain/proformas/entities/index.ts new file mode 100644 index 00000000..371c154f --- /dev/null +++ b/modules/customer-invoices/src/api/domain/proformas/entities/index.ts @@ -0,0 +1 @@ +export * from "./proforma-items"; diff --git a/modules/customer-invoices/src/api/domain/proformas/proforma-items/index.ts b/modules/customer-invoices/src/api/domain/proformas/entities/proforma-items/index.ts similarity index 100% rename from modules/customer-invoices/src/api/domain/proformas/proforma-items/index.ts rename to modules/customer-invoices/src/api/domain/proformas/entities/proforma-items/index.ts diff --git a/modules/customer-invoices/src/api/domain/proformas/proforma-items/proforma-item.entity.ts b/modules/customer-invoices/src/api/domain/proformas/entities/proforma-items/proforma-item.entity.ts similarity index 99% rename from modules/customer-invoices/src/api/domain/proformas/proforma-items/proforma-item.entity.ts rename to modules/customer-invoices/src/api/domain/proformas/entities/proforma-items/proforma-item.entity.ts index 2da63918..514ab875 100644 --- a/modules/customer-invoices/src/api/domain/proformas/proforma-items/proforma-item.entity.ts +++ b/modules/customer-invoices/src/api/domain/proformas/entities/proforma-items/proforma-item.entity.ts @@ -7,7 +7,7 @@ import { ItemDiscount, ItemQuantity, type ItemTaxGroup, -} from "../../common"; +} from "../../../common"; /** * diff --git a/modules/customer-invoices/src/api/domain/proformas/proforma-items/proforma-items.collection.ts b/modules/customer-invoices/src/api/domain/proformas/entities/proforma-items/proforma-items.collection.ts similarity index 99% rename from modules/customer-invoices/src/api/domain/proformas/proforma-items/proforma-items.collection.ts rename to modules/customer-invoices/src/api/domain/proformas/entities/proforma-items/proforma-items.collection.ts index 8f1288ba..6fce7e80 100644 --- a/modules/customer-invoices/src/api/domain/proformas/proforma-items/proforma-items.collection.ts +++ b/modules/customer-invoices/src/api/domain/proformas/entities/proforma-items/proforma-items.collection.ts @@ -1,7 +1,7 @@ import type { CurrencyCode, LanguageCode, Percentage } from "@repo/rdx-ddd"; import { Collection } from "@repo/rdx-utils"; -import { ItemAmount, ItemDiscount, type ItemTaxGroup } from "../../common"; +import { ItemAmount, ItemDiscount, type ItemTaxGroup } from "../../../common"; import type { ProformaItem } from "./proforma-item.entity"; diff --git a/modules/customer-invoices/src/api/domain/proformas/errors/index.ts b/modules/customer-invoices/src/api/domain/proformas/errors/index.ts new file mode 100644 index 00000000..e69de29b diff --git a/modules/customer-invoices/src/api/domain/proformas/index.ts b/modules/customer-invoices/src/api/domain/proformas/index.ts index 162cb4a5..b9b9e513 100644 --- a/modules/customer-invoices/src/api/domain/proformas/index.ts +++ b/modules/customer-invoices/src/api/domain/proformas/index.ts @@ -1,2 +1,3 @@ -export * from "./proforma.aggregate"; -export * from "./proforma-items"; +export * from "./aggregates"; +export * from "./entities"; +export * from "./errors"; diff --git a/modules/customer-invoices/src/api/index.ts b/modules/customer-invoices/src/api/index.ts index cf620655..6c058b65 100644 --- a/modules/customer-invoices/src/api/index.ts +++ b/modules/customer-invoices/src/api/index.ts @@ -4,9 +4,9 @@ import { type IssuedInvoicesInternalDeps, buildIssuedInvoicesDependencies, buildProformaServices, - issuedInvoicesRouter, models, } from "./infrastructure"; +import { issuedInvoicesRouter } from "./infrastructure/express"; export const customerInvoicesAPIModule: IModuleServer = { name: "customer-invoices", diff --git a/modules/customer-invoices/src/api/infrastructure/common/persistence/sequelize/mappers/domain/customer-invoice.mapper.ts b/modules/customer-invoices/src/api/infrastructure/common/persistence/sequelize/mappers/domain/customer-invoice.mapper.ts index 4ac9bc9b..e602b7c0 100644 --- a/modules/customer-invoices/src/api/infrastructure/common/persistence/sequelize/mappers/domain/customer-invoice.mapper.ts +++ b/modules/customer-invoices/src/api/infrastructure/common/persistence/sequelize/mappers/domain/customer-invoice.mapper.ts @@ -379,8 +379,8 @@ export class CustomerInvoiceDomainMapper subtotal_amount_value: allAmounts.subtotalAmount.value, subtotal_amount_scale: allAmounts.subtotalAmount.scale, - discount_percentage_value: source.discountPercentage.toPrimitive().value, - discount_percentage_scale: source.discountPercentage.toPrimitive().scale, + discount_percentage_value: source.globalDiscountPercentage.toPrimitive().value, + discount_percentage_scale: source.globalDiscountPercentage.toPrimitive().scale, discount_amount_value: allAmounts.globalDiscountAmount.value, discount_amount_scale: allAmounts.globalDiscountAmount.scale, diff --git a/modules/customer-invoices/src/api/infrastructure/common/persistence/sequelize/models/customer-invoice.model.ts b/modules/customer-invoices/src/api/infrastructure/common/persistence/sequelize/models/customer-invoice.model.ts index d4c465b9..9f5c9a47 100644 --- a/modules/customer-invoices/src/api/infrastructure/common/persistence/sequelize/models/customer-invoice.model.ts +++ b/modules/customer-invoices/src/api/infrastructure/common/persistence/sequelize/models/customer-invoice.model.ts @@ -68,13 +68,21 @@ export class CustomerInvoiceModel extends Model< declare subtotal_amount_value: number; declare subtotal_amount_scale: number; - // Discount percentage - declare discount_percentage_value: number; - declare discount_percentage_scale: number; + // Items discount amount (suma de descuentos individuales por ítem) + declare items_discount_amount_value: number; + declare items_discount_amount_scale: number; - // Discount amount - declare discount_amount_value: number; - declare discount_amount_scale: number; + // Global/header discount percentage + declare global_discount_percentage_value: number; + declare global_discount_percentage_scale: number; + + // Global/header discount amount + declare global_discount_amount_value: number; + declare global_discount_amount_scale: number; + + // Total discount amount (subtotal - descuentos) + declare total_discount_amount_value: number; + declare total_discount_amount_scale: number; // Taxable amount (base imponible) declare taxable_amount_value: number; @@ -283,25 +291,49 @@ export default (database: Sequelize) => { defaultValue: 2, }, - discount_percentage_value: { - type: new DataTypes.SMALLINT(), - allowNull: false, - defaultValue: 0, - }, - - discount_percentage_scale: { - type: new DataTypes.SMALLINT(), - allowNull: false, - defaultValue: 2, - }, - - discount_amount_value: { + items_discount_amount_value: { type: new DataTypes.BIGINT(), allowNull: false, defaultValue: 0, }, - discount_amount_scale: { + items_discount_amount_scale: { + type: new DataTypes.SMALLINT(), + allowNull: false, + defaultValue: 2, + }, + + global_discount_percentage_value: { + type: new DataTypes.SMALLINT(), + allowNull: false, + defaultValue: 0, + }, + + global_discount_percentage_scale: { + type: new DataTypes.SMALLINT(), + allowNull: false, + defaultValue: 2, + }, + + global_discount_amount_value: { + type: new DataTypes.BIGINT(), + allowNull: false, + defaultValue: 0, + }, + + global_discount_amount_scale: { + type: new DataTypes.SMALLINT(), + allowNull: false, + defaultValue: 2, + }, + + total_discount_amount_value: { + type: new DataTypes.BIGINT(), + allowNull: false, + defaultValue: 0, + }, + + total_discount_amount_scale: { type: new DataTypes.SMALLINT(), allowNull: false, defaultValue: 2, diff --git a/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/controllers/index.ts b/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/controllers/index.ts deleted file mode 100644 index 3c0009de..00000000 --- a/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/controllers/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from "./get-issued-invoice-by-id.controller"; -//export * from "./list-issued-invoices.controller"; -//export * from "./report-issued-invoice.controller"; diff --git a/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/index.ts b/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/index.ts index e6434f14..ad7259f0 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/index.ts +++ b/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/index.ts @@ -1,2 +1,3 @@ -export * from "./controllers"; +export * from "../../issued-invoices/express"; + export * from "./issued-invoices.routes"; diff --git a/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/issued-invoices.routes.ts b/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/issued-invoices.routes.ts index c04659c0..b362ed31 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/issued-invoices.routes.ts +++ b/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/issued-invoices.routes.ts @@ -8,11 +8,12 @@ import { ReportIssueInvoiceByIdParamsRequestSchema, ReportIssueInvoiceByIdQueryRequestSchema, } from "../../../../common/dto"; +import { + GetIssuedInvoiceByIdController, + ReportIssuedInvoiceController, +} from "../../issued-invoices"; import type { IssuedInvoicesInternalDeps } from "../../issued-invoices/di"; - -import { GetIssuedInvoiceByIdController } from "./controllers"; -import { ListIssuedInvoicesController } from "./controllers/list-issued-invoices.controller"; -import { ReportIssuedInvoiceController } from "./controllers/report-issued-invoice.controller"; +import { ListIssuedInvoicesController } from "../../issued-invoices/express/list-issued-invoices.controller"; export const issuedInvoicesRouter = (params: ModuleParams, deps: IssuedInvoicesInternalDeps) => { const { app, config } = params; diff --git a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/index.ts b/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/index.ts deleted file mode 100644 index 930e5f5e..00000000 --- a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -export * from "./change-status-proforma.controller"; -export * from "./create-proforma.controller"; -export * from "./delete-proforma.controller"; -export * from "./get-proforma.controller"; -export * from "./issue-proforma.controller"; -export * from "./list-proformas.controller"; -export * from "./report-proforma.controller"; -export * from "./update-proforma.controller"; diff --git a/modules/customer-invoices/src/api/infrastructure/express/proformas/index.ts b/modules/customer-invoices/src/api/infrastructure/express/proformas/index.ts index 7e5d4498..a4ac8a11 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/proformas/index.ts +++ b/modules/customer-invoices/src/api/infrastructure/express/proformas/index.ts @@ -1,3 +1,4 @@ -export * from "./controllers"; +export * from "../../proformas/express"; + export * from "./proformas.routes"; export * from "./proformas-api-error-mapper"; diff --git a/modules/customer-invoices/src/api/infrastructure/express/proformas/proformas-api-error-mapper.ts b/modules/customer-invoices/src/api/infrastructure/express/proformas/proformas-api-error-mapper.ts index 243ac245..0347011d 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/proformas/proformas-api-error-mapper.ts +++ b/modules/customer-invoices/src/api/infrastructure/express/proformas/proformas-api-error-mapper.ts @@ -7,6 +7,7 @@ import { type ErrorToApiRule, ValidationApiError, } from "@erp/core/api"; +import { isProformaCannotBeDeletedError } from "@erp/customer-invoices/api/domain/errors"; import { type CustomerInvoiceIdAlreadyExistsError, @@ -17,7 +18,6 @@ import { isEntityIsNotProformaError, isInvalidProformaTransitionError, isProformaCannotBeConvertedToInvoiceError, - isProformaCannotBeDeletedError, } from "../../../domain"; // Crea una regla específica (prioridad alta para sobreescribir mensajes) diff --git a/modules/customer-invoices/src/api/infrastructure/express/proformas/proformas.routes.ts b/modules/customer-invoices/src/api/infrastructure/express/proformas/proformas.routes.ts index 1ee34aad..ea0aec3a 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/proformas/proformas.routes.ts +++ b/modules/customer-invoices/src/api/infrastructure/express/proformas/proformas.routes.ts @@ -16,7 +16,6 @@ import { UpdateProformaByIdRequestSchema, } from "../../../../common"; import type { IssuedInvoicesInternalDeps } from "../../issued-invoices/di"; - import { ChangeStatusProformaController, CreateProformaController, @@ -26,7 +25,7 @@ import { ListProformasController, ReportProformaController, UpdateProformaController, -} from "./controllers"; +} from "../../proformas/express"; export const proformasRouter = (params: ModuleParams, deps: IssuedInvoicesInternalDeps) => { const { app, config } = params; diff --git a/modules/customer-invoices/src/api/infrastructure/issued-invoices/di/issued-invoice-repositories.di.ts b/modules/customer-invoices/src/api/infrastructure/issued-invoices/di/issued-invoice-repositories.di.ts index 207d95cb..34575b78 100644 --- a/modules/customer-invoices/src/api/infrastructure/issued-invoices/di/issued-invoice-repositories.di.ts +++ b/modules/customer-invoices/src/api/infrastructure/issued-invoices/di/issued-invoice-repositories.di.ts @@ -1,29 +1,19 @@ import { SpainTaxCatalogProvider } from "@erp/core"; -import { InMemoryMapperRegistry } from "@erp/core/api"; import type { Sequelize } from "sequelize"; import { - CustomerInvoiceDomainMapper, - CustomerInvoiceListMapper, - CustomerInvoiceRepository, -} from "../../common/persistence"; + IssuedInvoiceRepository, + SequelizeIssuedInvoiceDomainMapper, + SequelizeIssuedInvoiceListMapper, +} from "../persistence"; export const buildIssuedInvoiceRepository = (database: Sequelize) => { - const mapperRegistry = new InMemoryMapperRegistry(); - const taxCatalog = SpainTaxCatalogProvider(); - mapperRegistry - .registerDomainMapper( - { resource: "customer-invoice" }, - new CustomerInvoiceDomainMapper({ taxCatalog }) - ) - .registerQueryMappers([ - { - key: { resource: "customer-invoice", query: "LIST" }, - mapper: new CustomerInvoiceListMapper(), - }, - ]); + const domainMapper = new SequelizeIssuedInvoiceDomainMapper({ + taxCatalog, + }); + const listMapper = new SequelizeIssuedInvoiceListMapper(); - return new CustomerInvoiceRepository({ mapperRegistry, database }); + return new IssuedInvoiceRepository(domainMapper, listMapper, database); }; diff --git a/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/controllers/get-issued-invoice-by-id.controller.ts b/modules/customer-invoices/src/api/infrastructure/issued-invoices/express/get-issued-invoice-by-id.controller.ts similarity index 83% rename from modules/customer-invoices/src/api/infrastructure/express/issued-invoices/controllers/get-issued-invoice-by-id.controller.ts rename to modules/customer-invoices/src/api/infrastructure/issued-invoices/express/get-issued-invoice-by-id.controller.ts index 7ca1c0bc..d1e67bd6 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/controllers/get-issued-invoice-by-id.controller.ts +++ b/modules/customer-invoices/src/api/infrastructure/issued-invoices/express/get-issued-invoice-by-id.controller.ts @@ -5,9 +5,9 @@ import { requireCompanyContextGuard, } from "@erp/core/api"; -import { GetIssuedInvoiceByIdResponseSchema } from "../../../../../common"; -import type { GetIssuedInvoiceByIdUseCase } from "../../../../application/issued-invoices"; -import { customerInvoicesApiErrorMapper } from "../../proformas/proformas-api-error-mapper.ts"; +import { GetIssuedInvoiceByIdResponseSchema } from "../../../../common/index.ts"; +import type { GetIssuedInvoiceByIdUseCase } from "../../../application/issued-invoices/index.ts"; +import { customerInvoicesApiErrorMapper } from "../../express/proformas/proformas-api-error-mapper.ts"; export class GetIssuedInvoiceByIdController extends ExpressController { public constructor(private readonly useCase: GetIssuedInvoiceByIdUseCase) { diff --git a/modules/customer-invoices/src/api/infrastructure/issued-invoices/express/index.ts b/modules/customer-invoices/src/api/infrastructure/issued-invoices/express/index.ts new file mode 100644 index 00000000..aa08afdb --- /dev/null +++ b/modules/customer-invoices/src/api/infrastructure/issued-invoices/express/index.ts @@ -0,0 +1,3 @@ +export * from "./get-issued-invoice-by-id.controller"; +export * from "./list-issued-invoices.controller"; +export * from "./report-issued-invoice.controller"; diff --git a/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/controllers/list-issued-invoices.controller.ts b/modules/customer-invoices/src/api/infrastructure/issued-invoices/express/list-issued-invoices.controller.ts similarity index 85% rename from modules/customer-invoices/src/api/infrastructure/express/issued-invoices/controllers/list-issued-invoices.controller.ts rename to modules/customer-invoices/src/api/infrastructure/issued-invoices/express/list-issued-invoices.controller.ts index e695fa93..2a1253a3 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/controllers/list-issued-invoices.controller.ts +++ b/modules/customer-invoices/src/api/infrastructure/issued-invoices/express/list-issued-invoices.controller.ts @@ -6,9 +6,9 @@ import { } from "@erp/core/api"; import { Criteria } from "@repo/rdx-criteria/server"; -import { ListIssuedInvoicesResponseSchema } from "../../../../../common"; -import type { ListIssuedInvoicesUseCase } from "../../../../application/issued-invoices"; -import { customerInvoicesApiErrorMapper } from "../../proformas/proformas-api-error-mapper.ts"; +import { ListIssuedInvoicesResponseSchema } from "../../../../common/index.ts"; +import type { ListIssuedInvoicesUseCase } from "../../../application/issued-invoices/index.ts"; +import { customerInvoicesApiErrorMapper } from "../../express/proformas/proformas-api-error-mapper.ts"; export class ListIssuedInvoicesController extends ExpressController { public constructor(private readonly useCase: ListIssuedInvoicesUseCase) { diff --git a/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/controllers/report-issued-invoice.controller.ts b/modules/customer-invoices/src/api/infrastructure/issued-invoices/express/report-issued-invoice.controller.ts similarity index 89% rename from modules/customer-invoices/src/api/infrastructure/express/issued-invoices/controllers/report-issued-invoice.controller.ts rename to modules/customer-invoices/src/api/infrastructure/issued-invoices/express/report-issued-invoice.controller.ts index 7bc7584e..1e11ac59 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/issued-invoices/controllers/report-issued-invoice.controller.ts +++ b/modules/customer-invoices/src/api/infrastructure/issued-invoices/express/report-issued-invoice.controller.ts @@ -7,8 +7,8 @@ import { } from "@erp/core/api"; import type { ReportIssueInvoiceByIdQueryRequestDTO } from "@erp/customer-invoices/common"; -import type { ReportIssuedInvoiceUseCase } from "../../../../application"; -import { customerInvoicesApiErrorMapper } from "../../proformas/proformas-api-error-mapper.ts"; +import type { ReportIssuedInvoiceUseCase } from "../../../application/index.ts"; +import { customerInvoicesApiErrorMapper } from "../../express/proformas/proformas-api-error-mapper.ts"; export class ReportIssuedInvoiceController extends ExpressController { public constructor(private readonly useCase: ReportIssuedInvoiceUseCase) { diff --git a/modules/customer-invoices/src/api/infrastructure/issued-invoices/index.ts b/modules/customer-invoices/src/api/infrastructure/issued-invoices/index.ts index e8fe393b..7929b871 100644 --- a/modules/customer-invoices/src/api/infrastructure/issued-invoices/index.ts +++ b/modules/customer-invoices/src/api/infrastructure/issued-invoices/index.ts @@ -1,3 +1,4 @@ export * from "./di"; export * from "./documents"; +export * from "./express"; export * from "./persistence"; diff --git a/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/index.ts b/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/index.ts index 0c2e706a..bad13e0e 100644 --- a/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/index.ts +++ b/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/index.ts @@ -1,3 +1,2 @@ export * from "./mappers"; export * from "./repositories"; -export * from "./services"; diff --git a/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/mappers/domain/sequelize-issued-invoice-domain.mapper.ts b/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/mappers/domain/sequelize-issued-invoice-domain.mapper.ts index 1df3a7fd..71ea77d8 100644 --- a/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/mappers/domain/sequelize-issued-invoice-domain.mapper.ts +++ b/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/mappers/domain/sequelize-issued-invoice-domain.mapper.ts @@ -16,13 +16,13 @@ import { Maybe, Result, isNullishOrEmpty } from "@repo/rdx-utils"; import type { IIssuedInvoiceDomainMapper } from "../../../../../../application"; import { - CustomerInvoiceItems, - type IIssuedInvoiceProps, InvoiceNumber, InvoicePaymentMethod, InvoiceSerie, InvoiceStatus, IssuedInvoice, + IssuedInvoiceItems, + type IssuedInvoiceProps, } from "../../../../../../domain"; import type { CustomerInvoiceCreationAttributes, @@ -52,7 +52,7 @@ export class SequelizeIssuedInvoiceDomainMapper this._itemsMapper = new SequelizeIssuedInvoiceItemDomainMapper(params); // Instanciar el mapper de items this._recipientMapper = new SequelizeIssuedInvoiceRecipientDomainMapper(); - this._taxesMapper = new SequelizeIssuedInvoiceTaxesDomainMapper(params); + this._taxesMapper = new SequelizeIssuedInvoiceTaxesDomainMapper(); this._verifactuMapper = new SequelizeIssuedInvoiceVerifactuDomainMapper(); } @@ -236,17 +236,16 @@ export class SequelizeIssuedInvoiceDomainMapper // 6) Construcción del agregado (Dominio) - const items = CustomerInvoiceItems.create({ + const items = IssuedInvoiceItems.create({ languageCode: attributes.languageCode!, currencyCode: attributes.currencyCode!, globalDiscountPercentage: attributes.discountPercentage!, items: itemsResults.data.getAll(), }); - const invoiceProps: IIssuedInvoiceProps = { + const invoiceProps: IssuedInvoiceProps = { companyId: attributes.companyId!, - isIssuedInvoice: attributes.isIssuedInvoice, proformaId: attributes.proformaId!, status: attributes.status!, series: attributes.series!, @@ -264,7 +263,7 @@ export class SequelizeIssuedInvoiceDomainMapper languageCode: attributes.languageCode!, currencyCode: attributes.currencyCode!, - discountPercentage: attributes.discountPercentage!, + globalDiscountPercentage: attributes.discountPercentage!, paymentMethod: attributes.paymentMethod!, @@ -309,7 +308,7 @@ export class SequelizeIssuedInvoiceDomainMapper } // 2) Taxes - const taxesResult = this._taxesMapper.mapToPersistenceArray(source.getTaxes(), { + const taxesResult = this._taxesMapper.mapToPersistenceArray(source.taxes, { errors, parent: source, ...params, @@ -347,8 +346,6 @@ export class SequelizeIssuedInvoiceDomainMapper const taxes = taxesResult.data; const verifactu = verifactuResult.data; - const allAmounts = source.calculateAllAmounts(); // Da los totales ya calculados - const invoiceValues: Partial = { // Identificación id: source.id.toPrimitive(), @@ -356,11 +353,11 @@ export class SequelizeIssuedInvoiceDomainMapper // Flags / estado / serie / número is_proforma: false, - proforma_id: toNullable(source.proformaId, (v) => v.toPrimitive()), status: source.status.toPrimitive(), + proforma_id: toNullable(source.proformaId, (v) => v.toPrimitive()), + series: toNullable(source.series, (v) => v.toPrimitive()), invoice_number: source.invoiceNumber.toPrimitive(), - invoice_date: source.invoiceDate.toPrimitive(), operation_date: toNullable(source.operationDate, (v) => v.toPrimitive()), language_code: source.languageCode.toPrimitive(), @@ -368,13 +365,20 @@ export class SequelizeIssuedInvoiceDomainMapper reference: toNullable(source.reference, (reference) => reference), description: toNullable(source.description, (description) => description), + notes: toNullable(source.notes, (v) => v.toPrimitive()), + payment_method_id: toNullable(source.paymentMethod, (payment) => payment.toObjectString().id), + payment_method_description: toNullable( + source.paymentMethod, + (payment) => payment.toObjectString().payment_description + ), + subtotal_amount_value: source.subtotalAmount.value, subtotal_amount_scale: source.subtotalAmount.scale, - discount_percentage_value: source.discountPercentage.toPrimitive().value, - discount_percentage_scale: source.discountPercentage.toPrimitive().scale, + discount_percentage_value: source.globalDiscountPercentage.toPrimitive().value, + discount_percentage_scale: source.globalDiscountPercentage.toPrimitive().scale, discount_amount_value: source.globalDiscountAmount.value, discount_amount_scale: source.globalDiscountAmount.scale, @@ -388,12 +392,6 @@ export class SequelizeIssuedInvoiceDomainMapper total_amount_value: source.totalAmount.value, total_amount_scale: source.totalAmount.scale, - payment_method_id: toNullable(source.paymentMethod, (payment) => payment.toObjectString().id), - payment_method_description: toNullable( - source.paymentMethod, - (payment) => payment.toObjectString().payment_description - ), - customer_id: source.customerId.toPrimitive(), ...recipient, diff --git a/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/mappers/list/sequelize-issued-invoice-recipient.list.mapper.ts b/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/mappers/list/sequelize-issued-invoice-recipient.list.mapper.ts index 7ca1c651..1451fed8 100644 --- a/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/mappers/list/sequelize-issued-invoice-recipient.list.mapper.ts +++ b/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/mappers/list/sequelize-issued-invoice-recipient.list.mapper.ts @@ -14,11 +14,10 @@ import { } from "@repo/rdx-ddd"; import { Result } from "@repo/rdx-utils"; +import type { IssuedInvoiceListDTO } from "../../../../../../application"; import { InvoiceRecipient } from "../../../../../../domain"; import type { CustomerInvoiceModel } from "../../../../../common"; -import type { CustomerInvoiceListDTO } from "./sequelize-issued-invoice.list.mapper"; - export class SequelizeIssuedInvoiceRecipientListMapper extends SequelizeQueryMapper< CustomerInvoiceModel, InvoiceRecipient @@ -33,7 +32,7 @@ export class SequelizeIssuedInvoiceRecipientListMapper extends SequelizeQueryMap const { errors, attributes } = params as { errors: ValidationErrorDetail[]; - attributes: Partial; + attributes: Partial; }; const { isProforma } = attributes; diff --git a/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/mappers/list/sequelize-issued-invoice.list.mapper.ts b/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/mappers/list/sequelize-issued-invoice.list.mapper.ts index 8eb710bb..112257bf 100644 --- a/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/mappers/list/sequelize-issued-invoice.list.mapper.ts +++ b/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/mappers/list/sequelize-issued-invoice.list.mapper.ts @@ -2,7 +2,6 @@ import { type MapperParamsType, SequelizeQueryMapper } from "@erp/core/api"; import { CurrencyCode, LanguageCode, - Percentage, UniqueID, UtcDate, ValidationErrorCollection, @@ -94,7 +93,7 @@ export class SequelizeIssuedInvoiceListMapper operationDate: attributes.operationDate!, description: attributes.description!, - reference: attributes.description!, + reference: attributes.reference!, customerId: attributes.customerId!, recipient: recipientResult.data, @@ -102,9 +101,8 @@ export class SequelizeIssuedInvoiceListMapper languageCode: attributes.languageCode!, currencyCode: attributes.currencyCode!, - discountPercentage: attributes.discountPercentage!, subtotalAmount: attributes.subtotalAmount!, - discountAmount: attributes.discountAmount!, + totalDiscountAmount: attributes.totalDiscountAmount!, taxableAmount: attributes.taxableAmount!, taxesAmount: attributes.taxesAmount!, totalAmount: attributes.totalAmount!, @@ -175,15 +173,6 @@ export class SequelizeIssuedInvoiceListMapper errors ); - const discountPercentage = extractOrPushError( - Percentage.create({ - value: raw.discount_percentage_value, - scale: raw.discount_percentage_scale, - }), - "discount_percentage_value", - errors - ); - const subtotalAmount = extractOrPushError( InvoiceAmount.create({ value: raw.subtotal_amount_value, @@ -193,12 +182,12 @@ export class SequelizeIssuedInvoiceListMapper errors ); - const discountAmount = extractOrPushError( + const totalDiscountAmount = extractOrPushError( InvoiceAmount.create({ - value: raw.discount_amount_value, + value: raw.total_discount_amount_value, currency_code: currencyCode?.code, }), - "discount_amount_value", + "total_discount_amount_value", errors ); @@ -243,9 +232,9 @@ export class SequelizeIssuedInvoiceListMapper description, languageCode, currencyCode, - discountPercentage, + subtotalAmount, - discountAmount, + totalDiscountAmount, taxableAmount, taxesAmount, totalAmount, diff --git a/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/repositories/issued-invoice.repository.ts b/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/repositories/issued-invoice.repository.ts index fb6c5421..7689bff3 100644 --- a/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/repositories/issued-invoice.repository.ts +++ b/modules/customer-invoices/src/api/infrastructure/issued-invoices/persistence/sequelize/repositories/issued-invoice.repository.ts @@ -12,6 +12,10 @@ import { CustomerInvoiceTaxModel, VerifactuRecordModel, } from "../../../../common"; +import type { + SequelizeIssuedInvoiceDomainMapper, + SequelizeIssuedInvoiceListMapper, +} from "../mappers"; export class IssuedInvoiceRepository extends SequelizeRepository @@ -76,6 +80,38 @@ export class IssuedInvoiceRepository } } + /** + * Comprueba si existe una factura con un `id` dentro de una `company`. + * + * @param companyId - Identificador UUID de la empresa a la que pertenece la factura. + * @param id - Identificador UUID de la factura. + * @param transaction - Transacción activa para la operación. + * @param options - Opciones adicionales para la consulta (Sequelize FindOptions) + * @returns Result + */ + async existsByIdInCompany( + companyId: UniqueID, + id: UniqueID, + transaction: Transaction, + options: FindOptions> = {} + ): Promise> { + try { + const count = await CustomerInvoiceModel.count({ + ...options, + where: { + id: id.toString(), + company_id: companyId.toString(), + is_proforma: false, + ...(options.where ?? {}), + }, + transaction, + }); + return Result.ok(Boolean(count > 0)); + } catch (error: unknown) { + return Result.fail(translateSequelizeError(error)); + } + } + /** * * Busca una factura por su identificador único. @@ -86,7 +122,7 @@ export class IssuedInvoiceRepository * @param options - Opciones adicionales para la consulta (Sequelize FindOptions) * @returns Result */ - async getIssuedInvoiceByIdInCompany( + async getByIdInCompany( companyId: UniqueID, id: UniqueID, transaction: Transaction, diff --git a/modules/customer-invoices/src/api/infrastructure/proformas/di/proformas.di.ts b/modules/customer-invoices/src/api/infrastructure/proformas/di/proformas.di.ts index 64d567c9..e7cd12ec 100644 --- a/modules/customer-invoices/src/api/infrastructure/proformas/di/proformas.di.ts +++ b/modules/customer-invoices/src/api/infrastructure/proformas/di/proformas.di.ts @@ -1,45 +1,17 @@ -import { - InMemoryMapperRegistry, - InMemoryPresenterRegistry, - type ModuleParams, - buildTransactionManager, -} from "@erp/core/api"; -import { buildProformaCreator } from "@erp/customer-invoices/api/application/proformas/di/proforma-creator.di"; +import { type ModuleParams, buildTransactionManager } from "@erp/core/api"; import { type GetProformaByIdUseCase, + type ListProformasUseCase, + type ReportProformaUseCase, buildGetProformaByIdUseCase, buildListProformasUseCase, buildProformaFinder, + buildProformaSnapshotBuilders, buildReportProformaUseCase, } from "../../../application"; -import { buildProformaSnapshotBuilders } from "../../../application/issued-invoices"; -import { - ChangeStatusProformaUseCase, - CreateProformaUseCase, - CustomerInvoiceApplicationService, - DeleteProformaUseCase, - GetProformaUseCase, - IssueProformaUseCase, - ListProformasUseCase, - ProformaFullPresenter, - ProformaListPresenter, - ReportProformaUseCase, - UpdateProformaUseCase, -} from "../application"; -import { - ProformaItemsReportPresenter, - ProformaReportPresenter, - ProformaTaxesReportPresenter, -} from "../application/snapshot-builders/reports"; -import { SequelizeInvoiceNumberGenerator } from "./persistence/sequelize"; -import { - CustomerInvoiceDomainMapper, - CustomerInvoiceListMapper, -} from "./persistence/sequelize/mappers"; -import { buildProformaDocumentService } from "./proforma-documents.di"; -import { buildProformaNumberGenerator } from "./proforma-number-generator.di"; +import { buildproformaDocumentService } from "./proforma-documents.di"; import { buildProformaRepository } from "./proforma-repositories.di"; export type ProformasInternalDeps = { @@ -62,15 +34,15 @@ export function buildProformasDependencies(params: ModuleParams): ProformasInter // Infrastructure const transactionManager = buildTransactionManager(database); const repository = buildProformaRepository(database); - const numberService = buildProformaNumberGenerator(); + //const numberService = buildProformaNumberGenerator(); // Application helpers const finder = buildProformaFinder(repository); - const creator = buildProformaCreator(numberService, repository); + //const creator = buildProformaCreator(numberService, repository); const snapshotBuilders = buildProformaSnapshotBuilders(); - const documentGeneratorPipeline = buildProformaDocumentService(params); + const documentGeneratorPipeline = buildproformaDocumentService(params); // Internal use cases (factories) return { @@ -108,7 +80,7 @@ export function buildProformasDependencies(params: ModuleParams): ProformasInter }; } -const mapperRegistry = new InMemoryMapperRegistry(); +/*const mapperRegistry = new InMemoryMapperRegistry(); mapperRegistry .registerDomainMapper( { resource: "customer-invoice" }, @@ -124,7 +96,7 @@ mapperRegistry // Repository & Services const numberGenerator = new SequelizeInvoiceNumberGenerator(); -/** Aplicación */ + const appService = new CustomerInvoiceApplicationService(repository, numberGenerator); // Presenter Registry @@ -178,4 +150,5 @@ const useCases: ProformasDeps["useCases"] = { new ReportProformaUseCase(appService, transactionManager, presenterRegistry), issue_proforma: () => new IssueProformaUseCase(appService, transactionManager, presenterRegistry), changeStatus_proforma: () => new ChangeStatusProformaUseCase(appService, transactionManager), -}; + + */ diff --git a/modules/customer-invoices/src/api/infrastructure/proformas/documents/pipelines/proforma-document-pipeline-factory.ts b/modules/customer-invoices/src/api/infrastructure/proformas/documents/pipelines/proforma-document-pipeline-factory.ts index 5bcf6afb..fec8dcb7 100644 --- a/modules/customer-invoices/src/api/infrastructure/proformas/documents/pipelines/proforma-document-pipeline-factory.ts +++ b/modules/customer-invoices/src/api/infrastructure/proformas/documents/pipelines/proforma-document-pipeline-factory.ts @@ -16,7 +16,6 @@ import { ProformaDocumentPropertiesFactory, type ProformaReportSnapshot, } from "../../../../application"; -import { DigitalSignaturePostProcessor } from "../post-processors"; import { ProformaSignedDocumentCachePreProcessor } from "../pre-processors"; import { ProformaDocumentRenderer } from "../renderers"; import { PersistProformaDocumentSideEffect } from "../side-effects"; @@ -51,7 +50,7 @@ export class ProformaDocumentPipelineFactory { // 3) Firma real (Core / Infra) const postProcessor: IDocumentPostProcessor = new DocumentPostProcessorChain([ - new DigitalSignaturePostProcessor(deps.signingContextResolver, deps.documentSigningService), + // Aquí podrían ir más post-procesadores, como uno de validación o similar ]); // 4. Side-effects (persistencia best-effort) diff --git a/modules/customer-invoices/src/api/infrastructure/proformas/documents/post-processors/index.ts b/modules/customer-invoices/src/api/infrastructure/proformas/documents/post-processors/index.ts deleted file mode 100644 index 82543f53..00000000 --- a/modules/customer-invoices/src/api/infrastructure/proformas/documents/post-processors/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./digital-signature-post-processor"; diff --git a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/change-status-proforma.controller.ts b/modules/customer-invoices/src/api/infrastructure/proformas/express/change-status-proforma.controller.ts similarity index 87% rename from modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/change-status-proforma.controller.ts rename to modules/customer-invoices/src/api/infrastructure/proformas/express/change-status-proforma.controller.ts index a768fda4..94d78bca 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/change-status-proforma.controller.ts +++ b/modules/customer-invoices/src/api/infrastructure/proformas/express/change-status-proforma.controller.ts @@ -5,9 +5,7 @@ import { requireCompanyContextGuard, } from "@erp/core/api"; -import type { ChangeStatusProformaByIdRequestDTO } from "../../../../../common/dto"; -import type { ChangeStatusProformaUseCase } from "../../../../application"; -import { customerInvoicesApiErrorMapper } from "../proformas-api-error-mapper.ts"; +import type { ChangeStatusProformaByIdRequestDTO } from "../../../../common/dto/index.ts"; export class ChangeStatusProformaController extends ExpressController { public constructor(private readonly useCase: ChangeStatusProformaUseCase) { diff --git a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/create-proforma.controller.ts b/modules/customer-invoices/src/api/infrastructure/proformas/express/create-proforma.controller.ts similarity index 78% rename from modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/create-proforma.controller.ts rename to modules/customer-invoices/src/api/infrastructure/proformas/express/create-proforma.controller.ts index ff364fd6..2b6450b0 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/create-proforma.controller.ts +++ b/modules/customer-invoices/src/api/infrastructure/proformas/express/create-proforma.controller.ts @@ -5,9 +5,9 @@ import { requireCompanyContextGuard, } from "@erp/core/api"; -import type { CreateProformaRequestDTO } from "../../../../../common/dto"; -import type { CreateProformaUseCase } from "../../../../application"; -import { customerInvoicesApiErrorMapper } from "../proformas-api-error-mapper.ts"; +import type { CreateProformaRequestDTO } from "../../../../common/dto/index.ts"; +import type { CreateProformaUseCase } from "../../../application/index.ts"; +import { customerInvoicesApiErrorMapper } from "../../express/proformas/proformas-api-error-mapper.ts"; export class CreateProformaController extends ExpressController { public constructor(private readonly useCase: CreateProformaUseCase) { diff --git a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/delete-proforma.controller.ts b/modules/customer-invoices/src/api/infrastructure/proformas/express/delete-proforma.controller.ts similarity index 85% rename from modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/delete-proforma.controller.ts rename to modules/customer-invoices/src/api/infrastructure/proformas/express/delete-proforma.controller.ts index ccf5d17e..7e86f11b 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/delete-proforma.controller.ts +++ b/modules/customer-invoices/src/api/infrastructure/proformas/express/delete-proforma.controller.ts @@ -5,8 +5,8 @@ import { requireCompanyContextGuard, } from "@erp/core/api"; -import type { DeleteProformaUseCase } from "../../../../application"; -import { customerInvoicesApiErrorMapper } from "../proformas-api-error-mapper.ts"; +import type { DeleteProformaUseCase } from "../../../application/index.ts"; +import { customerInvoicesApiErrorMapper } from "../../express/proformas/proformas-api-error-mapper.ts"; export class DeleteProformaController extends ExpressController { public constructor(private readonly useCase: DeleteProformaUseCase) { diff --git a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/get-proforma.controller.ts b/modules/customer-invoices/src/api/infrastructure/proformas/express/get-proforma.controller.ts similarity index 84% rename from modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/get-proforma.controller.ts rename to modules/customer-invoices/src/api/infrastructure/proformas/express/get-proforma.controller.ts index e45e3695..387595ea 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/get-proforma.controller.ts +++ b/modules/customer-invoices/src/api/infrastructure/proformas/express/get-proforma.controller.ts @@ -5,8 +5,8 @@ import { requireCompanyContextGuard, } from "@erp/core/api"; -import type { GetProformaUseCase } from "../../../../application"; -import { customerInvoicesApiErrorMapper } from "../proformas-api-error-mapper.ts"; +import type { GetProformaUseCase } from "../../../application/index.ts"; +import { customerInvoicesApiErrorMapper } from "../../express/proformas/proformas-api-error-mapper.ts"; export class GetProformaController extends ExpressController { public constructor(private readonly useCase: GetProformaUseCase) { diff --git a/modules/customer-invoices/src/api/infrastructure/proformas/express/index.ts b/modules/customer-invoices/src/api/infrastructure/proformas/express/index.ts new file mode 100644 index 00000000..b094b4a9 --- /dev/null +++ b/modules/customer-invoices/src/api/infrastructure/proformas/express/index.ts @@ -0,0 +1,8 @@ +//export * from "./change-status-proforma.controller"; +//export * from "./create-proforma.controller"; +//export * from "./delete-proforma.controller"; +export * from "./get-proforma.controller"; +//export * from "./issue-proforma.controller"; +export * from "./list-proformas.controller"; +export * from "./report-proforma.controller"; +//export * from "./update-proforma.controller"; diff --git a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/issue-proforma.controller.ts b/modules/customer-invoices/src/api/infrastructure/proformas/express/issue-proforma.controller.ts similarity index 85% rename from modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/issue-proforma.controller.ts rename to modules/customer-invoices/src/api/infrastructure/proformas/express/issue-proforma.controller.ts index 96c7882a..30e3df05 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/issue-proforma.controller.ts +++ b/modules/customer-invoices/src/api/infrastructure/proformas/express/issue-proforma.controller.ts @@ -5,8 +5,8 @@ import { requireCompanyContextGuard, } from "@erp/core/api"; -import type { IssueProformaUseCase } from "../../../../application"; -import { customerInvoicesApiErrorMapper } from "../proformas-api-error-mapper.ts"; +import type { IssueProformaUseCase } from "../../../application/index.ts"; +import { customerInvoicesApiErrorMapper } from "../../express/proformas/proformas-api-error-mapper.ts"; export class IssueProformaController extends ExpressController { public constructor(private readonly useCase: IssueProformaUseCase) { diff --git a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/list-proformas.controller.ts b/modules/customer-invoices/src/api/infrastructure/proformas/express/list-proformas.controller.ts similarity index 89% rename from modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/list-proformas.controller.ts rename to modules/customer-invoices/src/api/infrastructure/proformas/express/list-proformas.controller.ts index 907e52f1..4c1881c5 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/list-proformas.controller.ts +++ b/modules/customer-invoices/src/api/infrastructure/proformas/express/list-proformas.controller.ts @@ -6,8 +6,8 @@ import { } from "@erp/core/api"; import { Criteria } from "@repo/rdx-criteria/server"; -import type { ListProformasUseCase } from "../../../../application"; -import { customerInvoicesApiErrorMapper } from "../proformas-api-error-mapper.ts"; +import type { ListProformasUseCase } from "../../../application/index.ts"; +import { customerInvoicesApiErrorMapper } from "../../express/proformas/proformas-api-error-mapper.ts"; export class ListProformasController extends ExpressController { public constructor(private readonly useCase: ListProformasUseCase) { diff --git a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/report-proforma.controller.ts b/modules/customer-invoices/src/api/infrastructure/proformas/express/report-proforma.controller.ts similarity index 86% rename from modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/report-proforma.controller.ts rename to modules/customer-invoices/src/api/infrastructure/proformas/express/report-proforma.controller.ts index 7841a733..29c24c37 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/report-proforma.controller.ts +++ b/modules/customer-invoices/src/api/infrastructure/proformas/express/report-proforma.controller.ts @@ -5,8 +5,8 @@ import { requireCompanyContextGuard, } from "@erp/core/api"; -import type { ReportProformaUseCase } from "../../../../application"; -import { customerInvoicesApiErrorMapper } from "../proformas-api-error-mapper.ts"; +import type { ReportProformaUseCase } from "../../../application/index.ts"; +import { customerInvoicesApiErrorMapper } from "../../express/proformas/proformas-api-error-mapper.ts"; export class ReportProformaController extends ExpressController { public constructor(private readonly useCase: ReportProformaUseCase) { diff --git a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/update-proforma.controller.ts b/modules/customer-invoices/src/api/infrastructure/proformas/express/update-proforma.controller.ts similarity index 80% rename from modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/update-proforma.controller.ts rename to modules/customer-invoices/src/api/infrastructure/proformas/express/update-proforma.controller.ts index e4d2e4da..ed85c6ad 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/proformas/controllers/update-proforma.controller.ts +++ b/modules/customer-invoices/src/api/infrastructure/proformas/express/update-proforma.controller.ts @@ -5,9 +5,9 @@ import { requireCompanyContextGuard, } from "@erp/core/api"; -import type { UpdateProformaByIdRequestDTO } from "../../../../../common/dto"; -import type { UpdateProformaUseCase } from "../../../../application"; -import { customerInvoicesApiErrorMapper } from "../proformas-api-error-mapper.ts"; +import type { UpdateProformaByIdRequestDTO } from "../../../../common/dto/index.ts"; +import type { UpdateProformaUseCase } from "../../../application/index.ts"; +import { customerInvoicesApiErrorMapper } from "../../express/proformas/proformas-api-error-mapper.ts"; export class UpdateProformaController extends ExpressController { public constructor(private readonly useCase: UpdateProformaUseCase) { diff --git a/modules/customer-invoices/src/api/infrastructure/proformas/index.ts b/modules/customer-invoices/src/api/infrastructure/proformas/index.ts index e8fe393b..7929b871 100644 --- a/modules/customer-invoices/src/api/infrastructure/proformas/index.ts +++ b/modules/customer-invoices/src/api/infrastructure/proformas/index.ts @@ -1,3 +1,4 @@ export * from "./di"; export * from "./documents"; +export * from "./express"; export * from "./persistence"; diff --git a/modules/customer-invoices/src/api/infrastructure/proformas/persistence/sequelize/mappers/domain/sequelize-proforma-domain.mapper.ts b/modules/customer-invoices/src/api/infrastructure/proformas/persistence/sequelize/mappers/domain/sequelize-proforma-domain.mapper.ts index b4ffbfba..fa2bfb79 100644 --- a/modules/customer-invoices/src/api/infrastructure/proformas/persistence/sequelize/mappers/domain/sequelize-proforma-domain.mapper.ts +++ b/modules/customer-invoices/src/api/infrastructure/proformas/persistence/sequelize/mappers/domain/sequelize-proforma-domain.mapper.ts @@ -249,7 +249,7 @@ export class SequelizeProformaDomainMapper languageCode: attributes.languageCode!, currencyCode: attributes.currencyCode!, - discountPercentage: attributes.discountPercentage!, + globalDiscountPercentage: attributes.discountPercentage!, paymentMethod: attributes.paymentMethod!, @@ -348,11 +348,17 @@ export class SequelizeProformaDomainMapper subtotal_amount_value: allAmounts.subtotalAmount.value, subtotal_amount_scale: allAmounts.subtotalAmount.scale, - discount_percentage_value: source.discountPercentage.toPrimitive().value, - discount_percentage_scale: source.discountPercentage.toPrimitive().scale, + items_discount_amount_value: allAmounts.itemDiscountAmount.value, + items_discount_amount_scale: allAmounts.itemDiscountAmount.scale, - discount_amount_value: allAmounts.globalDiscountAmount.value, - discount_amount_scale: allAmounts.globalDiscountAmount.scale, + global_discount_percentage_value: source.globalDiscountPercentage.toPrimitive().value, + global_discount_percentage_scale: source.globalDiscountPercentage.toPrimitive().scale, + + global_discount_amount_value: allAmounts.globalDiscountAmount.value, + global_discount_amount_scale: allAmounts.globalDiscountAmount.scale, + + total_discount_amount_value: allAmounts.totalDiscountAmount.value, + total_discount_amount_scale: allAmounts.totalDiscountAmount.scale, taxable_amount_value: allAmounts.taxableAmount.value, taxable_amount_scale: allAmounts.taxableAmount.scale, diff --git a/modules/customer-invoices/src/api/infrastructure/proformas/persistence/sequelize/mappers/domain/sequelize-proforma-taxes-domain.mapper.ts b/modules/customer-invoices/src/api/infrastructure/proformas/persistence/sequelize/mappers/domain/sequelize-proforma-taxes-domain.mapper.ts index 682d1cca..52ddaec7 100644 --- a/modules/customer-invoices/src/api/infrastructure/proformas/persistence/sequelize/mappers/domain/sequelize-proforma-taxes-domain.mapper.ts +++ b/modules/customer-invoices/src/api/infrastructure/proformas/persistence/sequelize/mappers/domain/sequelize-proforma-taxes-domain.mapper.ts @@ -42,9 +42,9 @@ export class SequelizeProformaTaxesDomainMapper extends SequelizeDomainMapper< }; try { - const { ivaAmount, recAmount, retentionAmount } = source.calculateAmounts(); + const { ivaAmount, recAmount, retentionAmount, totalAmount } = source.calculateAmounts(); - const totalTaxes = ivaAmount.add(recAmount).add(retentionAmount); + const totalTaxes = totalAmount; const dto: CustomerInvoiceTaxCreationAttributes = { tax_id: UniqueID.generateNewID().toPrimitive(), diff --git a/modules/customer-invoices/src/api/infrastructure/proformas/persistence/sequelize/mappers/list/sequelize-proforma.list.mapper.ts b/modules/customer-invoices/src/api/infrastructure/proformas/persistence/sequelize/mappers/list/sequelize-proforma.list.mapper.ts index 6a5d79f2..a311a661 100644 --- a/modules/customer-invoices/src/api/infrastructure/proformas/persistence/sequelize/mappers/list/sequelize-proforma.list.mapper.ts +++ b/modules/customer-invoices/src/api/infrastructure/proformas/persistence/sequelize/mappers/list/sequelize-proforma.list.mapper.ts @@ -2,7 +2,6 @@ import { type MapperParamsType, SequelizeQueryMapper } from "@erp/core/api"; import { CurrencyCode, LanguageCode, - Percentage, UniqueID, UtcDate, ValidationErrorCollection, @@ -83,9 +82,8 @@ export class SequelizeProformaListMapper languageCode: attributes.languageCode!, currencyCode: attributes.currencyCode!, - discountPercentage: attributes.discountPercentage!, subtotalAmount: attributes.subtotalAmount!, - discountAmount: attributes.discountAmount!, + totalDiscountAmount: attributes.totalDiscountAmount!, taxableAmount: attributes.taxableAmount!, taxesAmount: attributes.taxesAmount!, totalAmount: attributes.totalAmount!, @@ -154,15 +152,6 @@ export class SequelizeProformaListMapper errors ); - const discountPercentage = extractOrPushError( - Percentage.create({ - value: raw.discount_percentage_value, - scale: raw.discount_percentage_scale, - }), - "discount_percentage_value", - errors - ); - const subtotalAmount = extractOrPushError( InvoiceAmount.create({ value: raw.subtotal_amount_value, @@ -172,12 +161,12 @@ export class SequelizeProformaListMapper errors ); - const discountAmount = extractOrPushError( + const totalDiscountAmount = extractOrPushError( InvoiceAmount.create({ - value: raw.discount_amount_value, + value: raw.total_discount_amount_value, currency_code: currencyCode?.code, }), - "discount_amount_value", + "total_discount_amount_value", errors ); @@ -222,9 +211,9 @@ export class SequelizeProformaListMapper description, languageCode, currencyCode, - discountPercentage, + subtotalAmount, - discountAmount, + totalDiscountAmount, taxableAmount, taxesAmount, totalAmount,