40196-vm/backend/src/db/models/materials.js
2026-06-04 15:57:53 +00:00

339 lines
5.2 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,
},
name: {
type: DataTypes.TEXT,
},
material_number: {
type: DataTypes.TEXT,
},
material_type: {
type: DataTypes.ENUM,
values: [
"raw",
"subassembly",
"finished_good",
"packaging",
"consumable",
"spare_part"
],
},
uom: {
type: DataTypes.TEXT,
},
revision: {
type: DataTypes.TEXT,
},
lot_tracked: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
serial_tracked: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
active: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
description: {
type: DataTypes.TEXT,
},
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.bom_headers, {
as: 'bom_headers_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.lots, {
as: 'lots_material',
foreignKey: {
name: 'materialId',
},
constraints: false,
});
db.materials.hasMany(db.serials, {
as: 'serials_material',
foreignKey: {
name: 'materialId',
},
constraints: false,
});
db.materials.hasMany(db.inventory_balances, {
as: 'inventory_balances_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.cycle_count_lines, {
as: 'cycle_count_lines_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.material_allocations, {
as: 'material_allocations_component_material',
foreignKey: {
name: 'component_materialId',
},
constraints: false,
});
db.materials.hasMany(db.production_lots, {
as: 'production_lots_material',
foreignKey: {
name: 'materialId',
},
constraints: false,
});
db.materials.hasMany(db.inspection_plans, {
as: 'inspection_plans_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,
});
db.materials.hasMany(db.shipment_lines, {
as: 'shipment_lines_material',
foreignKey: {
name: 'materialId',
},
constraints: false,
});
//end loop
db.materials.belongsTo(db.organizations, {
as: 'organizations',
foreignKey: {
name: 'organizationsId',
},
constraints: false,
});
db.materials.hasMany(db.file, {
as: 'images',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.materials.getTableName(),
belongsToColumn: 'images',
},
});
db.materials.hasMany(db.file, {
as: 'spec_files',
foreignKey: 'belongsToId',
constraints: false,
scope: {
belongsTo: db.materials.getTableName(),
belongsToColumn: 'spec_files',
},
});
db.materials.belongsTo(db.users, {
as: 'createdBy',
});
db.materials.belongsTo(db.users, {
as: 'updatedBy',
});
};
return materials;
};