From 7e202c09508b4da860194a2bc8a67809370f11a5 Mon Sep 17 00:00:00 2001
From: Achraf <abz@odoo.com>
Date: Tue, 29 Aug 2023 14:34:08 +0200
Subject: [PATCH] [FIX] website_sale: Adapt compare_list_price widget currency
 to match list_price
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- Install `website_sale`
- Go to settings
- Enable 'Comparison Price'
- Go to products
- Open a random product
- Put a random `Compare to Price` (example $400 with a sales Price to $295)
- Click on smart button `Go To Website`

With $ 1 = 0.5 €
Before:

With `Public Pricelist`: `$ 300.00 ($ 400.00)`
With `EUR` pricelist: `150.00 € ($ 400.00)`

After:

With `Public Pricelist`: `$ 300.00 ($ 400.00)`
With `EUR` pricelist: `150.00 € (200.00 €)`

backport of https://github.com/odoo/odoo/pull/87134

closes odoo/odoo#133484

Signed-off-by: Achraf Ben Azzouz (abz) <abz@odoo.com>
---
 .../website_sale/models/product_template.py   | 11 ++++++++++
 ..._sale_shop_compare_list_price_pricelist.js |  9 ++++++++
 ...st_website_sale_show_compare_list_price.py | 21 +++++++++++++++++++
 addons/website_sale/views/templates.xml       | 16 +++++++-------
 4 files changed, 50 insertions(+), 7 deletions(-)

diff --git a/addons/website_sale/models/product_template.py b/addons/website_sale/models/product_template.py
index 3d17c08ea68f..e0fb9e4154fb 100644
--- a/addons/website_sale/models/product_template.py
+++ b/addons/website_sale/models/product_template.py
@@ -273,6 +273,12 @@ class ProductTemplate(models.Model):
                     fields.Date.today())
             has_discounted_price = pricelist.currency_id.compare_amounts(list_price, price) == 1
             prevent_zero_price_sale = not price and current_website.prevent_zero_price_sale
+
+            compare_list_price = self.compare_list_price
+            if pricelist and pricelist.currency_id != product.currency_id:
+                compare_list_price = self.currency_id._convert(self.compare_list_price, pricelist.currency_id, self.env.company,
+                                                  fields.Datetime.now(), round=False)
+
             combination_info.update(
                 base_unit_name=product.base_unit_name,
                 base_unit_price=product.base_unit_count and list_price / product.base_unit_count,
@@ -281,6 +287,7 @@ class ProductTemplate(models.Model):
                 price_extra=price_extra,
                 has_discounted_price=has_discounted_price,
                 prevent_zero_price_sale=prevent_zero_price_sale,
+                compare_list_price=compare_list_price
             )
 
         return combination_info
@@ -530,6 +537,10 @@ class ProductTemplate(models.Model):
             list_price = self.env['ir.qweb.field.monetary'].value_to_html(
                 combination_info['list_price'], monetary_options
             )
+        if combination_info['compare_list_price']:
+            list_price = self.env['ir.qweb.field.monetary'].value_to_html(
+                combination_info['compare_list_price'], monetary_options
+            )
 
         return price, list_price if combination_info['has_discounted_price'] else None
 
