[MERGE] merge from trunk addons
[odoo/odoo.git] / addons / hr_payroll / report / rml_parse.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22 from time import strptime
23
24 from report import report_sxw
25 import re
26 from lxml import etree
27
28
29 class rml_parse(report_sxw.rml_parse):
30     def __init__(self, cr, uid, name, context):
31         super(rml_parse, self).__init__(cr, uid, name, context=None)
32         self.localcontext.update({
33             'comma_me': self.comma_me,
34             'format_date': self._get_and_change_date_format_for_swiss,
35             'strip_name' : self._strip_name,
36             'explode_name' : self._explode_name,
37         })
38
39     def comma_me(self,amount):
40         if not amount:
41             amount = 0.0
42         if  type(amount) is float :
43             amount = str('%.2f'%amount)
44         else :
45             amount = str(amount)
46         if (amount == '0'):
47              return ' '
48         orig = amount
49         new = re.sub("^(-?\d+)(\d{3})", "\g<1>'\g<2>", amount)
50         if orig == new:
51             return new
52         else:
53             return self.comma_me(new)
54
55     def _ellipsis(self, string, maxlen=100, ellipsis = '...'):
56         ellipsis = ellipsis or ''
57         try:
58             return string[:maxlen - len(ellipsis) ] + (ellipsis, '')[len(string) < maxlen]
59         except Exception:
60             return False
61
62     def _strip_name(self, name, maxlen=50):
63         return self._ellipsis(name, maxlen, '...')
64
65     def _get_and_change_date_format_for_swiss (self,date_to_format):
66         date_formatted=''
67         if date_to_format:
68             date_formatted = strptime(date_to_format,'%Y-%m-%d').strftime('%d.%m.%Y')
69         return date_formatted
70
71     def _explode_name(self,chaine,length):
72         # We will test if the size is less then account
73         full_string = ''
74         if (len(str(chaine)) <= length):
75             return chaine
76         #
77         else:
78             chaine = unicode(chaine,'utf8').encode('iso-8859-1')
79             rup = 0
80             for carac in chaine:
81                 rup = rup + 1
82                 if rup == length:
83                     full_string = full_string + '\n'
84                     full_string = full_string + carac
85                     rup = 0
86                 else:
87                     full_string = full_string + carac
88
89         return full_string
90
91     def makeAscii(self,str):
92         try:
93             Stringer = str.encode("utf-8")
94         except UnicodeDecodeError:
95             try:
96                 Stringer = str.encode("utf-16")
97             except UnicodeDecodeError:
98                 print "UTF_16 Error"
99                 Stringer = str
100             else:
101                 return Stringer
102         else:
103             return Stringer
104         return Stringer
105
106     def explode_this(self,chaine,length):
107         chaine = chaine.rstrip()
108         ast = list(chaine)
109         i = length
110         while i <= len(ast):
111             ast.insert(i,'\n')
112             i = i + length
113         chaine = str("".join(ast))
114         return chaine
115
116     def repair_string(self,chaine):
117         ast = list(chaine)
118         UnicodeAst = []
119         i = 0
120         while i < len(ast):
121             elem = ast[i]
122             try:
123                 Stringer = elem.encode("utf-8")
124             except UnicodeDecodeError:
125                 to_reencode = elem + ast[i+1]
126                 print str(to_reencode)
127                 Good_char = to_reencode.decode('utf-8')
128                 UnicodeAst.append(Good_char)
129                 i += i +2
130             else:
131                 UnicodeAst.append(elem)
132                 i += i + 1
133         return "".join(UnicodeAst)
134
135     def _add_header(self, node, header='external'):
136         if header=='internal':
137             rml_head =  self.rml_header2
138         elif header=='internal landscape':
139             rml_head =  self.rml_header3
140         else:
141             rml_head =  self.rml_header
142         rml_head =  rml_head.replace('<pageGraphics>','''<pageGraphics> <image x="10" y="26cm" height="770.0" width="1120.0" >[[company.logo]] </image> ''')
143         rml_dom = node
144         head_dom = etree.XML(rml_head)
145         for tag in head_dom:
146             found = rml_dom.find('.//'+tag.tag)
147             if found is not None and len(found):
148                 if tag.get('position'):
149                     found.append(tag)
150                 else :
151                     found.getparent().replace(found,tag)
152         return True
153
154 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: