2010-04-01 13:38:12 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2008-11-03 19:18:56 +00:00
##############################################################################
2009-10-27 12:24:45 +00:00
#
2009-10-14 11:15:34 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2008-11-03 19:18:56 +00:00
#
# This program is free software: you can redistribute it and/or modify
2009-10-14 11:15:34 +00:00
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
2008-11-03 19:18:56 +00:00
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2009-10-14 11:15:34 +00:00
# GNU Affero General Public License for more details.
2008-11-03 19:18:56 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2009-10-27 12:24:45 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-11-03 19:18:56 +00:00
#
##############################################################################
2008-07-23 10:29:42 +00:00
import time
import base64
2009-10-27 12:24:45 +00:00
2009-03-06 22:18:24 +00:00
from tools . translate import _
2009-04-27 12:41:16 +00:00
import tools
2009-10-27 12:24:45 +00:00
from osv import fields , osv
class vat_listing_clients ( osv . osv_memory ) :
_name = ' vat.listing.clients '
_columns = {
2010-04-01 13:38:12 +00:00
' name ' : fields . char ( ' Client Name ' , size = 64 ) ,
2009-10-27 12:24:45 +00:00
' vat ' : fields . char ( ' VAT ' , size = 64 ) ,
' country ' : fields . char ( ' Country ' , size = 64 ) ,
' amount ' : fields . float ( ' Amount ' ) ,
' turnover ' : fields . float ( ' Turnover ' ) ,
2010-04-02 11:44:59 +00:00
}
2009-10-27 12:24:45 +00:00
vat_listing_clients ( )
2008-07-23 10:29:42 +00:00
2010-04-01 13:38:12 +00:00
class partner_vat ( osv . osv_memory ) :
""" Vat Listing """
_name = " partner.vat "
def get_partner ( self , cursor , user , ids , context = { } ) :
obj_period = self . pool . get ( ' account.period ' )
obj_partner = self . pool . get ( ' res.partner ' )
obj_vat_lclient = self . pool . get ( ' vat.listing.clients ' )
obj_model_data = self . pool . get ( ' ir.model.data ' )
data = self . read ( cursor , user , ids ) [ 0 ]
period = obj_period . search ( cursor , user , [ ( ' fiscalyear_id ' , ' = ' , data [ ' fyear ' ] ) ] , context = context )
2010-04-02 11:44:59 +00:00
p_id_list = obj_partner . search ( cursor , user , [ ( ' vat_subjected ' , ' != ' , False ) ] , context = context )
2008-07-23 10:29:42 +00:00
if not p_id_list :
2010-04-02 11:44:59 +00:00
raise osv . except_osv ( _ ( ' Data Insufficient! ' ) , _ ( ' No partner has a VAT Number asociated with him. ' ) )
2009-10-27 12:24:45 +00:00
partners = [ ]
records = [ ]
2010-04-01 13:38:12 +00:00
for obj_partner in obj_partner . browse ( cursor , user , p_id_list , context = context ) :
2009-02-05 13:18:41 +00:00
record = { } # this holds record per partner
2009-10-27 12:24:45 +00:00
#This listing is only for customers located in belgium, that's the
#reason why we skip all the partners that haven't their
2009-02-05 13:18:41 +00:00
#(or one of their) default address(es) located in Belgium.
go_ahead = False
for ads in obj_partner . address :
if ads . type == ' default ' and ( ads . country_id and ads . country_id . code == ' BE ' ) :
go_ahead = True
break
if not go_ahead :
continue
2010-04-01 13:38:12 +00:00
cursor . execute ( ' 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 %s and l.partner_id= %s and l.period_id=ANY( %s ) group by b.code ' , ( ( ' produit ' , ' tax ' ) , obj_partner . id , period , ) )
line_info = cursor . fetchall ( )
2008-07-23 10:29:42 +00:00
if not line_info :
continue
2009-02-05 13:18:41 +00:00
record [ ' vat ' ] = obj_partner . vat
#it seems that this listing is only for belgian customers
record [ ' country ' ] = ' BE '
#...deprecated...
2009-10-27 12:24:45 +00:00
#~addr = pool.get('res.partner').address_get(cr, uid, [obj_partner.id], ['invoice'])
2009-02-05 13:18:41 +00:00
#~ if addr.get('invoice',False):
2010-04-01 13:38:12 +00:00
#~ads=pool.get('res.partner.address').browse(cr, uid, [addr['invoice']])[0]
2009-10-27 12:24:45 +00:00
2009-02-05 13:18:41 +00:00
#~ if ads.country_id:
#~ record.append(ads.country_id.code)
#~ else:
#~ error_message.append('Data Insufficient! : '+ 'The Partner "'+obj_partner.name + '"'' has no country associated with its Invoice address!')
2009-10-27 12:24:45 +00:00
2009-02-05 13:18:41 +00:00
#~ if len(record)<2:
#~ record.append('')
#~ error_message.append('Data Insufficient! : '+ 'The Partner "'+obj_partner.name + '"'' has no Invoice address!')
record [ ' amount ' ] = 0
record [ ' turnover ' ] = 0
2009-10-27 12:24:45 +00:00
record [ ' name ' ] = obj_partner . name
2009-02-05 13:18:41 +00:00
for item in line_info :
2010-04-02 11:44:59 +00:00
if item [ 0 ] == ' produit ' :
2009-02-05 13:18:41 +00:00
record [ ' turnover ' ] + = item [ 1 ]
2008-07-23 10:29:42 +00:00
else :
2009-02-05 13:18:41 +00:00
record [ ' amount ' ] + = item [ 1 ]
2010-04-01 13:38:12 +00:00
id_client = obj_vat_lclient . create ( cursor , user , record , context = context )
2009-10-27 12:24:45 +00:00
partners . append ( id_client )
records . append ( record )
2010-04-01 13:38:12 +00:00
context . update ( { ' partner_ids ' : partners , ' fyear ' : data [ ' fyear ' ] , ' limit_amount ' : data [ ' limit_amount ' ] , ' mand_id ' : data [ ' mand_id ' ] } )
model_data_ids = obj_model_data . search ( cursor , user , [ ( ' model ' , ' = ' , ' ir.ui.view ' ) , ( ' name ' , ' = ' , ' view_vat_listing ' ) ] )
resource_id = obj_model_data . read ( cursor , user , model_data_ids , fields = [ ' res_id ' ] ) [ 0 ] [ ' res_id ' ]
return {
# 'domain': "[('id','in', ["+','.join(map(str,data['form']['invoice_ids']))+"])]",
' name ' : ' Vat Listing ' ,
' view_type ' : ' form ' ,
' view_mode ' : ' form ' ,
' res_model ' : ' partner.vat.list ' ,
' views ' : [ ( resource_id , ' form ' ) ] ,
' context ' : context ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
}
_columns = {
' fyear ' : fields . many2one ( ' account.fiscalyear ' , ' Fiscal Year ' , required = True ) ,
' mand_id ' : fields . char ( ' MandataireId ' , size = 14 , required = True , help = " This identifies the representative of the sending company. This is a string of 14 characters " ) ,
' limit_amount ' : fields . integer ( ' Limit Amount ' , required = True ) ,
' test_xml ' : fields . boolean ( ' Test XML file ' , help = " Sets the XML output as test file " ) ,
}
partner_vat ( )
2009-10-27 12:24:45 +00:00
2010-04-01 13:38:12 +00:00
class partner_vat_list ( osv . osv_memory ) :
2009-10-27 12:24:45 +00:00
2010-04-01 13:38:12 +00:00
""" Partner Vat Listing """
_name = " partner.vat.list "
_columns = {
' partner_ids ' : fields . many2many ( ' vat.listing.clients ' , ' vat_partner_rel ' , ' vat_id ' , ' partner_id ' , ' Clients ' , required = False , help = ' You can remove clients/partners which you do not want to show in xml file ' ) ,
' name ' : fields . char ( ' File Name ' , size = 32 ) ,
' msg ' : fields . text ( ' File created ' , size = 64 , readonly = True ) ,
' file_save ' : fields . binary ( ' Save File ' , readonly = True ) ,
}
2010-04-02 11:44:59 +00:00
def _get_partners ( self , cursor , user , context = { } ) :
2010-04-01 13:38:12 +00:00
return context . get ( ' partner_ids ' , [ ] )
2010-04-02 11:44:59 +00:00
_defaults = {
' partner_ids ' : _get_partners
}
2010-04-01 13:38:12 +00:00
def create_xml ( self , cursor , user , ids , context = { } ) :
2009-10-27 12:24:45 +00:00
datas = [ ]
2010-04-01 13:38:12 +00:00
obj_sequence = self . pool . get ( ' ir.sequence ' )
obj_users = self . pool . get ( ' res.users ' )
obj_partner = self . pool . get ( ' res.partner ' )
obj_fyear = self . pool . get ( ' account.fiscalyear ' )
obj_addr = self . pool . get ( ' res.partner.address ' )
obj_vat_lclient = self . pool . get ( ' vat.listing.clients ' )
obj_model_data = self . pool . get ( ' ir.model.data ' )
seq_controlref = obj_sequence . get ( cursor , user , ' controlref ' )
seq_declarantnum = obj_sequence . get ( cursor , user , ' declarantnum ' )
obj_cmpny = obj_users . browse ( cursor , user , user , context = context ) . company_id
2009-10-27 12:24:45 +00:00
company_vat = obj_cmpny . partner_id . vat
if not company_vat :
2010-04-01 13:38:12 +00:00
raise osv . except_osv ( _ ( ' Data Insufficient ' ) , _ ( ' No VAT Number Associated with Main Company! ' ) )
2009-10-27 12:24:45 +00:00
cref = company_vat + seq_controlref
dnum = cref + seq_declarantnum
2010-04-01 13:38:12 +00:00
obj_year = obj_fyear . browse ( cursor , user , context [ ' fyear ' ] , context = context )
2009-10-27 12:24:45 +00:00
street = zip_city = country = ' '
2010-04-01 13:38:12 +00:00
addr = obj_partner . address_get ( cursor , user , [ obj_cmpny . partner_id . id ] , [ ' invoice ' ] )
2009-10-27 12:24:45 +00:00
if addr . get ( ' invoice ' , False ) :
2010-04-01 13:38:12 +00:00
ads = obj_addr . browse ( cursor , user , [ addr [ ' invoice ' ] ] , context = context ) [ 0 ]
2009-10-27 12:24:45 +00:00
2010-04-01 13:38:12 +00:00
zip_city = obj_addr . get_city ( cursor , user , ads . id )
2009-10-27 12:24:45 +00:00
if not zip_city :
zip_city = ' '
if ads . street :
street = ads . street
if ads . street2 :
street + = ads . street2
if ads . country_id :
country = ads . country_id . code
sender_date = time . strftime ( ' % Y- % m- %d ' )
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 ) + ' " '
2010-04-01 13:38:12 +00:00
data_file + = ' ControlRef= " ' + cref + ' " MandataireId= " ' + tools . ustr ( context [ ' mand_id ' ] ) + ' " SenderDate= " ' + str ( sender_date ) + ' " '
if [ ' test_xml ' ] :
2009-10-27 12:24:45 +00:00
data_file + = ' Test= " 0 " '
data_file + = ' VersionTech= " 1.2 " > '
data_file + = ' \n <AgentRepr DecNumber= " 1 " > \n \t <CompanyInfo> \n \t \t <VATNum> ' + str ( company_vat ) + ' </VATNum> \n \t \t <Name> ' + tools . ustr ( obj_cmpny . name ) + ' </Name> \n \t \t <Street> ' + tools . ustr ( street ) + ' </Street> \n \t \t <CityAndZipCode> ' + tools . ustr ( zip_city ) + ' </CityAndZipCode> '
data_file + = ' \n \t \t <Country> ' + tools . ustr ( country ) + ' </Country> \n \t </CompanyInfo> \n </AgentRepr> '
data_comp = ' \n <CompanyInfo> \n \t <VATNum> ' + str ( company_vat ) + ' </VATNum> \n \t <Name> ' + tools . ustr ( obj_cmpny . name ) + ' </Name> \n \t <Street> ' + tools . ustr ( street ) + ' </Street> \n \t <CityAndZipCode> ' + tools . ustr ( zip_city ) + ' </CityAndZipCode> \n \t <Country> ' + tools . ustr ( country ) + ' </Country> \n </CompanyInfo> '
data_period = ' \n <Period> ' + tools . ustr ( obj_year . date_stop [ : 4 ] ) + ' </Period> '
error_message = [ ]
2010-04-01 13:38:12 +00:00
data = self . read ( cursor , user , ids ) [ 0 ]
for partner in data [ ' partner_ids ' ] :
2009-10-27 12:24:45 +00:00
if isinstance ( partner , list ) and partner :
datas . append ( partner [ 2 ] )
else :
2010-04-01 13:38:12 +00:00
client_data = obj_vat_lclient . read ( cursor , user , partner , context = context )
2009-10-27 12:24:45 +00:00
datas . append ( client_data )
2010-04-02 11:44:59 +00:00
seq = 0
data_clientinfo = ' '
sum_tax = 0.00
sum_turnover = 0.00
2009-01-19 06:02:04 +00:00
if len ( error_message ) :
2010-04-01 13:38:12 +00:00
msg = ' Exception : \n ' + ' - ' * 50 + ' \n ' + ' \n ' . join ( error_message )
return msg
2008-07-23 10:29:42 +00:00
for line in datas :
2009-10-27 12:24:45 +00:00
if not line :
continue
2010-04-01 13:38:12 +00:00
if line [ ' turnover ' ] < context [ ' limit_amount ' ] :
2008-07-23 10:29:42 +00:00
continue
seq + = 1
2009-02-05 13:18:41 +00:00
sum_tax + = line [ ' amount ' ]
sum_turnover + = line [ ' turnover ' ]
2009-04-27 12:41:16 +00:00
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> '
2008-07-23 10:29:42 +00:00
data_decl = ' \n <DeclarantList SequenceNum= " 1 " DeclarantNum= " ' + dnum + ' " ClientNbr= " ' + str ( seq ) + ' " TurnOverSum= " ' + str ( int ( sum_turnover * 100 ) ) + ' " TaxSum= " ' + str ( int ( sum_tax * 100 ) ) + ' " /> '
2009-04-27 12:41:16 +00:00
data_file + = tools . ustr ( data_decl ) + tools . ustr ( data_comp ) + tools . ustr ( data_period ) + tools . ustr ( data_clientinfo ) + ' \n </VatList> '
2010-04-01 13:38:12 +00:00
msg = ' Save the File with ' " .xml " ' extension. '
file_save = base64 . encodestring ( data_file . encode ( ' utf8 ' ) )
self . write ( cursor , user , ids , { ' file_save ' : file_save , ' msg ' : msg , ' name ' : ' vat_list.xml ' } , context = context )
return True
2008-07-23 10:29:42 +00:00
2010-04-01 13:38:12 +00:00
partner_vat_list ( )
2009-10-27 12:24:45 +00:00
2010-04-01 13:38:12 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: