From f472cea02d239589115463c900a9ea35e9544963 Mon Sep 17 00:00:00 2001 From: "watcha.h" Date: Fri, 13 Mar 2015 13:43:12 +0700 Subject: [PATCH] multi deparment --- .../layouts/clinic_department_form.xml | 2 +- .../layouts/clinic_hd_case_form.xml | 2 +- .../layouts/clinic_patient_form.xml | 2 +- .../layouts/clinic_patient_list.xml | 2 +- .../layouts/clinic_select_company.xml | 2 +- netforce_clinic/models/cycle_item.py | 14 +-- netforce_clinic/models/department.py | 2 +- netforce_clinic/models/hd_case.py | 7 +- netforce_clinic/models/patient.py | 12 ++ .../models/report_discontinue_patient.py | 6 + .../models/report_hd_case_summary.py | 21 +++- .../models/report_medical_summary.py | 17 +-- .../models/report_recent_patient.py | 6 + netforce_clinic/models/select_company.py | 32 +++++- netforce_clinic/models/setting.py | 103 ++++++++++++++++-- netforce_clinic/models/visit.py | 2 + netforce_clinic/models/visit_board.py | 36 +++--- 17 files changed, 206 insertions(+), 62 deletions(-) diff --git a/netforce_clinic/layouts/clinic_department_form.xml b/netforce_clinic/layouts/clinic_department_form.xml index 82432e2..8326513 100644 --- a/netforce_clinic/layouts/clinic_department_form.xml +++ b/netforce_clinic/layouts/clinic_department_form.xml @@ -1,7 +1,7 @@
diff --git a/netforce_clinic/layouts/clinic_hd_case_form.xml b/netforce_clinic/layouts/clinic_hd_case_form.xml index 6433f71..bdfe437 100644 --- a/netforce_clinic/layouts/clinic_hd_case_form.xml +++ b/netforce_clinic/layouts/clinic_hd_case_form.xml @@ -19,7 +19,7 @@ - + diff --git a/netforce_clinic/layouts/clinic_patient_form.xml b/netforce_clinic/layouts/clinic_patient_form.xml index ee5e1be..186da2b 100644 --- a/netforce_clinic/layouts/clinic_patient_form.xml +++ b/netforce_clinic/layouts/clinic_patient_form.xml @@ -19,7 +19,7 @@ - + diff --git a/netforce_clinic/layouts/clinic_patient_list.xml b/netforce_clinic/layouts/clinic_patient_list.xml index 03a2d58..1fb5a87 100644 --- a/netforce_clinic/layouts/clinic_patient_list.xml +++ b/netforce_clinic/layouts/clinic_patient_list.xml @@ -10,7 +10,7 @@ - + diff --git a/netforce_clinic/layouts/clinic_select_company.xml b/netforce_clinic/layouts/clinic_select_company.xml index 989d858..5e185b6 100644 --- a/netforce_clinic/layouts/clinic_select_company.xml +++ b/netforce_clinic/layouts/clinic_select_company.xml @@ -1,5 +1,5 @@ - + diff --git a/netforce_clinic/models/cycle_item.py b/netforce_clinic/models/cycle_item.py index 99bfd00..016fad4 100644 --- a/netforce_clinic/models/cycle_item.py +++ b/netforce_clinic/models/cycle_item.py @@ -2,7 +2,6 @@ import time from netforce.model import Model, fields, get_model from netforce.access import get_active_company, get_active_user -from netforce.utils import get_data_path class CycleItem(Model): _name="clinic.cycle.item" @@ -52,14 +51,15 @@ class CycleItem(Model): } def _get_branch(self,context={}): - b_ids=get_model('clinic.branch').search([]) - if b_ids: - return b_ids[0] + res=get_model('select.company').get_select() + if res: + return res['branch_id'] def _get_department(self,context={}): - dpt_ids=get_model('clinic.department').search([]) - if dpt_ids: - return dpt_ids[0] + res=get_model('select.company').get_select() + if res: + return res['department_id'] + _defaults={ 'state': 'draft', diff --git a/netforce_clinic/models/department.py b/netforce_clinic/models/department.py index b93d27c..407feb5 100644 --- a/netforce_clinic/models/department.py +++ b/netforce_clinic/models/department.py @@ -79,7 +79,7 @@ class Department(Model): if not profile_id: raise Exception("Profile not found") models=[ - ['clinic.patient','department_id.code','='], + ['clinic.patient','location','ilike'], ['clinic.patient.cycle','department_id.code','='], ['clinic.staff','location','ilike'], ['clinic.staff.rotation','staff_id.department_id.code','='], diff --git a/netforce_clinic/models/hd_case.py b/netforce_clinic/models/hd_case.py index d2f97c7..4650534 100644 --- a/netforce_clinic/models/hd_case.py +++ b/netforce_clinic/models/hd_case.py @@ -623,14 +623,15 @@ class HDCase(Model): if line.qty < 1: continue prod=line.product_id + if prod.type != 'stock': + continue # check orginal product prod_code=prod.code.split("-")[0] if len(prod_code)>1: prods=get_model('product').search_browse(['code','=',prod_code]) + if not prods: + raise Exception("Can not create good issue: product code %s is not found!"%prod_code) prod=prods[0] - if prod.type != 'stock': - continue - #XXX if prod_ids and prod.id not in prod_ids or prod.id in prod_exist_ids: continue prod_exist_ids.append(prod.id) diff --git a/netforce_clinic/models/patient.py b/netforce_clinic/models/patient.py index 1e5d11e..d7bebbe 100644 --- a/netforce_clinic/models/patient.py +++ b/netforce_clinic/models/patient.py @@ -64,6 +64,17 @@ class Patient(Model): } return res + def _get_location(self,ids,context={}): + res={} + for obj in self.browse(ids): + dpt_codes=set() + for cline in obj.cycles: + dpt_codes.update({cline.department_id.code}) + if not dpt_codes: + dpt_codes=[obj.department_id.code] + res[obj.id]=','.join([code for code in dpt_codes if code]) + return res + _fields={ "number": fields.Char("HN Number",required=True,search=True), "trt_no": fields.Char("TRT",search=True), @@ -134,6 +145,7 @@ class Patient(Model): 'state': fields.Selection([['admit','Admit'],['dispose','Dispose']],'State'), 'walkin': fields.Selection([['yes','Yes'],['no','No']],"Walk In"), 'departments': fields.Many2Many("clinic.department","Departments"), + 'location': fields.Char("Location",function="_get_location",store=True), #to filter } def _get_number(self,context={}): diff --git a/netforce_clinic/models/report_discontinue_patient.py b/netforce_clinic/models/report_discontinue_patient.py index 3f98d83..80aebe4 100644 --- a/netforce_clinic/models/report_discontinue_patient.py +++ b/netforce_clinic/models/report_discontinue_patient.py @@ -33,6 +33,12 @@ class ReportDiscontinuePatient(Model): department_id=defaults.get('department_id',None) if department_id: department_id=int(department_id) + select_apt=get_model('select.company').get_select() + if select_apt: + if not branch_id: + branch_id=select_apt['branch_id'] + if not department_id: + department_id=select_apt['department_id'] res={ 'date': date, 'date_from': date_from, diff --git a/netforce_clinic/models/report_hd_case_summary.py b/netforce_clinic/models/report_hd_case_summary.py index b754912..dea5912 100644 --- a/netforce_clinic/models/report_hd_case_summary.py +++ b/netforce_clinic/models/report_hd_case_summary.py @@ -30,10 +30,22 @@ class ReportHDCaseSummary(Model): weekday, total_day=monthrange(int(year), int(month)) return "%s-%s-%s"%(year,month,total_day) + def _get_branch(self,context={}): + res=get_model('select.company').get_select() + if res: + return res['branch_id'] + + def _get_department(self,context={}): + res=get_model('select.company').get_select() + if res: + return res['department_id'] + _defaults={ 'date': lambda *a: time.strftime("%Y-%m-%d"), 'date_from': _get_date_from, 'date_to': _get_date_to, + 'branch_id': _get_branch, + 'department_id': _get_department, } def get_report_data(self,ids,context={}): @@ -44,8 +56,13 @@ class ReportHDCaseSummary(Model): year=int(date[0:4]) crr_month=int(date[5:7]) weekday, crr_total_day=monthrange(year, crr_month) - branch_id=None - department_id=None + defaults=self.default_get(context=context) + branch_id=defaults.get("branch_id",None) + if branch_id: + branch_id=branch_id[0] + department_id=defaults.get("department_id",None) + if department_id: + department_id=department_id[0] time_start='%s-%s-01 00:00:00'%(year,str(crr_month).zfill(2)) time_stop='%s-%s-%s 23:59:59'%(year,str(crr_month).zfill(2),crr_total_day) if ids: diff --git a/netforce_clinic/models/report_medical_summary.py b/netforce_clinic/models/report_medical_summary.py index b04f0c9..e289915 100644 --- a/netforce_clinic/models/report_medical_summary.py +++ b/netforce_clinic/models/report_medical_summary.py @@ -21,7 +21,6 @@ class ReportMedicalSummary(Model): # in case link from another report def default_get(self,field_names=None,context={},**kw): - user_id=get_active_user() defaults=context.get("defaults",{}) date=defaults.get('date',time.strftime("%Y-%m-%d")) year,month=time.strftime("%Y-%m").split("-") @@ -34,21 +33,15 @@ class ReportMedicalSummary(Model): if categ_ids: categ_id=categ_ids[0] branch_id=defaults.get('branch_id',None) + select_dpt=get_model('select.company').get_select() if not branch_id: - staff=get_model("clinic.staff").search_browse([['user_id','=',user_id]]) - if staff: - branch_id=staff[0].branch_id.id + if select_dpt: + branch_id=select_dpt['branch_id'] else: branch_id=int(branch_id or "0") department_id=defaults.get('department_id',None) if not department_id: - dom=[] - dom.append(['user_id','=',user_id]) - if branch_id: - dom.append(['branch_id','=',branch_id]) - staff=get_model("clinic.staff").search_browse(dom) - if staff: - department_id=staff[0].department_id.id + department_id=select_dpt['department_id'] else: department_id=int(department_id or "0") res={ @@ -106,7 +99,7 @@ class ReportMedicalSummary(Model): if department_id: dom.append(['department_id','=',department_id]) for hd_case in get_model('clinic.hd.case').search_browse(dom): - patient_type_id=hd_case.patient_id.type_id.id + patient_type_id=hd_case.patient_type_id.id for line in hd_case.lines: prod=line.product_id prod_code=prod.code or "" diff --git a/netforce_clinic/models/report_recent_patient.py b/netforce_clinic/models/report_recent_patient.py index 2a03765..320355c 100644 --- a/netforce_clinic/models/report_recent_patient.py +++ b/netforce_clinic/models/report_recent_patient.py @@ -32,6 +32,12 @@ class ReportRecentPatient(Model): department_id=defaults.get('department_id',None) if department_id: department_id=int(department_id) + select_apt=get_model('select.company').get_select() + if select_apt: + if not branch_id: + branch_id=select_apt['branch_id'] + if not department_id: + department_id=select_apt['department_id'] res={ 'date': date, 'date_from': date_from, diff --git a/netforce_clinic/models/select_company.py b/netforce_clinic/models/select_company.py index fc08a4b..dc7bbaf 100644 --- a/netforce_clinic/models/select_company.py +++ b/netforce_clinic/models/select_company.py @@ -1,5 +1,6 @@ from netforce.model import Model, fields, get_model from netforce.access import get_active_user, set_active_user +from netforce.database import get_connection class SelectCompany(Model): _inherit="select.company" @@ -67,10 +68,16 @@ class SelectCompany(Model): def get_departments(self,context={}): user_id=get_active_user() + set_active_user(1) dpt_ids=[] - for st in get_model("clinic.staff").search_browse(['user_id','=',user_id]): - for dpt in st.departments: - dpt_ids.append(dpt.id) + user=get_model("base.user").browse(user_id) + if user.department_profile_id: + for dpt in user.department_profile_id.departments: + dpt_ids.append(dpt.id) + if not dpt_ids: + for st in get_model("clinic.staff").search_browse(['user_id','=',user_id]): + for dpt in st.departments: + dpt_ids.append(dpt.id) dom=[] if dpt_ids: dom.append(['id','in',dpt_ids]) @@ -99,5 +106,24 @@ class SelectCompany(Model): set_active_user(user_id) res=super().select(ids,context) return res + + def get_select(self,context={}): + user_id=get_active_user() + db=get_connection() + department_id=None + branch_id=None + count=0 + for r in db.query("select department from select_company where write_uid=%s order by id desc",user_id): + for dpt in get_model("clinic.department").search_browse([['name','=',r['department']]]): + department_id=dpt.id + branch_id=dpt.branch_id.id + count+=1 + break + if count<1: + return {} + return { + 'department_id': department_id, + 'branch_id': branch_id, + } SelectCompany.register() diff --git a/netforce_clinic/models/setting.py b/netforce_clinic/models/setting.py index d1b45c8..d4d9a34 100644 --- a/netforce_clinic/models/setting.py +++ b/netforce_clinic/models/setting.py @@ -1,8 +1,21 @@ +from datetime import datetime + + from netforce.model import Model, fields, get_model from netforce.utils import get_data_path from netforce.access import get_active_company, get_active_user, set_active_user from netforce.database import get_connection +DAYS={ + 0: 'mon', + 1: 'tue', + 2: 'wed', + 3: 'thu', + 4: 'fri', + 5: 'sat', + 6: 'sun', + } + class ClinicSetting(Model): _name="clinic.setting" _string="Setting" @@ -136,18 +149,89 @@ class ClinicSetting(Model): if user_id !=1: print("Only admin!!") return - dpt_ids=get_model("clinic.department").search([]) + ### udpate working location for staff + staffs={} + for hdcase in get_model('clinic.hd.case').search_browse([]): + dpt=hdcase.department_id + doctor=hdcase.doctor_id + citem=hdcase.cycle_item_id + if not citem: + continue + for cline in citem.lines: + nurse=cline.nurse_id + if nurse.id not in staffs.keys(): + staffs[nurse.id]=set() + staffs[nurse.id].update({dpt.id}) + if doctor.id not in staffs.keys(): + staffs[doctor.id]=set() + staffs[doctor.id].update({dpt.id}) + for st_id, dpt_ids in staffs.items(): + if not st_id: + continue + st=get_model('clinic.staff').browse(st_id) + olds=[dpt.id for dpt in st.departments] + for dpt_id in dpt_ids: + if dpt_id not in olds: + st.write({ + 'departments': [('add',[dpt_id])], + }) + #### update patient location for pt in get_model("clinic.patient").search_browse([]): - dpt2_ids=set() - for vst in pt.visits: - if vst.department_id: - dpt2_ids.update({(vst.department_id.id, vst.cycle_id.id)}) # get day - print('dpt2_ids ', dpt2_ids) - if not pt.departments: + pt.write({ + 'note': ' ', + }) + #### update department + #### by visit + dpt_ids=get_model("clinic.department").search([]) + fmt='%Y-%m-%d' + for pt in get_model("clinic.patient").search_browse([]): + cycles={} + print('len ', len(pt.visits)) + for hd in pt.hd_cases: + if hd.department_id: + date=hd.date + wd=datetime.strptime(date,fmt).weekday() + key='%s-%s-%s'%(hd.department_id.id, hd.cycle_id.id, wd) + if not key in cycles.keys(): + cycles[key]={ + 'department_id': hd.department_id.id, + 'cycle_id': hd.cycle_id.id, + 'day': wd, + } + print('cycles ', cycles) + if not pt.cycles and cycles: + clines=[] + for key, cycle_vals in cycles.items(): + clines.append(('create',cycle_vals)), pt.write({ - #'cycles': [['set',dpt2_ids]], + 'cycles': clines, }) - print('set department for ', pt.name) + print('update cycle for ', pt.name) + elif pt.cycles: + continue #XXX + clines=[] + x1=set(cycles.keys()) + x2=set() + for pcycle in pt.cycles: + pcycle_id=pcycle.cycle_id.id + pdpt_id=pcycle.department_id.id + wd=pcycle.day + key2='%s-%s-%s'%(pdpt_id,pcycle_id, wd) + x2.update({key2}) + for cvals in (x1.difference(x2)): + dpt_id, cycle_id, day=cvals.split("-") + clines.append(('create', { + 'department_id': int(dpt_id), + 'cycle_id': int(cycle_id), + 'day': day, + })) + if clines: + print("---- > update the rest ", clines) + pt.write({ + 'cycles': clines, + }) + else: + pass for st in get_model("clinic.staff").search_browse([]): if not st.departments: st.write({ @@ -158,6 +242,7 @@ class ClinicSetting(Model): print('Done!') return + ### remove douplicate visit visits={} for visit in get_model("clinic.visit").search_browse([]): key='%s-%s'%(visit.visit_date, visit.patient_id.id) diff --git a/netforce_clinic/models/visit.py b/netforce_clinic/models/visit.py index 874bf12..055a198 100644 --- a/netforce_clinic/models/visit.py +++ b/netforce_clinic/models/visit.py @@ -464,6 +464,8 @@ class Visit(Model): def cancel(self,ids,context={}): obj=self.browse(ids)[0] + for hdcase in obj.hd_cases: + hdcase.cancelled() obj.write({ 'state': 'cancelled', }) diff --git a/netforce_clinic/models/visit_board.py b/netforce_clinic/models/visit_board.py index 9d45688..3ef5906 100644 --- a/netforce_clinic/models/visit_board.py +++ b/netforce_clinic/models/visit_board.py @@ -38,26 +38,20 @@ class VisitBoard(Model): } def _get_branch(self,context={}): - user_id=get_active_user() - sts=get_model("clinic.staff").search_browse([['user_id','=',user_id]]) - branch_id=None - if sts: - branch_id=sts[0].branch_id.id - return branch_id + res=get_model('select.company').get_select() + if res: + return res['branch_id'] - def _get_deparment(self,context={}): - user_id=get_active_user() - sts=get_model("clinic.staff").search_browse([['user_id','=',user_id]]) - dpt_id=None - if sts: - dpt_id=sts[0].department_id.id - return dpt_id + def _get_department(self,context={}): + res=get_model('select.company').get_select() + if res: + return res['department_id'] _defaults={ 'date': lambda *a: time.strftime("%Y-%m-%d"), 'date_from': lambda *a: time.strftime("%Y-%m-%d"), 'date_to': lambda *a: (datetime.now()+timedelta(days=DRT)).strftime("%Y-%m-%d"), - 'department_id': _get_deparment, + 'department_id': _get_department, 'branch_id': _get_branch, } @@ -69,15 +63,17 @@ class VisitBoard(Model): patient_id=None cycle_id=None doctor_id=None - department_id=None - branch_id=None + defaults=self.default_get(context=context) + department_id=defaults.get("department_id",None) + if department_id: + department_id=department_id[0] + branch_id=defaults.get("branch_id",None) + if branch_id: + branch_id=branch_id[0] user_id=get_active_user() set_active_user(1) #FIXME to allow user to see doctor different department - sts=get_model("clinic.staff").search_browse([['user_id','=',user_id]]) - if sts: - department_id=sts[0].department_id.id - branch_id=sts[0].branch_id.id + if ids: obj=self.browse(ids)[0] date_from=obj.date_from