import { CreationOptional, DataTypes, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize, } from "sequelize"; import { CustomerInvoiceModel } from "./customer-invoice.model"; export type CustomerInvoiceItemCreationAttributes = InferCreationAttributes< CustomerInvoiceItemModel, { omit: "invoice" } >; export class CustomerInvoiceItemModel extends Model< InferAttributes, InferCreationAttributes > { declare item_id: string; declare invoice_id: string; declare parent_id: CreationOptional; declare position: number; declare item_type: string; declare description: CreationOptional; declare quantity_amount: CreationOptional; declare quantity_scale: CreationOptional; declare unit_price_amount: CreationOptional; declare unit_price_scale: CreationOptional; declare subtotal_amount: CreationOptional; declare subtotal_scale: CreationOptional; declare discount_amount: CreationOptional; declare discount_scale: CreationOptional; declare total_amount: CreationOptional; declare total_scale: CreationOptional; declare invoice: NonAttribute; static associate(database: Sequelize) { const { CustomerInvoiceModel, CustomerInvoiceItemModel } = database.models; CustomerInvoiceItemModel.belongsTo(CustomerInvoiceModel, { as: "customerInvoice", targetKey: "id", foreignKey: "invoice_id", onDelete: "CASCADE", }); } } export default (database: Sequelize) => { CustomerInvoiceItemModel.init( { item_id: { type: new DataTypes.UUID(), primaryKey: true, }, invoice_id: { type: new DataTypes.UUID(), primaryKey: true, }, parent_id: { type: new DataTypes.UUID(), allowNull: true, // Puede ser nulo para elementos de nivel superior }, position: { type: new DataTypes.MEDIUMINT(), autoIncrement: false, allowNull: false, }, item_type: { type: new DataTypes.STRING(), allowNull: false, defaultValue: "simple", }, description: { type: new DataTypes.TEXT(), allowNull: true, }, quantity_amount: { type: new DataTypes.BIGINT(), allowNull: true, defaultValue: null, }, quantity_scale: { type: new DataTypes.SMALLINT(), allowNull: true, defaultValue: null, }, unit_price_amount: { type: new DataTypes.BIGINT(), allowNull: true, defaultValue: null, }, unit_price_scale: { type: new DataTypes.SMALLINT(), allowNull: true, defaultValue: null, }, /*tax_slug: { type: new DataTypes.DECIMAL(3, 2), allowNull: true, }, tax_rate: { type: new DataTypes.DECIMAL(3, 2), allowNull: true, }, tax_equalization: { type: new DataTypes.DECIMAL(3, 2), allowNull: true, },*/ subtotal_amount: { type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes allowNull: true, defaultValue: null, }, subtotal_scale: { type: new DataTypes.SMALLINT(), allowNull: true, defaultValue: null, }, discount_amount: { type: new DataTypes.SMALLINT(), allowNull: true, defaultValue: null, }, discount_scale: { type: new DataTypes.SMALLINT(), allowNull: true, defaultValue: null, }, /*tax_amount: { type: new DataTypes.BIGINT(), allowNull: true, },*/ total_amount: { type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes allowNull: true, defaultValue: null, }, total_scale: { type: new DataTypes.SMALLINT(), allowNull: true, defaultValue: null, }, }, { sequelize: database, underscored: true, tableName: "customer_invoice_items", defaultScope: {}, scopes: {}, } ); return CustomerInvoiceItemModel; };