1

I wanna inherit and modify odoo default tax calculation in odoo_15 Community because Odoo default tax calculation is losing over 100 kyats from our revenue. How can I Inherit and Modify Odoo Tax Formulation?Tax Calculation Comparison

Gladiator
  • 21
  • 2

1 Answers1

0

You could extend (inherit) the AccountTaxPython class, to inject your own Tax computation (and your own custom parameters, like in my case : nbpersons_zsb)

# ODOO version 13
# source of the AccountTaxPython class : addons/account_tax_python/account_tax.py
# inherited class here:
class AccountTaxPython(models.Model):
    _inherit = "account.tax"

    # original _compute_amount def in addons/account/models/account.py, chged in  addons/account_tax_python/account_tax.py
    def _compute_amount(self, base_amount, price_unit, quantity=1.0, product=None, partner=None):

        self.ensure_one()
        if product and product._name == 'product.template':
            product = product.product_variant_id

        # WILL ONLY AFFECT PYTHON CODED TAXES (FILTER == code)
        if self.amount_type == 'code':
            company = self.env.company
            if self._context.get('nbpersons_zsb'):
                nbpersons_zsb = self._context.get('nbpersons_zsb')
                # if request.session['number_of_person']:
                # nbpersons_zsb = request.session['number_of_person']
                
                localdict = {'nbpersons_zsb': nbpersons_zsb, 'base_amount': base_amount, 'price_unit': price_unit,
                             'quantity': quantity, 'product': product, 'partner': partner, 'company': company}
                # localdict = {'base_amount': base_amount, 'price_unit':price_unit, 'quantity': quantity, 'product':product, 'partner':partner, 'company': company}
                safe_eval(self.python_compute, localdict, mode="exec", nocopy=True)
                return localdict['result']
        return super(AccountTaxPython, self)._compute_amount(base_amount, price_unit, quantity, product, partner)

    def compute_all(self, price_unit, currency=None, quantity=1.0, product=None, partner=None, is_refund=False,
                    handle_price_include=True):

        taxes = self.filtered(lambda r: r.amount_type != 'code')
        company = self.env.company
        if product and product._name == 'product.template':
            product = product.product_variant_id

        # WILL ONLY AFFECT PYTHON CODED TAXES (FILTER == code)
        for tax in self.filtered(lambda r: r.amount_type == 'code'):

            localdict = self._context.get('tax_computation_context', {})
            if self._context.get('nbpersons_zsb'):
                nbpersons_zsb = self._context.get('nbpersons_zsb')
                # if request.session['number_of_person']:
                # nbpersons_zsb = request.session['number_of_person']
                
                localdict.update(
                    {'nbpersons_zsb': nbpersons_zsb, 'price_unit': price_unit, 'quantity': quantity, 'product': product,
                     'partner': partner, 'company': company})
                # localdict.update({'price_unit':price_unit,'quantity':quantity,'product':product,'partner':partner,'company':company})
                safe_eval(tax.python_applicable, localdict, mode="exec", nocopy=True)
                if localdict.get('result', False):
                    taxes += tax
        return super(AccountTaxPython, taxes).compute_all(price_unit, currency, quantity, product, partner,
                                                          is_refund=is_refund,
                                                          handle_price_include=handle_price_include)
Ali
  • 922
  • 1
  • 9
  • 24
sylvain
  • 853
  • 1
  • 7
  • 20