create procedure ventas_crece_por_mescontado (
anio smallint)
returns (
nombremes varchar(25),
monto_ventas numeric(12,2),
monto_crecimiento numeric(12,2),
crecimiento numeric(12,2))
as
declare variable mes smallint;
declare variable monto_totalnc numeric(12,2);
declare variable mes_anterior numeric(12,2);
begin
-- Inicializo variables
mes_anterior = 0;
mes = 1;
while (mes <= 12) do
begin
-- Calculo nombre de mes
nombremes = decode(mes, 1, 'ENE', 2, 'FEB', 3, 'MAR', 4, 'ABR', 5, 'MAY', 6, 'JUN', 7, 'JUL', 8, 'AGO', 9, 'SEP', 10, 'OCT', 11, 'NOV', 12, 'DIC');
-- Calculo FACTURAS.MONTOTOTAL
select sum(f.montototal)
from facturas_ventas f
where
extract(month from f.fecha) = :mes and
extract(year from f.fecha) = :anio
into :monto_ventas;
-- Calculo NOTAS_CREADITOS.IMPORTE_TOTAL
select sum(n.importe_total)
from notas_creditos n
where
extract(month from n.fecha_actual) = :mes and
extract(year from n.fecha_actual) = :anio
into :monto_totalnc;
-- Limpio datos
monto_ventas = coalesce(monto_ventas, 0.00);
monto_totalnc = coalesce(monto_totalnc, 0.00);
-- Calculo MONTO_VENTAS
monto_ventas = (monto_ventas - coalesce(monto_totalnc, 0.00));
-- Calculo importe de crecimiento / decrecimiento
monto_crecimiento = mes_anterior - monto_ventas;
-- Calculo porcentaje de crecimiento / decrecimiento
crecimiento = 0;
if (monto_ventas <> 0) then
crecimiento = (1 - (mes_anterior / monto_ventas)) * 100;
-- Guardo MONTO_VENTAS para siguente vuelta del bucle con informacion del mes anterior
mes_anterior = monto_ventas;
-- Devuelvo datos para el mes
suspend;
mes = mes + 1;
end
end;