682 lines
17 KiB
JavaScript
682 lines
17 KiB
JavaScript
|
|
const db = require('../models');
|
|
const FileDBApi = require('./file');
|
|
const crypto = require('crypto');
|
|
const Utils = require('../utils');
|
|
|
|
|
|
|
|
const Sequelize = db.Sequelize;
|
|
const Op = Sequelize.Op;
|
|
|
|
module.exports = class Purchase_ordersDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const purchase_orders = await db.purchase_orders.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
po_number: data.po_number
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
order_date: data.order_date
|
|
||
|
|
null
|
|
,
|
|
|
|
expected_at: data.expected_at
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await purchase_orders.setSupplier( data.supplier || null, {
|
|
transaction,
|
|
});
|
|
|
|
await purchase_orders.setWarehouse( data.warehouse || null, {
|
|
transaction,
|
|
});
|
|
|
|
await purchase_orders.setRequested_by( data.requested_by || null, {
|
|
transaction,
|
|
});
|
|
|
|
await purchase_orders.setOrganizations( data.organizations || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.purchase_orders.getTableName(),
|
|
belongsToColumn: 'attachments',
|
|
belongsToId: purchase_orders.id,
|
|
},
|
|
data.attachments,
|
|
options,
|
|
);
|
|
|
|
|
|
return purchase_orders;
|
|
}
|
|
|
|
|
|
static async bulkImport(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
// Prepare data - wrapping individual data transformations in a map() method
|
|
const purchase_ordersData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
po_number: item.po_number
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
order_date: item.order_date
|
|
||
|
|
null
|
|
,
|
|
|
|
expected_at: item.expected_at
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: item.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const purchase_orders = await db.purchase_orders.bulkCreate(purchase_ordersData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < purchase_orders.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.purchase_orders.getTableName(),
|
|
belongsToColumn: 'attachments',
|
|
belongsToId: purchase_orders[i].id,
|
|
},
|
|
data[i].attachments,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return purchase_orders;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const globalAccess = currentUser.app_role?.globalAccess;
|
|
|
|
const purchase_orders = await db.purchase_orders.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.po_number !== undefined) updatePayload.po_number = data.po_number;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.order_date !== undefined) updatePayload.order_date = data.order_date;
|
|
|
|
|
|
if (data.expected_at !== undefined) updatePayload.expected_at = data.expected_at;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await purchase_orders.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.supplier !== undefined) {
|
|
await purchase_orders.setSupplier(
|
|
|
|
data.supplier,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.warehouse !== undefined) {
|
|
await purchase_orders.setWarehouse(
|
|
|
|
data.warehouse,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.requested_by !== undefined) {
|
|
await purchase_orders.setRequested_by(
|
|
|
|
data.requested_by,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.organizations !== undefined) {
|
|
await purchase_orders.setOrganizations(
|
|
|
|
data.organizations,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.purchase_orders.getTableName(),
|
|
belongsToColumn: 'attachments',
|
|
belongsToId: purchase_orders.id,
|
|
},
|
|
data.attachments,
|
|
options,
|
|
);
|
|
|
|
|
|
return purchase_orders;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const purchase_orders = await db.purchase_orders.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of purchase_orders) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of purchase_orders) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return purchase_orders;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const purchase_orders = await db.purchase_orders.findByPk(id, options);
|
|
|
|
await purchase_orders.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await purchase_orders.destroy({
|
|
transaction
|
|
});
|
|
|
|
return purchase_orders;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const purchase_orders = await db.purchase_orders.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!purchase_orders) {
|
|
return purchase_orders;
|
|
}
|
|
|
|
const output = purchase_orders.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.purchase_order_lines_purchase_order = await purchase_orders.getPurchase_order_lines_purchase_order({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.supplier = await purchase_orders.getSupplier({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.warehouse = await purchase_orders.getWarehouse({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.requested_by = await purchase_orders.getRequested_by({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.attachments = await purchase_orders.getAttachments({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.organizations = await purchase_orders.getOrganizations({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
globalAccess, options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
const userOrganizations = (user && user.organizations?.id) || null;
|
|
|
|
|
|
|
|
if (userOrganizations) {
|
|
if (options?.currentUser?.organizationsId) {
|
|
where.organizationsId = options.currentUser.organizationsId;
|
|
}
|
|
}
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.suppliers,
|
|
as: 'supplier',
|
|
|
|
where: filter.supplier ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.supplier.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.supplier.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.warehouses,
|
|
as: 'warehouse',
|
|
|
|
where: filter.warehouse ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.warehouse.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.warehouse.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'requested_by',
|
|
|
|
where: filter.requested_by ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.requested_by.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.requested_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.organizations,
|
|
as: 'organizations',
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'attachments',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.po_number) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'purchase_orders',
|
|
'po_number',
|
|
filter.po_number,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'purchase_orders',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.order_dateRange) {
|
|
const [start, end] = filter.order_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
order_date: {
|
|
...where.order_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
order_date: {
|
|
...where.order_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.expected_atRange) {
|
|
const [start, end] = filter.expected_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
expected_at: {
|
|
...where.expected_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
expected_at: {
|
|
...where.expected_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.organizations) {
|
|
const listItems = filter.organizations.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
organizationsId: {[Op.or]: listItems}
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.createdAtRange) {
|
|
const [start, end] = filter.createdAtRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (globalAccess) {
|
|
delete where.organizationsId;
|
|
}
|
|
|
|
|
|
const queryOptions = {
|
|
where,
|
|
include,
|
|
distinct: true,
|
|
order: filter.field && filter.sort
|
|
? [[filter.field, filter.sort]]
|
|
: [['createdAt', 'desc']],
|
|
transaction: options?.transaction,
|
|
logging: console.log
|
|
};
|
|
|
|
if (!options?.countOnly) {
|
|
queryOptions.limit = limit ? Number(limit) : undefined;
|
|
queryOptions.offset = offset ? Number(offset) : undefined;
|
|
}
|
|
|
|
try {
|
|
const { rows, count } = await db.purchase_orders.findAndCountAll(queryOptions);
|
|
|
|
return {
|
|
rows: options?.countOnly ? [] : rows,
|
|
count: count
|
|
};
|
|
} catch (error) {
|
|
console.error('Error executing query:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async findAllAutocomplete(query, limit, offset, globalAccess, organizationId,) {
|
|
let where = {};
|
|
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'purchase_orders',
|
|
'po_number',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.purchase_orders.findAll({
|
|
attributes: [ 'id', 'po_number' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['po_number', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.po_number,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|