def prescription_total_days_threshold_in_trigger_state(episode_case_properties, n, check_already_sent=True): threshold_case_prop = BETSDrugRefillRepeater._get_threshold_case_prop(n) if check_already_sent: return bool( string_to_date_or_None(episode_case_properties.get(threshold_case_prop)) and episode_case_properties.get("event_{}_{}".format(DRUG_REFILL_EVENT, n)) != "sent" ) else: return string_to_date_or_None(episode_case_properties.get(threshold_case_prop)) is not None
def create_ayush_referral_payload(cls, episode_case): episode_case_properties = episode_case.dynamic_case_properties() person_case = get_person_case_from_episode(episode_case.domain, episode_case.case_id) location = cls._get_location( episode_case_properties.get("registered_by"), field_name="registered_by", related_case_type="episode", related_case_id=episode_case.case_id, ) if not location.user_id: raise NikshayLocationNotFound( "Location {} does not have a virtual location user".format( location.location_id)) return cls( EventID=AYUSH_REFERRAL_EVENT, EventOccurDate=string_to_date_or_None( episode_case.get_case_property( FIRST_PRESCRIPTION_VOUCHER_REDEEMED_DATE)), BeneficiaryUUID=location.user_id, BeneficiaryType='ayush_other', EpisodeID=episode_case.case_id, Location=episode_case_properties.get("registered_by"), DTOLocation=_get_district_location_id(location), PersonId=person_case.get_case_property('person_id'), AgencyId=cls._get_agency_id( episode_case), # not migrated from UATBC, so we're good # Incentives are not yet approved in eNikshay EnikshayApprover=None, EnikshayRole=None, EnikshayApprovalDate=None, )
def create_diagnosis_and_notification_payload(cls, episode_case): person_case = get_person_case_from_episode(episode_case.domain, episode_case.case_id) location = cls._get_location( person_case.owner_id, field_name="owner_id", related_case_type="person", related_case_id=person_case.case_id, ) return cls( EventID=DIAGNOSIS_AND_NOTIFICATION_EVENT, EventOccurDate=string_to_date_or_None( episode_case.get_case_property( FIRST_PRESCRIPTION_VOUCHER_REDEEMED_DATE)), BeneficiaryUUID=episode_case.dynamic_case_properties().get( NOTIFYING_PROVIDER_USER_ID), BeneficiaryType=LOCATION_TYPE_MAP[location.location_type.code], EpisodeID=episode_case.case_id, Location=person_case.owner_id, DTOLocation=_get_district_location_id(location), PersonId=person_case.get_case_property('person_id'), AgencyId=cls._get_agency_id( episode_case), # not migrated from UATBC, so we're good # Incentives are not yet approved in eNikshay EnikshayApprover=None, EnikshayRole=None, EnikshayApprovalDate=None, )
def _get_successful_treatment_date(episode_case): completed_date = None if episode_case.get_case_property(TREATMENT_OUTCOME) in ( "cured", "treatment_completed"): completed_date = episode_case.get_case_property( TREATMENT_OUTCOME_DATE) if not completed_date: # the treatment_outcome_date property used to be called # "rx_outcome_date", and was changed at some point. Older cases # still have the rx_outcome_date property set. completed_date = episode_case.get_case_property( 'rx_outcome_date') threshold_met_date = episode_case.get_case_property( BETS_DATE_PRESCRIPTION_THRESHOLD_MET) if completed_date is None and threshold_met_date is None: raise AssertionError( "No treatment completion date found for episode {}. " "How was this triggered?".format(episode_case.case_id)) # We don't know whether the trigger fired because the threshold was met # or because treatment ended. Just use whichever happened first. return string_to_date_or_None( min([_f for _f in [completed_date, threshold_met_date] if _f]))
def create_drug_refill_payload(cls, episode_case, n): episode_case_properties = episode_case.dynamic_case_properties() person_case = get_person_case_from_episode(episode_case.domain, episode_case.case_id) event_date = string_to_date_or_None( episode_case_properties.get( PRESCRIPTION_TOTAL_DAYS_THRESHOLD.format(n))) pcp_location = cls._get_location( person_case.owner_id, field_name="owner_id", related_case_type="person", related_case_id=person_case.case_id, ) return cls( EventID=DRUG_REFILL_EVENT, EventOccurDate=event_date, BeneficiaryUUID=person_case.case_id, BeneficiaryType="patient", EpisodeID=episode_case.case_id, Location=person_case.owner_id, DTOLocation=_get_district_location_id(pcp_location), PersonId=person_case.get_case_property('person_id'), AgencyId=cls._get_agency_id( episode_case), # we don't have this for migrated cases # Incentives are not yet approved in eNikshay EnikshayApprover=None, EnikshayRole=None, EnikshayApprovalDate=None, )
def create_180_treatment_payload(cls, episode_case): episode_case_properties = episode_case.dynamic_case_properties() person_case = get_person_case_from_episode(episode_case.domain, episode_case.case_id) pcp_location = cls._get_location(person_case.owner_id, field_name="owner_id", related_case_type="person", related_case_id=person_case.case_id) treatment_outcome_date = string_to_date_or_None( episode_case_properties.get(TREATMENT_OUTCOME_DATE)) if treatment_outcome_date is None: treatment_outcome_date = datetime.utcnow().date() return cls( EventID=TREATMENT_180_EVENT, EventOccurDate=treatment_outcome_date, BeneficiaryUUID=episode_case_properties.get( LAST_VOUCHER_CREATED_BY_ID), BeneficiaryType="mbbs", EpisodeID=episode_case.case_id, Location=person_case.owner_id, DTOLocation=_get_district_location_id(pcp_location), PersonId=person_case.get_case_property('person_id'), AgencyId=cls._get_agency_id( episode_case), # not migrated from UATBC, so we're good # Incentives are not yet approved in eNikshay EnikshayApprover=None, EnikshayRole=None, EnikshayApprovalDate=None, )
def allowed_to_forward(self, episode_case): if not self.case_types_and_users_allowed(episode_case): return False episode_case_properties = episode_case.dynamic_case_properties() trigger_by_threshold = {} # threshold -> boolean threshold_prop_values_by_threshold = {} # threshold -> date for n in TOTAL_DAY_THRESHOLDS: threshold_case_prop = self._get_threshold_case_prop(n) threshold_prop_values_by_threshold[n] = string_to_date_or_None( episode_case_properties.get(threshold_case_prop)) trigger_for_n = bool( self.prescription_total_days_threshold_in_trigger_state( episode_case_properties, n, check_already_sent=True) and case_properties_changed(episode_case, [threshold_case_prop])) trigger_by_threshold[n] = trigger_for_n trigger_dates_unique = self._list_items_unique( filter(None, threshold_prop_values_by_threshold.values())) if not trigger_dates_unique: self._flag_program_team() return (trigger_dates_unique and any(trigger_by_threshold.values()) and is_valid_episode_submission(episode_case))
def create_voucher_payload(cls, voucher_case): voucher_case_properties = voucher_case.dynamic_case_properties() fulfilled_by_id = voucher_case_properties.get(FULFILLED_BY_ID) fulfilled_by_location_id = voucher_case_properties.get( FULFILLED_BY_LOCATION_ID) event_id = { "prescription": CHEMIST_VOUCHER_EVENT, "test": LAB_VOUCHER_EVENT, }[voucher_case_properties['voucher_type']] location = cls._get_location(fulfilled_by_location_id, field_name=FULFILLED_BY_LOCATION_ID, related_case_type="voucher", related_case_id=voucher_case.case_id) person_case = get_person_case_from_voucher(voucher_case.domain, voucher_case.case_id) agency_user = CommCareUser.get_by_user_id( voucher_case.get_case_property('voucher_fulfilled_by_id')) approver_id = voucher_case.get_case_property('voucher_approved_by_id') if not approver_id: raise AssertionError("Voucher does not have an approver") approver = CommCareUser.get_by_user_id(approver_id) approver_name = approver.name usertype = approver.user_data.get('usertype') approver_usertype = USERTYPE_DISPLAYS.get(usertype, usertype) amount = voucher_case_properties.get(AMOUNT_APPROVED, "") if amount == "": amount = voucher_case_properties.get(AMOUNT_FULFILLED) return cls( EventID=event_id, EventOccurDate=string_to_date_or_None( voucher_case_properties.get(DATE_FULFILLED)), VoucherID=voucher_case.case_id, ReadableVoucherID=voucher_case.get_case_property(VOUCHER_ID), BeneficiaryUUID=fulfilled_by_id, BeneficiaryType=LOCATION_TYPE_MAP[location.location_type.code], Location=fulfilled_by_location_id, # always round to nearest whole number, but send a string... Amount=str(int(round(float(amount)))), DTOLocation=_get_district_location_id(location), InvestigationType=voucher_case_properties.get(INVESTIGATION_TYPE), PersonId=person_case.get_case_property('person_id'), AgencyId=agency_user.raw_username, EnikshayApprover=approver_name, EnikshayRole=approver_usertype, EnikshayApprovalDate=voucher_case.get_case_property( 'date_approved'), )