From 2f3926f899e32c1fb6584c39472a90a79c22067a Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sat, 14 Oct 2023 17:46:49 -0500 Subject: [PATCH] Include `person_uuid` when importing members from CORE DB otherwise can cause duplicate person records, e.g. if customers also are imported from CORE --- rattail_corepos/importing/corepos/db.py | 51 +++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/rattail_corepos/importing/corepos/db.py b/rattail_corepos/importing/corepos/db.py index f819c48..9eeb537 100644 --- a/rattail_corepos/importing/corepos/db.py +++ b/rattail_corepos/importing/corepos/db.py @@ -258,6 +258,7 @@ class MemberImporter(FromCOREPOS, corepos_importing.model.MemberImporter): 'number', 'corepos_card_number', 'customer_uuid', + 'person_uuid', 'person_first_name', 'person_last_name', 'membership_type_number', @@ -274,6 +275,54 @@ class MemberImporter(FromCOREPOS, corepos_importing.model.MemberImporter): model.Customer, key='number') + query = self.session.query(model.Person)\ + .outerjoin(model.Customer, + model.Customer.account_holder_uuid == model.Person.uuid)\ + .outerjoin(model.CoreCustomer)\ + .outerjoin(model.Member, + model.Member.person_uuid == model.Person.uuid)\ + .outerjoin(model.CoreMember)\ + .filter(sa.or_( + model.CoreCustomer.corepos_card_number != None, + model.CoreMember.corepos_card_number != None))\ + .options(orm.joinedload(model.Person.customer_accounts)\ + .joinedload(model.Customer._corepos)) + def card_number(person, normal): + customer = self.app.get_customer(person) + if customer and customer.corepos_card_number: + return customer.corepos_card_number + member = self.app.get_member(person) + if member and member.corepos_card_number: + return member.corepos_card_number + self.people_by_card_number = self.cache_model(model.Person, query=query, + key=card_number) + + def get_person(self, member): + if hasattr(self, 'people_by_card_number'): + return self.people_by_card_number.get(member.card_number) + + model = self.model + + try: + return self.session.query(model.Person)\ + .join(model.Customer, + model.Customer.account_holder_uuid == model.Person.uuid)\ + .join(model.CoreCustomer)\ + .filter(model.CoreCustomer.corepos_card_number == member.card_number)\ + .one() + except orm.exc.NoResultFound: + pass + + try: + return self.session.query(model.Person)\ + .join(model.Member, + model.Member.person_uuid == model.Person.uuid)\ + .join(model.CoreMember)\ + .filter(model.CoreMember.corepos_card_number == member.card_number)\ + .one() + except orm.exc.NoResultFound: + pass + def get_customer_by_number(self, number): if hasattr(self, 'customers_by_number'): return self.customers_by_number.get(number) @@ -298,6 +347,7 @@ class MemberImporter(FromCOREPOS, corepos_importing.model.MemberImporter): card_number = core_member.card_number customer = self.get_customer_by_number(card_number) + person = self.get_person(core_member) typeno = None if core_customer and core_customer.member_type: @@ -318,6 +368,7 @@ class MemberImporter(FromCOREPOS, corepos_importing.model.MemberImporter): 'number': card_number, 'corepos_card_number': card_number, 'customer_uuid': customer.uuid if customer else None, + 'person_uuid': person.uuid if person else None, 'person_first_name': core_contact.first_name, 'person_last_name': core_contact.last_name, 'membership_type_number': typeno,