custom/plugins/fgitsAutoinvoiceSW6/src/fgitsAutoinvoiceSW6.php line 25

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Fgits\AutoInvoice;
  3. use Doctrine\DBAL\Connection;
  4. use Fgits\AutoInvoice\Service\FgitsLibrary\PluginSetup;
  5. use Psr\Log\LoggerInterface;
  6. use ReflectionClass;
  7. use Shopware\Core\Framework\Api\Context\SystemSource;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  12. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  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 Shopware\Core\System\SystemConfig\SystemConfigService;
  17. /**
  18.  * Copyright (c) 2020. GOLLE IT.
  19.  *
  20.  * @author Andrey Grigorkin <andrey@golle-it.de>
  21.  */
  22. class fgitsAutoinvoiceSW6 extends Plugin
  23. {
  24.     public const CUSTOM_FIELD_SET_NAME  'fgits_autoinvoice';
  25.     public const MAIL_TEMPLATE_CUSTOMER 'fgits_autoinvoice_customer';
  26.     public const MAIL_TEMPLATE_ADMIN    'fgits_autoinvoice_admin';
  27.     private PluginSetup $setup;
  28.     private LoggerInterface $logger;
  29.     /**
  30.      * @param UpdateContext $updateContext
  31.      */
  32.     public function update(UpdateContext $updateContext): void
  33.     {
  34.         // Initialise exportDirectory for all channels
  35.         if (str_replace('.'''$updateContext->getCurrentPluginVersion()) < 541) {
  36.             $salesChannelRepository $this->container->get('sales_channel.repository');
  37.             foreach ($salesChannelRepository->search(new Criteria(), new Context(new SystemSource()))->getEntities() as $salesChannelId => $salesChannel) {
  38.                 $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService')->set('fgitsAutoinvoiceSW6.config.exportDirectory''/files/export'$salesChannelId);
  39.             }
  40.             $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService')->set('fgitsAutoinvoiceSW6.config.exportDirectory''/files/export');
  41.         }
  42.         // Initialise attachEmail for all channels
  43.         if (str_replace('.'''$updateContext->getCurrentPluginVersion()) < 556) {
  44.             $salesChannelRepository $this->container->get('sales_channel.repository');
  45.             foreach ($salesChannelRepository->search(new Criteria(), new Context(new SystemSource()))->getEntities() as $salesChannelId => $salesChannel) {
  46.                 if ($this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService')->get('fgitsAutoinvoiceSW6.config.attachOrderEmail'$salesChannelId)) {
  47.                     $attachEmail 'order_confirmation_mail';
  48.                 } else {
  49.                     $attachEmail '';
  50.                 }
  51.                 $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService')->set('fgitsAutoinvoiceSW6.config.attachEmail'$attachEmail$salesChannelId);
  52.             }
  53.             if ($this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService')->get('fgitsAutoinvoiceSW6.config.attachOrderEmail')) {
  54.                 $attachEmail 'order_confirmation_mail';
  55.             } else {
  56.                 $attachEmail '';
  57.             }
  58.             $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService')->set('fgitsAutoinvoiceSW6.config.attachEmail'$attachEmail);
  59.         }
  60.         // Initialise active
  61.         if (str_replace('.'''$updateContext->getCurrentPluginVersion()) < 561) {
  62.             $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService')->set('fgitsAutoinvoiceSW6.config.active'true);
  63.         }
  64.     }
  65.     /**
  66.      * @param ActivateContext $context
  67.      */
  68.     public function activate(ActivateContext $context): void
  69.     {
  70.         $this->setup->initSystemConfig(
  71.             'fgitsAutoinvoiceSW6.config',
  72.             [
  73.                 'attachEmail'         => '',
  74.                 'exportDirectory'     => '/files/export',
  75.                 'processOrdersAfter'  => (new \DateTime())->format('c')
  76.             ]
  77.         );
  78.         $this->setup->initSystemConfig(
  79.             'fgitsAutoinvoiceSW6.config',
  80.             [
  81.                 'cronjobActive' => false
  82.             ],
  83.             true,
  84.             true
  85.         );
  86.         try {
  87.             $customFields = [
  88.                 // OrderEntity
  89.                 ['name' => self::CUSTOM_FIELD_SET_NAME '_cron_date''type' => CustomFieldTypes::DATETIME],
  90.                 ['name' => self::CUSTOM_FIELD_SET_NAME '_exported''type' => CustomFieldTypes::BOOL],
  91.                 ['name' => self::CUSTOM_FIELD_SET_NAME '_checkout_email''type' => CustomFieldTypes::TEXT],
  92.                 ['name' => self::CUSTOM_FIELD_SET_NAME '_customer_no_invoice''type' => CustomFieldTypes::BOOL],
  93.                 ['name' => self::CUSTOM_FIELD_SET_NAME '_processed''type' => CustomFieldTypes::BOOL],
  94.                 ['name' => self::CUSTOM_FIELD_SET_NAME '_processed_date''type' => CustomFieldTypes::DATETIME],
  95.                 ['name' => self::CUSTOM_FIELD_SET_NAME '_order_confirmation_sent''type' => CustomFieldTypes::BOOL]
  96.             ];
  97.             $this->setup->createCustomFieldSet(self::CUSTOM_FIELD_SET_NAME$customFields);
  98.         } catch (\Exception $e) {
  99.             $this->logger->warning('[#fgits] fgitsAutoinvoiceSW6: ' __CLASS__ '::' __FUNCTION__ '(): ' $e->getMessage());
  100.         }
  101.         /** @var Connection $connection */
  102.         $connection $this->container->get('Doctrine\DBAL\Connection');
  103.         $codeLocales $this->setup->getLocales();
  104.         try {
  105.             $templateTypeData['technicalName'] = self::MAIL_TEMPLATE_CUSTOMER;
  106.             $templateTypeData['availableEntities'] = [
  107.                 'customer'        => 'customer',
  108.                 'customerAddress' => 'customer_address',
  109.                 'customerGroup'   => 'customer_group',
  110.                 'order'           => 'order',
  111.                 'orderAddress'    => 'order_address',
  112.                 'orderCustomer'   => 'order_customer',
  113.                 'orderDelivery'   => 'order_delivery',
  114.                 'product'         => 'product',
  115.                 'salesChannel'    => 'sales_channel'
  116.             ];
  117.             foreach ($codeLocales as $code => $locales) {
  118.                 foreach ($locales as $locale) {
  119.                     $templateTypeData[$locale]['name'] = ($code == 'de-DE') ? 'AutoInvoice Kunden E-Mail' 'AutoInvoice Customer E-Mail';
  120.                     $templateData[$locale]['senderName']   = '{{ salesChannel.translated.name }}';
  121.                     $templateData[$locale]['subject']      = ($code == 'de-DE') ? 'Ihre Rechnung zur Bestellung {{ order.orderNumber }}' 'Your invoice for Order {{ order.orderNumber }}';
  122.                     $templateData[$locale]['description']  = '';
  123.                     $templateData[$locale]['contentHtml']  = $this->setup->getMailContent($this->getPath(), $code'customer''html');
  124.                     $templateData[$locale]['contentPlain'] = $this->setup->getMailContent($this->getPath(), $code'customer''plain');
  125.                 }
  126.             }
  127.             $mailTemplateTypeId $this->setup->createMailTemplateType($connection$templateTypeData);
  128.             $this->setup->createMailTemplate($connection$mailTemplateTypeId$templateData);
  129.         } catch (\Exception $e) {
  130.             $this->logger->warning('[#fgits] fgitsAutoinvoiceSW6: ' __CLASS__ '::' __FUNCTION__ '(): ' $e->getMessage());
  131.         }
  132.         try {
  133.             $templateTypeData['technicalName'] = self::MAIL_TEMPLATE_ADMIN;
  134.             $templateTypeData['availableEntities'] = [
  135.                 'customer'        => 'customer',
  136.                 'customerAddress' => 'customer_address',
  137.                 'customerGroup'   => 'customer_group',
  138.                 'order'           => 'order',
  139.                 'orderAddress'    => 'order_address',
  140.                 'orderCustomer'   => 'order_customer',
  141.                 'orderDelivery'   => 'order_delivery',
  142.                 'product'         => 'product',
  143.                 'salesChannel'    => 'sales_channel'
  144.             ];
  145.             foreach ($codeLocales as $code => $locales) {
  146.                 foreach ($locales as $locale) {
  147.                     $templateTypeData[$locale]['name'] = 'AutoInvoice Admin E-Mail';
  148.                     $templateData[$locale]['senderName']   = '{{ salesChannel.translated.name }}';
  149.                     $templateData[$locale]['subject']      = ($code == 'de-DE') ? 'Rechnung / Lieferschein zur Bestellung {{ order.orderNumber }}' 'Invoice / Delivery note for Order {{ order.orderNumber }}';
  150.                     $templateData[$locale]['description']  = '';
  151.                     $templateData[$locale]['contentHtml']  = $this->setup->getMailContent($this->getPath(), $code'admin''html');
  152.                     $templateData[$locale]['contentPlain'] = $this->setup->getMailContent($this->getPath(), $code'admin''plain');
  153.                 }
  154.             }
  155.             $mailTemplateTypeId $this->setup->createMailTemplateType($connection$templateTypeData);
  156.             $this->setup->createMailTemplate($connection$mailTemplateTypeId$templateData);
  157.         } catch (\Exception $e) {
  158.             $this->logger->warning('[#fgits] fgitsAutoinvoiceSW6: ' __CLASS__ '::' __FUNCTION__ '(): ' $e->getMessage());
  159.         }
  160.     }
  161.     /**
  162.      * @param DeactivateContext $context
  163.      */
  164.     public function deactivate(DeactivateContext $context): void
  165.     {
  166.         $config $this->container->get(SystemConfigService::class)->get('fgitsAutoinvoiceSW6.config');
  167.         $this->setup->deleteCustomFieldSet($context->getContext(), self::CUSTOM_FIELD_SET_NAME);
  168.         if ($config['setupDeactivateDeleteMailTemplates']) {
  169.             $this->setup->deleteMailTemplate($context->getContext(), self::MAIL_TEMPLATE_CUSTOMER);
  170.             $this->setup->deleteMailTemplateType($context->getContext(), self::MAIL_TEMPLATE_CUSTOMER);
  171.             $this->setup->deleteMailTemplate($context->getContext(), self::MAIL_TEMPLATE_ADMIN);
  172.             $this->setup->deleteMailTemplateType($context->getContext(), self::MAIL_TEMPLATE_ADMIN);
  173.         }
  174.     }
  175.     /**
  176.      * @param UninstallContext $context
  177.      */
  178.     public function uninstall(UninstallContext $context): void
  179.     {
  180.         parent::uninstall($context);
  181.         if ($context->keepUserData()) {
  182.             return;
  183.         }
  184.         $connection $this->container->get(Connection::class);
  185.         $connection->executeStatement('DELETE FROM system_config WHERE configuration_key LIKE :class', ['class' => (new ReflectionClass($this))->getShortName() . '%']);
  186.     }
  187.     /**
  188.      * @param PluginSetup $setup
  189.      *
  190.      * @required
  191.      */
  192.     public function setPluginSetup(PluginSetup $setup)
  193.     {
  194.         $this->setup $setup;
  195.     }
  196.     /**
  197.      * @param LoggerInterface $logger
  198.      *
  199.      * @required
  200.      */
  201.     public function setLogger(LoggerInterface $logger)
  202.     {
  203.         $this->logger $logger;
  204.     }
  205. }