Skip to content

Commit

Permalink
fix postgresql procedures, and dashboard reports
Browse files Browse the repository at this point in the history
  • Loading branch information
sekiskylink committed Feb 15, 2017
1 parent a407787 commit d1cdb19
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
1 change: 1 addition & 0 deletions requirements/production.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Jinja2
psycopg2
phonenumbers
simplejson
xlwt
57 changes: 30 additions & 27 deletions schema/llin.sql
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,16 @@ CREATE OR REPLACE FUNCTION public.get_children(loc_id integer)
$delim$
DECLARE
r locations_view%ROWTYPE;
our_lft INTEGER;
our_rght INTEGER;
lvl INTEGER;
BEGIN
--SELECT INTO our_lft lft FROM locations WHERE id = loc_id;
--SELECT INTO our_rght rght FROM locations WHERE id = loc_id;
SELECT lft, rght, level INTO our_lft, our_rght, lvl FROM locations_view WHERE id = loc_id;
FOR r IN SELECT * FROM locations_view WHERE lft > our_lft AND rght < our_rght AND level = lvl + 1
FOR r IN SELECT * FROM locations_view WHERE tree_parent_id = loc_id
LOOP
RETURN NEXT r;
END LOOP;
RETURN;
END;
$delim$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION public.get_descendants(loc_id bigint)
RETURNS SETOF locations_view AS
$delim$
Expand Down Expand Up @@ -534,6 +529,7 @@ CREATE VIEW distribution_log_w2sc_view AS
a.quantity_nets, b.name as warehouse, c.name as branch,
a.departure_date, a.departure_time,
get_location_name(a.destination) as destination,
a.destination destination_id,
get_location_name(a.district_id) as district, a.remarks,
a.arrival_date, a.arrival_time, a.quantity_received,
a.is_delivered, a.is_received, a.has_variance, a.created_by,
Expand Down Expand Up @@ -660,17 +656,14 @@ CREATE OR REPLACE FUNCTION public.get_district(loc_id bigint)
LANGUAGE plpgsql
AS $function$
DECLARE
r TEXT;
r TEXT := '';
our_lft INTEGER;
our_rght INTEGER;
BEGIN
SELECT lft, rght INTO our_lft, our_rght FROM locations WHERE id = loc_id;
FOR r IN SELECT name FROM locations WHERE lft <= our_lft AND rght >= our_rght AND
type_id=(SELECT id FROM locationtype WHERE name = 'district')
LOOP
RETURN r;
END LOOP;
RETURN '';
SELECT name into r FROM locations WHERE lft <= our_lft AND rght >= our_rght AND
type_id=(SELECT id FROM locationtype WHERE name = 'district');
RETURN r;
END;
$function$;

Expand All @@ -684,12 +677,25 @@ AS $function$
our_rght BIGINT;
BEGIN
SELECT lft, rght INTO our_lft, our_rght FROM locations WHERE id = loc_id;
FOR r IN SELECT id FROM locations WHERE lft <= our_lft AND rght >= our_rght AND
type_id=(SELECT id FROM locationtype WHERE name = 'district')
LOOP
RETURN r;
END LOOP;
RETURN 0;
SELECT id INTO r FROM locations WHERE lft <= our_lft AND rght >= our_rght AND
type_id=(SELECT id FROM locationtype WHERE name = 'district');
RETURN r;
END;
$function$;

CREATE OR REPLACE FUNCTION public.get_subcounty_id(loc_id bigint)
RETURNS bigint
LANGUAGE plpgsql
AS $function$
DECLARE
r BIGINT;
our_lft BIGINT;
our_rght BIGINT;
BEGIN
SELECT lft, rght INTO our_lft, our_rght FROM locations WHERE id = loc_id;
SELECT id INTO r FROM locations WHERE lft <= our_lft AND rght >= our_rght AND
type_id=(SELECT id FROM locationtype WHERE name = 'subcounty');
RETURN r;
END;
$function$;

Expand All @@ -698,17 +704,14 @@ CREATE OR REPLACE FUNCTION public.get_ancestor_by_type(loc_id bigint, atype text
LANGUAGE plpgsql
AS $function$
DECLARE
r TEXT;
r TEXT := '';
our_lft INTEGER;
our_rght INTEGER;
BEGIN
SELECT lft, rght INTO our_lft, our_rght FROM locations WHERE id = loc_id;
FOR r IN SELECT name FROM locations WHERE lft <= our_lft AND rght >= our_rght AND
type_id=(SELECT id FROM locationtype WHERE name = atype)
LOOP
RETURN r;
END LOOP;
RETURN '';
SELECT name INTO r FROM locations WHERE lft <= our_lft AND rght >= our_rght AND
type_id=(SELECT id FROM locationtype WHERE name = atype);
RETURN r;
END;
$function$;

Expand Down
2 changes: 1 addition & 1 deletion web/app/controllers/dashboard_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def GET(self):
total_nets = total_nets[0]
r = db.query("SELECT SUM(quantity_bales) AS total FROM distribution_log_w2sc_view")
sc_dist = r[0].total if r else 0
r = db.query("SELECT count(distinct id) FROM distribution_log_w2sc_view")
r = db.query("SELECT count(distinct destination_id) FROM distribution_log_w2sc_view")
sc_count = r[0].count or 0
l = locals()
del l['self']
Expand Down
4 changes: 2 additions & 2 deletions web/upload_rapidpro_reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def add_reporter_fields():

cur.execute(
"UPDATE reporters set reporting_location = get_subcounty_id(reporting_location) "
"WHERE id in (SELECT reporter_id FROM reporter_groups_reporters WHERE group_id = "
"(SELECT id FROM reporter_groups WHERE name = 'Subcounty Store Manager'))")
"WHERE id in (SELECT reporter_id FROM reporter_groups_reporters WHERE group_id IN "
"(SELECT id FROM reporter_groups WHERE name IN ('Subcounty Store Manager', 'Subcounty Chief')))")
conn.commit()

cur.execute(
Expand Down

0 comments on commit d1cdb19

Please sign in to comment.