2022-01-17 11:33:11 +00:00
|
|
|
const db = require('vn-print/core/database');
|
|
|
|
const closure = require('./closure');
|
|
|
|
|
|
|
|
module.exports = async function(request, response, next) {
|
|
|
|
try {
|
|
|
|
const reqArgs = request.query;
|
|
|
|
|
|
|
|
if (!reqArgs.agencyModeId)
|
|
|
|
throw new Error('The argument agencyModeId is required');
|
|
|
|
|
|
|
|
if (!reqArgs.warehouseId)
|
|
|
|
throw new Error('The argument warehouseId is required');
|
|
|
|
|
|
|
|
if (!reqArgs.to)
|
|
|
|
throw new Error('The argument to is required');
|
|
|
|
|
|
|
|
response.status(200).json({
|
|
|
|
message: 'Success'
|
|
|
|
});
|
|
|
|
|
|
|
|
const agencyIds = reqArgs.agencyModeId.split(',');
|
|
|
|
const tickets = await db.rawSql(`
|
|
|
|
SELECT
|
2022-01-19 08:12:10 +00:00
|
|
|
t.id,
|
|
|
|
t.clientFk,
|
|
|
|
c.name clientName,
|
|
|
|
c.email recipient,
|
|
|
|
c.salesPersonFk,
|
|
|
|
c.isToBeMailed,
|
|
|
|
c.hasToInvoice,
|
|
|
|
co.hasDailyInvoice,
|
|
|
|
eu.email salesPersonEmail
|
2022-01-17 11:33:11 +00:00
|
|
|
FROM expedition e
|
|
|
|
JOIN ticket t ON t.id = e.ticketFk
|
|
|
|
JOIN ticketState ts ON ts.ticketFk = t.id
|
|
|
|
JOIN alertLevel al ON al.id = ts.alertLevel
|
2022-01-19 08:12:10 +00:00
|
|
|
JOIN client c ON c.id = t.clientFk
|
|
|
|
JOIN province p ON p.id = c.provinceFk
|
|
|
|
JOIN country co ON co.id = p.countryFk
|
|
|
|
LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk
|
2022-01-17 11:33:11 +00:00
|
|
|
WHERE al.code = 'PACKED'
|
|
|
|
AND t.agencyModeFk IN(?)
|
|
|
|
AND t.warehouseFk = ?
|
|
|
|
AND DATE(t.shipped) BETWEEN DATE_ADD(?, INTERVAL -2 DAY)
|
|
|
|
AND util.dayEnd(?)
|
|
|
|
AND t.refFk IS NULL
|
|
|
|
GROUP BY e.ticketFk`, [
|
|
|
|
agencyIds,
|
|
|
|
reqArgs.warehouseId,
|
|
|
|
reqArgs.to,
|
|
|
|
reqArgs.to
|
|
|
|
]);
|
|
|
|
|
|
|
|
await closure.start(tickets, response.locals);
|
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
};
|