import time import datetime from netforce.model import Model, fields, get_model from netforce.access import get_active_company, get_active_user class Visit(Model): _name="clinic.visit" _string="Visit" _audit_log=True _name_field="number" _multi_company=True _fields={ "number": fields.Char("Number",required=True,search=True), "patient_id": fields.Many2One("clinic.patient","Patient",required=True,search=True), "doctor_id": fields.Many2One("clinic.doctor","Doctor",search=True), "nurse_id": fields.Many2One("clinic.nurse","Nurse",search=True), "visit_date": fields.Date("Visit Date",required=True,search=True), "department_id": fields.Many2One("clinic.department", "Department",search=True), "cycle": fields.Selection([("1","One"),("2","Two"),("3","Tree"),("4","Four")],"Cycle",required=True), "state": fields.Selection([("pending","Pending"),("confirmed","Confirmed"),("cancelled","Cancelled")],"Status",required=True), "comments": fields.One2Many("message","related_id","Comments"), "company_id": fields.Many2One("company","Company"), } def _get_number(self,context={}): while 1: num=get_model("sequence").get_number("sale_order") if not num: return None res=self.search([["number","=",num]]) if not res: return num get_model("sequence").increment("sale_order") def _get_nurse(self,context={}): user_id=get_active_user() print("user_id ",user_id) nurse_ids=get_model("clinic.nurse").search([['user_id','=',user_id]]) if nurse_ids: return nurse_ids[0] return None _defaults={ "state": "pending", "cycle": "1", 'visit_date': time.strftime("%Y-%m-%d"), "number": _get_number, "company_id": lambda *a: get_active_company(), 'nurse_id': _get_nurse, } _order="date,cycle" def confirm(self,ids,context={}): obj=self.browse(ids)[0] hd_case_obj=get_model("clinic.hd.case") dt=datetime.datetime fmt_date="%Y-%m-%d %H:%M:%S" timenow=dt.now().strftime("%H:%M:%S") date_from=dt.strptime("%s %s"%(obj.visit_date,timenow),fmt_date) to=3600 date_to=date_from+datetime.timedelta(seconds=to) date_to=date_to.strftime(fmt_date) date_from=date_from.strftime(fmt_date) hd_case_id=hd_case_obj.create({ 'patient_id': obj.patient_id.id, 'doctor_id': obj.doctor_id.id, 'nurse_id': obj.nurse_id.id, 'department_id': obj.department_id.id, 'date_start': date_from, 'date_stop': date_to, 'visit_id': obj.id, }) obj.write({"state":"confirmed"}) return { 'next': { 'name': 'clinic_hd_case', 'mode': 'form', 'active_id': hd_case_id, }, 'flash': 'Visit %s confirmed'%obj.number, } def cancel(self,ids,context={}): obj=self.browse(ids)[0] obj.write({"state":"cancelled"}) def reopen(self,ids,context={}): obj=self.browse(ids)[0] obj.write({"state":"pending"}) def onchange_patient(self,context={}): data=context['data'] patient_id=data['patient_id'] visits=self.search_browse([['patient_id','=',patient_id]]) if visits: visit=visits[-1] data['doctor_id']=visit.doctor_id.id data['department_id']=visit.department_id.id else: data['patient_id']=None data['department_id']=None return data Visit.register()