333 lines
13 KiB
Python
333 lines
13 KiB
Python
import os
|
|
import time
|
|
import urllib
|
|
from calendar import monthrange
|
|
|
|
from netforce.model import Model,fields,get_model
|
|
from netforce.database import get_active_db, get_connection
|
|
from netforce.access import get_active_user, get_active_company
|
|
import netforce.config as config
|
|
|
|
class PrintLaborCost(Model):
|
|
_name="clinic.print.labor.cost"
|
|
_string="Report Labor Cost"
|
|
|
|
def _get_name(self,ids,context={}):
|
|
res={}
|
|
for obj in self.browse(ids):
|
|
year_frm,month_frm,day_frm=obj.date_from.split("-")
|
|
year_to,month_to,day_to=obj.date_to.split("-")
|
|
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
|
|
name+='-'+obj.staff_type
|
|
res[obj.id]=name
|
|
return res
|
|
|
|
_fields={
|
|
"name": fields.Char("Name",function="_get_name"),
|
|
"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"),
|
|
"staff_type": fields.Selection([["doctor","Doctor"],["nurse","Nurse"]],"Type"),
|
|
'staff_id': fields.Many2One("clinic.staff","Staff"),
|
|
'lines': fields.One2Many("clinic.print.labor.cost.line","print_labor_cost_id","Lines"),
|
|
'sum_report_id': fields.Many2One("clinic.report.labor.cost.summary","Summary Report"),
|
|
'lc_report_id': fields.Many2One("clinic.report.labor.cost","Labor Cost Report"),
|
|
'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"),
|
|
'report_summary': fields.File("Summary"),
|
|
'report_labor_cost': fields.File("Labor Cost"),
|
|
'state': fields.Selection([['draft','Draft'],['waiting_doc','Waiting Document'],['done','Done']],'State'),
|
|
'company_id': fields.Many2One('company','Company'),
|
|
'zip_file': fields.File("File"),
|
|
}
|
|
|
|
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
|
|
|
|
def reset_data(self,ids,context={}):
|
|
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']
|
|
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']
|
|
lines.append(('create',{
|
|
'staff_id': staff_id,
|
|
'staff_name': staff_name,
|
|
'total_amount': total_amount,
|
|
'total_qty': total_qty,
|
|
}))
|
|
obj.write({
|
|
'lines': lines,
|
|
})
|
|
|
|
def do_generate(self,ids,context={}):
|
|
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)
|
|
|
|
def generate_report(self,ids,context={}):
|
|
db=get_connection()
|
|
obj=self.browse(ids)[0]
|
|
obj.write({
|
|
'state': 'waiting_doc',
|
|
'zip_file': None,
|
|
})
|
|
db.commit()
|
|
# clear and reload
|
|
obj.reset_data()
|
|
def load_report(fname,link):
|
|
host=config.config['host']
|
|
port=config.config['port']
|
|
hostname='http://%s:%s/'%(host,port)
|
|
link=hostname+link
|
|
req=urllib.request.Request(link)
|
|
response=urllib.request.urlopen(req)
|
|
data=response.read()
|
|
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))
|
|
|
|
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()
|
|
|
|
# generate labor costs
|
|
lcname='labor.cost.xlsx'
|
|
link='report_export_xls?model=clinic.report.labor.cost&template=report_labor_cost&active_id=%s'%(lc_id)
|
|
load_report(lcname,link)
|
|
|
|
# generate labor cost summary
|
|
sum_name='labor.cost-summary.xlsx'
|
|
link='report_export_xls?model=clinic.report.labor.cost.summary&template=report_labor_cost_summary&active_id=%s'%(sum_id)
|
|
load_report(sum_name,link)
|
|
|
|
obj.write({
|
|
'report_summary': sum_name,
|
|
'report_labor_cost': lcname,
|
|
})
|
|
db.commit()
|
|
|
|
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'},
|
|
]
|
|
for line in obj.lines:
|
|
staff=line.staff_id
|
|
for report_line in report_lines:
|
|
#name=staff.number or staff.name or staff.id
|
|
name=staff.number+"-"+(staff.name or staff.id)
|
|
fname='%s-%s.xlsx'%(name,report_line['report_file'])
|
|
print('fname ---> ', fname)
|
|
#report_file=report_line['report_file']
|
|
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,
|
|
'staff_type': staff.type
|
|
})
|
|
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,
|
|
'staff_type': staff.type
|
|
})
|
|
db.commit()
|
|
|
|
obj.write({
|
|
report_line['field_name']: report_id,
|
|
})
|
|
db.commit()
|
|
link='report_export_xls?model=%s&template=%s&active_id=%s'%(report_model,report_template,report_obj.id)
|
|
load_report(fname,link)
|
|
db.commit()
|
|
|
|
os.system("cp -rf %s ."%tmp_dir)
|
|
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)
|
|
obj.write({
|
|
'zip_file': '%s.zip'%obj.name,
|
|
'state': 'done',
|
|
})
|
|
|
|
vals={
|
|
'to_id': get_active_user(),
|
|
'subject': 'Generate Report',
|
|
'body': """
|
|
Generate labor cost report for successfully.
|
|
""",
|
|
}
|
|
get_model("message").create(vals)
|
|
db.commit()
|
|
|
|
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()
|