73 lines
1.9 KiB
SQL
73 lines
1.9 KiB
SQL
DELIMITER $$
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`Trabuque`(IN intIDTICKET BIGINT, IN dblINCREMENTO DOUBLE )
|
|
BEGIN
|
|
|
|
|
|
|
|
#Vamos a calcular el porcentaje a incrementar las cantidades de las lineas de movimiento para inflar / desinflar la base final
|
|
|
|
DECLARE dblBASE_INICIAL DOUBLE DEFAULT 0;
|
|
DECLARE dblBASE_FINAL DOUBLE DEFAULT 0;
|
|
DECLARE dblAUMENTO DOUBLE DEFAULT 1;
|
|
|
|
|
|
SELECT SUM(Cantidad * Preu * (100 - Descuento) / 100)
|
|
INTO dblBASE_INICIAL
|
|
FROM Movimientos
|
|
WHERE Id_Ticket = intIDTICKET;
|
|
|
|
SET dblBASE_FINAL = dblBASE_INICIAL;
|
|
|
|
|
|
#Eliminamos lineas a 0
|
|
|
|
DELETE FROM Movimientos WHERE Cantidad = 0 AND Id_Ticket = intIDTICKET;
|
|
|
|
# Vamos a ir probando incrementos paulatinos hasta que encontremos el que supera lo pedido, teniendo en cuenta el sentido del incremento
|
|
|
|
IF dblINCREMENTO < 0 THEN
|
|
|
|
|
|
|
|
WHILE (dblBASE_FINAL > dblBASE_INICIAL + dblINCREMENTO) or (dblAUMENTO < 0 ) DO
|
|
|
|
SET dblAUMENTO = dblAUMENTO - 0.01;
|
|
|
|
SELECT SUM(IF(@cantidad:= ROUND(dblAUMENTO * Cantidad),@cantidad,1) * Preu * (100 - Descuento) / 100)
|
|
INTO dblBASE_FINAL
|
|
FROM Movimientos
|
|
WHERE Id_Ticket = intIDTICKET;
|
|
|
|
END WHILE;
|
|
|
|
ELSE
|
|
|
|
WHILE dblBASE_FINAL < dblBASE_INICIAL + dblINCREMENTO DO
|
|
|
|
SET dblAUMENTO = dblAUMENTO + 0.01;
|
|
|
|
SELECT SUM(IF(@cantidad:= ROUND(dblAUMENTO * Cantidad),@cantidad,1) * Preu * (100 - Descuento) / 100)
|
|
INTO dblBASE_FINAL
|
|
FROM Movimientos
|
|
WHERE Id_Ticket = intIDTICKET;
|
|
|
|
END WHILE;
|
|
|
|
END IF;
|
|
|
|
UPDATE Movimientos
|
|
SET Cantidad = IF(@cantidad:= ROUND(dblAUMENTO * Cantidad),@cantidad,1)
|
|
WHERE Id_Ticket = intIDTICKET;
|
|
|
|
SELECT tipoiva
|
|
, ROUND(SUM(Cantidad * Preu * (100 - Descuento) / 100),2) as Base
|
|
, ROUND(ROUND(SUM(Cantidad * Preu * (100 - Descuento) / 100),2) * iva / 100,2) as Cuota
|
|
FROM Movimientos M
|
|
INNER JOIN Articles A USING(Id_Article)
|
|
INNER JOIN tblIVA USING(tipoiva)
|
|
WHERE Id_Ticket = intIDTICKET
|
|
GROUP BY tipoiva;
|
|
|
|
END$$
|
|
DELIMITER ;
|