import time from netforce.model import Model, fields from netforce.access import get_active_company, get_active_user class CycleDialy(Model): _name="clinic.cycle.dialy" _string="Cycle Dialy" 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_dialy_id", "Cycle Items"), 'lines': fields.One2Many("clinic.cycle.dialy.line","cycle_dialy_id", "Lines"), "state": fields.Selection([("draft","Draft"),('confirmed','Confirmed'),("done","Done")],"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_dialy', '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_dialy', '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_dialy', 'mode': 'form', 'active_id': obj.id, }, 'flash':'Draft!', } CycleDialy.register()