from datetime import datetime, timedelta from netforce.model import Model, fields, get_model from netforce.access import get_active_company, get_active_user class Schedule(Model): _name="clinic.schedule" _string="Schedule" _field_name="date" def _get_name(self,ids,context={}): res={} for obj in self.browse(ids): st="\n" # XXX count=0 for line in obj.lines: nurse=line.nurse_id st+='%s %s\n'%(nurse.name, nurse.level_id.name or "") count+=1 res[obj.id]='%s'%(st if count else 'No nurse') return res def _get_item(self,ids,context={}): res={} for obj in self.browse(ids): cycle_id=None if obj.cycle_id: cycle_id=obj.cycle_id.id item_ids=get_model("clinic.cycle.item").search([['cycle_id','=',cycle_id],['date','=',obj.time_start[0:10]]]) item_id=None if item_ids: item_id=item_ids[0] res[obj.id]=item_id return res _fields={ 'name': fields.Char("Name", function="_get_name"), 'cycle_item_id': fields.Many2One("clinic.cycle.item","Cycle Item", function="_get_item"), "time_start": fields.DateTime("Time Start",required=True), "time_stop": fields.DateTime("Time Stop",required=True), 'date': fields.Date("Date",required=True,search=True), 'lines': fields.One2Many("clinic.schedule.line","schedule_id","Lines"), 'company_id': fields.Many2One("company","Company"), 'state': fields.Selection([['draft','Draft'],['confirmed', 'Confirmed']],'State',search=True), 'user_id': fields.Many2One("base.user","Confirm By"), } _defaults={ 'user_id': lambda *a: get_active_user(), "company_id": lambda *a: get_active_company(), 'date': lambda *a: datetime.now().strftime("%Y-%m-%d"), 'time_start': lambda *a: datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'time_stop': lambda *a: (datetime.now()+timedelta(seconds=3600)).strftime("%Y-%m-%d %H:%M:%S"), 'state': 'draft', } _order="date desc" _sql_constraints=[ ('schedule_uniq','unique (date,company_id)','Date should be unique'), ] def confirm(self,ids,context={}): obj=self.browse(ids)[0] obj.write({ 'state': 'confirmed', }) def to_draft(self,ids,context={}): obj=self.browse(ids)[0] obj.write({ 'state': 'draft', }) def copy(self,ids,context={}): obj=self.browse(ids)[0] datenow=datetime.now().strftime("%Y-%m-%d") old_ids=get_model('clinic.schedule').search([['date','=',datenow]]) if old_ids: raise Exception("This schedule is already create!") vals={ 'lines': [], } for line in obj.lines: vals['lines'].append(('create', { 'nurse_id': line.nurse_id.id, 'cycle_id': line.cycle_id.id } )) new_id=get_model("clinic.schedule").create(vals) new_obj=get_model("clinic.schedule").browse(new_id) return { 'next': { 'name': 'clinic_schedule', 'mode': 'form', 'active_id': new_id, }, 'flash': 'Copy schedule from %s to %s successfully'%(obj.date,new_obj.date) } def copy2cycle_item(self,ids,context={}): obj=self.browse(ids)[0] nurses=[] for line in obj.lines: nurse=line.nurse_id nurses.append(('create',{ 'nurse_id': nurse.id, 'level_id': nurse.level_id.id, })) if not obj.cycle_item_id: item_id=get_model("clinic.cycle.item").create({ 'date': obj.date, 'cycle_id': obj.cycle_id.id, }) obj.write({ 'cycle_item_id': item_id, }) for nurse in obj.cycle_item_id.nurses: nurse.delete() obj.cycle_item_id.write({ 'nurses': nurses, }) return { 'next': { 'name': 'clinic_cycle_item', 'mode': 'form', 'active_id': obj.cycle_item_id.id, }, 'flash': 'Copy nurses to cycle item successfully', } def onchange_date(self,context={}): data=context['data'] date=data['date'] time_start=data['time_start'][11:] data['time_start']='%s %s'%(date,time_start) time_stop=data['time_stop'][11:] data['time_stop']='%s %s'%(date,time_stop) return data def load_all_nurse(self,ids,context={}): nurses=get_model("clinic.personal").search_browse([['type','=','nurse']]) vals={ 'lines': [], } for nurse in nurses: vals['lines'].append({ 'cycle_id': nurse.id, 'nurse_id': nurse.id, }) obj=self.browse(ids)[0] obj.write(vals) def clear(self,ids,context={}): obj=self.browse(ids)[0] for line in obj.lines: line.delete() return { 'next': { 'name': 'clinic_schedule', 'mode': 'form', 'active_id': obj.id, }, 'flash': 'List of nurse has been cleared.', } Schedule.register()