import time from datetime import datetime from calendar import monthrange from netforce.model import Model, fields, get_model from netforce.access import get_active_user, set_active_user from . import utils class PatientMove(Model): _name="clinic.patient.move" _fields={ 'patient_id': fields.Many2One('clinic.patient','Patient',search=True), 'date': fields.Date("Date", required=True,search=True), 'location_from_id': fields.Many2One("clinic.department","Location From",search=True), 'location_to_id': fields.Many2One("clinic.department","Location To",search=True), "state": fields.Selection([['normal','Normal'],['new','New'],['dispose','Dispose']], "State"), 'note': fields.Text("Note",search=True), } _defaults={ 'date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"), 'state': 'normal', } def get_data(self, date, department_id, context={}): if not date: raise Exception("Missing date!") y,m,d=date.split("-") y=int(y) m=int(m) weekday, day_month=monthrange(y, m) cond1=[ ['date','>=','%s-%s-01'%(y,str(m).zfill(2))], ['date','<=','%s-%s-%s'%(y,str(m).zfill(2),day_month)], ['location_to_id','=', department_id], ] cond=cond1+[['state','=','normal']] ids=self.search(cond) prev_month=len(ids) cond=cond1+[['state','=','new']] ids=self.search(cond) current_month=len(ids) current_items=[] for index, val in enumerate(self.read(ids)): val['no']=index+1 current_items.append(val) cond=cond1+[['state','=','dispose']] ids=self.search(cond) dispose_month=len(ids) dispose_items=[] for index, val in enumerate(self.read(ids)): val['no']=index+1 dispose_items.append(val) next_month=prev_month+current_month-dispose_month prev_m=m-1 if prev_m <= 0: prev_m=1 next_m=m+1 if next_m > 12: next_m=1 prev_month_thai=utils.MONTHS['th_TH'][prev_m] current_month_thai=utils.MONTHS['th_TH'][m] dispose_month_thai=utils.MONTHS['th_TH'][m] next_month_thai=utils.MONTHS['th_TH'][next_m] res={ 'prev_month': prev_month, 'prev_month_thai': prev_month_thai, 'current_month': current_month, 'current_month_thai': current_month_thai, 'current_items': current_items, 'dispose_month': dispose_month, 'dispose_month_thai': dispose_month_thai, 'dispose_items': dispose_items, 'next_month': next_month, 'next_month_thai': next_month_thai, } return res def auto_get_data(self, date, department_id, context={}): user_id=get_active_user() set_active_user(1) datenow=time.strftime("%Y-%m-%d") if not date: raise Exception("Missing date!") if not department_id: raise Exception("Missing department!") y,m,d=date.split("-") weekday, day_month=monthrange(int(y), int(m)) cond=[ ['date','>=','%s-%s-01'%(y,m)], ['date','<=','%s-%s-%s'%(y,m,day_month)], ['location_to_id','=',department_id], ] res=self.search(cond) #XXX reset data if res and date > datenow: ids=self.search(cond) print("reset data...") print(ids) self.delete(ids) res=None # copy data from previous month if not res: y=int(y) m=int(m)-1 if m<=0: m=1 y-=1 weekday, day_month=monthrange(int(y), int(m)) cond=[ ['date','>=','%s-%s-01'%(y,m)], ['date','<=','%s-%s-%s'%(y,m,day_month)], ['location_to_id','=',department_id], ] res=self.search_browse(cond) for obj in res: if obj.state=='dispose': continue vals={ 'location_from_id': obj.location_from_id.id, 'location_to_id': obj.location_to_id.id, 'patient_id': obj.patient_id.id, 'date': date, } self.create(vals) # copy from patient if not res: cond=[ ['walkin','=','no'], ['dispose','=',False], ] locations={ 'LS-FL1': 1, 'LS-FL2': 2, 'LS-FL3': 3, 'SS-FL3': 4, 'SS-FL4': 5, } for obj in get_model("clinic.patient").search_browse(cond): if not obj.location: continue locs=obj.location.split(",") vals={ 'location_from_id': locations[locs[0]], #first 'location_to_id': locations[locs[-1]], #last 'patient_id': obj.id, 'note': obj.note, 'date': date, } self.create(vals) set_active_user(user_id) PatientMove.register()