From 93e51c6cbbc762b44bb0044c975baa63300ca778 Mon Sep 17 00:00:00 2001 From: Yolann Sabaux Date: Wed, 2 Mar 2022 09:38:20 +0000 Subject: [PATCH] [FIX] website_event_booth_sale, website_event_sale: correct price Steps to reproduce: - Create a price list with different currency and discount with "show price and discount to the customer" - On the website select this pricelist and register for the event. Issue: The price in the cart is shown in the main currency Solution: [website_event_sale] There is an initial issue which when it calls '_compute_price_reduce'. We compare, 'product.lst_price' (in product.currency) and 'product.price' (which has been converted to the pricelist.currency). In order to compare apples with apples, a conversion is applied to have the 'product.lst_price' in the same currency. After that, we have kind of a coherent behaviour in the sense that 'ticket.price_reduce' is in the same currency as 'ticket.price'. Thereafter, a conversion is applied (if the pricelist.currency is different) to get the expected amount. The same reasoning is applied to [website_event_booth_sale]. Note: 'list_price' has been changed to 'lst_price' in Booth to have the same logic between Event and Booth opw-2766997 X-original-commit: ced49554dd7cca73a1da440607d545bad64e7af0 Part-of: odoo/odoo#107768 --- .../models/event_booth_category.py | 10 ++- .../models/sale_order_line.py | 7 +- addons/event_sale/models/event_ticket.py | 12 ++- addons/event_sale/models/sale_order.py | 16 ++-- .../tests/__init__.py | 1 + ...test_website_event_booth_sale_pricelist.py | 79 +++++++++++++++++++ .../views/event_booth_templates.xml | 20 +++-- addons/website_event_sale/tests/__init__.py | 2 + addons/website_event_sale/tests/common.py | 67 ++++++++++++++++ .../test_website_event_sale_pricelist.py | 69 ++++++++++++++++ .../views/website_event_templates.xml | 24 ++++-- 11 files changed, 282 insertions(+), 25 deletions(-) create mode 100644 addons/website_event_booth_sale/tests/test_website_event_booth_sale_pricelist.py create mode 100644 addons/website_event_sale/tests/common.py create mode 100644 addons/website_event_sale/tests/test_website_event_sale_pricelist.py diff --git a/addons/event_booth_sale/models/event_booth_category.py b/addons/event_booth_sale/models/event_booth_category.py index 76028fb7d98..ddf9aa1a17e 100644 --- a/addons/event_booth_sale/models/event_booth_category.py +++ b/addons/event_booth_sale/models/event_booth_category.py @@ -49,8 +49,14 @@ class EventBoothCategory(models.Model): def _compute_price_reduce(self): for category in self: product = category.product_id - list_price = product.list_price + product.price_extra - discount = (list_price - product._get_contextual_price()) / list_price if list_price else 0.0 + pricelist = product.product_tmpl_id._get_contextual_pricelist() + lst_price = product.currency_id._convert( + product.lst_price, + pricelist.currency_id, + self.env.company, + fields.Datetime.now() + ) + discount = (lst_price - product._get_contextual_price()) / lst_price if lst_price else 0.0 category.price_reduce = (1.0 - discount) * category.price @api.depends_context('pricelist', 'quantity') diff --git a/addons/event_booth_sale/models/sale_order_line.py b/addons/event_booth_sale/models/sale_order_line.py index bd3d3c88f1b..a25988791bc 100644 --- a/addons/event_booth_sale/models/sale_order_line.py +++ b/addons/event_booth_sale/models/sale_order_line.py @@ -94,7 +94,12 @@ class SaleOrderLine(models.Model): if self.event_booth_pending_ids and self.event_id: company = self.event_id.company_id or self.env.company currency = company.currency_id - total_price = sum([booth.price for booth in self.event_booth_pending_ids]) + product = self.event_booth_pending_ids.booth_category_id.product_id + pricelist = product.product_tmpl_id._get_contextual_pricelist() + if pricelist.discount_policy == "with_discount": + total_price = sum([booth.booth_category_id.price_reduce for booth in self.event_booth_pending_ids]) + else: + total_price = sum([booth.price for booth in self.event_booth_pending_ids]) return currency._convert( total_price, self.order_id.currency_id, self.order_id.company_id or self.env.company.id, diff --git a/addons/event_sale/models/event_ticket.py b/addons/event_sale/models/event_ticket.py index 542b6b362b5..415c647ef3e 100644 --- a/addons/event_sale/models/event_ticket.py +++ b/addons/event_sale/models/event_ticket.py @@ -54,10 +54,14 @@ class EventTemplateTicket(models.Model): def _compute_price_reduce(self): for ticket in self: product = ticket.product_id - # seems strange to not apply pricelist logic but still use pricelist discount... - discount = ( - product.lst_price - product._get_contextual_price() - ) / product.lst_price if product.lst_price else 0.0 + pricelist = product.product_tmpl_id._get_contextual_pricelist() + lst_price = product.currency_id._convert( + product.lst_price, + pricelist.currency_id, + self.env.company, + fields.Datetime.now() + ) + discount = (lst_price - product._get_contextual_price()) / lst_price if lst_price else 0.0 ticket.price_reduce = (1.0 - discount) * ticket.price def _init_column(self, column_name): diff --git a/addons/event_sale/models/sale_order.py b/addons/event_sale/models/sale_order.py index 879b56916ba..05c9ffa4b27 100644 --- a/addons/event_sale/models/sale_order.py +++ b/addons/event_sale/models/sale_order.py @@ -172,9 +172,15 @@ class SaleOrderLine(models.Model): pricelist=self.order_id.pricelist_id.id, uom=self.product_uom.id ) - if self.order_id.pricelist_id.discount_policy == 'with_discount': - return event_ticket.price_reduce + company = event_ticket.company_id or self.env.company + currency = company.currency_id + pricelist = self.order_id.pricelist_id + if pricelist.discount_policy == "with_discount": + price = event_ticket.price_reduce else: - return event_ticket.price - else: - return super()._get_display_price() + price = event_ticket.price + return currency._convert( + price, self.order_id.currency_id, + self.order_id.company_id or self.env.company.id, + self.order_id.date_order or fields.Date.today()) + return super()._get_display_price() diff --git a/addons/website_event_booth_sale/tests/__init__.py b/addons/website_event_booth_sale/tests/__init__.py index 63f5c3df649..3fa0f695185 100644 --- a/addons/website_event_booth_sale/tests/__init__.py +++ b/addons/website_event_booth_sale/tests/__init__.py @@ -2,3 +2,4 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. from . import test_event_booth_sale +from . import test_website_event_booth_sale_pricelist diff --git a/addons/website_event_booth_sale/tests/test_website_event_booth_sale_pricelist.py b/addons/website_event_booth_sale/tests/test_website_event_booth_sale_pricelist.py new file mode 100644 index 00000000000..b2d86cd7e08 --- /dev/null +++ b/addons/website_event_booth_sale/tests/test_website_event_booth_sale_pricelist.py @@ -0,0 +1,79 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo.addons.event_booth_sale.tests.common import TestEventBoothSaleCommon +from odoo.addons.website_event_sale.tests.common import TestWebsiteEventSaleCommon +from odoo.addons.website_sale.controllers.main import WebsiteSale +from odoo.addons.website.tools import MockRequest +from odoo.tests import tagged + + +@tagged('post_install', '-at_install') +class TestWebsiteBoothPriceList(TestEventBoothSaleCommon, TestWebsiteEventSaleCommon): + + @classmethod + def setUpClass(cls): + super(TestWebsiteBoothPriceList, cls).setUpClass() + + cls.WebsiteSaleController = WebsiteSale() + cls.booth_1 = cls.env['event.booth'].create({ + 'booth_category_id': cls.event_booth_category_1.id, + 'event_id': cls.event.id, + 'name': 'Test Booth 1', + }) + + cls.booth_2 = cls.env['event.booth'].create({ + 'booth_category_id': cls.event_booth_category_1.id, + 'event_id': cls.event.id, + 'name': 'Test Booth 2', + }) + + def test_pricelist_different_currency(self): + so_line = self.env['sale.order.line'].create({ + 'event_booth_category_id': self.event_booth_category_1.id, + 'event_booth_pending_ids': (self.booth_1 + self.booth_2).ids, + 'event_id': self.event.id, + 'order_id': self.so.id, + 'product_id': self.event_booth_product.id, + }) + # set pricelist to 0 - currency: company + self.pricelist.write({ + 'currency_id': self.new_company.currency_id.id, + 'discount_policy': 'with_discount', + 'item_ids': [(5, 0, 0), (0, 0, { + 'applied_on': '3_global', + 'compute_price': 'percentage', + 'percent_price': 0, + })], + 'name': 'With Discount Included', + }) + with MockRequest(self.env, sale_order_id=self.so.id, website=self.current_website): + self.WebsiteSaleController.pricelist(promo=None) + self.so._cart_update(line_id=so_line.id, product_id=self.event_booth_product.id, set_qty=1) + self.assertEqual(so_line.price_reduce, 40) + + # set pricelist to 10% - without discount + self.pricelist.write({ + 'currency_id': self.currency_test.id, + 'discount_policy': 'without_discount', + 'item_ids': [(5, 0, 0), (0, 0, { + 'applied_on': '3_global', + 'compute_price': 'percentage', + 'percent_price': 10, + })], + 'name': 'Without Discount Included', + }) + with MockRequest(self.env, sale_order_id=self.so.id, website=self.current_website): + self.WebsiteSaleController.pricelist(promo=None) + self.so._cart_update(line_id=so_line.id, product_id=self.event_booth_product.id, set_qty=1) + self.assertEqual(so_line.price_reduce, 360, 'Incorrect amount based on the pricelist "Without Discount" and its currency.') + + # set pricelist to 10% - with discount + self.pricelist.write({ + 'discount_policy': 'with_discount', + 'name': 'With Discount Included', + }) + with MockRequest(self.env, sale_order_id=self.so.id, website=self.current_website): + self.WebsiteSaleController.pricelist(promo=None) + self.so._cart_update(line_id=so_line.id, product_id=self.event_booth_product.id, set_qty=1) + self.assertEqual(so_line.price_reduce, 360, 'Incorrect amount based on the pricelist "With Discount" and its currency.') diff --git a/addons/website_event_booth_sale/views/event_booth_templates.xml b/addons/website_event_booth_sale/views/event_booth_templates.xml index 6ae8553c4d2..7b17b7220d0 100644 --- a/addons/website_event_booth_sale/views/event_booth_templates.xml +++ b/addons/website_event_booth_sale/views/event_booth_templates.xml @@ -4,19 +4,29 @@