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