~vauxoo/openerp-mexico-localization/oml_vatvoucher_dev_moy

« back to all changes in this revision

Viewing changes to l10n_mx_facturae_cbb/report/invoice_facturae_pdf.py

  • Committer: moylop260
  • Date: 2012-04-06 23:21:07 UTC
  • mto: This revision was merged to the branch mainline in revision 156.
  • Revision ID: moylop260@vauxoo.com-20120406232107-0gjbngureh4ugyev
[FIX] [l10n_mx_facturae_cbb] Add complete client address & correct tax

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
from report import report_sxw
29
29
import pooler
30
30
import tools
31
 
from amount_to_text_es import amount_to_text as amount_to_text_class
 
31
#from amount_to_text_es import amount_to_text as amount_to_text_class
32
32
 
33
 
amount_to_text_obj = amount_to_text_class()
 
33
#amount_to_text_obj = amount_to_text_class()
34
34
#amount_to_text = amount_to_text_obj.amount_to_text
35
 
amount_to_text = amount_to_text_obj.amount_to_text_cheque
 
35
#amount_to_text = amount_to_text_obj.amount_to_text_cheque
36
36
 
37
37
###sql_delete_report = "DELETE FROM ir_act_report_xml WHERE report_name = 'account.invoice.facturae.pdf'"--Si no toma la actualizacion del reporte xml, borrarlo directamente desde la base de datos, con este script.
38
38
 
42
42
        self.localcontext.update({
43
43
            'set_global_data': self._set_global_data,
44
44
            'facturae_data_dict': self._facturae_data_dict,
45
 
            'amount_to_text': self._get_amount_to_text,
 
45
            #'amount_to_text': self._get_amount_to_text,
46
46
            'split_string': self._split_string,
47
47
            'company_address': self._company_address,
48
48
            'subcompany_address': self._subcompany_address,
51
51
            'get_taxes': self._get_taxes,
52
52
            'get_taxes_ret': self._get_taxes_ret,
53
53
            'float': float,
 
54
            'exists_key': self._exists_key,
54
55
        })
55
56
        self.taxes = []
56
57
    
 
58
    def _exists_key(self, key):
 
59
        return self.invoice._columns.has_key(key)
 
60
        """
 
61
        try:
 
62
            str= 'self.invoice.'+key
 
63
            if eval(str):
 
64
                return True
 
65
        except:
 
66
            return False
 
67
        """
 
68
    
57
69
    def _set_global_data(self, o):
58
70
        try:
59
71
            self.setLang(o.partner_id.lang)
66
78
            print "exception: %s"%( e )
67
79
            pass
68
80
        try:
69
 
            self._get_facturae_data_dict(o.id)
 
81
            self._get_facturae_data_dict(o)
70
82
        except Exception, e:
71
83
            print "exception: %s"%( e )
72
84
            pass
79
91
        return self.sequence
80
92
    
81
93
    def _set_invoice_sequence_and_approval(self, invoice_id):
82
 
        #TinyERP Compatibility
83
94
        context = {}
84
95
        pool = pooler.get_pool(self.cr.dbname)
85
96
        invoice_obj = pool.get('account.invoice')
86
97
        sequence_obj = pool.get('ir.sequence')
87
 
        approval_obj = pool.get('ir.sequence.approval')
88
 
        #invoice = invoice_obj.browse(self.cr, self.uid, invoice_id)
89
 
        sequence_id = invoice_obj._get_invoice_sequence(self.cr, self.uid, [invoice_id])[invoice_id]
90
 
        sequence = sequence_obj.browse(self.cr, self.uid, [sequence_id])[0]
 
98
        #approval_obj = pool.get('ir.sequence.approval')
 
99
        invoice = invoice_obj.browse(self.cr, self.uid, [invoice_id], context=context)[0]
 
100
        context.update({'number_work': invoice.number})
 
101
        sequence = invoice.invoice_sequence_id or False
 
102
        sequence_id = sequence and sequence.id or False
 
103
        #invoice_obj._get_invoice_sequence(self.cr, self.uid, [invoice_id])[invoice_id]
 
