Skip to content

Commit

Permalink
support distribution to parish, with notifications going out on that …
Browse files Browse the repository at this point in the history
…level
  • Loading branch information
sekiskylink committed Feb 14, 2018
1 parent 395855b commit e2695ce
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 19 deletions.
108 changes: 91 additions & 17 deletions web/app/controllers/dispatch_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ def GET(self):
track_no_plate = r.track_no_plate
district = ""
subcounties = []
parish = r.dest_parish
parishes = []
ancestors = db.query(
"SELECT id, name, level FROM get_ancestors($loc) "
"WHERE level = 2 ORDER BY level DESC;", {'loc': r.destination})
if ancestors:
district = ancestors[0]
subcounties = db.query("SELECT id, name FROM get_children($id)", {'id': district['id']})
parishes = db.query("SELECT id, name FROM get_children($id)", {'id': r.destination})
wx = db.query(
"SELECT id FROM warehouses WHERE id = "
"(SELECT warehouse_id FROM warehouse_branches "
Expand Down Expand Up @@ -79,7 +82,7 @@ def POST(self):
ed="", d_id="", district="", subcounty="", release_order="", waybill="",
quantity_bales="", warehouse="", warehouse_branch="", departure_date="",
departure_time="", driver="", driver_tel="", remarks="", track_no_plate="",
quantity_bales_old=0)
quantity_bales_old=0, parish="")
session = get_session()
current_time = datetime.datetime.now()
allow_edit = False
Expand Down Expand Up @@ -147,13 +150,24 @@ def POST(self):
data_id = r[0]['id']
subcounty = ""
district = ""
parish = ""
# get subcounty name to use in SMS
sc = db.query(
"SELECT get_location_name($subcounty) as name;",
{'subcounty': params.subcounty})
if sc:
subcounty = sc[0]['name']

# get parish name if available
par = db.query(
"SELECT get_location_name($parish) as name;",
{'parish': params.parish})
if par:
parish = par[0]['name']
db.query(
"UPDATE distribution_log SET dest_parish = $parish "
"WHERE id = $id", {'parish': params.parish, 'id': data_id})

# get district name to use in SMS
dist = db.query(
"SELECT get_location_name($district) as name;",
Expand All @@ -166,6 +180,7 @@ def POST(self):

# appropriately edit scheduled messages
sms_args = {
'parish': parish,
'subcounty': subcounty,
'district': district,
'waybill': params.waybill,
Expand All @@ -180,6 +195,8 @@ def POST(self):
db, params.subcounty, ['Subcounty Supervisor'])
subcounty_reporters = get_location_role_reporters(
db, params.subcounty, config['subcounty_reporters'])
parish_reporters = get_location_role_reporters(
db, params.parish, config['parish_reporters'])

scheduled_msgs = db.query(
"SELECT a.schedule_id, a.level, a.triggered_by FROM distribution_log_schedules a, "
Expand All @@ -193,7 +210,10 @@ def POST(self):
"FOR UPDATE NOWAIT", {'sched_id': s['schedule_id']})

# build SMS to send to notifying parties
sms_text = config['national_sms_template'] % sms_args
sms_text = ""
if parish:
sms_text = config['parish_nets_sms_prefix_template'] % sms_args
sms_text += config['national_sms_template'] % sms_args
if s['level'] == 'national':
sms_params = {'text': sms_text, 'to': ' '.join(national_reporters)}
update_queued_sms(db, s['schedule_id'], sms_params, sched_time, session.sesid)
Expand All @@ -203,14 +223,25 @@ def POST(self):
update_queued_sms(db, s['schedule_id'], sms_params, sched_time, session.sesid)

elif s['level'] == 'subcounty':
sms_text += (
'\n Once received / offloaded, please send '
'"REC %(waybill)s %(quantity)s" to %(shortcode)s' % sms_args)
if subcounty_reporters and not parish:
sms_text += (
'\n Once received / offloaded, please send '
'"REC %(waybill)s %(quantity)s" to %(shortcode)s' % sms_args)
sms_params = {'text': sms_text, 'to': ' '.join(subcounty_reporters)}
update_queued_sms(db, s['schedule_id'], sms_params, sched_time, session.sesid)
elif s['level'] == 'parish':
if parish_reporters:
sms_text += (
'\n Once received / offloaded, please send '
'"REC %(waybill)s %(quantity)s" to %(shortcode)s' % sms_args)
sms_params = {'text': sms_text, 'to': ' '.join(parish_reporters)}
update_queued_sms(db, s['schedule_id'], sms_params, sched_time, session.sesid)

elif s['level'] == 'driver':
driver_sms_text = config['driver_sms_template'] % sms_args
if parish:
driver_sms_text = config['driver_parish_sms_template'] % sms_args
else:
driver_sms_text = config['driver_sms_template'] % sms_args
sms_params = {'text': driver_sms_text, 'to': params.driver_tel}
update_queued_sms(db, s['schedule_id'], sms_params, sched_time, session.sesid)
else: # no ready schedules were found
Expand All @@ -224,17 +255,30 @@ def POST(self):
sched_id = queue_schedule(db, sms_params, sched_time, session.sesid)
log_schedule(db, data_id, sched_id, 'national')
# print "*=======*=======*===>", national_reporters

sms_text += (
'\n Once received / offloaded, please send '
'"REC %(waybill)s %(quantity)s" to %(shortcode)s' % sms_args)
if subcounty_reporters and not parish:
sms_text += (
'\n Once received / offloaded, please send '
'"REC %(waybill)s %(quantity)s" to %(shortcode)s' % sms_args)
sms_params = {'text': sms_text, 'to': ' '.join(subcounty_reporters)}
sched_id = queue_schedule(db, sms_params, sched_time, session.sesid)
log_schedule(db, data_id, sched_id, 'subcounty')
# print "@=======@=======@===>", subcounty_reporters

if parish_reporters:
sms_text += (
'\n Once received / offloaded, please send '
'"REC %(waybill)s %(quantity)s" to %(shortcode)s' % sms_args)
sms_params = {'text': sms_text, 'to': ' '.join(parish_reporters)}
sched_id = queue_schedule(db, sms_params, sched_time, session.sesid)
log_schedule(db, data_id, sched_id, 'parish')
# print "@=======@=======@===>", parish_reporters

# for the driver
driver_sms_text = config['driver_sms_template'] % sms_args
if parish:
driver_sms_text = config['driver_parish_sms_template'] % sms_args
else:
driver_sms_text = config['driver_sms_template'] % sms_args

sms_params = {'text': driver_sms_text, 'to': params.driver_tel}
sched_id = queue_schedule(db, sms_params, sched_time, session.sesid)
log_schedule(db, data_id, sched_id, 'driver')
Expand Down Expand Up @@ -283,7 +327,7 @@ def POST(self):
'groups': ['Driver'],
}

sync_time = current_time + datetime.timedelta(seconds=5) # XXX consider making the seconds configurable
sync_time = current_time + datetime.timedelta(seconds=10) # XXX consider making the seconds configurable
queue_schedule(db, contact_params, sync_time, session.sesid, 'push_contact')

r = db.query(
Expand Down Expand Up @@ -311,12 +355,22 @@ def POST(self):
log_id = r[0]['id']
subcounty = ""
district = ""
parish = ""
# get subcounty name to use in SMS
sc = db.query(
"SELECT get_location_name($subcounty) as name;",
{'subcounty': params.subcounty})
if sc:
subcounty = sc[0]['name']
# get parish name if available
par = db.query(
"SELECT get_location_name($parish) as name;",
{'parish': params.parish})
if par:
parish = par[0]['name']
db.query(
"UPDATE distribution_log SET dest_parish = $parish "
"WHERE id = $id", {'parish': params.parish, 'id': log_id})

# get district name to use in SMS
dist = db.query(
Expand All @@ -330,13 +384,18 @@ def POST(self):

# build SMS to send to notifying parties
sms_args = {
'parish': parish,
'subcounty': subcounty,
'district': district,
'waybill': params.waybill,
'quantity': quantity_bales,
'shortcode': config.get('shortcode', '6400')
}
sms_text = config['national_sms_template'] % sms_args
sms_text = ""
if parish:
sms_text = config['parish_nets_sms_prefix_template'] % sms_args

sms_text += config['national_sms_template'] % sms_args

district_reporters = get_location_role_reporters(db, params.district, config['district_reporters'])
district_reporters += get_location_role_reporters(
Expand All @@ -353,16 +412,31 @@ def POST(self):
# print "*=======*=======*===>", national_reporters

subcounty_reporters = get_location_role_reporters(db, params.subcounty, config['subcounty_reporters'])
sms_text += (
'\n Once received / offloaded, please send '
'"REC %(waybill)s %(quantity)s" to %(shortcode)s' % sms_args)
parish_reporters = get_location_role_reporters(db, params.parish, config['parish_reporters'])

if subcounty_reporters and not parish:
sms_text += (
'\n Once received / offloaded, please send '
'"REC %(waybill)s %(quantity)s" to %(shortcode)s' % sms_args)
sms_params = {'text': sms_text, 'to': ' '.join(subcounty_reporters)}
sched_id = queue_schedule(db, sms_params, sched_time, session.sesid)
log_schedule(db, log_id, sched_id, 'subcounty')
# print "@=======@=======@===>", subcounty_reporters

if parish_reporters:
sms_text += (
'\n Once received / offloaded, please send '
'"REC %(waybill)s %(quantity)s" to %(shortcode)s' % sms_args)
sms_params = {'text': sms_text, 'to': ' '.join(parish_reporters)}
sched_id = queue_schedule(db, sms_params, sched_time, session.sesid)
log_schedule(db, log_id, sched_id, 'parish')
# print "@=======@=======@===>", parish_reporters

# for the driver
driver_sms_text = config['driver_sms_template'] % sms_args
if parish:
driver_sms_text = config['driver_parish_sms_template'] % sms_args
else:
driver_sms_text = config['driver_sms_template'] % sms_args
sms_params = {'text': driver_sms_text, 'to': params.driver_tel}
sched_id = queue_schedule(db, sms_params, sched_time, session.sesid)
log_schedule(db, log_id, sched_id, 'driver')
Expand Down
15 changes: 13 additions & 2 deletions web/app/views/dispatch.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ <h5>Dispatch<small> distribute to subcounties</small></h5>
<div class="row">
<div class="form-group"><label for="district" class="col-lg-3 control-label">District:</label>
<div class="col-lg-9">
<input type="hidden" name="location" id="location" value=""/>
<select name="district" id="district" class="form-control" required>
<option value="">Select District</option>
{% for d in districts %}
Expand All @@ -54,13 +55,23 @@ <h5>Dispatch<small> distribute to subcounties</small></h5>
<div class="form-group"><label for="subcounty" class="col-lg-3 control-label">Subcounty:</label>
<div class="col-lg-9">
<select name="subcounty" id="subcounty" class="form-control" required>
<option value="">Select Sub County</option>
<option value="0">Select Sub County</option>
{% for s in subcounties %}
<option value="{{ s.id }}" {% if s.id == subcounty %} selected="yes" {% endif %}>{{ s.name}}</option>
{% endfor %}
</select>
</div>
</div>
<div class="form-group"><label for="parish" class="col-lg-3 control-label">Parish:</label>
<div class="col-lg-9">
<select name="parish" id="parish" class="form-control">
<option value="0">Select Parish</option>
{% for p in parishes %}
<option value="{{ p.id }}" {% if p.id == parish %} selected="yes" {% endif %}>{{ p.name}}</option>
{% endfor %}
</select>
</div>
</div>

<div class="form-group"><label for="release_order" class="col-lg-3 control-label">Release Order:</label>
<div class="col-lg-9">
Expand Down Expand Up @@ -339,7 +350,7 @@ <h4 class="modal-title">Send SMS</h4>
<script src="/static/js/plugins/datapicker/bootstrap-datepicker.js"></script>

<script src="/static/js/llin/dispatch.js"></script>
<script src="/static/js/llin/dpoints.js"></script>
<!--<script src="/static/js/llin/dpoints.js"></script>-->
<script>
$(document).ready(function() {
$('.dataTables-example').DataTable({
Expand Down
50 changes: 50 additions & 0 deletions web/static/js/llin/dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,54 @@ $(function(){
});
return false;
});

$('#district').change(function(){
var districtid = $(this).val();
if (districtid == '0' || districtid == "")
return;
$('#location').val(districtid);
$('#subcounty').empty();
$('#subcounty').append("<option value='' selected='selected'>Select Sub County</option>");
$.get(
'/api/v1/loc_children/' + districtid,
{xtype:'district', xid: districtid},
function(data){
var subcounties = data;
for(var i in subcounties){
val = subcounties[i]["id"];
txt = subcounties[i]["name"];
$('#subcounty').append(
$(document.createElement("option")).attr("value",val).text(txt)
);
}
},
'json'
);
});

/*When subcounty is changed*/
$('#subcounty').change(function(){
var subcountyid = $(this).val();
if (subcountyid == '0' || subcountyid == '')
return;
$('#location').val(subcountyid);
$('#parish').empty();
$('#parish').append("<option value='' selected='selected'>Select Parish</option>");
$.get(
'/api/v1/loc_children/' + subcountyid,
{},
function(data){
var parishes = data;
for(var i in parishes){
val = parishes[i]['id'];
txt = parishes[i]['name'];
$('#parish').append(
$(document.createElement("option")).attr("value",val).text(txt)
);
}
},
'json'
);
});

});

0 comments on commit e2695ce

Please sign in to comment.