614 lines
15 KiB
JavaScript
614 lines
15 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 Pick_list_linesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const pick_list_lines = await db.pick_list_lines.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
requested_qty: data.requested_qty
|
|
||
|
|
null
|
|
,
|
|
|
|
picked_qty: data.picked_qty
|
|
||
|
|
null
|
|
,
|
|
|
|
lot_number: data.lot_number
|
|
||
|
|
null
|
|
,
|
|
|
|
serial_number: data.serial_number
|
|
||
|
|
null
|
|
,
|
|
|
|
line_status: data.line_status
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await pick_list_lines.setPick_list( data.pick_list || null, {
|
|
transaction,
|
|
});
|
|
|
|
await pick_list_lines.setSales_order_line( data.sales_order_line || null, {
|
|
transaction,
|
|
});
|
|
|
|
await pick_list_lines.setProduct( data.product || null, {
|
|
transaction,
|
|
});
|
|
|
|
await pick_list_lines.setFrom_location( data.from_location || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return pick_list_lines;
|
|
}
|
|
|
|
|
|
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 pick_list_linesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
requested_qty: item.requested_qty
|
|
||
|
|
null
|
|
,
|
|
|
|
picked_qty: item.picked_qty
|
|
||
|
|
null
|
|
,
|
|
|
|
lot_number: item.lot_number
|
|
||
|
|
null
|
|
,
|
|
|
|
serial_number: item.serial_number
|
|
||
|
|
null
|
|
,
|
|
|
|
line_status: item.line_status
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const pick_list_lines = await db.pick_list_lines.bulkCreate(pick_list_linesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return pick_list_lines;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const pick_list_lines = await db.pick_list_lines.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.requested_qty !== undefined) updatePayload.requested_qty = data.requested_qty;
|
|
|
|
|
|
if (data.picked_qty !== undefined) updatePayload.picked_qty = data.picked_qty;
|
|
|
|
|
|
if (data.lot_number !== undefined) updatePayload.lot_number = data.lot_number;
|
|
|
|
|
|
if (data.serial_number !== undefined) updatePayload.serial_number = data.serial_number;
|
|
|
|
|
|
if (data.line_status !== undefined) updatePayload.line_status = data.line_status;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await pick_list_lines.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.pick_list !== undefined) {
|
|
await pick_list_lines.setPick_list(
|
|
|
|
data.pick_list,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.sales_order_line !== undefined) {
|
|
await pick_list_lines.setSales_order_line(
|
|
|
|
data.sales_order_line,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.product !== undefined) {
|
|
await pick_list_lines.setProduct(
|
|
|
|
data.product,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.from_location !== undefined) {
|
|
await pick_list_lines.setFrom_location(
|
|
|
|
data.from_location,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return pick_list_lines;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const pick_list_lines = await db.pick_list_lines.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of pick_list_lines) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of pick_list_lines) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return pick_list_lines;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const pick_list_lines = await db.pick_list_lines.findByPk(id, options);
|
|
|
|
await pick_list_lines.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await pick_list_lines.destroy({
|
|
transaction
|
|
});
|
|
|
|
return pick_list_lines;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const pick_list_lines = await db.pick_list_lines.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!pick_list_lines) {
|
|
return pick_list_lines;
|
|
}
|
|
|
|
const output = pick_list_lines.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.pick_list = await pick_list_lines.getPick_list({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.sales_order_line = await pick_list_lines.getSales_order_line({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.product = await pick_list_lines.getProduct({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.from_location = await pick_list_lines.getFrom_location({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.pick_lists,
|
|
as: 'pick_list',
|
|
|
|
where: filter.pick_list ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.pick_list.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
pick_list_number: {
|
|
[Op.or]: filter.pick_list.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.sales_order_lines,
|
|
as: 'sales_order_line',
|
|
|
|
where: filter.sales_order_line ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.sales_order_line.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
line_notes: {
|
|
[Op.or]: filter.sales_order_line.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.products,
|
|
as: 'product',
|
|
|
|
where: filter.product ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.product.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
sku: {
|
|
[Op.or]: filter.product.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.locations,
|
|
as: 'from_location',
|
|
|
|
where: filter.from_location ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.from_location.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
code: {
|
|
[Op.or]: filter.from_location.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.lot_number) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'pick_list_lines',
|
|
'lot_number',
|
|
filter.lot_number,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.serial_number) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'pick_list_lines',
|
|
'serial_number',
|
|
filter.serial_number,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.requested_qtyRange) {
|
|
const [start, end] = filter.requested_qtyRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
requested_qty: {
|
|
...where.requested_qty,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
requested_qty: {
|
|
...where.requested_qty,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.picked_qtyRange) {
|
|
const [start, end] = filter.picked_qtyRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
picked_qty: {
|
|
...where.picked_qty,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
picked_qty: {
|
|
...where.picked_qty,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.line_status) {
|
|
where = {
|
|
...where,
|
|
line_status: filter.line_status,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
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.pick_list_lines.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, ) {
|
|
let where = {};
|
|
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'pick_list_lines',
|
|
'lot_number',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.pick_list_lines.findAll({
|
|
attributes: [ 'id', 'lot_number' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['lot_number', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.lot_number,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|