104
        #sequence_obj.browse(self.cr, self.uid, [sequence_id])[0]
91
105
        self.sequence = sequence
92
106
        
93
 
        invoice = invoice_obj.browse(self.cr, self.uid, [invoice_id])[0]
94
 
        context.update({'number_work': invoice.number})
95
 
        approval_id = sequence_obj._get_current_approval(self.cr, self.uid, [sequence_id], context=context)[sequence_id]
96
 
        approval = approval_obj.browse(self.cr, self.uid, [approval_id])[0]
 
107
        #invoice = invoice_obj.browse(self.cr, self.uid, [invoice_id])[0]
 
108
        approval = sequence and sequence.approval_id or False
 
109
        approval_id = approval and approval.id or False
 
110
        #approval_id = sequence_obj._get_current_approval(self.cr, self.uid, [sequence_id], context=context)[sequence_id]
 
111
        #approval = approval_obj.browse(self.cr, self.uid, [approval_id])[0]
97
112
        self.approval = approval
98
113
        return sequence, approval
99
114
    
107
122
            pass
108
123
        return []
109
124
    
 
125
    '''
 
126
    def _set_taxes(self, invoice_id):
 
127
        """
 
128
        pool = pooler.get_pool(self.cr.dbname)
 
129
        invoice_obj = pool.get('account.invoice')
 
130
        invoice = invoice_obj.browse(self.cr, self.uid, [invoice_id])[0]
 
131
        taxes = []
 
132
        for line_tax_id in invoice.tax_line:
 
133
            tax_name = line_tax_id.name.lower().replace('.','').replace('-','').replace(' ', '')
 
134
            if tax_name in ['iva']:
 
135
                tax_name = 'IVA'
 
136
            elif 'isr' in tax_name:
 
137
                tax_name = 'ISR'
 
138
            elif 'ieps' in tax_name:
 
139
                tax_name = 'IEPS'
 
140
            tax_names.append( tax_name )
 
141
            taxes.append({
 
142
                'name': tax_name,
 
143
                'rate': "%.2f"%( round( line_tax_id.amount/(invoice.amount_total-line_tax_id.amount)*100, 0) ),
 
144
                'amount': "%.2f"%( line_tax_id.amount or 0.0),
 
145
            })
 
146
        """
 
147
        self.taxes = self.invoice_data_dict['Comprobante']['Impuestos']['Traslados']
 
148
        #self.taxes = taxes
 
149
        return taxes
 
150
    '''
 
151
    
110
152
    def _split_string(self, string, length=100):
111
153
        if string:
112
154
            for i in range(0, len(string), length):
113
155
                string = string[:i] + ' ' + string[i:]
114
156
        return string
115
 
        
 
157
    """
116
158
    def _get_amount_to_text(self, amount, lang, currency=""):
117
159
        if currency.upper() in ('MXP', 'MXN', 'PESOS', 'PESOS MEXICANOS'):
118
160
            sufijo = 'M. N.'
123
165
        amount_text = amount_to_text(amount, currency, sufijo)
124
166
        amount_text = amount_text and amount_text.upper() or ''
125
167
        return amount_text
126
 
    
 
168
    """
127
169
    def _get_company_address(self, invoice_id):
128
170
        pool = pooler.get_pool(self.cr.dbname)
129
171
        invoice_obj = pool.get('account.invoice')
131
173
        address_obj = pool.get('res.partner.address')
132
174
        invoice = invoice_obj.browse(self.cr, self.uid, invoice_id)
133
175
        partner_id = invoice.company_id.parent_id and invoice.company_id.parent_id.partner_id.id or invoice.company_id.partner_id.id
 
176
        self.invoice = invoice
 
177
        #print "partner_id",partner_id
 
178
        #invoice = partner_obj.browse(cr, uid, invoice_id)
134
179
        address_id = partner_obj.address_get(self.cr, self.uid, [partner_id], ['invoice'])['invoice']
135
180
        self.company_address_invoice = address_obj.browse(self.cr, self.uid, address_id)
136
181
        
