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 CycleDaily(Model): _name="clinic.cycle.daily" _string="Cycle Daily" 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"), 'date': fields.Date("Date", required=True, search=True), 'cycle_items': fields.One2Many("clinic.cycle.item","cycle_daily_id", "Cycle Items"), 'lines': fields.One2Many("clinic.cycle.daily.line","cycle_daily_id", "Lines"), "state": fields.Selection([("draft","Draft"),('confirmed','Confirmed')],"Status",required=True), 'company_id': fields.Many2One("company","Company"), 'total': fields.Float("Total",function="_get_all", function_multi=True), 'user_id': fields.Many2One("base.user","User"), } _defaults={ 'company_id': lambda *a: get_active_company(), 'date': lambda *a: time.strftime("%Y-%m-%d"), 'name': lambda *a: time.strftime("%Y-%m-%d"), 'user_id': lambda *a: get_active_user(), 'state': 'draft', } def write(self,ids,vals,**kw): date=vals.get('date','') if date: vals['name']=date super().write(ids,vals,**kw) def confirm(self,ids,context={}): obj=self.browse(ids)[0] obj.write({ 'state': 'confirmed', }) return { 'next': { 'name': 'clinic_cycle_daily', 'mode': 'form', 'active_id': obj.id, }, 'flash':'Confirmed', } def recheck_item(self,ids,context={}): # copy cost of nurse and doctor from cycle item obj=self.browse(ids)[0] lines=[] ctx=context.copy() context['called']=True # sorted by sequence of cycle for cycle_item in sorted(obj.cycle_items, key=lambda x: x.cycle_id.sequence): lines+=cycle_item.validate(context=context) obj.write({ 'lines': lines, }) context=ctx return { 'next': { 'name': 'clinic_cycle_daily', 'mode': 'form', 'active_id': obj.id, }, 'flash':'Recheck successfully', } def to_draft(self,ids,context={}): obj=self.browse(ids)[0] # remove journal entry obj.write({ 'state': 'draft', }) return { 'next': { 'name': 'clinic_cycle_daily', 'mode': 'form', 'active_id': obj.id, }, 'flash':'Draft!', } 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 CycleDaily.register()