2285 - Added closure paths
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2020-06-16 08:17:01 +02:00
parent 1b71c1b3b1
commit 5d9ed49451
6 changed files with 304 additions and 19 deletions

View File

@ -0,0 +1,32 @@
CREATE DEFINER=`root`@`%` PROCEDURE `ticket_closeByAgencyList`(vWarehouseFk INT, vDateTo DATE)
BEGIN
/**
* Inserta los tickets de todos los almacenes en la tabla temporal
* para ser cerrados.
*
* @param vWarehouseFk Id del almacén
* @param vDate Fecha del cierre
*/
DROP TEMPORARY TABLE IF EXISTS tmp.ticketClosure;
CREATE TEMPORARY TABLE tmp.ticketClosure ENGINE = MEMORY (
SELECT
t.id AS ticketFk
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN tmp.ticketClosureAgencyList al ON al.agencyModeFk = t.agencyModeFk
JOIN ticketState ts ON ts.ticketFk = t.id
JOIN alertLevel al ON al.alertLevel = ts.alertLevel
WHERE al.code = 'PACKED'
AND t.warehouseFk = vWarehouseFk
AND DATE(t.shipped) BETWEEN DATE_ADD(vDateTo, INTERVAL -2 DAY) AND vDateTo
AND t.refFk IS NULL
GROUP BY e.ticketFk);
CALL ticket_close();
DROP TEMPORARY TABLE tmp.ticketClosureAgencyList;
DROP TEMPORARY TABLE tmp.ticketClosure;
END

View File

@ -0,0 +1,42 @@
CREATE DEFINER=`root`@`%` PROCEDURE `ticket_closeByAllWarehouses`(vDateTo DATE)
BEGIN
/**
* Inserta los tickets de todos los almacenes en la tabla temporal
* para ser cerrados.
*
* @param vDate Fecha del cierre
*/
DECLARE vDateToEndDay DATETIME DEFAULT util.dayEnd(vDateTo);
DROP TEMPORARY TABLE IF EXISTS tmp.ticketClosure;
CREATE TEMPORARY TABLE tmp.ticketClosure ENGINE = MEMORY (
SELECT
t.id AS ticketFk
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN warehouse w ON w.id = t.warehouseFk AND hasComission
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(vDateTo, INTERVAL -2 DAY) AND vDateTo
AND t.refFk IS NULL
GROUP BY e.ticketFk);
CALL ticket_close();
-- cau 15033
UPDATE ticket t
JOIN ticketState ts ON t.id = ts.ticketFk
JOIN alertLevel al ON al.alertLevel = ts.alertLevel
JOIN agencyMode am ON am.id = t.agencyModeFk
JOIN deliveryMethod dm ON dm.id = am.deliveryMethodFk
JOIN zone z ON z.id = t.zoneFk
SET t.routeFk = NULL
WHERE shipped BETWEEN vDateTo AND vDateToEndDay
AND al.code NOT IN('DELIVERED','PACKED')
AND t.routeFk
AND z.`name` LIKE '%MADRID%';
DROP TEMPORARY TABLE tmp.ticketClosure;
END

View File

@ -0,0 +1,29 @@
CREATE DEFINER=`root`@`%` PROCEDURE `ticket_closeByRoute`( vRouteFk INT)
BEGIN
/**
* Inserta los tickets de la ruta en la tabla temporal
* para ser cerrados.
*
* @param vWarehouseFk Almacén a cerrar
* @param vRouteFk Ruta a cerrar
* @param vDate Fecha del cierre
*/
DROP TEMPORARY TABLE IF EXISTS tmp.ticketClosure;
CREATE TEMPORARY TABLE tmp.ticketClosure ENGINE = MEMORY (
SELECT
t.id AS ticketFk
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN ticketState ts ON ts.ticketFk = t.id
JOIN alertLevel al ON al.alertLevel = ts.alertLevel
WHERE al.code = 'PACKED'
AND t.routeFk = vRouteFk
AND t.refFk IS NULL
GROUP BY e.ticketFk);
CALL ticket_close();
DROP TEMPORARY TABLE tmp.ticketClosure;
END

View File

@ -0,0 +1,28 @@
CREATE DEFINER=`root`@`%` PROCEDURE `ticket_closeByTicket`(vTicketFk INT)
BEGIN
/**
* Inserta el ticket en la tabla temporal
* para ser cerrado.
*
* @param vTicketFk Id del ticket
*/
DROP TEMPORARY TABLE IF EXISTS tmp.ticketClosure;
CREATE TEMPORARY TABLE tmp.ticketClosure ENGINE = MEMORY (
SELECT
t.id AS ticketFk
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN ticketState ts ON ts.ticketFk = t.id
JOIN alertLevel al ON al.alertLevel = ts.alertLevel
WHERE al.code = 'PACKED'
AND t.id = vTicketFk
AND t.refFk IS NULL
GROUP BY e.ticketFk);
CALL ticket_close();
DROP TEMPORARY TABLE tmp.ticketClosure;
END

View File

@ -0,0 +1,29 @@
CREATE DEFINER=`root`@`%` PROCEDURE `ticket_closeByWarehouse`(vWarehouseFk INT, vDateTo DATE)
BEGIN
/**
* Inserta los tickets del almacen en la tabla temporal
* para ser cerrados.
*
* @param vWarehouseFk Almacén a cerrar
* @param vDate Fecha del cierre
*/
DROP TEMPORARY TABLE IF EXISTS tmp.ticketClosure;
CREATE TEMPORARY TABLE ticketClosure ENGINE = MEMORY(
SELECT
t.id AS ticketFk
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN ticketState ts ON ts.ticketFk = t.id
JOIN alertLevel al ON al.alertLevel = ts.alertLevel
WHERE al.code = 'PACKED'
AND t.warehouseFk = vWarehouseFk
AND DATE(t.shipped) BETWEEN DATE_ADD(vDateTo, INTERVAL -2 DAY) AND vDateTo
AND t.refFk IS NULL
GROUP BY e.ticketFk);
CALL ticket_close();
DROP TEMPORARY TABLE tmp.ticketClosure;
END

View File

