~txerpa-openerp/openerp-spain/7.0-l10n_es_account_asset_extensions

« back to all changes in this revision

Viewing changes to l10n_es_aeat_mod340/wizard/export_mod340_to_boe.py

  • Committer: Ignacio Ibeas - Acysos S.L.
  • Date: 2014-02-03 11:13:07 UTC
  • mto: (391.1.31 openerp-spain)
  • mto: This revision was merged to the branch mainline in revision 393.
  • Revision ID: ignacio@acysos.com-20140203111307-01wochg4hx3yd0l1
[ADD] l10n_es_aeat_mod340: migración a OpenERP 7.0 y función añadida de Libros de IVA

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- coding: utf-8 -*-
2
2
##############################################################################
3
3
#
4
 
#    Copyright (c) 2011 Acysos S.L. (http://acysos.com) All Rights Reserved.
 
4
#    Copyright (c) 2011-2013 Acysos S.L. (http://acysos.com) All Rights Reserved.
5
5
#                       Ignacio Ibeas <ignacio@acysos.com>
6
6
#    Copyright (c) 2011 NaN Projectes de Programari Lliure, S.L.
7
7
#                       http://www.NaN-tic.com
21
21
#
22
22
##############################################################################
23
23
 
 
24
from datetime import datetime
 
25
from openerp.tools.translate import _
 
26
from openerp.osv import orm
24
27
import base64
25
28
import time
26
29
 
27
 
from osv import osv
28
 
from tools.translate import _
29
 
 
30
 
 
31
 
class l10n_es_aeat_mod340_export_to_boe(osv.osv_memory):
32
 
 
 
30
 
 
31
class l10n_es_aeat_mod340_export_to_boe(orm.TransientModel):
33
32
    _inherit = "l10n.es.aeat.report.export_to_boe"
34
33
    _name = "l10n.es.aeat.mod340.export_to_boe"
35
34
    _description = "Export AEAT Model 340 to BOE format"
63
62
            416-500     Blancos
