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