From 25f39f4173dbbfc0b855e902eafe3d3dd4650302 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sun, 24 Jul 2022 12:05:05 -0500 Subject: [PATCH] Add basic/minimal merge support for customers --- tailbone/views/customers.py | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tailbone/views/customers.py b/tailbone/views/customers.py index d061701f..693cd323 100644 --- a/tailbone/views/customers.py +++ b/tailbone/views/customers.py @@ -93,6 +93,18 @@ class CustomerView(MasterView): 'members', ] + mergeable = True + + merge_coalesce_fields = [ + 'email_addresses', + 'phone_numbers', + ] + + merge_fields = merge_coalesce_fields + [ + 'uuid', + 'name', + ] + def get_expose_active_in_pos(self): if not hasattr(self, '_expose_active_in_pos'): self._expose_active_in_pos = self.rattail_config.getbool( @@ -444,6 +456,37 @@ class CustomerView(MasterView): return self.redirect(self.request.get_referrer()) + def get_merge_data(self, customer): + return { + 'uuid': customer.uuid, + 'name': customer.name, + 'email_addresses': [e.address for e in customer.emails], + 'phone_numbers': [p.number for p in customer.phones], + } + + def merge_objects(self, removing, keeping): + coalesce = self.get_merge_coalesce_fields() + if coalesce: + + if 'email_addresses' in coalesce: + keeping_emails = [e.address for e in keeping.emails] + for email in removing.emails: + if email.address not in keeping_emails: + keeping.add_email(address=email.address, + type=email.type, + invalid=email.invalid) + keeping_emails.append(email.address) + + if 'phone_numbers' in coalesce: + keeping_phones = [e.number for e in keeping.phones] + for phone in removing.phones: + if phone.number not in keeping_phones: + keeping.add_phone(number=phone.number, + type=phone.type) + keeping_phones.append(phone.number) + + self.Session.delete(removing) + def configure_get_simple_settings(self): return [