# -*- coding: utf-8 -*-

# import sys
# reload(sys)    
# sys.setdefaultencoding('utf-8')  

import calendar
import datetime
<<<<<<< HEAD
=======
import os
>>>>>>> 3b236beb1aca71c7ab0879d1ab01bf96f280d5ea
import re
import shelve
import time
import urllib
import urlparse

# import pyvirtualdisplay
# pyvirtualdisplay.Display().start()

<<<<<<< HEAD
from xvfbwrapper import Xvfb
vdisplay = Xvfb()
vdisplay.start()
=======
# from xvfbwrapper import Xvfb
# vdisplay = Xvfb()
# vdisplay.start()


myuid = os.getresuid()

import psutil

pXvfb = None

for p in psutil.process_iter():
#     print p, p.name(), p.pid, myuid
    if p.name() == "Xvfb" and p.uids() == myuid:
#         print p, p.cmdline()
        pXvfb = p
        
if pXvfb:
#     print pXvfb
    for cmd in pXvfb.cmdline():
#         print cmd
        if cmd[0]==':':
            os.environ['DISPLAY'] = cmd
            break 
else:
    import pyvirtualdisplay
    pyvirtualdisplay.Display().start()

# print os.environ['DISPLAY']
>>>>>>> 3b236beb1aca71c7ab0879d1ab01bf96f280d5ea

import PyQt4
import pyquery
import spynner


SESSION = None
SESSION_TIMESTAMP = datetime.datetime.now()

SHELVE_FILE = '/tmp/session.shelve'

from myutil import *

def jprint(a):
    import json    
    print json.dumps(a, ensure_ascii = False, indent=4, sort_keys=True)

def Browser():
    browser = spynner.Browser()
#     browser.debug_level=spynner.DEBUG
    return browser
    

def Login():
    global SESSION, SESSION_TIMESTAMP
    
    d = shelve.open(SHELVE_FILE)
    try:
        SESSION = d['SESSION']
        SESSION_TIMESTAMP = d['SESSION_TIMESTAMP']
    except:
        pass
    d.close()

    if SESSION and datetime.datetime.now() < SESSION_TIMESTAMP + datetime.timedelta(minutes = 10):
        SESSION_TIMESTAMP = datetime.datetime.now()
        d = shelve.open(SHELVE_FILE)
        d['SESSION_TIMESTAMP'] = datetime.datetime.now()
        d.close()
#         print 'Use old SESSION: ', SESSION
        return SESSION

    browser = Browser()

# timeout when loading images

    try:
        browser.load("http://portal.ntuh.gov.tw/General/Login.aspx")
    except:
        pass
    
    browser.wk_fill("#txtUserID", "004552")
    browser.wk_fill("#txtPass", "n122119493")
    browser.click("#rdblQuickMenu_0")
    browser.click("#imgBtnSubmitNew", wait_load=True, timeout=9)
    html = browser.html
    browser.close()
    
    pattern = "SESSION=(\w*)"
    matches = re.findall(pattern, html)
    SESSION = matches[0]
    
    d = shelve.open(SHELVE_FILE)
    d['SESSION'] = SESSION
    d['SESSION_TIMESTAMP'] = datetime.datetime.now()
    d.close()
    
    print 'Use new SESSION: ', SESSION
    return SESSION


def SimpleInfoShowUsingPlaceHolder(form):
    
#     print form

    url = form.attr.action
    data = {}
    headers = { "Content-Type": "application/x-www-form-urlencoded" }
    
    req = PyQt4.QtNetwork.QNetworkRequest(PyQt4.QtCore.QUrl(url))
    for k, v in headers.items():
        req.setRawHeader(k, v)
    
    for input in form.children('input'):
        input = pyquery.PyQuery(input)
        n = input.attr.name
        v = input.attr.value.encode('utf8')
#         print n, v
        data[n] =v
        
    browser = Browser()
    browser.webframe.load(req, PyQt4.QtNetwork.QNetworkAccessManager.PostOperation, urllib.urlencode(data))
    browser._wait_load()
    data['html'] = unicode(browser.webframe.toHtml())
    browser.close()

    data['url'] = url

    KeyCodeList = data['KeyCodeList'].split('|')
    KeyNameList = data['KeyNameList'].split('|')
    
    for i in range(len(KeyCodeList)):
        if KeyCodeList[i] == data['AccountIDSE']:
            data['KeyCode'] = KeyCodeList[i]
            data['KeyName'] = KeyNameList[i]
            
