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.         try {
  104.             $templateTypeData['technicalName'] = self::MAIL_TEMPLATE_CUSTOMER;
  105.             $templateTypeData['availableEntities'] = [
  106.                 'customer'        => 'customer',
  107.                 'customerAddress' => 'customer_address',
  108.                 'customerGroup'   => 'customer_group',
  109.                 'order'           => 'order',
  110.                 'orderAddress'    => 'order_address',
  111.                 'orderCustomer'   => 'order_customer',
  112.                 'orderDelivery'   => 'order_delivery',
  113.                 'product'         => 'product',
  114.                 'salesChannel'    => 'sales_channel'
  115.             ];
  116.             $templateTypeData['en-GB']['name'] = 'AutoInvoice Customer E-Mail';
  117.             $templateTypeData['de-DE']['name'] = 'AutoInvoice Kunden E-Mail';
  118.             $mailTemplateTypeId $this->setup->createMailTemplateType($connection$templateTypeData);
  119.             $templateData['en-GB']['senderName']   = '{{ salesChannel.translated.name }}';
  120.             $templateData['en-GB']['subject']      = 'Your invoice for Order {{ order.orderNumber }}';
  121.             $templateData['en-GB']['description']  = '';
  122.             $templateData['en-GB']['contentHtml']  = $this->setup->getMailContent($this->getPath(), 'en-GB''customer''html');
  123.             $templateData['en-GB']['contentPlain'] = $this->setup->getMailContent($this->getPath(), 'en-GB''customer''plain');
  124.             $templateData['de-DE']['senderName']   = '{{ salesChannel.translated.name }}';
  125.             $templateData['de-DE']['subject']      = 'Ihre Rechnung zur Bestellung {{ order.orderNumber }}';
  126.             $templateData['de-DE']['description']  = '';
  127.             $templateData['de-DE']['contentHtml']  = $this->setup->getMailContent($this->getPath(), 'de-DE''customer''html');
  128.             $templateData['de-DE']['contentPlain'] = $this->setup->getMailContent($this->getPath(), 'de-DE''customer''plain');
  129.             $this->setup->createMailTemplate($connection$mailTemplateTypeId$templateData);
  130.         } catch (\Exception $e) {
  131.             $this->logger->warning('[#fgits] fgitsAutoinvoiceSW6: ' __CLASS__ '::' __FUNCTION__ '(): ' $e->getMessage());
  132.         }
  133.         try {
  134.             $templateTypeData['technicalName'] = self::MAIL_TEMPLATE_ADMIN;
  135.             $templateTypeData['availableEntities'] = [
  136.                 'customer'        => 'customer',
  137.                 'customerAddress' => 'customer_address',
  138.                 'customerGroup'   => 'customer_group',
  139.                 'order'           => 'order',
  140.                 'orderAddress'    => 'order_address',
  141.                 'orderCustomer'   => 'order_customer',
  142.                 'orderDelivery'   => 'order_delivery',
  143.                 'product'         => 'product',
  144.                 'salesChannel'    => 'sales_channel'
  145.             ];
  146.             $templateTypeData['en-GB']['name'] = 'AutoInvoice Admin E-Mail';
  147.             $templateTypeData['de-DE']['name'] = 'AutoInvoice Admin E-Mail';
  148.             $mailTemplateTypeId $this->setup->createMailTemplateType($connection$templateTypeData);
  149.             $templateData['en-GB']['senderName']   = '{{ salesChannel.translated.name }}';
  150.             $templateData['en-GB']['subject']      = 'Invoice / Delivery note for Order {{ order.orderNumber }}';
  151.             $templateData['en-GB']['description']  = '';
  152.             $templateData['en-GB']['contentHtml']  = $this->setup->getMailContent($this->getPath(), 'en-GB''admin''html');
  153.             $templateData['en-GB']['contentPlain'] = $this->setup->getMailContent($this->getPath(), 'en-GB''admin''plain');
  154.             $templateData['de-DE']['senderName']   = '{{ salesChannel.translated.name }}';
  155.             $templateData['de-DE']['subject']      = 'Rechnung / Lieferschein zur Bestellung {{ order.orderNumber }}';
  156.             $templateData['de-DE']['description']  = '';
  157.             $templateData['de-DE']['contentHtml']  = $this->setup->getMailContent($this->getPath(), 'de-DE''admin''html');
  158.             $templateData['de-DE']['contentPlain'] = $this->setup->getMailContent($this->getPath(), 'de-DE''admin''plain');
  159.             $this->setup->createMailTemplate($connection$mailTemplateTypeId$templateData);
  160.         } catch (\Exception $e) {
  161.             $this->logger->warning('[#fgits] fgitsAutoinvoiceSW6: ' __CLASS__ '::' __FUNCTION__ '(): ' $e->getMessage());
  162.         }
  163.     }
  164.     /**
  165.      * @param DeactivateContext $context
  166.      */
  167.     public function deactivate(DeactivateContext $context): void
  168.     {
  169.         $config $this->container->get(SystemConfigService::class)->get('fgitsAutoinvoiceSW6.config');
  170.         $this->setup->deleteCustomFieldSet($context->getContext(), self::CUSTOM_FIELD_SET_NAME);
  171.         if ($config['setupDeactivateDeleteMailTemplates']) {
  172.             $this->setup->deleteMailTemplate($context->getContext(), self::MAIL_TEMPLATE_CUSTOMER);
  173.             $this->setup->deleteMailTemplateType($context->getContext(), self::MAIL_TEMPLATE_CUSTOMER);
  174.             $this->setup->deleteMailTemplate($context->getContext(), self::MAIL_TEMPLATE_ADMIN);
  175.             $this->setup->deleteMailTemplateType($context->getContext(), self::MAIL_TEMPLATE_ADMIN);
  176.         }
  177.     }
  178.     /**
  179.      * @param UninstallContext $context
  180.      */
  181.     public function uninstall(UninstallContext $context): void
  182.     {
  183.         parent::uninstall($context);
  184.         if ($context->keepUserData()) {
  185.             return;
  186.         }
  187.         $connection $this->container->get(Connection::class);
  188.         $connection->executeStatement('DELETE FROM system_config WHERE configuration_key LIKE :class', ['class' => (new ReflectionClass($this))->getShortName() . '%']);
  189.     }
  190.     /**
  191.      * @param PluginSetup $setup
  192.      *
  193.      * @required
  194.      */
  195.     public function setPluginSetup(PluginSetup $setup)
  196.     {
  197.         $this->setup $setup;
  198.     }
  199.     /**
  200.      * @param LoggerInterface $logger
  201.      *
  202.      * @required
  203.      */
  204.     public function setLogger(LoggerInterface $logger)
  205.     {
  206.         $this->logger $logger;
  207.     }
  208. }