64
63
        """
65
64
        text = ''
66
 
 
67
 
        text += '1'                                           # Tipo de Registro
68
 
        text += '340'                                         # Modelo Declaración
69
 
        text += self._formatString(report.fiscalyear_id.code, 4)   # Ejercicio
70
 
        text += self._formatString(report.company_vat, 9)          # NIF del declarante
71
 
        text += self._formatString(report.company_id.name, 40)     # Apellidos y nombre o razón social del declarante
72
 
        text += self._formatString(report.support_type, 1)         # Tipo de soporte
73
 
        text += self._formatString(report.contact_phone, 9)       # Persona de contacto (Teléfono)
74
 
        text += self._formatString(report.name_contact, 40)        # Persona de contacto (Apellidos y nombre)
75
 
        text += self._formatNumber(report.number, 13)              # Número identificativo de la declaración
76
 
        if (report.type == 'C'): text += 'C'                       # Declaración complementaria
77
 
        else: text += ' '
78
 
        if (report.type == 'S'): text += 'S'                       # Declaración substitutiva
79
 
        else: text += ' '
80
 
        text += self._formatNumber(report.previous_number, 13)     # Número identificativo de la declaración anterior
81
 
        text += self._formatString(report.period,2)     # Periodo
82
 
        text += self._formatNumber(report.number_records, 9)          # Número total de registros
83
 
        text += self._formatNumber(report.total_taxable, 15, 2,True)       # Importe total de la base imponible
84
 
        text += self._formatNumber(report.total_sharetax, 15, 2,True)      # Importe Total de la cuota del impuesto
85
 
        text += self._formatNumber(report.total, 15, 2,True)      # Importe total de las facturas
86
 
        text += 190*' '                                       # Blancos
87
 
        text += self._formatString(report.representative_vat, 9)   # NIF del representante legal
88
 
        text += self._formatString(report.ean13, 17)   # Sello electrónico
89
 
        text += 84*' '                                        # Blancos
 
65
        # Tipo de Registro
 
66
        text += '1'
 
67
        # Modelo Declaración                                           
 
68
        text += '340'
 
69
        # Ejercicio                                         
 
70
        text += self._formatString(report.fiscalyear_id.code, 4)
 
71
        # NIF del declarante  
 
72
        text += self._formatString(report.company_vat, 9)
 
73
        # Apellidos y nombre o razón social del declarante         
 
74
        text += self._formatString(report.company_id.name, 40)
 
75
        # Tipo de soporte 
 
76
        text += self._formatString(report.support_type, 1)
 
77
        # Persona de contacto (Teléfono)        
 
78
        text += self._formatString(report.contact_phone, 9)
 
79
        # Persona de contacto (Apellidos y nombre)      
 
80
        text += self._formatString(report.name_contact, 40)
 
81
        # Número identificativo de la declaración        
 
82
        text += self._formatNumber(report.number, 13)
 
83
        # Declaración complementaria
 
84
        if (report.type == 'C'): text += 'C'                       
 
85
        else: text += ' '
 
86
        # Declaración substitutiva
 
87
        if (report.type == 'S'): text += 'S'
 
88
        else: text += ' '
 
89
        # Número identificativo de la declaración anterior
 
90
        text += self._formatNumber(report.previous_number, 13)
 
91
        period_stop = report.period_to.date_stop[5:7]
 
92
        period_start = report.period_to.date_start[5:7]
 
93
        if period_start == period_stop:
 
94
            period = period_stop
 
95
        else:
 
96
            if period_stop == '03':
 
97
                period = '1T'
 
98
            elif period_stop == '06':
 
99
                period = '2T'
 
100
            elif period_stop == '09':
 
101
                period = '3T'
 
102
            elif period_stop == '12':
 
103
                period = '4T'
 
104
            else:
 
105
                raise orm.except_orm( "ERROR",
 
106
                              _("The period hasn't a valid Mod340 period" ))
 
107
        # Periodo
 
108
        text += self._formatString(period,2)
 
109
        # Número total de registros 
 
110
        text += self._formatNumber(report.number_records, 9)
 
111
        # Importe total de la base imponible     
 
112
        text += self._formatNumber(report.total_taxable, 15, 2,True)
 
113
        # Importe Total de la cuota del impuesto
 
114
        text += self._formatNumber(report.total_sharetax, 15, 2,True)
 
115
        # Importe total de las facturas
 
116
        text += self._formatNumber(report.total, 15, 2,True)
 
117
        # Blancos   
 
118
        text += 190*' '
 
119
        # NIF del representante legal
 
120
        text += self._formatString(report.representative_vat, 9)
 
121
        # Sello electrónico
 
122
        text += self._formatString(report.ean13, 17)
 
123
        # Blancos
 
124
        text += 84*' '                                        
90
125
        text += '\r\n'
91
126
 
92
 
        assert len(text) == 502, _("The type 1 record must be 500 characters long")
 
127
        assert len(text)==502,_("The type 1 record must be 500 characters long")
 
128
        print 'Declaration'
 
129
        print text
93
130
        return text
94
131
    
95
132
    def _get_formated_invoice_issued(self, cr, uid, report, invoice_issued):
105
142
            9-17        NIF del declarante
106
143
            18-26       NIF del declarado
107
144
            27-35       NIF del representante legal
108
 
            36-75       Apellidos y nombre, razón social o denominación del declarado
 
145
            36-75       Apellidos y nombre, razón social o
 
146
                        denominación del declarado
109
147
            76-77       Código país
110
148
            78          Clave de identificación en el país de residencia
111
 
            79-95       Número de identificación fiscal en el país de residencia. TODO de momento blancos.
 
149
            79-95       Número de identificación fiscal en el país de
 
150
                        residencia. TODO de momento blancos.
112
151
            96-98       Blancos
113
152
            99          Clave tipo de libro. Constante 'E'.
114
 
            100         Clave de operación. Constante ' ' para un solo tipo de IVA. Constante 'C' para varios tipos de IVA. TODO Resto de operaciones. Varios tipos impositivos.
 
153
            100         Clave de operación. Constante ' ' para un solo tipo de
 
154
                        IVA. Constante 'C' para varios tipos de IVA. TODO Resto
 
155
                        de operaciones. Varios tipos impositivos.
115
156
            101-108     Fecha de expedición
116
 
            109-116     Fecha de operación. Se consigna la misma que expedición. TODO. Fecha del uso del bien.
 
157
            109-116     Fecha de operación. Se consigna la misma que
 
158
                        expedición. TODO. Fecha del uso del bien.
117
159
            117-121     Tipo impositivo
118
160
            122-135     Base imponible
119
161
            136-149     Cuota del impuesto
121
163
            164-177     Base imponible a coste. TODO de momento 0.
122
164
            178-217     Identificación de la factura
123
165
            218-235     Número de registro TODO No se exactamente que es
124
 
            236-243     Número de facturas. Siempre 1. TODO Resumenes de facturas o tickets. Clave A o B.
125
 
            244-245     Número de registro. Siempre 1. TODO Facturas con varios asientos. Clave C.
126
 
            246-325     Intervalo de acumulación. Vacio. TODO Intervalo de resumenes de facturas o tickets.
 
166
            236-243     Número de facturas. Siempre 1. TODO Resumenes de
 
167
                        facturas o tickets. Clave A o B.
 
168
            244-245     Número de registro. Siempre 1. TODO Facturas con varios
 
169
                        asientos. Clave C.
 
170
            246-325     Intervalo de acumulación. Vacio. TODO Intervalo de
 
171
                        resumenes de facturas o tickets.
127
172
            326-365     Identificación de la factura rectificativa. TODO.
128
173
            366-370     Tipo recargo de equivalencia. TODO.
129
174
            371-384     Cuota recargo de equivalencia. TODO.
130
 
            385         Situación del Inmueble #TODO  2012
131
 
            386-410     Referencia Catastral #TODO 2012
132
 
            411-425     Importe Percibido en Metálico #TODO 2012
133
 
            426-429     Ejercicio ( cifras del ejercicio en el que se hubieran declarado las operaciones que dan origen al cobro ) #TODO 2012
134
 
            430-444     Importe percibido por transmisiones de Inmuebles sujetas a IVA. #TODO 2012
 
175
            385         Situación del Inmueble #TODO
 
176
            386-410     Referencia Catastral #TODO
 
177
            411-425     Importe Percibido en Metálico #TODO
 
178
            426-429     Ejercicio (cifras del ejercicio en el que se hubieran
 
179
                        declarado las operaciones que dan origen al cobro)
 
180
                        #TODO
 
181
            430-444     Importe percibido por transmisiones de Inmuebles
 
182
                        sujetas a IVA. #TODO
135
183
            445-500     BLANCOS            
136
184
            
137
185
 
138
186
        """
