custom/plugins/CompraEsiSW6/src/CompraEsiSW6.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Compra\EsiSW6;
  3. use Compra\EsiSW6\Administration\Service\ConfigurationService;
  4. use Compra\EsiSW6\Import\Service\ImportService;
  5. use Compra\FoundationSW6\Core\Content\Mail\MailService;
  6. use Compra\FoundationSW6\Core\System\CustomFieldService;
  7. use Shopware\Core\Framework\Api\Context\SystemSource;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  12. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  14. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  15. use Shopware\Core\System\CustomField\CustomFieldTypes;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. class CompraEsiSW6 extends Plugin
  18. {
  19.     public function build(ContainerBuilder $container): void
  20.     {
  21.         $container->setParameter('compra_esi_s_w6.plugin_dir'$this->getPath());
  22.         parent::build($container);
  23.     }
  24.     /**
  25.      * @param InstallContext $installContext
  26.      */
  27.     public function install(InstallContext $installContext): void
  28.     {
  29.         parent::install($installContext);
  30.         $this->addCustomFields($installContext->getContext());
  31.         // handle update functions
  32.         $this->handleUpdates($installContext);
  33.         $mailService $this->container->get(MailService::class);
  34.         if (!$mailService) {
  35.             return;
  36.         }
  37.         $this->removeMailTemplates($mailService$installContext->getContext());
  38.         $this->createMailTemplates($mailService$installContext->getContext());
  39.     }
  40.     public function update(UpdateContext $updateContext): void
  41.     {
  42.         parent::update($updateContext);
  43.         $this->addCustomFields($updateContext->getContext());
  44.         $configurationService $this->container->get(ConfigurationService::class);
  45.         if ($configurationService) {
  46.             $configurationService->reloadEsiConfig();
  47.         }
  48.         // handle update functions
  49.         $this->handleUpdates($updateContext);
  50.         $mailService $this->container->get(MailService::class);
  51.         if (!$mailService) {
  52.             return;
  53.         }
  54.         $this->removeMailTemplates($mailService$updateContext->getContext());
  55.         $this->createMailTemplates($mailService$updateContext->getContext());
  56.     }
  57.     /**
  58.      * @param UninstallContext $uninstallContext
  59.      * @throws InconsistentCriteriaIdsException
  60.      */
  61.     public function uninstall(UninstallContext $uninstallContext): void
  62.     {
  63.         parent::uninstall($uninstallContext);
  64.         $mailService $this->container->get(MailService::class);
  65.         if (!$mailService || $uninstallContext->keepUserData()) {
  66.             return;
  67.         }
  68.         $this->removeMailTemplates($mailService$uninstallContext->getContext());
  69.     }
  70.     public function activate(ActivateContext $activateContext): void
  71.     {
  72.         parent::activate($activateContext);
  73.         $configurationService $this->container->get(ConfigurationService::class);
  74.         if ($configurationService) {
  75.             $configurationService->reloadEsiConfig();
  76.         }
  77.     }
  78.     /**
  79.      * Function to create the custom fields for the plugin.
  80.      *
  81.      * @param $context
  82.      */
  83.     public function addCustomFields($context): void
  84.     {
  85.         $customFieldService $this->container->get(CustomFieldService::class);
  86.         if (!$customFieldService) {
  87.             return;
  88.         }
  89.         try {
  90.             $customFieldService->createOrUpdateCustomFields([
  91.                 [
  92.                     'name' => 'compra_is_esi_source',
  93.                     'type' => CustomFieldTypes::BOOL
  94.                 ],
  95.                 [
  96.                     'name' => 'compra_is_esi_deleted',
  97.                     'type' => CustomFieldTypes::BOOL
  98.                 ],
  99.                 [
  100.                     'name' => 'compra_media_file_hash',
  101.                     'type' => CustomFieldTypes::TEXT
  102.                 ],
  103.                 [
  104.                     'name' => 'compra_eevo_lfdnr'// tables: country, product, tax
  105.                     'type' => CustomFieldTypes::INT
  106.                 ],
  107.                 [
  108.                     'name' => 'compra_eevo_landkuerzel'// table country
  109.                     'type' => CustomFieldTypes::TEXT
  110.                 ],
  111.                 [
  112.                     'name' => 'compra_esi_xml_created',  // order
  113.                     'type' => CustomFieldTypes::BOOL
  114.                 ],
  115.                 [
  116.                     'name' => 'compra_esi_xml_export',  // customer, address
  117.                     'type' => CustomFieldTypes::BOOL
  118.                 ],
  119.                 [
  120.                     'name' => 'compra_esi_xml_export_address'// customer
  121.                     'type' => CustomFieldTypes::BOOL
  122.                 ],
  123.                 [
  124.                     'name' => 'compra_esi_request_password_uuid',
  125.                     'type' => CustomFieldTypes::TEXT
  126.                 ],
  127.                 [
  128.                     'name' => 'compra_media_is_missing',    // media file not found or corrupted
  129.                     'type' => CustomFieldTypes::BOOL
  130.                 ],
  131.                 [
  132.                     'name' => 'compra_media_is_missing_error',  // error message for media file not found or corrupted
  133.                     'type' => CustomFieldTypes::TEXT
  134.                 ],
  135.                 [
  136.                     'name' => 'compra_eevo_ansprechpartner_lfdnr',   // e.g. for b2b_debtor_contact
  137.                     'type' => CustomFieldTypes::INT
  138.                 ],
  139.                 [
  140.                     'name' => 'compra_eevo_debtor_customernumber',      // e.g. for b2b_debtor_contact
  141.                     'type' => CustomFieldTypes::TEXT
  142.                 ]
  143.             ], $context);
  144.             // custom field set for customer, customer_address and order_address
  145.             $customFieldService->createOrUpdateCustomFieldSets([
  146.                 [
  147.                     'name' => 'compra_esi_customer_and_address',
  148.                     'config' => [
  149.                         'label' => [
  150.                             'de-DE' => 'COMPRA ESI Kunde + Adresse',
  151.                             'en-GB' => 'COMPRA ESI customer + address'
  152.                         ]
  153.                     ],
  154.                     'relations' => [
  155.                         [
  156.                             'entityName' => 'customer'
  157.                         ],
  158.                         [
  159.                             'entityName' => 'customer_address'
  160.                         ],
  161.                         [
  162.                             'entityName' => 'order_address'
  163.                         ]
  164.                     ],
  165.                     'customFields' => [
  166.                         [
  167.                             'name' => 'compra_eevo_kndnr',
  168.                             'type' => CustomFieldTypes::INT,
  169.                             'config' => [
  170.                                 'type' => 'number',
  171.                                 'label' => [
  172.                                     'de-DE' => 'Kndnr',
  173.                                     'en-GB' => 'Kndnr'
  174.                                 ],
  175.                                 'componentName' => 'sw-field',
  176.                                 'customFieldType' => 'number',
  177.                                 'customFieldPosition' => 0
  178.                             ]
  179.                         ]
  180.                     ]
  181.                 ]
  182.             ], $context);
  183.             // custom field set for customer_address and order_address
  184.             $customFieldService->createOrUpdateCustomFieldSets([
  185.                 [
  186.                     'name' => 'compra_esi_address',
  187.                     'config' => [
  188.                         'label' => [
  189.                             'de-DE' => 'COMPRA ESI Adresse',
  190.                             'en-GB' => 'COMPRA ESI address'
  191.                         ]
  192.                     ],
  193.                     'relations' => [
  194.                         [
  195.                             'entityName' => 'customer_address'
  196.                         ],
  197.                         [
  198.                             'entityName' => 'order_address'
  199.                         ]
  200.                     ],
  201.                     'customFields' => [
  202.                         [
  203.                             'name' => 'compra_is_eevo_address',
  204.                             'type' => CustomFieldTypes::BOOL,
  205.                             'config' => [
  206.                                 'type' => 'checkbox',
  207.                                 'label' => [
  208.                                     'de-DE' => 'Ist eEvolution Adresse',
  209.                                     'en-GB' => 'Is eEvolution address'
  210.                                 ],
  211.                                 'componentName' => 'sw-field',
  212.                                 'customFieldType' => 'checkbox',
  213.                                 'customFieldPosition' => 0
  214.                             ]
  215.                         ],
  216.                         [
  217.                             'name' => 'compra_eevo_address_type',
  218.                             'type' => CustomFieldTypes::TEXT,
  219.                             'config' => [
  220.                                 'type' => 'text',
  221.                                 'label' => [
  222.                                     'de-DE' => 'Adresstyp',
  223.                                     'en-GB' => 'Address type'
  224.                                 ],
  225.                                 'componentName' => 'sw-field',
  226.                                 'customFieldPosition' => 1
  227.                             ]
  228.                         ],
  229.                         [
  230.                             'name' => 'compra_eevo_address_lfdnr',
  231.                             'type' => CustomFieldTypes::INT,
  232.                             'config' => [
  233.                                 'type' => 'number',
  234.                                 'label' => [
  235.                                     'de-DE' => 'Adress lfdnr.',
  236.                                     'en-GB' => 'Address lfdnr.'
  237.                                 ],
  238.                                 'componentName' => 'sw-field',
  239.                                 'customFieldType' => 'number',
  240.                                 'customFieldPosition' => 2
  241.                             ]
  242.                         ],
  243.                         [
  244.                             'name' => 'compra_eevo_abw_kdnr1',
  245.                             'type' => CustomFieldTypes::TEXT,
  246.                             'config' => [
  247.                                 'type' => 'text',
  248.                                 'label' => [
  249.                                     'de-DE' => 'Abweichende kdnr1',
  250.                                     'en-GB' => 'Divergent kdnr1'
  251.                                 ],
  252.                                 'componentName' => 'sw-field',
  253.                                 'customFieldPosition' => 3
  254.                             ]
  255.                         ]
  256.                     ]
  257.                 ]
  258.             ], $context);
  259.             // custom field set order
  260.             $customFieldService->createOrUpdateCustomFieldSets([
  261.                 [
  262.                     'name' => 'compra_esi_order_fields',
  263.                     'config' => [
  264.                         'label' => [
  265.                             'de-DE' => 'COMPRA ESI Bestellung',
  266.                             'en-GB' => 'COMPRA ESI Order'
  267.                         ]
  268.                     ],
  269.                     'relations' => [
  270.                         [
  271.                             'entityName' => 'order'
  272.                         ],
  273.                     ],
  274.                     'customFields' => [
  275.                         [
  276.                             'name' => 'compra_eevo_auftragsnummer',
  277.                             'type' => CustomFieldTypes::TEXT,
  278.                             'config' => [
  279.                                 'type' => 'text',
  280.                                 'label' => [
  281.                                     'de-DE' => 'eEvolution Auftragsnummer',
  282.                                     'en-GB' => 'eEvolution Order Number'
  283.                                 ],
  284.                                 'componentName' => 'sw-field',
  285.                                 'customFieldPosition' => 0
  286.                             ]
  287.                         ],
  288.                         [
  289.                             'name' => 'compra_eevo_import_timestamp',
  290.                             'type' => CustomFieldTypes::TEXT,
  291.                             'config' => [
  292.                                 'type' => 'text',
  293.                                 'label' => [
  294.                                     'de-DE' => 'Einlesezeit',
  295.                                     'en-GB' => 'Insert Time'
  296.                                 ],
  297.                                 'componentName' => 'sw-field',
  298.                                 'customFieldPosition' => 1
  299.                             ]
  300.                         ],
  301.                         [
  302.                             'name' => 'compra_eevo_rechnungsnummer',
  303.                             'type' => CustomFieldTypes::TEXT,
  304.                             'config' => [
  305.                                 'type' => 'text',
  306.                                 'label' => [
  307.                                     'de-DE' => 'eEvolution Rechnungsnummer(n)',
  308.                                     'en-GB' => 'eEvolution Invoice Number(s)'
  309.                                 ],
  310.                                 'componentName' => 'sw-field',
  311.                                 'customFieldPosition' => 2
  312.                             ]
  313.                         ],
  314.                         [
  315.                             'name' => 'compra_eevo_rechnungsdruck',
  316.                             'type' => CustomFieldTypes::TEXT,
  317.                             'config' => [
  318.                                 'type' => 'text',
  319.                                 'label' => [
  320.                                     'de-DE' => 'Rechnungsdatum',
  321.                                     'en-GB' => 'Invoice Date'
  322.                                 ],
  323.                                 'componentName' => 'sw-field',
  324.                                 'customFieldPosition' => 3
  325.                             ]
  326.                         ]
  327.                     ]
  328.                 ]
  329.             ], $context);
  330.             // custom field to export orders based on payment method status
  331.             $customFieldService->createOrUpdateCustomFieldSets([
  332.                 [
  333.                     'name' => 'compra_esi_payment_method',
  334.                     'config' => [
  335.                         'label' => [
  336.                             'de-DE' => 'COMPRA ESI Zahlungsart',
  337.                             'en-GB' => 'COMPRA ESI payment method'
  338.                         ]
  339.                     ],
  340.                     'relations' => [
  341.                         [
  342.                             'entityName' => 'payment_method'
  343.                         ],
  344.                     ],
  345.                     'customFields' => [
  346.                         [
  347.                             'name' => 'compra_ignore_export_payment_status',
  348.                             'type' => CustomFieldTypes::BOOL,
  349.                             'config' => [
  350.                                 'type' => 'checkbox',
  351.                                 'label' => [
  352.                                     'de-DE' => 'Bestellung unabhängig von Zahlungsstatus exportieren',
  353.                                     'en-GB' => 'Ignore payment status for order export'
  354.                                 ],
  355.                                 'componentName' => 'sw-field',
  356.                                 'customFieldType' => 'checkbox',
  357.                                 'customFieldPosition' => 0
  358.                             ]
  359.                         ]
  360.                     ]
  361.                 ]
  362.             ], $context);
  363.         } catch (\Exception $e) {}
  364.     }
  365.     /**
  366.      * @param MailService $mailService
  367.      * @param Context $context
  368.      */
  369.     private function createMailTemplates(MailService $mailServiceContext $context): void
  370.     {
  371.         try {
  372.             // import error report
  373.             $mailService->createMailTemplate(
  374.                 'compra.esiImportError',
  375.                 [
  376.                     'de-DE' => [
  377.                         'name' => "COMPRA ESI Import Error Report"
  378.                     ],
  379.                     'en-GB' => [
  380.                         'name' => "COMPRA ESI Import Error Report"
  381.                     ]
  382.                 ],
  383.                 [
  384.                     'de-DE' => [
  385.                         'senderName' => "{{ salesChannel.name }}",
  386.                         'subject' => "Fehler beim ESI Import",
  387.                         'description' => "COMPRA ESI Import Error Report"
  388.                     ],
  389.                     'en-GB' => [
  390.                         'senderName' => "{{ salesChannel.name }}",
  391.                         'subject' => "Error during ESI Import",
  392.                         'description' => "COMPRA ESI Import Error Report"
  393.                     ]
  394.                 ],
  395.                 [
  396.                     'salesChannel' => 'sales_channel'
  397.                 ],
  398.                 $this->getPath() . '/Resources/mails/compraEsiImportError',
  399.                 'compraEsiImportError',
  400.                 $context
  401.             );
  402.         } catch (\Exception $e) {}
  403.     }
  404.     /**
  405.      * @param MailService $mailService
  406.      * @param Context $context
  407.      */
  408.     private function removeMailTemplates(MailService $mailServiceContext $context): void
  409.     {
  410.         $mailService->removeMailTemplate('compra.esiImportError'$context);
  411.     }
  412.     /**
  413.      * @param InstallContext|UpdateContext $context
  414.      */
  415.     private function handleUpdates($context): void
  416.     {
  417.         if (!$context instanceof InstallContext) {
  418.             // only execute for Install or Update action
  419.             return;
  420.         }
  421.         $currentVersion $context->getCurrentPluginVersion();
  422.         $updateVersion null;
  423.         if ($context instanceof UpdateContext) {
  424.             $updateVersion $context->getUpdatePluginVersion();
  425.         }
  426.         // handle updates
  427.         // 1.2.3
  428.         if (version_compare($currentVersion'1.2.3''<')
  429.             && (!$context instanceof UpdateContext || version_compare($updateVersion'1.2.3''>='))) {
  430.             $this->update_1_2_3($context);
  431.         }
  432.     }
  433.     /**
  434.      * Function to handle update 1.2.3:
  435.      * Update existing ESI user and set admin = true because it's necessary for setting the indexing-behavior.
  436.      *
  437.      * @param InstallContext $installContext
  438.      */
  439.     private function update_1_2_3(InstallContext $installContext): void
  440.     {
  441.         $importService $this->container->get(ImportService::class);
  442.         if (!$importService) {
  443.             return;
  444.         }
  445.         // try to get the ESI user ID
  446.         $esiUserId $importService->getEsiUser($installContext->getContext());
  447.         if (!$esiUserId) {
  448.             return;
  449.         }
  450.         try {
  451.             // If ESI user already exists, update and set admin = true
  452.             $importService->updateEsiUser([
  453.                 [
  454.                     'id' => $esiUserId,
  455.                     'admin' => true
  456.                 ]
  457.             ], new Context(new SystemSource()));
  458.         } catch (\Exception $e) {}
  459.     }
  460. }