From cf8cc3b47f8cfa85357f801d0e5566618bb014b2 Mon Sep 17 00:00:00 2001 From: Adrien Minet Date: Mon, 6 Dec 2021 11:23:34 +0000 Subject: [PATCH] [FIX] base: sum of company-dependent fields in partner merge wizard How to reproduce the bug: - Install POS and data_cleaning - Create 2 customers and set loyalty points - Select both customers and merge them - Once the merge is done, the loyalty points have not been summed Bug: When you merge 2 contacts that have both loyalty points, the resulting contact won't have the sum of the 2 source contacts. Instead of that, the resulting contact will only have the points of one of of the contact. closes odoo/odoo#84739 Opw: 2686208 X-original-commit: 56efd70e5c0e10302990d14730d801d3720f630e Related: odoo/enterprise#24445 Signed-off-by: Simon Goffin Signed-off-by: Minet Adrien (admi) --- odoo/addons/base/wizard/base_partner_merge.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/odoo/addons/base/wizard/base_partner_merge.py b/odoo/addons/base/wizard/base_partner_merge.py index 63023d74a21..23e64b272bc 100644 --- a/odoo/addons/base/wizard/base_partner_merge.py +++ b/odoo/addons/base/wizard/base_partner_merge.py @@ -2,6 +2,7 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. from ast import literal_eval +from collections import defaultdict import functools import itertools import logging @@ -250,8 +251,10 @@ class MergePartnerAutomatic(models.TransientModel): return item.id else: return item + # get all fields that are not computed or x2many values = dict() + values_by_company = defaultdict(dict) # {company: vals} for column in model_fields: field = dst_partner._fields[column] if field.type not in ('many2many', 'one2many') and field.compute is None: @@ -261,10 +264,21 @@ class MergePartnerAutomatic(models.TransientModel): values[column] += write_serializer(item[column]) else: values[column] = write_serializer(item[column]) + elif field.company_dependent and column in summable_fields: + # sum the values of partners for each company; use sudo() to + # compute the sum on all companies, including forbidden ones + partners = (src_partners + dst_partner).sudo() + for company in self.env['res.company'].sudo().search([]): + values_by_company[company][column] = sum( + partners.with_company(company).mapped(column) + ) + # remove fields that can not be updated (id and parent_id) values.pop('id', None) parent_id = values.pop('parent_id', None) dst_partner.write(values) + for company, vals in values_by_company.items(): + dst_partner.with_company(company).sudo().write(vals) # try to update the parent_id if parent_id and parent_id != dst_partner.id: try: