import time from calendar import monthrange from netforce.model import Model,fields,get_model class ComputeLaborCost(Model): _name="clinic.compute.labor.cost" _string="Compute Labor Cost" _transient=True _fields={ "period_id": fields.Many2One("clinic.period.line","Period"), "date": fields.Date("Month"), "date_from": fields.Date("From", required=True), "date_to": fields.Date("To", required=True), 'branch_id': fields.Many2One("clinic.branch","Branch"), 'department_id': fields.Many2One("clinic.department","Department"), } def _get_date_from(self,context={}): year,month=time.strftime("%Y-%m").split("-") return '%s-%s-01'%(year,month) def _get_date_to(self,context={}): year,month,day=time.strftime("%Y-%m-%d").split("-") weekday, total_day=monthrange(int(year), int(month)) return "%s-%s-%s"%(year,month,total_day) def default_get(self,field_names=None,context={},**kw): defaults=context.get("defaults",{}) date_from=defaults.get("date_from", self._get_date_from()) date_to=defaults.get("date_to", self._get_date_to()) yearnow=date_from.split("-")[0] for period in get_model('clinic.period').search_browse([['name','=',yearnow]]): for line in period.lines: if line.state=='open': period_id=line.id date_from=line.date_start date_to=line.date_stop break res={ 'period_id': period_id, 'date': time.strftime("%Y-%m-%d"), 'date_from': date_from, 'date_to': date_to, } return res def compute(self,ids,context={}): obj=self.browse(ids)[0] dom=[] dom.append(['date','>=', obj.date_from]) dom.append(['date','<=', obj.date_to]) if obj.branch_id: dom.append(['branch_id','=',obj.branch_id.id]) if obj.department_id: dom.append(['department_id','=',obj.department_id.id]) for lcost in get_model("clinic.labor.cost").search_browse(dom): print("compute %s ....", lcost.cycle_item_id.name) #item=lcost.cycle_item_id #item.to_draft() #item.validate() lcost.compute() print("Done!") return { 'next': { 'name': 'clinic_compute_labor_cost', 'mode': 'form', 'active_id': obj.id, }, 'flash': 'Compute Successfully', } def onchange_date(self,context={}): data=context['data'] date=data['date'] year,month,day=date.split("-") weekday, total_day=monthrange(int(year), int(month)) data['date_from']="%s-%s-01"%(year,month) data['date_to']="%s-%s-%s"%(year,month,total_day) return data def onchange_branch(self,context={}): data=context['data'] data['department_id']=None return data def onchange_period(self,context={}): data=context['data'] period_id=data['period_id'] period=get_model('clinic.period.line').browse(period_id) data['date_from']=period.date_start data['date_to']=period.date_stop return data ComputeLaborCost.register()