import time from netforce.model import Model, fields, get_model from netforce.access import get_active_company, get_active_user, set_active_user PATIENT_TYPE={ "sc":"Social Security", "uc":"UC", "others": "Others", } class Patient(Model): _name="clinic.patient" _string="Patient" _audit_log=True _multi_company=True _key=['name'] def _get_age(self,ids,context): res={} year_now=int(time.strftime("%Y")) for obj in self.browse(ids): age=0 if obj.birthday: year_bd=int(obj.birthday[0:4]) age=year_now-year_bd res[obj.id]=age return res # -> {1: 30, 2: 45,.....} _fields={ "type": fields.Selection([("sc","Social Security"),("uc","UC"),("others","Others")],"Type",required=True), "number": fields.Char("Number",required=True,search=True), "hn": fields.Char("REF/HN",search=True), "name": fields.Char("Name",required=True,search=True), "reg_date": fields.Date("Register Date",required=False,search=True), "birthday": fields.Date("Birthday",required=False,search=True), "phone": fields.Char("Phone",required=False,search=True), "mobile": fields.Char("Mobile",required=False,search=True), "job": fields.Char("Job/Position"), "age": fields.Integer("Age", function="_get_age"), 'picture': fields.File("Picture"), 'email': fields.Char("Email"), "weight": fields.Float("Weight (cm)"), "height": fields.Float("Height (Kg)"), "card_type": fields.Selection([("identification","Identification"),("passport","Passport")],"Card Type"), 'card_no' : fields.Char("Card Number"), 'card_exp' : fields.Date("Card Expire"), "app_no": fields.Char("Application No."), "salary": fields.Selection([["20000","5,001-20,000"],["50000","20,001-50,000"],["100000","50,001-100,000"],["100001","100,000+"]], "Salary"), "addresses": fields.One2Many("address","patient_id","Addresses"), "gender": fields.Selection([("male","Male"),("female","Female")],"Gender",required=False), "marital_status": fields.Selection([("single","Single"),("marry","Marry"),("divorce","Divorce"),("separated","Saparated"),("widowed","Widowed")],"Marital Status",required=False), "nation_id": fields.Many2One("clinic.nation","Nationality"), "race_id": fields.Many2One("clinic.race","Race"), "grad_id": fields.Many2One("clinic.graduation","Graduation"), "smoke": fields.Selection([("never","Never"),("stopped","Stopped"),("smoked","Smoked")],"Smoking"), "withdrawal" : fields.Selection([("social_security","Social Security"),("health_insurance","Health Insurance"),("etc","ETC.")],"Right of withdrawal"), "first_treatment" : fields.Selection([("hd","HD"),("test","Test")], "First treatment"), "first_hemodialysis": fields.Date("First time Hemodialysis",required=False), "hemodialysis": fields.Char("First Hemodialysis",required=False), "clinic_after": fields.Selection([("small","Small"),("medium","Medium"),("large","Large")],"Clinic Lastime",required=False), "clinic_after_name": fields.Char("Clinic after name",required=False), "first_permanent_vascular_access": fields.Date("First time Permanent Vascular",required=False), "first_tenckhoff_catheters": fields.Date("First time Tenckhoff Catheters",required=False), "start_date_clinic": fields.Date("Start Date Clinic",required=False), "waiting_transplantation": fields.Selection([("yes","Yes"),("no","No")],"Kidney Transplantation Waiting ?"), "who_transplantation": fields.Char("Who is Transplantation?"), "reason_of_chronic_renal_failure": fields.Char("Reason chronic renal failure ?"), 'causes': fields.One2Many("clinic.patient.cause","patient_id","Cause Line"), 'comorbilities': fields.One2Many("clinic.patient.comorbidity","patient_id","Comorbility"), 'morbilities': fields.One2Many("clinic.patient.morbidity","patient_id","Morbility"), "comments": fields.One2Many("message","related_id","Comments"), "company_id": fields.Many2One("company","Company"), "visits": fields.One2Many("clinic.visit","patient_id","Visits"), "hd_cases": fields.One2Many("clinic.hd.case","patient_id","HD Cases"), "partner_id": fields.Many2One("partner","Contact"), "dialyzers": fields.One2Many("clinic.dialyzer","patient_id","Dialyzers"), "active":fields.Boolean("Uncheck as discountinue", search=True), 'note': fields.Text("Note"), 'categ_id': fields.Many2One("clinic.patient.categ","Category"), 'doctor_id': fields.Many2One("clinic.staff","Doctor",domain=[['type','=','doctor']]), "documents": fields.One2Many("document","related_id","Documents"), 'resign_date': fields.Date("Resign Date"), 'rm_remain_visit': fields.Boolean("Auto Remove Remaining Visit"), 'branch_id': fields.Many2One("clinic.branch","Branch"), 'department_id': fields.Many2One("clinic.department","Department"), } def _get_number(self,context={}): while 1: seq_id=get_model("sequence").find_sequence(name="Clinic Patient") num=get_model("sequence").get_next_number(seq_id,context=context) if not num: return None user_id=get_active_user() set_active_user(1) res=self.search([["number","=",num]]) set_active_user(user_id) if not res: return num get_model("sequence").increment_number(seq_id,context=context) def _get_cause_line(self,context={}): cause_ids=get_model("clinic.cause.chronic").search([]) return cause_ids _defaults={ "number": _get_number, "type": "sc", "reg_date": lambda *a: time.strftime("%Y-%m-%d"), "company_id": lambda *a: get_active_company(), 'card_type': 'identification', "active" : True, } _order="resign_date desc,number desc" def void(self,ids, context={}): obj=self.browse(ids)[0] obj.write({"state":"voided"}) def get_partner_id( self,patient_type,data): partner_obj=get_model("partner") categ_name=PATIENT_TYPE.get(patient_type) categ_ids=get_model("partner.categ").search([['name','=',categ_name]]) if not categ_ids: raise Exception("Category: %s not found"%categ_name) partner_id=None if patient_type in ("mg","sc","uc"): partner_obj=get_model("partner") for partner in partner_obj.search_browse([]): if partner.categ_id.id in categ_ids: partner_id=partner.id print("Found partner ", partner.name, " ", partner.id) break if not partner_id: name=data.get("name","") partner_ids=partner_obj.search([['name','=',name]]) if not partner_ids: partner_id=partner_obj.create({ 'name': name, 'last_name': name, 'type': 'person' }) print("create new partner ", name) else: partner_id=partner_ids[0] return partner_id def onchange_type(self,context={}): data=context['data'] patient_type=data['type'] partner_id=None if patient_type: partner_id=self.get_partner_id(patient_type,data) data['partner_id']=partner_id return data def create(self, vals,**kw): obj_id=super(Patient,self).create(vals,**kw) obj=self.browse(obj_id) partner_id=obj.partner_id if not partner_id: for partner in get_model("partner").search_browse([['name', '=', obj.name]]): if partner.name==obj.name: partner_id=partner.id break if not partner_id: partner_id=get_model("partner").create({ 'name': obj.name, 'last_name': obj.name, 'type': 'person', }) address_id=get_model('address').create({ 'type': 'shipping', 'partner_id': partner_id, 'address': 'your address', 'address2': 'your address2', 'city': 'your city', 'postal_code': 'your zip', 'country_id': 1, }) get_model('address').browse(address_id).write({ 'related_id': "clinic.patient,%s"%obj.id, }) obj.write({ 'partner_id': partner_id, }) return obj_id def delete(self,ids,context={}): partner_ids=[] for obj in self.browse(ids): partner_ids.append(obj.partner_id.id) address_ids=[addr.id for addr in obj.partner_id.addresses] get_model("partner").delete(partner_ids) get_model("address").delete(address_ids) super().delete(ids) def write(self,ids,vals,**kw): if 'active' in vals.keys(): if not vals['active']: vals['resign_date']=time.strftime("%Y-%m-%d") for obj in self.browse(ids): partner_id=obj.partner_id if not partner_id: for partner in get_model("partner").search_browse([['name', '=', obj.name]]): if partner.name==obj.name: partner_id=partner.id break if not partner_id: partner_id=get_model("partner").create({ 'name': obj.name, 'last_name': obj.name, 'type': 'person', }) vals['partner_id']=partner_id if not isinstance(partner_id,int): partner_id=partner_id.id if obj.addresses: addr=obj.addresses[0] get_model("address").browse(addr.id).write({ 'partner_id': partner_id, }) else: if vals.get("addresses"): addr_vals=vals.get("addresses")[0][1] addr_id=get_model("address").create(addr_vals) get_model("address").browse(addr_id).write({ 'patient_id': obj.id, 'partner_id': partner_id, }) del vals['addresses'] print("create address for %s"%obj.name, ' ', addr_vals) if obj.rm_remain_visit: visit_ids=get_model('clinic.visit').search([['patient_id','=',obj.id],['state','=','draft']]) get_model('clinic.visit').delete(visit_ids) print('remove visit auto %s'%visit_ids) super().write(ids,vals,**kw) def name_get(self,ids,context={}): vals=[] for obj in self.browse(ids): name="%s %s %s"%(obj.number,obj.name, obj.hn) vals.append((obj.id,name)) return vals def name_search(self,name,domain=None,context={},**kw): dom=[["number","ilike","%"+name+"%"]] if domain: dom=[dom,domain] ids1=self.search(dom) dom=[["name","ilike","%"+name+"%"]] if domain: dom=[dom,domain] ids2=self.search(dom) dom=[["hn","ilike","%"+name+"%"]] if domain: dom=[dom,domain] ids3=self.search(dom) ids=list(set(ids1+ids2+ids3)) return self.name_get(ids,context=context) def simple_address(self,ids,context={}): print("call simple address ") for obj in self.browse(ids): if not obj.addresses: address_id=get_model("address").create({ 'patient_id': obj.id, 'partner_id': obj.partner_id.id, 'type': 'shipping', 'address': 'xxxx', 'address2': 'xxxx', 'city': 'BKK', 'country_id': 1, 'postal_code': '12000', }) if context.get('called'): return obj.id return { 'next': { 'name': 'clinic_patient', 'mode': 'form', 'active_id': obj.id, },'flash': 'Simple Address OK', } def new_dialyzer(self,ids,context={}): dlz_id=None for obj in self.browse(ids): dlz_vals=get_model("clinic.dialyzer").default_get() dlz_vals['patient_id']=obj.id dlz_vals['company_id']=dlz_vals['company_id'][0] dlz_vals['product_id']=dlz_vals['product_id'][0] dlz_id=get_model('clinic.dialyzer').create(dlz_vals) dialyzer=get_model("clinic.dialyzer").browse(dlz_id) dialyzer.confirm() if context.get('called'): return dlz_id return { 'next': { 'name': 'clinic_patient', 'mode': 'form', 'active_id': obj.id, },'flash': 'New Dialyzer successfully', } Patient.register()