clinic/netforce_clinic/models/cycle_daily.py

157 lines
4.7 KiB
Python
Raw Normal View History

2014-11-17 00:59:19 +00:00
import time
2014-11-27 01:50:15 +00:00
from netforce.model import Model, fields, get_model
2014-11-21 16:11:57 +00:00
from netforce.access import get_active_company, get_active_user
2014-11-24 10:14:21 +00:00
from netforce.utils import get_data_path
2014-11-27 01:50:15 +00:00
from netforce.database import get_connection
2014-11-17 00:59:19 +00:00
2014-11-27 00:47:38 +00:00
class CycleDaily(Model):
_name="clinic.cycle.daily"
_string="Cycle Daily"
2014-11-17 00:59:19 +00:00
2014-11-21 16:11:57 +00:00
def _get_all(self,ids,context={}):
res={}
for obj in self.browse(ids):
total=0.0
for line in obj.lines:
2014-11-23 15:39:41 +00:00
total+=(line.amount or 0)
2014-11-21 16:11:57 +00:00
res[obj.id]={
'total': total,
}
return res
2014-11-17 00:59:19 +00:00
_fields={
"name": fields.Char("Name"),
'date': fields.Date("Date", required=True, search=True),
2014-11-27 00:47:38 +00:00
'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),
2014-11-17 00:59:19 +00:00
'company_id': fields.Many2One("company","Company"),
2014-11-21 16:11:57 +00:00
'total': fields.Float("Total",function="_get_all", function_multi=True),
'user_id': fields.Many2One("base.user","User"),
2014-11-17 00:59:19 +00:00
}
_defaults={
'company_id': lambda *a: get_active_company(),
'date': lambda *a: time.strftime("%Y-%m-%d"),
'name': lambda *a: time.strftime("%Y-%m-%d"),
2014-11-21 16:11:57 +00:00
'user_id': lambda *a: get_active_user(),
'state': 'draft',
2014-11-17 00:59:19 +00:00
}
def write(self,ids,vals,**kw):
2014-11-21 16:11:57 +00:00
date=vals.get('date','')
if date:
vals['name']=date
2014-11-17 00:59:19 +00:00
super().write(ids,vals,**kw)
2014-11-21 16:11:57 +00:00
def confirm(self,ids,context={}):
obj=self.browse(ids)[0]
obj.write({
'state': 'confirmed',
})
2014-11-27 01:50:15 +00:00
# TODO create cycle monthly
all_vals={}
for line in obj.lines:
staff=line.staff_id
level=line.level_id
qty=line.qty
amt=line.amount
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]['qty']+=qty
all_vals[staff.id]['amount']+=amt
lines=[]
for staff_id, vals in all_vals.items():
vals.update({'staff_id': staff_id})
lines.append(('create',vals))
m=int(obj.date[5:7])
db=get_connection()
res=db.query("select id from clinic_cycle_monthly where extract(month from month)=%s"%m)
mobj=get_model("clinic.cycle.monthly")
mid=None
monthly=None
if res:
mid=res[0]['id']
monthly=mobj.browse(mid)
for line in monthly.lines:
line.delete()
monthly.write({
'lines': lines,
})
else:
mid=mobj.create({
'lines': lines,
})
monthly=mobj.browse(mid)
if not monthly:
return
2014-11-21 16:11:57 +00:00
return {
'next': {
2014-11-27 01:50:15 +00:00
'name': 'clinic_cycle_monthly',
2014-11-21 16:11:57 +00:00
'mode': 'form',
2014-11-27 01:50:15 +00:00
'active_id': monthly.id,
2014-11-21 16:11:57 +00:00
},
2014-11-27 01:50:15 +00:00
'flash':'Cycle %s has been confirmed and %s has been updated'%(obj.name,monthly.name)
2014-11-21 16:11:57 +00:00
}
def recheck_item(self,ids,context={}):
# copy cost of nurse and doctor from cycle item
2014-11-21 16:11:57 +00:00
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)
2014-11-21 16:11:57 +00:00
obj.write({
'lines': lines,
2014-11-21 16:11:57 +00:00
})
context=ctx
2014-11-21 16:11:57 +00:00
return {
'next': {
2014-11-27 00:47:38 +00:00
'name': 'clinic_cycle_daily',
2014-11-21 16:11:57 +00:00
'mode': 'form',
'active_id': obj.id,
},
'flash':'Recheck successfully',
2014-11-21 16:11:57 +00:00
}
def to_draft(self,ids,context={}):
obj=self.browse(ids)[0]
# remove journal entry
obj.write({
'state': 'draft',
})
return {
'next': {
2014-11-27 00:47:38 +00:00
'name': 'clinic_cycle_daily',
2014-11-21 16:11:57 +00:00
'mode': 'form',
'active_id': obj.id,
},
'flash':'Draft!',
}
2014-11-24 10:14:21 +00:00
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
2014-11-21 16:11:57 +00:00
2014-11-27 00:47:38 +00:00
CycleDaily.register()