def getMatchesByWord(self, aFragment): """Return matches for aFragment at start of words inside phrases.""" fragment_condition = "~* %(fragment)s" aFragment = gmPG2.sanitize_pg_regex(expression = aFragment, escape_all = False) self._args['fragment'] = "( %s)|(^%s)" % (aFragment, aFragment) return self._find_matches(fragment_condition)
def getMatchesByWord(self, aFragment): """Return matches for aFragment at start of words inside phrases.""" fragment_condition = "~* %(fragment)s" aFragment = gmPG2.sanitize_pg_regex(expression = aFragment, escape_all = False) self._args['fragment'] = "( %s)|(^%s)" % (aFragment, aFragment) return self._find_matches(fragment_condition)
def generate_invoice_id(template=None, pk_patient=None, person=None, date_format='%Y-%m-%d', time_format='%H%M%S'): """Generate invoice ID string, based on template. No template given -> generate old style fixed format invoice ID. Placeholders: %(pk_pat)s %(date)s %(time)s if included, $counter$ is not *needed* (but still possible) %(firstname)s %(lastname)s %(dob)s #counter# will be replaced by a counter, counting up from 1 until the invoice id is unique, max 999999 """ assert (None in [ pk_patient, person ]), u'either of <pk_patient> or <person> can be defined, but not both' if (template is None) or (template.strip() == u''): template = DEFAULT_INVOICE_ID_TEMPLATE date_format = '%Y-%m-%d' time_format = '%H%M%S' template = template.strip() _log.debug('invoice ID template: %s', template) if pk_patient is None: if person is not None: pk_patient = person.ID now = gmDateTime.pydt_now_here() data = {} data['pk_pat'] = gmTools.coalesce(pk_patient, '?') data['date'] = gmDateTime.pydt_strftime(now, date_format).strip() data['time'] = gmDateTime.pydt_strftime(now, time_format).strip() if person is None: data['firstname'] = u'?' data['lastname'] = u'?' data['dob'] = u'?' else: data['firstname'] = person['firstnames'].replace( ' ', gmTools.u_space_as_open_box).strip() data['lastname'] = person['lastnames'].replace( ' ', gmTools.u_space_as_open_box).strip() data['dob'] = person.get_formatted_dob(format=date_format, none_string=u'?', honor_estimation=False).strip() candidate_invoice_id = template % data if u'#counter#' not in candidate_invoice_id: if u'%(time)s' in template: return candidate_invoice_id candidate_invoice_id = candidate_invoice_id + u' [##counter#]' _log.debug('invoice id candidate: %s', candidate_invoice_id) # get existing invoice IDs consistent with candidate search_term = u'^\s*%s\s*$' % gmPG2.sanitize_pg_regex( expression=candidate_invoice_id).replace(u'#counter#', '\d+') cmd = u'SELECT invoice_id FROM bill.bill WHERE invoice_id ~* %(search_term)s UNION ALL SELECT invoice_id FROM audit.log_bill WHERE invoice_id ~* %(search_term)s' args = {'search_term': search_term} rows, idx = gmPG2.run_ro_queries(queries=[{'cmd': cmd, 'args': args}]) if len(rows) == 0: return candidate_invoice_id.replace(u'#counter#', u'1') existing_invoice_ids = [r['invoice_id'].strip() for r in rows] counter = None counter_max = 999999 for idx in range(1, counter_max): candidate = candidate_invoice_id.replace(u'#counter#', '%s' % idx) if candidate not in existing_invoice_ids: counter = idx break if counter is None: # exhausted the range, unlikely (1 million bills are possible # even w/o any other invoice ID data) but technically possible _log.debug( 'exhausted uniqueness space of [%s] invoice IDs per template', counter_max) counter = '>%s[%s]' % (counter_max, data['time']) return candidate_invoice_id.replace(u'#counter#', '%s' % counter)