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

178 lines
4.2 KiB
TypeScript
Raw Normal View History

2025-04-01 14:26:15 +00:00
import {
DataTypes,
InferAttributes,
InferCreationAttributes,
Model,
2025-04-22 08:11:50 +00:00
NonAttribute,
2025-04-01 14:26:15 +00:00
Sequelize,
} from "sequelize";
2025-06-12 06:55:17 +00:00
import {
CustomerInvoiceItemCreationAttributes,
CustomerInvoiceItemModel,
} from "./customer-invoice-item.model";
export type CustomerInvoiceCreationAttributes = InferCreationAttributes<
CustomerInvoiceModel,
{ omit: "items" }
> & {
2025-06-11 15:13:44 +00:00
items?: CustomerInvoiceItemCreationAttributes[];
2025-04-22 08:11:50 +00:00
};
2025-04-01 14:26:15 +00:00
2025-06-11 15:13:44 +00:00
export class CustomerInvoiceModel extends Model<
InferAttributes<CustomerInvoiceModel>,
InferCreationAttributes<CustomerInvoiceModel, { omit: "items" }>
2025-05-02 21:43:51 +00:00
> {
2025-04-01 14:26:15 +00:00
declare id: string;
2025-09-03 10:41:12 +00:00
declare company_id: string;
2025-04-01 14:26:15 +00:00
2025-09-03 10:41:12 +00:00
declare status: string;
declare series: string;
declare invoice_number: string;
declare issue_date: string;
declare operation_date: string;
declare language_code: string;
declare currency_code: string;
2025-04-01 14:26:15 +00:00
// Subtotal
2025-09-03 10:41:12 +00:00
declare subtotal_amount: number;
declare subtotal_scale: number;
2025-04-01 14:26:15 +00:00
// Total
2025-09-03 10:41:12 +00:00
declare total_amount: number;
declare total_scale: number;
2025-04-01 14:26:15 +00:00
2025-05-02 21:43:51 +00:00
// Relaciones
2025-06-11 15:13:44 +00:00
declare items: NonAttribute<CustomerInvoiceItemModel[]>;
//declare customer: NonAttribute<CustomerInvoiceParticipant_Model[]>;
2025-05-02 21:43:51 +00:00
static associate(database: Sequelize) {
2025-06-11 15:13:44 +00:00
const { CustomerInvoiceModel, CustomerInvoiceItemModel } = database.models;
2025-05-02 21:43:51 +00:00
2025-06-11 15:13:44 +00:00
CustomerInvoiceModel.hasMany(CustomerInvoiceItemModel, {
2025-05-02 21:43:51 +00:00
as: "items",
2025-06-12 06:55:17 +00:00
foreignKey: "invoice_id",
sourceKey: "id",
2025-05-02 21:43:51 +00:00
onDelete: "CASCADE",
constraints: true,
});
}
static hooks(database: Sequelize) {
// Soft-cascade manual: al borrar una factura, marcamos items como borrados (paranoid).
2025-09-03 10:41:12 +00:00
/*CustomerInvoiceModel.addHook("afterDestroy", async (invoice, options) => {
if (!invoice?.id) return;
await CustomerInvoiceItemModel.destroy({
2025-09-03 10:41:12 +00:00
where: { invoice_id: invoice.id },
individualHooks: true,
transaction: options.transaction,
});
2025-09-03 10:41:12 +00:00
});*/
2025-05-02 21:43:51 +00:00
}
2025-04-01 14:26:15 +00:00
}
2025-05-02 21:43:51 +00:00
export default (database: Sequelize) => {
2025-06-11 15:13:44 +00:00
CustomerInvoiceModel.init(
2025-04-01 14:26:15 +00:00
{
id: {
type: new DataTypes.UUID(),
primaryKey: true,
},
2025-09-03 10:41:12 +00:00
company_id: {
type: DataTypes.UUID,
allowNull: false,
},
status: {
2025-04-01 14:26:15 +00:00
type: new DataTypes.STRING(),
allowNull: false,
2025-09-03 10:41:12 +00:00
defaultValue: "draft",
2025-04-01 14:26:15 +00:00
},
2025-09-03 10:41:12 +00:00
series: {
2025-04-01 14:26:15 +00:00
type: new DataTypes.STRING(),
allowNull: true,
defaultValue: null,
},
2025-06-12 06:55:17 +00:00
invoice_number: {
2025-04-01 14:26:15 +00:00
type: new DataTypes.STRING(),
allowNull: true,
defaultValue: null,
},
issue_date: {
2025-04-01 15:32:53 +00:00
type: new DataTypes.DATEONLY(),
2025-04-01 14:26:15 +00:00
allowNull: true,
defaultValue: null,
},
operation_date: {
2025-04-01 15:32:53 +00:00
type: new DataTypes.DATEONLY(),
2025-04-01 14:26:15 +00:00
allowNull: true,
defaultValue: null,
},
2025-09-03 10:41:12 +00:00
language_code: {
type: DataTypes.STRING(2),
2025-04-01 14:26:15 +00:00
allowNull: false,
2025-09-03 10:41:12 +00:00
defaultValue: "es",
2025-04-01 14:26:15 +00:00
},
2025-09-03 10:41:12 +00:00
currency_code: {
type: new DataTypes.STRING(3),
2025-04-01 14:26:15 +00:00
allowNull: false,
2025-09-03 10:41:12 +00:00
defaultValue: "EUR",
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(),
allowNull: true,
defaultValue: null,
},
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,
},
},
{
2025-05-02 21:43:51 +00:00
sequelize: database,
2025-06-12 06:55:17 +00:00
tableName: "customer_invoices",
2025-04-01 14:26:15 +00:00
underscored: true,
2025-04-01 14:26:15 +00:00
paranoid: true, // softs deletes
timestamps: true,
createdAt: "created_at",
updatedAt: "updated_at",
deletedAt: "deleted_at",
2025-09-03 10:41:12 +00:00
indexes: [
{ name: "company_idx", fields: ["company_id"], unique: false },
{ name: "idx_company_idx", fields: ["id", "company_id"], unique: true },
{ unique: true, fields: ["invoice_number"] },
],
2025-04-01 14:26:15 +00:00
whereMergeStrategy: "and", // <- cómo tratar el merge de un scope
defaultScope: {},
scopes: {},
}
);
2025-06-11 15:13:44 +00:00
return CustomerInvoiceModel;
2025-04-01 14:26:15 +00:00
};