diff --git a/event_registration_partner_unique/README.rst b/event_registration_partner_unique/README.rst index 8a9272d05..03271544e 100644 --- a/event_registration_partner_unique/README.rst +++ b/event_registration_partner_unique/README.rst @@ -7,7 +7,7 @@ Unique Partner per Event !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:fa39a5416b9a53ff743d770b11ab214277fe38d04077dfa770174e259f481c29 + !! source digest: sha256:3d53fec9f6993c3a355d0cac4a4fc844e5290c7466ea731d4f6174c8486d9249 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png diff --git a/event_registration_partner_unique/__init__.py b/event_registration_partner_unique/__init__.py index 83e553ac4..0aa9b03c5 100644 --- a/event_registration_partner_unique/__init__.py +++ b/event_registration_partner_unique/__init__.py @@ -1,3 +1,4 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from . import models +from . import wizards diff --git a/event_registration_partner_unique/__manifest__.py b/event_registration_partner_unique/__manifest__.py index d589c8108..3075b93d3 100644 --- a/event_registration_partner_unique/__manifest__.py +++ b/event_registration_partner_unique/__manifest__.py @@ -14,5 +14,5 @@ "application": False, "installable": True, "depends": ["event", "partner_event"], - "data": ["views/event_event_view.xml"], + "data": ["views/event_event_view.xml", "wizards/base_partner_merge_views.xml"], } diff --git a/event_registration_partner_unique/models/event.py b/event_registration_partner_unique/models/event.py index 3ed3fa9ca..2bbe97d8f 100644 --- a/event_registration_partner_unique/models/event.py +++ b/event_registration_partner_unique/models/event.py @@ -18,6 +18,8 @@ class EventEvent(models.Model): @api.constrains("forbid_duplicates", "registration_ids") def _check_forbid_duplicates(self): """Ensure no duplicated attendee are found in the event.""" + if self.env.context.get("skip_registration_partner_unique"): + return return self.filtered( "forbid_duplicates" ).registration_ids._check_forbid_duplicates() @@ -29,9 +31,22 @@ class EventRegistration(models.Model): @api.constrains("event_id", "attendee_partner_id") def _check_forbid_duplicates(self): """Ensure no duplicated attendees are found in the event.""" + if self.env.context.get("skip_registration_partner_unique"): + return + dupes_to_unlink = self.browse() + # From the contact merge dialog we could choose to get rid of duplicated + # attendees linked to the merged partners. + unlink_dupes = self.env.context.get("merge_duplicated_registrations") for event_reg in self.filtered("event_id.forbid_duplicates"): - dupes = self.search(event_reg._duplicate_search_domain()) - if dupes: + dupes = self.search( + event_reg._duplicate_search_domain(), order="create_date desc" + ) + if not dupes: + continue + if unlink_dupes: + # Only the oldest one is kept + dupes_to_unlink += dupes[:-1] + else: raise ValidationError( _("Duplicated partners found in event %(name)s: %(partners)s.") % { @@ -42,6 +57,8 @@ def _check_forbid_duplicates(self): ), } ) + # Call with skipping context to avoid triggering the constraint again + dupes_to_unlink.with_context(skip_registration_partner_unique=True).unlink() def _duplicate_search_domain(self): """What to look for when searching duplicates.""" diff --git a/event_registration_partner_unique/static/description/index.html b/event_registration_partner_unique/static/description/index.html index e4cd2f28a..afbf0e16d 100644 --- a/event_registration_partner_unique/static/description/index.html +++ b/event_registration_partner_unique/static/description/index.html @@ -367,7 +367,7 @@
This module is intended for backend use only, and extends the functionality
diff --git a/event_registration_partner_unique/wizards/__init__.py b/event_registration_partner_unique/wizards/__init__.py
new file mode 100644
index 000000000..e3fc7010c
--- /dev/null
+++ b/event_registration_partner_unique/wizards/__init__.py
@@ -0,0 +1 @@
+from . import base_partner_merge
diff --git a/event_registration_partner_unique/wizards/base_partner_merge.py b/event_registration_partner_unique/wizards/base_partner_merge.py
new file mode 100644
index 000000000..21909b1d1
--- /dev/null
+++ b/event_registration_partner_unique/wizards/base_partner_merge.py
@@ -0,0 +1,21 @@
+# Copyright 2023 Tecnativa - David Vidal
+# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
+from odoo import fields, models
+
+
+class BasePartnerMergeAutomaticWizard(models.TransientModel):
+ _inherit = "base.partner.merge.automatic.wizard"
+
+ merge_duplicated_registrations = fields.Boolean(
+ help="If the merged partners were linked to registrations in an event that had "
+ "unique attendees flag we'll take only the oldest one",
+ )
+
+ def action_merge(self):
+ """Inject context so we can deal with duplicates when merging"""
+ if self.merge_duplicated_registrations:
+ return super(
+ BasePartnerMergeAutomaticWizard,
+ self.with_context(merge_registration_partner_unique=True),
+ ).action_merge()
+ return super().action_merge()
diff --git a/event_registration_partner_unique/wizards/base_partner_merge_views.xml b/event_registration_partner_unique/wizards/base_partner_merge_views.xml
new file mode 100644
index 000000000..d361e888b
--- /dev/null
+++ b/event_registration_partner_unique/wizards/base_partner_merge_views.xml
@@ -0,0 +1,12 @@
+
+