import time from netforce.model import Model, fields from netforce.access import get_active_company, get_active_user from netforce.utils import get_data_path class CycleMonthly(Model): _name="clinic.cycle.monthly" _string="Cycle Monthly" def _get_all(self,ids,context={}): res={} for obj in self.browse(ids): total=0.0 for line in obj.lines: total+=(line.amount or 0) res[obj.id]={ 'total': total, } return res _fields={ "name": fields.Char("Name"), 'cycle_dailies': fields.One2Many("clinic.cycle.daily","cycle_monthly_id", "Cycle Dialy"), 'lines': fields.One2Many("clinic.cycle.monthly.line","cycle_monthly_id", "Lines"), 'company_id': fields.Many2One("company","Company"), 'total': fields.Float("Total",function="_get_all", function_multi=True), 'user_id': fields.Many2One("base.user","User"), "state": fields.Selection([("draft","Draft"),('approved','Approved')],"Status",required=True), } _defaults={ 'company_id': lambda *a: get_active_company(), 'name': lambda *a: time.strftime("%Y/%m"), 'user_id': lambda *a: get_active_user(), 'state': 'draft', } def write(self,ids,vals,**kw): date=vals.get('month','') if date: vals['name']=date super().write(ids,vals,**kw) def approve(self,ids,context={}): obj=self.browse(ids)[0] obj.write({ 'state': 'approved', }) return { 'next': { 'name': 'clinic_cycle_monthly', 'mode': 'form', 'active_id': obj.id, }, 'flash':'Approved', } def recheck_daily(self,ids,context={}): # copy cost of nurse and doctor from cycle item obj=self.browse(ids)[0] lines=[] ctx=context.copy() context['called']=True for cycle_daily in obj.dailies: lines+=cycle_daily.confirm(context=context) obj.write({ 'lines': lines, }) context=ctx return { 'next': { 'name': 'clinic_cycle_monthly', 'mode': 'form', 'active_id': obj.id, }, 'flash':'Recheck successfully', } def onchange_line(self,context={}): data=context['data'] path=context['path'] line=get_data_path(data,path,parent=True) qty=line['qty'] rate=line['rate'] line['amount']=qty*rate total=0.0 for line in data['lines']: qty=line['qty'] rate=line['rate'] total+=qty*rate data['total']=total return data def to_draft(self,ids,context={}): obj=self.browse(ids)[0] obj.write({ 'state': 'draft', }) return { 'next': { 'name': 'clinic_cycle_monthly', 'mode': 'form', 'active_id': obj.id, }, 'flash': '%s has been set to draft'%obj.name } CycleMonthly.register()