#     jprint(data)
#     exit()

    return data
    

def PatientMedicalRecordListQuery(Chart, AfterDate = None, SESSION = None):
# def PatientMedicalRecordListQuery(q, Chart, SESSION = None):
    if not SESSION:
          SESSION = Login()
          
    Chart = str(Chart)
    while len(Chart) < 7:
        Chart = '0'+Chart
          
    '''
    病患就診紀錄
    Show hospital visit
    
    AfterDate: Also fetch medical record after Date
    '''
    
    url = "http://ihisaw.ntuh.gov.tw/WebApplication/OtherIndependentProj/PatientBasicInfoEdit/PatientMedicalRecordListQuery.aspx?QueryBySelf=N&SESSION=%s" % SESSION
    
    browser = Browser()
    browser.load(url)
    browser.wk_fill('#NTUHWeb1_PatientBasicInfoQueryByIDAndName1 > input:nth-child(4)', Chart)
#     browser.wk_click('#NTUHWeb1_PatientBasicInfoQueryByIDAndName1 > input[type="submit"]:nth-child(9)', wait_load=True, timeout=9)
#     browser.wk_click('#NTUHWeb1_PatientBasicInfoQueryByIDAndName1 > input[type="submit"]:nth-child(9)', wait_load=True)
    browser.wk_click('#NTUHWeb1_PatientBasicInfoQueryByIDAndName1 > input[type="submit"]:nth-child(13)', wait_load=True)
    body = unicode(browser.webframe.toHtml())
    
#     print type(body)

    pqhtml = pyquery.PyQuery(body)
    
    result = {}

#已死亡?

    pattern = r'<span id="NTUHWeb1_PatAccountListRecord1_PatBasicDescription" .*?>(?P<name>.*?)\((?P<gender>.*?),(?P<birthday>.*?),(?P<age>.*?)\) (?P<extra> .*)? \((?P<branch>.*?)\)</span>'
    r = re.compile(pattern)
    d = [m.groupdict() for m in r.finditer(body)]
    if d:
        d[0]['dead'] = None
        if d[0]['extra']:
            matches = re.findall(ur'(..../../..) 已死亡', d[0]['extra'])
            if matches:
                d[0]['dead'] = matches[0]
        result.update(d[0])
    else:
        result['dead'] = None

#     if 'birthday' in result:
#         result['birthday'] = datetime.datetime.strptime(result['birthday'], "%Y/%m/%d")

    pattern = r'id="NTUHWeb1_PatAccountListRecord1_personidHidden" value="(?P<PersonID>.*?)"'
    r = re.compile(pattern)
    d = [m.groupdict() for m in r.finditer(body)]
    if d:
        result['PersonID'] = d[0]['PersonID']


# 住
    InPatRecord = pqhtml('#NTUHWeb1_PatAccountListRecord1_GridViewInPatRecord')
    InPat = []
    for tr in pyquery.PyQuery(InPatRecord)('tr'):
        r = {}
        pqtr = pyquery.PyQuery(tr)
        pqspan = pqtr('span')
        if not len(pqspan):
            continue
        for span in pqspan:
            key = span.attrib['id'].split('_')[-1][7:]
            r[key] = span.text

        OutDate = datetime.datetime.strptime(r['OutDate'], '%Y/%m/%d').date() if r['OutDate'] else None
        if AfterDate and OutDate and OutDate >= AfterDate:
            browser.click('#%s' % pqtr('input')[0].attrib['id'], wait_load=True)
            pqclick = pyquery.PyQuery(unicode(browser.webframe.toHtml()))
            form = pqclick('form[name=dischargenotelist]')
            r.update(SimpleInfoShowUsingPlaceHolder(form))
#             r['DischargeSummary'] = SimpleInfoShowUsingPlaceHolder(form)
            
        InPat.append(r)
        
    result['InPat'] = InPat

# 急
    EmergencyContent = pqhtml('#NTUHWeb1_PatAccountListRecord1_GridViewEmergencyContent')
    Emergency = []
    for tr in pyquery.PyQuery(EmergencyContent)('tr'):
        r = {}
        pqtr = pyquery.PyQuery(tr)
        pqspan = pqtr('span')
        if not len(pqspan):
            continue
        for span in pqspan:
            key = span.attrib['id'].split('_')[-1][9:]
            r[key] = span.text
            
            
        DischargeDate = datetime.datetime.strptime(r['DischargeDate'], '%Y/%m/%d').date() if r['DischargeDate'] else None
        if AfterDate and DischargeDate and DischargeDate >= AfterDate:
            browser.click('#%s' % pqtr('input')[0].attrib['id'], wait_load=True)
            pqclick = pyquery.PyQuery(unicode(browser.webframe.toHtml()))
            form = pqclick('form[name=emerrecordlist]')
            r.update(SimpleInfoShowUsingPlaceHolder(form))
#             r['EmerMedicalRecordList'] = SimpleInfoShowUsingPlaceHolder(form)

        Emergency.append(r)
        
    result['Emergency'] = Emergency
    

# 門
    OutPatRecord = pqhtml('#NTUHWeb1_PatAccountListRecord1_GridViewOutPatRecord')
    OutPat = []
    for tr in pyquery.PyQuery(OutPatRecord)('tr'):
        r = {}
        pqtr = pyquery.PyQuery(tr)
        pqspan = pqtr('span')
        if not len(pqspan):
            continue
        for span in pqspan:
            key = span.attrib['id'].split('_')[-1][5:]
            r[key] = span.text
            
        ComeClinicDate = datetime.datetime.strptime(r['ComeClinicDate'], '%Y/%m/%d').date() if r['ComeClinicDate'] else None
        if AfterDate and ComeClinicDate and ComeClinicDate >= AfterDate:
            browser.click('#%s' % pqtr('input')[0].attrib['id'], wait_load=True)
            pqclick = pyquery.PyQuery(unicode(browser.webframe.toHtml()))
            form = pqclick('form[name=dischargenotelist]')
            r.update(SimpleInfoShowUsingPlaceHolder(form))
#             r['Clinics'] = SimpleInfoShowUsingPlaceHolder(form)
            
        OutPat.append(r)
    result['OutPat'] = OutPat
    
    browser.close()
    return result


def OPNoteList(Chart, SESSION = None):
# def PatientMedicalRecordListQuery(q, Chart, SESSION = None):
    if not SESSION:
          SESSION = Login()
    '''
    手術紀錄
    '''
    
    url = "http://ihisaw.ntuh.gov.tw/WebApplication/OtherIndependentProj/PatientBasicInfoEdit/PatientMedicalRecordListQuery.aspx?QueryBySelf=N&SESSION=%s" % SESSION
    
    browser = Browser()
    browser.load(url)
    browser.wk_fill('#NTUHWeb1_PatientBasicInfoQueryByIDAndName1 > input:nth-child(4)', Chart)
#     browser.wk_click('#NTUHWeb1_PatientBasicInfoQueryByIDAndName1 > input[type="submit"]:nth-child(9)', wait_load=True, timeout=9)
    browser.wk_click('#NTUHWeb1_PatientBasicInfoQueryByIDAndName1 > input[type="submit"]:nth-child(9)', wait_load=True)
    browser.click('#NTUHWeb1_PatAccountListRecord1_ShowOperationList', wait_load=True)
    match = re.search(r"'(http://ihisaw.ntuh.gov.tw/WebApplication/OtherIndependentProj/PatientBasicInfoEdit/SimpleInfoShowUsingPlaceHolder.aspx.+?)'", browser.html)
    if match is None:
        return []
    url = match.group(1)
    browser.load(url)
    
    html =unicode(browser.webframe.toHtml())
    pyhtml = pyquery.PyQuery(html)
    
    list = []
    
    for a in pyhtml('#TreeViewItem')('a'):
        r = {}
        id = a.attrib['id']
        
        r['Item']     = id
        r['KeyCode']    = a.attrib['href'].split(',')[-1].split("'")[1][1:]
        r['KeyName']    = a.text
        
        url = 'http://ihisaw.ntuh.gov.tw/WebApplication/OtherIndependentProj/PatientBasicInfoEdit/SimpleInfoShowUsingPlaceHolder.aspx?SESSION=%s' % SESSION
        data = {
            'KeyCodeList': r['KeyCode'],
            'KeyNameList': r['KeyName'],
            'Func'       : 'OPNoteList',
            }

        posturl = '%s&%s' % (url, urllib.urlencode(data))
        browser.load(posturl)
        
        r['url']        = posturl
        r['Report']     = unicode(browser.webframe.toHtml())
        
        list.append(r)

    browser.close()        
    return list


#病歷號/身分證號
def ReportPathology(ID, SESSION=None):
    if not SESSION:
          SESSION = Login()

    browser = Browser()

    url = 'http://ihisaw.ntuh.gov.tw/WebApplication/ElectronicMedicalReportViewer/MobileMasterPage.aspx?SESSION=%s' % SESSION
    browser.load(url)

#     print browser.url, len(browser.html)
#     print browser.html

    browser.fill("#txbIDInput", str(ID))
    browser.click("#btnQueryAction")
    browser.wait_load(timeout=30)
    
    body = unicode(browser.webframe.toHtml())
#     body = browser.html


    d = pyquery.PyQuery(body)
    
    d2 = d('div').filter(lambda i: pyquery.PyQuery(this).children('div').attr.reportgroup == u'病理')
    
#     print d2
    
#     SingleMedicalReport = []
    report = []            
    
    for ri in d2('.reportitem'):
#         print dir(ri)
#         print ri.attrib['param']

        pqri = pyquery.PyQuery(ri)
        
#         # ignore item without ReportDate
#         if pqri('td')[2].text.strip() == '*':
#             continue

        id = pqri('.linkrptbt').attr.id
        browser.click("#%s"%id)
        browser.wait_load()

        ifrmURL = pyquery.PyQuery(browser.html)('#Reportifrm').attr.src
        
        parse = urlparse.parse_qs(urlparse.urlparse(ifrmURL).query)
        ChartNo    = parse['ChartNo'][0]
        ReportKey  = parse['ReportKey'][0]
        ReportCode = parse['ReportCode'][0]
        PersonID   = parse['PersonID'][0]
        
        html = unicode(browser.webframe.childFrames()[0].toHtml())

        browser.click("#backToHome")
        browser.wait_load()
        
        d = pyquery.PyQuery(html)
       
        PathCode          = d('#rReportTab_lsvReportBody_ctrl0_ctl00_lblPathCode').text()
        SpecimenGetDate   = d('#rReportTab_lsvReportBody_ctrl0_ctl00_lblSpecimenGetDate').text() 
        ReportDate        = d('#rReportTab_lsvReportBody_ctrl0_ctl00_lblReportDate').text()

        SpecimenCode      = d('#rReportTab_lsvReportBody_ctrl0_ctl00_lblSpecimenCode').text()
        DepCode           = d('#rReportTab_lsvReportBody_ctrl0_ctl00_lblDepCode').text()
        WardNoRoomCoBedNo = d('#rReportTab_lsvReportBody_ctrl0_ctl00_lblWardNoRoomCoBedNo').text()
       
        Tissue            = d('#rReportTab_lsvReportBody_ctrl0_ctl00_lblTissue').text()
        BedDiagnosis      = d('#rReportTab_lsvReportBody_ctrl0_ctl00_lblBedDiagnosis').text()
       
        Result            = d('#rReportTab_lsvReportBody_ctrl0_ctl00_lblResult').text()

        ExamDoctorName    = d('#rReportTab_lsvReportBody_ctrl0_ctl00_lblExamDoctorName').text()
        InChargeDoctorName= d('#rReportTab_lsvReportBody_ctrl0_ctl00_lblInChargeDoctorName').text()
        ReCheckDoctorName = d('#rReportTab_lsvReportBody_ctrl0_ctl00_lblReCheckDoctorName').text()

       # ignore item without ReportDate
        if ReportDate == '':
           continue

        report.append({'url': ifrmURL, 
                      'html': html,

                      'ChartNo': ChartNo,
                      'ReportKey': ReportKey,
                      'ReportCode': ReportCode,
                      'PersonID': PersonID,
                      
                      'PathCode': PathCode,
                      'SpecimenGetDate': SpecimenGetDate,
                      'ReportDate': ReportDate,
                      
                      'SpecimenCode': SpecimenCode,
                      'DepCode': DepCode,
                      'WardNoRoomCoBedNo': WardNoRoomCoBedNo,
                      
                      'Tissue': Tissue,
                      'BedDiagnosis': BedDiagnosis,
                      
                      'Result': Result,
                      
                      'ExamDoctorName': ExamDoctorName,
                      'InChargeDoctorName': InChargeDoctorName,
                      'ReCheckDoctorName': ReCheckDoctorName,
                      
                      })
        
    browser.close()
    return report
        
#4th 報告
def ElectronicMedicalReportViewer(ID, SESSION=None):
    if not SESSION:
          SESSION = Login()

    browser = Browser()

    url = 'http://ihisaw.ntuh.gov.tw/WebApplication/ElectronicMedicalReportViewer/MobileMasterPage.aspx?SESSION=%s' % SESSION
    browser.load(url)

    browser.fill("#txbIDInput", str(ID))
    browser.click("#btnQueryAction")
    browser.wait_load(timeout=30)
    
    body = unicode(browser.webframe.toHtml())

    d = pyquery.PyQuery(body)
    
    report = []            
    
    for ri in d('.reportitem'):
        pqri = pyquery.PyQuery(ri)

        r = {}
        
        r['報告類別'] = pqri('td')[0].text.strip()
        r['檢查日期'] = pqri('td')[1].text.strip()
        r['報告日期'] = pqri('td')[2].text.strip()
        
#         print ri.text_content()
        id = pqri('.linkrptbt').attr.id
        browser.click("#%s"%id, wait_load=True)

        ifrmURL = pyquery.PyQuery(browser.html)('#Reportifrm').attr.src
        
#         print ifrmURL
        
        
        parse = urlparse.parse_qs(urlparse.urlparse(ifrmURL).query)
        r['ChartNo']    = parse['ChartNo'][0]
        r['ReportKey']  = parse['ReportKey'][0]
        r['ReportCode'] = parse['ReportCode'][0]
        r['PersonID']   = parse['PersonID'][0]
        
        html = unicode(browser.webframe.childFrames()[0].toHtml())
        
        r['url'] = ifrmURL
        r['html'] = html
#         print html

        browser.click("#backToHome")
        browser.wait_load()
        
#         d2 = pyquery.PyQuery(html)('span').filter(lambda i: '_lbl' in pyquery.PyQuery(this).attr.id)
        d2 = pyquery.PyQuery(html)('span')
#         d2 = pyquery.PyQuery(html)('span').filter(lambda i: hasattr(pyquery.PyQuery(this), 'attrib') and 'id' in pyquery.PyQuery(this).attrib)
        
        for lbl in d2:
            if 'id' not in lbl.attrib:
                continue
            if '_lbl' not in lbl.attrib['id']:
                continue
            if 'Title' in lbl.attrib['id']:
                continue
            key = lbl.attrib['id'].split('_')[-1][3:]
            value = lbl.text
            r[key] = value

        report.append(r)
        
    browser.close()
    return report
    
    
def QueryInPatientByDate(ID, begin, end, SESSION=None):
    '''
    病房作業
    查詢條件輸入
    依使用者
    '''
    if not SESSION:
      SESSION = Login()

    browser = Browser()
    
    browser.load('http://ihisaw.ntuh.gov.tw/WebApplication/InPatient/Ward/OpenWard.aspx?SESSION=%s' % SESSION)

    '''
    jquery on these fields doesn't work
    however, click does work 
    '''
    browser.wk_fill('input[id="NTUHWeb1_QueryInPatientPersonAccountControl1_IDInputTextBox"]', ID)

    browser.wk_fill('input[id="NTUHWeb1_QueryInPatientPersonAccountControl1_DateTextBoxYearMonthDayInputUI1_YearInput"]', begin.year)
    browser.wk_fill('input[id="NTUHWeb1_QueryInPatientPersonAccountControl1_DateTextBoxYearMonthDayInputUI1_MonthInput"]', begin.month)
    browser.wk_fill('input[id="NTUHWeb1_QueryInPatientPersonAccountControl1_DateTextBoxYearMonthDayInputUI1_DayInput"]', begin.day)
 
    browser.wk_fill('input[id="NTUHWeb1_QueryInPatientPersonAccountControl1_DateTextBoxYearMonthDayInputUI2_YearInput"]', end.year)
    browser.wk_fill('input[id="NTUHWeb1_QueryInPatientPersonAccountControl1_DateTextBoxYearMonthDayInputUI2_MonthInput"]', end.month)
    browser.wk_fill('input[id="NTUHWeb1_QueryInPatientPersonAccountControl1_DateTextBoxYearMonthDayInputUI2_DayInput"]', end.day)

    
    browser.click("#NTUHWeb1_QueryInPatientPersonAccountControl1_EmpNoCareQueryButton", wait_load=True)

    browser.wk_check('input[id="NTUHWeb1_QueryInPatientPersonAccountControl1_CheckBoxShowDrMainColumn"]')
    browser.click('#NTUHWeb1_QueryInPatientPersonAccountControl1_CheckBoxShowDrMainColumn', wait_load=True)

#     browser.snapshot().save("webpage3.png")
    
    html = unicode(browser.webframe.toHtml())
    
#     print html
    
    browser.close()
    
    d = pyquery.PyQuery(html)

    PatientList = [] 

    for tr in d('#NTUHWeb1_QueryInPatientPersonAccountControl1_DataGridAccountList > tbody')('tr')[1:]:
        d2 = pyquery.PyQuery(tr)
#         print d2.text()
#         print d2('span')
#         return
    
        Pat = {}
    
        for s in d2('span')+d2('a'):
            if 'id' in s.attrib:
                key = s.attrib['id'].split('_')[-1]
                Pat[key] = s.text
                if 'title' in s.attrib:
                        Pat[key+'Title'] = s.attrib['title']
                    
        Pat['主治']   = d2('span')[7].text
        Pat['住院']   = d2('span')[8].text
        Pat['科']    = d2('span')[9].text
        Pat['入']    = d2('span')[10].text
        Pat['住院總天數']    = d2('span')[10].attrib['title'].split(':')[-1].strip()
        Pat['出']    = d2('span')[11].text
        Pat['狀態']   = d2('span')[12].text
        Pat['狀態2']   = d2('span')[13].text

        Pat['id']   = '%s-%s'%(Pat['入'], Pat['PatChartNo'])

        PatientList.append(Pat)

    return PatientList

def QueryInPatientByMonth(ID, year, month, SESSION=None):
    '''
    病房作業
    查詢條件輸入
    依使用者
    '''
    if not SESSION:
      SESSION = Login()

    weakday, number = calendar.monthrange(year, month)
    
#     year   = str(year)
#     month  = str(month)
#     number = str(number)
    
    return QueryInPatientByDate(ID, datetime.date(year, month, 1), datetime.date(year, month, number), SESSION)
    


def QueryOpScheduleByDate(ID, begin, end, SESSION=None):
    '''
    手術作業
    主治查詢
    '''
    
    if not SESSION:
      SESSION = Login()
      
    browser = Browser()
    
    browser.load('http://ihisaw.ntuh.gov.tw/WebApplication/InPatient/OPManagement/SimpleQueryOpSchedule.aspx?SESSION=%s' % SESSION)
    browser.fill('#NTUHWeb1_QueryOPPatListCommon1_QueryDrIDInfoByDrName1_EmpNoQueryInput', str(ID))
    browser.fill('#NTUHWeb1_QueryOPPatListCommon1_txbStartDate', begin.strftime('%Y/%m/%d'))
    browser.fill('#NTUHWeb1_QueryOPPatListCommon1_txbEndDate', end.strftime('%Y/%m/%d'))
    browser.click('#NTUHWeb1_QueryOPPatListCommon1_QueryByMainDrCode', wait_load=True)
    pqhtml = pyquery.PyQuery(unicode(browser.webframe.toHtml()))
    browser.close()
    
    results = []
    
    for tr in pqhtml('#NTUHWeb1_QueryOPPatListCommon1_OPScheduleShowDataGrid1_dgRecordData > tbody')('tr'):
        r={}
        d2 = pyquery.PyQuery(tr)
        for child in d2('span') + d2('a'):
            key = child.attrib['id'].split('_')[-1]
            r[key] = child.text
            
            if 'title' in child.attrib:
                r[key+'Title'] = child.attrib['title'].strip()

<<<<<<< HEAD
	if r['OPDateString'].count('/') != 2:
	    date = '%d/%s' % (datetime.date.today().year, r['OPDateString'])
=======
    if r['OPDateString'].count('/') != 2:
        date = '%d/%s' % (datetime.date.today().year, r['OPDateString'])
>>>>>>> 3b236beb1aca71c7ab0879d1ab01bf96f280d5ea

#            date = '%d/%s' % (begin.year, r['OPDateString'])
#            if begin.year != end.year and date < begin:
#                date = '%d/%s' % (end.year, r['OPDateString'])
            r['OPDateString'] = date
            
        r['id'] = '%s-%s-%s-%s'%(r['OPDateString'], r['OpRoomNoShow'], r['OpSeqNoshow'], r['PatChartNo'])
#         r['id'] = r['id'].replace('/', '').replace('-', '')
        results.append(r)
    return results

def WardQueryUncompletedChart(Start, End, DeptCode='SURG', SESSION=None):
    '''
    未完成病歷查詢
    '''

    if not SESSION:
      SESSION = Login()
      
    browser = Browser()

    url = "http://ihisaw.ntuh.gov.tw/WebApplication/MedicalRecordManagement/WardQueryUncompletedChart.aspx?SESSION=%s" % SESSION
    
    browser.load(url)
#     print 123
#     browser.runjs(r"setTimeout('__doPostBack(\'NTUHWeb1$RBOutDate\',\'\')', 0);")
#     try:
#         browser.submit('#NTUHWeb1_RBOutDate', timeout=1)
#     except:
#         pass
    browser.click('#NTUHWeb1_RBOutDate')
    while 'NTUHWeb1_ddlDeptCode' not in browser.html:
#         print "wait"
        browser.wait(1)   
#     ret = browser.runjs("console.log(typeof(jQuery));")
#     browser.load_jquery(force=True)
#     print 456
#     exit()


#     html = unicode(browser.webframe.toHtml())
#     pqhtml = pyquery.PyQuery(html)
# 
#     print pqhtml('#NTUHWeb1_UpdateProgress1').attr('style')
#     
#     exit()
    
    browser.wk_fill('select[id="NTUHWeb1_ddlDeptCode"]', DeptCode)
    browser.wk_fill('input[id="NTUHWeb1_txtQStartDate"]', Start)
    browser.wk_fill('input[id="NTUHWeb1_txtQEndDate"]', End)
    
    browser.click('#NTUHWeb1_btn_QueryByDeptListAll')
    
#     browser.browse()
    
    for i in range(100):
        browser.wait(1)
        pqhtml = pyquery.PyQuery(unicode(browser.webframe.toHtml()))
        print i, pqhtml('#NTUHWeb1_UpdateProgress1').attr('style')
        if 'none' in pqhtml('#NTUHWeb1_UpdateProgress1').attr('style'):
            break
        
#     print unicode(browser.webframe.toHtml())
        
    results = []
        
    for tr in pqhtml('#NTUHWeb1_dgRecordData')('tr')[1:]:
        tds = pyquery.PyQuery(tr)('td')
        r = {
             '序': tds[0].text,
             '病歷號': tds[1].text,
             '帳號': tds[2].text.strip(),
             '科部': tds[3].text,
             '開始/手術日': tds[4].text,
             '結束日': tds[5].text,
             '姓名': tds[6].text,
             '床號': tds[7].text,
             '類別': tds[8].text.strip(),
             '主治': tds[9].text,
             '住院': tds[10].text,
             '註': tds[11].text,
             '逾期': tds[12].text,
             }
        
        results.append(r)
        
    return results


