跳至内容
菜单
此问题已终结
3 回复
2152 查看

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



形象
丢弃

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?

最佳答案

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 

形象
丢弃
编写者

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

最佳答案

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.

形象
丢弃
编写者 最佳答案

Hi,

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



形象
丢弃
相关帖文 回复 查看 活动
1
5月 25
1066
0
11月 24
1268
2
10月 24
1307
0
9月 24
948
0
8月 24
1159