clinic/netforce_clinic/models/print_labor_cost.py

268 lines
11 KiB
Python
Raw Normal View History

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-07-29 01:44:03 +00:00
class PrintLaborCost(Model):
_name="clinic.print.labor.cost"
_string="Print Report Labor Cost"
_fields={
"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-07-29 01:44:03 +00:00
}
def reload(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']
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-29 11:36:31 +00:00
def generate_report(self,ids,context={}):
obj=self.browse(ids)[0]
2015-07-29 15:56:58 +00:00
# clear and reload
obj.reload()
2015-07-29 11:36:31 +00:00
def load_report(fname,link):
req=urllib.request.Request(link)
response=urllib.request.urlopen(req)
data=response.read()
dbname=get_active_db()
fdir=os.path.join("static","db",dbname,"files")
open(os.path.join(fdir,fname),"wb").write(data)
db=get_connection()
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 02:13:02 +00:00
# generate labor costs
lcname='labor.cost.xlsx'
link='http://localhost:9999/report_export_xls?model=clinic.report.labor.cost&template=report_labor_cost&active_id=%s'%(lc_id)
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'
link='http://localhost:9999/report_export_xls?model=clinic.report.labor.cost.summary&template=report_labor_cost_summary&active_id=%s'%(sum_id)
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'},
{'report_file': 'report_daily','model': 'clinic.report.labor.cost.daily', 'report_id': obj.dl_report_id.id,'field_name': 'dl_report_id'},
{'report_file': 'report_ot','model': 'clinic.report.labor.cost.overtime', 'report_id': obj.ot_report_id.id,'field_name': 'ot_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:
name=staff.number or staff.name or staff.id
fname='%s-%s.xlsx'%(name,report_line['report_file'])
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,
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()
link='http://localhost:9999/report_export_xls?model=%s&template=%s&active_id=%s'%(report_model,report_template,report_obj.id)
load_report(fname,link)
line.write({
report_file: fname,
})
db.commit()
2015-07-29 11:36:31 +00:00
print("Done!")
2015-07-29 01:44:03 +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,
}
return res
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()