def SimpleQueryOpSchedule(DrCode, StartDate, EndDate, SESSION=None, NoCheck = []):
    if not SESSION:
      SESSION = Login()
      
    today_year = datetime.date.today().year
      
    browser = Browser()

    url = "http://ihisaw.ntuh.gov.tw/WebApplication/InPatient/OPManagement/SimpleQueryOpSchedule.aspx?SESSION=%s" % SESSION
    
    browser.load(url)
    browser.wk_fill('#NTUHWeb1_QueryOPPatListCommon1_QueryDrIDInfoByDrName1_EmpNoQueryInput', DrCode)
    browser.wk_fill('#NTUHWeb1_QueryOPPatListCommon1_txbStartDate', StartDate)
    browser.wk_fill('#NTUHWeb1_QueryOPPatListCommon1_txbEndDate', EndDate)
    
    browser.click('#NTUHWeb1_QueryOPPatListCommon1_QueryByMainDrCode', wait_load=True)
    
#     print unicode(browser.webframe.toHtml())
    pqhtml = pyquery.PyQuery(unicode(browser.webframe.toHtml()))
    results = []
    for tr in pqhtml('#NTUHWeb1_QueryOPPatListCommon1_OPScheduleShowDataGrid1_dgRecordData')('tr')[1:]:
        r = {}
        pqtr = pyquery.PyQuery(tr)
        children = pqtr('span')+ pqtr('a')
        for child in children:
#             print dir(td)
#             print td.text_content().strip()
            key = child.attrib['id'].split('_')[-1]
            r[key] = child.text
            if 'title' in child.attrib:
                r[key+'Title'] = child.attrib['title'].strip()
                
        if r['OPDateString'].count('/') == 1:
            r['OPDateString'] = '%s/%s' % (today_year, r['OPDateString'])
                
        r['Complete'] = r['CompleteStatueName']
        r['OPDate'] = r['OPDateString']
        r['OpRoomNo'] = r['OpRoomNoShow']
        r['OpSeqNo'] = r['OpSeqNoshow']
        r['PatName'] = r['LinkPatName']
        r['PatWard'] = r['PopupPatWardInfoWindow']        
        r['Anes'] = r['lbtPrintTitle']        
        r['StartTime'] = r['EstStartTimeShortString']
        r['SpendTime'] = r['EstSpendTime']
        
        print r['OPDateString'],
        id = '%s-%s-%s-%s' % (r['OPDateString'], r['OpRoomNo'], r['OpSeqNo'],r['PatChartNo'])
        print id
        
        if r['CompleteStatueName'] != '完成' and id not in NoCheck:
#             print "Investigate %s" % id
            r['lbnSelect'] =pqtr('a')[0].attrib['id']
#             print r['lbnSelect']
            browser.click('#%s'%r['lbnSelect'], wait_load=True)
            pqhtml2 = pyquery.PyQuery(unicode(browser.webframe.toHtml()))
#             print unicode(browser.webframe.toHtml())
#             print dir(pqhtml2('#NTUHWeb1_QueryOpSchedule1_txbRemark'))
            r['Remark'] = pqhtml2('#NTUHWeb1_QueryOpSchedule1_txbRemark').attr['value']
            r['MainOpMode'] = pqhtml2('#NTUHWeb1_QueryOpSchedule1_TextboxPrepareOPMode').attr['value']
            browser.webpage.history().back()
            browser.wait_load()

#             print r['Remark'],
#         print r['CompleteStatueName'],r
            
        results.append(r)
    
    return results


if __name__ == "__main__":
    
#     a = PatientMedicalRecordListQuery(4525725, datetime.datetime(2014, 6, 1, 17, 15, 30))
#     a = PatientMedicalRecordListQuery(2829917, None)
#     a = PatientMedicalRecordListQuery(3009684)
#     a = ReportPathology(3418370) 
#     a = ElectronicMedicalReportViewer(3009684) 
#     a = OPNoteList(2228813)
#     a = QueryInPatientByMonth('004552', 2014, 11)
#     a = WardQueryUncompletedChart('2014/12/01', '2014/12/02')
    a = SimpleQueryOpSchedule('004552', '2014/12/01', '2015/02/01')
     
    jprint(a)
    print len(a)
     
#      MyPrettyPrinter().pprint(a)
#     print a['name']
#     print a
