vendor/ezsystems/ezcommerce-shop/src/Silversolutions/Bundle/EshopBundle/Service/StandardTemplateDebitorService.php line 20

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace Silversolutions\Bundle\EshopBundle\Service;
  7. use eZ\Publish\Core\MVC\ConfigResolverInterface;
  8. use Silversolutions\Bundle\EshopBundle\Api\CountryZoneServiceInterface;
  9. use Silversolutions\Bundle\EshopBundle\Api\TemplateDebitorServiceInterface;
  10. use Silversolutions\Bundle\EshopBundle\Entities\Messages\Document\Party;
  11. /**
  12.  * This service is an implementation of the TemplateDebitorServiceInterface
  13.  * It assigns the template debitor customer and contact numbers depending on given parties and configuration.
  14.  *
  15.  * Class StandardTemplateDebitorService
  16.  */
  17. class StandardTemplateDebitorService implements TemplateDebitorServiceInterface
  18. {
  19.     const DEFAULT_IDENTIFIER 'default';
  20.     /**
  21.      * @var CountryZoneServiceInterface
  22.      */
  23.     private $countryZoneService;
  24.     /**
  25.      * fallback country (depending on siteaccess) that will be used if no user parties were set.
  26.      *
  27.      * @var string
  28.      */
  29.     private $fallbackCountry;
  30.     /**
  31.      * list of template debitor customer numbers
  32.      * Example:
  33.      * EU:
  34.      *      DE:
  35.      *          WVGW: V0002
  36.      *          DLRG: V0003
  37.      *          WVGW_DLRG: V0004
  38.      *          default: V0001
  39.      *      PL:
  40.      *          default: V0001
  41.      *      default: V0001
  42.      * World:
  43.      *      USA:
  44.      *          default: V0010
  45.      *      default: V0001
  46.      * default: V0001.
  47.      *
  48.      * @var array
  49.      */
  50.     private $templateDebitorCustomerNumbers;
  51.     /**
  52.      * list of template debitor contact numbers
  53.      * Example:
  54.      * EU:
  55.      *      DE:
  56.      *          WVGW: K0002
  57.      *          DLRG: K0003
  58.      *          WVGW_DLRG: K0004
  59.      *          default: K0001
  60.      *      PL:
  61.      *          default: K0001
  62.      *      default: K0001
  63.      * World:
  64.      *      USA:
  65.      *          default: K0010
  66.      *      default: K0001
  67.      * default: K0001.
  68.      *
  69.      * @var array
  70.      */
  71.     private $templateDebitorContactNumbers;
  72.     /** @var ConfigResolverInterface */
  73.     protected $configResolver;
  74.     /**
  75.      * inject the dependencies.
  76.      *
  77.      * @param CountryZoneServiceInterface $countryZoneService
  78.      * @param ConfigResolverInterface $configResolver
  79.      */
  80.     public function __construct(
  81.         CountryZoneServiceInterface $countryZoneService,
  82.         ConfigResolverInterface $configResolver
  83.     ) {
  84.         $this->countryZoneService $countryZoneService;
  85.         $this->configResolver $configResolver;
  86.     }
  87.     /**
  88.      * sets the parameters.
  89.      *
  90.      * @param $templateDebitorCustomerNumbers
  91.      * @param $templateDebitorContactNumbers
  92.      */
  93.     public function setParameters($templateDebitorCustomerNumbers$templateDebitorContactNumbers)
  94.     {
  95.         $this->fallbackCountry $this->configResolver->getParameter('template_debitor_country''siso_core');
  96.         $this->templateDebitorCustomerNumbers $templateDebitorCustomerNumbers;
  97.         $this->templateDebitorContactNumbers $templateDebitorContactNumbers;
  98.     }
  99.     /**
  100.      * This method will return template customer number depending on the given parties.
  101.      * If no parties are provided, configuration shall be used to provide fallback template customer number.
  102.      *
  103.      * @param \Silversolutions\Bundle\EshopBundle\Entities\Messages\Document\Party $buyerParty
  104.      * @param Party $invoiceParty
  105.      * @param Party $deliveryParty
  106.      *
  107.      * @return string
  108.      */
  109.     public function getTemplateCustomerNumber(
  110.         Party $buyerParty null,
  111.         Party $invoiceParty null,
  112.         Party $deliveryParty null
  113.     ) {
  114.         return $this->getTemplateDebitorNumber(
  115.             $buyerParty,
  116.             $invoiceParty,
  117.             $deliveryParty,
  118.             $this->templateDebitorCustomerNumbers
  119.         );
  120.     }
  121.     /**
  122.      * This method will return template contact number depending on the given parties.
  123.      * If no parties are provided, configuration shall be used to provide fallback template contact number.
  124.      *
  125.      * @param \Silversolutions\Bundle\EshopBundle\Entities\Messages\Document\Party $buyerParty
  126.      * @param Party $invoiceParty
  127.      * @param Party $deliveryParty
  128.      *
  129.      * @return string
  130.      */
  131.     public function getTemplateContactNumber(
  132.         Party $buyerParty null,
  133.         Party $invoiceParty null,
  134.         Party $deliveryParty null
  135.     ) {
  136.         return $this->getTemplateDebitorNumber(
  137.             $buyerParty,
  138.             $invoiceParty,
  139.             $deliveryParty,
  140.             $this->templateDebitorContactNumbers
  141.         );
  142.     }
  143.     /**
  144.      * this method is used to return the template debitor number
  145.      * depending on given $templateDebitorNumbers it will be either template debitor customer number
  146.      * or template debitor contact number.
  147.      *
  148.      * the configuration for template debitor customer- and contact numbers must have the same structure
  149.      *
  150.      * @param Party $buyerParty
  151.      * @param Party $invoiceParty
  152.      * @param Party $deliveryParty
  153.      * @param $templateDebitorNumbers
  154.      *
  155.      * @return string
  156.      */
  157.     protected function getTemplateDebitorNumber(
  158.         Party $buyerParty null,
  159.         /** @noinspection PhpUnusedParameterInspection */
  160.         Party $invoiceParty null,
  161.         /** @noinspection PhpUnusedParameterInspection */
  162.         Party $deliveryParty null,
  163.         $templateDebitorNumbers
  164.     ) {
  165.         $customerGroup self::DEFAULT_IDENTIFIER;
  166.         //if buyer party not set, use fallback country from configuration
  167.         if (!$buyerParty instanceof Party) {
  168.             $country $this->fallbackCountry;
  169.         } else {
  170.             $country $buyerParty->PostalAddress->Country->IdentificationCode->value;
  171.         }
  172.         //get the zone for the country
  173.         $zone $this->countryZoneService->getZone($country);
  174.         if ($buyerParty instanceof Party
  175.             && isset($buyerParty->SesExtension->value['CustomerGroups'])
  176.         ) {
  177.             $buyerCustomerGroups $buyerParty->SesExtension->value['CustomerGroups'];
  178.             foreach ($buyerCustomerGroups as $buyerCustomerGroup) {
  179.                 $customerGroup implode('_'$buyerCustomerGroup['Code']);
  180.             }
  181.         }
  182.         foreach ($templateDebitorNumbers as $templateZone => $templateCountries) {
  183.             if ($templateZone === $zone) {
  184.                 foreach ($templateCountries as $templateCountry => $templateCustomerGroups) {
  185.                     if ($templateCountry === $country) {
  186.                         foreach ($templateCustomerGroups as $templateCustomerGroup => $templateNumber) {
  187.                             if ($templateCustomerGroup === $customerGroup) {
  188.                                 return $templateNumber;
  189.                             } else {
  190.                                 //no customer groups fixes, return default customer group
  191.                                 return $templateCustomerGroups[self::DEFAULT_IDENTIFIER];
  192.                             }
  193.                         }
  194.                     } else {
  195.                         //no country fixes, return default country
  196.                         return $templateCountries[self::DEFAULT_IDENTIFIER];
  197.                     }
  198.                 }
  199.             } else {
  200.                 //no zone fixes, return default zone
  201.                 return $templateDebitorNumbers[self::DEFAULT_IDENTIFIER];
  202.             }
  203.         }
  204.         return $templateDebitorNumbers[self::DEFAULT_IDENTIFIER];
  205.     }
  206. }