6254-collection_getTickets #1769

Merged
alexm merged 8 commits from 6254-collection_getTickets into test 2023-10-02 07:52:01 +00:00
2 changed files with 62 additions and 96 deletions
Showing only changes of commit 9a7cb3ef7b - Show all commits

View File

@ -30,46 +30,37 @@ module.exports = Self => {
const $t = ctx.req.__; // $translate const $t = ctx.req.__; // $translate
const myOptions = {}; const myOptions = {};
let tx;
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
myOptions.userId = userId; myOptions.userId = userId;
const promises = []; const promises = [];
try {
const [tickets] = await Self.rawSql(`CALL vn.collection_getTickets(?)`, [id], myOptions); const [tickets] = await Self.rawSql(`CALL vn.collection_getTickets(?)`, [id], myOptions);
const sales = await Self.rawSql(` const sales = await Self.rawSql(`
SELECT SELECT s.ticketFk,
s.ticketFk,
sgd.saleGroupFk, sgd.saleGroupFk,
s.id saleFk, s.id saleFk,
s.itemFk, s.itemFk,
i.longName, i.longName,
i.size, i.size,
IFNULL(sub2.semaphore,st.semaphore) semaphore,
ic.color, ic.color,
ip.productor,
o.code origin, o.code origin,
s.concept, ish.packing,
b.packing, ish.grouping,
b.grouping,
s.isAdded, s.isAdded,
c.workerFk, s.originalQuantity,
i.packingShelve,
sm.id hasMistake,
s.originalQuantity,
s.quantity saleQuantity, s.quantity saleQuantity,
IF(p2.code IS NOT NULL, s.quantity, iss.quantity) reservedQuantity, 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, sh.code,
IFNULL(p2.code, p.code) parkingCode, IFNULL(p2.code, p.code) parkingCode,
IFNULL(p2.pickingOrder, p.pickingOrder) pickingOrder, IFNULL(p2.pickingOrder, p.pickingOrder) pickingOrder,
iss.id itemShelvingSaleFk iss.id itemShelvingSaleFk
iss.isChecked
FROM ticketCollection tc FROM ticketCollection tc
LEFT JOIN collection c ON c.id = tc.collectionFk LEFT JOIN collection c ON c.id = tc.collectionFk
JOIN ticket t ON t.id = tc.ticketFk JOIN ticket t ON t.id = tc.ticketFk
@ -77,103 +68,79 @@ module.exports = Self => {
LEFT JOIN saleGroupDetail sgd ON sgd.saleFk = s.id LEFT JOIN saleGroupDetail sgd ON sgd.saleFk = s.id
LEFT JOIN saleGroup sg ON sg.id = sgd.saleGroupFk LEFT JOIN saleGroup sg ON sg.id = sgd.saleGroupFk
LEFT JOIN parking p2 ON p2.id = sg.parkingFk LEFT JOIN parking p2 ON p2.id = sg.parkingFk
LEFT JOIN cache.last_buy lb ON lb.item_id = s.itemFk AND lb.warehouse_id = t.warehouseFk
LEFT JOIN buy b ON b.id = lb.buy_id
JOIN item i ON i.id = s.itemFk JOIN item i ON i.id = s.itemFk
LEFT JOIN itemShelvingSale iss ON iss.saleFk = s.id LEFT JOIN itemShelvingSale iss ON iss.saleFk = s.id
LEFT JOIN itemShelving ish ON ish.id = iss.itemShelvingFk LEFT JOIN itemShelving ish ON ish.id = iss.itemShelvingFk
LEFT JOIN shelving sh ON sh.code = ish.shelvingFk LEFT JOIN shelving sh ON sh.code = ish.shelvingFk
LEFT JOIN parking p ON p.id = sh.parkingFk LEFT JOIN parking p ON p.id = sh.parkingFk
LEFT JOIN (
SELECT sub.saleFk, sub.isChecked, sub.stateFk, sub.originalQuantity, sub.semaphore
FROM (
SELECT DISTINCT st.id,
st.saleFk, st.isChecked, st.stateFk, st.originalQuantity ,sta.semaphore
FROM ticketCollection tc
JOIN sale s ON s.ticketFk = tc.ticketFk
JOIN saleTracking st ON st.saleFk = s.id
JOIN state sta ON sta.id = st.stateFk
WHERE tc.collectionFk = ?
ORDER BY st.id DESC
LIMIT 10000000000000000000) sub
GROUP BY sub.saleFk, sub.stateFK
) sub2 ON sub2.saleFk = s.id AND sub2.isChecked = 1
LEFT JOIN state st ON st.id = sub2.stateFk
LEFT JOIN itemColor ic ON ic.itemFk = s.itemFk LEFT JOIN itemColor ic ON ic.itemFk = s.itemFk
LEFT JOIN itemProductor ip ON ip.itemFk = s.itemFk
LEFT JOIN origin o ON o.id = i.originFk LEFT JOIN origin o ON o.id = i.originFk
LEFT JOIN saleMistake sm ON sm.saleFk = s.id
WHERE tc.collectionFk = ? WHERE tc.collectionFk = ?
GROUP BY s.id, p.code, p2.code GROUP BY ish.id, p.code, p2.code
ORDER BY pickingOrder`, [id, id], myOptions); ORDER BY pickingOrder;`, [id], myOptions);
if (print) if (print)
await Self.rawSql(`CALL vn.collection_printSticker(?, ?)`, [id, null], myOptions); await Self.rawSql(`CALL vn.collection_printSticker(?, ?)`, [id, null], myOptions);
const collection = {collectionFk: id, tickets: []}; const collection = {collectionFk: id, tickets: []};
if (tickets && tickets.length) { if (tickets && tickets.length) {
for (const ticket of tickets) { for (const ticket of tickets) {
const ticketId = ticket.ticketFk; const ticketId = ticket.ticketFk;
// SEND ROCKET // SEND ROCKET
if (ticket.observaciones != '') { if (ticket.observaciones != '') {
for (observation of ticket.observaciones.split(' ')) { for (observation of ticket.observaciones.split(' ')) {
if (['#', '@'].includes(observation.charAt(0))) { if (['#', '@'].includes(observation.charAt(0))) {
promises.push(Self.app.models.Chat.send(ctx, observation, promises.push(Self.app.models.Chat.send(ctx, observation,
$t('The ticket is in preparation', { $t('The ticket is in preparation', {
ticketId: ticketId, ticketId: ticketId,
ticketUrl: `${origin}/#!/ticket/${ticketId}/summary`, ticketUrl: `${origin}/#!/ticket/${ticketId}/summary`,
salesPersonId: ticket.salesPersonFk salesPersonId: ticket.salesPersonFk
}))); })));
}
} }
} }
}
// SET COLLECTION // SET COLLECTION
if (sales && sales.length) { if (sales && sales.length) {
// GET BARCODES // GET BARCODES
const barcodes = await Self.rawSql(` const barcodes = await Self.rawSql(`
SELECT s.id saleFk, b.code, c.id SELECT s.id saleFk, b.code, c.id
FROM vn.sale s FROM vn.sale s
LEFT JOIN vn.itemBarcode b ON b.itemFk = s.itemFk LEFT JOIN vn.itemBarcode b ON b.itemFk = s.itemFk
LEFT JOIN vn.buy c ON c.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.entry e ON e.id = c.entryFk
LEFT JOIN vn.travel tr ON tr.id = e.travelFk LEFT JOIN vn.travel tr ON tr.id = e.travelFk
WHERE s.ticketFk = ? AND tr.landed >= DATE_SUB(util.VN_CURDATE(), INTERVAL 1 YEAR)`, WHERE s.ticketFk = ?
[ticketId], myOptions); AND tr.landed >= util.VN_CURDATE() - INTERVAL 1 YEAR`,
[ticketId], myOptions);
// BINDINGS // BINDINGS
ticket.sales = []; ticket.sales = [];
for (const sale of sales) { for (const sale of sales) {
if (sale.ticketFk === ticketId) { if (sale.ticketFk === ticketId) {
sale.Barcodes = []; sale.Barcodes = [];
if (barcodes && barcodes.length) { if (barcodes && barcodes.length) {
for (const barcode of barcodes) { for (const barcode of barcodes) {
if (barcode.saleFk === sale.saleFk) { if (barcode.saleFk === sale.saleFk) {
for (let prop in barcode) { for (const prop in barcode) {
if (['id', 'code'].includes(prop) && barcode[prop]) if (['id', 'code'].includes(prop) && barcode[prop])
sale.Barcodes.push(barcode[prop].toString(), '0' + barcode[prop]); sale.Barcodes.push(barcode[prop].toString(), '0' + barcode[prop]);
}
} }
} }
} }
ticket.sales.push(sale);
} }
ticket.sales.push(sale);
} }
} }
collection.tickets.push(ticket);
} }
collection.tickets.push(ticket);
} }
if (tx) await tx.commit();
await Promise.all(promises);
return collection;
} catch (e) {
if (tx) await tx.rollback();
throw e;
} }
await Promise.all(promises);
return collection;
}; };
}; };

View File

@ -318,6 +318,5 @@
"Street should be uppercase": "La dirección fiscal debe ir en mayúscula", "Street should be uppercase": "La dirección fiscal debe ir en mayúscula",
"The response is not a PDF": "La respuesta no es un PDF", "The response is not a PDF": "La respuesta no es un PDF",
"Ticket without Route": "Ticket sin ruta", "Ticket without Route": "Ticket sin ruta",
"The ticket is in preparation": "El ticket [{{ticketId}}]({{{ticketUrl}}}) del comercial {{salesPersonId}} està en preparación", "The ticket is in preparation": "El ticket [{{ticketId}}]({{{ticketUrl}}}) del comercial {{salesPersonId}} està en preparación"
"Invalid collection or ticket": "Invalid collection or ticket"
} }