salix/db/changes/10200-DeEscalation/ticket_closeByAllWarehouses...

42 lines
1.4 KiB
SQL

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