zip docoment

production
watcha.h 2015-11-19 14:08:33 +07:00
parent dca8c54e02
commit 8e091e131b
3 changed files with 49 additions and 40 deletions

View File

@ -10,6 +10,7 @@
<field name="department_id" domain='[["branch_id","=",branch_id]]' span="2"/>
<field name="cycle_id" span="2"/>
<field name="staff_type" required="1" span="2"/>
<field name="zip_file" span="2"/>
</group>
<foot>
<button string="Generate Report" method="do_generate" icon="arrow-right" type="default"/>

View File

@ -1,5 +1,5 @@
<list model="clinic.print.labor.cost">
<field name="period_id"/>
<field name="report_file"/>
<field name="zip_file"/>
<field name="state"/>
</list>

View File

@ -15,8 +15,8 @@ class PrintLaborCost(Model):
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(",")
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
@ -24,7 +24,7 @@ class PrintLaborCost(Model):
name+=obj.branch_id.name
if obj.department_id:
name+=obj.department_id.name
name+='-'%obj.type
name+='-'+obj.staff_type
res[obj.id]=name
return res
@ -48,9 +48,9 @@ class PrintLaborCost(Model):
'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'],['pending','Pending'],['done','Done']],'State'),
'state': fields.Selection([['draft','Draft'],['waiting_doc','Waiting Document'],['done','Done']],'State'),
'company_id': fields.Many2One('company','Company'),
'report_file': fields.File("File"),
'zip_file': fields.File("File"),
}
def _get_date_from(self,context={}):
@ -87,7 +87,7 @@ class PrintLaborCost(Model):
}
return res
def reload(self,ids,context={}):
def reset_data(self,ids,context={}):
obj=self.browse(ids)[0]
context['defaults']={
'date_from': obj.date_from,
@ -120,32 +120,26 @@ class PrintLaborCost(Model):
})
def do_generate(self,ids,context={}):
dom=[
['model', '=','clinic.print.labor.cost'],
['method','=','generate_report'],
]
for cj in get_model('cron.job').search_browse(dom):
cj.write({
'state': 'waiting',
})
vals={
'to_id': get_active_user(),
'subject': 'Generate Report',
'body': """
Please wait until report is generated.
""",
}
get_model("message").create(vals)
return {
'next': {
'type': 'reload',
}
}
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.reload()
obj.reset_data()
def load_report(fname,link):
host=config.config['host']
port=config.config['port']
@ -154,11 +148,15 @@ class PrintLaborCost(Model):
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")
open(os.path.join(fdir,fname),"wb").write(data)
tmp_dir='/tmp/labor_cost'
if not os.path.isdir(tmp_dir):
os.system("mkdir %s"%(tmp_dir))
db=get_connection()
sum_id=obj.sum_report_id.id
if not sum_id:
sum_id=get_model('clinic.report.labor.cost.summary').create({
@ -240,9 +238,11 @@ class PrintLaborCost(Model):
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 or staff.name or staff.id
name=staff.number+"-"+(staff.name or staff.id)
fname='%s-%s.xlsx'%(name,report_line['report_file'])
report_file=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']
@ -279,15 +279,24 @@ class PrintLaborCost(Model):
db.commit()
link='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()
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': """
Finish to generate report.
Generate labor cost report for successfully.
""",
}
get_model("message").create(vals)
@ -320,5 +329,4 @@ class PrintLaborCost(Model):
data['date_to']=period.date_stop
return data
PrintLaborCost.register()