from datetime import datetime from netforce.model import Model, fields, get_model from netforce.access import get_active_company class Cycle(Model): _name="clinic.cycle" _string="Cycle" _key=["name"] #_multi_company=True def _get_duration(self,ids,context={}): res={} fmt="%Y-%m-%d %H:%M" date=datetime.now().strftime(fmt)[0:10] for obj in self.browse(ids): tstart=obj.time_start tstop=obj.time_stop if not tstart: tstart='00:00' if not tstop: tstop='00:00' # recheck tstart=tstart.replace(".",":") tstop=tstop.replace(".",":") if len(tstart.split(":"))!=2: tstart="00:00" if len(tstop.split(":"))!=2: tstop="00:00" dstart='%s %s'%(date,tstart) dstop='%s %s'%(date,tstop) diff=datetime.strptime(dstop,fmt)-datetime.strptime(dstart,fmt) total_time=round(diff.seconds/3600,2) res[obj.id]=total_time return res _fields={ "name": fields.Char("Name",required=True,search=True), 'duration': fields.Integer("Duration(Hour)",function="_get_duration"), 'sequence': fields.Integer("Sequence"), 'time_start': fields.Char("Start Time"), 'time_stop': fields.Char("End Time"), 'hd_cases': fields.One2Many("clinic.hd.case","cycle_id", "HD Cases"), 'visits': fields.One2Many("clinic.visit","cycle_id", "Visits"), 'cycle_items': fields.One2Many("clinic.cycle.item","cycle_id", "Cycle Items"), 'var_k': fields.Float("K"), 'color': fields.Char("Color"), 'note': fields.Text("Note"), 'company_id': fields.Many2One("company", "Company"), } _defaults={ 'duration': 1, 'sequence': 1, 'company_id': lambda *a: get_active_company(), } def write(self,ids,vals,**kw): obj=self.browse(ids)[0] super().write(ids,vals,**kw) if 'color' in vals.keys(): color=vals['color'] dom=[] dom.append(['cycle_id','=',obj.id]) for vs in get_model('clinic.visit').search_browse(dom): vs.write({ 'cycle_color': color, }) Cycle.register()