clinic/netforce_clinic/models/import_payment.py

93 lines
2.8 KiB
Python
Raw Normal View History

2014-10-04 15:51:54 +00:00
import time
from netforce.model import Model, fields, get_model
from netforce.utils import get_file_path
2014-12-02 11:41:05 +00:00
from . import utils
2014-11-30 13:05:14 +00:00
2014-10-04 15:51:54 +00:00
class ImportPayment(Model):
2014-12-03 00:05:18 +00:00
_name="import.clinic.payment"
_string="Clinic Payment"
def _get_name(self,ids,context={}):
res={}
for obj in self.browse(ids):
res[obj.id]=obj.type_id.name
return res
2014-10-27 14:17:22 +00:00
2014-10-04 15:51:54 +00:00
_fields={
2014-12-03 00:05:18 +00:00
'name': fields.Char("Name",function="_get_name"),
'type_id': fields.Many2One("clinic.patient.type","Patient Type"),
'date': fields.Date("Date"),
2014-10-05 17:00:16 +00:00
'file': fields.File("File"),
2014-12-03 00:05:18 +00:00
'hcode_id': fields.Many2One("clinic.hospital", "Hospital",required=True),
'max_row': fields.Integer("Max Row"),
'remain_row': fields.Integer("Pending"),
'total_row': fields.Integer("Total Payment"),
'msg': fields.Text("Message"),
'done_qty': fields.Integer("Success"),
'fail_qty': fields.Integer("Fail"),
'state': fields.Selection([['draft','Draft'],['fail','Fail'],['success','Success']],'State'),
2014-10-04 15:51:54 +00:00
}
2014-10-22 03:45:23 +00:00
2014-12-03 00:05:18 +00:00
def get_hcode_id(self,context={}):
hp_ids=get_model("clinic.hospital").search([])
hp_id=None
if hp_ids:
hp_id=hp_ids[0]
return hp_id
2014-10-27 14:17:22 +00:00
2014-10-04 15:51:54 +00:00
_defaults={
2014-10-22 03:45:23 +00:00
'date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"),
2014-12-03 00:05:18 +00:00
'hcode_id': get_hcode_id,
'max_row': 50,
'state': 'draft',
2014-10-04 15:51:54 +00:00
}
2014-10-24 04:24:33 +00:00
2014-12-03 00:05:18 +00:00
def import_payment(self,ids,context={}):
2014-10-05 17:00:16 +00:00
obj=self.browse(ids)[0]
2014-10-22 03:45:23 +00:00
fname=obj.file
fpath=get_file_path(fname)
2014-12-03 00:05:18 +00:00
lines=utils.read_excel(fpath,show_datetime=True)
2014-10-24 08:08:56 +00:00
if not lines:
raise Exception("Wrong File")
2014-12-03 00:05:18 +00:00
msg=""
max_row=obj.max_row
count=0
nofound=0
did=0
blank=0
fail_qty=0
done_qty=0
msg+=""*10; msg+="hcode,hn,name\n"
2014-11-30 13:05:14 +00:00
for line in lines:
2014-12-03 00:05:18 +00:00
name=line.get("name14")
hn=line.get('hn',"")
hct=line.get("hct","")
2014-11-30 13:05:14 +00:00
hcode=line.get('hcode18','0')
if not hcode:
hcode='0'
hcode=int(hcode)
hcode=str(hcode)
2014-12-03 00:05:18 +00:00
if not obj.hcode_id.code==hcode:
if hcode:
nofound+=1
if hcode!='0':
msg+="not found %s, %s, %s \n"%(hcode,hn,name)
fail_qty+=1
2014-12-02 11:41:05 +00:00
else:
2014-12-03 00:05:18 +00:00
blank+=1
continue
remain_row=len(lines)-blank-did-count
if remain_row <= 0:
msg="Nothing to import"
2014-10-27 19:01:18 +00:00
obj.write({
2014-12-03 00:05:18 +00:00
'total_row': len(lines),
'remain_row': remain_row,
'msg': msg,
'done_qty': done_qty,
'fail_qty': fail_qty,
2014-10-27 19:01:18 +00:00
})
2014-12-03 00:05:18 +00:00
print("Done!")
2014-10-27 19:01:18 +00:00
2014-10-04 15:51:54 +00:00
ImportPayment.register()