custom/static-plugins/WexoBusinessCentralOverride/src/WexoBusinessCentralOverride.php line 15

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wexo\BusinessCentralOverride;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  9. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  10. use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
  11. use Shopware\Core\System\CustomField\CustomFieldTypes;
  12. class WexoBusinessCentralOverride extends Plugin
  13. {
  14.     //Customer custom field definitions
  15.     private const CUSTOM_SET_CUSTOMER 'customer_business_central';
  16.     public const CUSTOM_FIELD_ALLOW_CREDIT 'allow_credit';
  17.     public const CUSTOM_FIELD_GLN 'gln';
  18.     public const CUSTOM_FIELD_CURRENCY_CODE 'currency_code';
  19.     public const CUSTOM_FIELD_PRICE_GROUP 'price_group';
  20.     public const CUSTOM_FIELD_DISCOUNT_GROUP 'discount_group';
  21.     public const CUSTOM_FIELD_CONTACT_PERSON 'contact_person';
  22.     public const CUSTOM_FIELD_HOMEPAGE 'homepage';
  23.     //Integration keys
  24.     public const PRODUCT_SPECIAL_ITEM_KEY 'Speciel_Item';
  25.     //Order export status
  26.     private const CUSTOM_SET_ORDER_EXPORT 'order_export';
  27.     public const CUSTOM_FIELD_ORDER_EXPORTED_DATE 'order_exported_date';
  28.     protected ?Context $context null;
  29.     protected ?EntityRepository $customFieldSetRepository null;
  30.     protected ?EntityRepository $customFieldRepository null;
  31.     /**
  32.      * @param InstallContext $installContext
  33.      * @return void
  34.      */
  35.     public function install(InstallContext $installContext): void
  36.     {
  37.         $this->updateCustomFields();
  38.     }
  39.     /**
  40.      * @param UpdateContext $updateContext
  41.      * @return void
  42.      */
  43.     public function update(UpdateContext $updateContext): void
  44.     {
  45.         $this->updateCustomFields();
  46.     }
  47.     /**
  48.      * @return void
  49.      */
  50.     public function updateCustomFields(): void
  51.     {
  52.         //Customer custom fields
  53.         $customFieldSet $this->addCustomFieldSet(
  54.             self::CUSTOM_SET_CUSTOMER,
  55.             [
  56.                 'da-DK' => 'Business Central',
  57.                 'en-GB' => 'Business Central',
  58.                 'de-DE' => 'Business Central'
  59.             ],
  60.             [
  61.                 'customer'
  62.             ]
  63.         );
  64.         $this->addCustomField(
  65.             self::CUSTOM_FIELD_ALLOW_CREDIT,
  66.             CustomFieldTypes::BOOL,
  67.             [
  68.                 'da-DK' => 'Tillad kredit',
  69.                 'en-GB' => 'Allow credit',
  70.                 'de-DE' => 'Kredit zulassen'
  71.             ],
  72.             1,
  73.             $customFieldSet->getId()
  74.         );
  75.         $this->addCustomField(
  76.             self::CUSTOM_FIELD_GLN,
  77.             CustomFieldTypes::TEXT,
  78.             [
  79.                 'da-DK' => 'GLN',
  80.                 'en-GB' => 'GLN',
  81.                 'de-DE' => 'GLN'
  82.             ],
  83.             2,
  84.             $customFieldSet->getId()
  85.         );
  86.         $this->addCustomField(
  87.             self::CUSTOM_FIELD_CURRENCY_CODE,
  88.             CustomFieldTypes::TEXT,
  89.             [
  90.                 'da-DK' => 'Valuta kode',
  91.                 'en-GB' => 'Currency code',
  92.                 'de-DE' => 'Währungscode'
  93.             ],
  94.             3,
  95.             $customFieldSet->getId()
  96.         );
  97.         $this->addCustomField(
  98.             self::CUSTOM_FIELD_PRICE_GROUP,
  99.             CustomFieldTypes::TEXT,
  100.             [
  101.                 'da-DK' => 'Prisgruppe',
  102.                 'en-GB' => 'Price group',
  103.                 'de-DE' => 'Preisgruppe'
  104.             ],
  105.             4,
  106.             $customFieldSet->getId()
  107.         );
  108.         $this->addCustomField(
  109.             self::CUSTOM_FIELD_DISCOUNT_GROUP,
  110.             CustomFieldTypes::TEXT,
  111.             [
  112.                 'da-DK' => 'Rabatgruppe',
  113.                 'en-GB' => 'Discount group',
  114.                 'de-DE' => 'Rabattgruppe'
  115.             ],
  116.             5,
  117.             $customFieldSet->getId()
  118.         );
  119.         $this->addCustomField(
  120.             self::CUSTOM_FIELD_CONTACT_PERSON,
  121.             CustomFieldTypes::TEXT,
  122.             [
  123.                 'da-DK' => 'Kontaktperson',
  124.                 'en-GB' => 'Contact person',
  125.                 'de-DE' => 'Gesprächspartner'
  126.             ],
  127.             6,
  128.             $customFieldSet->getId()
  129.         );
  130.         $this->addCustomField(
  131.             self::CUSTOM_FIELD_HOMEPAGE,
  132.             CustomFieldTypes::TEXT,
  133.             [
  134.                 'da-DK' => 'Hjemmeside',
  135.                 'en-GB' => 'Homepage',
  136.                 'de-DE' => 'Startseite'
  137.             ],
  138.             7,
  139.             $customFieldSet->getId()
  140.         );
  141.         //Order export
  142.         $customFieldSet $this->addCustomFieldSet(
  143.             self::CUSTOM_SET_ORDER_EXPORT,
  144.             [
  145.                 'da-DK' => 'Business Central order export',
  146.                 'en-GB' => 'Business Central order export',
  147.                 'de-DE' => 'Business Central order export'
  148.             ],
  149.             [
  150.                 'order',
  151.                 'order_line_item'
  152.             ]
  153.         );
  154.         $this->addCustomField(
  155.             self::CUSTOM_FIELD_ORDER_EXPORTED_DATE,
  156.             CustomFieldTypes::DATETIME,
  157.             [
  158.                 'da-DK' => 'Eksport dato',
  159.                 'en-GB' => 'Export date',
  160.                 'de-DE' => 'Exportdatum'
  161.             ],
  162.             1,
  163.             $customFieldSet->getId()
  164.         );
  165.     }
  166.     /**
  167.      * @param string $name
  168.      * @param array $labels
  169.      * @param array $entityNames
  170.      * @return CustomFieldSetEntity
  171.      */
  172.     protected function addCustomFieldSet(
  173.         string $name,
  174.         array $labels,
  175.         array $entityNames
  176.     ): CustomFieldSetEntity {
  177.         $this->context $this->context ?? Context::createDefaultContext();
  178.         if (! $this->customFieldSetRepository) {
  179.             $this->customFieldSetRepository $this->container->get('custom_field_set.repository');
  180.         }
  181.         $criteria = new Criteria();
  182.         $criteria->addFilter(new EqualsFilter('name'$name));
  183.         /** @var CustomFieldSetEntity $customFieldSet */
  184.         $customFieldSet $this->customFieldSetRepository->search(
  185.             $criteria,
  186.             $this->context
  187.         )->first();
  188.         if (! $customFieldSet) {
  189.             $relations = [];
  190.             foreach ($entityNames as $entityName) {
  191.                 $relations[] = [
  192.                     'entityName' => $entityName
  193.                 ];
  194.             }
  195.             $this->customFieldSetRepository->create(
  196.                 [
  197.                     [
  198.                         'name' => $name,
  199.                         'config' => [
  200.                             'label' => $labels
  201.                         ],
  202.                         'relations' => $relations,
  203.                     ],
  204.                 ],
  205.                 $this->context
  206.             );
  207.             $customFieldSet $this->customFieldSetRepository->search(
  208.                 $criteria,
  209.                 $this->context
  210.             )->first();
  211.         }
  212.         return $customFieldSet;
  213.     }
  214.     /**
  215.      * @param string $name
  216.      * @param string $type
  217.      * @param array $labels
  218.      * @param int $position
  219.      * @param string $customFieldSetId
  220.      */
  221.     protected function addCustomField(
  222.         string $name,
  223.         string $type,
  224.         array $labels,
  225.         int $position,
  226.         string $customFieldSetId
  227.     ): void {
  228.         $this->context $this->context ?? Context::createDefaultContext();
  229.         if (! $this->customFieldRepository) {
  230.             $this->customFieldRepository $this->container->get('custom_field.repository');
  231.         }
  232.         $criteria = new Criteria();
  233.         $criteria->addFilter(new EqualsFilter('name'$name));
  234.         $customFieldExists $this->customFieldRepository->searchIds(
  235.             $criteria,
  236.             $this->context
  237.         )->firstId();
  238.         if (!$customFieldExists) {
  239.             if (! $this->customFieldSetRepository) {
  240.                 $this->customFieldSetRepository $this->container->get('custom_field_set.repository');
  241.             }
  242.             $this->customFieldSetRepository->upsert([
  243.                 [
  244.                     'id'           => $customFieldSetId,
  245.                     'customFields' => [
  246.                         [
  247.                             'name'   => $name,
  248.                             'type'   => $type,
  249.                             'config' => [
  250.                                 'label'               => $labels,
  251.                                 'componentName'       => 'sw-field',
  252.                                 'customFieldType'     => $type == CustomFieldTypes::BOOL 'checkbox' $type,
  253.                                 'customFieldPosition' => $position,
  254.                             ],
  255.                         ]
  256.                     ]
  257.                 ]
  258.             ], $this->context);
  259.         }
  260.     }
  261. }