2014-12-09 04:24:41 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
from netforce.model import Model, fields, get_model
|
|
|
|
from netforce.database import get_connection
|
|
|
|
from netforce.access import get_active_user, get_active_company
|
|
|
|
from netforce.utils import get_data_path
|
|
|
|
|
|
|
|
from . import utils
|
|
|
|
|
|
|
|
class LaborCostEntry(Model):
|
|
|
|
_name="clinic.labor.cost.entry"
|
|
|
|
_string="Labor Cost Entry"
|
|
|
|
_multi_company=True
|
|
|
|
_key=['name']
|
|
|
|
|
|
|
|
def _get_all(self,ids,context={}):
|
|
|
|
res={}
|
|
|
|
for obj in self.browse(ids):
|
|
|
|
total_amt=0.0
|
|
|
|
total_qty=0
|
|
|
|
for line in obj.lines:
|
|
|
|
total_qty+=line.qty or 0
|
|
|
|
total_amt+=line.amount or 0.0
|
|
|
|
res[obj.id]={
|
|
|
|
'total_amt':total_amt,
|
|
|
|
'total_qty': total_qty,
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
|
|
|
|
_fields={
|
|
|
|
'name': fields.Char("Name", search=True),
|
|
|
|
'date': fields.Date("Month",required=True),
|
|
|
|
'total_amt': fields.Float("Total", function="_get_all",function_multi=True),
|
|
|
|
'total_qty': fields.Float("Total Qty", function="_get_all",function_multi=True),
|
|
|
|
"lines": fields.One2Many("clinic.labor.cost.entry.line", "entry_id", "Lines"),
|
|
|
|
'state': fields.Selection([['draft','Draft'],['approved','Approved']],"State"),
|
|
|
|
'user_id': fields.Many2One("base.user","Approver"),
|
|
|
|
'note': fields.Text("Note"),
|
|
|
|
'company_id': fields.Many2One("company","Company"),
|
|
|
|
}
|
|
|
|
|
|
|
|
_defaults={
|
|
|
|
'state': 'draft',
|
|
|
|
'date': lambda *a: time.strftime("%Y-%m-%d"),
|
|
|
|
'user_id': lambda *a: get_active_user(),
|
|
|
|
'company_id': lambda *a: get_active_company(),
|
|
|
|
}
|
|
|
|
|
|
|
|
def compute(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
year,month,day=obj.date.split("-")
|
|
|
|
db=get_connection()
|
|
|
|
line_ids=[r['id'] for r in db.query("select id from clinic_labor_cost_line where extract(month from date)=%s",int(month))]
|
|
|
|
staffs={}
|
|
|
|
for line in get_model("clinic.labor.cost.line").browse(line_ids):
|
|
|
|
staff=line.staff_id
|
|
|
|
qty=line.qty
|
|
|
|
amt=line.amount
|
|
|
|
if not staffs.get(staff.id):
|
|
|
|
staffs[staff.id]={
|
|
|
|
'qty': 0,
|
|
|
|
'amt': 0,
|
|
|
|
'rate': 0, # XXX for special nurse
|
|
|
|
}
|
|
|
|
staffs[staff.id]['qty']+=qty
|
|
|
|
staffs[staff.id]['amt']+=amt
|
|
|
|
|
|
|
|
lines=[]
|
|
|
|
timenow=time.strftime("%Y-%m-%d")
|
|
|
|
for staff_id, vals in staffs.items():
|
2014-12-09 04:35:20 +00:00
|
|
|
qty=vals['qty'] or 0
|
|
|
|
amt=vals['amt'] or 0.0
|
|
|
|
rate=0.0
|
|
|
|
if qty:
|
|
|
|
rate=amt/qty # average
|
2014-12-09 04:24:41 +00:00
|
|
|
lines.append(('create',{
|
|
|
|
'staff_id': staff_id,
|
2014-12-09 04:35:20 +00:00
|
|
|
'qty': qty,
|
|
|
|
'amount': amt,
|
2014-12-09 04:24:41 +00:00
|
|
|
'date': timenow,
|
2014-12-09 04:35:20 +00:00
|
|
|
'rate': rate,
|
2014-12-09 04:24:41 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
for line in obj.lines:
|
|
|
|
line.delete()
|
|
|
|
|
|
|
|
obj.write({
|
|
|
|
'lines': lines,
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
'next': {
|
|
|
|
'name': 'clinic_labor_cost_entry',
|
|
|
|
'mode': 'form',
|
|
|
|
'active_id': obj.id,
|
|
|
|
},
|
|
|
|
'flash': 'Compute Succesffuly',
|
|
|
|
}
|
|
|
|
|
|
|
|
def create(self,vals,**kw):
|
|
|
|
vals['name']=utils.date2thai(vals['date'],format='%(BY)s/%(m)s')
|
|
|
|
new_id=super().create(vals,**kw)
|
|
|
|
return new_id
|
|
|
|
|
|
|
|
def write(self,ids,vals,**kw):
|
|
|
|
if 'date' in vals.keys():
|
|
|
|
vals['name']=utils.date2thai(vals['date'],format='%(BY)s/%(m)s')
|
|
|
|
else:
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
vals['name']=utils.date2thai(obj.date,format='%(BY)s/%(m)s')
|
|
|
|
super().write(ids,vals,**kw)
|
|
|
|
|
|
|
|
def onchange_line(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
path=context['path']
|
|
|
|
line=get_data_path(data,path,parent=True)
|
|
|
|
line['amount']=(line['qty'] or 0) * (line['rate'] or 0.0)
|
|
|
|
data=self.update_amount(context)
|
|
|
|
return data
|
|
|
|
|
|
|
|
def update_amount(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
total_amt=0.0
|
|
|
|
for line in data['lines']:
|
|
|
|
total_amt+=line['amount']
|
|
|
|
data['total_amt']=total_amt
|
|
|
|
return data
|
|
|
|
|
|
|
|
LaborCostEntry.register()
|