Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/dev/2.0' into 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrebenchuk committed Feb 18, 2017
2 parents a79ab2e + d152e39 commit 02249bd
Show file tree
Hide file tree
Showing 16 changed files with 889 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,13 @@ protected function connectCalendars()
{
// first user is admin, often
/** @var \Oro\Bundle\UserBundle\Entity\User $admin */
$admin = $this->user->find(1);
$admin = $this->em->getRepository('OroUserBundle:User')
->createQueryBuilder('u')
->select('u')
->orderBy('u.id')
->getQuery()
->setMaxResults(1)
->getSingleResult();
/** @var Calendar $calendarAdmin */
$calendarAdmin = $this->calendar->findDefaultCalendar($admin->getId(), $admin->getOrganization()->getId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function load(ObjectManager $manager)
$role = $manager->getRepository('OroUserBundle:Role')
->findOneBy(['role' => $roleConfigData['bap_role']]);

if ($aclManager->isAclEnabled()) {
if ($role && $aclManager->isAclEnabled()) {
$sid = $aclManager->getSid($role);
foreach ($roleConfigData['permissions'] as $permission => $acls) {
$this->processPermission($aclManager, $sid, $permission, $acls);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ protected function initSupportingEntities(ObjectManager $manager = null)
$this->organization = $this->getReference('default_organization');

/** @var User $adminUser */
$adminUser = $this->em->getRepository('OroUserBundle:User')->find(1);
$adminUser = $this->em->getRepository('OroUserBundle:User')
->createQueryBuilder('u')
->select('u')
->orderBy('u.id')
->getQuery()
->setMaxResults(1)
->getSingleResult();
$token = new UsernamePasswordOrganizationToken(
$adminUser,
$adminUser->getUsername(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,19 @@ protected function updateUserRole(AclManager $manager)
'ROLE_MARKETING_MANAGER',
'ROLE_LEADS_DEVELOPMENT_REP',
];
$oid = $manager->getOid('entity:Oro\Bundle\EmailBundle\Entity\EmailUser');

foreach ($roles as $roleName) {
$role = $this->getRole($roleName);
if ($role) {
$sid = $manager->getSid($role);

$oid = $manager->getOid('entity:Oro\Bundle\EmailBundle\Entity\EmailUser');
$maskBuilder = $manager->getMaskBuilder($oid)
->add('VIEW_BASIC')
->add('CREATE_BASIC')
->add('EDIT_BASIC');
$manager->setPermission($sid, $oid, $maskBuilder->get());
$mask = 0;
foreach (['VIEW', 'CREATE', 'EDIT'] as $permission) {
$maskBuilder = $manager->getMaskBuilder($oid, $permission);
$maskBuilder->add($permission . '_BASIC');
$mask |= $maskBuilder->get();
}
$manager->setPermission($sid, $oid, $mask);
}
}
}
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
modalWidgetAlias = 'transaction-dialog',
messageTemplate = _.template("<%= message %> <a href=\"<%= url %>\" class=\"order-link\"><%= urlLabel %></a> ");
$frame.load(function () {
$frame.on('load', function() {
var offset = $frame.closest('.ui-dialog').find('.ui-dialog-titlebar').outerHeight() || 0;
$frame.addClass('loaded').parent().css({'top': offset});
widgetManager.getWidgetInstance(
Expand Down

0 comments on commit 02249bd

Please sign in to comment.