172 lines
5.2 KiB
Python
172 lines
5.2 KiB
Python
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
|
|
from netforce.database import get_connection
|
|
|
|
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',
|
|
})
|
|
|
|
# TODO create/update 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,
|
|
'date': obj.date,
|
|
'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:
|
|
if line.date==obj.date:
|
|
line.delete()
|
|
monthly.write({
|
|
'lines': lines,
|
|
})
|
|
else:
|
|
mid=mobj.create({
|
|
'lines': lines,
|
|
})
|
|
monthly=mobj.browse(mid)
|
|
if not monthly:
|
|
return
|
|
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()
|