2009-10-14 11:15:34 +00:00
# -*- coding: utf-8 -*-
2009-03-24 11:01:43 +00:00
##############################################################################
2010-04-01 13:38:12 +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>).
2009-03-24 11:01:43 +00:00
#
2012-08-07 10:23:26 +00:00
# Adapted by Noviat to
2012-02-13 17:22:08 +00:00
# - make the 'mand_id' field optional
# - support Noviat tax code scheme
#
2009-10-14 11:15:34 +00:00
# This program is free software: you can redistribute it and/or modify
# 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.
2009-03-24 11:01:43 +00:00
#
2009-10-14 11:15:34 +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
# GNU Affero General Public License for more details.
2009-03-24 11:01:43 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-04-01 13:38:12 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2009-03-24 11:01:43 +00:00
#
##############################################################################
import time
import base64
2012-12-06 14:56:32 +00:00
from openerp . osv import fields , osv
from openerp . tools . translate import _
from openerp . report import report_sxw
2011-12-06 11:37:11 +00:00
2014-04-22 12:11:05 +00:00
2010-04-01 13:38:12 +00:00
class partner_vat_intra ( osv . osv_memory ) :
2010-05-04 11:49:34 +00:00
"""
Partner Vat Intra
"""
2010-04-01 13:38:12 +00:00
_name = " partner.vat.intra "
2010-04-02 04:32:42 +00:00
_description = ' Partner VAT Intra '
2010-04-01 13:38:12 +00:00
2010-06-11 07:04:25 +00:00
def _get_xml_data ( self , cr , uid , context = None ) :
2010-06-10 13:03:02 +00:00
if context . get ( ' file_save ' , False ) :
2010-08-23 19:49:16 +00:00
return base64 . encodestring ( context [ ' file_save ' ] . encode ( ' utf8 ' ) )
2010-06-11 07:04:25 +00:00
return ' '
2010-04-01 13:38:12 +00:00
2010-05-04 11:54:31 +00:00
def _get_europe_country ( self , cursor , user , context = None ) :
return self . pool . get ( ' res.country ' ) . search ( cursor , user , [ ( ' code ' , ' in ' , [ ' AT ' , ' BG ' , ' CY ' , ' CZ ' , ' DK ' , ' EE ' , ' FI ' , ' FR ' , ' DE ' , ' GR ' , ' HU ' , ' IE ' , ' IT ' , ' LV ' , ' LT ' , ' LU ' , ' MT ' , ' NL ' , ' PL ' , ' PT ' , ' RO ' , ' SK ' , ' SI ' , ' ES ' , ' SE ' , ' GB ' ] ) ] )
2010-04-01 13:38:12 +00:00
_columns = {
2010-05-04 11:49:34 +00:00
' name ' : fields . char ( ' File Name ' , size = 32 ) ,
2010-04-11 19:31:52 +00:00
' period_code ' : fields . char ( ' Period Code ' , size = 6 , required = True , help = ''' This is where you have to set the period code for the intracom declaration using the format: ppyyyy
PP can stand for a month : from ' 01 ' to ' 12 ' .
PP can stand for a trimester : ' 31 ' , ' 32 ' , ' 33 ' , ' 34 '
The first figure means that it is a trimester ,
The second figure identify the trimester .
PP can stand for a complete fiscal year : ' 00 ' .
YYYY stands for the year ( 4 positions ) .
'''
) ,
' period_ids ' : fields . many2many ( ' account.period ' , ' account_period_rel ' , ' acc_id ' , ' period_id ' , ' Period (s) ' , help = ' Select here the period(s) you want to include in your intracom declaration ' ) ,
2012-02-08 13:52:20 +00:00
' tax_code_id ' : fields . many2one ( ' account.tax.code ' , ' Company ' , domain = [ ( ' parent_id ' , ' = ' , False ) ] , help = " Keep empty to use the user ' s company " , required = True ) ,
2010-04-01 13:38:12 +00:00
' test_xml ' : fields . boolean ( ' Test XML file ' , help = " Sets the XML output as test file " ) ,
2012-02-13 17:22:08 +00:00
' mand_id ' : fields . char ( ' Reference ' , size = 14 , help = " Reference given by the Representative of the sending company. " ) ,
2010-05-04 11:49:34 +00:00
' msg ' : fields . text ( ' File created ' , size = 14 , readonly = True ) ,
2012-07-13 10:17:51 +00:00
' no_vat ' : fields . text ( ' Partner With No VAT ' , size = 14 , readonly = True , help = " The Partner whose VAT number is not defined and they are not included in XML File. " ) ,
2010-04-01 13:38:12 +00:00
' file_save ' : fields . binary ( ' Save File ' , readonly = True ) ,
' country_ids ' : fields . many2many ( ' res.country ' , ' vat_country_rel ' , ' vat_id ' , ' country_id ' , ' European Countries ' ) ,
2012-01-24 09:54:21 +00:00
' comments ' : fields . text ( ' Comments ' ) ,
2010-04-02 04:32:42 +00:00
}
2010-04-01 13:38:12 +00:00
2012-02-08 13:52:20 +00:00
def _get_tax_code ( self , cr , uid , context = None ) :
obj_tax_code = self . pool . get ( ' account.tax.code ' )
obj_user = self . pool . get ( ' res.users ' )
company_id = obj_user . browse ( cr , uid , uid , context = context ) . company_id . id
tax_code_ids = obj_tax_code . search ( cr , uid , [ ( ' company_id ' , ' = ' , company_id ) , ( ' parent_id ' , ' = ' , False ) ] , context = context )
return tax_code_ids and tax_code_ids [ 0 ] or False
2010-04-01 13:38:12 +00:00
_defaults = {
' country_ids ' : _get_europe_country ,
2010-06-10 13:03:02 +00:00
' file_save ' : _get_xml_data ,
2012-02-08 13:52:20 +00:00
' name ' : ' vat_intra.xml ' ,
' tax_code_id ' : _get_tax_code ,
2010-08-23 19:49:16 +00:00
}
2010-04-01 13:38:12 +00:00
2011-12-06 11:11:19 +00:00
def _get_datas ( self , cr , uid , ids , context = None ) :
2011-12-06 12:05:46 +00:00
""" Collects require data for vat intra xml
: param ids : id of wizard .
: return : dict of all data to be used to generate xml for Partner VAT Intra .
: rtype : dict
"""
2011-12-06 11:11:19 +00:00
if context is None :
context = { }
2010-04-01 13:38:12 +00:00
obj_user = self . pool . get ( ' res.users ' )
obj_sequence = self . pool . get ( ' ir.sequence ' )
obj_partner = self . pool . get ( ' res.partner ' )
2011-12-06 11:11:19 +00:00
xmldict = { }
2012-02-08 13:52:20 +00:00
post_code = street = city = country = data_clientinfo = ' '
2010-05-04 11:49:34 +00:00
seq = amount_sum = 0
2010-05-04 11:54:31 +00:00
2011-12-06 11:11:19 +00:00
wiz_data = self . browse ( cr , uid , ids [ 0 ] , context = context )
2012-01-24 09:54:21 +00:00
comments = wiz_data . comments
2011-12-06 11:11:19 +00:00
if wiz_data . tax_code_id :
2012-02-08 13:52:20 +00:00
data_company = wiz_data . tax_code_id . company_id
2010-06-14 07:20:12 +00:00
else :
2012-02-08 13:52:20 +00:00
data_company = obj_user . browse ( cr , uid , uid , context = context ) . company_id
2012-08-07 10:23:26 +00:00
2011-12-06 11:11:19 +00:00
# Get Company vat
2012-02-08 13:52:20 +00:00
company_vat = data_company . partner_id . vat
2009-03-24 11:01:43 +00:00
if not company_vat :
2013-06-07 11:38:29 +00:00
raise osv . except_osv ( _ ( ' Insufficient Data! ' ) , _ ( ' No VAT number associated with your company. ' ) )
2011-09-27 13:04:44 +00:00
company_vat = company_vat . replace ( ' ' , ' ' ) . upper ( )
2012-01-24 09:54:21 +00:00
issued_by = company_vat [ : 2 ]
2009-03-24 11:01:43 +00:00
2011-12-06 11:11:19 +00:00
if len ( wiz_data . period_code ) != 6 :
2012-08-07 10:23:26 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( ' Period code is not valid. ' ) )
2011-12-06 11:11:19 +00:00
if not wiz_data . period_ids :
2012-08-07 10:23:26 +00:00
raise osv . except_osv ( _ ( ' Insufficient Data! ' ) , _ ( ' Please select at least one Period. ' ) )
2011-12-06 11:11:19 +00:00
p_id_list = obj_partner . search ( cr , uid , [ ( ' vat ' , ' != ' , False ) ] , context = context )
if not p_id_list :
2013-06-07 11:38:29 +00:00
raise osv . except_osv ( _ ( ' Insufficient Data! ' ) , _ ( ' No partner has a VAT number associated with him. ' ) )
2011-12-06 11:11:19 +00:00
seq_declarantnum = obj_sequence . get ( cr , uid , ' declarantnum ' )
2012-02-08 11:19:36 +00:00
dnum = company_vat [ 2 : ] + seq_declarantnum [ - 4 : ]
2009-03-24 11:01:43 +00:00
2012-02-08 13:52:20 +00:00
addr = obj_partner . address_get ( cr , uid , [ data_company . partner_id . id ] , [ ' invoice ' ] )
email = data_company . partner_id . email or ' '
phone = data_company . partner_id . phone or ' '
2012-01-18 05:12:56 +00:00
2009-03-24 11:01:43 +00:00
if addr . get ( ' invoice ' , False ) :
2012-03-07 12:04:37 +00:00
ads = obj_partner . browse ( cr , uid , [ addr [ ' invoice ' ] ] ) [ 0 ]
2012-01-18 05:12:56 +00:00
city = ( ads . city or ' ' )
post_code = ( ads . zip or ' ' )
2009-03-24 11:01:43 +00:00
if ads . street :
street = ads . street
if ads . street2 :
street + = ' '
street + = ads . street2
if ads . country_id :
country = ads . country_id . code
2012-02-08 11:19:36 +00:00
if not country :
country = company_vat [ : 2 ]
if not email :
2012-08-07 10:23:26 +00:00
raise osv . except_osv ( _ ( ' Insufficient Data! ' ) , _ ( ' No email address associated with the company. ' ) )
2012-02-08 11:19:36 +00:00
if not phone :
2012-08-07 10:23:26 +00:00
raise osv . except_osv ( _ ( ' Insufficient Data! ' ) , _ ( ' No phone associated with the company. ' ) )
2011-12-06 11:11:19 +00:00
xmldict . update ( {
2012-02-08 13:52:20 +00:00
' company_name ' : data_company . name ,
2012-08-07 10:23:26 +00:00
' company_vat ' : company_vat ,
2011-12-06 11:11:19 +00:00
' vatnum ' : company_vat [ 2 : ] ,
2012-08-07 10:23:26 +00:00
' mand_id ' : wiz_data . mand_id ,
2011-12-06 11:11:19 +00:00
' sender_date ' : str ( time . strftime ( ' % Y- % m- %d ' ) ) ,
' street ' : street ,
2012-01-18 05:12:56 +00:00
' city ' : city ,
' post_code ' : post_code ,
2011-12-06 11:11:19 +00:00
' country ' : country ,
2012-01-18 05:12:56 +00:00
' email ' : email ,
2012-02-08 11:19:36 +00:00
' phone ' : phone . replace ( ' / ' , ' ' ) . replace ( ' . ' , ' ' ) . replace ( ' ( ' , ' ' ) . replace ( ' ) ' , ' ' ) . replace ( ' ' , ' ' ) ,
2011-12-06 11:11:19 +00:00
' period ' : wiz_data . period_code ,
2012-08-07 10:23:26 +00:00
' clientlist ' : [ ] ,
2012-01-24 09:54:21 +00:00
' comments ' : comments ,
' issued_by ' : issued_by ,
2011-12-06 11:11:19 +00:00
} )
2012-04-23 16:45:21 +00:00
#tax code 44: services
#tax code 46L: normal good deliveries
#tax code 46T: ABC good deliveries
#tax code 48xxx: credite note on tax code xxx
2012-02-08 11:18:04 +00:00
codes = ( ' 44 ' , ' 46L ' , ' 46T ' , ' 48s44 ' , ' 48s46L ' , ' 48s46T ' )
2012-08-07 10:23:26 +00:00
cr . execute ( ''' SELECT p.name As partner_name, l.partner_id AS partner_id, p.vat AS vat,
2012-02-08 11:18:04 +00:00
( CASE WHEN t . code = ' 48s44 ' THEN ' 44 '
WHEN t . code = ' 48s46L ' THEN ' 46L '
WHEN t . code = ' 48s46T ' THEN ' 46T '
ELSE t . code END ) AS intra_code ,
SUM ( CASE WHEN t . code in ( ' 48s44 ' , ' 48s46L ' , ' 48s46T ' ) THEN - l . tax_amount ELSE l . tax_amount END ) AS amount
2010-05-04 11:49:34 +00:00
FROM account_move_line l
LEFT JOIN account_tax_code t ON ( l . tax_code_id = t . id )
LEFT JOIN res_partner p ON ( l . partner_id = p . id )
2011-05-06 08:43:27 +00:00
WHERE t . code IN % s
2010-05-04 11:49:34 +00:00
AND l . period_id IN % s
2012-02-08 13:52:20 +00:00
AND t . company_id = % s
GROUP BY p . name , l . partner_id , p . vat , intra_code ''' , (codes, tuple([p.id for p in wiz_data.period_ids]), data_company.id))
2011-12-06 11:11:19 +00:00
2011-12-06 11:38:34 +00:00
p_count = 0
2012-02-08 11:18:04 +00:00
2011-12-06 11:11:19 +00:00
for row in cr . dictfetchall ( ) :
2010-05-04 11:49:34 +00:00
if not row [ ' vat ' ] :
2012-02-08 13:52:20 +00:00
row [ ' vat ' ] = ' '
2011-12-06 11:38:34 +00:00
p_count + = 1
2011-12-06 11:11:19 +00:00
2009-03-24 11:01:43 +00:00
seq + = 1
2012-02-08 13:52:20 +00:00
amt = row [ ' amount ' ] or 0.0
2009-03-24 11:01:43 +00:00
amount_sum + = amt
2011-12-06 11:11:19 +00:00
2011-05-06 08:43:27 +00:00
intra_code = row [ ' intra_code ' ] == ' 44 ' and ' S ' or ( row [ ' intra_code ' ] == ' 46L ' and ' L ' or ( row [ ' intra_code ' ] == ' 46T ' and ' T ' or ' ' ) )
2011-12-06 11:11:19 +00:00
xmldict [ ' clientlist ' ] . append ( {
2011-12-06 11:37:11 +00:00
' partner_name ' : row [ ' partner_name ' ] ,
2012-08-07 10:23:26 +00:00
' seq ' : seq ,
' vatnum ' : row [ ' vat ' ] [ 2 : ] . replace ( ' ' , ' ' ) . upper ( ) ,
2011-12-06 11:38:34 +00:00
' vat ' : row [ ' vat ' ] ,
2011-12-06 11:11:19 +00:00
' country ' : row [ ' vat ' ] [ : 2 ] ,
2013-08-09 12:22:37 +00:00
' amount ' : round ( amt , 2 ) ,
2011-12-06 11:38:34 +00:00
' intra_code ' : row [ ' intra_code ' ] ,
2011-12-06 11:11:19 +00:00
' code ' : intra_code } )
2013-08-09 12:22:37 +00:00
xmldict . update ( { ' dnum ' : dnum , ' clientnbr ' : str ( seq ) , ' amountsum ' : round ( amount_sum , 2 ) , ' partner_wo_vat ' : p_count } )
2011-12-06 11:11:19 +00:00
return xmldict
def create_xml ( self , cursor , user , ids , context = None ) :
2011-12-06 12:05:46 +00:00
""" Creates xml that is to be exported and sent to estate for partner vat intra.
: return : Value for next action .
: rtype : dict
"""
2011-12-06 11:11:19 +00:00
mod_obj = self . pool . get ( ' ir.model.data ' )
xml_data = self . _get_datas ( cursor , user , ids , context = context )
2012-01-18 05:12:56 +00:00
month_quarter = xml_data [ ' period ' ] [ : 2 ]
year = xml_data [ ' period ' ] [ 2 : ]
2011-12-06 11:11:19 +00:00
data_file = ' '
2011-12-06 11:55:40 +00:00
# Can't we do this by etree?
2012-02-08 11:19:36 +00:00
data_head = """ <?xml version= " 1.0 " encoding= " ISO-8859-1 " ?>
< ns2 : IntraConsignment xmlns = " http://www.minfin.fgov.be/InputCommon " xmlns : ns2 = " http://www.minfin.fgov.be/IntraConsignment " IntraListingsNbr = " 1 " >
< ns2 : Representative >
2012-02-13 17:22:08 +00:00
< RepresentativeID identificationType = " NVAT " issuedBy = " %(issued_by)s " > % ( company_vat ) s < / RepresentativeID >
2012-02-08 11:19:36 +00:00
< Name > % ( company_name ) s < / Name >
< Street > % ( street ) s < / Street >
< PostCode > % ( post_code ) s < / PostCode >
< City > % ( city ) s < / City >
< CountryCode > % ( country ) s < / CountryCode >
< EmailAddress > % ( email ) s < / EmailAddress >
< Phone > % ( phone ) s < / Phone >
2012-02-13 17:22:08 +00:00
< / ns2 : Representative > """ % (xml_data)
if xml_data [ ' mand_id ' ] :
data_head + = ' \n \t \t <ns2:RepresentativeReference> %(mand_id)s </ns2:RepresentativeReference> ' % ( xml_data )
2012-02-08 11:19:36 +00:00
data_comp_period = ' \n \t \t <ns2:Declarant> \n \t \t \t <VATNumber> %(vatnum)s </VATNumber> \n \t \t \t <Name> %(company_name)s </Name> \n \t \t \t <Street> %(street)s </Street> \n \t \t \t <PostCode> %(post_code)s </PostCode> \n \t \t \t <City> %(city)s </City> \n \t \t \t <CountryCode> %(country)s </CountryCode> \n \t \t \t <EmailAddress> %(email)s </EmailAddress> \n \t \t \t <Phone> %(phone)s </Phone> \n \t \t </ns2:Declarant> ' % ( xml_data )
2012-01-18 05:12:56 +00:00
if month_quarter . startswith ( ' 3 ' ) :
2012-02-13 17:22:08 +00:00
data_comp_period + = ' \n \t \t <ns2:Period> \n \t \t \t <ns2:Quarter> ' + month_quarter [ 1 ] + ' </ns2:Quarter> \n \t \t \t <ns2:Year> ' + year + ' </ns2:Year> \n \t \t </ns2:Period> '
2012-01-18 05:12:56 +00:00
elif month_quarter . startswith ( ' 0 ' ) and month_quarter . endswith ( ' 0 ' ) :
2012-02-13 17:22:08 +00:00
data_comp_period + = ' \n \t \t <ns2:Period> \n \t \t \t <ns2:Year> ' + year + ' </ns2:Year> \n \t \t </ns2:Period> '
2012-01-18 05:12:56 +00:00
else :
2012-02-08 11:19:36 +00:00
data_comp_period + = ' \n \t \t <ns2:Period> \n \t \t \t <ns2:Month> ' + month_quarter + ' </ns2:Month> \n \t \t \t <ns2:Year> ' + year + ' </ns2:Year> \n \t \t </ns2:Period> '
2011-12-06 11:11:19 +00:00
data_clientinfo = ' '
for client in xml_data [ ' clientlist ' ] :
2012-02-08 13:52:20 +00:00
if not client [ ' vatnum ' ] :
2012-08-07 10:23:26 +00:00
raise osv . except_osv ( _ ( ' Insufficient Data! ' ) , _ ( ' No vat number defined for %s . ' ) % client [ ' partner_name ' ] )
2013-09-06 15:08:35 +00:00
data_clientinfo + = ' \n \t \t <ns2:IntraClient SequenceNumber= " %(seq)s " > \n \t \t \t <ns2:CompanyVATNumber issuedBy= " %(country)s " > %(vatnum)s </ns2:CompanyVATNumber> \n \t \t \t <ns2:Code> %(code)s </ns2:Code> \n \t \t \t <ns2:Amount> %(amount).2f </ns2:Amount> \n \t \t </ns2:IntraClient> ' % ( client )
2011-12-06 11:11:19 +00:00
2013-09-06 15:08:35 +00:00
data_decl = ' \n \t <ns2:IntraListing SequenceNumber= " 1 " ClientsNbr= " %(clientnbr)s " DeclarantReference= " %(dnum)s " AmountSum= " %(amountsum).2f " > ' % ( xml_data )
2011-12-06 11:11:19 +00:00
2012-02-08 11:19:36 +00:00
data_file + = data_head + data_decl + data_comp_period + data_clientinfo + ' \n \t \t <ns2:Comment> %(comments)s </ns2:Comment> \n \t </ns2:IntraListing> \n </ns2:IntraConsignment> ' % ( xml_data )
2010-06-10 13:03:02 +00:00
context [ ' file_save ' ] = data_file
2011-12-06 11:11:19 +00:00
2011-12-06 11:37:11 +00:00
model_data_ids = mod_obj . search ( cursor , user , [ ( ' model ' , ' = ' , ' ir.ui.view ' ) , ( ' name ' , ' = ' , ' view_vat_intra_save ' ) ] , context = context )
resource_id = mod_obj . read ( cursor , user , model_data_ids , fields = [ ' res_id ' ] , context = context ) [ 0 ] [ ' res_id ' ]
2011-12-06 11:11:19 +00:00
2010-06-10 13:03:02 +00:00
return {
' name ' : _ ( ' Save ' ) ,
' context ' : context ,
' view_type ' : ' form ' ,
' view_mode ' : ' form ' ,
' res_model ' : ' partner.vat.intra ' ,
2011-12-06 11:37:11 +00:00
' views ' : [ ( resource_id , ' form ' ) ] ,
2010-06-10 13:03:02 +00:00
' view_id ' : ' view_vat_intra_save ' ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
}
2010-04-01 13:38:12 +00:00
2011-12-06 11:37:11 +00:00
def preview ( self , cr , uid , ids , context = None ) :
xml_data = self . _get_datas ( cr , uid , ids , context = context )
datas = {
' ids ' : [ ] ,
' model ' : ' partner.vat.intra ' ,
' form ' : xml_data
}
2014-04-22 12:11:05 +00:00
return self . pool [ ' report ' ] . get_action (
cr , uid , [ ] , ' l10n_be.report_l10nvatintraprint ' , data = datas , context = context
)
2011-12-06 11:37:11 +00:00
2010-04-01 13:38:12 +00:00
2011-12-06 11:37:11 +00:00
class vat_intra_print ( report_sxw . rml_parse ) :
def __init__ ( self , cr , uid , name , context ) :
super ( vat_intra_print , self ) . __init__ ( cr , uid , name , context = context )
self . localcontext . update ( {
' time ' : time ,
} )
2014-04-22 12:11:05 +00:00
class wrapped_vat_intra_print ( osv . AbstractModel ) :
_name = ' report.l10n_be.report_l10nvatintraprint '
_inherit = ' report.abstract_report '
_template = ' l10n_be.report_l10nvatintraprint '
_wrapped_report_class = vat_intra_print
2011-12-06 11:37:11 +00:00
2010-11-08 17:44:07 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: