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

49 lines
1.1 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`travel_clone`(vSelf INT, vDays INT, OUT vNewTravelFk INT)
BEGIN
/**
* Clona un travel el número de dias indicado y devuelve su id.
*
* @param vSelf Identificador de vn.travel
* @param vDays Número de dias a futuro
* @return vNewTravelFk Nuevo número de travel
*/
SELECT tr.id INTO vNewTravelFk
FROM vn.travel tr
JOIN vn.travel tr2 USING(shipped, warehouseInFk, warehouseOutFk, agencyModeFk, `ref`)
WHERE tr2.id = vSelf
AND tr.landed = TIMESTAMPADD(DAY, vDays, tr2.landed);
IF ISNULL(vNewTravelFk) THEN
START TRANSACTION;
INSERT INTO vn.travel( shipped,
shipmentHour,
landed,
landingHour,
warehouseInFk,
warehouseOutFk,
agencyModeFk,
`ref`,
cargoSupplierFk,
clonedFrom)
SELECT shipped,
shipmentHour,
TIMESTAMPADD(DAY, vDays, landed),
landingHour,
warehouseInFk,
warehouseOutFk,
agencyModeFk,
`ref`,
cargoSupplierFk,
vSelf
FROM vn.travel
WHERE id = vSelf;
SELECT LAST_INSERT_ID() INTO vNewTravelFk;
COMMIT;
END IF;
END$$
DELIMITER ;