139
187
        text = ''
140
188
        for tax_line in invoice_issued.tax_line_ids:
141
 
    
142
 
            text += '2'                                                     # Tipo de Registro
143
 
            text += '340'                                                   # Modelo Declaración
144
 
            text += self._formatString(report.fiscalyear_id.code, 4)             # Ejercicio
145
 
            text += self._formatString(report.company_vat, 9)                    # NIF del declarante
146
 
            if invoice_issued.partner_country_code == 'ES': text += self._formatString(invoice_issued.partner_vat, 9)            # NIF del declarado
147
 
            else: text += self._formatString(' ', 9) 
148
 
            text += self._formatString(invoice_issued.representative_vat, 9)     # NIF del representante legal
149
 
            text += self._formatString(invoice_issued.partner_id.name, 40)       # Apellidos y nombre, razón social o denominación del declarado
150
 
            text += self._formatString(invoice_issued.partner_country_code, 2)     # Código país
151
 
            text += self._formatNumber(invoice_issued.partner_id.vat_type, 1)   # Clave de identificación en el país de residencia
152
 
            if invoice_issued.partner_country_code != 'ES':                     # Número de identificación fiscal en el país de residencia.
153
 
                text += self._formatString(invoice_issued.partner_country_code, 2)
154
 
                text += self._formatString(invoice_issued.partner_vat, 15)
 
189
            # Tipo de Registro
 
190
            text += '2'
 
191
            # Modelo Declaración
 
192
            text += '340'
 
193
            # Ejercicio                                                 
 
194
            text += self._formatString(report.fiscalyear_id.code, 4)
 
195
            # NIF del declarante       
 
196
            text += self._formatString(report.company_vat, 9)
 
