Skip to content

Commit 02249bd

Browse files
committed
Merge remote-tracking branch 'remotes/dev/2.0' into 2.0
2 parents a79ab2e + d152e39 commit 02249bd

File tree

16 files changed

+889
-26
lines changed

16 files changed

+889
-26
lines changed

Diff for: src/Oro/Bridge/CalendarCRM/Migrations/Data/Demo/ORM/LoadUsersCalendarData.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,13 @@ protected function connectCalendars()
188188
{
189189
// first user is admin, often
190190
/** @var \Oro\Bundle\UserBundle\Entity\User $admin */
191-
$admin = $this->user->find(1);
191+
$admin = $this->em->getRepository('OroUserBundle:User')
192+
->createQueryBuilder('u')
193+
->select('u')
194+
->orderBy('u.id')
195+
->getQuery()
196+
->setMaxResults(1)
197+
->getSingleResult();
192198
/** @var Calendar $calendarAdmin */
193199
$calendarAdmin = $this->calendar->findDefaultCalendar($admin->getId(), $admin->getOrganization()->getId());
194200

Diff for: src/Oro/Bridge/CallCRM/Migrations/Data/ORM/UpdateCallAccessLevels.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function load(ObjectManager $manager)
5757
$role = $manager->getRepository('OroUserBundle:Role')
5858
->findOneBy(['role' => $roleConfigData['bap_role']]);
5959

60-
if ($aclManager->isAclEnabled()) {
60+
if ($role && $aclManager->isAclEnabled()) {
6161
$sid = $aclManager->getSid($role);
6262
foreach ($roleConfigData['permissions'] as $permission => $acls) {
6363
$this->processPermission($aclManager, $sid, $permission, $acls);

Diff for: src/Oro/Bundle/DemoDataBundle/Migrations/Data/Demo/ORM/LoadTagsData.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,13 @@ protected function initSupportingEntities(ObjectManager $manager = null)
9999
$this->organization = $this->getReference('default_organization');
100100

101101
/** @var User $adminUser */
102-
$adminUser = $this->em->getRepository('OroUserBundle:User')->find(1);
102+
$adminUser = $this->em->getRepository('OroUserBundle:User')
103+
->createQueryBuilder('u')
104+
->select('u')
105+
->orderBy('u.id')
106+
->getQuery()
107+
->setMaxResults(1)
108+
->getSingleResult();
103109
$token = new UsernamePasswordOrganizationToken(
104110
$adminUser,
105111
$adminUser->getUsername(),

Diff for: src/Oro/Bundle/DemoDataBundle/Migrations/Data/ORM/UpdateEmailAccessLevels.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,19 @@ protected function updateUserRole(AclManager $manager)
6565
'ROLE_MARKETING_MANAGER',
6666
'ROLE_LEADS_DEVELOPMENT_REP',
6767
];
68+
$oid = $manager->getOid('entity:Oro\Bundle\EmailBundle\Entity\EmailUser');
69+
6870
foreach ($roles as $roleName) {
6971
$role = $this->getRole($roleName);
7072
if ($role) {
7173
$sid = $manager->getSid($role);
72-
73-
$oid = $manager->getOid('entity:Oro\Bundle\EmailBundle\Entity\EmailUser');
74-
$maskBuilder = $manager->getMaskBuilder($oid)
75-
->add('VIEW_BASIC')
76-
->add('CREATE_BASIC')
77-
->add('EDIT_BASIC');
78-
$manager->setPermission($sid, $oid, $maskBuilder->get());
74+
$mask = 0;
75+
foreach (['VIEW', 'CREATE', 'EDIT'] as $permission) {
76+
$maskBuilder = $manager->getMaskBuilder($oid, $permission);
77+
$maskBuilder->add($permission . '_BASIC');
78+
$mask |= $maskBuilder->get();
79+
}
80+
$manager->setPermission($sid, $oid, $mask);
7981
}
8082
}
8183
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Oro\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\Bundle\MagentoBundle\Manager\CustomerAddressManager;
13+
use Oro\Component\Log\OutputLogger;
14+
15+
class CopyCustomerAddressesToContactCommand extends Command implements ContainerAwareInterface
16+
{
17+
use ContainerAwareTrait;
18+
19+
const BATCH_SIZE = 25;
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function configure()
25+
{
26+
$this
27+
->setName('oro:magento:copy-data-to-contact:addresses')
28+
->addOption(
29+
'id',
30+
null,
31+
InputOption::VALUE_OPTIONAL|InputOption::VALUE_IS_ARRAY,
32+
'If option exists then customer addresses will be copied to contact for given magento customer by id'
33+
)
34+
->addOption(
35+
'integration-id',
36+
null,
37+
InputOption::VALUE_OPTIONAL|InputOption::VALUE_IS_ARRAY,
38+
'If option exists then customer addresses will be copied to contact
39+
for given magento customers by integration_id'
40+
)
41+
->addOption(
42+
'batch-size',
43+
null,
44+
InputOption::VALUE_OPTIONAL,
45+
'Number of customers in batch. The default value is 25.'
46+
)
47+
->setDescription('Make copy addresses of magento customers to the contact');
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function execute(InputInterface $input, OutputInterface $output)
54+
{
55+
$logger = new OutputLogger($output);
56+
$logger->info('Executing command started.');
57+
58+
$integrationIds = $input->getOption('integration-id');
59+
$ids = $input->getOption('id');
60+
$batchSize = $input->getOption('batch-size') ? $input->getOption('batch-size') : self::BATCH_SIZE;
61+
62+
$logger->info('Parameters:');
63+
if ($integrationIds) {
64+
foreach ($integrationIds as $item) {
65+
$logger->info(sprintf('--integration-id=%s', $item));
66+
}
67+
}
68+
if ($ids) {
69+
foreach ($integrationIds as $item) {
70+
$logger->info(sprintf('--id=%s', $item));
71+
}
72+
}
73+
if ($batchSize) {
74+
$logger->info(sprintf('--batch-size=%s', $batchSize));
75+
$logger->info('');
76+
}
77+
78+
/** @var CustomerAddressManager $customerAddressManager */
79+
$customerAddressManager = $this->container->get('oro_magento.manager.customer_address_manager');
80+
$customerAddressManager->setLogger($logger);
81+
$customerAddressManager->copyToContact($ids, $integrationIds, $batchSize);
82+
$logger->info('Executing command finished.');
83+
}
84+
}

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

+26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Oro\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
use Oro\Bundle\MagentoBundle\Entity\Customer;
@@ -178,4 +180,28 @@ public function getReturningCustomersWhoMadeOrderQB()
178180

179181
return $qb;
180182
}
183+
184+
/**
185+
* @param int[]|null $customerIds
186+
* @param int[]|null $integrationIds
187+
*
188+
* @return BufferedQueryResultIterator
189+
*/
190+
public function getIteratorByIdsAndIntegrationIds($customerIds, $integrationIds)
191+
{
192+
$qb = $this->createQueryBuilder('c');
193+
$qb->orderBy('c.id');
194+
195+
if ($customerIds) {
196+
$qb->andWhere('c.id in (:customerIds)')
197+
->setParameter('customerIds', $customerIds);
198+
}
199+
200+
if ($integrationIds) {
201+
$qb->andWhere('c.channel in (:integrationIds)')
202+
->setParameter('integrationIds', $integrationIds);
203+
}
204+
205+
return new BufferedQueryResultIterator($qb->getQuery());
206+
}
181207
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace Oro\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 Oro\Bundle\ContactBundle\Entity\Contact;
14+
use Oro\Bundle\ContactBundle\Entity\ContactAddress;
15+
use Oro\Bundle\MagentoBundle\Entity\Address;
16+
use Oro\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+
protected $baseAddressProperties = [
29+
'label',
30+
'street',
31+
'street2',
32+
'city',
33+
'postalCode',
34+
'country',
35+
'organization',
36+
'region',
37+
'regionText',
38+
'namePrefix',
39+
'firstName',
40+
'middleName',
41+
'lastName',
42+
'nameSuffix'
43+
];
44+
45+
/**
46+
* @param EntityManager $em
47+
*/
48+
public function __construct(EntityManager $em)
49+
{
50+
$this->em = $em;
51+
$this->accessor = PropertyAccess::createPropertyAccessor();
52+
}
53+
54+
/**
55+
* @param int[]|null $customersIds
56+
* @param int[]|null $integrationIds
57+
* @param int $batchSize
58+
*/
59+
public function copyToContact($customersIds = null, $integrationIds = null, $batchSize = 25)
60+
{
61+
$i = 0;
62+
$this->logger->info(sprintf('Start process'));
63+
$repository = $this->em->getRepository('OroMagentoBundle:Customer');
64+
65+
$iterator = $repository->getIteratorByIdsAndIntegrationIds($customersIds, $integrationIds);
66+
$iterator->setBufferSize($batchSize);
67+
$customerCount = $iterator->count();
68+
69+
$iterator->setPageCallback(function () use (&$i, $customerCount) {
70+
$this->em->flush();
71+
$this->em->flush();
72+
$this->logger->info(sprintf('Processed %s customers from %s', $i, $customerCount));
73+
});
74+
75+
/** @var Customer $customer */
76+
foreach ($iterator as $customer) {
77+
$i++;
78+
$contact = $customer->getContact();
79+
if ($contact) {
80+
$addresses = $customer->getAddresses();
81+
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+
110+
foreach ($addresses as $address) {
111+
if ($this->isEqualAddresses($address, $contactAddress)) {
112+
return true;
113+
}
114+
}
115+
116+
return false;
117+
}
118+
119+
/**
120+
* @param ContactAddress $address1
121+
* @param ContactAddress $address2
122+
*
123+
* @return bool
124+
*/
125+
protected function isEqualAddresses(ContactAddress $address1, ContactAddress $address2)
126+
{
127+
$countEqualProperty = 0;
128+
foreach ($this->baseAddressProperties as $property) {
129+
if ($this->accessor->getValue($address1, $property) === $this->accessor->getValue($address2, $property)) {
130+
$countEqualProperty++;
131+
}
132+
}
133+
134+
return $countEqualProperty === count($this->baseAddressProperties);
135+
}
136+
137+
/**
138+
* @param Address $customerAddress
139+
*
140+
* @return ContactAddress
141+
*/
142+
protected function convertToContactAddress(Address $customerAddress)
143+
{
144+
$properties = $this->baseAddressProperties;
145+
$properties[] = 'types';
146+
$properties[] = 'primary';
147+
148+
$contactAddress = new ContactAddress();
149+
foreach ($properties as $property) {
150+
$this->accessor->setValue(
151+
$contactAddress,
152+
$property,
153+
$this->accessor->getValue($customerAddress, $property)
154+
);
155+
}
156+
157+
return $contactAddress;
158+
}
159+
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,13 @@ services:
589589
tags:
590590
- { name: oro_integration.delete_provider }
591591

592+
oro_magento.manager.customer_address_manager:
593+
class: Oro\Bundle\MagentoBundle\Manager\CustomerAddressManager
594+
arguments:
595+
- "@doctrine.orm.entity_manager"
596+
calls:
597+
- ['setLogger', ["@logger"]]
598+
592599
oro_magento.importexport.address_import_helper:
593600
class: %oro_magento.importexport.address_import_helper.class%
594601
arguments:

Diff for: src/Oro/Bundle/MagentoBundle/Resources/views/OrderPlace/widget/place.html.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
modalWidgetAlias = 'transaction-dialog',
1414
messageTemplate = _.template("<%= message %> <a href=\"<%= url %>\" class=\"order-link\"><%= urlLabel %></a> ");
1515
16-
$frame.load(function () {
16+
$frame.on('load', function() {
1717
var offset = $frame.closest('.ui-dialog').find('.ui-dialog-titlebar').outerHeight() || 0;
1818
$frame.addClass('loaded').parent().css({'top': offset});
1919
widgetManager.getWidgetInstance(

0 commit comments

Comments
 (0)