1
##############################################################################
3
# Copyright (c) 2004-2005 TINY SPRL. (http://tiny.be) All Rights Reserved.
5
# $Id: custom.py 1305 2005-09-08 14:39:51Z kayhman $
7
# WARNING: This program as such is intended to be used by professional
8
# programmers who take the whole responsability of assessing all potential
9
# consequences resulting from its eventual inadequacies and bugs
10
# End users who are looking for a ready-to-use solution with commercial
11
# garantees and support are strongly adviced to contract a Free Software
14
# This program is Free Software; you can redistribute it and/or
15
# modify it under the terms of the GNU General Public License
16
# as published by the Free Software Foundation; either version 2
17
# of the License, or (at your option) any later version.
19
# This program is distributed in the hope that it will be useful,
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
# GNU General Public License for more details.
24
# You should have received a copy of the GNU General Public License
25
# along with this program; if not, write to the Free Software
26
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28
##############################################################################
36
from interface import report_int
38
from osv.orm import browse_null
39
from osv.orm import browse_record_list
41
from xml.dom import minidom
48
class external_pdf(render.render):
49
def __init__(self, pdf):
50
render.render.__init__(self)
52
self.output_type='pdf'
59
#TODO: devrait heriter de report_rml a la place de report_int
60
# -> pourrait overrider que create_xml a la place de tout create
61
# heuu, ca marche pas ds tous les cas car graphs sont generes en pdf directment
62
# par pychart, et on passe donc pas par du rml
63
class report_custom(report_int):
64
def __init__(self, name):
65
report_int.__init__(self, name)
68
# fields = [['address','city'],['name'], ['zip']]
69
# conditions = [[('zip','==','3'),(,)],(,),(,)] #same structure as fields
70
# row_canvas = ['Rue', None, None]
72
# [ ['ville','name','zip'] ]
74
def _row_get(self, cr, uid, objs, fields, conditions, row_canvas=None, group_by=None):
79
for cond in conditions:
82
temp = c[0](eval('obj.'+c[1]))
83
if not eval('\''+temp+'\''+' '+c[2]+' '+'\''+str(c[3])+'\''):
89
for i in range(len(fields)):
91
row.append(row_canvas and row_canvas[i])
94
elif len(fields[i])==1:
95
if not isinstance(obj, browse_null):
96
row.append(str(eval('obj.'+fields[i][0])))
101
levels[fields[i][0]]=True
105
# Process group_by data first
107
if group_by != None and fields[group_by] != None:
108
if fields[group_by][0] in levels.keys():
109
key.append(fields[group_by][0])
110
for l in levels.keys():
111
if l != fields[group_by][0]:
116
objs = eval('obj.'+l)
117
if not isinstance(objs, browse_record_list) and type(objs) <> type([]):
121
for f in range(len(fields)):
122
if (fields[f] and fields[f][0])==l:
123
field_new.append(fields[f][1:])
124
cond_new.append(conditions[f][1:])
126
field_new.append(None)
127
cond_new.append(None)
129
result += self._row_get(cr, uid, objs, field_new, cond_new, row, group_by)
135
def create(self, cr, uid, ids, datas, context={}):
136
self.pool = pooler.get_pool(cr.dbname)
137
report = self.pool.get('ir.report.custom').browse(cr, uid, [datas['report_id']])[0]
138
datas['model'] = report.model_id.model
140
ids = self.pool.get(report.model_id.model).search(cr, uid, [])
143
service = netsvc.LocalService("object_proxy")
144
report_id = datas['report_id']
145
report = service.execute(cr.dbname, uid, 'ir.report.custom', 'read', [report_id], context=context)[0]
146
fields = service.execute(cr.dbname, uid, 'ir.report.custom.fields', 'read', report['fields_child0'], context=context)
148
fields.sort(lambda x,y : x['sequence'] - y['sequence'])
150
if report['field_parent']:
151
parent_field = service.execute(cr.dbname, uid, 'ir.model.fields', 'read', [report['field_parent'][0]],['model'])
152
model_name = service.execute(cr.dbname, uid, 'ir.model', 'read', [report['model_id'][0]], ['model'],context=context)[0]['model']
155
fct['id'] = lambda x : x
156
fct['gety'] = lambda x: x.split('-')[0]
157
fct['in'] = lambda x: x.split(',')
164
if f['field_child'+str(i)]:
165
row.append(f['field_child'+str(i)][1])
166
if f['fc'+str(i)+'_operande']:
168
cond_op = f['fc'+str(i)+'_op']
169
if len(f['fc'+str(i)+'_op'].split(',')) == 2:
170
cond_op = f['fc'+str(i)+'_op'].split(',')[1]
171
fct_name = f['fc'+str(i)+'_op'].split(',')[0]
172
cond.append((fct[fct_name], f['fc'+str(i)+'_operande'][1], cond_op, f['fc'+str(i)+'_condition']))
175
new_fields.append(row)
176
new_cond.append(cond)
177
objs = self.pool.get(model_name).browse(cr, uid, ids)
189
if report['field_parent']:
191
def build_tree(obj, level, depth):
192
res = self._row_get(cr, uid,[obj], new_fields, new_cond)
194
new_obj = eval('obj.'+report['field_parent'][1])
195
if not isinstance(new_obj, list) :
198
if not isinstance(o, browse_null):
199
res += build_tree(o, level, depth+1)
203
results += build_tree(obj, level, 0)
205
results = self._row_get(cr, uid,objs, new_fields, new_cond, group_by=groupby)
208
'calc_sum': lambda l: reduce(lambda x,y: float(x)+float(y), filter(None, l), 0),
209
'calc_avg': lambda l: reduce(lambda x,y: float(x)+float(y), filter(None, l), 0) / (len(filter(None, l)) or 1.0),
210
'calc_max': lambda l: reduce(lambda x,y: max(x,y), [(i or 0.0) for i in l], 0),
211
'calc_min': lambda l: reduce(lambda x,y: min(x,y), [(i or 0.0) for i in l], 0),
212
'calc_count': lambda l: len(filter(None, l)),
213
'False': lambda l: '\r\n'.join(filter(None, l)),
214
'groupby': lambda l: reduce(lambda x,y: x or y, l)
222
if not line[groupby] and prev in res_dic:
223
res_dic[prev].append(line)
226
if res_dic.has_key(line[groupby]):
227
res_dic[line[groupby]].append(line)
229
res_dic[line[groupby]] = []
230
res_dic[line[groupby]].append(line)
231
#we use the keys in results since they are ordered, whereas in res_dic.heys() they aren't
232
for key in filter(None, [x[groupby] for x in results]):
234
for col in range(len(fields)):
236
row.append(fct['groupby'](map(lambda x: x[col], res_dic[key])))
238
row.append(fct[str(fields[col]['operation'])](map(lambda x: x[col], res_dic[key])))
242
if report['type']=='table':
243
if report['field_parent']:
244
res = self._create_tree(uid, ids, report, fields, level, results, context)
247
for idx in range(len(fields)):
248
if fields[idx]['name'] == report['sortby']:
252
results.sort(lambda x,y : cmp(float(x[sort_idx]),float(y[sort_idx])))
254
results.sort(lambda x,y : cmp(x[sort_idx],y[sort_idx]))
256
results = results[:int(report['limitt'])]
257
res = self._create_table(uid, ids, report, fields, None, results, context)
258
elif report['type'] in ('pie','bar', 'line'):
263
for j in range(len(r)):
264
if j == 0 and not r[j]:
266
elif j == 0 and r[j]:
271
row.append(float(r[j]))
275
if report['type']=='pie':
276
res = self._create_pie(cr,uid, ids, report, fields, results2, context)
277
elif report['type']=='bar':
278
res = self._create_bars(cr,uid, ids, report, fields, results2, context)
279
elif report['type']=='line':
280
res = self._create_lines(cr,uid, ids, report, fields, results2, context)
281
return (self.obj.get(), 'pdf')
283
def _create_tree(self, uid, ids, report, fields, level, results, context):
284
pageSize=common.pageSize.get(report['print_format'], [210.0,297.0])
285
if report['print_orientation']=='landscape':
286
pageSize=[pageSize[1],pageSize[0]]
288
impl = minidom.getDOMImplementation()
289
new_doc = impl.createDocument(None, "report", None)
292
config = new_doc.createElement("config")
294
def _append_node(name, text):
295
n = new_doc.createElement(name)
296
t = new_doc.createTextNode(text)
298
config.appendChild(n)
300
_append_node('date', time.strftime('%d/%m/%Y'))
301
_append_node('PageFormat', '%s' % report['print_format'])
302
_append_node('PageSize', '%.2fmm,%.2fmm' % tuple(pageSize))
303
_append_node('PageWidth', '%.2f' % (pageSize[0] * 2.8346,))
304
_append_node('PageHeight', '%.2f' %(pageSize[1] * 2.8346,))
306
length = pageSize[0]-30-reduce(lambda x,y:x+(y['width'] or 0), fields, 0)
309
if not f['width']: count+=1
312
f['width']=round((float(length)/count)-0.5)
314
_append_node('tableSize', '%s' % ','.join(map(lambda x: '%.2fmm' % (x['width'],), fields)))
315
_append_node('report-header', '%s' % (report['title'],))
316
_append_node('report-footer', '%s' % (report['footer'],))
318
new_doc.childNodes[0].appendChild(config)
319
header = new_doc.createElement("header")
322
field = new_doc.createElement("field")
323
field_txt = new_doc.createTextNode('%s' % (f['name'],))
324
field.appendChild(field_txt)
325
header.appendChild(field)
327
new_doc.childNodes[0].appendChild(header)
329
lines = new_doc.createElement("lines")
333
node_line = new_doc.createElement("row")
335
for f in range(len(fields)):
336
col = new_doc.createElement("col")
338
col.setAttribute('para','yes')
339
col.setAttribute('tree','yes')
340
col.setAttribute('space',str(3*shift)+'mm')
342
txt = new_doc.createTextNode(prefix+str(line[f]) or '')
344
txt = new_doc.createTextNode('/')
346
node_line.appendChild(col)
348
lines.appendChild(node_line)
350
new_doc.childNodes[0].appendChild(lines)
352
styledoc = libxml2.parseFile(os.path.join(tools.config['root_path'],'addons/base/report/custom_new.xsl'))
353
style = libxslt.parseStylesheetDoc(styledoc)
354
doc = libxml2.parseDoc(new_doc.toxml())
355
rml_obj = style.applyStylesheet(doc, None)
356
rml = style.saveResultToString(rml_obj)
358
self.obj = render.rml(rml)
363
def _create_lines(self, cr, uid, ids, report, fields, results, context):
364
service = netsvc.LocalService("object_proxy")
365
pdf_string = StringIO.StringIO()
366
can = canvas.init(fname=pdf_string, format='pdf')
368
can.show(80,380,'/16/H'+report['title'])
370
ar = area.T(size=(350,350),
371
#x_coord = category_coord.T(['2005-09-01','2005-10-22'],0),
372
x_axis = axis.X(label = fields[0]['name'], format="/a-30{}%s"),
373
y_axis = axis.Y(label = ', '.join(map(lambda x : x['name'], fields[1:]))))
376
process_date['D'] = lambda x : reduce(lambda xx,yy : xx+'-'+yy,x.split('-')[1:3])
377
process_date['M'] = lambda x : x.split('-')[1]
378
process_date['Y'] = lambda x : x.split('-')[0]
381
order_date['D'] = lambda x : time.mktime((2005,int(x.split('-')[0]), int(x.split('-')[1]),0,0,0,0,0,0))
382
order_date['M'] = lambda x : x
383
order_date['Y'] = lambda x : x
392
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])
394
type = service.execute(cr.dbname, uid, 'ir.model.fields', 'read', [field_id],['ttype'])
395
if type[0]['ttype'] == 'date':
397
fct[idx] = process_date[report['frequency']]
399
fct[idx] = lambda x : x
401
fct[idx] = lambda x : x
404
# plots are usually displayed year by year
405
# so we do so if the first field is a date
409
key = process_date['Y'](r[date_idx])
410
if not data_by_year.has_key(key):
411
data_by_year[key] = []
412
for i in range(len(r)):
414
data_by_year[key].append(r)
416
data_by_year[''] = results
419
nb_bar = len(data_by_year)*(len(fields)-1)
420
colors = map(lambda x:line_style.T(color=x), misc.choice_colors(nb_bar))
422
for line in data_by_year.keys():
424
# sum data and save it in a list. An item for a fields
425
for d in data_by_year[line]:
426
for idx in range(len(fields)-1):
427
fields_bar.append({})
428
if fields_bar[idx].has_key(d[0]):
429
fields_bar[idx][d[0]] += d[idx+1]
431
fields_bar[idx][d[0]] = d[idx+1]
432
for idx in range(len(fields)-1):
434
for k in fields_bar[idx].keys():
436
data[k] += fields_bar[idx][k]
438
data[k] = fields_bar[idx][k]
443
# cumulate if necessary
445
data_cum.append([k, float(data[k])+float(prev)])
446
if fields[idx+1]['cumulate']:
449
plot = line_plot.T(label=fields[idx+1]['name']+' '+str(line), data = data_cum, line_style=colors[idx0*(len(fields)-1)+idx])
451
abscissa.update(fields_bar[idx])
454
abscissa = map(lambda x : [x, None], abscissa)
455
ar.x_coord = category_coord.T(abscissa,0)
459
self.obj = external_pdf(pdf_string.getvalue())
466
def _create_bars(self, cr, uid, ids, report, fields, results, context):
467
service = netsvc.LocalService("object_proxy")
468
pdf_string = StringIO.StringIO()
469
can = canvas.init(fname=pdf_string, format='pdf')
471
can.show(80,380,'/16/H'+report['title'])
474
process_date['D'] = lambda x : reduce(lambda xx,yy : xx+'-'+yy,x.split('-')[1:3])
475
process_date['M'] = lambda x : x.split('-')[1]
476
process_date['Y'] = lambda x : x.split('-')[0]
479
order_date['D'] = lambda x : time.mktime((2005,int(x.split('-')[0]), int(x.split('-')[1]),0,0,0,0,0,0))
480
order_date['M'] = lambda x : x
481
order_date['Y'] = lambda x : x
483
ar = area.T(size=(350,350),
484
x_axis = axis.X(label = fields[0]['name'], format="/a-30{}%s"),
485
y_axis = axis.Y(label = ', '.join(map(lambda x : x['name'], fields[1:]))))
491
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])
493
type = service.execute(cr.dbname, uid, 'ir.model.fields', 'read', [field_id],['ttype'])
494
if type[0]['ttype'] == 'date':
496
fct[idx] = process_date[report['frequency']]
498
fct[idx] = lambda x : x
500
fct[idx] = lambda x : x
503
# plot are usually displayed year by year
504
# so we do so if the first field is a date
508
key = process_date['Y'](r[date_idx])
509
if not data_by_year.has_key(key):
510
data_by_year[key] = []
511
for i in range(len(r)):
513
data_by_year[key].append(r)
515
data_by_year[''] = results
518
nb_bar = len(data_by_year)*(len(fields)-1)
519
colors = map(lambda x:fill_style.Plain(bgcolor=x), misc.choice_colors(nb_bar))
522
for line in data_by_year.keys():
524
# sum data and save it in a list. An item for a fields
525
for d in data_by_year[line]:
526
for idx in range(len(fields)-1):
527
fields_bar.append({})
528
if fields_bar[idx].has_key(d[0]):
529
fields_bar[idx][d[0]] += d[idx+1]
531
fields_bar[idx][d[0]] = d[idx+1]
532
for idx in range(len(fields)-1):
534
for k in fields_bar[idx].keys():
536
data[k] += fields_bar[idx][k]
538
data[k] = fields_bar[idx][k]
543
# cumulate if necessary
545
data_cum.append([k, float(data[k])+float(prev)])
546
if fields[idx+1]['cumulate']:
550
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])
552
abscissa.update(fields_bar[idx])
554
abscissa = map(lambda x : [x, None], abscissa)
555
ar.x_coord = category_coord.T(abscissa,0)
559
self.obj = external_pdf(pdf_string.getvalue())
564
def _create_pie(self, cr, uid, ids, report, fields, results, context):
565
pdf_string = StringIO.StringIO()
566
can = canvas.init(fname=pdf_string, format='pdf')
567
ar = area.T(size=(350,350), legend=legend.T(),
568
x_grid_style = None, y_grid_style = None)
569
colors = map(lambda x:fill_style.Plain(bgcolor=x), misc.choice_colors(len(results)))
571
if reduce(lambda x,y : x+y, map(lambda x : x[1],results)) == 0.0:
572
raise('The sum of the data (2nd field) is null. \nWe can draw a pie chart !')
574
plot = pie_plot.T(data=results, arc_offsets=[0,10,0,10],
575
shadow = (2, -2, fill_style.gray50),
577
arrow_style = arrow.a3,
582
self.obj = external_pdf(pdf_string.getvalue())
587
def _create_table(self, uid, ids, report, fields, tree, results, context):
588
pageSize=common.pageSize.get(report['print_format'], [210.0,297.0])
589
if report['print_orientation']=='landscape':
590
pageSize=[pageSize[1],pageSize[0]]
592
impl = minidom.getDOMImplementation()
593
new_doc = impl.createDocument(None, "report", None)
596
config = new_doc.createElement("config")
598
def _append_node(name, text):
599
n = new_doc.createElement(name)
600
t = new_doc.createTextNode(text)
602
config.appendChild(n)
604
_append_node('date', time.strftime('%d/%m/%Y'))
605
_append_node('PageSize', '%.2fmm,%.2fmm' % tuple(pageSize))
606
_append_node('PageFormat', '%s' % report['print_format'])
607
_append_node('PageWidth', '%.2f' % (pageSize[0] * 2.8346,))
608
_append_node('PageHeight', '%.2f' %(pageSize[1] * 2.8346,))
610
length = pageSize[0]-30-reduce(lambda x,y:x+(y['width'] or 0), fields, 0)
613
if not f['width']: count+=1
616
f['width']=round((float(length)/count)-0.5)
618
_append_node('tableSize', '%s' % ','.join(map(lambda x: '%.2fmm' % (x['width'],), fields)))
619
_append_node('report-header', '%s' % (report['title'],))
620
_append_node('report-footer', '%s' % (report['footer'],))
622
new_doc.childNodes[0].appendChild(config)
623
header = new_doc.createElement("header")
626
field = new_doc.createElement("field")
627
field_txt = new_doc.createTextNode('%s' % (f['name'],))
628
field.appendChild(field_txt)
629
header.appendChild(field)
631
new_doc.childNodes[0].appendChild(header)
633
lines = new_doc.createElement("lines")
635
node_line = new_doc.createElement("row")
636
for f in range(len(fields)):
637
col = new_doc.createElement("col")
638
col.setAttribute('tree','no')
640
txt = new_doc.createTextNode(str(line[f] or ''))
642
txt = new_doc.createTextNode('/')
644
node_line.appendChild(col)
645
lines.appendChild(node_line)
647
new_doc.childNodes[0].appendChild(lines)
649
# file('/tmp/terp.xml','w+').write(new_doc.toxml())
651
styledoc = libxml2.parseFile(os.path.join(tools.config['root_path'],'addons/base/report/custom_new.xsl'))
652
style = libxslt.parseStylesheetDoc(styledoc)
653
doc = libxml2.parseDoc(new_doc.toxml())
654
rml_obj = style.applyStylesheet(doc, None)
655
rml = style.saveResultToString(rml_obj)
657
self.obj = render.rml(rml)
660
report_custom('report.custom')