197
            # NIF del declarado        
 
198
            if invoice_issued.partner_country_code == 'ES': 
 
199
                text += self._formatString(invoice_issued.partner_vat, 9)
 
200
            else: text += self._formatString(' ', 9)
 
201
            # NIF del representante legal
 
202
            text += self._formatString(invoice_issued.representative_vat, 9)
 
203
            # Apellidos y nombre, razón social o denominación del declarado
 
204
            text += self._formatString(invoice_issued.partner_id.name, 40)
 
205
            # Código país
 
206
            text += self._formatString(invoice_issued.partner_country_code, 2)
 
207
            # Clave de identificación en el país de residencia
 
208
            text += self._formatNumber(invoice_issued.partner_id.vat_type, 1)
 
209
            # Número de identificación fiscal en el país de residencia.   
 
210
            if invoice_issued.partner_country_code != 'ES':
 
211
                text+=self._formatString(invoice_issued.partner_country_code,2)
 
212
                text+=self._formatString(invoice_issued.partner_vat, 15)
155
213
            else: text += 17*' '
156
 
            text += 3*' '                                                     # Blancos
157
 
            text += 'E'                                                         # Clave tipo de libro. Constante 'E'.
158
 
            
159
 
            if invoice_issued.invoice_id.origin_invoices_ids:               # Clave de operación
 
214
            # Blancos
 
215
            text += 3*' '
 
216
            # Clave tipo de libro. Constante 'E'.                                           
 
217
            text += 'E'                                                         
 
218
            # Clave de operación
 
219
            if invoice_issued.invoice_id.origin_invoices_ids:               
160
220
                text +='D'
161
221
            elif len(invoice_issued.tax_line_ids) > 1: text += 'C'
162
222
            elif invoice_issued.invoice_id.is_ticket_summary == 1: text += 'B'
164
224
            
165
225
            text += self._formatNumber(invoice_issued.invoice_id.date_invoice.split('-')[0],4)
166
226
            text += self._formatNumber(invoice_issued.invoice_id.date_invoice.split('-')[1],2)
167
 
            text += self._formatNumber(invoice_issued.invoice_id.date_invoice.split('-')[2],2)    # Fecha de expedición
 
227
            text += self._formatNumber(invoice_issued.invoice_id.date_invoice.split('-')[2],2)    
168
228
            text += self._formatNumber(invoice_issued.invoice_id.date_invoice.split('-')[0],4)
169
229
            text += self._formatNumber(invoice_issued.invoice_id.date_invoice.split('-')[1],2)
170
 
            text += self._formatNumber(invoice_issued.invoice_id.date_invoice.split('-')[2],2)    # Fecha de operación
171
 
            
172
 
            text += self._formatNumber(tax_line.tax_percentage*100,3,2)                        #Tipo impositivo
173
 
            text += self._formatNumber(tax_line.base_amount, 11,2,True)         # Base imponible
174
 
            text += self._formatNumber(tax_line.tax_amount, 11,2,True)         # Cuota del impuesto
175
 
            text += self._formatNumber(tax_line.tax_amount+tax_line.base_amount, 11,2,True)         # Importe total de la factura
176
 
            text += ' '+self._formatNumber(0, 11,2)                             # Base imponible a coste.
177
 
            text += self._formatString(invoice_issued.invoice_id.number, 40)  # Identificación de la factura
178
 
            text += self._formatString(self.pool.get('ir.sequence').get(cr, uid, 'mod340'),18)  # Número de registro
179
 
            if invoice_issued.invoice_id.is_ticket_summary == 1:           # Número de facturas
 
230
            text += self._formatNumber(invoice_issued.invoice_id.date_invoice.split('-')[2],2)    
 
231
            #Tipo impositivo
 
232
            text += self._formatNumber(tax_line.tax_percentage*100,3,2)
 
233
            # Base imponible
 
234
            text += self._formatNumber(tax_line.base_amount, 11,2,True)
 
235
            # Cuota del impuesto 
 
236
            text += self._formatNumber(tax_line.tax_amount, 11,2,True)
 
237
            # Importe total de la factura
 
238
            text += self._formatNumber(tax_line.tax_amount+
 
239
                                       tax_line.base_amount, 11,2,True)
 
