2020-04-20 11:49:33 +00:00
|
|
|
USE `vn`;
|
|
|
|
DROP procedure IF EXISTS `ticket_recalcComponents`;
|
|
|
|
|
|
|
|
DELIMITER $$
|
|
|
|
USE `vn`$$
|
2020-04-23 15:59:46 +00:00
|
|
|
CREATE DEFINER=`root`@`%` PROCEDURE `ticket_recalcComponents`(IN vTicketFk BIGINT, vIsTicketEditable BOOLEAN)
|
2020-04-20 11:49:33 +00:00
|
|
|
proc: BEGIN
|
|
|
|
|
|
|
|
/**
|
2020-04-23 15:59:46 +00:00
|
|
|
* Este procedimiento recalcula los componentes de un ticket,
|
2020-04-20 11:49:33 +00:00
|
|
|
* eliminando los componentes existentes e insertandolos de nuevo
|
|
|
|
*
|
|
|
|
* @param vTicketFk Id del ticket
|
2020-04-23 15:59:46 +00:00
|
|
|
* @param vIsTicketEditable si no se quiere forzar llamar con NULL
|
2020-04-20 11:49:33 +00:00
|
|
|
*/
|
|
|
|
DECLARE vShipped DATE;
|
|
|
|
DECLARE vWarehouseFk SMALLINT;
|
|
|
|
DECLARE vAgencyModeFk INT;
|
|
|
|
DECLARE vAddressFk INT;
|
|
|
|
DECLARE vLanded DATE;
|
|
|
|
DECLARE vZoneFk INTEGER;
|
|
|
|
|
2020-04-23 15:59:46 +00:00
|
|
|
IF vIsTicketEditable IS NULL THEN
|
|
|
|
SELECT IFNULL(ts.alertLevel,0) = 0 AND IFNULL(t.refFk,'') = ''
|
|
|
|
INTO vIsTicketEditable
|
|
|
|
FROM ticket t LEFT JOIN ticketState ts ON t.id = ts.ticket
|
|
|
|
WHERE id = vTicketFk;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
SELECT t.warehouseFk,
|
|
|
|
t.shipped,
|
|
|
|
t.addressFk,
|
|
|
|
t.agencyModeFk,
|
|
|
|
t.landed,
|
|
|
|
t.zoneFk
|
|
|
|
INTO vWarehouseFk, vShipped, vAddressFk, vAgencyModeFk, vLanded, vZoneFk
|
|
|
|
FROM ticket t LEFT JOIN ticketState ts ON t.id = ts.ticket
|
|
|
|
WHERE t.id = vTicketFk;
|
|
|
|
|
|
|
|
IF vLanded IS NULL OR vZoneFk IS NULL THEN
|
|
|
|
|
|
|
|
CALL zone_getLanded(vShipped, vAddressFk, vAgencyModeFk, vWarehouseFk);
|
|
|
|
|
|
|
|
IF (SELECT COUNT(*) FROM tmp.zoneGetLanded LIMIT 1) = 0 THEN
|
|
|
|
CALL util.throw('There is no zone for these parameters');
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
UPDATE ticket t
|
|
|
|
SET t.landed = (SELECT landed FROM tmp.zoneGetLanded LIMIT 1)
|
|
|
|
WHERE t.id = vTicketFk AND t.landed IS NULL;
|
|
|
|
|
|
|
|
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;
|
|
|
|
DROP TEMPORARY TABLE tmp.zoneGetLanded;
|
2020-04-20 11:49:33 +00:00
|
|
|
|
2020-04-23 15:59:46 +00:00
|
|
|
END IF;
|
|
|
|
|
|
|
|
-- rellena la tabla buyUltimate con la ultima compra
|
|
|
|
CALL buyUltimate (vWarehouseFk, vShipped);
|
2020-04-20 11:49:33 +00:00
|
|
|
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.ticketLot;
|
|
|
|
CREATE TEMPORARY TABLE tmp.ticketLot
|
|
|
|
SELECT vWarehouseFk warehouseFk, NULL available,
|
|
|
|
s.itemFk, bu.buyFk, vZoneFk zoneFk
|
|
|
|
FROM sale s
|
|
|
|
LEFT JOIN tmp.buyUltimate bu ON bu.itemFk = s.itemFk
|
|
|
|
WHERE s.ticketFk = vTicketFk
|
|
|
|
GROUP BY s.itemFk;
|
|
|
|
|
|
|
|
CALL catalog_componentPrepare();
|
2020-04-23 15:59:46 +00:00
|
|
|
CALL catalog_componentCalculate(vZoneFk, vAddressFk, vShipped, vWarehouseFk);
|
|
|
|
|
2020-04-20 11:49:33 +00:00
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.sale;
|
|
|
|
CREATE TEMPORARY TABLE tmp.sale
|
|
|
|
(PRIMARY KEY (saleFk)) ENGINE = MEMORY
|
2020-04-23 15:59:46 +00:00
|
|
|
SELECT id saleFk, vWarehouseFk warehouseFk
|
|
|
|
FROM sale s
|
|
|
|
WHERE s.ticketFk = vTicketFk;
|
|
|
|
|
|
|
|
-- si el ticket esta facturado, respeta los precios
|
|
|
|
CALL ticketComponentUpdateSale(IF(vIsTicketEditable, 1, 6));
|
2020-04-20 11:49:33 +00:00
|
|
|
|
2020-04-23 15:59:46 +00:00
|
|
|
CALL catalog_componentPurge();
|
|
|
|
DROP TEMPORARY TABLE
|
|
|
|
tmp.buyUltimate,
|
|
|
|
tmp.sale;
|
2020-04-20 11:49:33 +00:00
|
|
|
END$$
|
|
|
|
|
|
|
|
DELIMITER ;
|
|
|
|
|