salix/db/routines/vn/procedures/ticket_split.sql

77 lines
2.0 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_split`(
vTicketFk INT,
vTicketFutureFk INT,
vDated DATE
)
proc:BEGIN
/**
* Mueve las lineas con problemas a otro ticket existente o a uno nuevo.
*
* @param vTicketFk Identificador del ticket a splittar
* @param vTicketFutureFk Identificador del ticket al que se quieren mover las lineas
* @param vDate Fecha a la que se va a mover
* @table tmp.salesToSplit(saleFk, ticketFk, problem)
*/
DECLARE vHasFullProblem BOOL;
DECLARE vProblemLines INT;
DECLARE vTotalLines INT;
SELECT COUNT(*) INTO vProblemLines
FROM tmp.salesToSplit
WHERE ticketFk = vTicketFk;
SELECT COUNT(*) INTO vTotalLines
FROM sale s
WHERE s.ticketFk = vTicketFk;
SET vHasFullProblem = (vTotalLines = vProblemLines);
-- Ticket completo
IF vHasFullProblem THEN
UPDATE ticket
SET landed = vDated + INTERVAL 1 DAY,
shipped = vDated,
nickname = CONCAT('(',DAY(util.VN_CURDATE()),') ', nickname)
WHERE id = vTicketFk;
SELECT 'moved' message, NULL ticketFuture;
LEAVE proc;
END IF;
-- Ticket a futuro existe
IF vTicketFutureFk THEN
UPDATE sale s
JOIN tmp.salesToSplit ss ON s.id = ss.saleFk
SET s.ticketFk = vTicketFutureFk,
s.concept = CONCAT('(s) ', s.concept)
WHERE ss.ticketFk = vTicketFk;
SELECT 'future' message, NULL ticketFuture;
LEAVE proc;
END IF;
-- Ticket nuevo
CALL ticket_Clone(vTicketFk, vTicketFutureFk);
UPDATE ticket t
JOIN productionConfig pc
SET t.routeFk = IF(t.shipped = vDated , t.routeFk, NULL),
t.landed = vDated + INTERVAL 1 DAY,
t.shipped = vDated,
t.agencyModeFk = pc.defautlAgencyMode,
t.zoneFk = pc.defaultZone
WHERE t.id = vTicketFutureFk;
UPDATE sale s
JOIN tmp.salesToSplit sts ON sts.saleFk = s.id
SET s.ticketFk = vTicketFutureFk,
s.concept = CONCAT('(s) ', s.concept)
WHERE sts.ticketFk = vTicketFk;
CALL ticket_setState(vTicketFutureFk, 'FIXING');
SELECT 'new' message, vTicketFutureFk ticketFuture;
END$$
DELIMITER ;