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

264 lines
6.0 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";
export type CustomerInvoiceCreationAttributes = InferCreationAttributes<
CustomerInvoiceModel,
2025-02-25 19:17:30 +00:00
{ omit: "items" }
2025-02-24 19:00:28 +00:00
> & {
2025-02-25 19:17:30 +00:00
// creo que no es necesario
2025-02-24 19:00:28 +00:00
//items: CustomerInvoiceItemCreationAttributes[];
2025-02-25 19:17:30 +00:00
//customer_id: string;
2025-02-24 19:00:28 +00:00
};
export class CustomerInvoiceModel extends Model<
2025-02-25 19:17:30 +00:00
InferAttributes<CustomerInvoiceModel, { omit: "items" }>,
InferCreationAttributes<CustomerInvoiceModel, { omit: "items" }>
2025-02-24 19:00:28 +00:00
> {
// 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",
2025-02-25 19:17:30 +00:00
foreignKey: "customer_invoice_id",
2025-02-24 19:00:28 +00:00
onDelete: "CASCADE",
});
}
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-25 19:17:30 +00:00
declare invoice_customer_reference?: CreationOptional<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_street: string;
2025-02-25 19:17:30 +00:00
declare customer_street2?: CreationOptional<string>;
2025-02-24 19:00:28 +00:00
declare customer_city: string;
declare customer_state: string;
declare customer_postal_code: string;
declare customer_country: string;
2025-02-25 19:17:30 +00:00
declare subtotal_price?: CreationOptional<number>;
2025-02-24 19:00:28 +00:00
2025-02-25 19:17:30 +00:00
declare discount?: CreationOptional<number>;
declare discount_price?: CreationOptional<number>;
2025-02-24 19:00:28 +00:00
2025-02-25 19:17:30 +00:00
declare before_tax_price?: CreationOptional<number>;
2025-02-24 19:00:28 +00:00
2025-02-25 19:17:30 +00:00
declare tax?: CreationOptional<number>;
declare tax_price?: CreationOptional<number>;
2025-02-24 19:00:28 +00:00
2025-02-25 19:17:30 +00:00
declare total_price?: CreationOptional<number>;
2025-02-24 19:00:28 +00:00
2025-02-25 19:17:30 +00:00
declare notes?: CreationOptional<string>;
2025-02-24 19:00:28 +00:00
declare items: NonAttribute<CustomerInvoiceItemModel[]>;
2025-02-25 19:17:30 +00:00
declare integrity_hash?: CreationOptional<string>;
declare previous_invoice_id?: CreationOptional<string>;
declare signed_at?: CreationOptional<string>;
2025-02-24 19:00:28 +00:00
}
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 19:17:30 +00:00
invoice_customer_reference: {
type: new DataTypes.STRING(),
},
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(),
2025-03-04 17:08:33 +00:00
allowNull: false,
2025-02-24 19:00:28 +00:00
},
customer_name: {
type: DataTypes.STRING,
allowNull: false,
},
customer_tin: {
type: DataTypes.STRING,
allowNull: false,
},
customer_street: {
type: DataTypes.STRING,
allowNull: false,
},
2025-02-25 19:17:30 +00:00
customer_street2: {
type: DataTypes.STRING,
allowNull: true,
2025-03-04 17:08:33 +00:00
defaultValue: null,
2025-02-25 19:17:30 +00:00
},
2025-02-24 19:00:28 +00:00
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,
2025-03-04 17:08:33 +00:00
defaultValue: null,
2025-02-24 19:00:28 +00:00
},
discount: {
type: new DataTypes.SMALLINT(),
allowNull: true,
2025-03-04 17:08:33 +00:00
defaultValue: null,
2025-02-24 19:00:28 +00:00
},
discount_price: {
type: new DataTypes.BIGINT(),
allowNull: true,
2025-03-04 17:08:33 +00:00
defaultValue: null,
2025-02-24 19:00:28 +00:00
},
before_tax_price: {
type: new DataTypes.BIGINT(),
allowNull: true,
2025-03-04 17:08:33 +00:00
defaultValue: null,
2025-02-24 19:00:28 +00:00
},
tax: {
type: new DataTypes.SMALLINT(),
allowNull: true,
2025-03-04 17:08:33 +00:00
defaultValue: null,
2025-02-24 19:00:28 +00:00
},
tax_price: {
type: new DataTypes.BIGINT(),
allowNull: true,
2025-03-04 17:08:33 +00:00
defaultValue: null,
2025-02-24 19:00:28 +00:00
},
total_price: {
type: new DataTypes.BIGINT(),
allowNull: true,
2025-03-04 17:08:33 +00:00
defaultValue: null,
2025-02-24 19:00:28 +00:00
},
notes: {
type: DataTypes.TEXT,
2025-02-25 19:17:30 +00:00
allowNull: true,
2025-03-04 17:08:33 +00:00
defaultValue: null,
2025-02-24 19:00:28 +00:00
},
integrity_hash: {
type: DataTypes.STRING,
2025-02-25 19:17:30 +00:00
allowNull: true,
2025-03-04 17:08:33 +00:00
defaultValue: null,
2025-02-24 19:00:28 +00:00
comment: "Hash criptográfico para asegurar integridad",
},
previous_invoice_id: {
type: DataTypes.UUID,
allowNull: true,
2025-03-04 17:08:33 +00:00
defaultValue: null,
2025-02-24 19:00:28 +00:00
comment: "Referencia a la factura anterior (si aplica)",
},
signed_at: {
type: DataTypes.DATE,
2025-02-25 19:17:30 +00:00
allowNull: true,
2025-03-04 17:08:33 +00:00
defaultValue: null,
2025-02-24 19:00:28 +00:00
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"] },
2025-02-25 19:17:30 +00:00
{ name: "invoice_number_idx", fields: ["invoice_number"] },
2025-02-24 19:00:28 +00:00
{ 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;
};