1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
6
# Associazione OpenERP Italia (<http://www.openerp-italia.org>)
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU Affero General Public License as
10
# published by the Free Software Foundation, either version 3 of the
11
# License, or (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 Affero General Public License for more details.
18
# You should have received a copy of the GNU Affero General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
##############################################################################
23
from osv import fields, osv
26
class account_invoice_tax(osv.osv):
27
_inherit = "account.invoice.tax"
29
def compute(self, cr, uid, invoice_id, context=None):
30
tax_grouped = super(account_invoice_tax, self).compute(cr, uid, invoice_id, context)
33
total_amount_of_taxes_horizontal = 0
34
number_deductible_account = 0
35
cur_obj = self.pool.get('res.currency')
36
inv = self.pool.get('account.invoice').browse(cr, uid, invoice_id, context=context)
38
for line in inv.invoice_line:
39
# workout of total amount of taxes
40
for tax in line.invoice_line_tax_id:
42
if not key in total_tax:
45
total_tax[key][0] += (line.price_unit* (1-(line.discount or 0.0)/100.0)) * tax['amount']
48
for t in tax_grouped.values():
49
if inv.type in ('in_invoice') and t['base_code_id'] == False:
50
number_deductible_account += 1
51
total_amount_of_taxes_horizontal += t['tax_amount']
53
total_amount_of_taxes_vertical = 0
54
# round the total amount of taxes
55
for t in total_tax.values():
56
t[0] = cur_obj.round(cr, uid, cur, t[0])
57
total_amount_of_taxes_vertical += t[0]
58
total_amount_of_taxes_vertical = cur_obj.round(cr, uid, cur, total_amount_of_taxes_vertical)
59
total_amount_of_taxes_horizontal = cur_obj.round(cr, uid, cur, total_amount_of_taxes_horizontal)
60
if number_deductible_account != 0:
61
quotient = (total_amount_of_taxes_vertical - total_amount_of_taxes_horizontal) / number_deductible_account
62
quotient = cur_obj.round(cr, uid, cur, quotient)
63
remainder = (total_amount_of_taxes_vertical - total_amount_of_taxes_horizontal) - number_deductible_account * quotient
64
remainder = cur_obj.round(cr, uid, cur, remainder)
65
# change at least a deductible tax amount to to make coincide total amount of taxes
67
if inv.type in ('in_invoice') and total_amount_of_taxes_vertical != total_amount_of_taxes_horizontal:
68
for t in tax_grouped.values():
69
if t['base_code_id'] == False:
71
t['tax_amount'] = t['tax_amount'] + quotient
72
t['amount'] = t['amount'] + quotient
73
if counter == number_deductible_account:
74
t['tax_amount'] = t['tax_amount'] + remainder
75
t['amount'] = t['amount'] + remainder