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

111 lines
2.5 KiB
TypeScript
Raw Normal View History

2025-09-04 17:57:04 +00:00
import {
DataTypes,
InferAttributes,
InferCreationAttributes,
Model,
NonAttribute,
Sequelize,
} from "sequelize";
2025-09-13 18:45:55 +00:00
import { CustomerInvoiceItem } from "../../../domain";
2025-09-04 17:57:04 +00:00
2025-09-09 15:48:12 +00:00
export type CustomerInvoiceItemTaxCreationAttributes = InferCreationAttributes<
CustomerInvoiceItemTaxModel,
{ omit: "item" }
2025-09-04 17:57:04 +00:00
>;
2025-09-09 15:48:12 +00:00
export class CustomerInvoiceItemTaxModel extends Model<
InferAttributes<CustomerInvoiceItemTaxModel>,
InferCreationAttributes<CustomerInvoiceItemTaxModel>
2025-09-04 17:57:04 +00:00
> {
2025-09-05 11:23:45 +00:00
declare tax_id: string;
2025-09-04 17:57:04 +00:00
declare item_id: string;
declare tax_code: string; //"iva_21"
// Taxable amount (base imponible) // 100,00 €
declare taxable_amount_value: number;
declare taxable_amount_scale: number;
// Total tax amount / taxes total // 21,00 €
2025-09-09 18:13:54 +00:00
declare taxes_amount_value: number;
declare taxes_amount_scale: number;
2025-09-04 17:57:04 +00:00
// Relaciones
declare item: NonAttribute<CustomerInvoiceItem>;
static associate(database: Sequelize) {
const { CustomerInvoiceItemModel } = database.models;
2025-09-09 15:48:12 +00:00
CustomerInvoiceItemTaxModel.belongsTo(CustomerInvoiceItemModel, {
2025-09-04 17:57:04 +00:00
as: "item",
2025-09-05 11:23:45 +00:00
targetKey: "item_id",
2025-09-04 17:57:04 +00:00
foreignKey: "item_id",
onDelete: "CASCADE",
2025-09-09 15:48:12 +00:00
onUpdate: "CASCADE",
2025-09-04 17:57:04 +00:00
});
}
static hooks(database: Sequelize) {}
}
export default (database: Sequelize) => {
2025-09-09 15:48:12 +00:00
CustomerInvoiceItemTaxModel.init(
2025-09-04 17:57:04 +00:00
{
2025-09-05 11:23:45 +00:00
tax_id: {
2025-09-09 15:48:12 +00:00
type: DataTypes.UUID,
2025-09-04 17:57:04 +00:00
primaryKey: true,
},
item_id: {
type: DataTypes.UUID,
allowNull: false,
},
tax_code: {
type: new DataTypes.STRING(),
allowNull: false,
},
taxable_amount_value: {
type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes
2025-09-26 18:09:14 +00:00
allowNull: false,
defaultValue: 0,
2025-09-04 17:57:04 +00:00
},
2025-09-09 18:13:54 +00:00
2025-09-04 17:57:04 +00:00
taxable_amount_scale: {
type: new DataTypes.SMALLINT(),
allowNull: false,
2025-09-26 18:09:14 +00:00
defaultValue: 4,
2025-09-04 17:57:04 +00:00
},
2025-09-09 18:13:54 +00:00
taxes_amount_value: {
2025-09-04 17:57:04 +00:00
type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes
2025-09-26 18:09:14 +00:00
allowNull: false,
defaultValue: 0,
2025-09-04 17:57:04 +00:00
},
2025-09-09 18:13:54 +00:00
taxes_amount_scale: {
2025-09-04 17:57:04 +00:00
type: new DataTypes.SMALLINT(),
allowNull: false,
2025-09-26 18:09:14 +00:00
defaultValue: 4,
2025-09-04 17:57:04 +00:00
},
},
{
sequelize: database,
2025-09-09 15:48:12 +00:00
tableName: "customer_invoice_item_taxes",
2025-09-04 17:57:04 +00:00
underscored: true,
2025-09-07 19:55:12 +00:00
indexes: [],
2025-09-04 17:57:04 +00:00
whereMergeStrategy: "and", // <- cómo tratar el merge de un scope
defaultScope: {},
scopes: {},
}
);
2025-09-09 15:48:12 +00:00
return CustomerInvoiceItemTaxModel;
2025-09-04 17:57:04 +00:00
};