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

172 lines
4.1 KiB
TypeScript
Raw Normal View History

2025-03-18 08:05:00 +00:00
import {
CreationOptional,
DataTypes,
InferAttributes,
InferCreationAttributes,
Model,
2025-05-02 21:43:51 +00:00
NonAttribute,
2025-03-18 08:05:00 +00:00
Sequelize,
} from "sequelize";
2025-05-02 21:43:51 +00:00
import { InvoiceModel } from "./invoice.model";
2025-03-18 08:05:00 +00:00
2025-05-02 21:43:51 +00:00
export type InvoiceItemCreationAttributes = InferCreationAttributes<
InvoiceItemModel,
{ omit: "invoice" }
>;
2025-03-18 08:05:00 +00:00
2025-04-22 08:11:50 +00:00
export class InvoiceItemModel extends Model<
InferAttributes<InvoiceItemModel>,
2025-05-02 21:43:51 +00:00
InferCreationAttributes<InvoiceItemModel, { omit: "invoice" }>
2025-03-18 08:05:00 +00:00
> {
declare item_id: string;
2025-04-22 08:11:50 +00:00
declare invoice_id: string;
2025-03-18 08:05:00 +00:00
declare parent_id: CreationOptional<string>;
declare position: number;
declare item_type: string;
2025-04-22 08:11:50 +00:00
2025-03-18 08:05:00 +00:00
declare description: CreationOptional<string>;
2025-04-01 14:26:15 +00:00
declare quantity_amount: CreationOptional<number>;
declare quantity_scale: CreationOptional<number>;
declare unit_price_amount: CreationOptional<number>;
declare unit_price_scale: CreationOptional<number>;
declare subtotal_amount: CreationOptional<number>;
declare subtotal_scale: CreationOptional<number>;
2025-04-22 08:11:50 +00:00
declare discount_amount: CreationOptional<number>;
declare discount_scale: CreationOptional<number>;
2025-04-01 14:26:15 +00:00
declare total_amount: CreationOptional<number>;
declare total_scale: CreationOptional<number>;
2025-05-02 21:43:51 +00:00
declare invoice: NonAttribute<InvoiceModel>;
static associate(database: Sequelize) {
/*const { Invoice_Model, InvoiceItem_Model } = connection.models;
InvoiceItem_Model.belongsTo(Invoice_Model, {
as: "invoice",
foreignKey: "invoice_id",
onDelete: "CASCADE",
});*/
}
2025-03-18 08:05:00 +00:00
}
2025-05-02 21:43:51 +00:00
export default (database: Sequelize) => {
2025-04-22 08:11:50 +00:00
InvoiceItemModel.init(
2025-03-18 08:05:00 +00:00
{
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,
},
2025-04-01 14:26:15 +00:00
quantity_amount: {
type: new DataTypes.BIGINT(),
allowNull: true,
defaultValue: null,
},
quantity_scale: {
type: new DataTypes.SMALLINT(),
2025-03-18 08:05:00 +00:00
allowNull: true,
2025-04-01 14:26:15 +00:00
defaultValue: null,
2025-03-18 08:05:00 +00:00
},
2025-04-01 14:26:15 +00:00
unit_price_amount: {
2025-03-18 08:05:00 +00:00
type: new DataTypes.BIGINT(),
allowNull: true,
2025-04-01 14:26:15 +00:00
defaultValue: null,
2025-03-18 08:05:00 +00:00
},
2025-04-01 14:26:15 +00:00
unit_price_scale: {
type: new DataTypes.SMALLINT(),
allowNull: true,
defaultValue: null,
},
2025-03-18 08:05:00 +00:00
/*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,
},*/
2025-04-01 14:26:15 +00:00
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(),
2025-03-18 08:05:00 +00:00
allowNull: true,
2025-04-01 14:26:15 +00:00
defaultValue: null,
2025-03-18 08:05:00 +00:00
},
2025-04-01 14:26:15 +00:00
2025-04-22 08:11:50 +00:00
discount_amount: {
type: new DataTypes.SMALLINT(),
allowNull: true,
defaultValue: null,
},
discount_scale: {
type: new DataTypes.SMALLINT(),
allowNull: true,
defaultValue: null,
},
2025-03-18 08:05:00 +00:00
/*tax_amount: {
type: new DataTypes.BIGINT(),
allowNull: true,
},*/
2025-04-01 14:26:15 +00:00
total_amount: {
type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes
2025-03-18 08:05:00 +00:00
allowNull: true,
2025-04-01 14:26:15 +00:00
defaultValue: null,
},
total_scale: {
type: new DataTypes.SMALLINT(),
allowNull: true,
defaultValue: null,
2025-03-18 08:05:00 +00:00
},
},
{
2025-05-02 21:43:51 +00:00
sequelize: database,
2025-03-18 08:05:00 +00:00
tableName: "invoice_items",
2025-04-22 08:11:50 +00:00
defaultScope: {},
scopes: {},
2025-04-01 14:26:15 +00:00
}
2025-03-18 08:05:00 +00:00
);
2025-04-22 08:11:50 +00:00
return InvoiceItemModel;
2025-03-18 08:05:00 +00:00
};