Exemplo n.º 1
0
    def find_or_create_using_magento_id(self, cursor, user, magento_id,
                                        context):
        """
        Finds or creates partner using magento ID

        :param cursor: Database cursor
        :param user: ID of current user
        :param magento_id: Partner ID sent by magento
        :param context: Application context.
        :return: Browse record of record created/found
        """

        instance_obj = self.pool.get('magento.instance')
        partner = self.find_using_magento_id(cursor, user, magento_id, context)

        if not partner:
            instance = instance_obj.browse(cursor,
                                           user,
                                           context['magento_instance'],
                                           context=context)

            with magento.Customer(instance.url, instance.api_user,
                                  instance.api_key) as customer_api:
                customer_data = customer_api.info(magento_id)

            partner = self.create_using_magento_data(cursor, user,
                                                     customer_data, context)
        return partner
Exemplo n.º 2
0
    def test0010_create_partner(self):
        """
        Tests if customers imported from magento is created as partners
        in openerp
        """
        partner_obj = POOL.get('res.partner')
        magento_partner_obj = POOL.get('magento.website.partner')

        with Transaction().start(DB_NAME, USER, CONTEXT) as txn:

            self.setup_defaults(txn)

            context = deepcopy(CONTEXT)
            context.update({
                'magento_website': self.website_id1,
                'magento_store_view': self.store_view_id,
            })

            if settings.MOCK:
                customer_data = load_json('customers', '1')
            else:
                with magento.Order(*settings.ARGS) as order_api:
                    orders = order_api.list()
                    order_data = order_api.info(orders[0]['increment_id'])
                with magento.Customer(*settings.ARGS) as customer_api:
                    if order_data.get('customer_id'):
                        customer_data = customer_api.info(
                            order_data['customer_id']
                        )
                    else:
                        customer_data = {
                            'firstname': order_data['customer_firstname'],
                            'lastname': order_data['customer_lastname'],
                            'email': order_data['customer_email'],
                            'magento_id': 0
                        }

            partners_before_import = magento_partner_obj.search(
                txn.cursor, txn.user, [], context=context
            )

            # Create partner
            partner = partner_obj.find_or_create(
                txn.cursor, txn.user, customer_data, context
            )
            self.assert_(partner)

            self.assertTrue(
                partner_obj.search(
                    txn.cursor, txn.user, [
                        ('email', '=', customer_data['email'])
                    ], context=context
                )
            )
            partners_after_import = magento_partner_obj.search(
                txn.cursor, txn.user, [], context=context
            )

            self.assertTrue(partners_after_import > partners_before_import)
Exemplo n.º 3
0
    def find_or_create_using_magento_id(cls, magento_id):
        """
        This method tries to find the party with the magento ID first and
        if not found it will fetch the info from magento and create a new
        party with the data from magento using create_using_magento_data

        :param magento_id: Party ID sent by magento
        :return: Active record of record created/found
        """
        Channel = Pool().get('sale.channel')

        channel = Channel.get_current_magento_channel()

        party = cls.find_using_magento_id(magento_id)
        if not party:
            with magento.Customer(channel.magento_url,
                                  channel.magento_api_user,
                                  channel.magento_api_key) as customer_api:
                customer_data = customer_api.info(magento_id)

            party = cls.create_using_magento_data(customer_data)
        return party
Exemplo n.º 4
0
    def find_or_create_using_magento_id(cls, magento_id):
        """
        This method tries to find the party with the magento ID first and
        if not found it will fetch the info from magento and create a new
        party with the data from magento using create_using_magento_data

        :param magento_id: Party ID sent by magento
        :return: Active record of record created/found
        """
        Instance = Pool().get('magento.instance')

        party = cls.find_using_magento_id(magento_id)
        if not party:
            instance = Instance(Transaction().context.get('magento_instance'))

            with magento.Customer(
                instance.url, instance.api_user, instance.api_key
            ) as customer_api:
                customer_data = customer_api.info(magento_id)

            party = cls.create_using_magento_data(customer_data)
        return party
