-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from Jrgil20/feature/R2_gananciasNetas
Feature/r2 ganancias netas
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |