improve matching

conv_bal
watcha.h@almacom.co.th 2015-04-09 17:47:40 +07:00
parent ccab6a2ba2
commit 030ad8fcd5
7 changed files with 279 additions and 190 deletions

View File

@ -67,6 +67,9 @@
<!--</list>--> <!--</list>-->
<!--</field>--> <!--</field>-->
</tab> </tab>
<tab string="Matching HD Case">
<field name="skip_type_id"/>
</tab>
<tab string="Accounting"> <tab string="Accounting">
<field name="cash_account_id"/> <field name="cash_account_id"/>
<field name="income_account_id" string="Account Receivable"/> <field name="income_account_id" string="Account Receivable"/>

View File

@ -1,6 +1,30 @@
<form model="clinic.matching.hdcase"> <form model="clinic.matching.hdcase">
<field name="date_from" onchange="onchange_date" span="2"/> <tabs>
<field name="date_to" span="2"/> <tab string="General">
<field name="file" span="3"/> <field name="date_from" onchange="onchange_date" span="2"/>
<field name="state" span="3"/> <field name="date_to" span="2"/>
<field name="file" span="3"/>
<field name="state" span="3"/>
<!--<group span="6" columns="1">-->
<!--<field name="file_lines" nolabel="1">-->
<!--<list>-->
<!--<field name="file"/>-->
<!--</list>-->
<!--</field>-->
<!--</group>-->
<!--<group span="6" columns="1">-->
<!--</group>-->
</tab>
<tab string="Skip Type">
<group span="6" columns="1">
<field name="skip_lines" nolabel="1">
<list>
<field name="type_id"/>
</list>
</field>
</group>
<group span="6" columns="1">
</group>
</tab>
</tabs>
</form> </form>

View File

@ -114,6 +114,8 @@ from . import matching_payment_popup
from . import invoice_payment from . import invoice_payment
from . import invoice_payment_line from . import invoice_payment_line
from . import matching_hdcase from . import matching_hdcase
from . import matching_hdcase_line
from . import matching_hdcase_file
from . import sale_order from . import sale_order
from . import shop from . import shop
from . import shop_line from . import shop_line

View File

@ -12,18 +12,25 @@ class MatchingHDCase(Model):
"date_to": fields.Date("To", required=True), "date_to": fields.Date("To", required=True),
'file': fields.File("File"), 'file': fields.File("File"),
'state': fields.Selection([["match","Math"],["not_match","Not Match"]],"State"), 'state': fields.Selection([["match","Math"],["not_match","Not Match"]],"State"),
'skip_lines': fields.One2Many("clinic.matching.hdcase.line","matching_hdcase_id","Skip Lines"),
'file_lines': fields.One2Many("clinic.matching.hdcase.file","matching_hdcase_id","File Lines"),
'msg': fields.Text("Message"),
} }
def default_get(self,field_names=None,context={},**kw): def _get_skip_lines(self,context={}):
defaults=context.get("defaults",{}) skip_lines=[]
datenow=time.strftime("%Y-%m-%d") st=get_model('clinic.setting').browse(1)
date_from=defaults.get('date_from',datenow) if st.skip_type_id:
date_to=defaults.get('date_to',datenow) skip_lines.append({
res={ 'type_id': st.skip_type_id.id,
'date_from': date_from, })
'date_to': date_to, return skip_lines
}
return res _defaults={
'date_from': lambda *a: time.strftime('%Y-%m-%d'),
'date_to': lambda *a: time.strftime('%Y-%m-%d'),
'skip_lines': _get_skip_lines,
}
def get_rows(self,fpath=None): def get_rows(self,fpath=None):
if not fpath: if not fpath:
@ -72,194 +79,220 @@ class MatchingHDCase(Model):
state=obj.state state=obj.state
date_from=obj.date_from date_from=obj.date_from
date_to=obj.date_to date_to=obj.date_to
if obj.file: skip_lines=[skip.type_id.name for skip in obj.skip_lines]
fpath=get_file_path(obj.file) print('skip_lines ', skip_lines)
rows=self.get_rows(fpath) if not obj.file:
if not rows: return {}
raise Exception("No Data") #raise Exception("File Not found")
#{0: ' A 01', fpath=get_file_path(obj.file)
#1: ' 6', rows=self.get_rows(fpath)
#2: ' 11686', if not rows:
#3: ' 91312851', raise Exception("No Data")
#4: ' 450124497', #{0: ' A 01',
#5: ' 27/01/2558 06:05:00', #1: ' 6',
#6: ' C', #2: ' 11686',
#7: ' O', #3: ' 91312851',
#8: ' U ', #4: ' 450124497',
#9: ' N', #5: ' 27/01/2558 06:05:00',
#10: ' Espogen', #6: ' C',
#11: ' 4000', #7: ' O',
#12: ' 30', #8: ' U ',
#13: ' 1', #9: ' N',
#14: ' 0', #10: ' Espogen',
#15: ' 1500', #11: ' 4000',
#16: ' 1500', #12: ' 30',
#17: ' 0', #13: ' 1',
#18: ' 1500', #14: ' 0',
#19: ' S', #15: ' 1500',
#20: ' U '} #16: ' 1500',
#17: ' 0',
#18: ' 1500',
#19: ' S',
#20: ' U '}
patients={} patients={}
dom=[ dom=[
['dispose','=',False], ['dispose','=',False],
['walkin','=','no'], ['walkin','=','no'],
] ]
for pt in get_model("clinic.patient").search_read(dom,['name','hn_no','type_id']): for pt in get_model("clinic.patient").search_read(dom,['name','hn_no','type_id']):
hn=pt['hn_no'] hn=pt['hn_no']
patients[hn]={ patients[hn]={
'id': pt['id'], 'id': pt['id'],
'name': pt['name'] or '', 'name': pt['name'] or '',
'type': pt['type_id'][1], 'type': pt['type_id'][1],
} }
products={} products={}
for prod in get_model("product").search_read([],['name']): for prod in get_model("product").search_read([],['name']):
name=(prod['name'] or "").replace(" ", "") name=(prod['name'] or "").replace(" ", "")
name=name.upper() name=name.upper()
products[name]=prod['id'] products[name]=prod['id']
dom=[] dom=[]
dom.append(['patient_id.walkin',"=","no"]) #XXX dom.append(['patient_id.walkin',"=","no"]) #XXX
dom.append(['date',">=",date_from]) dom.append(['date',">=",date_from])
dom.append(['date',"<=",date_to]) dom.append(['date',"<=",date_to])
dom.append(['state','!=','cancelled']) dom.append(['state','!=','cancelled'])
hdcases={} hdcases={}
hdcases2={} hdcases2={}
hdcases3={} hdcases3={}
def get_account_patient_hn(patient_id): def get_account_patient_hn(patient_id):
res=get_model("clinic.setting.account.patient").search_read([['patient_id','=',patient_id]],['hn','patient_id']) res=get_model("clinic.setting.account.patient").search_read([['patient_id','=',patient_id]],['hn','patient_id'])
if res: if res:
return res[0] return res[0]
return res return res
for hdcase in get_model("clinic.hd.case").search_browse(dom): for hdcase in get_model("clinic.hd.case").search_browse(dom):
date=hdcase.date date=hdcase.date
hn=hdcase.patient_id.hn_no or "" hn=hdcase.patient_id.hn_no or ""
#hn=hn[0:7] #XXX K.Ekk explain on 03/04/2015 : 0-7 is main of hn #hn=hn[0:7] #XXX K.Ekk explain on 03/04/2015 : 0-7 is main of hn
fee_amt=0 fee_amt=0
hct=hdcase.hct or "0" hct=hdcase.hct or "0"
hct=round(float(hct),2) hct=round(float(hct),2)
prod_line=[] prod_line=[]
for line in hdcase.lines: for line in hdcase.lines:
prod=line.product_id prod=line.product_id
categ=prod.categ_id categ=prod.categ_id
if categ and line.reimbursable=='yes': if categ and line.reimbursable=='yes':
if categ.code=='EPO': if categ.code=='EPO':
prod_name=(prod.name or "").split("-") prod_name=(prod.name or "").split("-")
if len(prod_name) >= 1: if len(prod_name) >= 1:
prod_name=prod_name[0] prod_name=prod_name[0]
prod_line.append(prod_name) prod_line.append(prod_name)
elif categ.code=='FEE': elif categ.code=='FEE':
fee_amt=line.amount or 0 fee_amt=line.amount or 0
prod_name='' prod_name=''
if prod_line: if prod_line:
prod_name='-'.join(prod_line) prod_name='-'.join(prod_line)
key1='%s-%s-%s-%s'%(date,hn,prod_name,fee_amt) key1='%s-%s-%s-%s'%(date,hn,prod_name,fee_amt)
hdcases[key1]={ hdcases[key1]={
'id': hdcase.id,
'number': hdcase.number or '',
'note': '',
}
key2='%s-%s'%(date,hn)
hdcases2[key2]={
'id': hdcase.id,
'number': hdcase.number or '',
'note': '%s, %s, %s, %s, %s'%(date,hn,hct,prod_name,fee_amt),
}
patient=hdcase.patient_id
result=get_account_patient_hn(patient.id)
if result:
hn=result['hn']
key3='%s-%s-%s-%s'%(date,hn,prod_name,fee_amt)
hdcases3[key3]={
'id': hdcase.id, 'id': hdcase.id,
'number': hdcase.number or '', 'number': hdcase.number or '',
'note': '', 'note': '',
'patient_id': patient.id,
'patient_name': patient.name,
'patient_type': patient.type_id.name,
} }
key2='%s-%s'%(date,hn) for row in rows:
hdcases2[key2]={ vals=list(row.values())
'id': hdcase.id, date,_time=(vals[5] or "").split()
'number': hdcase.number or '', if len(date)<3:
'note': '%s, %s, %s, %s, %s'%(date,hn,hct,prod_name,fee_amt), raise Exception("Wrong format date")
} d,m,y=date.split("/")
patient=hdcase.patient_id y=int(y)-543
result=get_account_patient_hn(patient.id) date='%s-%s-%s'%(y,m,d)
if result: hn=vals[3]
hn=result['hn'] hn=''.join(x for x in (hn or "") if x.isdigit())
key3='%s-%s-%s-%s'%(date,hn,prod_name,fee_amt) pt=patients.get(hn)
hdcases3[key3]={ pt_type=''
'id': hdcase.id, pt_name=''
'number': hdcase.number or '', pt_id=None
'note': '', if pt:
'patient_id': patient.id, pt_name=pt['name']
'patient_name': patient.name, pt_id=pt['id']
'patient_type': patient.type_id.name, pt_type=pt['type']
} hct=float(vals[12] or "0")
for row in rows: hct=round(hct,2)
vals=list(row.values()) fee_amt=float(vals[15] or "0")
date,_time=(vals[5] or "").split() fee_amt=round(fee_amt,2)
if len(date)<3: prod_name=vals[10] or ''
raise Exception("Wrong format date") prod_name=prod_name.replace(" ", "")
d,m,y=date.split("/") prod_id=products.get(prod_name.upper(),None)
y=int(y)-543 key1='%s-%s-%s-%s'%(date,hn,prod_name.upper(),fee_amt)
date='%s-%s-%s'%(y,m,d) hdcase=hdcases.get(key1)
hn=vals[3] unit_amt=float(vals[11])
hn=''.join(x for x in (hn or "") if x.isdigit()) line_vals={
pt=patients.get(hn) 'hn': hn,
pt_type='' 'date': date,
pt_name='' 'patient_name': pt_name,
pt_id=None 'patient_id': pt_id,
if pt: 'patient_type': pt_type,
pt_name=pt['name'] 'hct': hct,
pt_id=pt['id'] 'prod_name': (prod_name or "").title(),
pt_type=pt['type'] 'prod_id': prod_id,
hct=float(vals[12] or "0") 'fee_amt': fee_amt,
hct=round(hct,2) 'note': '',
fee_amt=float(vals[15] or "0") 'nf_date': '',
fee_amt=round(fee_amt,2) 'nf_hn': '',
prod_name=vals[10] or '' 'nf_hct': '',
prod_name=prod_name.replace(" ", "") 'nf_epo': '',
prod_id=products.get(prod_name.upper(),None) 'nf_fee': '',
key1='%s-%s-%s-%s'%(date,hn,prod_name.upper(),fee_amt) 'unit_amt': unit_amt,
hdcase=hdcases.get(key1) }
unit_amt=float(vals[11]) if hdcase:
line_vals={ line_vals['is_match']=True
'hn': hn, line_vals['hd_case_id']=hdcase['id']
'date': date, line_vals['hd_case_number']=hdcase['number']
'patient_name': pt_name, else:
'patient_id': pt_id, line_vals['is_match']=False
'patient_type': pt_type,
'hct': hct, key2='%s-%s'%(date,hn)
'prod_name': (prod_name or "").title(), hdcase2=hdcases2.get(key2)
'prod_id': prod_id, if hdcase2:
'fee_amt': fee_amt, line_vals['hd_case_id']=hdcase2['id']
'note': '', line_vals['hd_case_number']=hdcase2['number']
'nf_date': '', line_vals['note']=''
'nf_hn': '', nf_date,nf_hn,nf_hct,nf_epo,nf_fee=hdcase2['note'].split(",")
'nf_hct': '', line_vals['nf_date']=nf_date
'nf_epo': '', line_vals['nf_hn']=nf_hn
'nf_fee': '', line_vals['nf_hct']=nf_hct
'unit_amt': unit_amt, nf_epo=(nf_epo or "").replace("-",",")
} line_vals['nf_epo']=nf_epo.title()
if hdcase: line_vals['nf_fee']=round(float((nf_fee or "0")),2)
line_vals['is_match']=True lines.append(line_vals)
line_vals['hd_case_id']=hdcase['id']
line_vals['hd_case_number']=hdcase['number']
else:
line_vals['is_match']=False
key2='%s-%s'%(date,hn)
hdcase2=hdcases2.get(key2)
if hdcase2:
line_vals['hd_case_id']=hdcase2['id']
line_vals['hd_case_number']=hdcase2['number']
line_vals['note']=''
nf_date,nf_hn,nf_hct,nf_epo,nf_fee=hdcase2['note'].split(",")
line_vals['nf_date']=nf_date
line_vals['nf_hn']=nf_hn
line_vals['nf_hct']=nf_hct
nf_epo=(nf_epo or "").replace("-",",")
line_vals['nf_epo']=nf_epo.title()
line_vals['nf_fee']=round(float((nf_fee or "0")),2)
lines.append(line_vals)
key3='%s-%s-%s-%s'%(date,hn,prod_name.upper(),fee_amt) key3='%s-%s-%s-%s'%(date,hn,prod_name.upper(),fee_amt)
hdcase=hdcases3.get(key3) hdcase=hdcases3.get(key3)
if hdcase: if hdcase:
line_vals['is_match']=True line_vals['is_match']=True
line_vals['hd_case_id']=hdcase['id'] line_vals['hd_case_id']=hdcase['id']
line_vals['hd_case_number']=hdcase['number'] line_vals['hd_case_number']=hdcase['number']
line_vals['patient_id']=hdcase['patient_id'] line_vals['patient_id']=hdcase['patient_id']
line_vals['patient_name']=hdcase['patient_name'] line_vals['patient_name']=hdcase['patient_name']
line_vals['patient_type']=hdcase['patient_type'] line_vals['patient_type']=hdcase['patient_type']
no=1 no=1
lines2=[] lines2=[]
total=0 total=0
total_match=0 total_match=0
total_unmatch=0 total_unmatch=0
mdate=[]
for line in sorted(lines,key=lambda x: x['hn']): for line in sorted(lines,key=lambda x: x['hn']):
ldate=line.get('date') or ""
if ldate not in mdate:
mdate.append(ldate)
cont=ldate and (ldate >=date_from and ldate <= date_to)
# skip out of range
if not cont:
continue
patient_type=line.get('patient_type')
hn=line.get('hn')
if patient_type in skip_lines:
print('continue ', patient_type)
continue
elif not patient_type and hn:
found=0
for pt in get_model("clinic.patient").search_browse([['hn_no','=',hn]]):
if pt.type_id.name in skip_lines:
found=1
break
if found:
print('continue ', hn)
continue
is_match=line.get('is_match',False) is_match=line.get('is_match',False)
if is_match: if is_match:
total_match+=1 total_match+=1
@ -288,11 +321,13 @@ class MatchingHDCase(Model):
'total_match': total_match, 'total_match': total_match,
'total_unmatch': total_unmatch, 'total_unmatch': total_unmatch,
} }
if no <=1 and ids:
raise Exception("File match only %s"%', '.join(sorted(mdate)))
return data return data
def onchange_date(self,context={}): def onchange_date(self,context={}):
data=context['data'] data=context['data']
data['date_to']=data['date_from'] #data['date_to']=data['date_from']
return data return data
MatchingHDCase.register() MatchingHDCase.register()

