import time from netforce.model import Model, fields, get_model from netforce.access import get_active_company, get_active_user, set_active_user class Staff(Model): _name="clinic.staff" _string="Staff" _audit_log=True _multi_company=True 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 def _get_level(self,ids,context={}): res={} for obj in self.browse(ids): level_id=None for rotate in obj.rotations: level_id=rotate.level_id.id res[obj.id]=level_id return res def _get_cycle(self,ids,context={}): res={} for obj in self.browse(ids): cycle_id=None for line in obj.cycles: cycle=line.cycle_id cycle_id=cycle.id res[obj.id]=cycle_id return res def _get_base(self,ids,context={}): res={} for obj in self.browse(ids): wage=0 max_cycle=0 ot_per_cycle=0 for rotate in obj.rotations: wage=rotate.wage max_cycle=rotate.max_cycle ot_per_cycle=rotate.ot_per_cycle or 0 res[obj.id]={ 'wage': wage, 'max_cycle': max_cycle, 'ot_per_cycle': ot_per_cycle, } return res def _get_hdcase(self,ids,context={}): # problem slow to load res={} for obj in self.browse(ids): hids=[] if obj.type=='doctor': hids=get_model("clinic.hd.case.staff").search([['staff_id','=',obj.id]]) elif obj.type=='nurse': item_ids=[item['cycle_item_id'][0] for item in get_model("clinic.cycle.item.line").search_read([['nurse_id','=',obj.id]],['cycle_item_id'])] for item in get_model("clinic.cycle.item").browse(item_ids): for hdcase in item.hd_cases: hids.append(hdcase.id) res[obj.id]=hids return res def _get_name(self,ids,context={}): res={} for obj in self.browse(ids): name='' title=obj.title_id if title: title_name=title.name or "" title_name=title_name.replace(" ","") if title_name.lower()!='notitle': name+=obj.title_id.name or "" if obj.first_name: name+=obj.first_name or "" name+=" " if obj.last_name: name+=obj.last_name or "" if not obj.active: name+='not_use' elif context.get('active'): name+='not_use' res[obj.id]={ 'name': name, 'name_check': name.replace(" ",""), # remove all space for make sure } return res _fields={ 'employee_id': fields.Many2One("hr.employee","Employee"), "number": fields.Char("Number",required=True,search=True), "title_id": fields.Many2One("clinic.name.title","Title"), "first_name": fields.Char("First Name"), "last_name": fields.Char("Last Name"), "name": fields.Char("Name",function="_get_name",function_multi=True,store=True,required=True,search=True), "name_check": fields.Char("Name",function="_get_name",function_multi=True,store=True), "name_eng": fields.Char("Eng Name",search=True), "nick_name": fields.Char("Nick Name",search=True), "identification" : fields.Char("Identification Card"), "expiry_card" : fields.Date("Expired Card"), "birthday": fields.Date("Birthday",search=True), "age": fields.Integer("Age", function="_get_age"), "state": fields.Selection([["part_time","Part Time"],["full_time","Full Time"]],"Working Status",search=True), "type": fields.Selection([['staff','Staff'],["doctor","Doctor"],["nurse","Nurse"]],"Type"), "gender": fields.Selection([["male","Male"],["female","Female"]],"Gender"), "nation_id": fields.Many2One("clinic.nation","Nationality"), "mobile": fields.Char("Mobile",required=False,search=True), "phone": fields.Char("Phone",search=True), 'email': fields.Char("Email"), "prof_license" : fields.Char("License No."), "prof_license_date" : fields.Date("License Date"), "prof_license_expiry" : fields.Date("Expired License"), "birthday": fields.Date("BirthDay",search=True), "department_id": fields.Many2One("clinic.department", "Department",search=True), "patients": fields.One2Many("clinic.patient","doctor_id","Patients"), "addresses": fields.One2Many("address","staff_id","Addresses"), "comments": fields.One2Many("message","related_id","Comments"), "nurse_visits": fields.One2Many("clinic.visit","nurse_id","Visits"), "doctor_visits": fields.One2Many("clinic.visit","doctor_id","Visits"), "company_id": fields.Many2One("company","Company",search=True), "user_id": fields.Many2One("base.user","User",search=True), 'image': fields.File("Image"), 'note': fields.Text("Note"), 'categ_id': fields.Many2One("clinic.staff.categ", "Category",search=True), 'level_id': fields.Many2One("clinic.staff.level", "Level", function="_get_level"), 'active': fields.Boolean("Active"), 'date': fields.Date("Register Date"), 'wage': fields.Float("Wage", function="_get_base",function_multi=True), 'max_cycle': fields.Integer("Max Cycle", function="_get_base",function_multi=True), 'ot_per_cycle': fields.Integer("OT Per Cycle", function="_get_base",function_multi=True), 'check_max_cycle': fields.Boolean("Check Max Cycle"), 'hire_date': fields.Date("Hire Date"), 'resign_date': fields.Date("Resign Date"), "documents": fields.One2Many("document","related_id","Documents"), "rotations": fields.One2Many("clinic.staff.rotation","staff_id","Staff Rotation"), "cycles": fields.One2Many("clinic.staff.cycle","staff_id","Cycles"), 'cycle_id': fields.Many2One('clinic.cycle','Last Cycle',function="_get_cycle"), "hd_case_staffs": fields.One2Many("clinic.hd.case.staff","staff_id","HD Cases"), "hd_cases": fields.Many2Many("clinic.hd.case","HD Cases",function="_get_hdcase"), # not need to use (it's slow to load) "cycle_item_nurses": fields.One2Many("clinic.cycle.item.line","nurse_id","Cycle Items"), 'branch_id': fields.Many2One("clinic.branch","Branch", search=True), "partner_id": fields.Many2One("partner","Contact"), 'departments': fields.Many2Many("clinic.department","Departments"), } def _get_number(self,context={}): while 1: seq_id=get_model("sequence").find_sequence(name="Clinic Staff") if not seq_id: return "/" 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_branch(self,context={}): b_ids=get_model('clinic.branch').search([]) if b_ids: return b_ids[0] _defaults={ 'active': True, "state": "part_time", "type": lambda self,ctx: ctx.get("type") or "staff", "date": lambda *a: time.strftime("%Y-%m-%d"), "number": _get_number, "company_id": lambda *a: get_active_company(), 'branch_id': _get_branch, } #_key=["name_check","branch_id"] #not working _sql_constraints=("clinic_staff_key_uniq","unique(name_check,branch_id)","name should be unique"), _order="date desc,number desc" def create_contact(self,name): partner_id=get_model("partner").create({ 'name': name, 'last_name': name, 'type': 'person', 'is_staff': True, }) 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, }) return partner_id, address_id def check_contact(self,name=""): partner_id=None dom=[ ['name','=',name], ['is_staff','=',True], ] for partner in get_model("partner").search_browse(dom): if partner.name==name: partner_id=partner.id break return partner_id def check_emp(self,name="", employee_id=None): names=name.split(" ") first_name=names[0] last_name=names[-1] if first_name==last_name: first_name="" vals={ 'first_name': first_name, 'last_name': last_name, } emp_obj=get_model("hr.employee") emp_ids=emp_obj.search([['last_name','=',last_name]]) emp_id=None if not emp_ids and not employee_id: emp_id=emp_obj.create(vals) elif employee_id: emp_obj.browse(employee_id).write(vals) emp_id=employee_id else: emp_id=emp_ids[0] return emp_id def create(self,vals,**kw): name=vals['name'] employee_id=vals.get('employee_id') emp_id=self.check_emp(name,employee_id) if emp_id: vals['employee_id']=emp_id partner_id=get_model("clinic.staff").check_contact(name) address_id=None if not partner_id: vals['partner_id'],address_id=self.create_contact(name) new_id=super().create(vals,**kw) self.function_store([new_id]) if address_id: addr=get_model('address').browse(address_id) addr.write({ 'partner_id': vals['partner_id'], 'staff_id': new_id, }) return new_id def write(self,ids,vals,**kw): obj=self.browse(ids)[0] name=vals.get("name","") if not name: name=obj.name emp_id=self.check_emp(name,obj.employee_id.id) if emp_id: vals['employee_id']=emp_id partner_id=get_model("clinic.staff").check_contact(name) address_id=None if not partner_id: vals['partner_id'],address_id=self.create_contact(name) super().write(ids,vals,**kw) self.function_store(ids) if address_id: addr=get_model('address').browse(address_id) addr.write({ 'partner_id': vals['partner_id'], 'staff_id': ids[0], }) def name_get(self,ids,context={}): vals=[] for obj in self.browse(ids): level=obj.level_id name=obj.name or "" nickname=obj.nick_name or "" if level: if nickname: name+=" (%s)"%(nickname) name+=" - %s"%(level.name or "") vals.append((obj.id,name)) return vals def name_search(self,name,domain=None,condition=[],context={},**kw): dom=[["name","ilike","%"+name+"%"]] if domain: dom=[dom,domain] ids1=self.search(dom) dom=[["name","ilike","%"+name+"%"]] level_ids=get_model('clinic.staff.level').search(dom) ids2=[] for x in self.search_read([domain],['level_id']): if x['level_id']: level_id=x['level_id'][0] if level_id in level_ids: ids2.append(x['id']) dom=[["nick_name","ilike","%"+name+"%"]] if domain: dom=[dom,domain] ids3=self.search(dom) ids=list(set(ids1+ids2+ids3)) return self.name_get(ids,context=context) Staff.register()