Skip to content

Commit

Permalink
CRM-7596: Import addresses from Magento Customer to Contact
Browse files Browse the repository at this point in the history
  • Loading branch information
Krushelnitskiy authored and mccar committed Feb 17, 2017
1 parent 64d7c31 commit d152e39
Show file tree
Hide file tree
Showing 7 changed files with 856 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Oro\Bundle\MagentoBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;

use Oro\Bundle\MagentoBundle\Manager\CustomerAddressManager;
use Oro\Component\Log\OutputLogger;

class CopyCustomerAddressesToContactCommand extends Command implements ContainerAwareInterface
{
use ContainerAwareTrait;

const BATCH_SIZE = 25;

/**
* {@inheritdoc}
*/
public function configure()
{
$this
->setName('oro:magento:copy-data-to-contact:addresses')
->addOption(
'id',
null,
InputOption::VALUE_OPTIONAL|InputOption::VALUE_IS_ARRAY,
'If option exists then customer addresses will be copied to contact for given magento customer by id'
)
->addOption(
'integration-id',
null,
InputOption::VALUE_OPTIONAL|InputOption::VALUE_IS_ARRAY,
'If option exists then customer addresses will be copied to contact
for given magento customers by integration_id'
)
->addOption(
'batch-size',
null,
InputOption::VALUE_OPTIONAL,
'Number of customers in batch. The default value is 25.'
)
->setDescription('Make copy addresses of magento customers to the contact');
}

/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$logger = new OutputLogger($output);
$logger->info('Executing command started.');

$integrationIds = $input->getOption('integration-id');
$ids = $input->getOption('id');
$batchSize = $input->getOption('batch-size') ? $input->getOption('batch-size') : self::BATCH_SIZE;

$logger->info('Parameters:');
if ($integrationIds) {
foreach ($integrationIds as $item) {
$logger->info(sprintf('--integration-id=%s', $item));
}
}
if ($ids) {
foreach ($integrationIds as $item) {
$logger->info(sprintf('--id=%s', $item));
}
}
if ($batchSize) {
$logger->info(sprintf('--batch-size=%s', $batchSize));
$logger->info('');
}

/** @var CustomerAddressManager $customerAddressManager */
$customerAddressManager = $this->container->get('oro_magento.manager.customer_address_manager');
$customerAddressManager->setLogger($logger);
$customerAddressManager->copyToContact($ids, $integrationIds, $batchSize);
$logger->info('Executing command finished.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Oro\Bundle\MagentoBundle\Entity\Repository;

use Doctrine\ORM\QueryBuilder;

use Oro\Bundle\BatchBundle\ORM\Query\BufferedQueryResultIterator;
use Oro\Bundle\DashboardBundle\Helper\DateHelper;
use Oro\Bundle\SecurityBundle\ORM\Walker\AclHelper;
use Oro\Bundle\MagentoBundle\Entity\Customer;
Expand Down Expand Up @@ -178,4 +180,28 @@ public function getReturningCustomersWhoMadeOrderQB()

return $qb;
}

/**
* @param int[]|null $customerIds
* @param int[]|null $integrationIds
*
* @return BufferedQueryResultIterator
*/
public function getIteratorByIdsAndIntegrationIds($customerIds, $integrationIds)
{
$qb = $this->createQueryBuilder('c');
$qb->orderBy('c.id');

if ($customerIds) {
$qb->andWhere('c.id in (:customerIds)')
->setParameter('customerIds', $customerIds);
}

if ($integrationIds) {
$qb->andWhere('c.channel in (:integrationIds)')
->setParameter('integrationIds', $integrationIds);
}

return new BufferedQueryResultIterator($qb->getQuery());
}
}
159 changes: 159 additions & 0 deletions src/Oro/Bundle/MagentoBundle/Manager/CustomerAddressManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

namespace Oro\Bundle\MagentoBundle\Manager;

use Doctrine\ORM\EntityManager;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;

use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;

