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

116 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";
import { CustomerInvoice } from "../../domain";
2025-09-09 15:48:12 +00:00
export type CustomerInvoiceTaxCreationAttributes = InferCreationAttributes<
CustomerInvoiceTaxModel,
{ omit: "invoice" }
2025-09-04 17:57:04 +00:00
>;
2025-09-09 15:48:12 +00:00
export class CustomerInvoiceTaxModel extends Model<
InferAttributes<CustomerInvoiceTaxModel>,
InferCreationAttributes<CustomerInvoiceTaxModel>
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 invoice_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 invoice: NonAttribute<CustomerInvoice>;
static associate(database: Sequelize) {
const { CustomerInvoiceModel } = database.models;
2025-09-09 15:48:12 +00:00
CustomerInvoiceTaxModel.belongsTo(CustomerInvoiceModel, {
2025-09-04 17:57:04 +00:00
as: "invoice",
targetKey: "id",
foreignKey: "invoice_id",
onDelete: "CASCADE",
});
}
static hooks(database: Sequelize) {}
}
export default (database: Sequelize) => {
2025-09-09 15:48:12 +00:00
CustomerInvoiceTaxModel.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,
},
invoice_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
allowNull: true,
defaultValue: null,
},
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,
defaultValue: 2,
},
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
allowNull: true,
defaultValue: null,
},
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,
defaultValue: 2,
},
},
{
sequelize: database,
2025-09-09 15:48:12 +00:00
tableName: "customer_invoice_taxes",
2025-09-04 17:57:04 +00:00
underscored: true,
2025-09-09 15:48:12 +00:00
indexes: [
{
name: "invoice_id_idx",
fields: ["invoice_id"],
unique: false,
},
],
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 CustomerInvoiceTaxModel;
2025-09-04 17:57:04 +00:00
};