2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-07-22 09:52:42 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`sale_recalcComponent`(vOption VARCHAR(25))
|
2024-01-15 11:31:03 +00:00
|
|
|
proc: BEGIN
|
|
|
|
/**
|
|
|
|
* Este procedimiento recalcula los componentes de un conjunto de sales,
|
|
|
|
* eliminando los componentes existentes e insertandolos de nuevo
|
|
|
|
*
|
|
|
|
* @param vOption si no se quiere forzar llamar con NULL
|
|
|
|
* @table tmp.recalculateSales (id)
|
|
|
|
*/
|
|
|
|
DECLARE vShipped DATE;
|
|
|
|
DECLARE vWarehouseFk SMALLINT;
|
2024-01-25 16:33:54 +00:00
|
|
|
DECLARE vAgencyModeFk INT;
|
|
|
|
DECLARE vAddressFk INT;
|
|
|
|
DECLARE vTicketFk INT;
|
|
|
|
DECLARE vLanded DATE;
|
|
|
|
DECLARE vIsEditable BOOLEAN;
|
|
|
|
DECLARE vZoneFk INTEGER;
|
|
|
|
DECLARE vDone BOOL DEFAULT FALSE;
|
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE vCur CURSOR FOR
|
|
|
|
SELECT DISTINCT s.ticketFk
|
|
|
|
FROM tmp.recalculateSales rs
|
|
|
|
JOIN vn.sale s ON s.id = rs.id;
|
2024-01-25 16:33:54 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
2024-01-25 16:33:54 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
OPEN vCur;
|
|
|
|
|
|
|
|
l: LOOP
|
|
|
|
SET vDone = FALSE;
|
|
|
|
FETCH vCur INTO vTicketFk;
|
|
|
|
|
|
|
|
IF vDone THEN
|
|
|
|
LEAVE l;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
SELECT (hasToRecalcPrice OR ts.alertLevel IS NULL) AND t.refFk IS NULL,
|
|
|
|
t.zoneFk,
|
|
|
|
t.warehouseFk,
|
|
|
|
t.shipped,
|
|
|
|
t.addressFk,
|
|
|
|
t.agencyModeFk,
|
|
|
|
t.landed
|
|
|
|
INTO vIsEditable,
|
|
|
|
vZoneFk,
|
|
|
|
vWarehouseFk,
|
|
|
|
vShipped,
|
|
|
|
vAddressFk,
|
|
|
|
vAgencyModeFk,
|
|
|
|
vLanded
|
|
|
|
FROM ticket t
|
|
|
|
LEFT JOIN ticketState ts ON t.id = ts.ticketFk
|
|
|
|
LEFT JOIN alertLevel al ON al.id = ts.alertLevel
|
|
|
|
WHERE t.id = vTicketFk;
|
|
|
|
|
|
|
|
CALL zone_getLanded(vShipped, vAddressFk, vAgencyModeFk, vWarehouseFk, TRUE);
|
|
|
|
|
|
|
|
IF NOT EXISTS (SELECT TRUE FROM tmp.zoneGetLanded LIMIT 1) THEN
|
|
|
|
CALL util.throw(CONCAT('There is no zone for these parameters ', vTicketFk));
|
|
|
|
END IF;
|
2024-01-25 16:33:54 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
IF vLanded IS NULL OR vZoneFk IS NULL THEN
|
|
|
|
|
|
|
|
UPDATE ticket t
|
|
|
|
SET t.landed = (SELECT landed FROM tmp.zoneGetLanded LIMIT 1)
|
|
|
|
WHERE t.id = vTicketFk AND t.landed IS NULL;
|
2024-01-25 16:33:54 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
IF vZoneFk IS NULL THEN
|
|
|
|
SELECT zoneFk INTO vZoneFk FROM tmp.zoneGetLanded LIMIT 1;
|
|
|
|
UPDATE ticket t
|
|
|
|
SET t.zoneFk = vZoneFk
|
|
|
|
WHERE t.id = vTicketFk AND t.zoneFk IS NULL;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tmp.zoneGetLanded;
|
2024-01-25 16:33:54 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
-- rellena la tabla buyUltimate con la ultima compra
|
2024-01-25 16:33:54 +00:00
|
|
|
CALL buyUltimate (vWarehouseFk, vShipped);
|
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.sale
|
|
|
|
(PRIMARY KEY (saleFk)) ENGINE = MEMORY
|
|
|
|
SELECT s.id saleFk, vWarehouseFk warehouseFk
|
|
|
|
FROM sale s
|
|
|
|
JOIN tmp.recalculateSales rs ON s.id = rs.id
|
|
|
|
WHERE s.ticketFk = vTicketFk;
|
|
|
|
|
2024-01-25 16:33:54 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketLot
|
2024-01-15 11:31:03 +00:00
|
|
|
SELECT vWarehouseFk warehouseFk, NULL available, s.itemFk, bu.buyFk, vZoneFk zoneFk
|
|
|
|
FROM sale s
|
|
|
|
JOIN tmp.recalculateSales rs ON s.id = rs.id
|
|
|
|
LEFT JOIN tmp.buyUltimate bu ON bu.itemFk = s.itemFk
|
|
|
|
WHERE s.ticketFk = vTicketFk
|
|
|
|
GROUP BY s.itemFk;
|
|
|
|
|
|
|
|
CALL catalog_componentPrepare();
|
|
|
|
CALL catalog_componentCalculate(vZoneFk, vAddressFk, vShipped, vWarehouseFk);
|
|
|
|
|
|
|
|
IF vOption IS NULL THEN
|
2024-01-25 16:33:54 +00:00
|
|
|
SET vOption = IF(vIsEditable, 'renewPrices', 'imbalance');
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
CALL ticketComponentUpdateSale(vOption);
|
|
|
|
CALL catalog_componentPurge();
|
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
DROP TEMPORARY TABLE tmp.buyUltimate;
|
2024-01-25 16:33:54 +00:00
|
|
|
DROP TEMPORARY TABLE tmp.sale;
|
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
END LOOP;
|
|
|
|
CLOSE vCur;
|
|
|
|
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|