~openerp-spain-team/openerp-spain/6.0

« back to all changes in this revision

Viewing changes to l10n_es_aeat_mod340/wizard/calculate_mod340_records.py

  • Committer: Ignacio Ibeas - Acysos S.L.
  • Date: 2013-05-23 17:16:14 UTC
  • mfrom: (346.1.2 openerp-spain-6.0-mod340)
  • Revision ID: ignacio@acysos.com-20130523171614-3sxgt1va47w01rnu
[FIX] l10n_es_aeat_mod340: añade seleccion de periodos, impuestos a incluir en el modelo y registro de tipo 0

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
##############################################################################
3
3
#
4
4
#    Copyright (C) 2011 Ting. All Rights Reserved
5
 
#    Copyright (c) 2011 Acysos S.L. (http://acysos.com) All Rights Reserved
 
5
#    Copyright (c) 2011-2013 Acysos S.L. (http://acysos.com) All Rights Reserved
6
6
#                       Ignacio Ibeas Izquierdo <ignacio@acysos.com>
7
7
#
8
8
#    This program is free software: you can redistribute it and/or modify
53
53
            
54
54
            invoices340 = self.pool.get('l10n.es.aeat.mod340.issued')
55
55
            invoices340_rec = self.pool.get('l10n.es.aeat.mod340.received')
 
56
            period_obj = self.pool.get('account.period')
56
57
            
57
58
            mod340.write({
58
59
                'state' : 'calculated',
64
65
        
65
66
            wf_service = netsvc.LocalService("workflow")
66
67
            wf_service.trg_validate(uid, 'l10n.es.aeat.mod347.report', ids and ids[0], 'calculate', cr)
67
 
            
68
 
            fecha_ini = False
69
 
            fecha_fin = False
70
 
            
71
 
            dec_year =mod340.fiscalyear_id.date_start.split('-')[0]
72
 
            
73
 
            mod = mod340.period
74
 
 
75
 
            if mod >= '01' and mod <= '12':
76
 
                fecha_ini = datetime.strptime('%s-%s-01' % (dec_year, mod), '%Y-%m-%d')
77
 
                fecha_fin = fecha_ini + relativedelta(months=+1, days=-1)
78
 
 
79
 
            if mod in ('1T', '2T', '3T', '4T'):
80
 
                month = ( ( int(mod[0])-1 ) * 3 ) + 1
81
 
                fecha_ini = datetime.strptime('%s-%s-01' % (dec_year, month), '%Y-%m-%d')
82
 
                fecha_fin = fecha_ini + relativedelta(months=+3, days=-1)
83
 
                mod = '%02d' % month
84
 
 
85
 
            code = '340'+dec_year+''+mod+'0001'
86
 
            
87
 
            account_period_id = self.pool.get('account.period').search(cr,uid,[('date_start','=',fecha_ini),('date_stop','=',fecha_fin)])
88
 
            
89
 
            if not account_period_id:
90
 
                raise osv.except_osv(_('El periodo seleccionado no coincide con los periodos del año fiscal:'), dec_year)
91
 
            
 
68
 
 
69
            code = '340'+mod340.fiscalyear_id.code+''+mod340.period_to.date_stop[5:7]+'0001'
 
70
            
 
71
            account_period_ids = period_obj.build_ctx_periods(cr, uid, mod340.period_from.id, mod340.period_to.id)
 
72
 
 
73
            if len(account_period_ids) == 0:
 
74
                raise osv.except_osv(_('Error'), _("The periods selected don't belong to the fiscal year  %s") % (mod340.fiscalyear_id.name))
92
75
            
93
76
            tot_base = 0
94
77
            tot_amount = 0
110
93
            if del_ids:
111
94
                invoices340_rec.unlink(cr, uid, del_ids, context=context)
112
95
            
113
 
            domain = [('period_id', '=',account_period_id[0]), 
 
96
            domain = [('period_id', 'in',account_period_ids), 
114
97
                ('state', 'in', ('open', 'paid'))]
115
98
 
116
99
            invoice_ids = self.pool.get('account.invoice').search(cr, uid, domain, context=context)
117
100
            for invoice in self.pool.get('account.invoice').browse(cr, uid, invoice_ids, context):
118
 
                if invoice.partner_id.vat_type == 1:
119
 
                    if not invoice.partner_id.vat:
120
 
                        raise osv.except_osv(_('La siguiente empresa no tiene asignado nif:'), invoice.partner_id.name)
121
 
                
122
 
                nif = invoice.partner_id.vat and re.match(r"([A-Z]{0,2})(.*)", invoice.partner_id.vat).groups()[1]
123
 
                country_code = invoice.address_invoice_id.country_id.code
124
 
                
125
 
                values = {
126
 
                    'mod340_id': mod340.id,
127
 
                    'partner_id':invoice.partner_id.id,
128
 
                    'partner_vat':nif,
129
 
                    'representative_vat': '',
130
 
                    'partner_country_code' : country_code,
131
 
                    'invoice_id':invoice.id,
132
 
                    'base_tax':invoice.amount_untaxed,
133
 
                    'amount_tax':invoice.amount_tax,
134
 
                    'total':invoice.amount_total
135
 
                }
136
 
                if invoice.type in ( 'out_refund','in_refund'):
137
 
                    values['base_tax'] *=-1
138
 
                    values['amount_tax'] *=-1
139
 
                    values['total'] *=-1
140
 
 
141
 
 
142
 
                if invoice.type=="out_invoice" or invoice.type=="out_refund":
143
 
                    invoice_created = invoices340.create(cr,uid,values)
144
 
                    
145
 
                if invoice.type=="in_invoice" or invoice.type=="in_refund":
146
 
                    invoice_created = invoices340_rec.create(cr,uid,values)
147
 
                
148
 
                tot_tax_invoice = 0
149
 
                check_tax=0
150
 
                check_base=0
151
 
                
152
 
                # Add the invoices detail to the partner record
 
101
                include = False
153
102
                for tax_line in invoice.tax_line:
154
 
                    if tax_line.name.find('IRPF') == -1: # Remove IRPF from Mod340
155
 
                        tax_description = tax_line.name.split(' - ')
156
 
                        if len(tax_description) == 2: name = tax_description[1]
157
 
                        if len(tax_description) == 1: name = tax_description[0]
158
 
                        account_tax = self.pool.get('account.tax').browse(cr, uid, self.pool.get('account.tax').search(cr, uid, [('name','=',name)], context=context))
159
 
                        if  account_tax[0].amount < 0:
160
 
                            continue
161
 
 
162
 
                        values = {
163
 
                            'name': name,
164
 
                            'tax_percentage': account_tax[0].amount,
165
 
                            'tax_amount': tax_line.tax_amount,
166
 
                            'base_amount': tax_line.base_amount,
167
 
                            'invoice_record_id': invoice_created,
168
 
                        }
169
 
                        if invoice.type=="out_invoice" or invoice.type=="out_refund":
170
 
                            self.pool.get('l10n.es.aeat.mod340.tax_line_issued').create(cr, uid, values)
171
 
                        if invoice.type=="in_invoice" or invoice.type=="in_refund":
172
 
                            self.pool.get('l10n.es.aeat.mod340.tax_line_received').create(cr, uid, values)
173
 
                        tot_tax_invoice += tax_line.tax_amount
174
 
                        tot_rec += 1
175
 
                        check_tax += tax_line.tax_amount
176
 
                        check_base += tax_line.base_amount
177
 
                                                          
178
 
                tot_base += invoice.amount_untaxed
179
 
                tot_amount += tot_tax_invoice
180
 
                tot_tot += invoice.amount_untaxed + tot_tax_invoice
181
 
            
182
 
                if invoice.type=="out_invoice" or invoice.type=="out_refund":
183
 
                    invoices340.write(cr,uid,invoice_created,{'amount_tax':tot_tax_invoice})
184
 
                if invoice.type=="in_invoice" or invoice.type=="in_refund":
185
 
                    invoices340_rec.write(cr,uid,invoice_created,{'amount_tax':tot_tax_invoice})
186
 
 
187
 
                sign=1
188
 
                if  invoice.type in ( 'out_refund','in_refund' ):
189
 
                    sign = -1
190
 
                    
191
 
                if str(invoice.amount_untaxed*sign) != str(check_base):
192
 
                    print "id:", invoice.id, "base: %.5f"%invoice.amount_untaxed , "taxlibe base: %.5f"%check_base, "number:",invoice.number
193
 
                    raise osv.except_osv( "REVIEW INVOICE", _('Invoice  %s, Amount untaxed Lines %.2f do not correspond to AmountUntaxed on Invoice %.2f' )%(invoice.number, check_base,  invoice.amount_untaxed*sign)  )
 
103
                    if tax_line.base_code_id:
 
104
                        if tax_line.base_code_id.mod340 == True:
 
105
                            include = True
 
106
                if include == True:
 
107
                    if invoice.partner_id.vat_type == 1:
 
108
                        if not invoice.partner_id.vat:
 
109
                            raise osv.except_osv(_('La siguiente empresa no tiene asignado nif:'), invoice.partner_id.name)
 
110
                    
 
111
                    nif = invoice.partner_id.vat and re.match(r"([A-Z]{0,2})(.*)", invoice.partner_id.vat).groups()[1]
 
112
                    country_code = invoice.address_invoice_id.country_id.code
 
113
                    
 
114
                    values = {
 
115
                        'mod340_id': mod340.id,
 
116
                        'partner_id':invoice.partner_id.id,
 
117
                        'partner_vat':nif,
 
118
                        'representative_vat': '',
 
119
                        'partner_country_code' : country_code,
 
120
                        'invoice_id':invoice.id,
 
121
                        'base_tax':invoice.amount_untaxed,
 
122
                        'amount_tax':invoice.amount_tax,
 
123
                        'total':invoice.amount_total
 
124
                    }
 
125
                    if invoice.type in ( 'out_refund','in_refund'):
 
126
                        values['base_tax'] *=-1
 
127
                        values['amount_tax'] *=-1
 
128
                        values['total'] *=-1
 
129
    
 
130
    
 
131
                    if invoice.type=="out_invoice" or invoice.type=="out_refund":
 
132
                        invoice_created = invoices340.create(cr,uid,values)
 
133
                        
 
134
                    if invoice.type=="in_invoice" or invoice.type=="in_refund":
 
135
                        invoice_created = invoices340_rec.create(cr,uid,values)
 
136
                    
 
137
                    tot_tax_invoice = 0
 
138
                    check_tax=0
 
139
                    check_base=0
 
140
                    
 
141
                    # Add the invoices detail to the partner record
 
142
                    for tax_line in invoice.tax_line:
 
143
                        if tax_line.base_code_id:
 
144
                            if tax_line.base_code_id.mod340 == True:
 
145
                                tax_percentage = tax_line.amount/tax_line.base
 
146
        
 
147
                                values = {
 
148
                                    'name': tax_line.name,
 
149
                                    'tax_percentage': tax_percentage,
 
150
                                    'tax_amount': tax_line.tax_amount,
 
151
                                    'base_amount': tax_line.base_amount,
 
152
                                    'invoice_record_id': invoice_created,
 
153
                                }
 
154
                                if invoice.type=="out_invoice" or invoice.type=="out_refund":
 
155
                                    self.pool.get('l10n.es.aeat.mod340.tax_line_issued').create(cr, uid, values)
 
156
                                if invoice.type=="in_invoice" or invoice.type=="in_refund":
 
157
                                    self.pool.get('l10n.es.aeat.mod340.tax_line_received').create(cr, uid, values)
 
158
                                tot_tax_invoice += tax_line.tax_amount
 
159
                                tot_rec += 1
 
160
                                check_tax += tax_line.tax_amount
 
161
                                check_base += tax_line.base_amount
 
162
                                                            
 
163
                    tot_base += invoice.amount_untaxed
 
164
                    tot_amount += tot_tax_invoice
 
165
                    tot_tot += invoice.amount_untaxed + tot_tax_invoice
 
166
                
 
167
                    if invoice.type=="out_invoice" or invoice.type=="out_refund":
 
168
                        invoices340.write(cr,uid,invoice_created,{'amount_tax':tot_tax_invoice})
 
169
                    if invoice.type=="in_invoice" or invoice.type=="in_refund":
 
170
                        invoices340_rec.write(cr,uid,invoice_created,{'amount_tax':tot_tax_invoice})
 
171
    
 
172
                    sign=1
 
173
                    if  invoice.type in ( 'out_refund','in_refund' ):
 
174
                        sign = -1
 
175
                        
 
176
                    if str(invoice.amount_untaxed*sign) != str(check_base):
 
177
                        print "id:", invoice.id, "base: %.5f"%invoice.amount_untaxed , "taxlibe base: %.5f"%check_base, "number:",invoice.number
 
178
                        raise osv.except_osv( "REVIEW INVOICE", _('Invoice  %s, Amount untaxed Lines %.2f do not correspond to AmountUntaxed on Invoice %.2f' )%(invoice.number, check_base,  invoice.amount_untaxed*sign)  )
194
179
                
195
180
            mod340.write({'total_taxable':tot_base,'total_sharetax':tot_amount,'number_records':tot_rec,'total':tot_tot,'number':code})
196
181