
from odoo import api, models


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

    @api.onchange('product_id', 'product_uom_qty')
    def _onchange_product_id_sale_commission_pricelist(self):
        self.ensure_one()
        if self.product_id and self.order_id.pricelist_id:
            rule_id = self.order_id.pricelist_id.get_product_price_rule(
                product=self.product_id,
                quantity=self.product_uom_qty or 1.0,
                partner=self.order_id.partner_id,
                date=self.order_id.date_order,
                uom_id=self.product_uom.id)[1]
            rule = self.env['product.pricelist.item'].browse(rule_id)
            if rule.commission_id:
                self.agents.update({
                    'commission': rule.commission_id.id,
                })


from odoo import api, models


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

    def _get_commission_from_pricelist(self):
        self.ensure_one()
        if not self.product_id or not self.order_id.pricelist_id:
            return False
        rule_id = self.order_id.pricelist_id.get_product_price_rule(
            product=self.product_id,
            quantity=self.product_uom_qty or 1.0,
            partner=self.order_id.partner_id,
            date=self.order_id.date_order,
            uom_id=self.product_uom.id)[1]
        rule = self.env['product.pricelist.item'].browse(rule_id)
        return rule.commission_id

    @api.onchange('product_id', 'product_uom_qty')
    def _onchange_product_id_sale_commission_pricelist(self):
        commission = self._get_commission_from_pricelist()
        if commission:
            self.agents.update({
                'commission': commission.id,
            })

    def _prepare_agents_vals(self):
        self.ensure_one()
        res = super(SaleOrderLine, self)._prepare_agents_vals()
        commission = self._get_commission_from_pricelist()
        if commission:
            for vals in res:
                vals['commission'] = commission.id
        return res

Make this to work on button recompute
[FIX] sale_commission_pricelist: Make this to work on button recompute

  
  from odoo import api, models
  
  
  class SaleOrderLine(models.Model):
      _inherit = 'sale.order.line'
  
+     def _get_commission_from_pricelist(self):
+         self.ensure_one()
+         if not self.product_id or not self.order_id.pricelist_id:
+             return False
+         rule_id = self.order_id.pricelist_id.get_product_price_rule(
+             product=self.product_id,
+             quantity=self.product_uom_qty or 1.0,
+             partner=self.order_id.partner_id,
+             date=self.order_id.date_order,
+             uom_id=self.product_uom.id)[1]
+         rule = self.env['product.pricelist.item'].browse(rule_id)
+         return rule.commission_id
+ 
      @api.onchange('product_id', 'product_uom_qty')
      def _onchange_product_id_sale_commission_pricelist(self):
+         commission = self._get_commission_from_pricelist()
+         if commission:
+             self.agents.update({
+                 'commission': commission.id,
+             })
+ 
+     def _prepare_agents_vals(self):
          self.ensure_one()
+         res = super(SaleOrderLine, self)._prepare_agents_vals()
+         commission = self._get_commission_from_pricelist()
-         if self.product_id and self.order_id.pricelist_id:
-             rule_id = self.order_id.pricelist_id.get_product_price_rule(
-                 product=self.product_id,
-                 quantity=self.product_uom_qty or 1.0,
-                 partner=self.order_id.partner_id,
-                 date=self.order_id.date_order,
-                 uom_id=self.product_uom.id)[1]
-             rule = self.env['product.pricelist.item'].browse(rule_id)
-             if rule.commission_id:
+         if commission:
-                 self.agents.update({
+             for vals in res:
-                     'commission': rule.commission_id.id,
+                 vals['commission'] = commission.id
-                 })
+         return res
  
Make this to work on button recompute
## Code Before:

from odoo import api, models


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

    @api.onchange('product_id', 'product_uom_qty')
    def _onchange_product_id_sale_commission_pricelist(self):
        self.ensure_one()
        if self.product_id and self.order_id.pricelist_id:
            rule_id = self.order_id.pricelist_id.get_product_price_rule(
                product=self.product_id,
                quantity=self.product_uom_qty or 1.0,
                partner=self.order_id.partner_id,
                date=self.order_id.date_order,
                uom_id=self.product_uom.id)[1]
            rule = self.env['product.pricelist.item'].browse(rule_id)
            if rule.commission_id:
                self.agents.update({
                    'commission': rule.commission_id.id,
                })

## Instruction:
Make this to work on button recompute
## Code After:

from odoo import api, models


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

    def _get_commission_from_pricelist(self):
        self.ensure_one()
        if not self.product_id or not self.order_id.pricelist_id:
            return False
        rule_id = self.order_id.pricelist_id.get_product_price_rule(
            product=self.product_id,
            quantity=self.product_uom_qty or 1.0,
            partner=self.order_id.partner_id,
            date=self.order_id.date_order,
            uom_id=self.product_uom.id)[1]
        rule = self.env['product.pricelist.item'].browse(rule_id)
        return rule.commission_id

    @api.onchange('product_id', 'product_uom_qty')
    def _onchange_product_id_sale_commission_pricelist(self):
        commission = self._get_commission_from_pricelist()
        if commission:
            self.agents.update({
                'commission': commission.id,
            })

    def _prepare_agents_vals(self):
        self.ensure_one()
        res = super(SaleOrderLine, self)._prepare_agents_vals()
        commission = self._get_commission_from_pricelist()
        if commission:
            for vals in res:
                vals['commission'] = commission.id
        return res
