~domsense/openobject-italia/6.1-fix-primanota-lep

« back to all changes in this revision

Viewing changes to account_invoice_tax_by_column/invoice.py

  • Committer: eLBati
  • Date: 2011-08-23 16:36:37 UTC
  • Revision ID: lorenzo.battistini@agilebg.com-20110823163637-k268yxwt8q4y88ip
reverted to 118

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2011
 
6
#    Associazione OpenERP Italia (<http://www.openerp-italia.org>)
 
7
#
 
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.
 
12
#
 
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.
 
17
#
 
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/>.
 
20
#
 
21
##############################################################################
 
22
import time
 
23
from osv import fields, osv
 
24
 
 
25
 
 
26
class account_invoice_tax(osv.osv):
 
27
    _inherit = "account.invoice.tax"
 
28
 
 
29
    def compute(self, cr, uid, invoice_id, context=None):
 
30
        tax_grouped = super(account_invoice_tax, self).compute(cr, uid, invoice_id, context)
 
31
        total_base = 0
 
32
        total_tax = {}
 
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)
 
37
        cur = inv.currency_id
 
38
        for line in inv.invoice_line:
 
39
            # workout of total amount of taxes
 
40
            for tax in line.invoice_line_tax_id:
 
41
                key = tax['amount']
 
42
                if not key in total_tax:
 
43
                    # Total_tax 
 
44
                    total_tax[key] = [0]
 
45
                total_tax[key][0] += (line.price_unit* (1-(line.discount or 0.0)/100.0)) * tax['amount']
 
46
 
 
47
        index = 0
 
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']
 
52
 
 
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
 
66
        counter = 0
 
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:
 
70
                    counter += 1
 
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  
 
76
        return tax_grouped
 
77
    
 
78
account_invoice_tax()    
 
79