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-20 10:53:54 +00:00
|
|
|
const url = await 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);
|
2024-04-11 10:16:00 +00:00
|
|
|
|
2023-09-29 10:30:05 +00:00
|
|
|
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-11-23 10:49:00 +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,
|
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,
|
2024-11-25 15:16:47 +00:00
|
|
|
iss.itemShelvingFk,
|
|
|
|
st.code stateCode
|
2023-09-27 13:08:36 +00:00
|
|
|
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
|
2023-09-27 13:08:36 +00:00
|
|
|
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
|
2024-02-16 06:03:05 +00:00
|
|
|
JOIN itemShelvingSale iss ON iss.saleFk = s.id
|
2023-09-27 13:08:36 +00:00
|
|
|
LEFT JOIN itemShelving ish ON ish.id = iss.itemShelvingFk
|
2024-11-13 10:15:31 +00:00
|
|
|
LEFT JOIN shelving sh ON sh.id = ish.shelvingFk
|
2023-09-27 13:08:36 +00:00
|
|
|
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
|
2024-11-25 15:16:47 +00:00
|
|
|
LEFT JOIN state st ON st.id = sg.stateFk
|
2023-11-23 10:49:00 +00:00
|
|
|
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,
|
2024-11-25 15:16:47 +00:00
|
|
|
iss.itemShelvingFk,
|
|
|
|
st.code stateCode
|
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
|
2024-11-13 10:15:31 +00:00
|
|
|
LEFT JOIN shelving sh ON sh.id = ish.shelvingFk
|
2024-04-11 10:16:00 +00:00
|
|
|
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-11-25 15:16:47 +00:00
|
|
|
LEFT JOIN state st ON st.id = sg.stateFk
|
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-27 13:08:36 +00:00
|
|
|
|
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-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
|
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
|
2023-11-23 10:49:00 +00:00
|
|
|
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-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
|
|
|
};
|
|
|
|
};
|