clinic/netforce_clinic/models/print_labor_cost.py

226 lines
8.1 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"),
'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]
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()
#period_name='%sto%s'%(obj.date_from,obj.date_to[-2:])
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)
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)
obj.write({
'report_summary': sum_name,
'report_labor_cost': lcname,
})
db.commit()
for line in obj.lines:
staff=line.staff_id
report_sum.write({
'staff_id': staff.id,
})
db.commit()
#period_name='%sto%s'%(obj.date_from,obj.date_to[-2:])
#fname='%s-%s-%s-detail.xlsx'%(staff.name,period_name,active_id)
#link='http://localhost:9999/report_export_xls?model=clinic.report.labor.cost.summary&template=report_labor_cost_summary&active_id=%s'%(active_id)
#load_report(fname,link)
#line.write({
#'report_detail': fname,
#})
#db.commit()
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()