2015-07-29 11:36:31 +00:00
|
|
|
import os
|
2015-07-29 01:44:03 +00:00
|
|
|
import time
|
2015-07-29 11:36:31 +00:00
|
|
|
import urllib
|
2015-07-29 01:44:03 +00:00
|
|
|
from calendar import monthrange
|
|
|
|
|
|
|
|
from netforce.model import Model,fields,get_model
|
2015-07-29 11:36:31 +00:00
|
|
|
from netforce.database import get_active_db, get_connection
|
2015-11-18 11:18:05 +00:00
|
|
|
from netforce.access import get_active_user, get_active_company
|
2015-07-30 03:44:41 +00:00
|
|
|
import netforce.config as config
|
2015-07-29 01:44:03 +00:00
|
|
|
|
|
|
|
class PrintLaborCost(Model):
|
|
|
|
_name="clinic.print.labor.cost"
|
2015-11-18 11:18:05 +00:00
|
|
|
_string="Report Labor Cost"
|
|
|
|
|
|
|
|
def _get_name(self,ids,context={}):
|
|
|
|
res={}
|
|
|
|
for obj in self.browse(ids):
|
2015-11-19 07:08:33 +00:00
|
|
|
year_frm,month_frm,day_frm=obj.date_from.split("-")
|
|
|
|
year_to,month_to,day_to=obj.date_to.split("-")
|
2015-11-18 11:18:05 +00:00
|
|
|
name=''.join([year_frm,month_frm,day_frm,month_to,day_to])
|
|
|
|
if obj.cycle_id:
|
|
|
|
name+=obj.cycle_id.name
|
|
|
|
if obj.branch_id:
|
|
|
|
name+=obj.branch_id.name
|
|
|
|
if obj.department_id:
|
|
|
|
name+=obj.department_id.name
|
2015-11-19 07:08:33 +00:00
|
|
|
name+='-'+obj.staff_type
|
2015-11-18 11:18:05 +00:00
|
|
|
res[obj.id]=name
|
|
|
|
return res
|
|
|
|
|
2015-07-29 01:44:03 +00:00
|
|
|
_fields={
|
2015-11-18 11:18:05 +00:00
|
|
|
"name": fields.Char("Name",function="_get_name"),
|
2015-07-29 01:44:03 +00:00
|
|
|
"date": fields.Date("Month"),
|
|
|
|
"period_id": fields.Many2One("clinic.period.line","Period"),
|
|
|
|
"date_from": fields.Date("From", required=True),
|
|
|
|
"date_to": fields.Date("To", required=True),
|
|
|
|
"cycle_id": fields.Many2One("clinic.cycle","Cycle"),
|
|
|
|
"branch_id": fields.Many2One("clinic.branch","Branch"),
|
|
|
|
"department_id": fields.Many2One("clinic.department","Department"),
|
2015-07-29 11:36:31 +00:00
|
|
|
"staff_type": fields.Selection([["doctor","Doctor"],["nurse","Nurse"]],"Type"),
|
|
|
|
'staff_id': fields.Many2One("clinic.staff","Staff"),
|
2015-07-29 01:44:03 +00:00
|
|
|
'lines': fields.One2Many("clinic.print.labor.cost.line","print_labor_cost_id","Lines"),
|
2015-07-29 11:36:31 +00:00
|
|
|
'sum_report_id': fields.Many2One("clinic.report.labor.cost.summary","Summary Report"),
|
|
|
|
'lc_report_id': fields.Many2One("clinic.report.labor.cost","Labor Cost Report"),
|
2015-07-29 15:56:58 +00:00
|
|
|
'dt_report_id': fields.Many2One("clinic.report.labor.cost.detail","Detail Report"),
|
|
|
|
'sub_dt_report_id': fields.Many2One("clinic.report.labor.cost.sub.detail","Sub Detail Report"),
|
|
|
|
'dl_report_id': fields.Many2One("clinic.report.labor.cost.daily","Daily Report"),
|
|
|
|
'ot_report_id': fields.Many2One("clinic.report.labor.cost.overtime","Overtime Report"),
|
2015-07-29 11:36:31 +00:00
|
|
|
'report_summary': fields.File("Summary"),
|
|
|
|
'report_labor_cost': fields.File("Labor Cost"),
|
2015-11-19 07:08:33 +00:00
|
|
|
'state': fields.Selection([['draft','Draft'],['waiting_doc','Waiting Document'],['done','Done']],'State'),
|
2015-11-18 11:18:05 +00:00
|
|
|
'company_id': fields.Many2One('company','Company'),
|
2015-11-19 07:08:33 +00:00
|
|
|
'zip_file': fields.File("File"),
|
2015-07-29 01:44:03 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 11:18:05 +00:00
|
|
|
def _get_date_from(self,context={}):
|
|
|
|
year,month,day=time.strftime("%Y-%m-%d").split("-")
|
|
|
|
#return '%s-%s-01'%(year,month)
|
|
|
|
return '%s-%s-%s'%(year,month,day)
|
|
|
|
|
|
|
|
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)
|
|
|
|
return "%s-%s-%s"%(year,month,day)
|
|
|
|
|
|
|
|
def default_get(self,field_names=None,context={},**kw):
|
|
|
|
defaults=context.get("defaults",{})
|
|
|
|
date_from=defaults.get("date_from", self._get_date_from())
|
|
|
|
date_to=defaults.get("date_to", self._get_date_to())
|
|
|
|
print('defaults ', defaults)
|
|
|
|
yearnow=date_from.split("-")[0]
|
|
|
|
for period in get_model('clinic.period').search_browse([['name','=',yearnow]]):
|
|
|
|
for line in period.lines:
|
|
|
|
if line.state=='open':
|
|
|
|
period_id=line.id
|
|
|
|
date_from=line.date_start
|
|
|
|
date_to=line.date_stop
|
|
|
|
break
|
|
|
|
res={
|
|
|
|
'period_id': period_id,
|
|
|
|
'date': time.strftime("%Y-%m-%d"),
|
|
|
|
'date_from': date_from,
|
|
|
|
'date_to': date_to,
|
|
|
|
'state': 'draft',
|
|
|
|
'company_id': get_active_company(),
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
|
2015-11-19 07:08:33 +00:00
|
|
|
def reset_data(self,ids,context={}):
|
2015-07-29 01:44:03 +00:00
|
|
|
obj=self.browse(ids)[0]
|
|
|
|
context['defaults']={
|
|
|
|
'date_from': obj.date_from,
|
|
|
|
'date_to': obj.date_to,
|
|
|
|
'cycle_id': obj.cycle_id.id,
|
|
|
|
'branch_id': obj.branch_id.id,
|
|
|
|
'department_id': obj.department_id.id,
|
|
|
|
'staff_type': obj.staff_type,
|
|
|
|
}
|
|
|
|
cost_lines=get_model('clinic.report.labor.cost.summary').get_report_data(ids=[],context=context)['lines']
|
|
|
|
for line in obj.lines:
|
|
|
|
line.delete()
|
|
|
|
lines=[]
|
|
|
|
for cost_line in cost_lines:
|
|
|
|
staff_id=cost_line['staff_id']
|
2015-07-29 11:36:31 +00:00
|
|
|
staff_name=cost_line['staff_name']
|
|
|
|
total_amount=0
|
|
|
|
total_qty=0
|
|
|
|
for sub_line in cost_line['sub_lines']:
|
|
|
|
total_qty+=sub_line['qty']
|
|
|
|
total_amount+=sub_line['amt']
|
2015-07-29 01:44:03 +00:00
|
|
|
lines.append(('create',{
|
|
|
|
'staff_id': staff_id,
|
2015-07-29 11:36:31 +00:00
|
|
|
'staff_name': staff_name,
|
|
|
|
'total_amount': total_amount,
|
|
|
|
'total_qty': total_qty,
|
2015-07-29 01:44:03 +00:00
|
|
|
}))
|
|
|
|
obj.write({
|
|
|
|
'lines': lines,
|
|
|
|
})
|
|
|
|
|
2015-07-30 03:44:41 +00:00
|
|
|
def do_generate(self,ids,context={}):
|
2015-11-19 07:08:33 +00:00
|
|
|
self.trigger(ids,"do_gen")
|
|
|
|
|
|
|
|
def wkf_gen_report(self,context={},**kw):
|
|
|
|
print("#"*80)
|
|
|
|
trigger_ids=context.get("trigger_ids")
|
|
|
|
if not trigger_ids:
|
|
|
|
raise Exception("Missing trigger ids")
|
|
|
|
print("trigger_ids",trigger_ids)
|
|
|
|
self.generate_report(trigger_ids,context,**kw)
|
2015-07-30 03:44:41 +00:00
|
|
|
|
2015-07-29 11:36:31 +00:00
|
|
|
def generate_report(self,ids,context={}):
|
2015-11-19 07:08:33 +00:00
|
|
|
db=get_connection()
|
2015-07-29 11:36:31 +00:00
|
|
|
obj=self.browse(ids)[0]
|
2015-11-19 07:08:33 +00:00
|
|
|
obj.write({
|
|
|
|
'state': 'waiting_doc',
|
|
|
|
'zip_file': None,
|
|
|
|
})
|
|
|
|
db.commit()
|
2015-07-29 15:56:58 +00:00
|
|
|
# clear and reload
|
2015-11-19 07:08:33 +00:00
|
|
|
obj.reset_data()
|
2015-07-29 11:36:31 +00:00
|
|
|
def load_report(fname,link):
|
2015-07-30 03:44:41 +00:00
|
|
|
host=config.config['host']
|
|
|
|
port=config.config['port']
|
|
|
|
hostname='http://%s:%s/'%(host,port)
|
|
|
|
link=hostname+link
|
2015-07-29 11:36:31 +00:00
|
|
|
req=urllib.request.Request(link)
|
|
|
|
response=urllib.request.urlopen(req)
|
|
|
|
data=response.read()
|
2015-11-19 07:08:33 +00:00
|
|
|
fpath=os.path.join('/tmp/labor_cost',fname)
|
|
|
|
open(fpath,"wb").write(data)
|
|
|
|
|
|
|
|
dbname=get_active_db()
|
|
|
|
fdir=os.path.join("static","db",dbname,"files")
|
|
|
|
tmp_dir='/tmp/labor_cost'
|
|
|
|
if not os.path.isdir(tmp_dir):
|
|
|
|
os.system("mkdir %s"%(tmp_dir))
|
2015-07-29 11:36:31 +00:00
|
|
|
|
|
|
|
sum_id=obj.sum_report_id.id
|
|
|
|
if not sum_id:
|
|
|
|
sum_id=get_model('clinic.report.labor.cost.summary').create({
|
|
|
|
'date': obj.date_from,
|
|
|
|
'date_from': obj.date_from,
|
|
|
|
'date_to': obj.date_to,
|
|
|
|
'staff_type': obj.staff_type,
|
|
|
|
'cycle_id': obj.cycle_id.id,
|
|
|
|
'branch_id': obj.branch_id.id,
|
|
|
|
'department_id': obj.department_id.id,
|
|
|
|
})
|
|
|
|
obj.write({
|
|
|
|
'sum_report_id': sum_id,
|
|
|
|
})
|
|
|
|
|
|
|
|
report_sum=get_model("clinic.report.labor.cost.summary").browse(sum_id)
|
|
|
|
report_sum.write({
|
|
|
|
'date': obj.date_from,
|
|
|
|
'date_from': obj.date_from,
|
|
|
|
'date_to': obj.date_to,
|
|
|
|
'staff_type': obj.staff_type,
|
|
|
|
'cycle_id': obj.cycle_id.id,
|
|
|
|
'branch_id': obj.branch_id.id,
|
|
|
|
'department_id': obj.department_id.id,
|
|
|
|
'staff_id': None,
|
|
|
|
})
|
|
|
|
|
|
|
|
lc_id=obj.lc_report_id.id
|
|
|
|
if not lc_id:
|
|
|
|
lc_vals={
|
|
|
|
'date': obj.date_from,
|
|
|
|
'period_id': obj.period_id.id,
|
|
|
|
'date_from': obj.date_from,
|
|
|
|
'date_to': obj.date_to,
|
|
|
|
'cycle_id': obj.cycle_id.id,
|
|
|
|
'branch_id': obj.branch_id.id,
|
|
|
|
'report_type': 'cross',
|
|
|
|
'department_id': obj.department_id.id,
|
|
|
|
}
|
|
|
|
lc_id=get_model('clinic.report.labor.cost').create(lc_vals)
|
|
|
|
obj.write({
|
|
|
|
'lc_report_id': lc_id,
|
|
|
|
})
|
|
|
|
|
|
|
|
report_lc=get_model("clinic.report.labor.cost").browse(lc_id)
|
|
|
|
report_lc.write({
|
|
|
|
'date': obj.date_from,
|
|
|
|
'period_id': obj.period_id.id,
|
|
|
|
'date_from': obj.date_from,
|
|
|
|
'date_to': obj.date_to,
|
|
|
|
'cycle_id': obj.cycle_id.id,
|
|
|
|
'branch_id': obj.branch_id.id,
|
|
|
|
'report_type': 'cross',
|
|
|
|
'department_id': obj.department_id.id,
|
|
|
|
})
|
|
|
|
|
|
|
|
db.commit()
|
2015-07-30 03:44:41 +00:00
|
|
|
|
2015-07-30 02:13:02 +00:00
|
|
|
# generate labor costs
|
|
|
|
lcname='labor.cost.xlsx'
|
2015-07-30 03:44:41 +00:00
|
|
|
link='report_export_xls?model=clinic.report.labor.cost&template=report_labor_cost&active_id=%s'%(lc_id)
|
2015-07-30 02:13:02 +00:00
|
|
|
load_report(lcname,link)
|
2015-07-29 11:36:31 +00:00
|
|
|
|
2015-07-30 02:13:02 +00:00
|
|
|
# generate labor cost summary
|
2015-07-30 02:33:51 +00:00
|
|
|
sum_name='labor.cost-summary.xlsx'
|
2015-07-30 03:44:41 +00:00
|
|
|
link='report_export_xls?model=clinic.report.labor.cost.summary&template=report_labor_cost_summary&active_id=%s'%(sum_id)
|
2015-07-30 02:33:51 +00:00
|
|
|
load_report(sum_name,link)
|
2015-07-29 11:36:31 +00:00
|
|
|
|
2015-07-30 02:33:51 +00:00
|
|
|
obj.write({
|
|
|
|
'report_summary': sum_name,
|
|
|
|
'report_labor_cost': lcname,
|
|
|
|
})
|
|
|
|
db.commit()
|
2015-07-29 15:56:58 +00:00
|
|
|
|
|
|
|
report_lines=[
|
|
|
|
{'report_file': 'report_detail','model': 'clinic.report.labor.cost.detail', 'report_id': obj.dt_report_id.id,'field_name': 'dt_report_id'},
|
|
|
|
{'report_file': 'report_sub_detail','model': 'clinic.report.labor.cost.sub.detail', 'report_id': obj.sub_dt_report_id.id,'field_name': 'sub_dt_report_id'},
|
|
|
|
]
|
2015-07-29 11:36:31 +00:00
|
|
|
for line in obj.lines:
|
|
|
|
staff=line.staff_id
|
2015-07-29 15:56:58 +00:00
|
|
|
for report_line in report_lines:
|
2015-12-11 08:43:01 +00:00
|
|
|
name='%s-%s'%(staff.first_name,staff.number)
|
|
|
|
name=name.replace(" ","-")
|
2015-07-29 15:56:58 +00:00
|
|
|
fname='%s-%s.xlsx'%(name,report_line['report_file'])
|
2015-11-19 07:08:33 +00:00
|
|
|
print('fname ---> ', fname)
|
2015-07-29 15:56:58 +00:00
|
|
|
report_model=report_line['model']
|
|
|
|
report_template=report_model.replace(".","_").replace("clinic_","")
|
|
|
|
report_id=report_line['report_id']
|
|
|
|
if not report_id:
|
|
|
|
report_id=get_model(report_model).create({
|
|
|
|
'date': obj.date_from,
|
|
|
|
'date_from': obj.date_from,
|
|
|
|
'date_to': obj.date_to,
|
|
|
|
'cycle_id': obj.cycle_id.id,
|
|
|
|
'branch_id': obj.branch_id.id,
|
|
|
|
'department_id': obj.department_id.id,
|
|
|
|
'staff_id': staff.id,
|
2015-07-30 02:13:02 +00:00
|
|
|
'staff_type': staff.type
|
2015-07-29 15:56:58 +00:00
|
|
|
})
|
|
|
|
report_obj=get_model(report_model).browse(report_id)
|
|
|
|
db.commit()
|
|
|
|
else:
|
|
|
|
report_obj=get_model(report_model).browse(report_id)
|
|
|
|
report_obj.write({
|
|
|
|
'date': obj.date_from,
|
|
|
|
'date_from': obj.date_from,
|
|
|
|
'date_to': obj.date_to,
|
|
|
|
'cycle_id': obj.cycle_id.id,
|
|
|
|
'branch_id': obj.branch_id.id,
|
|
|
|
'department_id': obj.department_id.id,
|
|
|
|
'staff_id': staff.id,
|
2015-07-30 02:13:02 +00:00
|
|
|
'staff_type': staff.type
|
2015-07-29 15:56:58 +00:00
|
|
|
})
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
obj.write({
|
|
|
|
report_line['field_name']: report_id,
|
|
|
|
})
|
|
|
|
db.commit()
|
2015-07-30 03:44:41 +00:00
|
|
|
link='report_export_xls?model=%s&template=%s&active_id=%s'%(report_model,report_template,report_obj.id)
|
2015-07-29 15:56:58 +00:00
|
|
|
load_report(fname,link)
|
|
|
|
db.commit()
|
2015-11-19 07:08:33 +00:00
|
|
|
|
|
|
|
os.system("cp -rf %s ."%tmp_dir)
|
2015-12-11 08:43:01 +00:00
|
|
|
|
|
|
|
os.system("cp merge.py labor_cost/")
|
2015-12-11 09:08:46 +00:00
|
|
|
os.system("python labor_cost/merge.py labor_cost %s"%(obj.name))
|
2015-12-11 08:43:01 +00:00
|
|
|
os.system("rm labor_cost/*.py")
|
|
|
|
os.system("rm labor_cost/*.xlsx")
|
|
|
|
|
2015-11-19 07:08:33 +00:00
|
|
|
os.system("mv labor_cost %s"%obj.name)
|
|
|
|
os.system("zip %s.zip %s/*"%(obj.name,obj.name))
|
|
|
|
os.system("mv %s.zip %s/"%(obj.name,fdir))
|
|
|
|
os.system("rm -r %s*"%(obj.name))
|
|
|
|
os.system("rm %s/*"%tmp_dir)
|
2015-12-11 08:43:01 +00:00
|
|
|
|
2015-11-19 07:08:33 +00:00
|
|
|
obj.write({
|
|
|
|
'zip_file': '%s.zip'%obj.name,
|
|
|
|
'state': 'done',
|
|
|
|
})
|
|
|
|
|
2015-07-30 03:44:41 +00:00
|
|
|
vals={
|
|
|
|
'to_id': get_active_user(),
|
|
|
|
'subject': 'Generate Report',
|
|
|
|
'body': """
|
2015-11-19 07:08:33 +00:00
|
|
|
Generate labor cost report for successfully.
|
2015-07-30 03:44:41 +00:00
|
|
|
""",
|
|
|
|
}
|
|
|
|
get_model("message").create(vals)
|
|
|
|
db.commit()
|
2015-07-29 01:44:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def onchange_branch(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
data['department_id']=None
|
|
|
|
return data
|
|
|
|
|
|
|
|
def onchange_from(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
data['date_to']=data['date_from']
|
|
|
|
return data
|
|
|
|
|
|
|
|
def onchange_period(self,context={}):
|
|
|
|
data=context['data']
|
|
|
|
period_id=data['period_id']
|
|
|
|
period=get_model('clinic.period.line').browse(period_id)
|
|
|
|
data['date_from']=period.date_start
|
|
|
|
data['date_to']=period.date_stop
|
|
|
|
return data
|
|
|
|
|
|
|
|
PrintLaborCost.register()
|