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

210 lines
4.9 KiB
TypeScript
Raw Normal View History

2025-03-18 08:05:00 +00:00
import {
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-06-12 06:55:17 +00:00
import { CustomerInvoiceModel } from "./customer-invoice.model";
2025-03-18 08:05:00 +00:00
2025-06-11 15:13:44 +00:00
export type CustomerInvoiceItemCreationAttributes = InferCreationAttributes<
CustomerInvoiceItemModel,
2025-06-12 06:55:17 +00:00
{ omit: "invoice" }
2025-05-02 21:43:51 +00:00
>;
2025-03-18 08:05:00 +00:00
2025-06-11 15:13:44 +00:00
export class CustomerInvoiceItemModel extends Model<
InferAttributes<CustomerInvoiceItemModel>,
2025-06-12 06:55:17 +00:00
InferCreationAttributes<CustomerInvoiceItemModel, { omit: "invoice" }>
2025-03-18 08:05:00 +00:00
> {
declare item_id: string;
2025-06-12 06:55:17 +00:00
declare invoice_id: string;
2025-04-22 08:11:50 +00:00
2025-03-18 08:05:00 +00:00
declare position: number;
2025-04-22 08:11:50 +00:00
2025-09-03 10:41:12 +00:00
declare description: string;
2025-03-18 08:05:00 +00:00
2025-09-04 17:57:04 +00:00
declare quantity_value: number;
2025-09-03 10:41:12 +00:00
declare quantity_scale: number;
2025-04-01 14:26:15 +00:00
2025-09-04 17:57:04 +00:00
declare unit_amount_value: number;
declare unit_amount_scale: number;
2025-04-01 14:26:15 +00:00
2025-09-04 17:57:04 +00:00
// Subtotal
declare subtotal_amount_value: number;
declare subtotal_amount_scale: number;
2025-04-01 14:26:15 +00:00
2025-09-04 17:57:04 +00:00
// Discount percentage
declare discount_percentage_value: number;
declare discount_percentage_scale: number;
2025-04-22 08:11:50 +00:00
2025-09-04 17:57:04 +00:00
// Discount amount
declare discount_amount_value: number;
declare discount_amount_scale: number;
// Taxable amount (base imponible)
declare taxable_amount_value: number;
declare taxable_amount_scale: number;
// Total taxes amount / taxes total
declare taxes_amount_value: number;
declare taxes_amount_scale: number;
// Total
declare total_amount_value: number;
declare total_amount_scale: number;
2025-04-01 14:26:15 +00:00
2025-06-12 06:55:17 +00:00
declare invoice: NonAttribute<CustomerInvoiceModel>;
2025-05-02 21:43:51 +00:00
static associate(database: Sequelize) {
2025-08-23 11:57:48 +00:00
const { CustomerInvoiceModel, CustomerInvoiceItemModel } = database.models;
2025-05-02 21:43:51 +00:00
2025-08-23 11:57:48 +00:00
CustomerInvoiceItemModel.belongsTo(CustomerInvoiceModel, {
2025-06-12 06:55:17 +00:00
as: "customerInvoice",
targetKey: "id",
2025-06-12 06:55:17 +00:00
foreignKey: "invoice_id",
2025-05-02 21:43:51 +00:00
onDelete: "CASCADE",
});
2025-05-02 21:43:51 +00:00
}
2025-03-18 08:05:00 +00:00
}
2025-05-02 21:43:51 +00:00
export default (database: Sequelize) => {
2025-06-11 15:13:44 +00:00
CustomerInvoiceItemModel.init(
2025-03-18 08:05:00 +00:00
{
item_id: {
type: new DataTypes.UUID(),
primaryKey: true,
},
2025-09-04 17:57:04 +00:00
2025-06-12 06:55:17 +00:00
invoice_id: {
2025-03-18 08:05:00 +00:00
type: new DataTypes.UUID(),
2025-09-03 10:41:12 +00:00
allowNull: false,
2025-03-18 08:05:00 +00:00
},
2025-09-04 17:57:04 +00:00
2025-03-18 08:05:00 +00:00
position: {
2025-09-03 10:41:12 +00:00
type: new DataTypes.MEDIUMINT().UNSIGNED,
2025-03-18 08:05:00 +00:00
autoIncrement: false,
allowNull: false,
},
2025-09-04 17:57:04 +00:00
2025-03-18 08:05:00 +00:00
description: {
type: new DataTypes.TEXT(),
allowNull: true,
2025-09-03 10:41:12 +00:00
defaultValue: null,
2025-03-18 08:05:00 +00:00
},
2025-04-01 14:26:15 +00:00
2025-09-04 17:57:04 +00:00
quantity_value: {
2025-04-01 14:26:15 +00:00
type: new DataTypes.BIGINT(),
allowNull: true,
defaultValue: null,
},
2025-09-04 17:57:04 +00:00
2025-04-01 14:26:15 +00:00
quantity_scale: {
type: new DataTypes.SMALLINT(),
2025-09-04 17:57:04 +00:00
allowNull: false,
defaultValue: 2,
2025-03-18 08:05:00 +00:00
},
2025-04-01 14:26:15 +00:00
2025-09-04 17:57:04 +00:00
unit_amount_value: {
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-09-04 17:57:04 +00:00
unit_amount_scale: {
2025-04-01 14:26:15 +00:00
type: new DataTypes.SMALLINT(),
2025-09-04 17:57:04 +00:00
allowNull: false,
defaultValue: 4,
},
subtotal_amount_value: {
type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes
2025-04-01 14:26:15 +00:00
allowNull: true,
defaultValue: null,
},
2025-09-04 17:57:04 +00:00
subtotal_amount_scale: {
type: new DataTypes.SMALLINT(),
allowNull: false,
defaultValue: 4,
2025-03-18 08:05:00 +00:00
},
2025-09-04 17:57:04 +00:00
discount_percentage_value: {
type: new DataTypes.SMALLINT(),
2025-03-18 08:05:00 +00:00
allowNull: true,
2025-09-04 17:57:04 +00:00
defaultValue: null,
},
discount_percentage_scale: {
type: new DataTypes.SMALLINT(),
allowNull: false,
defaultValue: 2,
2025-03-18 08:05:00 +00:00
},
2025-04-01 14:26:15 +00:00
2025-09-04 17:57:04 +00:00
discount_amount_value: {
2025-04-01 14:26:15 +00:00
type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes
allowNull: true,
defaultValue: null,
},
2025-09-04 17:57:04 +00:00
discount_amount_scale: {
2025-04-01 14:26:15 +00:00
type: new DataTypes.SMALLINT(),
2025-09-04 17:57:04 +00:00
allowNull: false,
defaultValue: 4,
},
taxable_amount_value: {
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,
2025-03-18 08:05:00 +00:00
},
2025-04-01 14:26:15 +00:00
2025-09-04 17:57:04 +00:00
taxable_amount_scale: {
2025-04-22 08:11:50 +00:00
type: new DataTypes.SMALLINT(),
2025-09-04 17:57:04 +00:00
allowNull: false,
defaultValue: 4,
},
taxes_amount_value: {
type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes
2025-04-22 08:11:50 +00:00
allowNull: true,
defaultValue: null,
},
2025-09-04 17:57:04 +00:00
taxes_amount_scale: {
2025-04-22 08:11:50 +00:00
type: new DataTypes.SMALLINT(),
2025-09-04 17:57:04 +00:00
allowNull: false,
defaultValue: 4,
2025-04-22 08:11:50 +00:00
},
2025-09-04 17:57:04 +00:00
total_amount_value: {
2025-04-01 14:26:15 +00:00
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,
},
2025-09-04 17:57:04 +00:00
total_amount_scale: {
2025-04-01 14:26:15 +00:00
type: new DataTypes.SMALLINT(),
2025-09-04 17:57:04 +00:00
allowNull: false,
defaultValue: 4,
2025-03-18 08:05:00 +00:00
},
},
{
2025-05-02 21:43:51 +00:00
sequelize: database,
2025-06-12 06:55:17 +00:00
tableName: "customer_invoice_items",
2025-04-22 08:11:50 +00:00
2025-09-03 10:41:12 +00:00
underscored: true,
2025-09-05 11:23:45 +00:00
indexes: [{ name: "invoice_idx", fields: ["invoice_id"], unique: false }],
2025-09-03 10:41:12 +00:00
whereMergeStrategy: "and", // <- cómo tratar el merge de un scope
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-06-11 15:13:44 +00:00
return CustomerInvoiceItemModel;
2025-03-18 08:05:00 +00:00
};