diff --git a/modules/web_shop_shopify/party.py b/modules/web_shop_shopify/party.py index 2fb40b3983..c603b27070 100644 --- a/modules/web_shop_shopify/party.py +++ b/modules/web_shop_shopify/party.py @@ -30,7 +30,8 @@ class Party(IdentifiersMixin, metaclass=PoolMeta): continue index = len(contact_mechanisms) for i, contact_mechanism in enumerate(contact_mechanisms): - if contact_mechanism.type in types: + if (contact_mechanism.type in types + and not contact_mechanism.address): index = min(i, index) if (contact_mechanism.value_compact == contact_mechanism.format_value_compact( @@ -49,13 +50,36 @@ class Party(IdentifiersMixin, metaclass=PoolMeta): def get_address_from_shopify(self, shopify_address): pool = Pool() Address = pool.get('party.address') + ContactMechanism = pool.get('party.contact_mechanism') shopify_values = Address.get_shopify_values(shopify_address) for address in self.addresses: if address.shopify_values() == shopify_values: - return address - address = Address(**shopify_values) - address.party = self - address.save() + break + else: + address = Address(**shopify_values) + address.party = self + address.save() + + contact_mechanisms = list(self.contact_mechanisms) + if phone := remove_forbidden_chars(shopify_address.phone): + index = len(contact_mechanisms) + for i, contact_mechanism in enumerate(contact_mechanisms): + if (contact_mechanism.type in ['phone', 'mobile'] + and contact_mechanism.address == address): + index = min(i, index) + if (contact_mechanism.value_compact + == contact_mechanism.format_value_compact( + phone, contact_mechanism.type)): + contact_mechanisms.insert( + index, + contact_mechanisms.pop(i)) + break + else: + contact_mechanisms.insert(index, ContactMechanism( + party=self, address=address, + type='phone', value=phone)) + ContactMechanism.save(sequence_reorder( + contact_mechanisms)) return address diff --git a/modules/web_shop_shopify/tests/scenario_web_shop_shopify.rst b/modules/web_shop_shopify/tests/scenario_web_shop_shopify.rst index 2684c2931d..f423cd8adf 100644 --- a/modules/web_shop_shopify/tests/scenario_web_shop_shopify.rst +++ b/modules/web_shop_shopify/tests/scenario_web_shop_shopify.rst @@ -438,10 +438,13 @@ Create an order on Shopify:: >>> customer_phone = '+32-495-555-' + ( ... ''.join(random.choice(string.digits) for _ in range(3))) >>> customer.phone = customer_phone + >>> customer_address_phone = '+32-495-555-' + ( + ... ''.join(random.choice(string.digits) for _ in range(3))) >>> customer.addresses = [{ ... 'address1': "Street", ... 'city': "City", ... 'country': "Belgium", + ... 'phone': customer_address_phone, ... }] >>> customer.save() True @@ -517,8 +520,13 @@ Run fetch order:: 'Customer' >>> assertTrue(sale.party.email) >>> assertEqual(sale.party.phone.replace(' ', ''), customer_phone.replace('-', '')) + >>> address, = sale.party.addresses + >>> address_contact_mechanism, = address.contact_mechanisms + >>> assertEqual( + ... address_contact_mechanism.value.replace(' ', ''), + ... customer_address_phone.replace('-', '')) >>> len(sale.party.contact_mechanisms) - 2 + 3 >>> assertTrue(sale.web_status_url) Capture full amount:: @@ -543,7 +551,7 @@ Capture full amount:: >>> len(sale.invoices) 0 >>> len(sale.party.contact_mechanisms) - 2 + 3 Make a partial shipment::