1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
30 from interface import report_int
32 from osv.osv import except_osv
33 from osv.orm import browse_null
34 from osv.orm import browse_record_list
36 from xml.dom import minidom
40 from lxml import etree
42 class external_pdf(render.render):
43 def __init__(self, pdf):
44 render.render.__init__(self)
46 self.output_type='pdf'
53 #TODO: devrait heriter de report_rml a la place de report_int
54 # -> pourrait overrider que create_xml a la place de tout create
55 # heuu, ca marche pas ds tous les cas car graphs sont generes en pdf directment
56 # par pychart, et on passe donc pas par du rml
57 class report_custom(report_int):
58 def __init__(self, name):
59 report_int.__init__(self, name)
62 # fields = [['address','city'],['name'], ['zip']]
63 # conditions = [[('zip','==','3'),(,)],(,),(,)] #same structure as fields
64 # row_canvas = ['Rue', None, None]
66 # [ ['ville','name','zip'] ]
68 def _row_get(self, cr, uid, objs, fields, conditions, row_canvas=None, group_by=None):
73 for cond in conditions:
76 temp = c[0](eval('obj.'+c[1]))
77 if not eval('\''+temp+'\''+' '+c[2]+' '+'\''+str(c[3])+'\''):
83 for i in range(len(fields)):
85 row.append(row_canvas and row_canvas[i])
88 elif len(fields[i])==1:
89 if not isinstance(obj, browse_null):
90 row.append(str(eval('obj.'+fields[i][0])))
95 levels[fields[i][0]]=True
99 # Process group_by data first
101 if group_by != None and fields[group_by] != None:
102 if fields[group_by][0] in levels.keys():
103 key.append(fields[group_by][0])
104 for l in levels.keys():
105 if l != fields[group_by][0]:
110 objs = eval('obj.'+l)
111 if not isinstance(objs, browse_record_list) and type(objs) <> type([]):
115 for f in range(len(fields)):
116 if (fields[f] and fields[f][0])==l:
117 field_new.append(fields[f][1:])
118 cond_new.append(conditions[f][1:])
120 field_new.append(None)
121 cond_new.append(None)
123 result += self._row_get(cr, uid, objs, field_new, cond_new, row, group_by)
129 def create(self, cr, uid, ids, datas, context=None):
132 self.pool = pooler.get_pool(cr.dbname)
133 report = self.pool.get('ir.report.custom').browse(cr, uid, [datas['report_id']])[0]
134 datas['model'] = report.model_id.model
136 ids = self.pool.get(report.model_id.model).search(cr, uid, [])
139 service = netsvc.LocalService("object_proxy")
140 report_id = datas['report_id']
141 report = service.execute(cr.dbname, uid, 'ir.report.custom', 'read', [report_id], context=context)[0]
142 fields = service.execute(cr.dbname, uid, 'ir.report.custom.fields', 'read', report['fields_child0'], context=context)
144 fields.sort(lambda x,y : x['sequence'] - y['sequence'])
146 if report['field_parent']:
147 parent_field = service.execute(cr.dbname, uid, 'ir.model.fields', 'read', [report['field_parent'][0]],['model'])
148 model_name = service.execute(cr.dbname, uid, 'ir.model', 'read', [report['model_id'][0]], ['model'],context=context)[0]['model']
151 fct['id'] = lambda x : x
152 fct['gety'] = lambda x: x.split('-')[0]
153 fct['in'] = lambda x: x.split(',')
160 field_child = f['field_child'+str(i)]
163 service.execute(cr.dbname, uid,
164 'ir.model.fields', 'read', [field_child[0]],
165 ['name'], context=context)[0]['name']
167 if f['fc'+str(i)+'_operande']:
169 cond_op = f['fc'+str(i)+'_op']
170 if len(f['fc'+str(i)+'_op'].split(',')) == 2:
171 cond_op = f['fc'+str(i)+'_op'].split(',')[1]
172 fct_name = f['fc'+str(i)+'_op'].split(',')[0]
173 cond.append((fct[fct_name], f['fc'+str(i)+'_operande'][1], cond_op, f['fc'+str(i)+'_condition']))
176 new_fields.append(row)
177 new_cond.append(cond)
178 objs = self.pool.get(model_name).browse(cr, uid, ids)
190 if report['field_parent']:
192 def build_tree(obj, level, depth):
193 res = self._row_get(cr, uid,[obj], new_fields, new_cond)
195 new_obj = eval('obj.'+report['field_parent'][1])
196 if not isinstance(new_obj, list) :
199 if not isinstance(o, browse_null):
200 res += build_tree(o, level, depth+1)
204 results += build_tree(obj, level, 0)
206 results = self._row_get(cr, uid,objs, new_fields, new_cond, group_by=groupby)
209 'calc_sum': lambda l: reduce(lambda x,y: float(x)+float(y), filter(None, l), 0),
210 'calc_avg': lambda l: reduce(lambda x,y: float(x)+float(y), filter(None, l), 0) / (len(filter(None, l)) or 1.0),
211 'calc_max': lambda l: reduce(lambda x,y: max(x,y), [(i or 0.0) for i in l], 0),
212 'calc_min': lambda l: reduce(lambda x,y: min(x,y), [(i or 0.0) for i in l], 0),
213 'calc_count': lambda l: len(filter(None, l)),
214 'False': lambda l: '\r\n'.join(filter(None, l)),
215 'groupby': lambda l: reduce(lambda x,y: x or y, l)
223 if not line[groupby] and prev in res_dic:
224 res_dic[prev].append(line)
227 if res_dic.has_key(line[groupby]):
228 res_dic[line[groupby]].append(line)
230 res_dic[line[groupby]] = []
231 res_dic[line[groupby]].append(line)
232 #we use the keys in results since they are ordered, whereas in res_dic.heys() they aren't
233 for key in filter(None, [x[groupby] for x in results]):
235 for col in range(len(fields)):
237 row.append(fct['groupby'](map(lambda x: x[col], res_dic[key])))
239 row.append(fct[str(fields[col]['operation'])](map(lambda x: x[col], res_dic[key])))
243 if report['type']=='table':
244 if report['field_parent']:
245 res = self._create_tree(uid, ids, report, fields, level, results, context)
248 for idx in range(len(fields)):
249 if fields[idx]['name'] == report['sortby']:
253 results.sort(lambda x,y : cmp(float(x[sort_idx]),float(y[sort_idx])))
255 results.sort(lambda x,y : cmp(x[sort_idx],y[sort_idx]))
257 results = results[:int(report['limitt'])]
258 res = self._create_table(uid, ids, report, fields, None, results, context)
259 elif report['type'] in ('pie','bar', 'line'):
264 for j in range(len(r)):
265 if j == 0 and not r[j]:
267 elif j == 0 and r[j]:
272 row.append(float(r[j]))
276 if report['type']=='pie':
277 res = self._create_pie(cr,uid, ids, report, fields, results2, context)
278 elif report['type']=='bar':
279 res = self._create_bars(cr,uid, ids, report, fields, results2, context)
280 elif report['type']=='line':
281 res = self._create_lines(cr,uid, ids, report, fields, results2, context)
282 return (self.obj.get(), 'pdf')
284 def _create_tree(self, uid, ids, report, fields, level, results, context):
285 pageSize=common.pageSize.get(report['print_format'], [210.0,297.0])
286 if report['print_orientation']=='landscape':
287 pageSize=[pageSize[1],pageSize[0]]
289 impl = minidom.getDOMImplementation()
290 new_doc = impl.createDocument(None, "report", None)
293 config = new_doc.createElement("config")
295 def _append_node(name, text):
296 n = new_doc.createElement(name)
297 t = new_doc.createTextNode(text)
299 config.appendChild(n)
301 _append_node('date', time.strftime('%d/%m/%Y'))
302 _append_node('PageFormat', '%s' % report['print_format'])
303 _append_node('PageSize', '%.2fmm,%.2fmm' % tuple(pageSize))
304 _append_node('PageWidth', '%.2f' % (pageSize[0] * 2.8346,))
305 _append_node('PageHeight', '%.2f' %(pageSize[1] * 2.8346,))
307 length = pageSize[0]-30-reduce(lambda x,y:x+(y['width'] or 0), fields, 0)
310 if not f['width']: count+=1
313 f['width']=round((float(length)/count)-0.5)
315 _append_node('tableSize', '%s' % ','.join(map(lambda x: '%.2fmm' % (x['width'],), fields)))
316 _append_node('report-header', '%s' % (report['title'],))
317 _append_node('report-footer', '%s' % (report['footer'],))
319 new_doc.childNodes[0].appendChild(config)
320 header = new_doc.createElement("header")
323 field = new_doc.createElement("field")
324 field_txt = new_doc.createTextNode('%s' % (f['name'],))
325 field.appendChild(field_txt)
326 header.appendChild(field)
328 new_doc.childNodes[0].appendChild(header)
330 lines = new_doc.createElement("lines")
334 node_line = new_doc.createElement("row")
336 for f in range(len(fields)):
337 col = new_doc.createElement("col")
339 col.setAttribute('para','yes')
340 col.setAttribute('tree','yes')
341 col.setAttribute('space',str(3*shift)+'mm')
343 txt = new_doc.createTextNode(prefix+str(line[f]) or '')
345 txt = new_doc.createTextNode('/')
347 node_line.appendChild(col)
349 lines.appendChild(node_line)
351 new_doc.childNodes[0].appendChild(lines)
353 transform = etree.XSLT(
354 etree.parse(os.path.join(tools.config['root_path'],
355 'addons/base/report/custom_new.xsl')))
356 rml = etree.tostring(
357 transform(etree.fromstring(new_doc.toxml())))
359 self.obj = render.rml(rml)
364 def _create_lines(self, cr, uid, ids, report, fields, results, context):
365 service = netsvc.LocalService("object_proxy")
366 pdf_string = cStringIO.StringIO()
367 can = canvas.init(fname=pdf_string, format='pdf')
369 can.show(80,380,'/16/H'+report['title'])
371 ar = area.T(size=(350,350),
372 #x_coord = category_coord.T(['2005-09-01','2005-10-22'],0),
373 x_axis = axis.X(label = fields[0]['name'], format="/a-30{}%s"),
374 y_axis = axis.Y(label = ', '.join(map(lambda x : x['name'], fields[1:]))))
377 process_date['D'] = lambda x : reduce(lambda xx,yy : xx+'-'+yy,x.split('-')[1:3])
378 process_date['M'] = lambda x : x.split('-')[1]
379 process_date['Y'] = lambda x : x.split('-')[0]
382 order_date['D'] = lambda x : time.mktime((2005,int(x.split('-')[0]), int(x.split('-')[1]),0,0,0,0,0,0))
383 order_date['M'] = lambda x : x
384 order_date['Y'] = lambda x : x
393 field_id = (f['field_child3'] and f['field_child3'][0]) or (f['field_child2'] and f['field_child2'][0]) or (f['field_child1'] and f['field_child1'][0]) or (f['field_child0'] and f['field_child0'][0])
395 type = service.execute(cr.dbname, uid, 'ir.model.fields', 'read', [field_id],['ttype'])
396 if type[0]['ttype'] == 'date':
398 fct[idx] = process_date[report['frequency']]
400 fct[idx] = lambda x : x
402 fct[idx] = lambda x : x
405 # plots are usually displayed year by year
406 # so we do so if the first field is a date
410 key = process_date['Y'](r[date_idx])
411 if not data_by_year.has_key(key):
412 data_by_year[key] = []
413 for i in range(len(r)):
415 data_by_year[key].append(r)
417 data_by_year[''] = results
420 nb_bar = len(data_by_year)*(len(fields)-1)
421 colors = map(lambda x:line_style.T(color=x), misc.choice_colors(nb_bar))
423 for line in data_by_year.keys():
425 # sum data and save it in a list. An item for a fields
426 for d in data_by_year[line]:
427 for idx in range(len(fields)-1):
428 fields_bar.append({})
429 if fields_bar[idx].has_key(d[0]):
430 fields_bar[idx][d[0]] += d[idx+1]
432 fields_bar[idx][d[0]] = d[idx+1]
433 for idx in range(len(fields)-1):
435 for k in fields_bar[idx].keys():
437 data[k] += fields_bar[idx][k]
439 data[k] = fields_bar[idx][k]
444 # cumulate if necessary
446 data_cum.append([k, float(data[k])+float(prev)])
447 if fields[idx+1]['cumulate']:
450 plot = line_plot.T(label=fields[idx+1]['name']+' '+str(line), data = data_cum, line_style=colors[idx0*(len(fields)-1)+idx])
452 abscissa.update(fields_bar[idx])
455 abscissa = map(lambda x : [x, None], abscissa)
456 ar.x_coord = category_coord.T(abscissa,0)
460 self.obj = external_pdf(pdf_string.getvalue())
467 def _create_bars(self, cr, uid, ids, report, fields, results, context):
468 service = netsvc.LocalService("object_proxy")
469 pdf_string = cStringIO.StringIO()
470 can = canvas.init(fname=pdf_string, format='pdf')
472 can.show(80,380,'/16/H'+report['title'])
475 process_date['D'] = lambda x : reduce(lambda xx,yy : xx+'-'+yy,x.split('-')[1:3])
476 process_date['M'] = lambda x : x.split('-')[1]
477 process_date['Y'] = lambda x : x.split('-')[0]
480 order_date['D'] = lambda x : time.mktime((2005,int(x.split('-')[0]), int(x.split('-')[1]),0,0,0,0,0,0))
481 order_date['M'] = lambda x : x
482 order_date['Y'] = lambda x : x
484 ar = area.T(size=(350,350),
485 x_axis = axis.X(label = fields[0]['name'], format="/a-30{}%s"),
486 y_axis = axis.Y(label = ', '.join(map(lambda x : x['name'], fields[1:]))))
492 field_id = (f['field_child3'] and f['field_child3'][0]) or (f['field_child2'] and f['field_child2'][0]) or (f['field_child1'] and f['field_child1'][0]) or (f['field_child0'] and f['field_child0'][0])
494 type = service.execute(cr.dbname, uid, 'ir.model.fields', 'read', [field_id],['ttype'])
495 if type[0]['ttype'] == 'date':
497 fct[idx] = process_date[report['frequency']]
499 fct[idx] = lambda x : x
501 fct[idx] = lambda x : x
504 # plot are usually displayed year by year
505 # so we do so if the first field is a date
509 key = process_date['Y'](r[date_idx])
510 if not data_by_year.has_key(key):
511 data_by_year[key] = []
512 for i in range(len(r)):
514 data_by_year[key].append(r)
516 data_by_year[''] = results
519 nb_bar = len(data_by_year)*(len(fields)-1)
520 colors = map(lambda x:fill_style.Plain(bgcolor=x), misc.choice_colors(nb_bar))
523 for line in data_by_year.keys():
525 # sum data and save it in a list. An item for a fields
526 for d in data_by_year[line]:
527 for idx in range(len(fields)-1):
528 fields_bar.append({})
529 if fields_bar[idx].has_key(d[0]):
530 fields_bar[idx][d[0]] += d[idx+1]
532 fields_bar[idx][d[0]] = d[idx+1]
533 for idx in range(len(fields)-1):
535 for k in fields_bar[idx].keys():
537 data[k] += fields_bar[idx][k]
539 data[k] = fields_bar[idx][k]
544 # cumulate if necessary
546 data_cum.append([k, float(data[k])+float(prev)])
547 if fields[idx+1]['cumulate']:
551 plot = bar_plot.T(label=fields[idx+1]['name']+' '+str(line), data = data_cum, cluster=(idx0*(len(fields)-1)+idx,nb_bar), fill_style=colors[idx0*(len(fields)-1)+idx])
553 abscissa.update(fields_bar[idx])
555 abscissa = map(lambda x : [x, None], abscissa)
557 ar.x_coord = category_coord.T(abscissa,0)
561 self.obj = external_pdf(pdf_string.getvalue())
566 def _create_pie(self, cr, uid, ids, report, fields, results, context):
567 pdf_string = cStringIO.StringIO()
568 can = canvas.init(fname=pdf_string, format='pdf')
569 ar = area.T(size=(350,350), legend=legend.T(),
570 x_grid_style = None, y_grid_style = None)
571 colors = map(lambda x:fill_style.Plain(bgcolor=x), misc.choice_colors(len(results)))
573 if reduce(lambda x,y : x+y, map(lambda x : x[1],results)) == 0.0:
574 raise except_osv(_('Error'), _("The sum of the data (2nd field) is null.\nWe can't draw a pie chart !"))
576 plot = pie_plot.T(data=results, arc_offsets=[0,10,0,10],
577 shadow = (2, -2, fill_style.gray50),
579 arrow_style = arrow.a3,
584 self.obj = external_pdf(pdf_string.getvalue())
589 def _create_table(self, uid, ids, report, fields, tree, results, context):
590 pageSize=common.pageSize.get(report['print_format'], [210.0,297.0])
591 if report['print_orientation']=='landscape':
592 pageSize=[pageSize[1],pageSize[0]]
594 impl = minidom.getDOMImplementation()
595 new_doc = impl.createDocument(None, "report", None)
598 config = new_doc.createElement("config")
600 def _append_node(name, text):
601 n = new_doc.createElement(name)
602 t = new_doc.createTextNode(text)
604 config.appendChild(n)
606 _append_node('date', time.strftime('%d/%m/%Y'))
607 _append_node('PageSize', '%.2fmm,%.2fmm' % tuple(pageSize))
608 _append_node('PageFormat', '%s' % report['print_format'])
609 _append_node('PageWidth', '%.2f' % (pageSize[0] * 2.8346,))
610 _append_node('PageHeight', '%.2f' %(pageSize[1] * 2.8346,))
612 length = pageSize[0]-30-reduce(lambda x,y:x+(y['width'] or 0), fields, 0)
615 if not f['width']: count+=1
618 f['width']=round((float(length)/count)-0.5)
620 _append_node('tableSize', '%s' % ','.join(map(lambda x: '%.2fmm' % (x['width'],), fields)))
621 _append_node('report-header', '%s' % (report['title'],))
622 _append_node('report-footer', '%s' % (report['footer'],))
624 new_doc.childNodes[0].appendChild(config)
625 header = new_doc.createElement("header")
628 field = new_doc.createElement("field")
629 field_txt = new_doc.createTextNode('%s' % (f['name'],))
630 field.appendChild(field_txt)
631 header.appendChild(field)
633 new_doc.childNodes[0].appendChild(header)
635 lines = new_doc.createElement("lines")
637 node_line = new_doc.createElement("row")
638 for f in range(len(fields)):
639 col = new_doc.createElement("col")
640 col.setAttribute('tree','no')
642 txt = new_doc.createTextNode(str(line[f] or ''))
644 txt = new_doc.createTextNode('/')
646 node_line.appendChild(col)
647 lines.appendChild(node_line)
649 new_doc.childNodes[0].appendChild(lines)
651 transform = etree.XSLT(
652 etree.parse(os.path.join(tools.config['root_path'],
653 'addons/base/report/custom_new.xsl')))
654 rml = etree.tostring(
655 transform(etree.fromstring(new_doc.toxml())))
657 self.obj = render.rml(rml)
660 report_custom('report.custom')
663 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: