From 5456f198e304ef100c1f4caf62d339e1de3d5d44 Mon Sep 17 00:00:00 2001
From: "Walid HANNICHE (waha)" <waha@odoo.com>
Date: Fri, 7 Oct 2022 16:28:04 +0000
Subject: [PATCH] [FIX] sale_coupon: move global coupons to the end of the SO

Steps to reproduce:
 - Install website_sale_coupon
 - Load demo data
 - Create a promotion program that applies on the order
 - Go to the webshop and add 2 different products to the cart

Bug:
The line of promotion is in second place, instead of at the end.

Fix:
move the old sale order line to the end if it applies on all the order

opw-2985632

closes odoo/odoo#102700

Signed-off-by: William Braeckman (wbr) <wbr@odoo.com>
---
 addons/sale_coupon/models/sale_order.py       |  4 +++
 .../sale_coupon/tests/test_sale_invoicing.py  | 35 +++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/addons/sale_coupon/models/sale_order.py b/addons/sale_coupon/models/sale_order.py
index 26bde24ad1a0..97f924b0dd03 100644
--- a/addons/sale_coupon/models/sale_order.py
+++ b/addons/sale_coupon/models/sale_order.py
@@ -406,6 +406,10 @@ class SaleOrder(models.Model):
         def update_line(order, lines, values):
             '''Update the lines and return them if they should be deleted'''
             lines_to_remove = self.env['sale.order.line']
+            # move global coupons to the end of the SO
+            if program.discount_apply_on == 'on_order':
+                values['sequence'] = max(order.order_line.mapped('sequence')) + 1
+
             # Check commit 6bb42904a03 for next if/else
             # Remove reward line if price or qty equal to 0
             if values['product_uom_qty'] and values['price_unit']:
diff --git a/addons/sale_coupon/tests/test_sale_invoicing.py b/addons/sale_coupon/tests/test_sale_invoicing.py
index da995b2945ab..c793806658de 100644
--- a/addons/sale_coupon/tests/test_sale_invoicing.py
+++ b/addons/sale_coupon/tests/test_sale_invoicing.py
@@ -50,3 +50,38 @@ class TestSaleInvoicing(TestSaleCouponCommon):
         self.assertEqual(order.order_line, invoiceable_lines)
         account_move = order._create_invoices()
         self.assertEqual(len(account_move.invoice_line_ids), 2)
+
+    def test_coupon_on_order_sequence(self):
+        # discount_coupon_program
+        self.env['sale.coupon.program'].create({
+            'name': '10% Discount', # Default behavior
+            'program_type': 'coupon_program',
+            'reward_type': 'discount',
+            'discount_apply_on': 'on_order',
+            'promo_code_usage': 'no_code_needed',
+        })
+        order = self.empty_order
+
+        # orderline1
+        self.env['sale.order.line'].create({
+            'product_id': self.env.ref('product.product_product_6').id,
+            'name': 'largeCabinet',
+            'product_uom_qty': 1.0,
+            'order_id': order.id,
+        })
+
+        order.recompute_coupon_lines()
+        self.assertEqual(len(order.order_line), 2, 'Coupon correctly applied')
+
+        # orderline2
+        self.env['sale.order.line'].create({
+            'product_id': self.env.ref('product.product_product_11').id,
+            'name': 'conferenceChair',
+            'product_uom_qty': 1.0,
+            'order_id': order.id,
+        })
+
+        order.recompute_coupon_lines()
+        self.assertEqual(len(order.order_line), 3, 'Coupon correctly applied')
+
+        self.assertTrue(order.order_line.sorted(lambda x: x.sequence)[-1].is_reward_line, 'Global coupons appear on the last line')
-- 
GitLab