1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
27 from tools.translate import _
30 form = """<?xml version="1.0"?>
31 <form string="Select Fiscal Year">
32 <label string="This wizard will create an XML file for Vat details and total invoiced amounts per partner." colspan="4"/>
34 <field name="date_start" default_focus="1"/>
36 <field name="date_stop"/>
38 <field name="mand_id" help="Should be provided when subscription of INTERVAT service is done"/>
40 <field name="limit_amount" help="Limit under which the partners will not be included into the listing"/>
42 <field name="test_xml" help="Set the XML output as test file"/>
46 'date_start': {'string':"Start date",'type':'date','required':True ,'default': lambda *a: time.strftime('%Y-01-01')},
47 'date_stop': {'string':"End date",'type':'date','required':True, 'default': lambda *a: time.strftime('%Y-12-01')},
48 'mand_id':{'string':'MandataireId','type':'char','size':'30','required': True,},
49 'limit_amount':{'string':'Limit Amount','type':'integer','required': True, },
50 'test_xml': {'string':'Test XML file', 'type':'boolean', },
52 msg_form = """<?xml version="1.0"?>
53 <form string="Notification">
54 <separator string="XML File has been Created." colspan="4"/>
55 <field name="msg" colspan="4" nolabel="1"/>
56 <field name="file_save" />
60 'msg': {'string':'File created', 'type':'text', 'size':'100','readonly':True},
61 'file_save':{'string': 'Save File',
66 class wizard_vat(wizard.interface):
68 def _create_xml(self, cr, uid, data, context):
70 seq_controlref = pooler.get_pool(cr.dbname).get('ir.sequence').get(cr, uid,'controlref')
71 seq_declarantnum = pooler.get_pool(cr.dbname).get('ir.sequence').get(cr, uid,'declarantnum')
72 obj_cmpny = pooler.get_pool(cr.dbname).get('res.users').browse(cr, uid, uid).company_id
73 company_vat = obj_cmpny.partner_id.vat
75 raise wizard.except_wizard(_('Data Insufficient'),_('No VAT Number Associated with Main Company!'))
77 cref = company_vat + seq_controlref
78 dnum = cref + seq_declarantnum
80 p_id_list = pooler.get_pool(cr.dbname).get('res.partner').search(cr,uid,[('vat_subjected','!=',False)])
83 raise wizard.except_wizard(_('Data Insufficient!'),_('No partner has a VAT Number asociated with him.'))
84 domains = [('date_start','>=',data['form']['date_start']),('date_stop','<=',data['form']['date_stop'])]
85 date_stop = data['form']['date_stop']
86 period_ids = pooler.get_pool(cr.dbname).get('account.period').search(cr, uid, domains)
87 period = "("+','.join(map(lambda x: str(x), period_ids)) +")"
89 street = zip_city = country = ''
90 addr = pooler.get_pool(cr.dbname).get('res.partner').address_get(cr, uid, [obj_cmpny.partner_id.id], ['invoice'])
91 if addr.get('invoice',False):
92 ads=pooler.get_pool(cr.dbname).get('res.partner.address').browse(cr,uid,[addr['invoice']])[0]
94 zip_city = pooler.get_pool(cr.dbname).get('res.partner.address').get_city(cr,uid,ads.id)
100 street += ads.street2
102 country = ads.country_id.code
104 sender_date = time.strftime('%Y-%m-%d')
105 comp_name = obj_cmpny.name
106 data_file = '<?xml version="1.0"?>\n<VatList xmlns="http://www.minfin.fgov.be/VatList" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.minfin.fgov.be/VatList VatList.xml" RecipientId="VAT-ADMIN" SenderId="'+ str(company_vat) + '"'
107 data_file +=' ControlRef="'+ cref + '" MandataireId="'+ data['form']['mand_id'] + '" SenderDate="'+ str(sender_date)+ '"'
108 if data['form']['test_xml']:
109 data_file += ' Test="0"'
110 data_file += ' VersionTech="1.2">'
111 data_file += '\n<AgentRepr DecNumber="1">\n\t<CompanyInfo>\n\t\t<VATNum>'+str(company_vat)+'</VATNum>\n\t\t<Name>'+ comp_name +'</Name>\n\t\t<Street>'+ street +'</Street>\n\t\t<CityAndZipCode>'+ zip_city +'</CityAndZipCode>'
112 data_file += '\n\t\t<Country>'+ country +'</Country>\n\t</CompanyInfo>\n</AgentRepr>'
113 data_comp = '\n<CompanyInfo>\n\t<VATNum>'+str(company_vat)+'</VATNum>\n\t<Name>'+ comp_name +'</Name>\n\t<Street>'+ street +'</Street>\n\t<CityAndZipCode>'+ zip_city +'</CityAndZipCode>\n\t<Country>'+ country +'</Country>\n</CompanyInfo>'
114 data_period = '\n<Period>'+ date_stop[:4] +'</Period>'
117 for p_id in p_id_list:
118 record = {} # this holds record per partner
119 obj_partner = pooler.get_pool(cr.dbname).get('res.partner').browse(cr,uid,p_id)
121 #This listing is only for customers located in belgium, that's the
122 #reason why we skip all the partners that haven't their
123 #(or one of their) default address(es) located in Belgium.
125 for ads in obj_partner.address:
126 if ads.type == 'default' and (ads.country_id and ads.country_id.code == 'BE'):
131 query = 'select b.code,sum(credit)-sum(debit) from account_move_line l left join account_account a on (l.account_id=a.id) left join account_account_type b on (a.user_type=b.id) where b.code in ('"'produit'"','"'tax'"') and l.partner_id='+str(p_id)+' and l.period_id in '+period+' group by b.code'
133 line_info = cr.fetchall()
137 record['vat'] = obj_partner.vat
139 #it seems that this listing is only for belgian customers
140 record['country'] = 'BE'
143 #~addr = pooler.get_pool(cr.dbname).get('res.partner').address_get(cr, uid, [obj_partner.id], ['invoice'])
145 #~ if addr.get('invoice',False):
146 #~ads=pooler.get_pool(cr.dbname).get('res.partner.address').browse(cr,uid,[addr['invoice']])[0]
148 #~ if ads.country_id:
149 #~ record.append(ads.country_id.code)
151 #~ error_message.append('Data Insufficient! : '+ 'The Partner "'+obj_partner.name + '"'' has no country associated with its Invoice address!')
155 #~ error_message.append('Data Insufficient! : '+ 'The Partner "'+obj_partner.name + '"'' has no Invoice address!')
158 record['turnover'] = 0
160 for item in line_info:
161 if item[0]=='produit':
162 record['turnover'] += item[1]
164 record['amount'] += item[1]
171 if len(error_message):
172 data['form']['msg']='Exception : \n' +'-'*50+'\n'+ '\n'.join(error_message)
175 if line['turnover'] < data['form']['limit_amount']:
178 sum_tax +=line['amount']
179 sum_turnover +=line['turnover']
180 data_clientinfo +='\n<ClientList SequenceNum="'+str(seq)+'">\n\t<CompanyInfo>\n\t\t<VATNum>'+line['vat'] +'</VATNum>\n\t\t<Country>'+tools.ustr(line['country']) +'</Country>\n\t</CompanyInfo>\n\t<Amount>'+str(int(line['amount'] * 100)) +'</Amount>\n\t<TurnOver>'+str(int(line['turnover'] * 100)) +'</TurnOver>\n</ClientList>'
182 data_decl ='\n<DeclarantList SequenceNum="1" DeclarantNum="'+ dnum + '" ClientNbr="'+ str(seq) +'" TurnOverSum="'+ str(int(sum_turnover * 100)) +'" TaxSum="'+ str(int(sum_tax * 100)) +'" />'
183 data_file += data_decl + data_comp + str(data_period) + data_clientinfo + '\n</VatList>'
185 data['form']['msg'] = 'Save the File with '".xml"' extension.'
186 data['form']['file_save'] = base64.encodestring(data_file.encode('utf8'))
192 'result': {'type':'form', 'arch':form, 'fields':fields, 'state':[('end','Cancel'),('go','Create XML')]},
195 'actions': [_create_xml],
196 'result': {'type':'form', 'arch':msg_form, 'fields':msg_fields, 'state':[('end','Ok')]},
201 wizard_vat('list.vat.detail')
202 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: