Skip to Content
Menu
This question has been flagged
3 Replies
2137 Views

Hi,


I am trying to add a calculated field into the sale order line. What i need to accomplish is to have calculated the quantities delivered of the order line.


I tried with this code, but is not making the operation properly:

for  record in self:

​if record.sale_order.line.qty_delivered > 0:

​record['x_studio_subtotal_entregado'] = (record.qty_delivered * record.price_unit)

​else:

​result = 0

and in the dependencies, added the fields.


Am i missing something?


Thanks.!



Avatar
Discard

Would you please tell us how it is not working? Are you getting an error or is it not giving you the expected result?
Which fields did you add in the dependencies?

Best Answer

Hello Luis Jacobo,


I hope you're doing well.


Attached is a screenshot of the studio field used to calculate the price of delivered quantities on the sale order line for your reference.


Here is the code to calculate the subtotal amount of delivered quantities:


for record in self:

    record['x_studio_subtotal_entregado'] = 0

    if record.qty_delivered > 0:

        record['x_studio_subtotal_entregado'] = record.qty_delivered * record.price_unit


I hope this is helpful for you.

Thanks & Regards,

Email:   odoo@aktivsoftware.com           

Skype: kalpeshmaheshwari 

Avatar
Discard
Author

That works perfectly.

But now, using the same logic, how can I calculate the tax amount from that field?

Tried with the tax_id field, but had some errors when validating the field.

How can I pass the tax rate to this new calculated field?

Thanks

Best Answer

Hi,

To add a calculated field into the sale order line to compute the delivered quantities properly, you should use the @api.depends decorator to ensure the field is updated whenever the related fields change.For example, use the following code:

from odoo import models, fields, api


class SaleOrderLine(models.Model):

    _inherit = 'sale.order.line'


    x_studio_subtotal_entregado = fields.Float(string='Subtotal Entregado', compute='_compute_subtotal_entregado', store=True)


    @api.depends('qty_delivered', 'price_unit')

    def _compute_subtotal_entregado(self):

        for record in self:

            if record.qty_delivered > 0:

                record.x_studio_subtotal_entregado = record.qty_delivered * record.price_unit

            else:

                record.x_studio_subtotal_entregado = 0


Hope this helps.

Avatar
Discard
Author Best Answer

Hi,

Tried the code, but is not reflecting the result of the calculation in the sale order line.



Avatar
Discard
Related Posts Replies Views Activity
1
May 25
1031
0
Nov 24
1262
2
Oct 24
1275
0
Sep 24
943
0
Aug 24
1154