37431-vm/backend/src/db/models/materials.js
2026-01-13 11:35:23 +00:00

341 lines
4.8 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 materials = sequelize.define(
'materials',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
material_code: {
type: DataTypes.TEXT,
},
material_name: {
type: DataTypes.TEXT,
},
material_type: {
type: DataTypes.ENUM,
values: [
"raw",
"component",
"subassembly",
"finished_good",
"consumable",
"spare_part"
],
},
standard_cost: {
type: DataTypes.DECIMAL,
},
standard_price: {
type: DataTypes.DECIMAL,
},
planning_method: {
type: DataTypes.ENUM,
values: [
"make_to_stock",
"make_to_order"
],
},
is_lot_tracked: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
is_serial_tracked: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
requires_inspection: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
shelf_life_days: {
type: DataTypes.INTEGER,
},
specification: {
type: DataTypes.TEXT,
},
status: {
type: DataTypes.ENUM,
values: [
"active",
"inactive",
"obsolete"
],
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
materials.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.materials.hasMany(db.material_revisions, {
as: 'material_revisions_material',
foreignKey: {
name: 'materialId',
},
constraints: false,
});
db.materials.hasMany(db.boms, {
as: 'boms_parent_material',
foreignKey: {
name: 'parent_materialId',
},
constraints: false,
});
db.materials.hasMany(db.bom_lines, {
as: 'bom_lines_component_material',
foreignKey: {
name: 'component_materialId',
},
constraints: false,
});
db.materials.hasMany(db.routings, {
as: 'routings_material',
foreignKey: {
name: 'materialId',
},
constraints: false,
});
db.materials.hasMany(db.production_orders, {
as: 'production_orders_material',
foreignKey: {
name: 'materialId',
},
constraints: false,
});
db.materials.hasMany(db.inventory_lots, {
as: 'inventory_lots_material',
foreignKey: {
name: 'materialId',
},
constraints: false,
});
db.materials.hasMany(db.inventory_transactions, {
as: 'inventory_transactions_material',
foreignKey: {
name: 'materialId',
},
constraints: false,
});
db.materials.hasMany(db.purchase_order_lines, {
as: 'purchase_order_lines_material',
foreignKey: {
name: 'materialId',
},
constraints: false,
});
db.materials.hasMany(db.quality_standards, {
as: 'quality_standards_material',
foreignKey: {
name: 'materialId',
},
constraints: false,
});
db.materials.hasMany(db.inspections, {
as: 'inspections_material',
foreignKey: {
name: 'materialId',
},
constraints: false,
});
db.materials.hasMany(db.nonconformances, {
as: 'nonconformances_material',
foreignKey: {
name: 'materialId',
},
constraints: false,
});
//end loop
db.materials.belongsTo(db.units_of_measure, {
as: 'default_uom',
foreignKey: {
name: 'default_uomId',
},
constraints: false,
});
db.materials.belongsTo(db.suppliers, {
as: 'preferred_supplier',
foreignKey: {
name: 'preferred_supplierId',
},
constraints: false,
});
db.materials.hasMany(db.file, {
as: 'attachments',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.materials.getTableName(),
belongsToColumn: 'attachments',
},
});
db.materials.belongsTo(db.users, {
as: 'createdBy',
});
db.materials.belongsTo(db.users, {
as: 'updatedBy',
});
};
return materials;
};