~jgrandguillaume-c2c/openobject-addons/multi-company-cost-price

« back to all changes in this revision

Viewing changes to account_balance/report/rml_parse.py

  • Committer: Joël Grand-Guillaume
  • Date: 2010-04-08 09:00:10 UTC
  • mfrom: (2533.3.664)
  • Revision ID: joel.grandguillaume@camptocamp.com-20100408090010-c0pqjan341s18bxs
[MRG] Merge from last trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
##############################################################################
3
 
#    
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6
 
#
7
 
#    This program is free software: you can redistribute it and/or modify
8
 
#    it under the terms of the GNU Affero General Public License as
9
 
#    published by the Free Software Foundation, either version 3 of the
10
 
#    License, or (at your option) any later version.
11
 
#
12
 
#    This program is distributed in the hope that it will be useful,
13
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
#    GNU Affero General Public License for more details.
16
 
#
17
 
#    You should have received a copy of the GNU Affero General Public License
18
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19
 
#
20
 
##############################################################################
21
 
 
22
 
from report import report_sxw
23
 
import xml.dom.minidom
24
 
import os, time
25
 
import osv
26
 
import re
27
 
import tools
28
 
import pooler
29
 
import re
30
 
import sys
31
 
 
32
 
 
33
 
class rml_parse(report_sxw.rml_parse):
34
 
    def __init__(self, cr, uid, name, context):
35
 
        super(rml_parse, self).__init__(cr, uid, name, context=context)
36
 
        self.localcontext.update({
37
 
            'comma_me': self.comma_me,
38
 
            'format_date': self._get_and_change_date_format_for_swiss,
39
 
            'strip_name' : self._strip_name,
40
 
            'explode_name' : self._explode_name,
41
 
        })
42
 
 
43
 
    def comma_me(self,amount):
44
 
        #print "#" + str(amount) + "#"
45
 
        if not amount:
46
 
            amount = 0.0
47
 
        if  type(amount) is float :
48
 
            amount = str('%.2f'%amount)
49
 
        else :
50
 
            amount = str(amount)
51
 
        if (amount == '0'):
52
 
             return ' '
53
 
        orig = amount
54
 
        new = re.sub("^(-?\d+)(\d{3})", "\g<1>'\g<2>", amount)
55
 
        if orig == new:
56
 
            return new
57
 
        else:
58
 
            return self.comma_me(new)
59
 
        
60
 
    def _ellipsis(self, string, maxlen=100, ellipsis = '...'):
61
 
        ellipsis = ellipsis or ''
62
 
        try:
63
 
            return string[:maxlen - len(ellipsis) ] + (ellipsis, '')[len(string) < maxlen]
64
 
        except Exception, e:
65
 
            return False
66
 
        
67
 
    def _strip_name(self, name, maxlen=50):
68
 
        return self._ellipsis(name, maxlen, '...')
69
 
 
70
 
    def _get_and_change_date_format_for_swiss (self,date_to_format):
71
 
        date_formatted=''
72
 
        if date_to_format:
73
 
            date_formatted = strptime (date_to_format,'%Y-%m-%d').strftime('%d.%m.%Y')
74
 
        return date_formatted
75
 
 
76
 
    def _explode_name(self,chaine,length):
77
 
        # We will test if the size is less then account
78
 
        full_string = ''
79
 
        if (len(str(chaine)) <= length):
80
 
            return chaine
81
 
        #
82
 
        else:
83
 
            chaine = unicode(chaine,'utf8').encode('iso-8859-1')
84
 
            rup = 0
85
 
            for carac in chaine:
86
 
                rup = rup + 1
87
 
                if rup == length:
88
 
                    full_string = full_string + '\n'
89
 
                    full_string = full_string + carac
90
 
                    rup = 0
91
 
                else:
92
 
                    full_string = full_string + carac
93
 
 
94
 
        return full_string
95
 
 
96
 
    def makeAscii(self,str):
97
 
        try:
98
 
            Stringer = str.encode("utf-8")
99
 
        except UnicodeDecodeError:
100
 
            try:
101
 
                Stringer = str.encode("utf-16")
102
 
            except UnicodeDecodeError:
103
 
                print "UTF_16 Error"
104
 
                Stringer = str
105
 
            else:
106
 
                return Stringer
107
 
        else:
108
 
            return Stringer
109
 
        return Stringer
110
 
    
111
 
    def explode_this(self,chaine,length):
112
 
        #chaine = self.repair_string(chaine)
113
 
        chaine = rstrip(chaine)
114
 
        ast = list(chaine)
115
 
        i = length
116
 
        while i <= len(ast):
117
 
            ast.insert(i,'\n')
118
 
            i = i + length
119
 
        chaine = str("".join(ast))
120
 
        return chaine
121
 
    
122
 
    def repair_string(self,chaine):
123
 
        ast = list(chaine)
124
 
        UnicodeAst = []
125
 
        _previouslyfound = False
126
 
        i = 0
127
 
        #print str(ast)
128
 
        while i < len(ast):
129
 
            elem = ast[i]
130
 
            try:
131
 
                Stringer = elem.encode("utf-8")
132
 
            except UnicodeDecodeError:
133
 
                to_reencode = elem + ast[i+1]
134
 
                print str(to_reencode)
135
 
                Good_char = to_reencode.decode('utf-8')
136
 
                UnicodeAst.append(Good_char)
137
 
                i += i +2
138
 
            else:
139
 
                UnicodeAst.append(elem)
140
 
                i += i + 1
141
 
 
142
 
 
143
 
        return "".join(UnicodeAst)
144
 
 
145
 
    def ReencodeAscii(self,str):
146
 
        print sys.stdin.encoding
147
 
        try:
148
 
            Stringer = str.decode("ascii")
149
 
        except UnicodeEncodeError:
150
 
            print "REENCODING ERROR"
151
 
            return str.encode("ascii")
152
 
        except UnicodeDecodeError:
153
 
            print "DECODING ERROR"
154
 
            return str.encode("ascii")
155
 
 
156
 
        else:
157
 
            print Stringer
158
 
            return Stringer
159
 
        
160
 
    def _add_header(self, node, header=1):
161
 
        if header==2:
162
 
            rml_head =  self.rml_header2
163
 
        else:
164
 
            rml_head =  self.rml_header
165
 
        rml_head =  rml_head.replace('<pageGraphics>','''<pageGraphics> <image x="10" y="26cm" height="770.0" width="1120.0" >[[company.logo]] </image> ''')
166
 
        return True
167
 
 
168
 
 
169
 
 
170
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: