Uecko_ERP/modules/customer-invoices/src/api/infrastructure/sequelize/customer-invoice.model.ts

151 lines
3.6 KiB
TypeScript
Raw Normal View History

2025-04-01 14:26:15 +00:00
import {
CreationOptional,
DataTypes,
InferAttributes,
InferCreationAttributes,
Model,
2025-04-22 08:11:50 +00:00
NonAttribute,
2025-04-01 14:26:15 +00:00
Sequelize,
} from "sequelize";
2025-06-12 06:55:17 +00:00
import {
CustomerInvoiceItemCreationAttributes,
CustomerInvoiceItemModel,
} from "./customer-invoice-item.model";
export type CustomerInvoiceCreationAttributes = InferCreationAttributes<
CustomerInvoiceModel,
{ omit: "items" }
> & {
2025-06-11 15:13:44 +00:00
items?: CustomerInvoiceItemCreationAttributes[];
2025-04-22 08:11:50 +00:00
};
2025-04-01 14:26:15 +00:00
2025-06-11 15:13:44 +00:00
export class CustomerInvoiceModel extends Model<
InferAttributes<CustomerInvoiceModel>,
InferCreationAttributes<CustomerInvoiceModel, { omit: "items" }>
2025-05-02 21:43:51 +00:00
> {
2025-04-01 14:26:15 +00:00
declare id: string;
2025-06-12 06:55:17 +00:00
declare invoice_status: string;
declare invoice_series: CreationOptional<string>;
declare invoice_number: CreationOptional<string>;
2025-04-01 14:26:15 +00:00
declare issue_date: CreationOptional<string>;
declare operation_date: CreationOptional<string>;
2025-06-12 06:55:17 +00:00
declare invoice_language: string;
declare invoice_currency: string;
2025-04-01 14:26:15 +00:00
// Subtotal
declare subtotal_amount: CreationOptional<number>;
declare subtotal_scale: CreationOptional<number>;
// Total
declare total_amount: CreationOptional<number>;
declare total_scale: CreationOptional<number>;
2025-05-02 21:43:51 +00:00
// Relaciones
2025-06-11 15:13:44 +00:00
declare items: NonAttribute<CustomerInvoiceItemModel[]>;
//declare customer: NonAttribute<CustomerInvoiceParticipant_Model[]>;
2025-05-02 21:43:51 +00:00
static associate(database: Sequelize) {
2025-06-11 15:13:44 +00:00
const { CustomerInvoiceModel, CustomerInvoiceItemModel } = database.models;
2025-05-02 21:43:51 +00:00
2025-06-11 15:13:44 +00:00
CustomerInvoiceModel.hasMany(CustomerInvoiceItemModel, {
2025-05-02 21:43:51 +00:00
as: "items",
2025-06-12 06:55:17 +00:00
foreignKey: "invoice_id",
2025-05-02 21:43:51 +00:00
onDelete: "CASCADE",
});
}
2025-04-01 14:26:15 +00:00
}
2025-05-02 21:43:51 +00:00
export default (database: Sequelize) => {
2025-06-11 15:13:44 +00:00
CustomerInvoiceModel.init(
2025-04-01 14:26:15 +00:00
{
id: {
type: new DataTypes.UUID(),
primaryKey: true,
},
2025-06-12 06:55:17 +00:00
invoice_status: {
2025-04-01 14:26:15 +00:00
type: new DataTypes.STRING(),
allowNull: false,
},
2025-06-12 06:55:17 +00:00
invoice_series: {
2025-04-01 14:26:15 +00:00
type: new DataTypes.STRING(),
allowNull: true,
defaultValue: null,
},
2025-06-12 06:55:17 +00:00
invoice_number: {
2025-04-01 14:26:15 +00:00
type: new DataTypes.STRING(),
allowNull: true,
defaultValue: null,
},
issue_date: {
2025-04-01 15:32:53 +00:00
type: new DataTypes.DATEONLY(),
2025-04-01 14:26:15 +00:00
allowNull: true,
defaultValue: null,
},
operation_date: {
2025-04-01 15:32:53 +00:00
type: new DataTypes.DATEONLY(),
2025-04-01 14:26:15 +00:00
allowNull: true,
defaultValue: null,
},
2025-06-12 06:55:17 +00:00
invoice_language: {
2025-04-01 14:26:15 +00:00
type: new DataTypes.STRING(),
allowNull: false,
},
2025-06-12 06:55:17 +00:00
invoice_currency: {
2025-04-01 14:26:15 +00:00
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,
},
},
{
2025-05-02 21:43:51 +00:00
sequelize: database,
2025-06-12 06:55:17 +00:00
tableName: "customer_invoices",
2025-04-01 14:26:15 +00:00
paranoid: true, // softs deletes
timestamps: true,
createdAt: "created_at",
updatedAt: "updated_at",
deletedAt: "deleted_at",
2025-06-12 06:55:17 +00:00
indexes: [{ unique: true, fields: ["invoice_number"] }],
2025-04-01 14:26:15 +00:00
whereMergeStrategy: "and", // <- cómo tratar el merge de un scope
defaultScope: {},
scopes: {},
}
);
2025-06-11 15:13:44 +00:00
return CustomerInvoiceModel;
2025-04-01 14:26:15 +00:00
};