import { CreationOptional, DataTypes, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize, } from "sequelize"; import { CustomerInvoiceItemCreationAttributes, CustomerInvoiceItemModel } from "./customerCustomerInvoice-item.model"; export type CustomerInvoiceCreationAttributes = InferCreationAttributes & { items?: CustomerInvoiceItemCreationAttributes[]; }; export class CustomerInvoiceModel extends Model< InferAttributes, InferCreationAttributes > { declare id: string; declare customerCustomerInvoice_status: string; declare customerCustomerInvoice_series: CreationOptional; declare customerCustomerInvoice_number: CreationOptional; declare issue_date: CreationOptional; declare operation_date: CreationOptional; declare customerCustomerInvoice_language: string; declare customerCustomerInvoice_currency: string; // Subtotal declare subtotal_amount: CreationOptional; declare subtotal_scale: CreationOptional; // Total declare total_amount: CreationOptional; declare total_scale: CreationOptional; // Relaciones declare items: NonAttribute; //declare customer: NonAttribute; static associate(database: Sequelize) { const { CustomerInvoiceModel, CustomerInvoiceItemModel } = database.models; CustomerInvoiceModel.hasMany(CustomerInvoiceItemModel, { as: "items", foreignKey: "customerCustomerInvoice_id", onDelete: "CASCADE", }); } } export default (database: Sequelize) => { CustomerInvoiceModel.init( { id: { type: new DataTypes.UUID(), primaryKey: true, }, customerCustomerInvoice_status: { type: new DataTypes.STRING(), allowNull: false, }, customerCustomerInvoice_series: { type: new DataTypes.STRING(), allowNull: true, defaultValue: null, }, customerCustomerInvoice_number: { type: new DataTypes.STRING(), allowNull: true, defaultValue: null, }, issue_date: { type: new DataTypes.DATEONLY(), allowNull: true, defaultValue: null, }, operation_date: { type: new DataTypes.DATEONLY(), allowNull: true, defaultValue: null, }, customerCustomerInvoice_language: { type: new DataTypes.STRING(), allowNull: false, }, customerCustomerInvoice_currency: { type: new DataTypes.STRING(3), // ISO 4217 allowNull: false, }, 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, }, 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, tableName: "customerCustomerInvoices", paranoid: true, // softs deletes timestamps: true, createdAt: "created_at", updatedAt: "updated_at", deletedAt: "deleted_at", indexes: [{ unique: true, fields: ["customerCustomerInvoice_number"] }], whereMergeStrategy: "and", // <- cómo tratar el merge de un scope defaultScope: {}, scopes: {}, } ); return CustomerInvoiceModel; };