37511-vm/backend/src/db/models/inventory_items.js
2026-01-16 13:36:36 +00:00

141 lines
1.9 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 inventory_items = sequelize.define(
'inventory_items',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
sku: {
type: DataTypes.TEXT,
},
location: {
type: DataTypes.TEXT,
},
quantity_on_hand: {
type: DataTypes.DECIMAL,
},
reserved_qty: {
type: DataTypes.DECIMAL,
},
lot_number: {
type: DataTypes.TEXT,
},
expiry_date: {
type: DataTypes.DATE,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
inventory_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.inventory_items.hasMany(db.stock_movements, {
as: 'stock_movements_inventory_item',
foreignKey: {
name: 'inventory_itemId',
},
constraints: false,
});
//end loop
db.inventory_items.belongsTo(db.materials, {
as: 'material',
foreignKey: {
name: 'materialId',
},
constraints: false,
});
db.inventory_items.belongsTo(db.warehouses, {
as: 'warehouse',
foreignKey: {
name: 'warehouseId',
},
constraints: false,
});
db.inventory_items.belongsTo(db.users, {
as: 'createdBy',
});
db.inventory_items.belongsTo(db.users, {
as: 'updatedBy',
});
};
return inventory_items;
};