350 lines
8.0 KiB
TypeScript
350 lines
8.0 KiB
TypeScript
import {
|
|
DataTypes,
|
|
InferAttributes,
|
|
InferCreationAttributes,
|
|
Model,
|
|
NonAttribute,
|
|
Sequelize,
|
|
} from "sequelize";
|
|
import {
|
|
CustomerInvoiceItemCreationAttributes,
|
|
CustomerInvoiceItemModel,
|
|
} from "./customer-invoice-item.model";
|
|
|
|
import { CustomerModel } from "@erp/customers/api";
|
|
import {
|
|
CustomerInvoiceTaxCreationAttributes,
|
|
CustomerInvoiceTaxModel,
|
|
} from "./customer-invoice-tax.model";
|
|
|
|
export type CustomerInvoiceCreationAttributes = InferCreationAttributes<
|
|
CustomerInvoiceModel,
|
|
{ omit: "items" | "taxes" | "current_customer" }
|
|
> & {
|
|
items?: CustomerInvoiceItemCreationAttributes[];
|
|
taxes?: CustomerInvoiceTaxCreationAttributes[];
|
|
};
|
|
|
|
export class CustomerInvoiceModel extends Model<
|
|
InferAttributes<CustomerInvoiceModel>,
|
|
InferCreationAttributes<CustomerInvoiceModel, { omit: "items" | "taxes" }>
|
|
> {
|
|
declare id: string;
|
|
declare company_id: string;
|
|
|
|
declare is_proforma: boolean;
|
|
declare status: string;
|
|
declare series: string;
|
|
declare invoice_number: string;
|
|
declare invoice_date: string;
|
|
declare operation_date: string;
|
|
declare language_code: string;
|
|
declare currency_code: string;
|
|
|
|
declare notes: string;
|
|
|
|
// Subtotal
|
|
declare subtotal_amount_value: number;
|
|
declare subtotal_amount_scale: number;
|
|
|
|
// Discount percentage
|
|
declare discount_percentage_value: number;
|
|
declare discount_percentage_scale: number;
|
|
|
|
// Discount amount
|
|
declare discount_amount_value: number;
|
|
declare discount_amount_scale: number;
|
|
|
|
// Taxable amount (base imponible)
|
|
declare taxable_amount_value: number;
|
|
declare taxable_amount_scale: number;
|
|
|
|
// Total taxes amount / taxes total
|
|
declare taxes_amount_value: number;
|
|
declare taxes_amount_scale: number;
|
|
|
|
// Total
|
|
declare total_amount_value: number;
|
|
declare total_amount_scale: number;
|
|
|
|
// Customer
|
|
declare customer_id: string;
|
|
declare customer_tin: string;
|
|
declare customer_name: string;
|
|
declare customer_street: string;
|
|
declare customer_street2: string;
|
|
declare customer_city: string;
|
|
declare customer_province: string;
|
|
declare customer_postal_code: string;
|
|
declare customer_country: string;
|
|
|
|
// Método de pago
|
|
declare payment_method_id: string;
|
|
declare payment_method_description: string;
|
|
|
|
// Relaciones
|
|
declare items: NonAttribute<CustomerInvoiceItemModel[]>;
|
|
declare taxes: NonAttribute<CustomerInvoiceTaxModel[]>;
|
|
declare current_customer: NonAttribute<CustomerModel>;
|
|
|
|
static associate(database: Sequelize) {
|
|
const {
|
|
CustomerInvoiceModel,
|
|
CustomerInvoiceItemModel,
|
|
CustomerModel,
|
|
CustomerInvoiceTaxModel,
|
|
} = database.models;
|
|
|
|
CustomerInvoiceModel.belongsTo(CustomerModel, {
|
|
as: "current_customer",
|
|
foreignKey: "customer_id",
|
|
constraints: false,
|
|
});
|
|
|
|
CustomerInvoiceModel.hasMany(CustomerInvoiceItemModel, {
|
|
as: "items",
|
|
foreignKey: "invoice_id",
|
|
sourceKey: "id",
|
|
constraints: true,
|
|
onDelete: "CASCADE",
|
|
onUpdate: "CASCADE",
|
|
});
|
|
|
|
CustomerInvoiceModel.hasMany(CustomerInvoiceTaxModel, {
|
|
as: "taxes",
|
|
foreignKey: "invoice_id",
|
|
sourceKey: "id",
|
|
constraints: true,
|
|
onDelete: "CASCADE",
|
|
onUpdate: "CASCADE",
|
|
});
|
|
}
|
|
|
|
static hooks(database: Sequelize) {}
|
|
}
|
|
|
|
export default (database: Sequelize) => {
|
|
CustomerInvoiceModel.init(
|
|
{
|
|
id: {
|
|
type: DataTypes.UUID,
|
|
primaryKey: true,
|
|
},
|
|
|
|
company_id: {
|
|
type: DataTypes.UUID,
|
|
allowNull: false,
|
|
},
|
|
|
|
is_proforma: {
|
|
type: DataTypes.BOOLEAN,
|
|
allowNull: false,
|
|
defaultValue: false,
|
|
},
|
|
|
|
status: {
|
|
type: new DataTypes.STRING(),
|
|
allowNull: false,
|
|
defaultValue: "draft",
|
|
},
|
|
|
|
series: {
|
|
type: new DataTypes.STRING(),
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
invoice_number: {
|
|
type: new DataTypes.STRING(),
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
invoice_date: {
|
|
type: new DataTypes.DATEONLY(),
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
operation_date: {
|
|
type: new DataTypes.DATEONLY(),
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
language_code: {
|
|
type: DataTypes.STRING(2),
|
|
allowNull: false,
|
|
defaultValue: "es",
|
|
},
|
|
|
|
currency_code: {
|
|
type: new DataTypes.STRING(3),
|
|
allowNull: false,
|
|
},
|
|
|
|
notes: {
|
|
type: new DataTypes.TEXT(),
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
payment_method_id: {
|
|
type: DataTypes.UUID,
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
payment_method_description: {
|
|
type: new DataTypes.STRING(),
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
subtotal_amount_value: {
|
|
type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes
|
|
allowNull: false,
|
|
defaultValue: 0,
|
|
},
|
|
subtotal_amount_scale: {
|
|
type: new DataTypes.SMALLINT(),
|
|
allowNull: false,
|
|
defaultValue: 2,
|
|
},
|
|
|
|
discount_percentage_value: {
|
|
type: new DataTypes.SMALLINT(),
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
discount_percentage_scale: {
|
|
type: new DataTypes.SMALLINT(),
|
|
allowNull: false,
|
|
defaultValue: 2,
|
|
},
|
|
|
|
discount_amount_value: {
|
|
type: new DataTypes.BIGINT(),
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
discount_amount_scale: {
|
|
type: new DataTypes.SMALLINT(),
|
|
allowNull: false,
|
|
defaultValue: 2,
|
|
},
|
|
|
|
taxable_amount_value: {
|
|
type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
taxable_amount_scale: {
|
|
type: new DataTypes.SMALLINT(),
|
|
allowNull: false,
|
|
defaultValue: 2,
|
|
},
|
|
|
|
taxes_amount_value: {
|
|
type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
taxes_amount_scale: {
|
|
type: new DataTypes.SMALLINT(),
|
|
allowNull: false,
|
|
defaultValue: 2,
|
|
},
|
|
|
|
total_amount_value: {
|
|
type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
total_amount_scale: {
|
|
type: new DataTypes.SMALLINT(),
|
|
allowNull: false,
|
|
defaultValue: 2,
|
|
},
|
|
|
|
customer_id: {
|
|
type: DataTypes.UUID,
|
|
allowNull: false,
|
|
},
|
|
|
|
customer_tin: {
|
|
type: DataTypes.STRING,
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
customer_name: {
|
|
type: DataTypes.STRING,
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
customer_street: {
|
|
type: DataTypes.STRING,
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
customer_street2: {
|
|
type: DataTypes.STRING,
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
customer_city: {
|
|
type: DataTypes.STRING,
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
customer_province: {
|
|
type: DataTypes.STRING,
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
customer_postal_code: {
|
|
type: DataTypes.STRING,
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
|
|
customer_country: {
|
|
type: DataTypes.STRING,
|
|
allowNull: true,
|
|
defaultValue: null,
|
|
},
|
|
},
|
|
{
|
|
sequelize: database,
|
|
tableName: "customer_invoices",
|
|
|
|
underscored: true,
|
|
paranoid: true, // softs deletes
|
|
timestamps: true,
|
|
|
|
createdAt: "created_at",
|
|
updatedAt: "updated_at",
|
|
deletedAt: "deleted_at",
|
|
|
|
indexes: [{ name: "company_idx", fields: ["company_id"], unique: false }],
|
|
|
|
whereMergeStrategy: "and", // <- cómo tratar el merge de un scope
|
|
|
|
defaultScope: {},
|
|
|
|
scopes: {},
|
|
}
|
|
);
|
|
|
|
return CustomerInvoiceModel;
|
|
};
|