From 56ef824d3960a371f4d56d84e90792b021a782ec Mon Sep 17 00:00:00 2001 From: "Julien (jula)" Date: Tue, 5 Dec 2023 15:56:19 +0100 Subject: [PATCH] [FIX] website_sale: set correct payment terms MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit __Current behavior before commit:__ When a sale order is created with a product added to the cart, `payment_term_id` is initially set by [sale_get_payment_term][1]. However, when the user sets their address, `payment_term_id` is then set to `partner_id.property_payment_term_id` due to the triggering of [_compute_payment_term_id][2]. This means that `payment_term_id` is computed differently if the user edits his address before confirming the order or not. (Resulting in it being `False` most of the time). __Description of the fix:__ Inherit [_compute_payment_term_id][2] to set `payment_term_id` using [sale_get_payment_term][1] from the **website_sale** module. __Steps to reproduce the issue on runbot:__ - Add a product to the cart through the website. → A new sale order will be created with payment terms set. - Proceed to checkout and edit the address on the website → The sale order payment terms will be removed opw-3492868 [1]: https://github.com/odoo/odoo/blob/1336ecf08210b04fb4b365d9e3a2bf8ae434de51/addons/website_sale/models/website.py#L215 [2]: https://github.com/odoo/odoo/blob/1336ecf08210b04fb4b365d9e3a2bf8ae434de51/addons/sale/models/sale_order.py#L518 closes odoo/odoo#145023 X-original-commit: 14f1168d1a27c04312a42f365ebfd24c0d07dba8 Signed-off-by: Julien Launois (jula) --- addons/website_sale/models/sale_order.py | 7 +++++ .../website_sale/tests/test_sale_process.py | 29 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/addons/website_sale/models/sale_order.py b/addons/website_sale/models/sale_order.py index 5be17a4118a..20a5145faad 100644 --- a/addons/website_sale/models/sale_order.py +++ b/addons/website_sale/models/sale_order.py @@ -101,6 +101,13 @@ class SaleOrder(models.Model): else: order.is_abandoned_cart = False + @api.depends('partner_id') + def _compute_payment_term_id(self): + super()._compute_payment_term_id() + for order in self: + if order.website_id: + order.payment_term_id = order.website_id.with_company(order.company_id).sale_get_payment_term(order.partner_id) + def _search_abandoned_cart(self, operator, value): website_ids = self.env['website'].search_read(fields=['id', 'cart_abandoned_delay', 'partner_id']) deadlines = [[ diff --git a/addons/website_sale/tests/test_sale_process.py b/addons/website_sale/tests/test_sale_process.py index 879d96e1f28..2fec09a3707 100644 --- a/addons/website_sale/tests/test_sale_process.py +++ b/addons/website_sale/tests/test_sale_process.py @@ -683,3 +683,32 @@ class TestWebsiteSaleCheckoutAddress(TransactionCaseWithUserDemo, HttpCaseWithUs }) invoice.action_post() self.assertFalse(partner_1._can_edit_name()) + + def test_11_payment_term_when_address_change(self): + ''' This test ensures that the payment term set when triggering + `onchange_partner_id` by changing the address of a website sale + order is computed by `sale_get_payment_term`. + ''' + self._setUp_multicompany_env() + product_id = self.env['product.product'].create({ + 'name': 'Product A', + 'list_price': 100, + 'website_published': True, + 'sale_ok': True}).id + + env = api.Environment(self.env.cr, self.portal_user.id, {}) + with MockRequest(env, website=self.website.with_env(env).with_context(website_id=self.website.id)) as req: + req.httprequest.method = "POST" + + self.WebsiteSaleController.cart_update(product_id) + so = self.portal_user.sale_order_ids[0] + self.assertTrue(so.payment_term_id, "A payment term should be set by default on the sale order") + + self.default_address_values['partner_id'] = self.portal_partner.id + self.default_address_values['name'] = self.portal_partner.name + self.WebsiteSaleController.address(**self.default_address_values) + self.assertTrue(so.payment_term_id, "A payment term should still be set on the sale order") + + so.website_id = False + self.WebsiteSaleController.address(**self.default_address_values) + self.assertFalse(so.payment_term_id, "The website default payment term should not be set on a sale order not coming from the website")