From f0f30828fc2786a2f13bc85ef322543e79c438ae Mon Sep 17 00:00:00 2001 From: Yannick Tivisse Date: Wed, 27 Oct 2021 16:43:29 +0200 Subject: [PATCH] [IMP] sale: Convert onchange_partner_shipping_id into compute method --- .../wizard/choose_delivery_carrier.py | 1 - addons/sale/models/sale_order.py | 15 +++++++++++---- .../wizard/project_create_sale_order.py | 1 - addons/website_sale/controllers/main.py | 1 - addons/website_sale/models/website.py | 8 +------- .../controllers/controllers.py | 1 - 6 files changed, 12 insertions(+), 15 deletions(-) diff --git a/addons/delivery_mondialrelay/wizard/choose_delivery_carrier.py b/addons/delivery_mondialrelay/wizard/choose_delivery_carrier.py index 70507e63822..89f3a5c2de3 100644 --- a/addons/delivery_mondialrelay/wizard/choose_delivery_carrier.py +++ b/addons/delivery_mondialrelay/wizard/choose_delivery_carrier.py @@ -56,6 +56,5 @@ class ChooseDeliveryCarrier(models.TransientModel): }) if partner_shipping != self.order_id.partner_shipping_id: self.order_id.partner_shipping_id = partner_shipping - self.order_id.onchange_partner_shipping_id() return super().button_confirm() diff --git a/addons/sale/models/sale_order.py b/addons/sale/models/sale_order.py index 07cacdc6464..dad902d7cb9 100644 --- a/addons/sale/models/sale_order.py +++ b/addons/sale/models/sale_order.py @@ -242,6 +242,7 @@ class SaleOrder(models.Model): fiscal_position_id = fields.Many2one( 'account.fiscal.position', string='Fiscal Position', domain="[('company_id', '=', company_id)]", check_company=True, + compute='_compute_fiscal_position_id', store=True, readonly=False, help="Fiscal positions are used to adapt taxes and accounts for particular customers or sales orders/invoices." "The default value comes from the customer.") tax_country_id = fields.Many2one( @@ -398,13 +399,19 @@ class SaleOrder(models.Model): return self.env.ref('sale.mt_order_sent') return super(SaleOrder, self)._track_subtype(init_values) - @api.onchange('partner_shipping_id', 'partner_id', 'company_id') - def onchange_partner_shipping_id(self): + @api.depends('partner_shipping_id', 'partner_id', 'company_id') + def _compute_fiscal_position_id(self): """ Trigger the change of fiscal position when the shipping address is modified. """ - self.fiscal_position_id = self.env['account.fiscal.position'].with_company(self.company_id).get_fiscal_position(self.partner_id.id, self.partner_shipping_id.id) - return {} + cache = {} + for order in self: + key = (order.company_id, order.partner_id, order.partner_shipping_id) + if key not in cache: + cache[key] = self.env['account.fiscal.position'].with_company( + order.company_id + )._get_fiscal_position(order.partner_id, order.partner_shipping_id) + order.fiscal_position_id = cache[key] @api.onchange('partner_id') def onchange_partner_id(self): diff --git a/addons/sale_timesheet/wizard/project_create_sale_order.py b/addons/sale_timesheet/wizard/project_create_sale_order.py index 82f36387ea5..7459f4cfd19 100644 --- a/addons/sale_timesheet/wizard/project_create_sale_order.py +++ b/addons/sale_timesheet/wizard/project_create_sale_order.py @@ -118,7 +118,6 @@ class ProjectCreateSalesOrder(models.TransientModel): 'company_id': self.project_id.company_id.id, }) sale_order.onchange_partner_id() - sale_order.onchange_partner_shipping_id() # rewrite the user as the onchange_partner_id erases it sale_order.write({'user_id': self.project_id.user_id.id}) sale_order.onchange_user_id() diff --git a/addons/website_sale/controllers/main.py b/addons/website_sale/controllers/main.py index e75441b12b1..507ac2552b1 100644 --- a/addons/website_sale/controllers/main.py +++ b/addons/website_sale/controllers/main.py @@ -925,7 +925,6 @@ class WebsiteSale(http.Controller): if redirection: return redirection - order.onchange_partner_shipping_id() order.order_line._compute_tax_id() request.session['sale_last_order_id'] = order.id request.website.sale_get_order(update_pricelist=True) diff --git a/addons/website_sale/models/website.py b/addons/website_sale/models/website.py index 6e1498ff934..8d2cf5a4f87 100644 --- a/addons/website_sale/models/website.py +++ b/addons/website_sale/models/website.py @@ -305,16 +305,11 @@ class Website(models.Model): sale_order = self.env['sale.order'].with_company(request.website.company_id.id).with_user(SUPERUSER_ID).create(so_data) # set fiscal position - if request.website.partner_id.id != partner.id: - sale_order.onchange_partner_shipping_id() - else: # For public user, fiscal position based on geolocation + if request.website.partner_id.id == partner.id: # For public user, fiscal position based on geolocation country_code = request.session['geoip'].get('country_code') if country_code: country_id = request.env['res.country'].search([('code', '=', country_code)], limit=1).id sale_order.fiscal_position_id = request.env['account.fiscal.position'].sudo().with_company(request.website.company_id.id)._get_fpos_by_region(country_id) - else: - # if no geolocation, use the public user fp - sale_order.onchange_partner_shipping_id() request.session['sale_order_id'] = sale_order.id @@ -336,7 +331,6 @@ class Website(models.Model): sale_order.write({'partner_id': partner.id}) sale_order.with_context(not_self_saleperson=True).onchange_partner_id() sale_order.write({'partner_invoice_id': partner.id}) - sale_order.onchange_partner_shipping_id() # fiscal position sale_order['payment_term_id'] = self.sale_get_payment_term(partner) # check the pricelist : update it if the pricelist is not the 'forced' one diff --git a/addons/website_sale_delivery_mondialrelay/controllers/controllers.py b/addons/website_sale_delivery_mondialrelay/controllers/controllers.py index 6b495769d9f..5f2178f7de4 100644 --- a/addons/website_sale_delivery_mondialrelay/controllers/controllers.py +++ b/addons/website_sale_delivery_mondialrelay/controllers/controllers.py @@ -31,7 +31,6 @@ class MondialRelay(http.Controller): }) if order.partner_shipping_id != partner_shipping: order.partner_shipping_id = partner_shipping - order.onchange_partner_shipping_id() return { 'address': request.env['ir.qweb']._render('website_sale.address_on_payment', {