Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
3 Replies
2145 Tampilan

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
Buang

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?

Jawaban Terbai

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
Buang
Penulis

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

Jawaban Terbai

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
Buang
Penulis Jawaban Terbai

Hi,

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



Avatar
Buang
Post Terkait Replies Tampilan Aktivitas
1
Mei 25
1058
0
Nov 24
1265
2
Okt 24
1303
0
Sep 24
947
0
Agu 24
1159