@ -4,34 +4,157 @@ const smtp = require('../core/smtp');
const config = require('../core/config');
module.exports = app => {
app.get('/api/closure/by-ticket', async function(req, res) {
});
app.get('/api/closure/all', async function(req, res) {
app.get('/api/closure/all', async function(req, res, next) {
try {
res.status(200).json({
message: 'Task executed successfully'
});
await db.rawSql(`DROP TEMPORARY TABLE IF EXISTS tmp.ticket_close`);
await db.rawSql(`
CREATE TEMPORARY TABLE tmp.ticket_close ENGINE = MEMORY (
SELECT
t.id AS ticketFk
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN warehouse wh ON wh.id = t.warehouseFk AND wh.hasComission
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 t.refFk IS NULL
GROUP BY e.ticketFk)`);
await closeAll(req.args);
} catch (error) {
next(error);
}
});
app.get('/api/closure/by-ticket', async function(req, res, next) {
try {
const reqArgs = req.args;
if (!reqArgs.ticketId)
throw new Error('The argument ticketId is required');
res.status(200).json({
message: 'Task executed successfully'
});
await db.rawSql(`DROP TEMPORARY TABLE IF EXISTS tmp.ticket_close`);
await db.rawSql(`
CREATE TEMPORARY TABLE tmp.ticket_close ENGINE = MEMORY (
SELECT
t.id AS ticketFk
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN ticketState ts ON ts.ticketFk = t.id
JOIN alertLevel al ON al.alertLevel = ts.alertLevel
WHERE al.code = 'PACKED'
AND t.id = :ticketId
AND t.refFk IS NULL
GROUP BY e.ticketFk)`, {
ticketId: reqArgs.ticketId
});
await closeAll(reqArgs);
} catch (error) {
next(error);
}
});
app.get('/api/closure/by-agency', async function(req, res) {
try {
const reqArgs = req.args;
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');
res.status(200).json({
message: 'Task executed successfully'
});
await db.rawSql(`DROP TEMPORARY TABLE IF EXISTS tmp.ticket_close`);
await db.rawSql(`
CREATE TEMPORARY TABLE tmp.ticket_close ENGINE = MEMORY (
SELECT
t.id AS ticketFk
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN ticketState ts ON ts.ticketFk = t.id
JOIN alertLevel al ON al.alertLevel = ts.alertLevel
WHERE al.code = 'PACKED'
AND t.agencyModeFk = :agencyModeId
AND t.warehouseFk = :warehouseId
AND DATE(t.shipped) BETWEEN DATE_ADD(:to, INTERVAL -2 DAY) AND :to
AND t.refFk IS NULL
GROUP BY e.ticketFk)`, {
agencyModeId: reqArgs.agencyModeId,
warehouseId: reqArgs.warehouseId,
to: reqArgs.to
});
await closeAll(reqArgs);
} catch (error) {
next(error);
}
});
app.get('/api/closure/by-route', async function(req, res) {
try {
const reqArgs = req.args;
if (!reqArgs.routeId)
throw new Error('The argument routeId is required');
res.status(200).json({
message: 'Task executed successfully'
});
await db.rawSql(`DROP TEMPORARY TABLE IF EXISTS tmp.ticket_close`);
await db.rawSql(`
CREATE TEMPORARY TABLE tmp.ticket_close ENGINE = MEMORY (
SELECT
t.id AS ticketFk
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
JOIN ticketState ts ON ts.ticketFk = t.id
JOIN alertLevel al ON al.alertLevel = ts.alertLevel
WHERE al.code = 'PACKED'
AND t.routeFk = :routeId
AND t.refFk IS NULL
GROUP BY e.ticketFk)`, {
routeId: reqArgs.routeId
});
await closeAll(reqArgs);
} catch (error) {
next(error);
}
});
async function closeAll(reqArgs) {
const failedtickets = [];
const tickets = await db.rawSql(`
SELECT
t.id,
t.clientFk,
c.email recipient,
c.isToBeMailed,
c.salesPersonFk,
c.isToBeMailed,
c.hasToInvoice,
co.hasDailyInvoice,
eu.email salesPersonEmail
FROM expedition e
JOIN ticket t ON t.id = e.ticketFk
FROM tmp.ticket_close tt
JOIN ticket t ON t.id = tt.ticketFk
JOIN client c ON c.id = t.clientFk
JOIN warehouse wh ON wh.id = t.warehouseFk AND wh.hasComission
JOIN ticketState ts ON ts.ticketFk = t.id
JOIN alertLevel al ON al.alertLevel = ts.alertLevel
LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk
WHERE al.code = 'PACKED'
AND DATE(t.shipped) BETWEEN DATE_ADD(CURDATE(), INTERVAL -2 DAY) AND CURDATE()
AND t.refFk IS NULL
GROUP BY e.ticketFk`);
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`);
for (const ticket of tickets) {
try {
@ -39,7 +162,8 @@ module.exports = app => {
ticketId: ticket.id
});
if (!ticket.salesPersonFk || !ticket.isToBeMailed) continue;
const hasToInvoice = ticket.hasToInvoice && ticket.hasDailyInvoice;
if (!ticket.salesPersonFk || !ticket.isToBeMailed || hasToInvoice) continue;
if (!ticket.recipient) {
const body = `No se ha podido enviar el albarán <strong>${ticket.id}</strong>
@ -54,7 +178,6 @@ module.exports = app => {
continue;
}
const reqArgs = req.args;
const args = Object.assign({
ticketId: ticket.id,
recipientId: ticket.clientFk,
@ -88,5 +211,7 @@ module.exports = app => {
html: body
});
}
});
await db.rawSql(`DROP TEMPORARY TABLE tmp.ticket_close`);
}
};