Function field getter with context not getting context key from instance

I need to filter component tax lines so they can be reported at mandated levels (see previous topic for context). The way I have approached this is to save a reporting code on each account.tax.line since the level depends on the day of the transaction and the customer’s shipping address. For the Open Tax Code wizard I use a search domain, and this is working great to get the correct lines for a tax code. But for the Chart of Tax Codes, I have taken the approach of storing the reporting code in the transaction context so it is available to the Tax._amount_where() clause. This works if I set the context from a class method (e.g. TaxCode.get_amount()), but if I set it from an instance property (TaxCodeLine.value) where it needs to be set, the reporting code is missing from the context when I need it (Tax._amount_where(), another class method). I have noticed that another transaction is created when the query is run on the Tax.get_amount (a Function.getter) but I’m not sure whether that is related to the problem.

I’m new to Tryton and Python in general, so any help would be appreciated. I’m sure I am missing something obvious. Thanks in advance!

Here are some simplified code excerpts to show what I am trying to accomplish:

class Tax(metaclass=PoolMeta):
    __name__ = 'account.tax'

    @classmethod
    def _amount_where(cls, tax_line, move_line, move):
        context = Transaction().context
        reporting_code = context.get('reporting_code')
        if reporting_code:
            return where & (tax_line.code == reporting_code)
        else:
            return where

class TaxCodeLine(metaclass=PoolMeta):
    __name__ = 'account.tax.code.line'

    @property
    def value(self):
        with Transaction().set_context(reporting_code=self.code.code):
            v = super().value
        return v

The context, under which the getter of a function field is run, is the context set when the record was instantiated.
So setting the context in the property method does not change the context of the instance nor the one used to compute the amounts.

In your case you need to set a context on the tax instance from the tax field, so you need to set it in the field definition (like the company key).

Thank you! That bit about the context being set when the record was instantiated I did not yet know. That helped me understand so much more of what was happening.

Here is what ended up working. It seemed that I could only reference the field itself in TaxCodeLine.__setup__(), not a field of the field. So I had to re-instantiate the tax code in the _amount_where method.

class Tax(metaclass=PoolMeta):
    __name__ = 'account.tax'

    @classmethod
    def _amount_where(cls, tax_line, move_line, move):
        ...
        if Transaction().context.get('code'):
            TaxCode = Pool().get('account.tax.code')
            code = TaxCode(Transaction().context['code'])
            return where & (tax_line.code == code.code)
        else:
            return where

class TaxCodeLine(metaclass=PoolMeta):
    __name__ = 'account.tax.code.line'

    @classmethod
    def __setup__(cls):
        super().__setup__()
        cls.tax.context['code'] = Eval('code')
        cls.tax.depends.add('code')

I created Document that getter function is called with the record context (!1692) · Merge requests · Tryton / Tryton · GitLab to clarrify

See Fields — Tryton server

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.