Skip to content

Commit

Permalink
Merge pull request #40 from Jrgil20/feature/R2_gananciasNetas
Browse files Browse the repository at this point in the history
Feature/r2 ganancias netas
  • Loading branch information
Jrgil20 authored Jan 11, 2025
2 parents 41a31b4 + 5a8b31a commit 3ba8587
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions ganaciasNetas.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CREATE OR REPLACE FUNCTION ganancias_floristeria(
p_idFloristeria NUMERIC,
p_mes DATE
)
RETURNS TABLE(
ganancias_brutas NUMERIC,
costos NUMERIC,
ganancias_netas NUMERIC
)
AS $$
DECLARE
fecha_inicio DATE := DATE_TRUNC('month', p_mes);
fecha_fin DATE := (DATE_TRUNC('month', p_mes) + INTERVAL '1 month') - INTERVAL '1 day';
_ganancias_brutas NUMERIC;
_costos NUMERIC;
BEGIN
SELECT COALESCE(SUM(montoTotal), 0)
INTO _ganancias_brutas
FROM FACTURA_FINAL
WHERE idFloristeria = p_idFloristeria
AND fechaEmision >= fecha_inicio
AND fechaEmision <= fecha_fin;

SELECT COALESCE(SUM(montoTotal), 0)
INTO _costos
FROM FACTURA
WHERE idAfiliacionFloristeria = p_idFloristeria
AND fechaEmision >= fecha_inicio
AND fechaEmision <= fecha_fin;

RETURN QUERY SELECT _ganancias_brutas, _costos, _ganancias_brutas - _costos;
END;
$$ LANGUAGE plpgsql;

0 comments on commit 3ba8587

Please sign in to comment.