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={ "date": fields.Date("Month"), "date_from": fields.Date("From", required=True), "date_to": fields.Date("To", required=True), } 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) _defaults={ 'date': lambda *a: time.strftime("%Y-%m-%d"), 'date_from': _get_date_from, 'date_to': _get_date_to, } def compute(self,ids,context={}): obj=self.browse(ids)[0] dom=[] dom.append(['date','>=', obj.date_from]) dom.append(['date','<=', obj.date_to]) for lcost in get_model("clinic.labor.cost").search_browse(dom): print("compute %s ....", lcost.cycle_item_id.name) 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 ComputeLaborCost.register()