View File

@ -0,0 +1,12 @@
from netforce.model import Model, fields, get_model
class MatchingHDCaseFile(Model):
_name="clinic.matching.hdcase.file"
_transient=True
_fields={
"matching_hdcase_id": fields.Many2One("clinic.matching.hdcase","Matching HD Case"),
'file': fields.File("File"),
}
MatchingHDCaseFile.register()

View File

@ -0,0 +1,12 @@
from netforce.model import Model, fields, get_model
class MatchingHDCaseLine(Model):
_name="clinic.matching.hdcase.line"
_transient=True
_fields={
"matching_hdcase_id": fields.Many2One("clinic.matching.hdcase","Matching HD Case"),
'type_id': fields.Many2One("clinic.patient.type","Type"),
}
MatchingHDCaseLine.register()

View File

@ -55,6 +55,7 @@ class ClinicSetting(Model):
'branch_id': fields.Many2One("clinic.branch","Branch"), 'branch_id': fields.Many2One("clinic.branch","Branch"),
'shop_categs': fields.Many2Many("product.categ","Categs"), 'shop_categs': fields.Many2Many("product.categ","Categs"),
'shop_type_id': fields.Many2One("clinic.patient.type","Patient Type"), 'shop_type_id': fields.Many2One("clinic.patient.type","Patient Type"),
'skip_type_id': fields.Many2One("clinic.patient.type","Skip Type"), # Matching HD Case
"cash_account_id": fields.Many2One("account.account","Cash Account",multi_company=True), "cash_account_id": fields.Many2One("account.account","Cash Account",multi_company=True),
"income_account_id": fields.Many2One("account.account","Income Account",multi_company=True), "income_account_id": fields.Many2One("account.account","Income Account",multi_company=True),
"import_account_id": fields.Many2One("account.account","Import Account",multi_company=True), "import_account_id": fields.Many2One("account.account","Import Account",multi_company=True),