import time from netforce.model import Model, fields, get_model 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"), 'cycle_monthly_id': fields.Many2One("clinic.cycle.monthly","Monthly"), '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', }) all_vals={} for line in obj.lines: staff=line.staff_id level=line.level_id amt=line.amount or 0.0 qty=line.qty or 0 if not all_vals.get(staff.id): all_vals[staff.id]={ 'level_id': level.id, 'type': line.type, 'qty': 0, 'amount': 0, } all_vals[staff.id]['amount']+=amt all_vals[staff.id]['qty']+=qty lines=[] for staff_id, vals in all_vals.items(): vals.update({'staff_id': staff_id}) lines.append(('create',vals)) mname=obj.date[0:7].replace("-","/") mobj=get_model("clinic.cycle.monthly") res=mobj.search_browse([['name','=',mname]]) mid=None monthly=None if res: mid=res[0]['id'] monthly=mobj.browse(mid) # find other cycle daily and summary salary of staff for daily in monthly.cycle_dailies: # don't include amount the same date if daily.date==obj.date: continue for line in daily.lines: amt=line.amount or 0.0 qty=line.qty or 0 staff=line.staff_id vals=all_vals.get(staff.id,None) if vals: vals['amount']+=amt lines=[] for staff_id, vals in all_vals.items(): vals.update({'staff_id': staff_id}) lines.append(('create',vals)) # update staff amount staff_ids=all_vals.keys() for line in monthly.lines: staff=line.staff_id if staff.id in staff_ids: line.delete() monthly.write({ 'lines': lines, }) else: mid=mobj.create({ 'name': mname, 'lines': lines, }) monthly=mobj.browse(mid) if not monthly: return obj.write({ 'cycle_monthly_id': mid, }) return { 'next': { 'name': 'clinic_cycle_monthly', 'mode': 'form', 'active_id': monthly.id, }, 'flash':'Cycle %s has been confirmed and %s has been updated'%(obj.name,monthly.name) } 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 def view_cycle_monthly(self,ids,context): obj=self.browse(ids)[0] monthly=obj.cycle_monthly_id return { 'next': { 'name': 'clinic_monthly', 'mode': 'form', 'active_id': monthly.id, }, } CycleDaily.register()