Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
3 Odpowiedzi
2149 Widoki

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.!



Awatar
Odrzuć

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?

Najlepsza odpowiedź

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 

Awatar
Odrzuć
Autor

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

Najlepsza odpowiedź

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.

Awatar
Odrzuć
Autor Najlepsza odpowiedź

Hi,

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



Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
1
maj 25
1063
0
lis 24
1266
2
paź 24
1305
0
wrz 24
947
0
sie 24
1159