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

174 lines
4.3 KiB
TypeScript

import {
CreationOptional,
DataTypes,
InferAttributes,
InferCreationAttributes,
Model,
NonAttribute,
Sequelize,
} from "sequelize";
import { CustomerInvoiceModel } from "./customer-invoice.model";
export type CustomerInvoiceItemCreationAttributes = InferCreationAttributes<
CustomerInvoiceItemModel,
{ omit: "invoice" }
>;
export class CustomerInvoiceItemModel extends Model<
InferAttributes<CustomerInvoiceItemModel>,
InferCreationAttributes<CustomerInvoiceItemModel, { omit: "invoice" }>
> {
declare item_id: string;
declare invoice_id: string;
declare parent_id: CreationOptional<string>;
declare position: number;
declare item_type: string;
declare description: CreationOptional<string>;
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>;
declare discount_amount: CreationOptional<number>;
declare discount_scale: CreationOptional<number>;
declare total_amount: CreationOptional<number>;
declare total_scale: CreationOptional<number>;
declare invoice: NonAttribute<CustomerInvoiceModel>;
static associate(database: Sequelize) {
const { CustomerInvoiceModel, CustomerInvoiceItemModel } = database.models;
CustomerInvoiceItemModel.belongsTo(CustomerInvoiceModel, {
as: "customerInvoice",
targetKey: "id",
foreignKey: "invoice_id",
onDelete: "CASCADE",
});
}
}
export default (database: Sequelize) => {
CustomerInvoiceItemModel.init(
{
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,
},
quantity_amount: {
type: new DataTypes.BIGINT(),
allowNull: true,
defaultValue: null,
},
quantity_scale: {
type: new DataTypes.SMALLINT(),
allowNull: true,
defaultValue: null,
},
unit_price_amount: {
type: new DataTypes.BIGINT(),
allowNull: true,
defaultValue: null,
},
unit_price_scale: {
type: new DataTypes.SMALLINT(),
allowNull: true,
defaultValue: null,
},
/*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,
},*/
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,
},
discount_amount: {
type: new DataTypes.SMALLINT(),
allowNull: true,
defaultValue: null,
},
discount_scale: {
type: new DataTypes.SMALLINT(),
allowNull: true,
defaultValue: null,
},
/*tax_amount: {
type: new DataTypes.BIGINT(),
allowNull: true,
},*/
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,
underscored: true,
tableName: "customer_invoice_items",
defaultScope: {},
scopes: {},
}
);
return CustomerInvoiceItemModel;
};