2026-06-22 17:52:45 +00:00

194 lines
2.3 KiB
JavaScript

const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');
module.exports = function(sequelize, DataTypes) {
const items = sequelize.define(
'items',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
item_name: {
type: DataTypes.TEXT,
},
sku: {
type: DataTypes.TEXT,
},
item_type: {
type: DataTypes.ENUM,
values: [
"product",
"service"
],
},
unit_name: {
type: DataTypes.TEXT,
},
unit_price: {
type: DataTypes.DECIMAL,
},
cost_price: {
type: DataTypes.DECIMAL,
},
is_taxable: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
tax_rate: {
type: DataTypes.DECIMAL,
},
stock_quantity: {
type: DataTypes.INTEGER,
},
low_stock_threshold: {
type: DataTypes.INTEGER,
},
description: {
type: DataTypes.TEXT,
},
status: {
type: DataTypes.ENUM,
values: [
"active",
"inactive"
],
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
items.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.items.hasMany(db.invoice_lines, {
as: 'invoice_lines_item',
foreignKey: {
name: 'itemId',
},
constraints: false,
});
//end loop
db.items.hasMany(db.file, {
as: 'images',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.items.getTableName(),
belongsToColumn: 'images',
},
});
db.items.belongsTo(db.users, {
as: 'createdBy',
});
db.items.belongsTo(db.users, {
as: 'updatedBy',
});
};
return items;
};