diff --git a/print/methods/closure.js b/print/methods/closure.js index 2a7ac19fb..e31369963 100644 --- a/print/methods/closure.js +++ b/print/methods/closure.js @@ -6,6 +6,10 @@ const config = require('../core/config'); module.exports = app => { app.get('/api/closure/all', async function(req, res, next) { try { + const reqArgs = req.args; + if (!reqArgs.to) + throw new Error('The argument to is required'); + res.status(200).json({ message: 'Task executed successfully' }); @@ -19,9 +23,12 @@ module.exports = app => { JOIN ticketState ts ON ts.ticketFk = t.id JOIN alertLevel al ON al.alertLevel = ts.alertLevel WHERE al.code = 'PACKED' - AND DATE(t.shipped) BETWEEN DATE_ADD(CURDATE(), INTERVAL -2 DAY) AND CURDATE() + AND DATE(t.shipped) BETWEEN DATE_ADD(:to, INTERVAL -2 DAY) + AND util.dayEnd(:to) AND t.refFk IS NULL - GROUP BY e.ticketFk`); + GROUP BY e.ticketFk`, { + to: reqArgs.to + }); const ticketIds = tickets.map(ticket => ticket.id); await closeAll(ticketIds, req.args); @@ -33,10 +40,13 @@ module.exports = app => { JOIN deliveryMethod dm ON dm.id = am.deliveryMethodFk JOIN zone z ON z.id = t.zoneFk SET t.routeFk = NULL - WHERE shipped BETWEEN CURDATE() AND util.dayEnd(CURDATE()) + WHERE DATE(t.shipped) BETWEEN DATE_ADD(:to, INTERVAL -2 DAY) + AND util.dayEnd(:to) AND al.code NOT IN('DELIVERED','PACKED') AND t.routeFk - AND z.name LIKE '%MADRID%'`); + AND z.name LIKE '%MADRID%'`, { + to: reqArgs.to + }); } catch (error) { next(error); } @@ -100,7 +110,8 @@ module.exports = app => { WHERE al.code = 'PACKED' AND t.agencyModeFk IN(:agencyModeId) AND t.warehouseFk = :warehouseId - AND DATE(t.shipped) BETWEEN DATE_ADD(:to, INTERVAL -2 DAY) AND :to + AND DATE(t.shipped) BETWEEN DATE_ADD(:to, INTERVAL -2 DAY) + AND util.dayEnd(:to) AND t.refFk IS NULL GROUP BY e.ticketFk`, { agencyModeId: agenciesId,