1

There is a subtotal (price_subtotal) field in Quotation Order Line.

I have added a new field extra_discount.

I have tried this code but to no avail.

@api.depends('product_uom_qty', 'price_unit', 'extra_discount')
    def compute_all(self):
        for record in self:
            record.price_subtotal = (record.product_uom_qty * record.price_unit) - record.extra_discount

It does nothing to the subtotal.

So, how do I make this happen?

holydragon
  • 6,158
  • 6
  • 39
  • 62
  • you've to use `update` method to update the current record....like `self.update{'price_subtotal': NEWVALUE}` – Adam Strauss Mar 11 '21 at 09:43
  • @AdamStrauss I tried adding it under the last line but it gave me `SyntaxError: invalid syntax`. – holydragon Mar 11 '21 at 09:48
  • write in loop `price_subtotal_n = (record.product_uom_qty * record.price_unit) - record.extra_discount` then in next line `record.update({'price_subtotal': price_subtotal_n})' – Adam Strauss Mar 11 '21 at 10:00
  • @AdamStrauss It does not have any errors now, but it does not affect the subtotal field either. It looks as if nothing happens. – holydragon Mar 11 '21 at 10:08
  • are you sure method is calling? if not add onchange decorator above in this method – Adam Strauss Mar 11 '21 at 10:09
  • @AdamStrauss you don't need to use `update` but just assign the values. So that is not the problem here. – CZoellner Mar 11 '21 at 10:14
  • @CZoellner I think we cant assign value to `price_subtotal` directly. – Adam Strauss Mar 11 '21 at 10:21
  • Ofcourse you can, you also can use `update` for example when assigning values to more than one field. Both ways are working, so that's not the problem here, but more something you've questioned already: is this new method called? – CZoellner Mar 11 '21 at 10:23
  • I don't know if it is actually called. How can I check that? Please also note that I am not even sure this `compute_all` is the right method to use either. – holydragon Mar 11 '21 at 10:24
  • I would try to override [this method](https://github.com/odoo/odoo/blob/f7dc4a4c8c6700cfecebffa1ef516e29a46aef61/addons/sale/models/sale.py#L1175-L1189). But i'm not sure if `api.depends()` extensions (adding one more recomputation trigger field) are working as expected. – CZoellner Mar 11 '21 at 10:30
  • @CZoellner That's it. It conceptually solves the problem. I made adjustments according to your suggestion, and it works like a charm now! Please convert it into an answer, and I will accept it right away. Thank you! – holydragon Mar 11 '21 at 10:50
  • @CZoellner Oh okay got it.... :) – Adam Strauss Mar 11 '21 at 11:25

1 Answers1

1

I would try to override the computation method behind the field sale.order.line.price_subtotal.

But i'm not sure if api.depends() extensions (adding one more recomputation trigger field) are working as expected.

But it should look something like that:

class SaleOrderLine(models.Model):
    _inherit = "sale.order.line"

    extra_discount = fields.Float()

    @api.depends('product_uom_qty', 'discount', 'price_unit',
                 'tax_id', 'extra_discount')
    def _compute_amount(self):
        """
        Compute the amounts of the SO line.

        Fully overridden to add field extra_discount to the
        formula and triggers.
        """
        for line in self:
            price = line.price_unit * (1 - (line.discount or 0.0) / 100.0)
            # new: substract extra_discount
            price -= line.extra_discount
            taxes = line.tax_id.compute_all(price, line.order_id.currency_id, line.product_uom_qty, product=line.product_id, partner=line.order_id.partner_shipping_id)
            line.update({
                'price_tax': sum(t.get('amount', 0.0) for t in taxes.get('taxes', [])),
                'price_total': taxes['total_included'],
                'price_subtotal': taxes['total_excluded'],
            })
            if self.env.context.get('import_file', False) and not self.env.user.user_has_groups('account.group_account_manager'):
                line.tax_id.invalidate_cache(['invoice_repartition_line_ids'], [line.tax_id.id])
CZoellner
  • 13,553
  • 3
  • 25
  • 38