Exemplo n.º 5
0
    def import_customers(self, cursor, user, website, context):
        """
        Imports products for current instance

        :param cursor: Database cursor
        :param user: ID of current user
        :param website: Browse record of website
        :param context: Application context
        :return: List of product IDs
        """
        partner_obj = self.pool.get('res.partner')

        instance = website.instance

        with magento.Customer(instance.url, instance.api_user,
                              instance.api_key) as customer_api:
            mag_customers = []
            customers = []

            # Products are linked to websites. But the magento api filters
            # the products based on store views. The products available on
            # website are always available on all of its store views.
            # So we get one store view for each website in current instance.
            mag_customers.extend(
                customer_api.list(website.stores[0].store_views[0].magento_id))
            context.update({
                'magento_website': website.id,
                'website': website.id,
                'magento_instance': website.instance.id
            })

            for mag_customer in mag_customers:
                partner_exist = partner_obj.find_using_magento_id(
                    cursor, user, mag_customer['customer_id'], context)

                if not partner_exist:
                    partner = partner_obj.find_or_create_using_magento_id(
                        cursor,
                        user,
                        mag_customer['customer_id'],
                        context,
                    )
                    customers.append(partner)

                    customer_details = customer_api.info(
                        mag_customer['customer_id'])
                    address_details = customer_details['billing_address']

                    country_obj = self.pool.get('res.country')
                    state_obj = self.pool.get('res.country.state')
                    #print "------------> %s" %(address_details['country_id'])
                    country = country_obj.search_using_magento_code(
                        cursor, user, address_details['country_id'], context)
                    if address_details.get('region', False):
                        state_id = state_obj.find_or_create_using_magento_region(
                            cursor, user, country, address_details['region'],
                            context).id
                    else:
                        state_id = None

                    self.pool.get('res.partner').write(
                        cursor, user, [partner.id], {
                            'street':
                            address_details.get('street', False),
                            'state_id':
                            state_id,
                            'country_id':
                            country.id,
                            'city':
                            address_details.get('city', False),
                            'zip':
                            address_details.get('postcode', False),
                            'phone':
                            address_details.get('telephone', False),
                            'fax':
                            address_details.get('fax', False),
                            'company_name_ept':
                            address_details.get('company', False)
                        })


#
#                 partner_invoice_address = \
#                 partner_obj.find_or_create_address_as_partner_using_magento_data(
#                     cursor, user, customer_details['billing_address'], partner, context
#                 )
#
#                 partner_shipping_address = \
#                     partner_obj.find_or_create_address_as_partner_using_magento_data(
#                         cursor, user, customer_details['shipping_address'], partner, context
#                     )
        return map(int, customers)
