[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 <sig@odoo.com>
Signed-off-by: Minet Adrien (admi) <admi@odoo.com>
This commit is contained in:
Adrien Minet
2022-02-28 12:55:52 +00:00
committed by Raphael Collet
parent bc9536ebe9
commit cf8cc3b47f
@@ -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: