From be8d0cf716833fd21400e8733d2af8d640af1790 Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Wed, 7 Jun 2017 17:20:54 +0200 Subject: [PATCH] [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 --- models/sale_order.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/models/sale_order.py b/models/sale_order.py index cba25817132..667489cfd47 100644 --- a/models/sale_order.py +++ b/models/sale_order.py @@ -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()