Hello,
i had to override the create function in sale order line model to create a bonus sale order line depending on a certain condition.
the problem is when i duplicate a sale order it creates two bonus sale order lines not only one.
is there a way to avoid that? or should i find another way to create the bonus line
here's my code:
class PricelistItem(models.Model):
_inherit = "product.pricelist.item"
compute_price = fields.Selection([
('fixed', 'Fixed Price'),
('percentage', 'Percentage (discount)'),
('formula', 'Formula'),
('bonus', 'Bonus')], index=True, default='fixed', required=True)
ordered_quantity = fields.Float('Ordered Quantity', digits='Product Unit of Measure', default=0)
bonus_quantity = fields.Float('Bonus Quantity', digits='Product Unit of Measure', default=0)
class CustomSaleOrderLine(models.Model):
_inherit = "sale.order.line"
active = fields.Boolean(default=True, readonly=True)
ordered_quantity = fields.Float('Ordered Quantity', digits='Product Unit of Measure', readonly=True, default=0)
bonus_quantity = fields.Float('Bonus Quantity', digits='Product Unit of Measure', readonly=True, default=0)
@api.model
def create(self, vals):
lines = super(CustomSaleOrderLine, self).create(vals)
PricelistItem = self.env['product.pricelist.item']
for line in lines:
product_context = dict(line.env.context, partner_id=line.order_id.partner_id.id, date=line.order_id.date_order,
uom=line.product_uom.id)
final_price, rule_id = line.order_id.pricelist_id.with_context(product_context).get_product_price_rule(line.product_id, line.product_uom_qty or 1.0, line.order_id.partner_id)
if rule_id:
pricelist_item = PricelistItem.browse(rule_id)
if pricelist_item.compute_price == 'bonus':
if line.product_uom_qty >= pricelist_item.ordered_quantity:
line.bonus_quantity = pricelist_item.bonus_quantity
sol = self.env['sale.order.line'].create({
'name': line.product_id.name,
'product_id': line.product_id.id,
'product_uom_qty': pricelist_item.bonus_quantity,
'product_uom': line.product_id.uom_id.id,
'price_unit': 0,
'active': False,
'order_id': line.order_id.id,
})
return lines
thanks
will be nice if you add the codes along with question