custom/plugins/AbasBecoCustomizations/src/Subscribers/Customer/BecoCustomerCreatedSubscriber.php line 82

Open in your IDE?
  1. <?php
  2. namespace AbasBecoCustomizations\Subscribers\Customer;
  3. use Abas\AbasConnector\Service\Core\Content\CustomerRole\CustomerRoleEntity;
  4. use Abas\AbasConnector\Service\Core\Content\CustomerRole\CustomerRoleRelationEntity;
  5. use Abas\AbasConnector\Service\Core\Enums\CustomerRole;
  6. use Abas\AbasConnector\Service\Sync\Customer\CustomerSync;
  7. use AbasBecoCustomizations\Service\Sync\BecoProspectContactSync;
  8. use Shopware\Core\Checkout\Customer\CustomerEntity;
  9. use Shopware\Core\Checkout\Customer\CustomerEvents;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  17. use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class BecoCustomerCreatedSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var EntityRepositoryInterface
  23.      */
  24.     private $customerRepository;
  25.     /**
  26.      * @var EntityRepositoryInterface
  27.      */
  28.     private $customerRoleRelationRepository;
  29.     /**
  30.      * @var EntityRepositoryInterface
  31.      */
  32.     private $customerRoleRepository;
  33.     /**
  34.      * @var CustomerSync
  35.      */
  36.     private $customerSync;
  37.     /**
  38.      * @var BecoProspectContactSync
  39.      */
  40.     private $prospectContactSync;
  41.     /**
  42.      * @var EntityRepositoryInterface
  43.      */
  44.     private $customerGroupRepository;
  45.     /**
  46.      * @var EntityRepositoryInterface
  47.      */
  48.     private $languageRepository;
  49.     public function __construct(
  50.         EntityRepositoryInterface $customerRepository,
  51.         EntityRepositoryInterface $customerRoleRelationRepository,
  52.         EntityRepositoryInterface $customerRoleRepository,
  53.         CustomerSync $customerSync,
  54.         BecoProspectContactSync $prospectContactSync,
  55.         EntityRepositoryInterface $customerGroupRepository,
  56.         EntityRepositoryInterface $languageRepository
  57.     ) {
  58.         $this->customerRepository $customerRepository;
  59.         $this->customerRoleRelationRepository $customerRoleRelationRepository;
  60.         $this->customerRoleRepository $customerRoleRepository;
  61.         $this->customerSync $customerSync;
  62.         $this->prospectContactSync $prospectContactSync;
  63.         $this->customerGroupRepository $customerGroupRepository;
  64.         $this->languageRepository $languageRepository;
  65.     }
  66.     public static function getSubscribedEvents()
  67.     {
  68.         return [
  69.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'syncCustomerProspectToAbas'
  70.         ];
  71.     }
  72.     /**
  73.      * @param EntityWrittenEvent $event
  74.      */
  75.     public function syncCustomerProspectToAbas(EntityWrittenEvent $event)
  76.     {
  77.         $customerIds $event->getIds();
  78.         $context $event->getContext();
  79.         $executedIds = [];
  80.         foreach ($customerIds as $id) {
  81.             if (in_array($id$executedIds)) {
  82.                 continue;
  83.             }
  84.             $customer $this->getCustomer($id$context);
  85.             if($customer->getGuest()){
  86.                 continue;
  87.             }
  88.             if ($customer && $this->shouldSyncCustomer($customer)) {
  89.                 $this->customerSync->syncCustomer($customer);
  90.                 // reload for customFields
  91.                 $customer $this->getCustomer($id$context);
  92.                 $this->createProspectContact($customer);
  93.                 $roleKey CustomerRole::USER;
  94.                 $this->customerRoleRelationRepository->upsert(
  95.                     [
  96.                         [
  97.                             'customerId' => $customer->getId(),
  98.                             'customerRoleId' => $this->getCustomerRoleByKey($roleKey$context)->getId()
  99.                         ]
  100.                     ],
  101.                     $context
  102.                 );
  103.                 $executedIds[] = $id;
  104.             }
  105.         }
  106.     }
  107.     /**
  108.      * @param string $key
  109.      * @param Context $context
  110.      * @return CustomerRoleEntity|null
  111.      */
  112.     private function getCustomerRoleByKey(string $keyContext $context)
  113.     {
  114.         return $this->customerRoleRepository->search(
  115.             (new Criteria())
  116.                 ->addAssociation('permissions')
  117.                 ->addFilter(new EqualsFilter('roleKey'$key)),
  118.             $context
  119.         )->first();
  120.     }
  121.     protected function shouldSyncCustomer(CustomerEntity $customerEntity)
  122.     {
  123.         return !($customerEntity->getCustomFields() &&
  124.             isset($customerEntity->getCustomFields()['abas_id']) &&
  125.             $customerEntity->getCustomFields()['abas_id']);
  126.     }
  127.     private function createProspectContact(?CustomerEntity $prospect)
  128.     {
  129.         if ($prospect === null){
  130.             return;
  131.         }
  132.         $abasComponent null;
  133.         try {
  134.             $customFields $prospect->getCustomFields();
  135.             $customFields['abas_database_and_group'] = "0:7";
  136.             $prospect->setCustomFields($customFields);// Interessentkontakt erstellen
  137.             $abasComponent $this->prospectContactSync->prepareEntityToAbas($prospect);
  138.             $abasComponent->setFieldValue("firma""$,," $this->getCustSelectionIdentifier($prospect));
  139.             $abasComponent->setFieldValue("yvorname"$prospect->getFirstName());
  140.             $abasComponent->setFieldValue("kontakt"$prospect->getLastName());
  141.             $abasComponent->setFieldValue("ywebshopabsw"".");
  142.             $erpLang $this->getErpLanguage($prospect->getLanguageId());
  143.             $abasComponent->setFieldValue("spr"$erpLang);
  144.             $abasComponent->saveAndReload();
  145.             $this->prospectContactSync->syncContactIdentifierToShopware($abasComponent$prospect->get("id"));
  146.         } catch (\Throwable $e) {
  147.             echo $e->getMessage();
  148.         } finally {
  149.             if ($abasComponent !== null) {
  150.                 $abasComponent->close();
  151.             }
  152.         }
  153.     }
  154.     /**
  155.      * Get a customFields value
  156.      * @param Entity $entity
  157.      * @param string $key
  158.      * @return string|null
  159.      */
  160.     protected function getCustomFieldValue(Entity $entitystring $key): ?string
  161.     {
  162.         if ($entity->has('customFields')) {
  163.             $customFields $entity->get('customFields');
  164.             if ($customFields && isset($customFields[$key])) {
  165.                 return $customFields[$key];
  166.             }
  167.         }
  168.         return null;
  169.     }
  170.     /**
  171.      * Return the abas identifier value (GUID or ID)
  172.      * @param Entity $entity
  173.      * @return string|null
  174.      */
  175.     protected function getCustSelectionIdentifier(Entity $entity)
  176.     {
  177.         $guid $this->getCustomFieldValue($entity'abas_guid');
  178.         if ($guid) {
  179.             return 'guid==' $guid;
  180.         }
  181.         return 'id==' $this->getCustomFieldValue($entity'abas_id');
  182.     }
  183.     private function getCustomer($idContext $context): CustomerEntity
  184.     {
  185.         $criteria = (new Criteria())->addFilter(new EqualsFilter('id'$id));
  186.         $criteria->addAssociation('defaultBillingAddress');
  187.         $criteria->addAssociation('defaultShippingAddress');
  188.         $criteria->addAssociation('salutation');
  189.         $criteria->addAssociation('customFields');
  190.         /** @var CustomerEntity $customer */
  191.         return $this->customerRepository->search($criteria$context)->first();
  192.     }
  193.     private function getErpLanguage(string $languageId): string
  194.     {
  195.         $language $this->languageRepository->search(
  196.             (new Criteria([$languageId])),
  197.             Context::createDefaultContext()
  198.         )->first()->getName();
  199.         switch ($language) {
  200.             case 'English (EN)':
  201.                 return "Englisch";
  202.             case 'Español (ES)':
  203.                 return "Spanisch";
  204.             case 'Nederlands (NL)':
  205.                 return "Niederländisch";
  206.             case 'Français (FR)':
  207.                 return "Französisch";
  208.             default:
  209.                 return "Deutsch";
  210.         }
  211.     }
  212. }