2023-09-27 13:08:36 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('getTickets', {
|
|
|
|
description: 'Make a new collection of tickets',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
2023-09-28 06:30:50 +00:00
|
|
|
arg: 'id',
|
2023-09-27 13:08:36 +00:00
|
|
|
type: 'number',
|
2023-09-28 06:30:50 +00:00
|
|
|
description: 'The collection id',
|
|
|
|
required: true,
|
|
|
|
http: {source: 'path'}
|
2023-09-27 13:08:36 +00:00
|
|
|
}, {
|
|
|
|
arg: 'print',
|
|
|
|
type: 'boolean',
|
|
|
|
description: 'True if you want to print'
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: ['object'],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
2023-09-28 06:30:50 +00:00
|
|
|
path: `/:id/getTickets`,
|
2023-09-27 13:08:36 +00:00
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-09-28 06:30:50 +00:00
|
|
|
Self.getTickets = async(ctx, id, print, options) => {
|
2023-09-27 13:08:36 +00:00
|
|
|
const userId = ctx.req.accessToken.userId;
|
2023-10-19 06:37:28 +00:00
|
|
|
const url = Self.app.models.Url.getUrl();
|
2023-09-29 11:47:29 +00:00
|
|
|
const $t = ctx.req.__;
|
2023-09-27 13:08:36 +00:00
|
|
|
const myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
myOptions.userId = userId;
|
|
|
|
|
|
|
|
const promises = [];
|
2023-09-29 10:30:05 +00:00
|
|
|
const [tickets] = await Self.rawSql(`CALL vn.collection_getTickets(?)`, [id], myOptions);
|
|
|
|
const sales = await Self.rawSql(`
|
|
|
|
SELECT s.ticketFk,
|
2023-09-27 13:08:36 +00:00
|
|
|
sgd.saleGroupFk,
|
|
|
|
s.id saleFk,
|
|
|
|
s.itemFk,
|
|
|
|
i.longName,
|
|
|
|
i.size,
|
|
|
|
ic.color,
|
2023-10-16 16:24:58 +00:00
|
|
|
o.code origin,
|
2023-09-29 10:30:05 +00:00
|
|
|
ish.packing,
|
|
|
|
ish.grouping,
|
2023-09-27 13:08:36 +00:00
|
|
|
s.isAdded,
|
2023-10-16 16:24:58 +00:00
|
|
|
s.originalQuantity,
|
2023-09-27 13:08:36 +00:00
|
|
|
s.quantity saleQuantity,
|
2023-09-29 10:30:05 +00:00
|
|
|
iss.quantity reservedQuantity,
|
|
|
|
SUM(iss.quantity) OVER (PARTITION BY s.id ORDER BY ish.id) accumulatedQuantity,
|
|
|
|
ROW_NUMBER () OVER (PARTITION BY s.id ORDER BY pickingOrder) currentItemShelving,
|
|
|
|
COUNT(*) OVER (PARTITION BY s.id ORDER BY s.id) totalItemShelving,
|
2023-09-27 13:08:36 +00:00
|
|
|
sh.code,
|
|
|
|
IFNULL(p2.code, p.code) parkingCode,
|
|
|
|
IFNULL(p2.pickingOrder, p.pickingOrder) pickingOrder,
|
2023-09-29 11:44:41 +00:00
|
|
|
iss.id itemShelvingSaleFk,
|
|
|
|
iss.isPicked
|
2023-09-27 13:08:36 +00:00
|
|
|
FROM ticketCollection tc
|
|
|
|
LEFT JOIN collection c ON c.id = tc.collectionFk
|
|
|
|
JOIN ticket t ON t.id = tc.ticketFk
|
|
|
|
JOIN sale s ON s.ticketFk = t.id
|
|
|
|
LEFT JOIN saleGroupDetail sgd ON sgd.saleFk = s.id
|
|
|
|
LEFT JOIN saleGroup sg ON sg.id = sgd.saleGroupFk
|
|
|
|
LEFT JOIN parking p2 ON p2.id = sg.parkingFk
|
|
|
|
JOIN item i ON i.id = s.itemFk
|
|
|
|
LEFT JOIN itemShelvingSale iss ON iss.saleFk = s.id
|
|
|
|
LEFT JOIN itemShelving ish ON ish.id = iss.itemShelvingFk
|
|
|
|
LEFT JOIN shelving sh ON sh.code = ish.shelvingFk
|
|
|
|
LEFT JOIN parking p ON p.id = sh.parkingFk
|
|
|
|
LEFT JOIN itemColor ic ON ic.itemFk = s.itemFk
|
2023-10-16 16:24:58 +00:00
|
|
|
LEFT JOIN origin o ON o.id = i.originFk
|
2023-09-27 13:08:36 +00:00
|
|
|
WHERE tc.collectionFk = ?
|
2023-09-29 10:30:05 +00:00
|
|
|
GROUP BY ish.id, p.code, p2.code
|
|
|
|
ORDER BY pickingOrder;`, [id], myOptions);
|
2023-09-27 13:08:36 +00:00
|
|
|
|
2023-09-29 10:30:05 +00:00
|
|
|
if (print)
|
|
|
|
await Self.rawSql(`CALL vn.collection_printSticker(?, ?)`, [id, null], myOptions);
|
2023-09-27 13:08:36 +00:00
|
|
|
|
2023-09-29 10:30:05 +00:00
|
|
|
const collection = {collectionFk: id, tickets: []};
|
|
|
|
if (tickets && tickets.length) {
|
|
|
|
for (const ticket of tickets) {
|
|
|
|
const ticketId = ticket.ticketFk;
|
|
|
|
if (ticket.observaciones != '') {
|
|
|
|
for (observation of ticket.observaciones.split(' ')) {
|
|
|
|
if (['#', '@'].includes(observation.charAt(0))) {
|
|
|
|
promises.push(Self.app.models.Chat.send(ctx, observation,
|
|
|
|
$t('The ticket is in preparation', {
|
|
|
|
ticketId: ticketId,
|
2023-10-16 16:24:58 +00:00
|
|
|
ticketUrl: `${url}ticket/${ticketId}/summary`,
|
2023-09-29 10:30:05 +00:00
|
|
|
salesPersonId: ticket.salesPersonFk
|
|
|
|
})));
|
2023-09-27 13:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-29 10:30:05 +00:00
|
|
|
}
|
|
|
|
if (sales && sales.length) {
|
|
|
|
const barcodes = await Self.rawSql(`
|
|
|
|
SELECT s.id saleFk, b.code, c.id
|
|
|
|
FROM vn.sale s
|
|
|
|
LEFT JOIN vn.itemBarcode b ON b.itemFk = s.itemFk
|
|
|
|
LEFT JOIN vn.buy c ON c.itemFk = s.itemFk
|
|
|
|
LEFT JOIN vn.entry e ON e.id = c.entryFk
|
|
|
|
LEFT JOIN vn.travel tr ON tr.id = e.travelFk
|
|
|
|
WHERE s.ticketFk = ?
|
|
|
|
AND tr.landed >= util.VN_CURDATE() - INTERVAL 1 YEAR`,
|
|
|
|
[ticketId], myOptions);
|
|
|
|
ticket.sales = [];
|
|
|
|
for (const sale of sales) {
|
|
|
|
if (sale.ticketFk === ticketId) {
|
|
|
|
sale.Barcodes = [];
|
|
|
|
if (barcodes && barcodes.length) {
|
|
|
|
for (const barcode of barcodes) {
|
|
|
|
if (barcode.saleFk === sale.saleFk) {
|
|
|
|
for (const prop in barcode) {
|
|
|
|
if (['id', 'code'].includes(prop) && barcode[prop])
|
|
|
|
sale.Barcodes.push(barcode[prop].toString(), '0' + barcode[prop]);
|
2023-09-27 13:08:36 +00:00
|
|
|
}
|
2023-09-28 06:30:50 +00:00
|
|
|
}
|
2023-09-27 13:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-29 10:30:05 +00:00
|
|
|
ticket.sales.push(sale);
|
2023-09-28 06:30:50 +00:00
|
|
|
}
|
2023-09-27 13:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-29 10:30:05 +00:00
|
|
|
collection.tickets.push(ticket);
|
2023-09-27 13:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-29 10:30:05 +00:00
|
|
|
await Promise.all(promises);
|
|
|
|
return collection;
|
2023-09-27 13:08:36 +00:00
|
|
|
};
|
|
|
|
};
|