def is_valid_voucher_submission(voucher_case): try: person_case = get_person_case_from_voucher(voucher_case.domain, voucher_case) except ENikshayCaseNotFound: return False return is_valid_person_submission(person_case)
def handle(self, domain, **options): accessor = CaseAccessors(domain) voucher_ids = accessor.get_case_ids_in_domain(CASE_TYPE_VOUCHER) rows = [[ 'voucher_id', 'state', 'comments', 'person_id', 'person_name' ]] for voucher in with_progress_bar(accessor.iter_cases(voucher_ids), len(voucher_ids)): if voucher.get_case_property('state') in ('paid', 'rejected'): person = get_person_case_from_voucher(domain, voucher.case_id) rows.append([ voucher.case_id, voucher.get_case_property('state'), voucher.get_case_property('comments'), person.case_id, "{} {}".format(person.get_case_property(PERSON_FIRST_NAME), person.get_case_property(PERSON_LAST_NAME)), ]) filename = 'voucher_statuses.csv' with open(filename, 'w') as f: writer = csv.writer(f) writer.writerows(rows) print( '{} cases have a status of paid or rejected. Details written to {}' .format(len(rows) - 1, filename))
def person_case_from_voucher_case(handler, reminder): voucher_case = reminder.case if not voucher_case: return None if voucher_case.type != 'voucher': return None try: return get_person_case_from_voucher(voucher_case.domain, voucher_case.case_id) except ENikshayCaseNotFound: return None
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'), )