

DROP procedure IF EXISTS `vn`.`ticketComponentUpdateSale`;

DELIMITER $$

CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`ticketComponentUpdateSale`(vOption INT)
BEGIN
/**
 * A partir de la tabla tmp.sale, crea los Movimientos_componentes
 * y modifica el campo Preu de la tabla Movimientos
 *
 * @param i_option integer tipo de actualizacion
 * @param table tmp.sale  tabla memory con el campo saleFk, warehouseFk
 **/
	DECLARE vComponentFk INT;
    DECLARE vRenewComponents BOOLEAN;
    DECLARE vKeepPrices BOOLEAN;
    
    CASE vOption
		WHEN 1 THEN  
			SET vRenewComponents = TRUE;
            SET vKeepPrices = FALSE;
		WHEN 2 THEN
			SET vComponentFk = 17;  
			SET vRenewComponents = TRUE;
            SET vKeepPrices = TRUE;
		WHEN 3 THEN 
			SET vComponentFk = 37; 
			SET vRenewComponents = TRUE;
            SET vKeepPrices = TRUE;	
		WHEN 4 THEN 
			SET vComponentFk = 34; 
			SET vRenewComponents = TRUE;
            SET vKeepPrices = TRUE;
        WHEN 5 THEN 
			SET vComponentFk = 35;
			SET vRenewComponents = TRUE;
            SET vKeepPrices = TRUE;
		WHEN 6 THEN 
    		SET vComponentFk = 36;   
			SET vRenewComponents = TRUE;
            SET vKeepPrices = TRUE;
        WHEN 7 THEN
		    REPLACE INTO saleComponent(saleFk, componentFk, value)
				SELECT s.id, 28, ROUND(((s.price * (100 - s.discount) / 100) - SUM(IFNULL(sc.value, 0))) * 0.8, 3) 
				    FROM sale s
                        JOIN tmp.sale tmps ON tmps.saleFk = s.id
				        LEFT JOIN saleComponent sc ON sc.saleFk = s.id 
                            AND sc.componentFk NOT IN (28, 29)
                    GROUP BY s.id;
			
			REPLACE INTO saleComponent(saleFk, componentFk, value)
				SELECT s.id, 29, ROUND(((s.price * (100 - s.discount) / 100) - SUM(IFNULL(sc.value, 0))) * 0.2, 3) 
				    FROM sale s
                        JOIN tmp.sale tmps ON tmps.saleFk = s.id
				        LEFT JOIN saleComponent sc ON sc.saleFk = s.id 
                            AND sc.componentFk NOT IN (28, 29)
                GROUP BY s.id;
                
			SET vRenewComponents = FALSE;
            SET vKeepPrices = FALSE;
		WHEN 8 THEN
            DELETE sc.* 
				FROM tmp.sale tmps JOIN saleComponent sc ON sc.saleFk = tmps.saleFk;
			
			REPLACE INTO saleComponent(saleFk, componentFk, value)
				SELECT s.id, 28, ROUND(((s.price * (100 - s.discount) / 100)), 3) 
                    FROM sale s
                        JOIN tmp.sale tmps ON tmps.saleFk = s.id;
				
			SET vRenewComponents = FALSE;
            SET vKeepPrices = FALSE;
		WHEN 9 THEN  
			SET vRenewComponents = TRUE;
            SET vKeepPrices = TRUE;
    END CASE;

    IF vRenewComponents THEN
        DELETE sc.* 
            FROM tmp.sale tmps 
                JOIN saleComponent sc ON sc.saleFk = tmps.saleFk
                JOIN `component` c ON c.id = sc.componentFk
        WHERE c.isRenewable;
				
        REPLACE INTO saleComponent(saleFk, componentFk, value)
            SELECT s.id, tc.componentFk, tc.cost
                FROM sale s 
                    JOIN tmp.sale tmps ON tmps.saleFk = s.id
                    JOIN tmp.ticketComponent tc ON tc.itemFk = s.itemFk AND tc.warehouseFk = tmps.warehouseFk
                    LEFT JOIN saleComponent sc ON sc.saleFk = s.id 
                        AND sc.componentFk = tc.componentFk 
                    LEFT JOIN `component` c ON c.id = tc.componentFk 
                WHERE IF(sc.componentFk IS NULL AND NOT c.isRenewable, FALSE, TRUE);
    END IF;

    IF vKeepPrices THEN
		REPLACE INTO saleComponent(saleFk, componentFk, value)
		    SELECT s.id, vComponentFk, ROUND((s.price * (100 - s.discount) / 100) - SUM(sc.value), 3) dif
		        FROM sale s
                    JOIN tmp.sale tmps ON tmps.saleFk = s.id
                    LEFT JOIN saleComponent sc ON sc.saleFk = s.id
		    WHERE  sc.saleFk <> vComponentFk
		    GROUP BY s.id
		    HAVING dif <> 0;
    ELSE
		UPDATE sale s
			JOIN item i on i.id = s.itemFk
			JOIN itemType it on it.id = i.typeFk
				JOIN (SELECT SUM(sc.value) sumValue, sc.saleFk 
						FROM saleComponent sc
							JOIN tmp.sale tmps ON tmps.saleFk = sc.saleFk
						GROUP BY sc.saleFk) sc ON sc.saleFk = s.id
				SET s.price = sumValue
			WHERE it.code != 'PRT' ; 
            
		REPLACE INTO saleComponent(saleFk, componentFk, value)
			SELECT s.id, 21, ROUND((s.price * (100 - s.discount) / 100) - SUM(value), 3) saleValue
				FROM sale s
					JOIN tmp.sale tmps ON tmps.saleFk = s.id
					LEFT JOIN saleComponent sc ON sc.saleFk = s.id
				WHERE sc.componentFk != 21
				GROUP BY s.id 
				HAVING ROUND(saleValue, 4) <> 0;
    END IF;
    
    UPDATE sale s
		JOIN (
			SELECT SUM(sc.value) sumValue, sc.saleFk 
				FROM saleComponent sc
					JOIN tmp.sale tmps ON tmps.saleFk = sc.saleFk
					JOIN `component` c ON c.id = sc.componentFk
					JOIN componentType ct on ct.id = c.typeFk AND ct.isBase
				GROUP BY sc.saleFk) sc ON sc.saleFk = s.id
		SET s.priceFixed = sumValue, s.isPriceFixed = 1;

    DELETE sc.*
		FROM saleComponent sc
			JOIN tmp.sale tmps ON tmps.saleFk = sc.saleFk
			JOIN sale s on s.id = sc.saleFk
			JOIN item i ON i.id = s.itemFk
			JOIN itemType it ON it.id = i.typeFk
		WHERE it.code = 'PRT'; 
		
	INSERT INTO saleComponent(saleFk, componentFk, value)
		SELECT s.id, 15, s.price   
			FROM sale s
				JOIN tmp.sale tmps ON tmps.saleFk = s.id
				JOIN item i ON i.id = s.itemFK
				JOIN itemType it ON it.id = i.typeFk
		WHERE it.code = 'PRT' AND s.price > 0;
END$$

DELIMITER ;

