~openbias/bias-trunk/bias-public-trunk

« back to all changes in this revision

Viewing changes to bias_electronic_invoice_v6/report/invoice.py

  • Committer: Jose Patricio
  • Date: 2011-10-19 03:16:40 UTC
  • Revision ID: josepato@bias.com.mx-20111019031640-05zd7r5lxwx084qu
el push inicial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
5
# WARNING: This program as such is intended to be used by professional
 
6
# programmers who take the whole responsability of assessing all potential
 
7
# consequences resulting from its eventual inadequacies and bugs
 
8
# End users who are looking for a ready-to-use solution with commercial
 
9
# garantees and support are strongly adviced to contract a Free Software
 
10
# Service Company
 
11
#
 
12
# This program is Free Software; you can redistribute it and/or
 
13
# modify it under the terms of the GNU General Public License
 
14
# as published by the Free Software Foundation; either version 2
 
15
# of the License, or (at your option) any later version.
 
16
#
 
17
# This program is distributed in the hope that it will be useful,
 
18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
# GNU General Public License for more details.
 
21
#
 
22
# You should have received a copy of the GNU General Public License
 
23
# along with this program; if not, write to the Free Software
 
24
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
25
#
 
26
##############################################################################
 
27
 
 
28
import time
 
29
from report  import report_sxw
 
30
import text
 
31
import pooler
 
32
from lxml import etree
 
33
import re
 
34
 
 
35
 
 
36
class account_invoice(report_sxw.rml_parse):
 
37
        def __init__(self, cr, uid, name, context):
 
38
                super(account_invoice, self).__init__(cr, uid, name, context)
 
39
                user = self.pool.get('res.users').browse(cr, uid, uid)
 
40
                self.localcontext.update({
 
41
                        'time': time,
 
42
                        'date_sp': text.date_sp,
 
43
                        'moneyfmt': text.moneyfmt,
 
44
                        'texto': self._get_text,
 
45
                        'get_money_name': text.get_money_name,
 
46
                        #'fix_space': text.fix_space,
 
47
                        'user': user,
 
48
                        'get_impuestos':self._get_impuestos,
 
49
                        'tipoDeComprobante': self._get_tipoDeComprobante,
 
50
                        'leyendaTipoDeCambio': self._get_leyendaTipoDeCambio,
 
51
                        'leyenda':self._get_leyenda_UDS,
 
52
                        'leyenda_pitex':self._get_leyenda_pitex,
 
53
                        'cadena':self._get_cadena,
 
54
                        'get_nopartidas': self._get_nopartidas,
 
55
                        'get_lineas_partidas': self._get_lineas_partidas,
 
56
                        'get_total_products': self._get_total_products,
 
57
                        'convert_value': self._convert_value,
 
58
                        #'give_me_space':self._give_me_space,
 
59
                })
 
60
 
 
61
        def _get_lines(self, txt):
 
62
                res = txt[:len(txt)/2] + ' ' + txt[len(txt)/2:]
 
63
                return res
 
64
 
 
65
        def _get_text(self, amount):
 
66
                return text.text(int(amount))
 
67
 
 
68
        def _get_cadena(self, cadena):
 
69
                rows = len(cadena)/120
 
70
                res = ''
 
71
                for rn in range(rows):
 
72
                        res += cadena[200*rn:(200*(rn+1))] + '\n'
 
73
                return res
 
74
 
 
75
        def _get_leyenda_UDS(self, obj):
 
76
                if obj.currency_id.code == 'USD':
 
77
                        return 'LA PRESENTE FACTURA SE SOLVENTARA ENTREGADO EL EQUIVALENTE EN MONEDA NACIONAL, AL TIPO DE CAMBIO QUE EL BANCO DE MEXICO PUBLIQUE EN EL DIARIO OFICIAL DE LA FEDERACION, EL DIA HABIL BANCARIO INMEDIATO ANTERIOR A AQUEL EN QUE SE EFECTUE EL PAGO'
 
78
                else:
 
79
                        return ''
 
80
 
 
81
        def _get_leyenda_pitex(self, obj):
 
82
                if obj.partner_id.property_account_position.name == 'RETENEDOR DEL IVA':
 
83
                        return 'Impuesto retenido de conformidad con la Ley del Impuesto al Valor Agregado'
 
84
                else:
 
85
                        return ''
 
86
 
 
87
 
 
88
        def _get_tipoDeComprobante(self, inv_brw):
 
89
                if inv_brw.type == 'out_invoice':
 
90
                        return 'Ingreso'
 
91
                elif  inv_brw.type == 'out_refund':
 
92
                        return 'Egreso'
 
93
                elif inv_brw.company_id.partner_id.vat == inv_brw.partner_id.vat:
 
94
                        return 'Traslado'
 
95
                return ''
 
96
 
 
97
 
 
98
 
 
99
        def _get_leyendaTipoDeCambio(self, inv_brw):
 
100
                if inv_brw.currency_id.code == 'USD':
 
101
                        return 'La presente factura se solventara entregado el equivalente en moneda nacional, al tipo de cambio que el Banco de Mexico publique en el Diario Oficial de la Federacion, el dia habil bancario inmediato anterior a aquel en que se efectue el pago'
 
102
                else:
 
103
                        return 'abc'
 
104
 
 
105
 
 
106
        
 
107
        def _get_impuestos(self, invoice_obj):
 
108
                tax_obj = pooler.get_pool(self.cr.dbname).get('account.invoice')
 
109
                tax_xml = tax_obj.get_impuestos(self.cr, self.uid, invoice_obj.tax_line)
 
110
                res = []
 
111
                for tax_type in tax_xml:
 
112
                        for tax in tax_type:
 
113
                                res_dir = {}
 
114
                                tax.values()
 
115
                                if tax.get('impuesto'):
 
116
                                        res_dir['impuesto'] =  tax_type[0].tag + ' ' + tax.get('impuesto')
 
117
                                else:
 
118
                                        res_dir['impuesto'] = ''
 
119
                                if tax.get('importe'):
 
120
                                        res_dir['importe'] =  '$ ' +   text.moneyfmt('%.2f' % float(tax.get('importe')))
 
121
                                else:
 
122
                                        res_dir['importe'] =  ''
 
123
                                if tax.get('tasa'):
 
124
                                        res_dir['tasa'] =  tax.get('tasa') + '%'
 
125
                                else:
 
126
                                        res_dir['tasa'] = ''
 
127
                                res.append(res_dir)
 
128
                if not res:
 
129
                        res_dir ={'impuesto':'', 'importe':'', 'tasa':''}
 
130
                        res.append(res_dir)
 
131
                return res
 
132
 
 
133
##      def _get_impuestos(self, invoice_obj):
 
134
##              tax_obj = pooler.get_pool(self.cr.dbname).get('account.invoice')
 
135
##              tax_xml = tax_obj.get_impuestos(self.cr, self.uid, invoice_obj.tax_line)
 
136
##              res = []
 
137
##              for tax_type in tax_xml:
 
138
##                      for tax in tax_type:
 
139
##                              res_dir = {}
 
140
##                              if tax.get('impuesto'):
 
141
##                                      res_dir['impuesto'] =  tax_type[0].tag + ' ' + tax.get('impuesto')
 
142
##                              else:
 
143
##                                      res_dir['impuesto'] = ''
 
144
##                              if tax.get('importe'):
 
145
##                                      res_dir['importe'] =  tax.get('importe')
 
146
##                              else:
 
147
##                                      res_dir['importe'] =  ''
 
148
##                              if tax.get('tasa'):
 
149
##                                      res_dir['tasa'] =  tax.get('tasa') + '%'
 
150
##                              else:
 
151
##                                      res_dir['tasa'] = ''
 
152
##                              res.append(res_dir)
 
153
##              return res
 
154
        
 
155
        def _get_nopartidas(self, invoice_obj):
 
156
                query = "SELECT count(id) FROM account_invoice_line WHERE invoice_id=%s"%(invoice_obj.id)
 
157
                self.cr.execute(query)
 
158
                res = self.cr.fetchone()
 
159
                if res:
 
160
                        res=res[0]
 
161
                return res
 
162
 
 
163
 
 
164
        def _get_lineas_partidas(self, invoice_obj):
 
165
                query2 = "SELECT a.quantity, a.name, a.price_unit, a.discount, a.price_subtotal, p.default_code, a.note FROM account_invoice_line a, product_product p WHERE invoice_id=%s and a.product_id = p.id order by name"%(invoice_obj.id)
 
166
                self.cr.execute(query2)
 
167
                res = self.cr.dictfetchall()
 
168
                count =0
 
169
                total_discount = 0
 
170
                for rr in res:
 
171
                        count +=1 
 
172
                        rr.update({'renglon':count})
 
173
                        if rr['discount']:
 
174
                                rr.update({'discount':str(rr['discount']) +'%'})
 
175
 
 
176
                return res
 
177
 
 
178
        def _get_total_products(self, invoice_obj):
 
179
                totales = "SELECT SUM(quantity) FROM account_invoice_line WHERE invoice_id=%s"%(invoice_obj.id)
 
180
                self.cr.execute(totales)
 
181
                res = self.cr.fetchone()
 
182
                if res:
 
183
                        res=res[0]
 
184
                return text.moneyfmt( '%.2f' % float(res))
 
185
 
 
186
        def _convert_value(self, value, context={}):
 
187
                if not value:
 
188
                        return ''
 
189
                if context.get('strip', False) and context.get('model', False):
 
190
                        quitar=len(context['model'])+3
 
191
                        value = value[quitar:]
 
192
                        return value
 
193
                if context.get('currency', False):      
 
194
                        if value == 'USD':      
 
195
                                return 'EL VALOR DE ESTA FACTURA ESTA EN USD'
 
196
                        else:
 
197
                                return ''
 
198
                if context.get('money_format', False):
 
199
                        return '$ ' + text.moneyfmt( '%.2f' % float(value))
 
200
                if context.get('re', False):
 
201
                        return re.sub(',',"",str(value))
 
202
                if context.get('vat_fmt', False):
 
203
                        return re.sub('[-. |*]',"",str(value)).upper()
 
204
                return value
 
205
 
 
206
 
 
207
 
 
208
report_sxw.report_sxw('report.account.invoice.electronic', 'account.invoice', 'addons/bias_electronic_invoice_v6/report/invoice_b.rml', parser=account_invoice, header=False)
 
209