import { DataTypes, type InferAttributes, type InferCreationAttributes, Model, type NonAttribute, type Sequelize, } from "sequelize"; import type { CustomerInvoiceItem } from "../../../domain"; export type CustomerInvoiceItemTaxCreationAttributes = InferCreationAttributes< CustomerInvoiceItemTaxModel, { omit: "item" } >; export class CustomerInvoiceItemTaxModel extends Model< InferAttributes, InferCreationAttributes > { declare tax_id: string; declare item_id: string; declare tax_code: string; //"iva_21" // Taxable amount (base imponible) // 100,00 € declare taxable_amount_value: number; declare taxable_amount_scale: number; // Total tax amount / taxes total // 21,00 € declare taxes_amount_value: number; declare taxes_amount_scale: number; // Relaciones declare item: NonAttribute; static associate(database: Sequelize) { const models = database.models; const requiredModels = ["CustomerInvoiceItemModel"]; // Comprobamos que los modelos existan for (const name of requiredModels) { if (!models[name]) { throw new Error(`[CustomerInvoiceItemTaxModel.associate] Missing model: ${name}`); } } const { CustomerInvoiceItemModel } = models; CustomerInvoiceItemTaxModel.belongsTo(CustomerInvoiceItemModel, { as: "item", targetKey: "item_id", foreignKey: "item_id", onDelete: "CASCADE", onUpdate: "CASCADE", }); } static hooks(_database: Sequelize) { // } } export default (database: Sequelize) => { CustomerInvoiceItemTaxModel.init( { tax_id: { type: DataTypes.UUID, primaryKey: true, }, item_id: { type: DataTypes.UUID, allowNull: false, }, tax_code: { type: new DataTypes.STRING(), allowNull: false, }, taxable_amount_value: { type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes allowNull: false, defaultValue: 0, }, taxable_amount_scale: { type: new DataTypes.SMALLINT(), allowNull: false, defaultValue: 4, }, taxes_amount_value: { type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes allowNull: false, defaultValue: 0, }, taxes_amount_scale: { type: new DataTypes.SMALLINT(), allowNull: false, defaultValue: 4, }, }, { sequelize: database, modelName: "CustomerInvoiceItemTaxModel", tableName: "customer_invoice_item_taxes", underscored: true, indexes: [], whereMergeStrategy: "and", // <- cómo tratar el merge de un scope defaultScope: {}, scopes: {}, } ); return CustomerInvoiceItemTaxModel; };