188 lines
2.5 KiB
JavaScript
188 lines
2.5 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,
|
|
},
|
|
|
|
sku: {
|
|
type: DataTypes.TEXT,
|
|
|
|
|
|
|
|
},
|
|
|
|
name: {
|
|
type: DataTypes.TEXT,
|
|
|
|
|
|
|
|
},
|
|
|
|
description: {
|
|
type: DataTypes.TEXT,
|
|
|
|
|
|
|
|
},
|
|
|
|
item_type: {
|
|
type: DataTypes.ENUM,
|
|
|
|
|
|
|
|
values: [
|
|
|
|
"service",
|
|
|
|
|
|
"product"
|
|
|
|
],
|
|
|
|
},
|
|
|
|
sales_price: {
|
|
type: DataTypes.DECIMAL,
|
|
|
|
|
|
|
|
},
|
|
|
|
purchase_cost: {
|
|
type: DataTypes.DECIMAL,
|
|
|
|
|
|
|
|
},
|
|
|
|
is_active: {
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
allowNull: false,
|
|
defaultValue: false,
|
|
|
|
|
|
|
|
},
|
|
|
|
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,
|
|
});
|
|
|
|
|
|
|
|
db.items.hasMany(db.bill_lines, {
|
|
as: 'bill_lines_item',
|
|
foreignKey: {
|
|
name: 'itemId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//end loop
|
|
|
|
|
|
|
|
db.items.belongsTo(db.companies, {
|
|
as: 'company',
|
|
foreignKey: {
|
|
name: 'companyId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.items.belongsTo(db.chart_of_accounts, {
|
|
as: 'income_account',
|
|
foreignKey: {
|
|
name: 'income_accountId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.items.belongsTo(db.chart_of_accounts, {
|
|
as: 'expense_account',
|
|
foreignKey: {
|
|
name: 'expense_accountId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.items.belongsTo(db.tax_rates, {
|
|
as: 'tax_rate',
|
|
foreignKey: {
|
|
name: 'tax_rateId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
|
|
|
|
|
|
db.items.belongsTo(db.users, {
|
|
as: 'createdBy',
|
|
});
|
|
|
|
db.items.belongsTo(db.users, {
|
|
as: 'updatedBy',
|
|
});
|
|
};
|
|
|
|
|
|
|
|
return items;
|
|
};
|
|
|
|
|