clinic/netforce_clinic/models/import_uc.py

89 lines
2.7 KiB
Python
Raw Normal View History

2014-12-21 18:00:54 +00:00
from netforce.model import Model, fields, get_model
2014-12-21 18:45:46 +00:00
from netforce.utils import get_file_path
from . import utils
2014-12-21 18:00:54 +00:00
class ImportUC(Model):
_name="clinic.import.uc"
_transient=True
_fields={
"payment_id": fields.Many2One("account.payment","Payment",required=True,on_delete="cascade"),
"file": fields.File("File", required=True),
'type_id': fields.Many2One("clinic.patient.type","Type", required=True),
}
def _get_payment_id(self,context={}):
payment_id=context.get("refer_id")
if not payment_id:
return None
return int(payment_id)
def _get_type(self,context={}):
st=get_model("clinic.setting").browse(1)
return st.imp_patient_type_id.id
_defaults={
'payment_id': _get_payment_id,
'type_id': _get_type,
}
def do_import(self,ids,context):
obj=self.browse(ids)[0]
2014-12-21 18:45:46 +00:00
partner=obj.type_id.contact_id
if not partner:
raise Exception("No partner")
account_id=partner.account_income_id.id
if not account_id:
raise Exception("No account income")
vals={
'direct_lines':[],
}
patients={}
for p in get_model("clinic.patient").search_browse([]):
2015-01-09 05:19:52 +00:00
patients[p.hn_no]=p.name
2014-12-21 18:45:46 +00:00
if obj.type_id.code=='U':
fname=obj.file
fpath=get_file_path(fname)
node='EPOBills'
lines=utils.read_xml(fpath,node=node)
#hcode_impt=fname.split("_")[0]
epoadm=0
for line in lines:
hn=line.get('hn')
2015-01-09 05:19:52 +00:00
hn_no=''.join(x for x in hn if x.isdigit()) #XXX
2014-12-21 18:45:46 +00:00
epoadm=line.get("epoadm")
lines2=line.get("epos").get('EPO',[])
2015-01-09 05:19:52 +00:00
name='%s %s'%(hn,patients.get(hn_no,""))
2014-12-21 18:45:46 +00:00
for l2 in lines2:
if type(l2)==type(''):
continue
desc=l2.get('@desc')
if '-' in desc:
continue
name+=desc
vals['direct_lines'].append(('create',{
'description': name,
'qty': 1,
'unit_price': epoadm,
'amount': epoadm,
'account_id': account_id,
}))
for dline in obj.payment_id.direct_lines:
dline.delete()
obj.payment_id.write(vals)
2014-12-21 18:00:54 +00:00
return {
'next': {
'name': 'payment',
'mode': 'form',
'active_id': obj.payment_id.id,
},
'flash': 'Import successfully',
}
ImportUC.register()