140
185
        else:
141
186
            subaddress_id = partner_obj.address_get(self.cr, self.uid, [subpartner_id], ['invoice'])['invoice']
142
187
            self.subcompany_address_invoice = address_obj.browse(self.cr, self.uid, subaddress_id)
 
188
        #print "self.company_address_invoice",self.company_address_invoice
 
189
        #print "self.company_address_invoice[0]",self.company_address_invoice[0]
 
190
        #self.company_address_invoice  = self.company_address_invoice and self.company_address_invoice[0] or False
 
191
        #print "self.company_address_invoice",self.company_address_invoice
 
192
        #return [self.company_address_invoice]
143
193
        return ""
144
194
    
145
195
    def _company_address(self):
 
196
        #print "self.company_address_invoice",self.company_address_invoice
146
197
        return self.company_address_invoice
147
198
    
148
199
    def _subcompany_address(self):
149
200
        return self.subcompany_address_invoice
150
201
    
151
202
    def _facturae_data_dict(self):
 
203
        #print "self.invoice_data_dict",self.invoice_data_dict
152
204
        return self.invoice_data_dict
153
205
    
154
 
    def _get_facturae_data_dict(self, invoice_id):
 
206
    def _get_facturae_data_dict(self, invoice):
 
207
        self._set_invoice_sequence_and_approval( invoice.id )
 
208
        #invoice_obj = pool.get('account.invoice')
 
209
        self.taxes = [tax for tax in invoice.tax_line if tax.tax_percent > 0.01]
 
210
        self.taxes_ret = [tax for tax in invoice.tax_line if tax.tax_percent <= 0.01]
 
211
        return ""
 
212
        """
155
213
        pool = pooler.get_pool(self.cr.dbname)
156
214
        invoice_obj = pool.get('account.invoice')
157
 
        invoice_tax_obj = pool.get('account.invoice.tax')
 
215
        self.invoice_data_dict = invoice_obj._get_facturae_invoice_xml_data(self.cr, self.uid, [invoice_id], context={'type_data': 'dict'})
 
216
        #print "self.invoice_data_dict['Comprobante']['Impuestos']['Traslados']",self.invoice_data_dict['Comprobante']['Impuestos']['Traslados']
158
217
        try:
159
 
            self._set_invoice_sequence_and_approval( invoice_id )
160
 
        except:
161
 
            print "report error: self._set_invoice_sequence_and_approval( invoice_id )"
 
218
            self.taxes = [ traslado['Traslado'] for traslado in self.invoice_data_dict['Comprobante']['Impuestos']['Traslados'] if float( traslado['Traslado']['tasa'] ) >0.01 ]
 
219
            #self.taxes = [tax for tax in invoice.account_invoice_tax if tax.tax_percent > 0.01]
 
220
            #self.taxes.extend( self.taxes_ret )
 
221
        except Exception, e:
 
222
            print "exception: %s"%( e )
162
223
            pass
163
 
        invoice = invoice_obj.browse(self.cr, self.uid, [invoice_id])[0]
164
 
        tax_line_ids = [tax_line.id for tax_line in invoice.tax_line]
165
 
        tax_datas = invoice_tax_obj._get_tax_data(self.cr, self.uid, tax_line_ids)
166
 
        self.taxes = tax_datas.values()
 
224
        
 
225
        #self.taxes_ret = []
 
226
        for retencion in self.invoice_data_dict['Comprobante']['Impuestos'].get('Retenciones', []):
 
227
            amount_untaxed = float( self.invoice_data_dict['Comprobante']['subTotal'] )
 
228
            tax_ret_amount = float( retencion['Retencion']['importe'] )
 
229
            tasa = tax_ret_amount and amount_untaxed and tax_ret_amount * 100 / amount_untaxed or 0.0
 
230
            retencion['Retencion'].update({'tasa':  tasa})
 
231
            self.taxes_ret.append( retencion['Retencion'] )
167
232
        return ""
 
233
        """
168
234
    
169
235
report_sxw.report_sxw(
170
236
    'report.account.invoice.facturae.pdf2',