def _add_encounter(self, encounter): # adds data from a visit. doesn't update the couch-saved properties self._encounters.append(encounter) edd = get_edd(encounter) if not self.pregnancy_dates_set() and edd: self.edd = edd if not self.anchor_form_id: self.anchor_form_id = encounter.xform_id fu = get_pregnancy_followup(encounter) if fu.closes_case(): self.closed = True self.closed_on = datetime.combine(encounter.visit_date, time()) self.outcome = fu.get_outcome()
def _is_match(potential_match, encounter): """ Whether a pregnancy matches an encounter. """ # if it's open and the visit is before the end date it's a clear match # this assumes you're always looking at the last pregnancy and a recent form # which is valid as long as people aren't entering crazy backlogged data. if not potential_match.closed and potential_match.get_end_date() > encounter.visit_date: return True # if it's closed, but was only closed in the last two weeks it's a match elif potential_match.closed and \ potential_match.get_end_date() + timedelta(14) > encounter.visit_date: return True elif potential_match.pregnancy_dates_set(): # the dates were set and we didn't match. no match. return False else: # if the dates weren't set, cross check against _our_ dates edd = get_edd(encounter) if edd: end_date = edd + timedelta(days=DAYS_AFTER_EDD_END) start_date = lmp_from_edd(edd) - timedelta(days=DAYS_BEFORE_LMP_START) if start_date < potential_match.get_first_visit_date() < end_date and \ start_date < potential_match.get_last_visit_date() < end_date: return True elif start_date < potential_match.get_first_visit_date() < end_date or \ start_date < potential_match.get_last_visit_date() < end_date: # what to do about this corner case? Half the visits fall in # the other half don't. # just add it for now logging.error("Pregnancy dates out of whack for visit, trying our best " "to deal with it (xform: %s)" % encounter.get_xform().get_id) return True else: # dates clearly not in range return False else: # if the visit date is < 9 months (270 days) from the first known # visit date, count it return potential_match.get_first_visit_date() < encounter.visit_date < potential_match.get_first_visit_date() + timedelta(days=270)