2014-12-09 04:24:41 +00:00
|
|
|
import time
|
2015-01-22 10:40:05 +00:00
|
|
|
from calendar import monthrange
|
2014-12-09 04:24:41 +00:00
|
|
|
|
|
|
|
from netforce.model import Model, fields, get_model
|
|
|
|
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
|
2015-01-22 10:40:05 +00:00
|
|
|
#_key=['name']
|
2014-12-09 04:24:41 +00:00
|
|
|
|
|
|
|
def _get_all(self,ids,context={}):
|
|
|
|
res={}
|
|
|
|
for obj in self.browse(ids):
|
2015-01-22 10:40:05 +00:00
|
|
|
total=0.0
|
2014-12-09 04:24:41 +00:00
|
|
|
total_qty=0
|
2015-01-22 10:40:05 +00:00
|
|
|
dcost=0
|
|
|
|
ncost=0
|
2014-12-09 04:24:41 +00:00
|
|
|
for line in obj.lines:
|
2015-01-22 10:40:05 +00:00
|
|
|
amt=line.amount or 0
|
|
|
|
qty=line.qty or 0
|
|
|
|
if line.type=='nurse':
|
|
|
|
ncost+=amt
|
|
|
|
else:
|
|
|
|
dcost+=amt
|
|
|
|
total_qty+=qty
|
|
|
|
total+=amt
|
2014-12-09 04:24:41 +00:00
|
|
|
res[obj.id]={
|
2015-01-22 10:40:05 +00:00
|
|
|
'total':total,
|
2014-12-09 04:24:41 +00:00
|
|
|
'total_qty': total_qty,
|
2015-01-22 10:40:05 +00:00
|
|
|
'dcost': dcost,
|
|
|
|
'ncost': ncost,
|
2014-12-09 04:24:41 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
|
|
|
|
_fields={
|
2015-01-22 10:40:05 +00:00
|
|
|
'name': fields.Char("Name", required=True,search=True),
|
2014-12-09 04:24:41 +00:00
|
|
|
'date': fields.Date("Month",required=True),
|
2015-01-22 10:40:05 +00:00
|
|
|
'date_from': fields.Date("From",required=True),
|
|
|
|
'date_to': fields.Date("To",required=True),
|
|
|
|
'total': fields.Float("Total (Baht)", function="_get_all",function_multi=True),
|
2014-12-09 04:24:41 +00:00
|
|
|
'total_qty': fields.Float("Total Qty", function="_get_all",function_multi=True),
|
2015-01-22 10:40:05 +00:00
|
|
|
'dcost': fields.Float("Doctor Cost", function="_get_all",function_multi=True),
|
|
|
|
'ncost': fields.Float("Nurse Cost", function="_get_all",function_multi=True),
|
2014-12-09 04:24:41 +00:00
|
|
|
"lines": fields.One2Many("clinic.labor.cost.entry.line", "entry_id", "Lines"),
|
2015-01-22 10:40:05 +00:00
|
|
|
"nurse_lines": fields.One2Many("clinic.labor.cost.entry.line", "entry_id", "Nurse Lines",domain=[['type','=','nurse']]),
|
|
|
|
"doctor_lines": fields.One2Many("clinic.labor.cost.entry.line", "entry_id", "Doctor Lines",domain=[['type','=','doctor']]),
|
2015-02-02 14:19:53 +00:00
|
|
|
"work_state": fields.Selection([["part_time","Part Time"],["full_time","Full Time"]],"Working Status",search=True),
|
|
|
|
"state": fields.Selection([["draft","Draft"],["approved","Approved"]],"Status"),
|
2015-01-22 10:40:05 +00:00
|
|
|
'user_id': fields.Many2One("base.user","Approver",search=True),
|
2014-12-09 04:24:41 +00:00
|
|
|
'note': fields.Text("Note"),
|
|
|
|
'company_id': fields.Many2One("company","Company"),
|
2015-01-22 10:40:05 +00:00
|
|
|
'department_id': fields.Many2One("clinic.department","Department",search=True),
|
|
|
|
'branch_id': fields.Many2One("clinic.branch","Branch",search=True),
|
2014-12-09 04:24:41 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 10:40:05 +00:00
|
|
|
def _get_date_from(self,context={}):
|
|
|
|
year,month=time.strftime("%Y-%m").split("-")
|
|
|
|
return '%s-%s-01'%(year,month)
|
|
|
|
|
|
|
|
def _get_date_to(self,context={}):
|
|
|
|
year,month,day=time.strftime("%Y-%m-%d").split("-")
|
|
|
|
weekday, total_day=monthrange(int(year), int(month))
|
|
|
|
return "%s-%s-%s"%(year,month,total_day)
|
|
|
|
|
2014-12-09 04:24:41 +00:00
|
|
|
_defaults={
|
2015-01-22 10:40:05 +00:00
|
|
|
'name': '/',
|
2014-12-09 04:24:41 +00:00
|
|
|
'date': lambda *a: time.strftime("%Y-%m-%d"),
|
2015-01-22 10:40:05 +00:00
|
|
|
'date_from': _get_date_from,
|
|
|
|
'date_to': _get_date_to,
|
2014-12-09 04:24:41 +00:00
|
|
|
'user_id': lambda *a: get_active_user(),
|
|
|
|
'company_id': lambda *a: get_active_company(),
|
2015-02-02 14:19:53 +00:00
|
|
|
'state': 'draft',
|
2014-12-09 04:24:41 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 10:40:05 +00:00
|
|
|
_order="date_from desc,branch_id,department_id"
|
|
|
|
|
2014-12-09 04:24:41 +00:00
|
|
|
def compute(self,ids,context={}):
|
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
year,month,day=obj.date.split("-")
|
2015-01-22 10:40:05 +00:00
|
|
|
line_ids=[]
|
|
|
|
dom=[
|
|
|
|
['date','>=',obj.date_from],
|
|
|
|
['date','<=',obj.date_to],
|
|
|
|
]
|
|
|
|
if obj.branch_id:
|
|
|
|
dom.append(['labor_cost_id.cycle_item_id.branch_id','=',obj.branch_id.id])
|
|
|
|
if obj.department_id:
|
|
|
|
dom.append(['labor_cost_id.cycle_item_id.department_id','=',obj.department_id.id])
|
2015-02-02 14:56:24 +00:00
|
|
|
if obj.work_state:
|
|
|
|
dom.append(['staff_id.state','=',obj.work_state])
|
2015-01-22 10:40:05 +00:00
|
|
|
for lc_id in get_model("clinic.labor.cost.line").search(dom):
|
|
|
|
line_ids.append(lc_id)
|
2014-12-09 04:24:41 +00:00
|
|
|
staffs={}
|
|
|
|
for line in get_model("clinic.labor.cost.line").browse(line_ids):
|
|
|
|
staff=line.staff_id
|
|
|
|
qty=line.qty
|
|
|
|
amt=line.amount
|
2015-01-23 01:50:44 +00:00
|
|
|
state=line.state or staff.state
|
2015-01-22 10:40:05 +00:00
|
|
|
dpt=staff.department_id
|
2015-01-30 11:15:13 +00:00
|
|
|
citem=line.labor_cost_id.cycle_item_id
|
2015-01-22 10:40:05 +00:00
|
|
|
if not dpt:
|
2015-01-30 11:15:13 +00:00
|
|
|
dpt=citem.department_id
|
2014-12-09 04:24:41 +00:00
|
|
|
if not staffs.get(staff.id):
|
|
|
|
staffs[staff.id]={
|
2015-01-30 11:15:13 +00:00
|
|
|
'staff_type': staff.type,
|
2015-01-22 10:40:05 +00:00
|
|
|
'department_id': dpt.id,
|
2014-12-09 04:24:41 +00:00
|
|
|
'qty': 0,
|
|
|
|
'amt': 0,
|
|
|
|
'rate': 0, # XXX for special nurse
|
2015-01-23 01:50:44 +00:00
|
|
|
'state': state,
|
2015-01-30 11:15:13 +00:00
|
|
|
'report_staff_id': get_model("clinic.report.staff").create({
|
|
|
|
'date_from': obj.date_from,
|
|
|
|
'date_to': obj.date_to,
|
|
|
|
'staff_id': staff.id,
|
2015-02-02 10:35:47 +00:00
|
|
|
'entry_id': obj.id,
|
|
|
|
'staff_type': staff.type,
|
2015-01-30 11:15:13 +00:00
|
|
|
}),
|
|
|
|
'items': set(),
|
2014-12-09 04:24:41 +00:00
|
|
|
}
|
|
|
|
staffs[staff.id]['qty']+=qty
|
|
|
|
staffs[staff.id]['amt']+=amt
|
2015-01-30 11:15:13 +00:00
|
|
|
staffs[staff.id]['items'].update({citem.id})
|
2014-12-09 04:24:41 +00:00
|
|
|
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
|
2015-01-23 01:50:44 +00:00
|
|
|
state=vals['state']
|
2015-01-30 11:15:13 +00:00
|
|
|
report_staff_id=vals['report_staff_id']
|
|
|
|
report_staff=get_model("clinic.report.staff").browse(report_staff_id)
|
2015-02-02 10:35:47 +00:00
|
|
|
|
|
|
|
# Find hd case for each staff
|
2015-01-30 11:15:13 +00:00
|
|
|
hids=set()
|
2015-02-02 10:35:47 +00:00
|
|
|
pids=set()
|
2015-01-30 11:15:13 +00:00
|
|
|
for item in get_model("clinic.cycle.item").browse(vals['items']):
|
|
|
|
if vals['staff_type']=='nurse':
|
|
|
|
for hd_case in item.hd_cases:
|
2015-02-02 10:35:47 +00:00
|
|
|
#hids.update({hd_case.id})
|
|
|
|
pids.update({hd_case.patient_id.id})
|
2015-02-03 05:09:30 +00:00
|
|
|
#print('pids ', pids)
|
2015-01-30 11:15:13 +00:00
|
|
|
else:
|
|
|
|
for hd_case in item.hd_cases:
|
|
|
|
doctor_id=hd_case.doctor_id.id
|
|
|
|
if staff_id==doctor_id:
|
|
|
|
hids.update({hd_case.id})
|
2015-02-02 10:35:47 +00:00
|
|
|
#pids.update({hd_case.patient_id.id})
|
2015-01-30 11:15:13 +00:00
|
|
|
rlines=[]
|
|
|
|
for hid in list(hids):
|
|
|
|
rlines.append(('create',{
|
|
|
|
'hd_case_id': hid,
|
2015-02-02 10:35:47 +00:00
|
|
|
'amount': amt/qty, #XXX
|
|
|
|
}))
|
|
|
|
|
|
|
|
plines=[]
|
2015-02-03 05:09:30 +00:00
|
|
|
print('pids ', list(pids))
|
2015-02-02 10:35:47 +00:00
|
|
|
for pid in list(pids):
|
|
|
|
plines.append(('create',{
|
|
|
|
'patient_id': pid,
|
2015-01-30 11:15:13 +00:00
|
|
|
}))
|
2015-02-02 10:35:47 +00:00
|
|
|
|
2015-01-30 11:15:13 +00:00
|
|
|
report_staff.write({
|
|
|
|
'lines': rlines,
|
2015-02-02 10:35:47 +00:00
|
|
|
'patients': plines,
|
2015-01-30 11:15:13 +00:00
|
|
|
})
|
|
|
|
#####
|
|
|
|
|
2014-12-09 04:35:20 +00:00
|
|
|
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,
|
2015-01-23 01:50:44 +00:00
|
|
|
'state': state,
|
2015-01-22 10:40:05 +00:00
|
|
|
'department_id': vals['department_id'],
|
2015-01-30 11:15:13 +00:00
|
|
|
'report_staff_id': report_staff.id,
|
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)
|
|
|
|
|
2015-01-22 10:40:05 +00:00
|
|
|
def update_amount(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
total_amt=0.0
|
2015-02-02 10:35:47 +00:00
|
|
|
total_damt=0.0
|
|
|
|
total_namt=0.0
|
|
|
|
for line in data['doctor_lines']:
|
|
|
|
total_amt+=line['amount']
|
|
|
|
total_damt+=line['amount']
|
|
|
|
for line in data['nurse_lines']:
|
2015-01-22 10:40:05 +00:00
|
|
|
total_amt+=line['amount']
|
2015-02-02 10:35:47 +00:00
|
|
|
total_namt+=line['amount']
|
|
|
|
data['total']=total_amt
|
|
|
|
data['dcost']=total_damt
|
|
|
|
data['ncost']=total_namt
|
2015-01-22 10:40:05 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
def onchange_date(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
date=data['date']
|
|
|
|
year,month,day=date.split("-")
|
|
|
|
weekday, total_day=monthrange(int(year), int(month))
|
|
|
|
data['date_from']="%s-%s-01"%(year,month)
|
|
|
|
data['date_to']="%s-%s-%s"%(year,month,total_day)
|
|
|
|
return data
|
|
|
|
|
2014-12-09 04:24:41 +00:00
|
|
|
def onchange_line(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
path=context['path']
|
|
|
|
line=get_data_path(data,path,parent=True)
|
2015-02-02 10:35:47 +00:00
|
|
|
cmax=line['max_cycle'] or 0
|
|
|
|
qty=line['qty'] or 0
|
|
|
|
rate=line['rate'] or 0
|
|
|
|
over=min(0,cmax-qty)
|
|
|
|
print('xx ', cmax, qty, over)
|
|
|
|
if cmax<=0:
|
|
|
|
over=0
|
|
|
|
line['amount']=qty*rate
|
|
|
|
line['over_cycle']=over
|
2014-12-09 04:24:41 +00:00
|
|
|
data=self.update_amount(context)
|
|
|
|
return data
|
2015-01-22 10:40:05 +00:00
|
|
|
|
|
|
|
def onchange_branch(self,context={}):
|
2014-12-09 04:24:41 +00:00
|
|
|
data=context['data']
|
2015-01-22 10:40:05 +00:00
|
|
|
data['department_id']=None
|
2014-12-09 04:24:41 +00:00
|
|
|
return data
|
2015-01-23 05:23:54 +00:00
|
|
|
|
|
|
|
def approve(self,ids,context={}):
|
|
|
|
for obj in self.browse(ids):
|
|
|
|
obj.write({
|
|
|
|
'state': 'approved',
|
|
|
|
})
|
|
|
|
|
|
|
|
def to_draft(self,ids,context={}):
|
|
|
|
for obj in self.browse(ids):
|
|
|
|
obj.write({
|
|
|
|
'state': 'draft',
|
|
|
|
})
|
2014-12-09 04:24:41 +00:00
|
|
|
|
|
|
|
LaborCostEntry.register()
|