Uecko_ERP/apps/server/src/contexts/customer-billing/infraestructure/sequelize/customer-invoice.model.ts

252 lines
5.8 KiB
TypeScript
Raw Normal View History

2025-02-24 19:00:28 +00:00
import {
CreationOptional,
DataTypes,
InferAttributes,
InferCreationAttributes,
Model,
NonAttribute,
Sequelize,
} from "sequelize";
import { CustomerInvoiceItemModel } from "./customer-invoice-item.model";
import { CustomerModel } from "./customer.model";
export type CustomerInvoiceCreationAttributes = InferCreationAttributes<
CustomerInvoiceModel,
{ omit: "items" | "customer" }
> & {
//items: CustomerInvoiceItemCreationAttributes[];
customer_id: string;
};
export class CustomerInvoiceModel extends Model<
InferAttributes<CustomerInvoiceModel, { omit: "items" | "customer" }>,
InferCreationAttributes<CustomerInvoiceModel, { omit: "items" | "customer" }>
> {
// To avoid table creation
/*static async sync(): Promise<any> {
return Promise.resolve();
}*/
static associate(connection: Sequelize) {
const { CustomerInvoiceModel, CustomerInvoiceItemModel, CustomerModel } = connection.models;
CustomerInvoiceModel.hasMany(CustomerInvoiceItemModel, {
as: "items",
foreignKey: "quote_id",
onDelete: "CASCADE",
});
CustomerInvoiceModel.belongsTo(CustomerModel, {
foreignKey: "dealer_id",
as: "dealer",
onDelete: "RESTRICT",
});
}
declare id: string;
declare status: string;
declare issue_date: string;
declare invoice_number: string;
2025-02-25 17:27:07 +00:00
declare invoice_type: string;
2025-02-24 19:00:28 +00:00
declare lang_code: string;
declare currency_code: string;
declare customer_id: string;
declare customer_tin: string;
declare customer_name: string;
declare customer_reference: CreationOptional<string>;
declare customer_street: string;
declare customer_city: string;
declare customer_state: string;
declare customer_postal_code: string;
declare customer_country: string;
declare subtotal_price: CreationOptional<number | null>;
declare discount: CreationOptional<number | null>;
declare discount_price: CreationOptional<number | null>;
declare before_tax_price: CreationOptional<number | null>;
declare tax: CreationOptional<number | null>;
declare tax_price: CreationOptional<number | null>;
declare total_price: CreationOptional<number | null>;
declare notes: CreationOptional<string>;
declare items: NonAttribute<CustomerInvoiceItemModel[]>;
declare customer: NonAttribute<CustomerModel>;
declare integrity_hash: CreationOptional<string>;
declare previous_invoice_id: CreationOptional<string>;
declare signed_at: CreationOptional<string>;
}
export default (sequelize: Sequelize) => {
CustomerInvoiceModel.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
},
status: {
type: new DataTypes.STRING(),
allowNull: false,
},
issue_date: {
type: new DataTypes.DATEONLY(),
allowNull: false,
},
invoice_number: {
type: DataTypes.STRING(),
allowNull: false,
},
2025-02-25 17:27:07 +00:00
invoice_type: {
2025-02-24 19:00:28 +00:00
type: new DataTypes.STRING(),
allowNull: false,
},
lang_code: {
type: DataTypes.STRING(2),
allowNull: false,
defaultValue: "es",
},
currency_code: {
type: new DataTypes.STRING(3),
allowNull: false,
defaultValue: "EUR",
},
customer_id: {
type: new DataTypes.UUID(),
primaryKey: true,
},
customer_name: {
type: DataTypes.STRING,
allowNull: false,
},
customer_tin: {
type: DataTypes.STRING,
allowNull: false,
},
customer_reference: {
type: new DataTypes.STRING(),
},
customer_street: {
type: DataTypes.STRING,
allowNull: false,
},
customer_city: {
type: DataTypes.STRING,
allowNull: false,
},
customer_state: {
type: DataTypes.STRING,
allowNull: false,
},
customer_postal_code: {
type: DataTypes.STRING,
allowNull: false,
},
customer_country: {
type: DataTypes.STRING,
allowNull: false,
},
subtotal_price: {
type: new DataTypes.BIGINT(),
allowNull: true,
},
discount: {
type: new DataTypes.SMALLINT(),
allowNull: true,
},
discount_price: {
type: new DataTypes.BIGINT(),
allowNull: true,
},
before_tax_price: {
type: new DataTypes.BIGINT(),
allowNull: true,
},
tax: {
type: new DataTypes.SMALLINT(),
allowNull: true,
},
tax_price: {
type: new DataTypes.BIGINT(),
allowNull: true,
},
total_price: {
type: new DataTypes.BIGINT(),
allowNull: true,
},
notes: {
type: DataTypes.TEXT,
},
integrity_hash: {
type: DataTypes.STRING,
allowNull: false,
comment: "Hash criptográfico para asegurar integridad",
},
previous_invoice_id: {
type: DataTypes.UUID,
allowNull: true,
comment: "Referencia a la factura anterior (si aplica)",
},
signed_at: {
type: DataTypes.DATE,
allowNull: false,
comment: "Fecha en que la factura fue firmada digitalmente",
},
},
{
sequelize,
tableName: "customer_invoices",
paranoid: true, // softs deletes
timestamps: true,
createdAt: "created_at",
updatedAt: "updated_at",
deletedAt: "deleted_at",
indexes: [
{ name: "status_idx", fields: ["status"] },
{ name: "reference_idx", fields: ["reference"] },
{ name: "deleted_at_idx", fields: ["deleted_at"] },
{ name: "signed_at_idx", fields: ["signed_at"] },
],
whereMergeStrategy: "and", // <- cómo tratar el merge de un scope
defaultScope: {},
scopes: {},
}
);
return CustomerInvoiceModel;
};