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
#
2011-05-05 13:27:00 +00:00
# Corrections & modifications by Noviat nv/sa, (http://www.noviat.be):
# - VAT listing based upon year in stead of fiscal year
# - sql query adapted to select only 'tax-out' move lines
# - extra button to print readable PDF report
#
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-03-06 22:18:24 +00:00
from tools . translate import _
2009-10-27 12:24:45 +00:00
from osv import fields , osv
2012-01-20 09:47:53 +00:00
class vat_listing_clients ( osv . osv_memory ) :
_name = ' vat.listing.clients '
_columns = {
2012-01-31 05:54:13 +00:00
' name ' : fields . char ( ' Client Name ' , size = 32 ) ,
2012-01-20 09:47:53 +00:00
' vat ' : fields . char ( ' VAT ' , size = 64 ) ,
2012-01-31 05:54:13 +00:00
' country ' : fields . char ( ' Country ' , size = 16 ) ,
2012-01-20 09:47:53 +00:00
' amount ' : fields . float ( ' Amount ' ) ,
' turnover ' : fields . float ( ' Turnover ' ) ,
}
def name_get ( self , cr , uid , ids , context = None ) :
if not len ( ids ) :
return [ ]
return [ ( r [ ' id ' ] , r [ ' name ' ] or ' ' + ' - ' + r [ ' vat ' ] or ' ' ) \
for r in self . read ( cr , uid , ids , [ ' name ' , ' vat ' ] ,
context , load = ' _classic_write ' ) ]
def name_search ( self , cr , uid , name , args = None , operator = ' ilike ' , context = None , limit = 100 ) :
2012-01-30 13:24:53 +00:00
args . append ( [ ' id ' , ' in ' , context [ ' partner_ids ' ] ] )
2012-01-20 09:47:53 +00:00
client = self . search ( cr , uid , [ ( ' vat ' , ' = ' , name ) ] + args , limit = limit , context = context )
if not client :
client = self . search ( cr , uid , [ ( ' name ' , ' ilike ' , ' %% %s %% ' % name ) ] + args , limit = limit , context = context )
return self . name_get ( cr , uid , client , context = context )
vat_listing_clients ( )
2011-05-05 13:27:00 +00:00
class partner_vat_13 ( osv . osv_memory ) :
2010-04-01 13:38:12 +00:00
""" Vat Listing """
2011-05-05 13:27:00 +00:00
_name = " partner.vat_13 "
2010-04-01 13:38:12 +00:00
2010-11-19 13:48:01 +00:00
def get_partner ( self , cursor , user , ids , context = None ) :
2010-04-01 13:38:12 +00:00
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 ]
2011-05-05 13:27:00 +00:00
#period = obj_period.search(cursor, user, [('fiscalyear_id', '=', data['fyear'])], context=context)
year = data [ ' year ' ]
date_start = year + ' -01-01 '
date_stop = year + ' -12-31 '
period = obj_period . search ( cursor , user , [ ( ' date_start ' , ' >= ' , date_start ) , ( ' date_stop ' , ' <= ' , date_stop ) ] )
if not period :
raise osv . except_osv ( _ ( ' Data Insufficient! ' ) , _ ( ' No data for the selected Year. ' ) )
#logger.notifyChannel('addons.'+self._name, netsvc.LOG_WARNING, 'period = %s' %period )
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 :
2011-04-07 10:09:25 +00:00
if ads . type == ' default ' and ( ads . country_id and ads . country_id . code == ' BE ' ) and ( obj_partner . vat or ' ' ) . startswith ( ' BE ' ) :
2009-02-05 13:18:41 +00:00
go_ahead = True
break
if not go_ahead :
continue
2011-05-06 13:05:19 +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 IN %s group by b.code ' , ( ( ' income ' , ' produit ' , ' tax_out ' ) , obj_partner . id , tuple ( period ) , ) )
2010-04-01 13:38:12 +00:00
line_info = cursor . fetchall ( )
2008-07-23 10:29:42 +00:00
if not line_info :
continue
2011-05-05 13:27:00 +00:00
record [ ' vat ' ] = obj_partner . vat . replace ( ' ' , ' ' ) . upper ( )
2009-02-05 13:18:41 +00:00
#it seems that this listing is only for belgian customers
record [ ' country ' ] = ' BE '
2011-05-05 13:27:00 +00:00
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 :
2011-05-05 14:57:41 +00:00
if item [ 0 ] in ( ' income ' , ' produit ' ) :
2011-05-05 13:27:00 +00:00
record [ ' turnover ' ] + = item [ 1 ]
2008-07-23 10:29:42 +00:00
else :
2011-05-05 13:27:00 +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 )
2011-05-05 13:27:00 +00:00
context . update ( { ' partner_ids ' : partners , ' year ' : data [ ' year ' ] , ' limit_amount ' : data [ ' limit_amount ' ] } )
model_data_ids = obj_model_data . search ( cursor , user , [ ( ' model ' , ' = ' , ' ir.ui.view ' ) , ( ' name ' , ' = ' , ' view_vat_listing_13 ' ) ] )
2010-04-01 13:38:12 +00:00
resource_id = obj_model_data . read ( cursor , user , model_data_ids , fields = [ ' res_id ' ] ) [ 0 ] [ ' res_id ' ]
return {
' name ' : ' Vat Listing ' ,
' view_type ' : ' form ' ,
' view_mode ' : ' form ' ,
2011-05-05 13:27:00 +00:00
' res_model ' : ' partner.vat.list_13 ' ,
2010-04-01 13:38:12 +00:00
' views ' : [ ( resource_id , ' form ' ) ] ,
' context ' : context ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
}
_columns = {
2011-05-05 13:27:00 +00:00
' year ' : fields . char ( ' Year ' , size = 4 , required = True ) ,
2010-04-01 13:38:12 +00:00
' limit_amount ' : fields . integer ( ' Limit Amount ' , required = True ) ,
}
2011-05-05 13:27:00 +00:00
_defaults = {
' year ' : lambda * a : str ( int ( time . strftime ( ' % Y ' ) ) - 1 ) ,
' limit_amount ' : 250 ,
}
partner_vat_13 ( )
2009-10-27 12:24:45 +00:00
2011-05-05 13:27:00 +00:00
class partner_vat_list_13 ( osv . osv_memory ) :
2009-10-27 12:24:45 +00:00
2010-04-01 13:38:12 +00:00
""" Partner Vat Listing """
2011-05-05 13:27:00 +00:00
_name = " partner.vat.list_13 "
2010-04-01 13:38:12 +00:00
_columns = {
2011-07-19 13:34:19 +00:00
# TODO the referenced model has been deleted at revno 4672.1.2.
2012-01-31 05:47:13 +00:00
' partner_ids ' : fields . many2many ( ' vat.listing.clients ' , ' vat_partner_rel ' , ' vat_id ' , ' partner_id ' , ' Clients ' , help = ' You can remove clients/partners which you do not want to show in xml file ' ) ,
2010-04-01 13:38:12 +00:00
' name ' : fields . char ( ' File Name ' , size = 32 ) ,
' msg ' : fields . text ( ' File created ' , size = 64 , readonly = True ) ,
' file_save ' : fields . binary ( ' Save File ' , readonly = True ) ,
2012-01-27 05:58:00 +00:00
' identification_type ' : fields . selection ( [ ( ' tin ' , ' TIN ' ) , ( ' nvat ' , ' NVAT ' ) , ( ' other ' , ' Other ' ) ] , ' Identification Type ' , required = True ) ,
' other ' : fields . char ( ' Other Qlf ' , size = 16 , help = " Description of a Identification Type " ) ,
' comments ' : fields . text ( ' Comments ' ) ,
2010-04-01 13:38:12 +00:00
}
2010-11-19 13:48:01 +00:00
def _get_partners ( self , cursor , user , context = None ) :
2010-04-01 13:38:12 +00:00
return context . get ( ' partner_ids ' , [ ] )
2010-04-02 11:44:59 +00:00
_defaults = {
2011-07-19 13:34:19 +00:00
# TODO the referenced model has been deleted at revno 4672.1.2.
2012-01-30 07:10:59 +00:00
' partner_ids ' : _get_partners ,
2012-01-27 05:58:00 +00:00
' identification_type ' : ' tin '
2010-04-02 11:44:59 +00:00
}
2010-11-19 13:48:01 +00:00
def create_xml ( self , cursor , user , ids , context = None ) :
2010-08-23 19:49:16 +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 ' )
2012-01-27 05:58:00 +00:00
2010-04-01 13:38:12 +00:00
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
2011-05-05 13:27:00 +00:00
2009-10-27 12:24:45 +00:00
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
2011-05-05 13:27:00 +00:00
company_vat = company_vat . replace ( ' ' , ' ' ) . upper ( )
SenderId = company_vat [ 2 : ]
2012-01-27 05:58:00 +00:00
issued_by = company_vat [ : 2 ]
2011-05-05 13:27:00 +00:00
cref = SenderId + seq_controlref
dnum = cref + seq_declarantnum
#obj_year= obj_fyear.browse(cursor, user, context['fyear'], context=context)
2012-01-20 09:47:53 +00:00
street = 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 ]
2012-01-20 09:47:53 +00:00
phone = ads . phone or ' '
email = ads . email or ' '
name = ads . name or ' '
city = obj_addr . get_city ( cursor , user , ads . id )
2012-01-27 05:58:00 +00:00
zip = obj_addr . browse ( cursor , user , ads . id , context = context ) . zip or ' '
2012-01-20 09:47:53 +00:00
if not city :
city = ' '
2009-10-27 12:24:45 +00:00
if ads . street :
2012-01-20 09:47:53 +00:00
street = ads . street + ' '
2009-10-27 12:24:45 +00:00
if ads . street2 :
street + = ads . street2
if ads . country_id :
country = ads . country_id . code
2012-01-27 05:58:00 +00:00
data = self . read ( cursor , user , ids ) [ 0 ]
other = data [ ' other ' ] or ' '
2009-10-27 12:24:45 +00:00
sender_date = time . strftime ( ' % Y- % m- %d ' )
2011-05-05 13:27:00 +00:00
comp_name = obj_cmpny . name
2012-01-27 12:31:07 +00:00
annual_listing_data = {
' identificationType ' : data [ ' identification_type ' ] . upper ( ) ,
' issued_by ' : issued_by ,
' other ' : other ,
' company_vat ' : company_vat ,
' comp_name ' : comp_name ,
' street ' : street ,
' zip ' : zip ,
' city ' : city ,
' country ' : country ,
' email ' : email ,
' phone ' : phone ,
' SenderId ' : SenderId ,
' period ' : context [ ' year ' ] ,
2012-01-30 07:10:59 +00:00
' comments ' : data [ ' comments ' ] or ' '
2012-01-27 12:31:07 +00:00
}
data_file = """ <?xml version= " 1.0 " ?>
< ClientListingConsignment xmlns = " http://www.minfin.fgov.be/ClientListingConsignment " ClientListingsNbr = " 1 " >
< Representative >
< RepresentativeID identificationType = " %(identificationType)s " issuedBy = " %(issued_by)s " otherQlf = " %(other)s " > % ( company_vat ) s < / RepresentativeID >
< Name > % ( comp_name ) s < / Name >
< Street > % ( street ) s < / Street >
< PostCode > % ( zip ) s < / PostCode >
< City > % ( city ) s < / City >
< CountryCode > % ( country ) s < / CountryCode >
< EmailAddress > % ( email ) s < / EmailAddress >
< Phone > % ( phone ) s < / Phone >
< / Representative >
< RepresentativeReference > < / RepresentativeReference >
""" % (annual_listing_data)
data_comp = """
< ReplacedClientListing > < / ReplacedClientListing >
< Declarant >
< VATNumber xmlns = " http://www.minfin.fgov.be/InputCommon " > % ( SenderId ) s < / VATNumber >
< Name > % ( comp_name ) s < / Name >
< Street > % ( street ) s < / Street >
< PostCode > % ( zip ) s < / PostCode >
< City > % ( city ) s < / City >
< CountryCode > % ( country ) s < / CountryCode >
< EmailAddress > % ( email ) s < / EmailAddress >
< Phone > % ( phone ) s < / Phone >
< / Declarant >
< Period > % ( period ) s < / Period >
""" % (annual_listing_data)
2009-10-27 12:24:45 +00:00
error_message = [ ]
2012-01-27 05:58:00 +00:00
2010-04-01 13:38:12 +00:00
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 = ' '
2011-05-05 13:27:00 +00:00
sum_tax = 0.00
sum_turnover = 0.00
2009-01-19 06:02:04 +00:00
if len ( error_message ) :
2010-05-21 10:41:26 +00:00
return ' Exception : \n ' + ' - ' * 50 + ' \n ' + ' \n ' . join ( error_message )
2012-01-30 07:10:59 +00:00
amount_data = {
' seq ' : str ( seq ) ,
' dnum ' : dnum ,
' sum_tax ' : str ( 0 ) ,
' sum_turnover ' : str ( 0 ) ,
}
2008-07-23 10:29:42 +00:00
for line in datas :
2012-01-20 09:47:53 +00:00
vat_issued = line [ ' vat ' ] [ : 2 ]
if vat_issued == ' BE ' :
vat_issued = ' '
else :
vat_issued = vat_issued
2009-10-27 12:24:45 +00:00
if not line :
continue
2011-05-05 13:27:00 +00:00
if line [ ' turnover ' ] < context [ ' limit_amount ' ] :
2008-07-23 10:29:42 +00:00
continue
2010-08-23 19:49:16 +00:00
seq + = 1
2011-05-05 13:27:00 +00:00
sum_tax + = line [ ' amount ' ]
sum_turnover + = line [ ' turnover ' ]
2012-01-27 12:31:07 +00:00
2012-01-30 09:56:31 +00:00
amount_data . update ( {
2012-01-27 12:31:07 +00:00
' seq ' : str ( seq ) ,
' vat_issued ' : vat_issued ,
' only_vat ' : line [ ' vat ' ] . replace ( ' ' , ' ' ) . upper ( ) [ 2 : ] ,
' turnover ' : str ( int ( round ( line [ ' turnover ' ] * 100 ) ) ) ,
' vat_amount ' : str ( int ( round ( line [ ' amount ' ] * 100 ) ) ) ,
' sum_tax ' : str ( int ( round ( sum_tax * 100 ) ) ) ,
' sum_turnover ' : str ( int ( round ( sum_turnover * 100 ) ) ) ,
2012-01-30 09:56:31 +00:00
} )
2012-01-30 05:42:54 +00:00
# Turnover and Farmer tags are not included
2012-01-27 12:31:07 +00:00
data_clientinfo + = """
< Client SequenceNumber = " %(seq)s " >
< CompanyVATNumber issuedby = " %(vat_issued)s " > % ( only_vat ) s < / CompanyVATNumber >
< TurnOver > % ( turnover ) s < / TurnOver >
< VATAmount > % ( vat_amount ) s < / VATAmount >
< / Client > """ % (amount_data)
data_decl = ' <ClientListing SequenceNumber= " 1 " ClientsNbr= " %(seq)s " DeclarantReference= " %(dnum)s " TurnOverSum= " %(sum_turnover)s " VATAmountSum= " %(sum_tax)s " > ' % ( amount_data )
data_file + = data_decl + data_comp + data_clientinfo + ' \n \t \t <FileAttachment></FileAttachment> \n \t \t <Comment> %(comments)s </Comment> \n \t </ClientListing> \n </ClientListingConsignment> ' % ( annual_listing_data )
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
2011-05-05 13:27:00 +00:00
2012-01-31 05:37:36 +00:00
# Not fully implemented
# def print_vatlist(self, cursor, user, ids, context=None):
# if context is None:
# context = {}
# obj_vat_lclient = self.pool.get('vat.listing.clients')
# client_datas = []
# data = self.read(cursor, user, ids)[0]
# for partner in data['partner_ids']:
# if isinstance(partner, list) and partner:
# client_datas.append(partner[2])
# else:
# client_data = obj_vat_lclient.read(cursor, user, partner, context=context)
# client_datas.append(client_data)
#
# datas = {'ids': []}
# datas['model'] = 'res.company'
# datas['year'] = context['year']
# datas['limit_amount'] = context['limit_amount']
# datas['client_datas'] = client_datas
# return {
# 'type': 'ir.actions.report.xml',
# 'report_name': 'partner.vat.listing.print',
# 'datas': datas,
# }
2008-07-23 10:29:42 +00:00
2011-05-05 13:27:00 +00:00
partner_vat_list_13 ( )
2009-10-27 12:24:45 +00:00
2010-04-11 19:31:52 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: