2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2008-11-03 19:18:56 +00:00
##############################################################################
2010-03-25 06:53:08 +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>).
2012-08-07 10:23:26 +00:00
#
# Adapted by Noviat to
2011-05-05 12:00:03 +00:00
# - enforce correct vat number
# - support negative balance
2011-05-05 14:49:42 +00:00
# - assign amount of tax code 71-72 correclty to grid 71 or 72
2011-05-06 08:46:28 +00:00
# - support Noviat tax code scheme
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
2010-03-25 06:53:08 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-11-03 19:18:56 +00:00
#
##############################################################################
2010-04-07 06:55:02 +00:00
import base64
2010-05-21 09:24:05 +00:00
2012-12-06 14:56:32 +00:00
from openerp . osv import fields , osv
from openerp . tools . translate import _
2009-02-03 14:03:11 +00:00
2010-03-26 14:13:52 +00:00
class l10n_be_vat_declaration ( osv . osv_memory ) :
2010-04-07 06:55:02 +00:00
""" Vat Declaration """
2010-03-26 14:13:52 +00:00
_name = " l1on_be.vat.declaration "
_description = " Vat Declaration "
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-03-26 14:13:52 +00:00
_columns = {
2010-05-21 09:24:05 +00:00
' name ' : fields . char ( ' File Name ' , size = 32 ) ,
2010-03-26 14:13:52 +00:00
' period_id ' : fields . many2one ( ' account.period ' , ' Period ' , required = True ) ,
2012-02-13 11:02:14 +00:00
' tax_code_id ' : fields . many2one ( ' account.tax.code ' , ' Tax Code ' , domain = [ ( ' parent_id ' , ' = ' , False ) ] , required = True ) ,
2010-03-26 14:13:52 +00:00
' msg ' : fields . text ( ' File created ' , size = 64 , readonly = True ) ,
2010-04-02 04:32:42 +00:00
' file_save ' : fields . binary ( ' Save File ' ) ,
2012-09-13 06:40:30 +00:00
' ask_restitution ' : fields . boolean ( ' Ask Restitution ' , help = ' It indicates whether a restitution is to make or not? ' ) ,
' ask_payment ' : fields . boolean ( ' Ask Payment ' , help = ' It indicates whether a payment is to make or not? ' ) ,
2012-02-13 11:02:14 +00:00
' client_nihil ' : fields . boolean ( ' Last Declaration, no clients in client listing ' , help = ' Tick this case only if it concerns only the last statement on the civil or cessation of activity: ' \
' no clients to be included in the client listing. ' ) ,
2012-01-23 12:25:12 +00:00
' comments ' : fields . text ( ' Comments ' ) ,
2010-04-07 09:54:57 +00:00
}
2012-02-06 16:19:51 +00:00
2012-02-06 16:23:45 +00:00
def _get_tax_code ( self , cr , uid , context = None ) :
2012-02-06 16:19:51 +00:00
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-03-26 14:13:52 +00:00
_defaults = {
2010-05-21 09:24:05 +00:00
' msg ' : ' Save the File with ' " .xml " ' extension. ' ,
2010-06-10 13:03:02 +00:00
' file_save ' : _get_xml_data ,
2010-06-11 07:04:25 +00:00
' name ' : ' vat_declaration.xml ' ,
2012-02-06 16:19:51 +00:00
' tax_code_id ' : _get_tax_code ,
2010-08-23 19:49:16 +00:00
}
2010-03-26 14:13:52 +00:00
2010-05-21 09:24:05 +00:00
def create_xml ( self , cr , uid , ids , context = None ) :
2010-03-26 14:13:52 +00:00
obj_tax_code = self . pool . get ( ' account.tax.code ' )
obj_acc_period = self . pool . get ( ' account.period ' )
obj_user = self . pool . get ( ' res.users ' )
2012-09-13 09:28:14 +00:00
obj_partner = self . pool . get ( ' res.partner ' )
2010-06-11 07:04:25 +00:00
mod_obj = self . pool . get ( ' ir.model.data ' )
2010-03-26 14:13:52 +00:00
2010-05-21 09:24:05 +00:00
if context is None :
context = { }
2011-05-05 12:00:03 +00:00
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 ' ]
2010-06-14 09:17:41 +00:00
data_tax = self . browse ( cr , uid , ids [ 0 ] )
if data_tax . tax_code_id :
obj_company = data_tax . tax_code_id . company_id
2010-06-14 07:20:12 +00:00
else :
obj_company = obj_user . browse ( cr , uid , uid , context = context ) . company_id
2011-12-01 15:35:35 +00:00
vat_no = obj_company . partner_id . vat
2008-07-23 10:29:42 +00:00
if not vat_no :
2013-04-29 05:52:49 +00:00
raise osv . except_osv ( _ ( ' Insufficient Data! ' ) , _ ( ' No VAT number associated with your company. ' ) )
2011-12-01 15:35:35 +00:00
vat_no = vat_no . replace ( ' ' , ' ' ) . upper ( )
2012-01-24 12:22:31 +00:00
vat = vat_no [ 2 : ]
2008-07-23 10:29:42 +00:00
2012-02-06 16:19:51 +00:00
tax_code_ids = obj_tax_code . search ( cr , uid , [ ( ' parent_id ' , ' child_of ' , data_tax . tax_code_id . id ) , ( ' company_id ' , ' = ' , obj_company . id ) ] , context = context )
2008-07-23 10:29:42 +00:00
ctx = context . copy ( )
2010-03-26 14:13:52 +00:00
data = self . read ( cr , uid , ids ) [ 0 ]
2012-02-13 11:02:14 +00:00
ctx [ ' period_id ' ] = data [ ' period_id ' ] [ 0 ]
2010-03-25 06:53:08 +00:00
tax_info = obj_tax_code . read ( cr , uid , tax_code_ids , [ ' code ' , ' sum_period ' ] , context = ctx )
2008-07-23 10:29:42 +00:00
2012-09-13 09:28:14 +00:00
default_address = obj_partner . address_get ( cr , uid , [ obj_company . partner_id . id ] )
default_address_id = default_address . get ( " default " , obj_company . partner_id . id )
address_id = obj_partner . browse ( cr , uid , default_address_id , context )
2010-03-25 06:53:08 +00:00
2011-04-07 10:09:25 +00:00
account_period = obj_acc_period . browse ( cr , uid , data [ ' period_id ' ] [ 0 ] , context = context )
2012-03-22 10:34:50 +00:00
issued_by = vat_no [ : 2 ]
2012-01-23 12:25:12 +00:00
comments = data [ ' comments ' ] or ' '
2012-03-22 10:34:50 +00:00
2010-04-06 15:20:04 +00:00
send_ref = str ( obj_company . partner_id . id ) + str ( account_period . date_start [ 5 : 7 ] ) + str ( account_period . date_stop [ : 4 ] )
2012-03-22 10:34:50 +00:00
2009-02-05 05:43:13 +00:00
starting_month = account_period . date_start [ 5 : 7 ]
ending_month = account_period . date_stop [ 5 : 7 ]
2012-01-27 09:01:35 +00:00
quarter = str ( ( ( int ( starting_month ) - 1 ) / 3 ) + 1 )
2012-03-22 10:34:50 +00:00
2012-09-13 09:28:14 +00:00
if not address_id . email :
2012-08-07 10:23:26 +00:00
raise osv . except_osv ( _ ( ' Insufficient Data! ' ) , _ ( ' No email address associated with the company. ' ) )
2012-09-13 09:28:14 +00:00
if not address_id . phone :
2012-08-07 10:23:26 +00:00
raise osv . except_osv ( _ ( ' Insufficient Data! ' ) , _ ( ' No phone associated with the company. ' ) )
2012-01-27 09:01:35 +00:00
file_data = {
' issued_by ' : issued_by ,
' vat_no ' : vat_no ,
' only_vat ' : vat_no [ 2 : ] ,
' cmpny_name ' : obj_company . name ,
2012-09-13 09:28:14 +00:00
' address ' : " %s %s " % ( address_id . street or " " , address_id . street2 or " " ) ,
' post_code ' : address_id . zip or " " ,
' city ' : address_id . city or " " ,
' country_code ' : address_id . country_id and address_id . country_id . code or " " ,
' email ' : address_id . email or " " ,
' phone ' : address_id . phone . replace ( ' . ' , ' ' ) . replace ( ' / ' , ' ' ) . replace ( ' ( ' , ' ' ) . replace ( ' ) ' , ' ' ) . replace ( ' ' , ' ' ) ,
2012-01-27 09:01:35 +00:00
' send_ref ' : send_ref ,
' quarter ' : quarter ,
' month ' : starting_month ,
' year ' : str ( account_period . date_stop [ : 4 ] ) ,
' client_nihil ' : ( data [ ' client_nihil ' ] and ' YES ' or ' NO ' ) ,
' ask_restitution ' : ( data [ ' ask_restitution ' ] and ' YES ' or ' NO ' ) ,
' ask_payment ' : ( data [ ' ask_payment ' ] and ' YES ' or ' NO ' ) ,
' comments ' : comments ,
}
2012-08-07 10:23:26 +00:00
2012-01-27 09:01:35 +00:00
data_of_file = """ <?xml version= " 1.0 " ?>
2012-02-07 12:32:28 +00:00
< ns2 : VATConsignment xmlns = " http://www.minfin.fgov.be/InputCommon " xmlns : ns2 = " http://www.minfin.fgov.be/VATConsignment " VATDeclarationsNbr = " 1 " >
< ns2 : Representative >
2012-02-13 11:02:14 +00:00
< RepresentativeID identificationType = " NVAT " issuedBy = " %(issued_by)s " > % ( only_vat ) s < / RepresentativeID >
2012-01-27 09:01:35 +00:00
< Name > % ( cmpny_name ) s < / Name >
< Street > % ( address ) s < / Street >
< PostCode > % ( post_code ) s < / PostCode >
< City > % ( city ) s < / City >
< CountryCode > % ( country_code ) s < / CountryCode >
< EmailAddress > % ( email ) s < / EmailAddress >
< Phone > % ( phone ) s < / Phone >
2012-02-07 12:32:28 +00:00
< / ns2 : Representative >
< ns2 : VATDeclaration SequenceNumber = " 1 " DeclarantReference = " %(send_ref)s " >
< ns2 : Declarant >
< VATNumber xmlns = " http://www.minfin.fgov.be/InputCommon " > % ( only_vat ) s < / VATNumber >
2012-01-27 09:01:35 +00:00
< Name > % ( cmpny_name ) s < / Name >
< Street > % ( address ) s < / Street >
< PostCode > % ( post_code ) s < / PostCode >
< City > % ( city ) s < / City >
< CountryCode > % ( country_code ) s < / CountryCode >
< EmailAddress > % ( email ) s < / EmailAddress >
< Phone > % ( phone ) s < / Phone >
2012-02-07 12:32:28 +00:00
< / ns2 : Declarant >
< ns2 : Period >
2012-01-27 09:01:35 +00:00
""" % (file_data)
2012-02-13 11:02:14 +00:00
2009-02-03 14:03:11 +00:00
if starting_month != ending_month :
2010-03-25 06:53:08 +00:00
#starting month and ending month of selected period are not the same
2009-02-03 14:03:11 +00:00
#it means that the accounting isn't based on periods of 1 month but on quarters
2012-02-07 12:32:28 +00:00
data_of_file + = ' \t \t <ns2:Quarter> %(quarter)s </ns2:Quarter> \n \t \t ' % ( file_data )
2009-02-03 14:03:11 +00:00
else :
2012-02-07 12:32:28 +00:00
data_of_file + = ' \t \t <ns2:Month> %(month)s </ns2:Month> \n \t \t ' % ( file_data )
data_of_file + = ' \t <ns2:Year> %(year)s </ns2:Year> ' % ( file_data )
data_of_file + = ' \n \t \t </ns2:Period> \n '
data_of_file + = ' \t \t <ns2:Data> \t '
2011-05-05 12:00:03 +00:00
cases_list = [ ]
2008-07-23 10:29:42 +00:00
for item in tax_info :
2010-06-24 10:42:25 +00:00
if item [ ' code ' ] == ' 91 ' and ending_month != 12 :
#the tax code 91 can only be send for the declaration of December
continue
2012-02-07 12:32:28 +00:00
if item [ ' code ' ] and item [ ' sum_period ' ] :
2011-05-06 08:46:28 +00:00
if item [ ' code ' ] == ' VI ' :
2011-05-05 12:00:03 +00:00
if item [ ' sum_period ' ] > = 0 :
item [ ' code ' ] = ' 71 '
else :
item [ ' code ' ] = ' 72 '
2008-07-23 10:29:42 +00:00
if item [ ' code ' ] in list_of_tags :
2011-05-05 12:00:03 +00:00
cases_list . append ( item )
cases_list . sort ( )
for item in cases_list :
2012-01-27 09:01:35 +00:00
grid_amount_data = {
' code ' : str ( int ( item [ ' code ' ] ) ) ,
2013-05-10 11:01:45 +00:00
' amount ' : ' %.2f ' % abs ( item [ ' sum_period ' ] ) ,
2012-01-27 09:01:35 +00:00
}
2012-02-07 12:32:28 +00:00
data_of_file + = ' \n \t \t \t <ns2:Amount GridNumber= " %(code)s " > %(amount)s </ns2:Amount ' ' > ' % ( grid_amount_data )
2012-08-07 10:23:26 +00:00
2012-02-07 12:32:28 +00:00
data_of_file + = ' \n \t \t </ns2:Data> '
data_of_file + = ' \n \t \t <ns2:ClientListingNihil> %(client_nihil)s </ns2:ClientListingNihil> ' % ( file_data )
data_of_file + = ' \n \t \t <ns2:Ask Restitution= " %(ask_restitution)s " Payment= " %(ask_payment)s " /> ' % ( file_data )
data_of_file + = ' \n \t \t <ns2:Comment> %(comments)s </ns2:Comment> ' % ( file_data )
data_of_file + = ' \n \t </ns2:VATDeclaration> \n </ns2:VATConsignment> '
2010-06-10 13:03:02 +00:00
model_data_ids = mod_obj . search ( cr , uid , [ ( ' model ' , ' = ' , ' ir.ui.view ' ) , ( ' name ' , ' = ' , ' view_vat_save ' ) ] , context = context )
resource_id = mod_obj . read ( cr , uid , model_data_ids , fields = [ ' res_id ' ] , context = context ) [ 0 ] [ ' res_id ' ]
context [ ' file_save ' ] = data_of_file
return {
2010-06-11 07:04:25 +00:00
' name ' : _ ( ' Save XML For Vat declaration ' ) ,
2010-06-10 13:03:02 +00:00
' context ' : context ,
' view_type ' : ' form ' ,
' view_mode ' : ' form ' ,
' res_model ' : ' l1on_be.vat.declaration ' ,
' views ' : [ ( resource_id , ' form ' ) ] ,
' view_id ' : ' view_vat_save ' ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
}
2010-06-11 07:04:25 +00:00
2010-03-25 06:53:08 +00:00
2010-10-13 12:09:11 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: