/
claim_sync.py
executable file
·413 lines (352 loc) · 14.8 KB
/
claim_sync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/env python
'''claim_sync -- sync claims between hh-office and FreeClaims
TODO:
1. 'make claims' redirects to this python app with a list of visit ids
2. Response is a confirmation page that
a. enumerates the visits (perhaps date, client name, dx, cpt, price)
b. includes the CSV data in a textarea
c. offers to Submit the CSV data to freeclaims
3. Response has either
a. a link to the new batch on FreeClaims and a link back to Xata,
with the claim_uid and bill_date of the relevant Visits updated, or
b. a "try again shortly" link (perhaps it tries again automatically)
'''
import time
import logging
from collections import namedtuple
import datetime
from xml.sax import saxutils
import StringIO
import csv
from mechanize._beautifulsoup import BeautifulSoup as HTML
import mechanize
import MySQLdb
import paste.request
from export_claims import format_claims
from hhtcb import WSGI, Xataface
import ocap
log = logging.getLogger(__name__)
def cgi_main(here, mkCGIHandler, format_exc):
xf = Xataface.make(here)
sync = SyncApp(xf)
syncE = WSGI.error_middleware(format_exc, sync)
mkCGIHandler().run(syncE)
def test_main(argv):
import sys
import pprint
logging.basicConfig(level=logging.DEBUG, stream=sys.stderr)
ua = FreeClaimsUA()
while argv[1:]:
action = argv[1]
if action == '--login':
username, password = argv[2:4]
del argv[1:4]
ua.login(username, password)
elif action == '--upload':
claim_fn = argv[2]
del argv[1:3]
ua.upload(claim_fn)
elif action == '--batches':
del argv[1]
pprint.pprint(ua.batches())
elif action == '--batches-local':
doc_fn = argv[2]
del argv[1:3]
pprint.pprint(ua._batches(open(doc_fn).read()))
elif action == '--claims':
del argv[1]
batches_flatten(sys.stdout,
[(batch, ua.claims(batch))
for batch in ua.batches()])
else:
raise ValueError('huh? ' + action)
class SyncApp(object):
def __init__(self, xf,
title='Hope Harbor FreeClaims Helper'):
self._xf = xf
self._title = title
def __call__(self, env, start_response):
'''Check authorization, login to the DB, and dispatch to show/upload.
'''
try:
host, user, password, name = self._xf.webapp_login(env)
except KeyError:
start_response('400 bad request', WSGI.PLAIN)
return ['missing key parameter ']
except OSError:
start_response('401 unauthorized', WSGI.PLAIN)
return ['report key does not match.']
# scrub user input
params = paste.request.parse_formvars(env)
visit_ids = [int(visit_id)
for visit_id in params.get('visits').split(',')]
log.info('requested visit_ids: %s', visit_ids)
log.info('MySQLdb.connect(db=%s) ...', name)
conn = MySQLdb.connect(host=host, user=user, passwd=password, db=name)
content, claims = format_claims(conn, visit_ids)
log.info('found %s claims', len(claims))
if env['REQUEST_METHOD'] == 'POST':
u, p = params.get('user'), params.get('password')
batches, results = self.upload(u, p, content, claims)
update_visits(conn, claims, results)
start_response('200 ok', WSGI.HTML8)
return format_upload_results(self._title, batches[0], results)
else:
start_response('200 ok', WSGI.HTML8)
base = paste.request.construct_url(env, with_query_string=False)
key = params.get('key')
return self.show(base, key, visit_ids, content, claims)
def show(self, base, key, visit_ids, content, claims,
default_user='4482'):
r'''
>>> a = SyncApp(xf=None)
>>> body = a.show(base='b:', key='k1', visit_ids=[1,2], content=['@@con'], claims=[])
>>> len(body)
21
>>> body[3]
'<h1>Insurance Claim Batch</h1>\n'
'''
return doc_with(
self._title,
['<h1>Insurance Claim Batch</h1>\n',
'<form method="POST" action="?key=%s&visits=%s">\n' % (
key, ','.join(map(str, visit_ids))),
'<ol>\n'] +
[piece for claim in claims
for piece in
([' <li>%s<br />%s $%s<br />DX: %s %s <ol>\n' % (
saxutils.escape(claim['detail'][
'Insurance Company Name']),
saxutils.escape(claim['detail']['2-PatientName']),
claim['detail']['28-TotalCharge'],
claim['detail']['21.1-Diagnosis'],
claim['detail']['21.2-Diagnosis'] or '')] +
[' <li>on %s CPT: %s $%s</li>\n' % (
item['24.1.a-DOSFrom'],
item['24.1.d-CPT'],
item['24.1.f-Charges'])
for item in claim['items']] +
['</ol>\n', '</li>\n'])] +
['</ol>\n',
'<p><em>Submitting this form will upload a "file"',
' to FreeClaims and wait for acknowledgement.',
' It may take a few minutes.</em></p>',
'<label>FreeClaims file data:'
'<textarea name="claim_data">\n'] +
[saxutils.escape(piece) for piece in content] +
['</textarea></label>\n',
'<p>FreeClaims user id: ',
'<input name="user" value="%s"/></p>' % default_user,
'<p>FreeClaims password: ',
'<input name="password" type="password" /></p>',
'<input type="submit" value="Upload" /></p>',
'</form>\n'])
def upload(self, u, p, content, claims, fn='claim_sync.csv'):
ua = FreeClaimsUA()
log.info('FreeClaimsUA.login(u=%s) ...', u)
ua.login(u, p)
txt = ''.join(content)
data = StringIO.StringIO(txt)
log.info('uploading %d bytes', len(txt))
batches = ua.upload_batch_data(data, fn)
log.info('found batch: %s; getting claims', batches[0].batch_no)
results = ua.claims(batches[0])
log.info('found %d claims', len(results))
return batches, results
def update_visits(conn, claims, results):
for cin, cout in zip(claims, results):
visit_ids = cin['visit_ids']
sql = ("""update Visit v
set claim_uid = concat(%%s, ',', %%s)
where v.id in (%s)""" % ', '.join(map(str, visit_ids)))
log.info('Updating (%s) to trace %s, batch %s\nSQL:%s',
visit_ids, cout.trace_no, cout.batch, sql)
work = conn.cursor()
work.execute(sql, (cout.trace_no, cout.batch))
conn.commit()
def doc_with(title, body):
return (['<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">\n',
'<head><title>%s</title></head>\n' % title,
'<body>\n'] + body +
['</body>\n',
'</html>\n'])
def format_upload_results(title, batch, results):
return doc_with(
title,
['<table border="1">\n',
'<tr>\n',
'<th>Trace</th><th>Batch</th><th>Status</th>',
'<th>Last</th><th>First</th><th>Acct</th>',
'<th>Service Date</th><th>Date Received</th>',
'</tr>\n'] +
[piece for cout in results for piece in
['<tr>',
'<td><a href="%s%s">%s</a></td>' % (
FreeClaimsUA.base, cout.href, cout.trace_no),
'<td><a href="%s%s">%s</a></td>' % (
FreeClaimsUA.base, batch.href, batch.batch_no),
'<td>%s</td>' % cout.status,
'<td>%s</td>' % cout.last,
'<td>%s</td>' % cout.first,
'<td>%s</td>' % cout.acc_no,
'<td>%s</td>' % cout.service_date,
'<td>%s</td>' % cout.date_received,
'</tr>'
]] +
['</table>',
'<p><strong>To update billing dates, return to Hope Harbor',
' Attendance using the back button'
' and choose the Update action button.',
'</strong></p>'])
class FreeClaimsUA(mechanize.Browser):
base = 'https://sfreeclaims.anvicare.com/docs/'
def login(self, username, password, pg='member_login.asp'):
log.debug('login: open(%s)', self.base + pg)
self.open(self.base + pg)
self.select_form(name="loginForm")
self['username'] = username
self['userpassword'] = password
log.debug('login: submit creds.')
self.submit()
def batches(self, pg='ViewBatch.asp'):
log.debug('batches: open(%s).', pg)
return self._batches(self.open(pg).get_data())
def _batches(self, txt):
doc = HTML(txt)
doc.done()
t = doc.html.first('table', {'align': 'Center'})
return [batch(row('td')[1])
# skip heading row
for row in t('tr')[1:]]
def claims(self, batch):
log.debug('claims: open (%s)', batch.href)
doc = HTML(self.open(batch.href).get_data())
doc.done()
t = doc.html.first('table', {'align': 'Center'})
return [claim(row) for row in t('tr')[1:]]
def upload_file(self, claim_fn):
self.upload_data(open(claim_fn), claim_fn)
def upload_data(self, claim_fp, claim_fn, pg='upload.asp', form='Upload'):
log.debug('upload: open(%s).', pg)
r3 = self.open(pg)
scrub_nested_form(self, r3)
log.debug('upload: select_form(%s).', form)
self.select_form(form)
log.debug('upload: add_file(%s).', claim_fn)
self.form.add_file(claim_fp, 'text/plain', claim_fn)
log.debug('upload: submit claims.')
r4 = self.submit()
# @@TODO: parse server-assigned name
return r4.get_data()
def upload_batch(self, claim_fn):
self.upload_batch_data(open(claim_fn), claim_fn)
def upload_batch_data(self, claim_fp, claim_fn):
'''Upload a file of claims and wait for the new batch number.
@return: list of batches for this account, starting with the new one.
We assume noone else is uploading concurrently.
'''
before = self.batches()
self.upload_data(claim_fp, claim_fn)
while 1:
log.debug('wait: sleep 2')
time.sleep(2)
b = self.batches()
if b[0].batch_no > before[0].batch_no:
return b
Batch = namedtuple('Batch', 'batch_no href')
def batch(cell):
'''
>>> batch(HTML('<td nowrap="nowrap"> '
... '<a href="ViewClaims.asp?batch_no=2027161">2027161</a>'
... '</td>'))
Batch(batch_no=2027161, href='ViewClaims.asp?batch_no=2027161')
'''
return Batch(int(cell.a.contents[0]), cell.a['href'])
Claim = namedtuple('Claim',
'trace_no href batch status '
'last first acc_no service_date date_received')
def batches_flatten(out, batches_claims):
co = csv.writer(out)
co.writerow('''batch_no trace_no status
last first acc_no client_id
service_date date_received'''.split())
co.writerows([
(batch.batch_no,
claim.trace_no, claim.status,
claim.last, claim.first,
claim.acc_no, claim.acc_no.split('.')[-1],
claim.service_date, claim.date_received)
for batch, claims in batches_claims
for claim in claims])
def claim(row):
'''
>>> claim(HTML(_CLAIM_MARKUP))
... # doctest: +NORMALIZE_WHITESPACE
Claim(trace_no=33298999, href='viewonehcfa.asp?trace_no=33298999',
batch=2027161, status='ACCEPTED', last='SMITH', first='STEVEN',
acc_no='JAM.STE.10999',
service_date=datetime.datetime(2011, 11, 10, 0, 0),
date_received=datetime.datetime(2011, 12, 2, 0, 0))
'''
txts = [cell.contents[0].replace(' ', '') for cell in row('td')]
trace_cell = row('td')[1]
return Claim(int(trace_cell.a.contents[0]), trace_cell.a['href'],
int(txts[2]), txts[3], txts[4], txts[5], txts[6],
datetime.datetime.strptime(txts[9], '%m/%d/%Y'),
datetime.datetime.strptime(txts[15], '%m/%d/%Y'))
_CLAIM_MARKUP = '''
<tr bgcolor=ddf7f7 >
<td class="g" align="right">1</td>
<td class="g"><a href='viewonehcfa.asp?trace_no=33298999'
target='_new'>33298999</a></td>
<td class="g">2027161</td>
<td class="g">ACCEPTED</td>
<td class="g"> SMITH</td>
<td class="g"> STEVEN</td>
<td class="g"> JAM.STE.10999</td>
<td class="g"> 60999</td>
<td class="g"> MDONL</td>
<td class="g"> 11/10/2011</td>
<td class="g" align="right"> 120.00</td>
<td class="g"> </td>
<td class="g"> </td>
<td class="g"> JOHNSON</td>
<td class="g"> WILLIAM</td>
<td class="g" align="right"> 12/2/2011</td>
</tr>
'''
def scrub_nested_form(ua, response):
# Is it possible to hook up a more robust HTML parser to Python mechanize?
# http://stackoverflow.com/questions/1782368/
# submitting-nested-form-with-python-mechanize
# http://stackoverflow.com/questions/7135964/
tweak = ('''<form method="POST" action="FRCLsaveClaim" '''
'''enctype="multipart/form-data" name="uploadClaimForm">''')
log.debug('tweak code')
txt = response.get_data()
if tweak in txt:
log.debug('tweaking!')
response.set_data(txt.replace(tweak, '').replace(
'</form> </center>', '</center>'))
ua.set_response(response)
if __name__ == '__main__':
def _configure_logging():
logging.basicConfig(level=logging.INFO, filename='claim_sync.log',
format='%(asctime)s %(levelname)s: %(message)s')
log.debug('logging configured')
def _bootstrap():
from os import environ
if 'SCRIPT_NAME' in environ:
from os import path as os_path
from wsgiref.handlers import CGIHandler
from traceback import format_exc
here = ocap.Rd(os_path.dirname(__file__), os_path,
open_rd=lambda n: open(n))
cgi_main(here,
mkCGIHandler=lambda: CGIHandler(),
format_exc=format_exc)
else:
from sys import argv
test_main(argv)
_configure_logging()
_bootstrap()