import { CreationOptional, DataTypes, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize, } from "sequelize"; import { CustomerInvoiceItemModel } from "./customer-invoice-item.model"; import { CustomerModel } from "./customer.model"; export type CustomerInvoiceCreationAttributes = InferCreationAttributes< CustomerInvoiceModel, { omit: "items" | "customer" } > & { //items: CustomerInvoiceItemCreationAttributes[]; customer_id: string; }; export class CustomerInvoiceModel extends Model< InferAttributes, InferCreationAttributes > { // To avoid table creation /*static async sync(): Promise { return Promise.resolve(); }*/ static associate(connection: Sequelize) { const { CustomerInvoiceModel, CustomerInvoiceItemModel, CustomerModel } = connection.models; CustomerInvoiceModel.hasMany(CustomerInvoiceItemModel, { as: "items", foreignKey: "quote_id", onDelete: "CASCADE", }); CustomerInvoiceModel.belongsTo(CustomerModel, { foreignKey: "dealer_id", as: "dealer", onDelete: "RESTRICT", }); } declare id: string; declare status: string; declare issue_date: string; declare invoice_number: string; declare invoice_type: string; declare lang_code: string; declare currency_code: string; declare customer_id: string; declare customer_tin: string; declare customer_name: string; declare customer_reference: CreationOptional; declare customer_street: string; declare customer_city: string; declare customer_state: string; declare customer_postal_code: string; declare customer_country: string; declare subtotal_price: CreationOptional; declare discount: CreationOptional; declare discount_price: CreationOptional; declare before_tax_price: CreationOptional; declare tax: CreationOptional; declare tax_price: CreationOptional; declare total_price: CreationOptional; declare notes: CreationOptional; declare items: NonAttribute; declare customer: NonAttribute; declare integrity_hash: CreationOptional; declare previous_invoice_id: CreationOptional; declare signed_at: CreationOptional; } export default (sequelize: Sequelize) => { CustomerInvoiceModel.init( { id: { type: DataTypes.UUID, primaryKey: true, }, status: { type: new DataTypes.STRING(), allowNull: false, }, issue_date: { type: new DataTypes.DATEONLY(), allowNull: false, }, invoice_number: { type: DataTypes.STRING(), allowNull: false, }, invoice_type: { type: new DataTypes.STRING(), allowNull: false, }, lang_code: { type: DataTypes.STRING(2), allowNull: false, defaultValue: "es", }, currency_code: { type: new DataTypes.STRING(3), allowNull: false, defaultValue: "EUR", }, customer_id: { type: new DataTypes.UUID(), primaryKey: true, }, customer_name: { type: DataTypes.STRING, allowNull: false, }, customer_tin: { type: DataTypes.STRING, allowNull: false, }, customer_reference: { type: new DataTypes.STRING(), }, customer_street: { type: DataTypes.STRING, allowNull: false, }, customer_city: { type: DataTypes.STRING, allowNull: false, }, customer_state: { type: DataTypes.STRING, allowNull: false, }, customer_postal_code: { type: DataTypes.STRING, allowNull: false, }, customer_country: { type: DataTypes.STRING, allowNull: false, }, subtotal_price: { type: new DataTypes.BIGINT(), allowNull: true, }, discount: { type: new DataTypes.SMALLINT(), allowNull: true, }, discount_price: { type: new DataTypes.BIGINT(), allowNull: true, }, before_tax_price: { type: new DataTypes.BIGINT(), allowNull: true, }, tax: { type: new DataTypes.SMALLINT(), allowNull: true, }, tax_price: { type: new DataTypes.BIGINT(), allowNull: true, }, total_price: { type: new DataTypes.BIGINT(), allowNull: true, }, notes: { type: DataTypes.TEXT, }, integrity_hash: { type: DataTypes.STRING, allowNull: false, comment: "Hash criptográfico para asegurar integridad", }, previous_invoice_id: { type: DataTypes.UUID, allowNull: true, comment: "Referencia a la factura anterior (si aplica)", }, signed_at: { type: DataTypes.DATE, allowNull: false, comment: "Fecha en que la factura fue firmada digitalmente", }, }, { sequelize, tableName: "customer_invoices", paranoid: true, // softs deletes timestamps: true, createdAt: "created_at", updatedAt: "updated_at", deletedAt: "deleted_at", indexes: [ { name: "status_idx", fields: ["status"] }, { name: "reference_idx", fields: ["reference"] }, { name: "deleted_at_idx", fields: ["deleted_at"] }, { name: "signed_at_idx", fields: ["signed_at"] }, ], whereMergeStrategy: "and", // <- cómo tratar el merge de un scope defaultScope: {}, scopes: {}, } ); return CustomerInvoiceModel; };