Skip to content

Commit 92e1193

Browse files
Krushelnitskiymccar
authored andcommitted
CRM-7596: Import addresses from Magento Customer to Contact
1 parent dfe7732 commit 92e1193

File tree

6 files changed

+815
-0
lines changed

6 files changed

+815
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace OroCRM\Bundle\MagentoBundle\Command;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Input\InputOption;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
10+
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
11+
12+
use Oro\Component\Log\OutputLogger;
13+
14+
use OroCRM\Bundle\MagentoBundle\Manager\CustomerAddressManager;
15+
16+
class CopyCustomerAddressesToContactCommand extends Command implements ContainerAwareInterface
17+
{
18+
use ContainerAwareTrait;
19+
20+
const BATCH_SIZE = 25;
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function configure()
26+
{
27+
$this
28+
->setName('oro:magento:copy-data-to-contact:addresses')
29+
->addOption(
30+
'id',
31+
null,
32+
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
33+
'If option exists then customer addresses will be copied to contact for given magento customer by id'
34+
)
35+
->addOption(
36+
'integration-id',
37+
null,
38+
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
39+
'If option exists then customer addresses will be copied to contact
40+
for given magento customers by integration_id'
41+
)
42+
->addOption(
43+
'batch-size',
44+
null,
45+
InputOption::VALUE_OPTIONAL,
46+
'Number of customers in batch. The default value is 25.'
47+
)
48+
->setDescription('Make copy addresses of magento customers to the contact');
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function execute(InputInterface $input, OutputInterface $output)
55+
{
56+
$logger = new OutputLogger($output);
57+
$logger->info('Executing command started.');
58+
59+
$integrationIds = $input->getOption('integration-id');
60+
$ids = $input->getOption('id');
61+
$batchSize = $input->getOption('batch-size') ? $input->getOption('batch-size') : self::BATCH_SIZE;
62+
63+
$logger->info('Parameters:');
64+
if ($integrationIds) {
65+
foreach ($integrationIds as $item) {
66+
$logger->info(sprintf('--integration-id=%s', $item));
67+
}
68+
}
69+
if ($ids) {
70+
foreach ($integrationIds as $item) {
71+
$logger->info(sprintf('--id=%s', $item));
72+
}
73+
}
74+
if ($batchSize) {
75+
$logger->info(sprintf('--batch-size=%s', $batchSize));
76+
$logger->info('');
77+
}
78+
79+
/** @var CustomerAddressManager $customerAddressManager */
80+
$customerAddressManager = $this->container->get('oro_magento.manager.customer_address_manager');
81+
$customerAddressManager->setLogger($logger);
82+
$customerAddressManager->copyToContact($ids, $integrationIds, $batchSize);
83+
84+
$logger->info('Executing command finished.');
85+
}
86+
}

Diff for: src/OroCRM/Bundle/MagentoBundle/Entity/Repository/CustomerRepository.php

+26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace OroCRM\Bundle\MagentoBundle\Entity\Repository;
44

55
use Doctrine\ORM\QueryBuilder;
6+
7+
use Oro\Bundle\BatchBundle\ORM\Query\BufferedQueryResultIterator;
68
use Oro\Bundle\DashboardBundle\Helper\DateHelper;
79
use Oro\Bundle\SecurityBundle\ORM\Walker\AclHelper;
810

@@ -179,4 +181,28 @@ public function getReturningCustomersWhoMadeOrderQB()
179181

180182
return $qb;
181183
}
184+
185+
/**
186+
* @param int[]|null $customerIds
187+
* @param int[]|null $integrationIds
188+
*
189+
* @return BufferedQueryResultIterator
190+
*/
191+
public function getIteratorByIdsAndIntegrationIds($customerIds, $integrationIds)
192+
{
193+
$qb = $this->createQueryBuilder('c');
194+
$qb->orderBy('c.id');
195+
196+
if ($customerIds) {
197+
$qb->andWhere('c.id in (:customerIds)')
198+
->setParameter('customerIds', $customerIds);
199+
}
200+
201+
if ($integrationIds) {
202+
$qb->andWhere('c.channel in (:integrationIds)')
203+
->setParameter('integrationIds', $integrationIds);
204+
}
205+
206+
return new BufferedQueryResultIterator($qb->getQuery());
207+
}
182208
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
3+
namespace OroCRM\Bundle\MagentoBundle\Manager;
4+
5+
use Doctrine\ORM\EntityManager;
6+
7+
use Psr\Log\LoggerAwareInterface;
8+
use Psr\Log\LoggerAwareTrait;
9+
10+
use Symfony\Component\PropertyAccess\PropertyAccess;
11+
use Symfony\Component\PropertyAccess\PropertyAccessor;
12+
13+
use OroCRM\Bundle\ContactBundle\Entity\Contact;
14+
use OroCRM\Bundle\ContactBundle\Entity\ContactAddress;
15+
use OroCRM\Bundle\MagentoBundle\Entity\Address;
16+
use OroCRM\Bundle\MagentoBundle\Entity\Customer;
17+
18+
class CustomerAddressManager implements LoggerAwareInterface
19+
{
20+
use LoggerAwareTrait;
21+
22+
/** @var EntityManager */
23+
protected $em;
24+
25+
/** @var PropertyAccessor */
26+
protected $accessor;
27+
28+
/** @var array */
29+
protected $baseAddressProperties = [
30+
'label',
31+
'street',
32+
'street2',
33+
'city',
34+
'postalCode',
35+
'country',
36+
'organization',
37+
'region',
38+
'regionText',
39+
'namePrefix',
40+
'firstName',
41+
'middleName',
42+
'lastName',
43+
'nameSuffix'
44+
];
45+
46+
/**
47+
* @param EntityManager $em
48+
*/
49+
public function __construct(EntityManager $em)
50+
{
51+
$this->em = $em;
52+
$this->accessor = PropertyAccess::createPropertyAccessor();
53+
}
54+
55+
/**
56+
* @param int[]|null $customersIds
57+
* @param int[]|null $integrationIds
58+
* @param int $batchSize
59+
*/
60+
public function copyToContact($customersIds = null, $integrationIds = null, $batchSize = 25)
61+
{
62+
$i = 0;
63+
$this->logger->info(sprintf('Start process'));
64+
$repository = $this->em->getRepository('OroCRMMagentoBundle:Customer');
65+
66+
$iterator = $repository->getIteratorByIdsAndIntegrationIds($customersIds, $integrationIds);
67+
$iterator->setBufferSize($batchSize);
68+
$customerCount = $iterator->count();
69+
70+
$iterator->setPageCallback(function () use (&$i, $customerCount) {
71+
$this->em->flush();
72+
$this->em->flush();
73+
$this->logger->info(sprintf('Processed %s customers from %s', $i, $customerCount));
74+
});
75+
76+
/** @var Customer $customer */
77+
foreach ($iterator as $customer) {
78+
$i++;
79+
$contact = $customer->getContact();
80+
if ($contact) {
81+
$addresses = $customer->getAddresses();
82+
if ($addresses->count() > 0) {
83+
foreach ($addresses as $address) {
84+
$newContactAddress = $this->convertToContactAddress($address);
85+
if (!$this->contactHasAddress($contact, $newContactAddress)) {
86+
$contact->addAddress($newContactAddress);
87+
$message = 'Customer address with id=%s was copied in contact with id=%s';
88+
$this->logger->info(sprintf($message, $address->getId(), $contact->getId()));
89+
}
90+
}
91+
$this->em->persist($contact);
92+
}
93+
}
94+
}
95+
96+
$this->em->flush();
97+
$this->logger->info(sprintf('Finish process'));
98+
}
99+
100+
/**
101+
* @param Contact $contact
102+
* @param ContactAddress $contactAddress
103+
*
104+
* @return bool
105+
*/
106+
protected function contactHasAddress(Contact $contact, ContactAddress $contactAddress)
107+
{
108+
$addresses = $contact->getAddresses();
109+
foreach ($addresses as $address) {
110+
if ($this->isEqualAddresses($address, $contactAddress)) {
111+
return true;
112+
}
113+
}
114+
115+
return false;
116+
}
117+
118+
/**
119+
* @param ContactAddress $address1
120+
* @param ContactAddress $address2
121+
*
122+
* @return bool
123+
*/
124+
protected function isEqualAddresses(ContactAddress $address1, ContactAddress $address2)
125+
{
126+
$countEqualProperty = 0;
127+
foreach ($this->baseAddressProperties as $property) {
128+
if ($this->accessor->getValue($address1, $property) === $this->accessor->getValue($address2, $property)) {
129+
$countEqualProperty++;
130+
}
131+
}
132+
133+
return $countEqualProperty === count($this->baseAddressProperties);
134+
}
135+
136+
/**
137+
* @param Address $customerAddress
138+
*
139+
* @return ContactAddress
140+
*/
141+
protected function convertToContactAddress(Address $customerAddress)
142+
{
143+
$properties = $this->baseAddressProperties;
144+
$properties[] = 'types';
145+
$properties[] = 'primary';
146+
147+
$contactAddress = new ContactAddress();
148+
foreach ($properties as $property) {
149+
$this->accessor->setValue(
150+
$contactAddress,
151+
$property,
152+
$this->accessor->getValue($customerAddress, $property)
153+
);
154+
}
155+
156+
return $contactAddress;
157+
}
158+
}

Diff for: src/OroCRM/Bundle/MagentoBundle/Resources/config/services.yml

+7
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,13 @@ services:
581581
tags:
582582
- { name: oro_integration.delete_provider }
583583

584+
oro_magento.manager.customer_address_manager:
585+
class: OroCRM\Bundle\MagentoBundle\Manager\CustomerAddressManager
586+
arguments:
587+
- "@doctrine.orm.entity_manager"
588+
calls:
589+
- ['setLogger', ["@logger"]]
590+
584591
orocrm_magento.importexport.address_import_helper:
585592
class: %orocrm_magento.importexport.address_import_helper.class%
586593
arguments:

0 commit comments

Comments
 (0)