diff --git a/addons/website_sale/static/tests/tours/website_sale_shop_compare_list_price_pricelist.js b/addons/website_sale/static/tests/tours/website_sale_shop_compare_list_price_pricelist.js
index 8cc183458e41..9c27ac26c7c8 100644
--- a/addons/website_sale/static/tests/tours/website_sale_shop_compare_list_price_pricelist.js
+++ b/addons/website_sale/static/tests/tours/website_sale_shop_compare_list_price_pricelist.js
@@ -15,6 +15,15 @@ tour.register('compare_list_price_price_list_display', {
         tourUtils.assertProductPrice("price_reduce", "4,000", "test_product_with_pricelist_and_compare_list_price"),
         tourUtils.assertProductPrice("base_price",   "4,500", "test_product_with_pricelist_and_compare_list_price"),
 
+        ...tourUtils.selectPriceList('pricelist_other_currency'),
+
+        tourUtils.assertProductPrice("price_reduce", "2,000", "test_product_default"),
+        tourUtils.assertProductPrice("price_reduce", "4,000", "test_product_with_compare_list_price"),
+        tourUtils.assertProductPrice("base_price",   "2,500", "test_product_with_compare_list_price"),
+        tourUtils.assertProductPrice("price_reduce",   "4,000", "test_product_with_pricelist"),
+        tourUtils.assertProductPrice("price_reduce", "8,000", "test_product_with_pricelist_and_compare_list_price"),
+        tourUtils.assertProductPrice("base_price",   "4,500", "test_product_with_pricelist_and_compare_list_price"),
+
         ...tourUtils.selectPriceList('pricelist_with_discount'),
 
         tourUtils.assertProductPrice("price_reduce", "1,000", "test_product_default"),
diff --git a/addons/website_sale/tests/test_website_sale_show_compare_list_price.py b/addons/website_sale/tests/test_website_sale_show_compare_list_price.py
index a448bcaa3a96..6b377de08cf7 100644
--- a/addons/website_sale/tests/test_website_sale_show_compare_list_price.py
+++ b/addons/website_sale/tests/test_website_sale_show_compare_list_price.py
@@ -12,6 +12,8 @@ class WebsiteSaleShopPriceListCompareListPriceDispayTests(AccountTestInvoicingHt
         ProductTemplate = cls.env['product.template']
         Pricelist = cls.env['product.pricelist']
         PricelistItem = cls.env['product.pricelist.item']
+        Currency = cls.env['res.currency']
+        CurrencyRate = cls.env['res.currency.rate']
 
         # Cleanup existing pricelist.
         cls.env['website'].search([]).write({'sequence': 1000})
@@ -52,6 +54,17 @@ class WebsiteSaleShopPriceListCompareListPriceDispayTests(AccountTestInvoicingHt
             'company_id': cls.env.company.id,
         })
 
+        cls.test_custom_currency = Currency.create({
+            'name': "Test currency",
+            'symbol': 'A',
+        })
+
+        CurrencyRate.create({
+            'currency_id': cls.test_custom_currency.id,
+            'name': '2000-01-01',
+            'rate': 2.0,
+        })
+
         # Three pricelists
         Pricelist.search([]).write({'sequence': 1000})
         cls.pricelist_default = Pricelist.create({
@@ -77,6 +90,14 @@ class WebsiteSaleShopPriceListCompareListPriceDispayTests(AccountTestInvoicingHt
             'sequence': 3,
             'discount_policy': 'without_discount',
         })
+        cls.pricelist_other_currency = Pricelist.create({
+            'name': 'pricelist_other_currency',
+            'website_id': website.id,
+            'company_id': cls.env.company.id,
+            'selectable': True,
+            'sequence': 4,
+            'currency_id': cls.test_custom_currency.id,
+        })
 
         # Pricelist items
         PricelistItem.create({
diff --git a/addons/website_sale/views/templates.xml b/addons/website_sale/views/templates.xml
index cb0b6839bd6a..83f9d9146249 100644
--- a/addons/website_sale/views/templates.xml
+++ b/addons/website_sale/views/templates.xml
@@ -1155,18 +1155,20 @@
                 <span class="oe_price" style="white-space: nowrap;" t-esc="combination_info['price']" t-options="{'widget': 'monetary', 'display_currency': website.currency_id}"/>
                 <span itemprop="price" style="display:none;" t-esc="combination_info['price']"/>
                 <span itemprop="priceCurrency" style="display:none;" t-esc="website.currency_id.name"/>
-                <span t-attf-class="text-danger oe_default_price ms-1 h5 {{'' if combination_info['has_discounted_price'] else 'd-none'}}" style="text-decoration: line-through; white-space: nowrap;"
-                    t-esc="combination_info['list_price']" t-options="{'widget': 'monetary', 'display_currency': website.currency_id}"
+                <span t-attf-class="text-danger oe_default_price ms-1 h5 {{'' if combination_info['has_discounted_price'] and not combination_info['compare_list_price'] else 'd-none'}}"
+                    style="text-decoration: line-through; white-space: nowrap;"
+                    t-esc="combination_info['list_price']"
+                    t-options="{'widget': 'monetary', 'display_currency': website.currency_id}"
                     itemprop="listPrice"
                 />
                 <t t-if="is_view_active('website_sale.tax_indication')" t-call="website_sale.tax_indication"/>
-                <del t-if="product.compare_list_price">
+                <del t-if="combination_info['compare_list_price'] and (combination_info['compare_list_price'] &gt; combination_info['price'])">
                     <bdi dir="inherit">
-                    <span t-field="product.compare_list_price"
+                    <span t-esc="combination_info['compare_list_price']"
                         groups="website_sale.group_product_price_comparison"
                         t-options='{
                            "widget": "monetary",
-                           "display_currency": product.currency_id,
+                           "display_currency": website.currency_id,
                        }'/>
                     </bdi>
                 </del>
@@ -1178,13 +1180,13 @@
                        "display_currency": product.currency_id,
                 }'/>
                 <t t-if="is_view_active('website_sale.tax_indication')" t-call="website_sale.tax_indication"/>
-                <del t-if="product.compare_list_price">
+                <del t-if="combination_info['compare_list_price'] and (combination_info['compare_list_price'] &gt; combination_info['price'])">
                     <bdi dir="inherit">
                     <span t-field="product.compare_list_price"
                         groups="website_sale.group_product_price_comparison"
                         t-options='{
                             "widget": "monetary",
-                            "display_currency": product.currency_id,
+                            "display_currency": website.currency_id,
                         }'/>
                     </bdi>
                 </del>
-- 
GitLab