77d8515d78bb14fbfe19b911d1fcef32dc348c0c
[odoo/odoo.git] / addons / l10n_be / wizard / l10n_be_account_vat_declaration.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #    
7 #    Adapted by Noviat to 
8 #     - enforce correct vat number
9 #     - support negative balance
10 #     - assign amount of tax code 71-72 correclty to grid 71 or 72
11 #     - support Noviat tax code scheme
12 #
13 #    This program is free software: you can redistribute it and/or modify
14 #    it under the terms of the GNU Affero General Public License as
15 #    published by the Free Software Foundation, either version 3 of the
16 #    License, or (at your option) any later version.
17 #
18 #    This program is distributed in the hope that it will be useful,
19 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
20 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 #    GNU Affero General Public License for more details.
22 #
23 #    You should have received a copy of the GNU Affero General Public License
24 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
25 #
26 ##############################################################################
27 import base64
28
29 from osv import osv, fields
30 from tools.translate import _
31
32 class l10n_be_vat_declaration(osv.osv_memory):
33     """ Vat Declaration """
34     _name = "l1on_be.vat.declaration"
35     _description = "Vat Declaration"
36
37     def _get_xml_data(self, cr, uid, context=None):
38         if context.get('file_save', False):
39             return base64.encodestring(context['file_save'].encode('utf8'))
40         return ''
41
42     _columns = {
43         'name': fields.char('File Name', size=32),
44         'period_id': fields.many2one('account.period','Period', required=True),
45         'tax_code_id': fields.many2one('account.tax.code', 'Tax Code', domain=[('parent_id', '=', False)]),
46         'msg': fields.text('File created', size=64, readonly=True),
47         'file_save': fields.binary('Save File'),
48         'ask_restitution': fields.boolean('Ask Restitution',help='It indicates whether a resitution is to made or not?'),
49         'ask_payment': fields.boolean('Ask Payment',help='It indicates whether a payment is to made or not?'),
50         'client_nihil': fields.boolean('Last Declaration of Enterprise',help='Tick this case only if it concerns only the last statement on the civil or cessation of activity'),
51     }
52     _defaults = {
53         'msg': 'Save the File with '".xml"' extension.',
54         'file_save': _get_xml_data,
55         'name': 'vat_declaration.xml',
56     }
57
58     def create_xml(self, cr, uid, ids, context=None):
59         obj_tax_code = self.pool.get('account.tax.code')
60         obj_acc_period = self.pool.get('account.period')
61         obj_user = self.pool.get('res.users')
62         mod_obj = self.pool.get('ir.model.data')
63
64         if context is None:
65             context = {}
66
67         list_of_tags = ['00','01','02','03','44','45','46','47','48','49','54','55','56','57','59','61','62','63','64','71','72','81','82','83','84','85','86','87','88','91']
68         data_tax = self.browse(cr, uid, ids[0])
69         if data_tax.tax_code_id:
70             obj_company = data_tax.tax_code_id.company_id
71         else:
72             obj_company = obj_user.browse(cr, uid, uid, context=context).company_id
73         vat_no = obj_company.partner_id.vat.replace(' ','').upper()
74         if not vat_no:
75             raise osv.except_osv(_('Data Insufficient'), _('No VAT Number Associated with Main Company!'))
76
77         tax_code_ids = obj_tax_code.search(cr, uid, [], context=context)
78         ctx = context.copy()
79         data  = self.read(cr, uid, ids)[0]
80         ctx['period_id'] = data['period_id'][0] #added context here
81         tax_info = obj_tax_code.read(cr, uid, tax_code_ids, ['code','sum_period'], context=ctx)
82
83         address = post_code = city = country_code = ''
84         city, post_code, address, country_code = self.pool.get('res.company')._get_default_ad(obj_company.partner_id.address)
85
86         account_period = obj_acc_period.browse(cr, uid, data['period_id'][0], context=context)
87
88         send_ref = str(obj_company.partner_id.id) + str(account_period.date_start[5:7]) + str(account_period.date_stop[:4])
89         data_of_file = '<?xml version="1.0"?>\n<VATSENDING xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="MultiDeclarationTVA-NoSignature-16.xml">'
90         data_of_file +='\n\t<DECLARER>\n\t\t<VATNUMBER>'+str(vat_no)+'</VATNUMBER>\n\t\t<NAME>'+ obj_company.name +'</NAME>\n\t\t<ADDRESS>'+address+'</ADDRESS>'
91         data_of_file +='\n\t\t<POSTCODE>'+post_code+'</POSTCODE>\n\t\t<CITY>'+city+'</CITY>\n\t\t<COUNTRY>'+country_code+'</COUNTRY>\n\t\t<SENDINGREFERENCE>'+send_ref+'</SENDINGREFERENCE>\n\t</DECLARER>'
92         data_of_file +='\n\t<VATRECORD>\n\t\t<RECNUM>1</RECNUM>\n\t\t<VATNUMBER>'+((vat_no and str(vat_no[2:])) or '')+'</VATNUMBER>\n\t\t<DPERIODE>\n\t\t\t'
93
94         starting_month = account_period.date_start[5:7]
95         ending_month = account_period.date_stop[5:7]
96         if starting_month != ending_month:
97             #starting month and ending month of selected period are not the same
98             #it means that the accounting isn't based on periods of 1 month but on quarters
99             quarter = str(((int(starting_month) - 1) / 3) + 1)
100             data_of_file += '<QUARTER>'+quarter+'</QUARTER>\n\t\t\t'
101         else:
102             data_of_file += '<MONTH>'+starting_month+'</MONTH>\n\t\t\t'
103         data_of_file += '<YEAR>' + str(account_period.date_stop[:4]) + '</YEAR>\n\t\t</DPERIODE>\n'
104         data_of_file += '\t\t<ASK RESTITUTION="' + (data['ask_restitution'] and 'YES' or 'NO') + '" PAYMENT="' + (data['ask_payment'] and 'YES' or 'NO') +'"/>'
105         data_of_file += '\n\t\t<ClientListingNihil>'+ (data['client_nihil'] and 'YES' or 'NO') +'</ClientListingNihil>'
106         data_of_file +='\n\t\t<DATA>\n\t\t\t<DATA_ELEM>'
107
108         cases_list = []
109         for item in tax_info:
110             if item['code'] == '91' and ending_month != 12:
111                 #the tax code 91 can only be send for the declaration of December
112                 continue
113             if item['code']:
114                 if item['code'] == 'VI':
115                     if item['sum_period'] >= 0:
116                         item['code'] = '71'
117                     else:
118                         item['code'] = '72'
119                 if item['code'] in list_of_tags:
120                     cases_list.append(item)
121         cases_list.sort()
122         for item in cases_list:
123             data_of_file +='\n\t\t\t\t<D'+str(int(item['code'])) +'>' + str(abs(int(round(item['sum_period']*100)))) +  '</D'+str(int(item['code'])) +'>'
124
125         data_of_file += '\n\t\t\t</DATA_ELEM>\n\t\t</DATA>\n\t</VATRECORD>\n</VATSENDING>'
126         model_data_ids = mod_obj.search(cr, uid,[('model','=','ir.ui.view'),('name','=','view_vat_save')], context=context)
127         resource_id = mod_obj.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
128         context['file_save'] = data_of_file
129         return {
130             'name': _('Save XML For Vat declaration'),
131             'context': context,
132             'view_type': 'form',
133             'view_mode': 'form',
134             'res_model': 'l1on_be.vat.declaration',
135             'views': [(resource_id,'form')],
136             'view_id': 'view_vat_save',
137             'type': 'ir.actions.act_window',
138             'target': 'new',
139         }
140
141 l10n_be_vat_declaration()
142
143 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: