import time from netforce.model import Model, fields, get_model from netforce.utils import get_file_path from . import utils class ImportPayment(Model): _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 _fields={ 'name': fields.Char("Name",function="_get_name"), 'type_id': fields.Many2One("clinic.patient.type","Patient Type"), 'date': fields.Date("Date"), 'file': fields.File("File"), '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'), } 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 _defaults={ 'date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"), 'hcode_id': get_hcode_id, 'max_row': 50, 'state': 'draft', } def import_payment(self,ids,context={}): obj=self.browse(ids)[0] fname=obj.file fpath=get_file_path(fname) lines=utils.read_excel(fpath,show_datetime=True) if not lines: raise Exception("Wrong File") 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" for line in lines: name=line.get("name14") hn=line.get('hn',"") hct=line.get("hct","") hcode=line.get('hcode18','0') if not hcode: hcode='0' hcode=int(hcode) hcode=str(hcode) 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 else: blank+=1 continue remain_row=len(lines)-blank-did-count if remain_row <= 0: msg="Nothing to import" obj.write({ 'total_row': len(lines), 'remain_row': remain_row, 'msg': msg, 'done_qty': done_qty, 'fail_qty': fail_qty, }) print("Done!") ImportPayment.register()