<?php
declare(strict_types=0);
/*
* WellCommerce Foundation
*
* This file is part of the WellCommerce package.
*
* (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
namespace WellCommerce\Bundle\AppBundle\EventListener;
use Carbon\Carbon;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManagerInterface;
use Geocoder\Geocoder;
use Geocoder\Location;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use WellCommerce\Bundle\AppBundle\Entity\AddressInterface;
use WellCommerce\Bundle\AppBundle\Entity\Client;
use WellCommerce\Bundle\AppBundle\Entity\ClientAddress;
use WellCommerce\Bundle\AppBundle\Entity\ClientGroup;
use WellCommerce\Bundle\AppBundle\Entity\ComplaintHistory;
use WellCommerce\Bundle\AppBundle\Entity\ComplaintProduct;
use WellCommerce\Bundle\AppBundle\Entity\ComplaintStatus;
use WellCommerce\Bundle\AppBundle\Entity\NewsletterSubscriber;
use WellCommerce\Bundle\AppBundle\Entity\ProductClientGroupDiscount;
use WellCommerce\Bundle\AppBundle\Entity\User;
use WellCommerce\Bundle\AppBundle\Manager\NewsletterSubscriberManager;
use WellCommerce\Bundle\AppBundle\Service\ClientAddress\ClientAddressHelper;
use WellCommerce\Bundle\AppBundle\Service\Geocoder\Configuration\GeocoderSystemConfigurator;
use WellCommerce\Bundle\AppBundle\Service\Geocoder\Provider\GeocoderProvider;
use WellCommerce\Bundle\CatalogBundle\Entity\Product;
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
use WellCommerce\Bundle\CoreBundle\Doctrine\Repository\RepositoryInterface;
use WellCommerce\Bundle\CoreBundle\Helper\Request\RequestHelper;
use WellCommerce\Bundle\CoreBundle\Helper\Router\RouterHelperInterface;
use WellCommerce\Bundle\CoreBundle\Helper\Security\SecurityHelperInterface;
use WellCommerce\Bundle\CoreBundle\Helper\Translator\TranslatorHelper;
use WellCommerce\Bundle\OrderBundle\Entity\Order;
use WellCommerce\Bundle\OrderBundle\Entity\OrderProduct;
use WellCommerce\Bundle\OrderBundle\Provider\Front\OrderProvider;
use WellCommerce\Component\Form\Elements\Fieldset\FieldsetInterface;
use WellCommerce\Component\Form\Event\FormEvent;
/**
* Class ClientSubscriber
*
* @author Adam Piotrowski <adam@wellcommerce.org>
*/
class ClientSubscriber implements EventSubscriberInterface
{
protected SecurityHelperInterface $securityHelper;
protected EntityManagerInterface $entityManager;
protected RouterHelperInterface $routerHelper;
protected GeocoderProvider $geocoderProvider;
protected GeocoderSystemConfigurator $geocoderSystemConfigurator;
protected NewsletterSubscriberManager $newsletterSubscriberManager;
protected TranslatorHelper $translatorHelper;
protected RequestHelper $requestHelper;
protected ClientAddressHelper $clientAddressHelper;
protected OrderProvider $orderProvider;
protected bool $debug;
public function __construct(
SecurityHelperInterface $securityHelper,
EntityManagerInterface $entityManager,
RouterHelperInterface $routerHelper,
RequestHelper $requestHelper,
GeocoderProvider $geocoderProvider,
GeocoderSystemConfigurator $geocoderSystemConfigurator,
NewsletterSubscriberManager $newsletterSubscriberManager,
TranslatorHelper $translatorHelper,
ClientAddressHelper $clientAddressHelper,
OrderProvider $orderProvider,
bool $debug
) {
$this->securityHelper = $securityHelper;
$this->entityManager = $entityManager;
$this->routerHelper = $routerHelper;
$this->requestHelper = $requestHelper;
$this->geocoderProvider = $geocoderProvider;
$this->geocoderSystemConfigurator = $geocoderSystemConfigurator;
$this->newsletterSubscriberManager = $newsletterSubscriberManager;
$this->translatorHelper = $translatorHelper;
$this->clientAddressHelper = $clientAddressHelper;
$this->orderProvider = $orderProvider;
$this->debug = $debug;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => ['onKernelController', 0],
'admin.product.pre_form_init' => ['onProductFormAdminInit'],
'client.post_create' => ['onClientPostCreate', 10],
'client.post_update' => ['onClientPostUpdate', 10],
'client.pre_create' => ['onClientPreCreate', 10],
'client.pre_update' => ['onClientPreUpdate', 10],
'client_address.pre_update' => ['onClientAddressPreUpdate', 10],
'admin.client.pre_form_init' => ['onClientAdminFormInit', 10],
'admin.client.post_form_init' => ['onClientAdminFormPostInit', 10],
'admin.client_complaint.pre_form_init' => ['onClientComplaintAdminFormInit', 10],
'security.interactive_login' => ['onSecurityInteractiveLogin', 0],
];
}
public function onKernelController(FilterControllerEvent $event)
{
if ($event->isMasterRequest()) {
$currentClient = $this->securityHelper->getCurrentClient();
if ($currentClient instanceof Client) {
$currentClient->setLastActive(Carbon::now()->toDateTimeImmutable());
$this->entityManager->flush();
}
}
}
public function onProductFormAdminInit(FormEvent $event)
{
$resource = $event->getResource();
if ($resource instanceof Product) {
/** @var RepositoryInterface $repository */
$repository = $this->entityManager->getRepository(ProductClientGroupDiscount::class);
$form = $event->getForm();
$builder = $event->getFormBuilder();
$transformer = $builder->getRepositoryTransformer('product_client_group_discount', ProductClientGroupDiscount::class);
$clientGroups = $this->entityManager->getRepository(ClientGroup::class)->matching(new Criteria());
if ($clientGroups instanceof Collection) {
$clientGroupData = $form->addChild(
$builder->getElement('nested_fieldset', [
'name' => 'client_group_discount_data',
'label' => 'product.form.fieldset.client_group_discounts',
'transformer' => $transformer,
'property_path' => new PropertyPath('clientGroupDiscounts'),
])
);
$clientGroups->map(function (ClientGroup $clientGroup) use ($form, $builder, $resource, $clientGroupData, $repository) {
$clientGroupDiscounts = $repository->findBy([
'product' => $resource,
'clientGroup' => $clientGroup,
]);
if ($form->getChildren()->has('price_pane')) {
$sellPriceTax = $form->getChildren()->get('price_pane')->getChildren()->get('sell_price_settings')->getChildren(
)->get('sellPriceTax');
$priceStockData = $clientGroupData->addChild(
$builder->getElement('nested_fieldset', [
'name' => 'client_group_' . $clientGroup->getId(),
'label' => $clientGroup->translate()->getName(),
])
);
$ranges = [];
/** @var ProductClientGroupDiscount $clientGroupDiscount */
foreach ($clientGroupDiscounts as $clientGroupDiscount) {
$ranges[] = [
'min' => $clientGroupDiscount->getQuantityFrom(),
'max' => $clientGroupDiscount->getQuantityTo(),
'price' => $clientGroupDiscount->getDiscount(),
];
}
$priceStockData->addChild(
$builder->getElement('range_editor', [
'name' => 'discounts',
'label' => 'Rabaty',
'vat_field' => $sellPriceTax,
'range_precision' => 0,
'price_precision' => 0,
])
)->setValue(['ranges' => $ranges]);
}
});
}
}
}
public function onClientComplaintAdminFormInit(FormEvent $event)
{
$resource = $event->getResource();
if ($resource instanceof ComplaintHistory) {
$complaint = $resource->getComplaint();
$history = $complaint->getHistory();
$form = $event->getForm();
$builder = $event->getFormBuilder();
$fieldset = $form->getChildren()->get('products_data');
$html = '<ul class="changes-detailed">';
$complaint->getProducts()->map(function (ComplaintProduct $complaintProduct) use (&$html) {
$orderProduct = $complaintProduct->getOrderProduct();
if ($orderProduct instanceof OrderProduct) {
$product = $orderProduct->getProduct();
$url = $this->routerHelper->generateUrl('admin.product.edit', ['id' => $product->getId()]);
$name = sprintf('%s x %s', $complaintProduct->getQuantity(), $product->translate()->getName());
foreach ($orderProduct->getOptions() as $option) {
$name .= ' - ' . $option;
}
$html .= '<li style="border-bottom: 1px solid #dfdfdf;list-style: none;margin-bottom: 15px;">';
$html .= '<p><a href="' . $url . '">' . $name . '</a></p>';
$html .= '</li > ';
} else {
$product = $complaintProduct->getProduct();
if ($product instanceof Product) {
$url = $this->routerHelper->generateUrl('admin.product.edit', ['id' => $product->getId()]);
$name = sprintf('%s x %s', $complaintProduct->getQuantity(), $product->translate()->getName());
$html .= '<li style="border-bottom: 1px solid #dfdfdf;list-style: none;margin-bottom: 15px;">';
$html .= '<p><a href="' . $url . '">' . $name . '</a></p>';
$html .= '</li > ';
}
}
});
$fieldset->addChild(
$builder->getElement('static_text', [
'text' => $html,
])
);
/** @var FieldsetInterface $fieldset */
$fieldset = $form->addChild(
$builder->getElement('nested_fieldset', [
'name' => 'comment_data',
'label' => 'complaint.fieldset.comments',
])
);
$html = '<ul class="changes-detailed">';
$history->map(function (ComplaintHistory $complaintHistory) use (&$html, $complaint) {
$user = $complaintHistory->getUser();
$client = $complaint->getClient();
$date = $complaintHistory->getCreatedAt()->format('Y-m-d H:i:s');
$status = $complaintHistory->getStatus() instanceof ComplaintStatus ? $complaintHistory->getStatus()->translate()->getName(
) : '';
$html .= '<li style="border-bottom: 1px solid #dfdfdf;list-style: none;margin-bottom: 15px;">';
if ($user instanceof User) {
$html .= '<h4><span>' . $date . ' ' . $user->getFirstName() . ' ' . $user->getLastName(
) . ' (' . $status . ')</span></h4>';
} else {
$html .= '<h4><span>' . $date . ' ' . $client->getContactDetails()->getFirstName() . ' ' . $client->getContactDetails(
)->getLastName() . ' (' . $status . ')</h4>';
}
$html .= ' <p>' . nl2br($complaintHistory->getComment()) . '</p>';
if (!empty($complaintHistory->getFiles())) {
$html .= ' <p><strong>Załączniki</strong></p>';
foreach ($complaintHistory->getFiles() as $file) {
$html .= '<p><a href="/' . $complaint->getWebPath() . '/' . $file . '" target="_blank">' . $file . '</a></p>';
}
}
$html .= '</li > ';
});
$html .= '</ul > ';
$fieldset->addChild(
$builder->getElement('static_text', [
'text' => $html,
])
);
}
}
public function onClientAdminFormPostInit(FormEvent $event)
{
$resource = $event->getResource();
if ($resource instanceof Client) {
$form = $event->getForm();
$builder = $event->getFormBuilder();
/** @var FieldsetInterface $fieldset */
$requiredData = $form->getChildren()->get('required_data');
$clientDetailsData = $requiredData->addChild(
$builder->getElement('nested_fieldset', [
'name' => 'clientDetails',
'label' => 'client.heading.client_details',
])
);
$clientDetailsData->addChild(
$builder->getElement('select', [
'name' => 'clientDetails.sex',
'label' => 'Płeć',
'options' => [
'm' => 'Mężczyzna',
'f' => 'Kobieta',
],
])
)->setValue($resource->getClientDetails()->getSex());
$clientDetailsData->addChild(
$builder->getElement('text_field', [
'name' => 'clientDetails.username',
'label' => 'client.label.username',
'rules' => [
$builder->getRule('required'),
],
])
)->setValue($resource->getClientDetails()->getUsername());
if ($form->getResource() instanceof Client && null === $form->getResource()->getId()) {
$clientDetailsData->addChild(
$builder->getElement('text_field', [
'name' => 'clientDetails.hashedPassword',
'label' => 'client.label.password',
'rules' => [
$builder->getRule('required'),
],
])
)->setValue($this->securityHelper->generateRandomPassword());
}
$clientDetailsData->addChild(
$builder->getElement('checkbox', [
'name' => 'clientDetails.conditionsAccepted',
'label' => 'client.label.accept_conditions_admin',
])
)->setValue(null === $resource->getId() ? true : $resource->getClientDetails()->isConditionsAccepted());
$clientDetailsData->addChild(
$builder->getElement('checkbox', [
'name' => 'clientDetails.privacyAccepted',
'label' => 'client.label.accept_privacy_admin',
])
)->setValue(null === $resource->getId() ? true : $resource->getClientDetails()->isPrivacyAccepted());
$clientDetailsData->addChild(
$builder->getElement('checkbox', [
'name' => 'clientDetails.newsletterAccepted',
'label' => 'client.label.accept_newsletter_admin',
])
)->setValue(null === $resource->getId() ? true : $resource->getClientDetails()->isNewsletterAccepted());
$clientDetailsData->addChild(
$builder->getElement('checkbox', [
'name' => 'clientDetails.registrationConfirmed',
'label' => 'client.label.registration_confirmed',
])
)->setValue(null === $resource->getId() ? true : $resource->getClientDetails()->isRegistrationConfirmed());
}
}
public function onClientAdminFormInit(FormEvent $event)
{
$resource = $event->getResource();
if ($resource instanceof Client) {
$form = $event->getForm();
$builder = $event->getFormBuilder();
$billingAddresses = $resource->getBillingAddresses();
$shippingAddresses = $resource->getShippingAddresses();
/** @var FieldsetInterface $fieldset */
$fieldset = $form->getChildren()->get('billingAddress');
$billingAddressesFieldset = $fieldset->addChild(
$builder->getElement('nested_fieldset', [
'name' => 'billingAddresses',
'label' => 'client.fieldset.addresses',
])
);
if ($this->routerHelper->getCurrentAction() === 'editAction') {
$billingAddressesFieldset->addChild(
$builder->getElement('button', [
'name' => 'add_billing_address',
'label' => 'Dodaj adres',
'target' => '_self',
'url' => $this->routerHelper->generateUrl('admin.client.add_address', [
'id' => $resource->getId(),
'type' => 'billing',
]),
])
);
}
if ($billingAddresses->count()) {
$html = '<ul class="changes-detailed">';
$billingAddresses->map(function (ClientAddress $address) use (&$html) {
$addressUrl = $this->routerHelper->generateUrl('admin.client.edit_address', ['id' => $address->getId()]);
$html .= '<li style="border-bottom: 1px solid #dfdfdf;list-style: none;margin-bottom: 15px;">';
$html .= ' <p>' . $this->formatClientAddress($address) . '</p>';
$html .= ' <p><a href="' . $addressUrl . '">Edycja</a></p > ';
$html .= '</li > ';
});
$html .= '</ul > ';
$billingAddressesFieldset->addChild(
$builder->getElement('static_text', [
'text' => $html,
])
);
}
/** @var FieldsetInterface $fieldset */
$fieldset = $form->getChildren()->get('shippingAddress');
$shippingAddressesFieldset = $fieldset->addChild(
$builder->getElement('nested_fieldset', [
'name' => 'shippingAddresses',
'label' => 'client.fieldset.addresses',
])
);
if ($this->routerHelper->getCurrentAction() === 'editAction') {
$shippingAddressesFieldset->addChild(
$builder->getElement('button', [
'name' => 'add_shipping_address',
'label' => 'Dodaj adres',
'target' => '_self',
'url' => $this->routerHelper->generateUrl('admin.client.add_address', [
'id' => $resource->getId(),
'type' => 'shipping',
]),
])
);
}
if ($shippingAddresses->count()) {
$html = '<ul class="changes-detailed">';
$shippingAddresses->map(function (ClientAddress $address) use (&$html) {
$addressUrl = $this->routerHelper->generateUrl('admin.client.edit_address', ['id' => $address->getId()]);
$html .= '<li style="border-bottom: 1px solid #dfdfdf;list-style: none;margin-bottom: 15px;">';
$html .= ' <p>' . $this->formatClientAddress($address) . '</p>';
$html .= ' <p><a href="' . $addressUrl . '">Edycja</a></p > ';
$html .= '</li > ';
});
$html .= '</ul > ';
$shippingAddressesFieldset->addChild(
$builder->getElement('static_text', [
'text' => $html,
])
);
}
}
}
public function onClientPreUpdate(EntityEvent $event)
{
$resource = $event->getEntity();
if ($resource instanceof Client) {
$shop = $resource->getShop();
if ($shop->isRegistrationConfirmationRequired()) {
$resource->setEnabled($resource->getClientDetails()->isRegistrationConfirmed());
}
if ($this->geocoderSystemConfigurator->isClientEnabled()) {
$this->geocodeClientAddresses($resource);
}
}
}
public function onClientPostUpdate(EntityEvent $event)
{
$resource = $event->getEntity();
if ($resource instanceof Client) {
$email = $resource->getUsername();
$shop = $resource->getShop();
$subscriber = $this->newsletterSubscriberManager->getRepository()->findOneBy([
'email' => $email,
'shop' => $shop,
]);
if (!$subscriber instanceof NewsletterSubscriber) {
/** @var NewsletterSubscriber $subscriber */
$subscriber = $this->newsletterSubscriberManager->initResource();
$subscriber->setEnabled($resource->getClientDetails()->isNewsletterAccepted());
$subscriber->setEmail($email);
$subscriber->setConditionsAccepted($resource->getClientDetails()->isNewsletterAccepted());
$subscriber->setClient($resource);
$subscriber->setShop($shop);
if (false === $subscriber->getShop()->getNewsletterSettings()->isSendConfirmationEmail()) {
$subscriber->setEnabled(true);
}
$this->newsletterSubscriberManager->createResource($subscriber);
}
}
}
public function onClientPostCreate(EntityEvent $event)
{
$resource = $event->getEntity();
if ($resource instanceof Client) {
$email = $resource->getUsername();
$shop = $resource->getShop();
$subscriber = $this->newsletterSubscriberManager->getRepository()->findOneBy([
'email' => $email,
'shop' => $shop,
]);
if (!$subscriber instanceof NewsletterSubscriber) {
/** @var NewsletterSubscriber $subscriber */
$subscriber = $this->newsletterSubscriberManager->initResource();
$subscriber->setEnabled($resource->getClientDetails()->isNewsletterAccepted());
$subscriber->setEmail($email);
$subscriber->setConditionsAccepted($resource->getClientDetails()->isNewsletterAccepted());
$subscriber->setClient($resource);
$subscriber->setShop($shop);
if (false === $subscriber->getShop()->getNewsletterSettings()->isSendConfirmationEmail()) {
$subscriber->setEnabled(true);
}
$this->newsletterSubscriberManager->createResource($subscriber);
}
}
}
public function onClientPreCreate(EntityEvent $event)
{
$entity = $event->getEntity();
if ($entity instanceof Client) {
if ($entity->getContactDetails()->getEmail() === '') {
$entity->getContactDetails()->setEmail($entity->getUsername());
}
if ($entity->getBillingAddress()->isNotEmpty()) {
if (false === $entity->getShippingAddress()->isNotEmpty()) {
$entity->getShippingAddress()->setFirstName($entity->getBillingAddress()->getFirstName());
$entity->getShippingAddress()->setLastName($entity->getBillingAddress()->getLastName());
$entity->getShippingAddress()->setLine1($entity->getBillingAddress()->getLine1());
$entity->getShippingAddress()->setLine2($entity->getBillingAddress()->getLine2());
$entity->getShippingAddress()->setPostalCode($entity->getBillingAddress()->getPostalCode());
$entity->getShippingAddress()->setCity($entity->getBillingAddress()->getCity());
$entity->getShippingAddress()->setCompanyName($entity->getBillingAddress()->getCompanyName());
$entity->getShippingAddress()->setCountry($entity->getBillingAddress()->getCountry());
$entity->getShippingAddress()->setState($entity->getBillingAddress()->getState());
}
$billingAddress = new ClientAddress(AddressInterface::ADDRESS_TYPE_BILLING, $entity);
$shippingAddress = new ClientAddress(AddressInterface::ADDRESS_TYPE_SHIPPING, $entity);
$billingAddress->setDefaultAddress(false);
$shippingAddress->setDefaultAddress(false);
if ($this->geocoderSystemConfigurator->isClientEnabled()) {
$this->geocodeClientAddresses($entity);
}
$this->entityManager->persist($billingAddress);
$this->entityManager->persist($shippingAddress);
}
}
}
public function onClientAddressPreUpdate(EntityEvent $event)
{
$geocoder = $this->getGeocoder();
$entity = $event->getEntity();
if ($entity instanceof ClientAddress) {
try {
$geocodeAddress = $geocoder->geocode($entity->getGeocodableAddress())->first();
if ($geocodeAddress instanceof Location) {
$coordinates = $geocodeAddress->getCoordinates();
$entity->setLongitude($coordinates->getLongitude());
$entity->setLatitude($coordinates->getLatitude());
}
} catch (\Exception $e) {
}
}
}
private function geocodeClientAddresses(Client $client)
{
$geocoder = $this->getGeocoder();
try {
$billingAddress = $client->getBillingAddress();
$geocodeBilling = $geocoder->geocode($billingAddress->getGeocodableAddress())->first();
if ($geocodeBilling instanceof Location) {
$coordinates = $geocodeBilling->getCoordinates();
$billingAddress->setLongitude($coordinates->getLongitude());
$billingAddress->setLatitude($coordinates->getLatitude());
}
} catch (\Exception $e) {
}
try {
$shippingAddress = $client->getShippingAddress();
$geocodeShipping = $geocoder->geocode($shippingAddress->getGeocodableAddress())->first();
if ($geocodeShipping instanceof Location) {
$coordinates = $geocodeShipping->getCoordinates();
$shippingAddress->setLongitude($coordinates->getLongitude());
$shippingAddress->setLatitude($coordinates->getLatitude());
}
} catch (\Exception $e) {
}
$client->getBillingAddresses()->map(function (ClientAddress $clientAddress) use ($geocoder) {
try {
$geocodeAddress = $geocoder->geocode($clientAddress->getGeocodableAddress())->first();
if ($geocodeAddress instanceof Location) {
$coordinates = $geocodeAddress->getCoordinates();
$clientAddress->setLongitude($coordinates->getLongitude());
$clientAddress->setLatitude($coordinates->getLatitude());
}
} catch (\Exception $e) {
}
});
$client->getShippingAddresses()->map(function (ClientAddress $clientAddress) use ($geocoder) {
try {
$geocodeAddress = $geocoder->geocode($clientAddress->getGeocodableAddress())->first();
if ($geocodeAddress instanceof Location) {
$coordinates = $geocodeAddress->getCoordinates();
$clientAddress->setLongitude($coordinates->getLongitude());
$clientAddress->setLatitude($coordinates->getLatitude());
}
} catch (\Exception $e) {
}
});
}
private function getGeocoder(): Geocoder
{
return $this->geocoderProvider->createGeocoder();
}
protected function formatClientAddress(ClientAddress $address, $lineSeparator = '<br />')
{
$lines = [];
$lines[] = sprintf('%s %s', $address->getFirstName(), $address->getLastName());
if ('' !== $address->getCompanyName()) {
$lines[] = $address->getCompanyName();
}
$lines[] = sprintf('%s, %s %s', $address->getLine1(), $address->getLine2(), $address->getLine3());
$lines[] = sprintf('%s, %s %s', $address->getCountry(), $address->getPostalCode(), $address->getCity());
return implode($lineSeparator, $lines);
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$client = $event->getAuthenticationToken()->getUser();
if ($client instanceof Client) {
if ($this->orderProvider->hasCurrentOrder()) {
$order = $this->orderProvider->getCurrentOrder();
$this->clientAddressHelper->syncBillingAddress($client->getBillingAddress(), $order->getBillingAddress(), true);
$this->clientAddressHelper->syncShippingAddress($client->getShippingAddress(), $order->getShippingAddress(), true);
$this->clientAddressHelper->syncContactDetails($client->getContactDetails(), $order->getContactDetails(), true);
}
if (!empty($client->getBillingAddress()->getCompanyName())) {
$client->getBillingAddress()->setCompanyAddress(true);
} else {
$client->getBillingAddress()->setCompanyAddress(false);
}
if (!empty($client->getShippingAddress()->getCompanyName())) {
$client->getShippingAddress()->setCompanyAddress(true);
} else {
$client->getShippingAddress()->setCompanyAddress(false);
}
$email = $client->getContactDetails()->getEmail();
$orders = $this->entityManager->getRepository(Order::class)->findBy(['contactDetails.email' => $email]);
/** @var Order $order */
foreach ($orders as $order) {
if (!$order->getClient() instanceof Client) {
$order->setClient($client);
}
}
$this->entityManager->flush();
}
}
}