Example #1
0
File: case.py Project: dimagi/bhoma
def get_healthy_pregnancy_case(pregnancy, encounter):
    # Any pregnancy case that is created that hasn't been closed by a delivery 
    # gets a followup.  The followups become active at week 42 and expires in
    # week 46 of the pregnancy
    # The CHW should track the delivery and give an outcome to the pregnancy.
    
    lmp = pregnancy.lmp
    # TODO: is this check/logic necessary?
    if lmp:
        send_to_phone = True
        reason = "pregnancy_expecting_outcome"
    else:
        send_to_phone = False
        reason = "unknown_lmp"
    
    ltfu_date = lmp + timedelta(days=46*7) if lmp else None
    bhoma_case = PatientCase(_id=get_bhoma_case_id_from_pregnancy(pregnancy), 
                             opened_on=datetime.combine(encounter.visit_date, time()),
                             modified_on=datetime.utcnow(),
                             type=const.CASE_TYPE_PREGNANCY,
                             encounter_id=encounter.get_id,
                             patient_id=get_patient_id_from_form(encounter.get_xform()),
                             ltfu_date=ltfu_date,
                             outcome=pregnancy.outcome,
                             closed=pregnancy.closed,
                             closed_on=pregnancy.closed_on,
                             send_to_phone=send_to_phone,
                             send_to_phone_reason=reason)
    bhoma_case.status = "pending outcome"
    
    if send_to_phone and not bhoma_case.closed:
        cccase = get_first_commcare_case(encounter, bhoma_case=bhoma_case, 
                                     case_id=get_commcare_case_id_from_block(encounter,bhoma_case))
        cccase.followup_type = const.PHONE_FOLLOWUP_TYPE_PREGNANCY
    
        # starts and becomes active the same day, 42 weeks from LMP
        bhoma_case.lmp = lmp
        cccase.start_date = lmp + timedelta(days= 7 * 42)
        cccase.missed_appointment_date = cccase.start_date
        cccase.activation_date = cccase.start_date
        cccase.due_date = cccase.activation_date + timedelta(days=DAYS_AFTER_PREGNANCY_ACTIVE_DUE)
        bhoma_case.commcare_cases = [cccase]
    return bhoma_case
Example #2
0
def process_followup(patient, new_encounter):
    form = new_encounter.get_xform()
    assert form.namespace == config.CHW_FOLLOWUP_NAMESPACE
    caseblocks = extract_case_blocks(form)
    for caseblock in caseblocks:
        case_id = caseblock[const.CASE_TAG_ID]
        # find bhoma case 
        try:
            results = get_db().view("case/bhoma_case_lookup", key=case_id, reduce=False).one()
        except MultipleResultsFound:
            logging.error("Found duplicate cases in a patient: patient_id: %s, case_id: %s" % (patient.get_id, case_id))
            results = None
        if results:
            raw_data = results["value"]
            bhoma_case = PatientCase.wrap(raw_data)
            for case in bhoma_case.commcare_cases:
                if case.case_id == case_id:
                    # apply generic commcare update to the case
                    case.update_from_block(caseblock, new_encounter.visit_date)
                    
                    # apply custom updates to bhoma case
                    bhoma_case_close_value = case.all_properties().get(const.CASE_TAG_BHOMA_CLOSE, None)
                    bhoma_case_outcome_value = case.all_properties().get(const.CASE_TAG_BHOMA_OUTCOME, "")
                            
                    if bhoma_case_close_value and int(bhoma_case_close_value):
                        # bhoma case should be closed
                        if bhoma_case.closed:
                            logging.warn("Tried to close case %s from phone but it was already closed! Ignoring." % bhoma_case.get_id) 
                        else:
                            bhoma_case.closed = True
                            bhoma_case.outcome = bhoma_case_outcome_value
                            bhoma_case.closed_on = datetime.combine(new_encounter.visit_date, time())
                    else:
                        # we didn't close the bhoma case, check if we need to 
                        # create any new commcare cases

                        # referred back: create an appointment
                        if bhoma_case_outcome_value == const.Outcome.REFERRED_BACK_TO_CLINIC:
                            appt_date_string = form.xpath("met/followup/refer_when")
                            if appt_date_string:
                                new_case = new_commcare_case(case_id=get_commcare_case_id_from_block(new_encounter, bhoma_case),
                                                             name=get_commcare_case_name(new_encounter, bhoma_case), 
                                                             type=bhoma_case.type, 
                                                             opened_on=datetime.combine(new_encounter.visit_date, time()), 
                                                             modified_on=datetime.utcnow(),
                                                             user_id=get_user_id(new_encounter), 
                                                             encounter_id=new_encounter.get_id, 
                                                             bhoma_case_id=bhoma_case.get_id)
                                new_case.followup_type = const.PHONE_FOLLOWUP_TYPE_MISSED_APPT
                                appt_date = string_to_datetime(appt_date_string).date()
                                add_missed_appt_dates(new_case, appt_date)
                                bhoma_case.status = const.Status.RETURN_TO_CLINIC
                                bhoma_case.commcare_cases.append(new_case)
                        elif bhoma_case_outcome_value == const.Outcome.ACTUALLY_WENT_TO_CLINIC:
                            bhoma_case.status = const.STATUS_WENT_BACK_TO_CLINIC
                        elif bhoma_case_outcome_value == const.Outcome.PENDING_PATIENT_MEETING:
                            bhoma_case.status = const.STATUS_PENDING_CHW_MEETING

            if is_delivery(bhoma_case):
                custom_process_delivery_case(bhoma_case, new_encounter)

            patient.update_cases([bhoma_case])
        else:
            logging.error(("No case in patient %s with id %s found.  "
                           "If you are not debugging then this is a weird error.") % (patient.get_id, case_id))