Ir al contenido
Menú
Se marcó esta pregunta
3 Respuestas
2151 Vistas

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
Descartar

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?

Mejor respuesta

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
Descartar
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

Mejor respuesta

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
Descartar
Autor Mejor respuesta

Hi,

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



Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
may 25
1065
0
nov 24
1268
2
oct 24
1307
0
sept 24
948
0
ago 24
1159