# -*- encoding: utf-8 -*-
##############################################################################
#    
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2010 Savoir-faire Linux (<http://www.savoirfairelinux.com>).
#
#    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.
#
#    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.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
#
##############################################################################
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

from osv import osv, fields
 
class account_tax(osv.osv):
    _inherit = 'account.tax'
    _columns = {
        'expense_include': fields.boolean('Tax Included in Expense',
                                          help="Check this if this tax is \
                                            included in the expense amount."),
    }

    def compute_all(self, cr, uid, taxes, price_unit, quantity, address_id=None, product=None, partner=None):
        """
        RETURN: {
                'total': 0.0,                # Total without taxes
                'total_included: 0.0,        # Total with taxes
                'taxes': []                  # List of taxes, see compute for the format
            }
        """
        precision = self.pool.get('decimal.precision').precision_get(cr, uid, 'Account')
        totalin = totalex = round(price_unit * quantity, precision)
        tin = []
        tex = []
        for tax in taxes:
            if (partner and partner.employee and tax.expense_include) or tax.price_include:
                tin.append(tax)
            else:
                tex.append(tax)
        tin = self.compute_inv(cr, uid, tin, price_unit, quantity, address_id=address_id, product=product, partner=partner)
        for r in tin:
            totalex -= r.get('amount', 0.0)
        totlex_qty = 0.0
        try:
            totlex_qty=totalex/quantity
        except:
            pass
        tex = self._compute(cr, uid, tex, totlex_qty, quantity, address_id=address_id, product=product, partner=partner)
        for r in tex:
            totalin += r.get('amount', 0.0)
        return {
            'total': totalex,
            'total_included': totalin,
            'taxes': tin + tex
        }


account_tax()

@@ -1098,17 +1098,66 @@
  fields%0A
- 
+from openerp.tools.float_utils import float_round%0A
 %0Aclass a
@@ -1521,25 +1521,8 @@
 ity,
- address_id=None,
  pro
@@ -1544,16 +1544,38 @@
 ner=None
+, force_excluded=False
 ):%0A     
@@ -1593,352 +1593,1407 @@
     
-RETURN: %7B%0A                'total': 0.0,                # Total without taxes%0A                'total_included: 0.0,        # Total with taxes%0A                'taxes': %5B%5D                  # List of taxes, see compute for the format%0A            %7D%0A        %22%22%22%0A        precision = self.pool.get('decimal.precision').precision_get(cr, uid, 'Account')
+:param force_excluded: boolean used to say that we don't want to consider the value of field price_include of%0A            tax. It's used in encoding by line where you don't matter if you encoded a tax with that boolean to True or%0A            False%0A        RETURN: %7B%0A                'total': 0.0,                # Total without taxes%0A                'total_included: 0.0,        # Total with taxes%0A                'taxes': %5B%5D                  # List of taxes, see compute for the format%0A            %7D%0A        %22%22%22%0A%0A        # By default, for each tax, tax amount will first be computed%0A        # and rounded at the 'Account' decimal precision for each%0A        # PO/SO/invoice line and then these rounded amounts will be%0A        # summed, leading to the total amount for that tax. But, if the%0A        # company has tax_calculation_rounding_method = round_globally,%0A        # we still follow the same method, but we use a much larger%0A        # precision when we round the tax amount for each line (we use%0A        # the 'Account' decimal precision + 5), and that way it's like%0A        # rounding after the sum of the tax amounts of each line%0A        precision = self.pool.get('decimal.precision').precision_get(cr, uid, 'Account')%0A        tax_compute_precision = precision%0A        if taxes and taxes%5B0%5D.company_id.tax_calculation_rounding_method == 'round_globally':%0A            tax_compute_precision += 5
 %0A   
@@ -3017,16 +3017,22 @@
 talex = 
+float_
 round(pr
@@ -3142,52 +3142,20 @@
  if 
-(partner and partner.employee and tax.expens
+not tax.pric
 e_in
@@ -3163,30 +3163,26 @@
 lude
-)
  or 
-tax.price_in
+force_ex
 clude
+d
 :%0A  
@@ -3196,18 +3196,18 @@
        t
-in
+ex
 .append(
@@ -3238,34 +3238,34 @@
                t
-ex
+in
 .append(tax)%0A   
@@ -3327,39 +3327,16 @@
 uantity,
- address_id=address_id,
  product
@@ -3352,32 +3352,65 @@
  partner=partner
+, precision=tax_compute_precision
 )%0A        for r 
@@ -3521,17 +3521,19 @@
 tlex_qty
-=
+ = 
 totalex/
@@ -3641,31 +3641,8 @@
 ity,
- address_id=address_id,
  pro
@@ -3670,16 +3670,49 @@
 =partner
+, precision=tax_compute_precision
 )%0A      

fix account_tax_expense_include tax calculation