How to update discount amount in a custom method in sale?

Hi,

I’m working on a RPC method to update discounts of sale lines.

I am working with sale_discount module and reading the rst file of tests [0], especially the code:

>>> line.discount_amount = Decimal('3.3333')
>>> line.unit_price
Decimal('6.6667')
>>> line.discount_rate
Decimal('0.3333')
>>> line.discount
'$3.3333'

The RPC method is the following but doesn’t work:

    @classmethod
    def update_line_discount(cls, lines, discount=None):
        if discount and discount > _ZERO:

            for line in lines:
                line.discount_amount = Decimal(discount)
                line.save()

I tried with the following method too but doesn’t work neither:

    @classmethod
    def update_line_discount(cls, lines, discount=None):
        # TODO: Add compute with context sale price list
        pool = Pool()
        SaleLine = pool.get('sale.line')

        if discount and discount > _ZERO:

            for line in lines:
                SaleLine.write([line], {'discount_amount': Decimal(discount)})

Any help is welcome.

[0] sale_discount/scenario_sale_discount.rst at 5.8 · tryton/sale_discount · GitHub

discount_amount is a Function field that does not store anything.
You must update the unit_price field directly or call the on_change_discount_amount after changing discount_amount.

1 Like

It works, thanks for your help.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.