salix/back/methods/collection/getTickets.js

180 lines
7.6 KiB
JavaScript
Raw Permalink Normal View History

module.exports = Self => {
Self.remoteMethodCtx('getTickets', {
description: 'Make a new collection of tickets',
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'number',
description: 'The collection id',
required: true,
http: {source: 'path'}
}, {
arg: 'print',
type: 'boolean',
description: 'True if you want to print'
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/:id/getTickets`,
verb: 'POST'
}
});
Self.getTickets = async(ctx, id, print, options) => {
const userId = ctx.req.accessToken.userId;
const url = await Self.app.models.Url.getUrl();
2023-09-29 11:47:29 +00:00
const $t = ctx.req.__;
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);
2024-04-11 10:16:00 +00:00
2023-09-29 10:30:05 +00:00
const sales = await Self.rawSql(`
SELECT s.ticketFk,
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,
s.isAdded,
s.originalQuantity,
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,
sh.code,
2024-11-12 10:59:51 +00:00
p2.code parkingCodePrevia,
p2.pickingOrder pickingOrderPrevia,
p.code parkingCode,
p.pickingOrder pickingOrder,
2023-09-29 11:44:41 +00:00
iss.id itemShelvingSaleFk,
2024-10-15 15:01:09 +00:00
iss.isPicked,
iss.itemShelvingFk
FROM ticketCollection tc
LEFT JOIN collection c ON c.id = tc.collectionFk
2024-07-12 11:14:00 +00:00
JOIN sale s ON s.ticketFk = tc.ticketFk
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
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
WHERE tc.collectionFk = ?
2023-11-22 06:04:33 +00:00
GROUP BY s.id, ish.id, p.code, p2.code
2024-04-11 10:16:00 +00:00
UNION ALL
SELECT s.ticketFk,
sgd.saleGroupFk,
s.id saleFk,
s.itemFk,
i.longName,
i.size,
ic.color,
o.code origin,
ish.packing,
ish.grouping,
s.isAdded,
s.originalQuantity,
s.quantity,
iss.quantity,
SUM(iss.quantity) OVER (PARTITION BY s.id ORDER BY ish.id),
ROW_NUMBER () OVER (PARTITION BY s.id ORDER BY p.pickingOrder),
COUNT(*) OVER (PARTITION BY s.id ORDER BY s.id) ,
sh.code,
2024-07-30 15:28:55 +00:00
p2.code,
p2.pickingOrder,
p.code,
p.pickingOrder,
2024-04-11 10:16:00 +00:00
iss.id itemShelvingSaleFk,
2024-10-15 15:01:09 +00:00
iss.isPicked,
iss.itemShelvingFk
2024-04-11 10:16:00 +00:00
FROM sectorCollection sc
JOIN sectorCollectionSaleGroup ss ON ss.sectorCollectionFk = sc.id
JOIN saleGroup sg ON sg.id = ss.saleGroupFk
2024-07-12 11:14:00 +00:00
LEFT JOIN saleGroupDetail sgd ON sgd.saleGroupFk = sg.id
JOIN sale s ON s.id = sgd.saleFk
2024-04-11 10:16:00 +00:00
LEFT JOIN parking p2 ON p2.id = sg.parkingFk
JOIN item i ON i.id = s.itemFk
2024-04-11 11:45:43 +00:00
JOIN itemShelvingSale iss ON iss.saleFk = s.id
2024-04-11 10:16:00 +00:00
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
LEFT JOIN origin o ON o.id = i.originFk
2024-04-11 11:45:43 +00:00
WHERE sc.id = ?
AND sgd.saleGroupFk
2024-04-11 10:16:00 +00:00
GROUP BY s.id, ish.id, p.code, p2.code`, [id, id], myOptions);
2023-09-29 10:30:05 +00:00
if (print)
await Self.rawSql(`CALL vn.collection_printSticker(?, ?)`, [id, null], myOptions);
2023-09-29 10:30:05 +00:00
const collection = {collectionFk: id, tickets: []};
2024-04-11 10:16:00 +00:00
2023-09-29 10:30:05 +00:00
if (tickets && tickets.length) {
for (const ticket of tickets) {
const ticketId = ticket.ticketFk;
2024-04-11 10:16:00 +00:00
if (ticket.observation) {
for (observation of ticket.observation?.split(' ')) {
2023-09-29 10:30:05 +00:00
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-29 10:30:05 +00:00
}
if (sales && sales.length) {
const barcodes = await Self.rawSql(`
SELECT s.id saleFk, b.code, c.id
2024-04-11 10:16:00 +00:00
FROM sale s
LEFT JOIN itemBarcode b ON b.itemFk = s.itemFk
LEFT JOIN buy c ON c.itemFk = s.itemFk
LEFT JOIN entry e ON e.id = c.entryFk
LEFT JOIN travel tr ON tr.id = e.travelFk
WHERE s.ticketFk = ?
2023-09-29 10:30:05 +00:00
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-29 10:30:05 +00:00
ticket.sales.push(sale);
}
}
}
2023-09-29 10:30:05 +00:00
collection.tickets.push(ticket);
}
}
2023-09-29 10:30:05 +00:00
await Promise.all(promises);
return collection;
};
};