Exemplo n.º 6
0
    def test0020_create_partner_for_same_website(self):
        """
        Tests that partners should be unique in a website
        """
        partner_obj = POOL.get('res.partner')
        magento_partner_obj = POOL.get('magento.website.partner')

        with Transaction().start(DB_NAME, USER, CONTEXT) as txn:

            self.setup_defaults(txn)

            context = deepcopy(CONTEXT)
            context.update({
                'magento_website': self.website_id1,
                'magento_store_view': self.store_view_id,
            })

            initial_partners = magento_partner_obj.search(
                txn.cursor, txn.user, [], context=context
            )

            if settings.MOCK:
                customer_data = load_json('customers', '1')
            else:
                with magento.Order(*settings.ARGS) as order_api:
                    orders = order_api.list()
                    order_data = order_api.info(orders[0]['increment_id'])
                with magento.Customer(*settings.ARGS) as customer_api:
                    if order_data.get('customer_id'):
                        customer_data = customer_api.info(
                            order_data['customer_id']
                        )
                    else:
                        customer_data = {
                            'firstname': order_data['customer_firstname'],
                            'lastname': order_data['customer_lastname'],
                            'email': order_data['customer_email'],
                            'magento_id': 0
                        }

            partner = partner_obj.find_or_create(
                txn.cursor, txn.user, customer_data, context
            )
            self.assert_(partner)
            self.assertTrue(
                partner_obj.search(
                    txn.cursor, txn.user, [
                        ('email', '=', customer_data['email'])
                    ], context=context
                )
            )
            partners = magento_partner_obj.search(
                txn.cursor, txn.user, [], context=context
            )
            self.assertEqual(len(partners), len(initial_partners) + 1)

            # Create partner with same magento_id and website_id it will not
            # create new one
            partner_obj.find_or_create(
                txn.cursor, txn.user, customer_data, context
            )
            partners = magento_partner_obj.search(
                txn.cursor, txn.user, [], context=context
            )
            self.assertEqual(len(partners), len(initial_partners) + 1)

            # Create partner with different website
            context.update({
                'magento_website': self.website_id2
            })

            partner = partner_obj.find_or_create(
                txn.cursor, txn.user, customer_data, context
            )
            self.assert_(partner)

            partners = magento_partner_obj.search(
                txn.cursor, txn.user, [], context=context
            )
            self.assertEqual(len(partners), len(initial_partners) + 2)

            # Create partner with different magento_id
            context.update({
                'magento_website': self.website_id1
            })

            if settings.MOCK:
                customer_data = load_json('customers', '2')
            else:
                with magento.Order(*settings.ARGS) as order_api:
                    orders = order_api.list()
                with magento.Customer(*settings.ARGS) as customer_api:
                    for order in orders:
                        if order.get('customer_id'):
                            # Search for different cusotmer
                            if order_data['customer_id'] == \
                                    order['customer_id']:
                                continue
                            customer_data = customer_api.info(
                                order['customer_id']
                            )
                        else:
                            customer_data = {
                                'firstname': order['customer_firstname'],
                                'lastname': order['customer_lastname'],
                                'email': order['customer_email'],
                                'magento_id': 0
                            }

            self.assertFalse(
                partner_obj.search(
                    txn.cursor, txn.user, [
                        ('email', '=', customer_data['email'])
                    ], context=context
                )
            )

            partner = partner_obj.find_or_create(
                txn.cursor, txn.user, customer_data, context
            )
            self.assert_(partner)
            self.assertTrue(
                partner_obj.search(
                    txn.cursor, txn.user, [
                        ('email', '=', customer_data['email'])
                    ], context=context
                )
            )
            partners = magento_partner_obj.search(
                txn.cursor, txn.user, [], context=context
            )
            self.assertEqual(len(partners), len(initial_partners) + 3)
Exemplo n.º 7
0
    def test0040_match_address(self):
        """
        Tests if address matching works as expected
        """
        partner_obj = POOL.get('res.partner')

        with Transaction().start(DB_NAME, USER, CONTEXT) as txn:

            self.setup_defaults(txn)

            context = deepcopy(CONTEXT)
            context.update({
                'magento_website': self.website_id1,
                'magento_store_view': self.store_view_id,
            })

            if settings.MOCK:
                customer_data = load_json('customers', '1')
                address_data = load_json('addresses', '1')
                address_data2 = load_json('addresses', '1b')
                address_data3 = load_json('addresses', '1c')
                address_data4 = load_json('addresses', '1d')
                address_data5 = load_json('addresses', '1e')
            else:
                with magento.Order(*settings.ARGS) as order_api:
                    orders = [
                        order_api.info(order['increment_id'])
                            for order in order_api.list()
                    ]
                    order_data = orders[0]
                with magento.Customer(*settings.ARGS) as customer_api:
                    if order_data.get('customer_id'):
                        customer_data = customer_api.info(
                            order_data['customer_id']
                        )
                    else:
                        customer_data = {
                            'firstname': order_data['customer_firstname'],
                            'lastname': order_data['customer_lastname'],
                            'email': order_data['customer_email'],
                            'magento_id': 0
                        }
                    address_data = order_data['billing_address']
                    for order in orders:
                        # Search for address with different country
                        if order['billing_address']['country_id'] != \
                                address_data['country_id']:
                            address_data2 = order['billing_address']

                        # Search for address with different state
                        if order['billing_address']['region'] != \
                                address_data['region']:
                            address_data3 = order['billing_address']

                        # Search for address with different telephone
                        if order['billing_address']['telephone'] != \
                                address_data['telephone']:
                            address_data4 = order['billing_address']

                        # Search for address with different street
                        if order['billing_address']['street'] != \
                                address_data['street']:
                            address_data5 = order['billing_address']

            # Create partner
            partner = partner_obj.find_or_create(
                txn.cursor, txn.user, customer_data, context
            )

            address = partner_obj.\
                find_or_create_address_as_partner_using_magento_data(
                    txn.cursor, txn.user, address_data, partner, context
                )

            # Same address imported again
            self.assertTrue(
                partner_obj.match_address_with_magento_data(
                    txn.cursor, txn.user, address, address_data
                )
            )

            # Exactly similar address imported again
            self.assertTrue(
                partner_obj.match_address_with_magento_data(
                    txn.cursor, txn.user, address, address_data
                )
            )

            # Similar with different country
            self.assertFalse(
                partner_obj.match_address_with_magento_data(
                    txn.cursor, txn.user, address, address_data2
                )
            )

            # Similar with different state
            self.assertFalse(
                partner_obj.match_address_with_magento_data(
                    txn.cursor, txn.user, address, address_data3
                )
            )

            # Similar with different telephone
            self.assertFalse(
                partner_obj.match_address_with_magento_data(
                    txn.cursor, txn.user, address, address_data4
                )
            )

            # Similar with different street
            self.assertFalse(
                partner_obj.match_address_with_magento_data(
                    txn.cursor, txn.user, address, address_data5
                )
            )
Exemplo n.º 8
0
    def test0030_create_address(self):
        """
        Tests if address creation works as expected
        """
        partner_obj = POOL.get('res.partner')

        with Transaction().start(DB_NAME, USER, CONTEXT) as txn:

            self.setup_defaults(txn)

            context = deepcopy(CONTEXT)
            context.update({
                'magento_website': self.website_id1,
                'magento_store_view': self.store_view_id,
            })

            if settings.MOCK:
                customer_data = load_json('customers', '1')
                address_data = load_json('addresses', '1')
            else:
                with magento.Order(*settings.ARGS) as order_api:
                    orders = order_api.list()
                    order_data = order_api.info(orders[0]['increment_id'])
                with magento.Customer(*settings.ARGS) as customer_api:
                    if order_data.get('customer_id'):
                        customer_data = customer_api.info(
                            order_data['customer_id']
                        )
                    else:
                        customer_data = {
                            'firstname': order_data['customer_firstname'],
                            'lastname': order_data['customer_lastname'],
                            'email': order_data['customer_email'],
                            'magento_id': 0
                        }
                    address_data = order_data['billing_address']

            # Create partner
            partner = partner_obj.find_or_create(
                txn.cursor, txn.user, customer_data, context
            )

            partners_before_address = partner_obj.search(
                txn.cursor, txn.user, [], context=context, count=True
            )

            address_partner = partner_obj.\
                find_or_create_address_as_partner_using_magento_data(
                    txn.cursor, txn.user, address_data, partner, context
                )

            partners_after_address = partner_obj.search(
                txn.cursor, txn.user, [], context=context, count=True
            )

            self.assertTrue(partners_after_address > partners_before_address)

            self.assertEqual(
                address_partner.name, ' '.join(
                    [address_data['firstname'], address_data['lastname']]
                )
            )