USE `vn`;
DROP procedure IF EXISTS `ticketCalculateClon`;

DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`%` PROCEDURE `ticketCalculateClon`(IN vTicketNew INT, vTicketOld INT)
BEGIN
	/*
	* @vTicketNew id del nuevo ticket clonado
    * @vTicketOld id ticket original, a partir del qual se clonara el nuevo
	* Este procedimiento "rebioniza" una linea, eliminando los componentes existentes e insertandolos de nuevo
	*/
	DECLARE vShipped DATE;
	DECLARE vClient INT;
	DECLARE vWarehouse SMALLINT;
    DECLARE vAgencyMode INT;
    DECLARE vAddress INT;
	DECLARE vLanded DATE;
    DECLARE vAgency INT;

    INSERT INTO orderTicket(orderFk,ticketFk)
		SELECT orderFk, vTicketNew 
			FROM orderTicket 
            WHERE ticketFk = vTicketOld;
    	
	SELECT t.clientFk , t.warehouseFk, date(t.shipped), t.addressFk, t.agencyModeFk, t.landed, a.agencyFk
		INTO vClient, vWarehouse, vShipped, vAddress, vAgencyMode, vLanded, vAgency
    FROM vn.agencyMode a
		JOIN vn.ticket t ON t.agencyModeFk = a.id
    WHERE t.id = vTicketNew;
    
	DROP TEMPORARY TABLE IF EXISTS tmp.agencyHourGetShipped;
	CREATE TEMPORARY TABLE tmp.agencyHourGetShipped ENGINE = MEMORY
		SELECT vWarehouse warehouseFk, vShipped shipped, vLanded landed;
    
    CALL buyUltimate(vWarehouse, vShipped);  -- rellena la tabla tmp.buyUltimate con la ultima compra
    
    DROP TEMPORARY TABLE IF EXISTS tmp.ticketLot;
    CREATE TEMPORARY TABLE tmp.ticketLot 
	SELECT vWarehouse warehouseFk,NULL available,s.itemFk, bu.buyFk
		FROM sale s
			LEFT JOIN tmp.buyUltimate bu ON bu.itemFk = s.itemFk
		WHERE s.ticketFk = vTicketOld GROUP BY s.itemFk;
                
	CALL ticketComponentCalculate(vAddress,vAgencyMode);
    
    -- Bionizamos lineas con Preu = 0
    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 AND s.price = 0;
                
	CALL ticketComponentUpdateSale(1);
    
    -- Bionizamos lineas con Preu > 0
	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
                    AND s.price > 0;
                
	CALL ticketComponentUpdateSale(6);

	IF vLanded IS NULL THEN
		CALL agencyHourGetLanded(vShipped, vAddress, vAgency,vWarehouse);
        
        UPDATE ticket t
				JOIN tmp.agencyHourGetLanded ah ON t.warehouseFk = ah.warehouseFk 
			SET t.landed = ah.landed 
			WHERE t.id = vTicketNew;
    END IF;
	
	-- Log
    CALL `logAdd`(vTicketNew, 'update', ' ticket' , 'Bioniza Ticket');

	-- Limpieza
	DROP TEMPORARY TABLE IF EXISTS tmp.buyUltimate;
    DROP TEMPORARY TABLE IF EXISTS tmp.sale;
    DROP TEMPORARY TABLE IF EXISTS tmp.ticketLot;
     DROP TEMPORARY TABLE IF EXISTS agencyHourGetShipped;
END$$

DELIMITER ;

