bzr revid: hda@tinyerp.com-4429532dbe7569da981caa4538b5488b7f7d10ac
[odoo/odoo.git] / bin / report / printscreen / ps_list.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #                    Fabien Pinckaers <fp@tiny.Be>
5 #
6 # WARNING: This program as such is intended to be used by professional
7 # programmers who take the whole responsability of assessing all potential
8 # consequences resulting from its eventual inadequacies and bugs
9 # End users who are looking for a ready-to-use solution with commercial
10 # garantees and support are strongly adviced to contract a Free Software
11 # Service Company
12 #
13 # This program is Free Software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 #
27 ##############################################################################
28
29 from report.interface import report_int
30 import pooler
31 import tools
32
33 from report import render
34
35 from xml.dom import minidom
36 import libxml2
37 import libxslt
38
39 import time, os
40
41 class report_printscreen_list(report_int):
42         def __init__(self, name):
43                 report_int.__init__(self, name)
44
45         def _parse_node(self, root_node):
46                 result = []
47                 for node in root_node.childNodes:
48                         if node.localName == 'field':
49                                 attrsa = node.attributes
50                                 attrs = {}
51                                 if not attrsa is None:
52                                         for i in range(attrsa.length):
53                                                 attrs[attrsa.item(i).localName] = attrsa.item(i).nodeValue
54                                 result.append(attrs['name'])
55                         else:
56                                 result.extend(self._parse_node(node))
57                 return result
58
59         def _parse_string(self, view):
60                 dom = minidom.parseString(view.encode('utf-8'))
61                 return self._parse_node(dom)
62
63         def create(self, cr, uid, ids, datas, context={}):
64                 pool = pooler.get_pool(cr.dbname)
65                 model = pool.get(datas['model'])
66                 model_id = pool.get('ir.model').search(cr, uid, [('model','=',model._name)])
67                 if model_id:
68                         model_desc = pool.get('ir.model').browse(cr, uid, model_id[0], context).name
69                 else:
70                         model_desc = model._description
71
72                 datas['ids'] = ids
73                 model = pooler.get_pool(cr.dbname).get(datas['model'])
74
75                 result = model.fields_view_get(cr, uid, view_type='tree', context=context)
76                 fields_order = self._parse_string(result['arch'])
77                 rows = model.read(cr, uid, datas['ids'], result['fields'].keys(), context )
78                 res = self._create_table(uid, datas['ids'], result['fields'], fields_order, rows, context, model_desc)
79                 return (self.obj.get(), 'pdf')
80
81
82         def _create_table(self, uid, ids, fields, fields_order, results, context, title=''):
83                 pageSize=[297.0,210.0]
84
85                 impl = minidom.getDOMImplementation()
86                 new_doc = impl.createDocument(None, "report", None)
87
88                 # build header
89                 config = new_doc.createElement("config")
90
91                 def _append_node(name, text):
92                         n = new_doc.createElement(name)
93                         t = new_doc.createTextNode(text)
94                         n.appendChild(t)
95                         config.appendChild(n)
96
97                 _append_node('date', time.strftime('%d/%m/%Y'))
98                 _append_node('PageSize', '%.2fmm,%.2fmm' % tuple(pageSize))
99                 _append_node('PageWidth', '%.2f' % (pageSize[0] * 2.8346,))
100                 _append_node('PageHeight', '%.2f' %(pageSize[1] * 2.8346,))
101
102                 _append_node('report-header', title)
103                 l = []
104                 t = 0
105                 rowcount=0;
106                 strmax = (pageSize[0]-40) * 2.8346
107                 temp = []
108                 count = len(fields_order)
109                 for i in range(0,count):
110                         temp.append(0)
111
112                 ince = -1;
113                 for f in fields_order:
114                         s = 0
115                         ince += 1
116                         if fields[f]['type'] in ('date','time','float','integer'):
117                                 s = 60
118                                 strmax -= s
119                                 if fields[f]['type'] in ('float','integer'):
120                                         temp[ince]=1;
121                         else:
122                                 t += fields[f].get('size', 80) / 28 + 1
123
124                         l.append(s)
125
126                 for pos in range(len(l)):
127                         if not l[pos]:
128                                 s = fields[fields_order[pos]].get('size', 80) / 28 + 1
129                                 l[pos] = strmax * s / t
130
131                 _append_node('tableSize', ','.join(map(str,l)) )
132                 new_doc.childNodes[0].appendChild(config)
133                 header = new_doc.createElement("header")
134
135                 for f in fields_order:
136                         field = new_doc.createElement("field")
137                         field_txt = new_doc.createTextNode('%s' % (fields[f]['string'],))
138                         field.appendChild(field_txt)
139                         header.appendChild(field)
140
141                 new_doc.childNodes[0].appendChild(header)
142
143                 lines = new_doc.createElement("lines")
144
145                 tsum = []
146                 count = len(fields_order)
147                 for i in range(0,count):
148                         tsum.append(0)
149
150                 for line in results:
151                         node_line = new_doc.createElement("row")
152
153                         count = -1
154                         for f in fields_order:
155                                 count += 1
156                                 if fields[f]['type']=='many2one' and line[f]:
157                                         line[f] = line[f][1]
158                                 if fields[f]['type'] in ('one2many','many2many') and line[f]:
159                                         line[f] = '( '+str(len(line[f])) + ' )'
160                                 col = new_doc.createElement("col")
161                                 col.setAttribute('para','yes')
162                                 col.setAttribute('tree','no')
163                                 if line[f] != None:
164                                         txt = new_doc.createTextNode(str(line[f] or ''))
165                                         if temp[count] == 1:
166                                                 tsum[count] = tsum[count] + line[f];
167
168                                 else:
169                                         txt = new_doc.createTextNode('/')
170                                 col.appendChild(txt)
171                                 node_line.appendChild(col)
172                         lines.appendChild(node_line)
173                 node_line = new_doc.createElement("row")
174                 lines.appendChild(node_line)
175                 node_line = new_doc.createElement("row")
176                 for f in range(0,count):
177                         col = new_doc.createElement("col")
178                         col.setAttribute('para','yes')
179                         col.setAttribute('tree','no')
180                         if tsum[f] != None:
181                                 txt = new_doc.createTextNode(str(tsum[f] or ''))
182                         else:
183                                 txt = new_doc.createTextNode('/')
184                         if f == 0:
185                                 txt = new_doc.createTextNode('Total')
186
187                         col.appendChild(txt)
188                         node_line.appendChild(col)
189                 lines.appendChild(node_line)
190
191                 new_doc.childNodes[0].appendChild(lines)
192
193                 styledoc = libxml2.parseFile(os.path.join(tools.config['root_path'],'addons/base/report/custom_new.xsl'))
194                 style = libxslt.parseStylesheetDoc(styledoc)
195                 doc = libxml2.parseDoc(new_doc.toxml())
196                 rml_obj = style.applyStylesheet(doc, None)
197                 rml = style.saveResultToString(rml_obj)
198                 self.obj = render.rml(rml)
199                 self.obj.render()
200                 return True
201 report_printscreen_list('report.printscreen.list')
202