use Oro\Bundle\ContactBundle\Entity\Contact;
use Oro\Bundle\ContactBundle\Entity\ContactAddress;
use Oro\Bundle\MagentoBundle\Entity\Address;
use Oro\Bundle\MagentoBundle\Entity\Customer;

class CustomerAddressManager implements LoggerAwareInterface
{
use LoggerAwareTrait;

/** @var EntityManager */
protected $em;

/** @var PropertyAccessor */
protected $accessor;

protected $baseAddressProperties = [
'label',
'street',
'street2',
'city',
'postalCode',
'country',
'organization',
'region',
'regionText',
'namePrefix',
'firstName',
'middleName',
'lastName',
'nameSuffix'
];

/**
* @param EntityManager $em
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
$this->accessor = PropertyAccess::createPropertyAccessor();
}

/**
* @param int[]|null $customersIds
* @param int[]|null $integrationIds
* @param int $batchSize
*/
public function copyToContact($customersIds = null, $integrationIds = null, $batchSize = 25)
{
$i = 0;
$this->logger->info(sprintf('Start process'));
$repository = $this->em->getRepository('OroMagentoBundle:Customer');

$iterator = $repository->getIteratorByIdsAndIntegrationIds($customersIds, $integrationIds);
$iterator->setBufferSize($batchSize);
$customerCount = $iterator->count();

$iterator->setPageCallback(function () use (&$i, $customerCount) {
$this->em->flush();
$this->em->flush();
$this->logger->info(sprintf('Processed %s customers from %s', $i, $customerCount));
});

/** @var Customer $customer */
foreach ($iterator as $customer) {
$i++;
$contact = $customer->getContact();
if ($contact) {
$addresses = $customer->getAddresses();

if ($addresses->count() > 0) {
foreach ($addresses as $address) {
$newContactAddress = $this->convertToContactAddress($address);
if (!$this->contactHasAddress($contact, $newContactAddress)) {
$contact->addAddress($newContactAddress);
$message = 'Customer address with id=%s was copied in contact with id=%s';
$this->logger->info(sprintf($message, $address->getId(), $contact->getId()));
}
}
$this->em->persist($contact);
}
}
}

$this->em->flush();
$this->logger->info(sprintf('Finish process'));
}

/**
* @param Contact $contact
* @param ContactAddress $contactAddress
*
* @return bool
*/
protected function contactHasAddress(Contact $contact, ContactAddress $contactAddress)
{
$addresses = $contact->getAddresses();

foreach ($addresses as $address) {
if ($this->isEqualAddresses($address, $contactAddress)) {
return true;
}
}

return false;
}

/**
* @param ContactAddress $address1
* @param ContactAddress $address2
*
* @return bool
*/
protected function isEqualAddresses(ContactAddress $address1, ContactAddress $address2)
{
$countEqualProperty = 0;
foreach ($this->baseAddressProperties as $property) {
if ($this->accessor->getValue($address1, $property) === $this->accessor->getValue($address2, $property)) {
$countEqualProperty++;
}
}

return $countEqualProperty === count($this->baseAddressProperties);
}

/**
* @param Address $customerAddress
*
* @return ContactAddress
*/
protected function convertToContactAddress(Address $customerAddress)
{
$properties = $this->baseAddressProperties;
$properties[] = 'types';
$properties[] = 'primary';

$contactAddress = new ContactAddress();
foreach ($properties as $property) {
$this->accessor->setValue(
$contactAddress,
$property,
$this->accessor->getValue($customerAddress, $property)
);
}

return $contactAddress;
}
}
7 changes: 7 additions & 0 deletions src/Oro/Bundle/MagentoBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,13 @@ services:
tags:
- { name: oro_integration.delete_provider }

oro_magento.manager.customer_address_manager:
class: Oro\Bundle\MagentoBundle\Manager\CustomerAddressManager
arguments:
- "@doctrine.orm.entity_manager"
calls:
- ['setLogger', ["@logger"]]

oro_magento.importexport.address_import_helper:
class: %oro_magento.importexport.address_import_helper.class%
arguments:
Expand Down

0 comments on commit d152e39

Please sign in to comment.