Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
2 Răspunsuri
372 Vizualizări

Here is my function code:

@api.depends_context('lang')
@api.depends('order_line.taxes_id', 'order_line.price_subtotal', 'amount_untaxed', 'tax_amount', 'discount_amount')
def _compute_tax_totals(self):
for order in self:
order_lines = order.order_line.filtered(lambda x: not x.display_type)

# Step 1: Get base tax totals (Odoo default logic)
base_totals = self.env['account.tax']._prepare_tax_totals(
[line._convert_to_tax_base_line_dict() for line in order_lines],
order.currency_id or order.company_id.currency_id,
)

# Step 2: Override tax + discount logic
computed_tax = base_totals.get('amount_tax', 0.0)
tax_adjustment = order.tax_amount - computed_tax
tax_totals = base_totals['amount_total'] + tax_adjustment - order.discount_amount

# Step 3: Set modified tax_totals (overwrite the whole dict!)
order.tax_totals = {
**base_totals,
'amount_tax': order.tax_amount,
'amount_total': tax_totals,
'formatted_amount_total': order.currency_id.format(tax_totals),
}

I tried to update total amount on the purchase order, and for that I found tax_totlas field. I have found that it's not a field but a @property, so I can't update value like this. Please let me know what's wrong with my code and how can I acheive it. Thanks
Imagine profil
Abandonează
Cel mai bun răspuns

Hi,


Try with the following code


class PurchaseOrder(models.Model):

    _inherit = 'purchase.order'


    discount_amount = fields.Monetary(string='Discount Amount')

    tax_amount = fields.Monetary(string='Manual Tax Amount')


    @api.depends('amount_untaxed', 'tax_amount', 'discount_amount')

    def _compute_amount(self):

        for order in self:

            order.amount_tax = order.tax_amount  # override tax if needed

            order.amount_total = (

                order.amount_untaxed + order.tax_amount - order.discount_amount

            )


Odoo already computes amount_untaxed and amount_tax, so you might override amount_tax with your manual value (order.tax_amount), and then adjust the total.


-Don't try to write order.tax_totals = {...} — it's a computed UI field.

-Instead, override _compute_amount() to update amount_total based on your logic.

-Ensure amount_total is updated via this method, as Odoo uses it for invoice creation, vendor bills, and reports.



Hope it helps

Imagine profil
Abandonează
Cel mai bun răspuns

Hii,

Override the tax_totals property on the purchase.order model.

Compute and return the adjusted values in the getter.

Example Fix

from odoo import models, api


class PurchaseOrder(models.Model):

    _inherit = 'purchase.order'


    @property

    def tax_totals(self):

        self.ensure_one()

        order_lines = self.order_line.filtered(lambda x: not x.display_type)


        # Step 1: Get base tax totals

        base_totals = self.env['account.tax']._prepare_tax_totals(

            [line._convert_to_tax_base_line_dict() for line in order_lines],

            self.currency_id or self.company_id.currency_id,

        )


        # Step 2: Adjust with manual values

        computed_tax = base_totals.get('amount_tax', 0.0)

        tax_adjustment = self.tax_amount - computed_tax

        tax_totals = base_totals['amount_total'] + tax_adjustment - self.discount_amount


        # Step 3: Return modified totals

        return {

            **base_totals,

            'amount_tax': self.tax_amount,

            'amount_total': tax_totals,

            'formatted_amount_total': self.currency_id.format(tax_totals),

        }

tax_totals is primarily used in portal views, PDF reports, or frontend rendering.

You don’t store this field — it’s used at read-time (e.g., in templates or JSON responses).

If you want to store a modified total (e.g., for backend use), create a new fields.Monetary field like custom_total_amount and compute it separately.


i hope it is usefull

Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
2
mai 25
2045
3
dec. 24
5998
1
iul. 24
2864
1
iun. 24
2026
2
iun. 24
2821