salix/db/routines/vn/procedures/sectorCollectionSaleGroup_a...

58 lines
1.6 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`sectorCollectionSaleGroup_add`(vSaleGroupFk INT, vSectorCollectionFk INT)
BEGIN
/**
* Inserta un nuevo registro en vn.sectorCollectionSaleGroup
* Actualiza el usuario en vn.saleGroup y reemplaza los registros de vn.saleTracking
*
* @param vSaleGroupFk Identificador de vn.saleGroup
* @param vSectorCollectionFk Identificador de vn.sectorCollection
*/
2024-02-20 15:10:16 +00:00
DECLARE vHasSaleGroup INT;
DECLARE vHasSectorCollection INT;
SELECT COUNT(id) INTO vHasSaleGroup
2024-02-21 06:08:45 +00:00
FROM vn.saleGroup
WHERE id = vSaleGroupFk;
2024-02-20 15:10:16 +00:00
IF !vHasSaleGroup THEN
2024-02-21 06:08:45 +00:00
CALL util.throw ("invalid saleGroup");
2024-02-20 15:10:16 +00:00
END IF;
SELECT COUNT(id) INTO vHasSectorCollection
2024-02-21 06:08:45 +00:00
FROM vn.sectorCollection
WHERE id = vSectorCollectionFk;
2024-02-20 15:10:16 +00:00
IF !vHasSectorCollection THEN
2024-02-21 06:08:45 +00:00
CALL util.throw ("invalid sectorCollection");
2024-02-20 15:10:16 +00:00
END IF;
REPLACE sectorCollectionSaleGroup
SET sectorCollectionFk = vSectorCollectionFk,
saleGroupFk = vSaleGroupFk;
UPDATE saleGroup sg
JOIN sectorCollectionSaleGroup scsg ON scsg.saleGroupFk = sg.id
JOIN sectorCollection sc ON sc.id = scsg.sectorCollectionFk
SET sg.userFk = sc.userFk
WHERE sg.id = vSaleGroupFk;
INSERT IGNORE saleTracking(
saleFk,
isChecked,
workerFk,
stateFk
)
SELECT sgd.saleFk ,
FALSE,
sc.userFk,
s.id
FROM saleGroupDetail sgd
JOIN sectorCollectionSaleGroup scsg
ON scsg.saleGroupFk = sgd.saleGroupFk
JOIN sectorCollection sc ON sc.id = scsg.sectorCollectionFk
JOIN state s ON s.code = 'PREVIOUS_PREPARATION'
WHERE sgd.saleGroupFk = vSaleGroupFk;
END$$
DELIMITER ;