240
            # Base imponible a coste.     
 
241
            text += ' '+self._formatNumber(0, 11,2)
 
242
            # Identificación de la factura  
 
243
            text += self._formatString(invoice_issued.invoice_id.number, 40)
 
244
            # Número de registro
 
245
            text += self._formatString(self.pool.get('ir.sequence').get(cr, uid, 'mod340'),18)
 
246
            # Número de facturas
 
247
            if invoice_issued.invoice_id.is_ticket_summary == 1:           
180
248
                text += self._formatNumber(invoice_issued.invoice_id.number_tickets, 8)
181
249
            else: text += self._formatNumber(1, 8)
182
 
            text += self._formatNumber(len(invoice_issued.tax_line_ids), 2)  # Número de registros (Desglose)
183
 
            if invoice_issued.invoice_id.is_ticket_summary == 1:      # Intervalo de identificación de la acumulación
 
250
            # Número de registros (Desglose)
 
251
            text += self._formatNumber(len(invoice_issued.tax_line_ids), 2)
 
252
            # Intervalo de identificación de la acumulación
 
253
            if invoice_issued.invoice_id.is_ticket_summary == 1:      
184
254
                text += self._formatString(invoice_issued.invoice_id.first_ticket, 40)
185
255
                text += self._formatString(invoice_issued.invoice_id.last_ticket, 40)
186
256
            else: text += 80*' '
187
 
            text +=  self._formatString( ",".join( [x.number for x in  invoice_issued.invoice_id.origin_invoices_ids]) , 40 )   # Identificación factura rectificativa
 
257
            # Identificación factura rectificativa
 
258
            text +=  self._formatString( ",".join( [x.number for x in  invoice_issued.invoice_id.origin_invoices_ids]) , 40 )
188
259
            text += self._formatNumber(0, 5)  # Tipo Recargo de equivalencia
189
260
            text += ' '+self._formatNumber(0, 11,2)  # Couta del recargo de equivalencia
190
261
            text += '0'  #Situación del Inmueble #TODO  2012
254
325
            text += 3*' '                                                     # Blancos
255
326
            text += 'R'                                                         # Clave tipo de libro. Constante 'E'.
256
327
            
257
 
            if invoice_received.invoice_id.operation_key == 'I':
258
 
                text +='I'
259
 
            elif len(invoice_received.tax_line_ids) > 1: text += 'C'              # Clave de operación
 
328
            if len(invoice_received.tax_line_ids) > 1: text += 'C'              # Clave de operación
260
329
            else: text += ' '
261
330
            
262
331
            text += self._formatNumber(invoice_received.invoice_id.date_invoice.split('-')[0],4)
291
360
 
292
361
        for invoice_received in report.received:
293
362
            file_contents += self._get_formated_invoice_received(cr,uid,report, invoice_received)
294
 
 
 
363
        print 'Other'
 
364
        print file_contents
295
365
        return file_contents
296
 
    
297
 
 
298
 
    def _export_boe_file(self, cr, uid, ids, report, model=None, context=None):
299
 
        """
300
 
        Action that exports the data into a BOE formated text file
301
 
        """
302
 
        if context is None:
303
 
            context = {}
304
 
        
305
 
        file_contents = ''
306
 
 
307
 
        ##
308
 
        ## Add header record
309
 
        file_contents += self._get_formated_declaration_record(report)
310
 
 
311
 
        ##
312
 
        ## Adds other fields
313
 
        file_contents += self._get_formated_other_records(cr,uid,report)
314
 
 
315
 
        ##
316
 
        ## Generate the file and save as attachment
317
 
        file = base64.encodestring(file_contents)
318
 
 
319
 
        file_name = _("340_report_%s.txt") % (time.strftime(_("%Y-%m-%d")))
320
 
        self.pool.get("ir.attachment").create(cr, uid, {
321
 
            "name" : file_name,
322
 
            "datas" : file,
323
 
            "datas_fname" : file_name,
324
 
            "res_model" : "l10n.es.aeat.mod340",
325
 
            "res_id" : ids and ids[0]
326
 
        }, context=context)
327
 
 
328
 
l10n_es_aeat_mod340_export_to_boe()
 
366