2020-04-20 11:49:33 +00:00
|
|
|
USE `vn`;
|
|
|
|
DROP procedure IF EXISTS `ticketCalculateClon`;
|
|
|
|
|
|
|
|
DELIMITER $$
|
|
|
|
USE `vn`$$
|
|
|
|
CREATE DEFINER=`root`@`%` PROCEDURE `ticketCalculateClon`(IN vTicketNew INT, vTicketOld INT)
|
|
|
|
BEGIN
|
2020-04-23 15:59:46 +00:00
|
|
|
/*
|
|
|
|
* Recalcula los componentes un ticket clonado,
|
|
|
|
* las lineas a precio cero fuerza para que tengan precio, el resto lo respeta
|
|
|
|
* @param vTicketNew nuevo ticket clonado
|
|
|
|
* @param vTicketOld icket original, a partir del qual se clonara el nuevo
|
|
|
|
*/
|
2020-04-20 11:49:33 +00:00
|
|
|
DECLARE vShipped DATE;
|
|
|
|
DECLARE vClient INT;
|
|
|
|
DECLARE vWarehouse SMALLINT;
|
2020-04-23 15:59:46 +00:00
|
|
|
DECLARE vAgencyMode INT;
|
|
|
|
DECLARE vAddress INT;
|
2020-04-20 11:49:33 +00:00
|
|
|
DECLARE vLanded DATE;
|
2020-04-23 15:59:46 +00:00
|
|
|
DECLARE vAgency INT;
|
|
|
|
DECLARE vZoneFk INT;
|
2020-04-20 11:49:33 +00:00
|
|
|
|
2020-04-23 15:59:46 +00:00
|
|
|
REPLACE INTO orderTicket(orderFk,ticketFk)
|
2020-04-20 11:49:33 +00:00
|
|
|
SELECT orderFk, vTicketNew
|
|
|
|
FROM orderTicket
|
|
|
|
WHERE ticketFk = vTicketOld;
|
|
|
|
|
|
|
|
SELECT t.clientFk, t.warehouseFk, date(t.shipped), t.addressFk, t.agencyModeFk, t.landed, a.agencyFk, t.zoneFk
|
|
|
|
INTO vClient, vWarehouse, vShipped, vAddress, vAgencyMode, vLanded, vAgency, vZoneFk
|
2020-04-23 15:59:46 +00:00
|
|
|
FROM agencyMode a
|
|
|
|
JOIN ticket t ON t.agencyModeFk = a.id
|
|
|
|
WHERE t.id = vTicketNew;
|
2020-04-20 11:49:33 +00:00
|
|
|
|
|
|
|
IF vLanded IS NULL THEN
|
|
|
|
CALL zone_getLanded(vShipped, vAddress, vAgency, vWarehouse);
|
2020-04-23 15:59:46 +00:00
|
|
|
UPDATE ticket t
|
2020-04-20 11:49:33 +00:00
|
|
|
JOIN tmp.zoneGetLanded zgl ON t.warehouseFk = zgl.warehouseFk
|
|
|
|
SET t.landed = zgl.landed,
|
|
|
|
t.zone = zgl.zoneFk
|
|
|
|
WHERE t.id = vTicketNew;
|
|
|
|
|
2020-04-23 15:59:46 +00:00
|
|
|
SELECT zoneFk INTO vZoneFk FROM tmp.zoneGetLanded LIMIT 1;
|
2020-04-20 11:49:33 +00:00
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.zoneGetLanded;
|
2020-04-23 15:59:46 +00:00
|
|
|
END IF;
|
|
|
|
|
|
|
|
-- rellena la tabla tmp.buyUltimate con la ultima compra
|
|
|
|
CALL buyUltimate(vWarehouse, vShipped);
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.ticketLot;
|
|
|
|
CREATE TEMPORARY TABLE tmp.ticketLot
|
2020-04-20 11:49:33 +00:00
|
|
|
SELECT vWarehouse 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 = vTicketOld GROUP BY s.itemFk;
|
2020-04-23 15:59:46 +00:00
|
|
|
|
2020-04-20 11:49:33 +00:00
|
|
|
CALL catalog_componentPrepare();
|
|
|
|
CALL catalog_componentCalculate(vZoneFk, vAddress, vAgencyMode, vWarehouse);
|
2020-04-23 15:59:46 +00:00
|
|
|
|
|
|
|
-- Bionizamos lineas con Preu = 0
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.sale;
|
2020-04-20 11:49:33 +00:00
|
|
|
CREATE TEMPORARY TABLE tmp.sale
|
|
|
|
(PRIMARY KEY (saleFk)) ENGINE = MEMORY
|
|
|
|
SELECT s.id saleFk, vWarehouse warehouseFk
|
|
|
|
FROM sale s
|
|
|
|
JOIN ticket t on t.id = s.ticketFk WHERE s.ticketFk = vTicketNew AND s.price = 0;
|
2020-04-23 15:59:46 +00:00
|
|
|
|
2020-04-20 11:49:33 +00:00
|
|
|
CALL ticketComponentUpdateSale(1);
|
2020-04-23 15:59:46 +00:00
|
|
|
|
|
|
|
-- Bionizamos lineas con Preu > 0
|
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
|
|
|
|
SELECT s.id saleFk, vWarehouse warehouseFk
|
|
|
|
FROM sale s
|
|
|
|
JOIN ticket t on t.id = s.ticketFk WHERE s.ticketFk = vTicketNew
|
2020-04-23 15:59:46 +00:00
|
|
|
AND s.price > 0;
|
|
|
|
|
2020-04-20 11:49:33 +00:00
|
|
|
CALL ticketComponentUpdateSale(6);
|
2020-04-23 15:59:46 +00:00
|
|
|
|
2020-04-20 11:49:33 +00:00
|
|
|
-- Log
|
2020-04-23 15:59:46 +00:00
|
|
|
CALL `logAdd`(vTicketNew, 'update', ' ticket' , 'Bioniza Ticket');
|
2020-04-20 11:49:33 +00:00
|
|
|
|
|
|
|
-- Limpieza
|
2020-04-23 15:59:46 +00:00
|
|
|
CALL catalog_componentPurge();
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS
|
|
|
|
tmp.buyUltimate,
|
|
|
|
tmp.sale,
|
|
|
|
tmp.zoneGetLanded;
|
2020-04-20 11:49:33 +00:00
|
|
|
|
|
|
|
END$$
|
|
|
|
|
|
|
|
DELIMITER ;
|
|
|
|
|