[FIX] sale_coupon: unlink an empty set of order lines

This is possible that `unlink` is called without any record in self.
In the given case, this is because of
`self.env['sale.order.line'].search([('order_id', 'in', self.ids), ('is_delivery', '=', True)]).unlink()`
in the `undelivery_set` method of the module delivery.

`Unlink` on an empty set is supported, and it should therefore be the case here as well.
This is not because `self.order_id.no_code_promo_program_ids` expects a singleton
for the `self.order_id` part, and this is actually an empty set as `self` is an empty set.

Besides, it makes sense to loop on the lines passed to `self`,
as you could pass several lines, not all having the same
`product_id`, for the above line
`related_program = self.env['sale.coupon.program'].search([('discount_line_product_id', '=', self.product_id.id)])`

opw-746084
This commit is contained in:
Denis Ledoux
2017-06-07 17:25:12 +02:00
parent 803c148769
commit be8d0cf716
+5 -4
View File
@@ -273,10 +273,11 @@ class SaleOrderLine(models.Model):
coupons_to_reactivate.write({'state': 'new'})
line.order_id.applied_coupon_ids -= coupons_to_reactivate
# Remove the program from the order in the case it's still valid
related_program = self.env['sale.coupon.program'].search([('discount_line_product_id', '=', self.product_id.id)])
if related_program:
self.order_id.no_code_promo_program_ids -= related_program
self.order_id.code_promo_program_id -= related_program
for line in self:
related_program = self.env['sale.coupon.program'].search([('discount_line_product_id', '=', line.product_id.id)])
if related_program:
line.order_id.no_code_promo_program_ids -= related_program
line.order_id.code_promo_program_id -= related_program
orders = self.mapped('order_id')
res = super(SaleOrderLine, self).unlink()
orders._recompute_coupon_lines()