custom/plugins/AcrisTaxCS/src/AcrisTaxCS.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\Tax;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Content\Rule\Aggregate\RuleCondition\RuleConditionEntity;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  13. class AcrisTaxCS extends Plugin
  14. {
  15.     public function update(UpdateContext $updateContext): void
  16.     {
  17.         if(version_compare($updateContext->getCurrentPluginVersion(), '2.3.0''<')
  18.             && version_compare($updateContext->getUpdatePluginVersion(), '2.3.0''>=')) {
  19.             $this->replaceExistingRuleNames($updateContext->getContext());
  20.         }
  21.     }
  22.     public function uninstall(UninstallContext $context): void
  23.     {
  24.         if ($context->keepUserData()) {
  25.             return;
  26.         }
  27.         $this->cleanupDatabase();
  28.     }
  29.     private function cleanupDatabase(): void
  30.     {
  31.         $connection $this->container->get(Connection::class);
  32.         $connection->executeUpdate('DROP TABLE IF EXISTS acris_tax_customer_group_rule');
  33.         $connection->executeUpdate('DROP TABLE IF EXISTS acris_tax_country_rule');
  34.         try {
  35.             $connection->executeUpdate('ALTER TABLE `customer_group` DROP `acrisRules`');
  36.             $connection->executeUpdate('ALTER TABLE `country` DROP `acrisRules`');
  37.         } catch (\Exception $e) { }
  38.     }
  39.     private function replaceExistingRuleNames(Context $context)
  40.     {
  41.         $ruleConditionRepository $this->container->get('rule_condition.repository');
  42.         /** @var EntitySearchResult $existingRuleConditionSearchResult */
  43.         $existingRuleConditionSearchResult $ruleConditionRepository->search((new Criteria())->addFilter(
  44.             new MultiFilter(MultiFilter::CONNECTION_OR, [
  45.                 new EqualsFilter('type''customerHasVatIdBillingRule'),
  46.                 new EqualsFilter('type''customerHasVatIdShippingRule')
  47.             ])
  48.         ), $context);
  49.         if ($existingRuleConditionSearchResult->count() <= 0) {
  50.             return;
  51.         }
  52.         $changedRuleConditions = [];
  53.         /** @var RuleConditionEntity $ruleConditionEntity */
  54.         foreach ($existingRuleConditionSearchResult->getElements() as $ruleConditionEntity) {
  55.             $value $ruleConditionEntity->getValue();
  56.             if(!is_array($value)) {
  57.                 continue;
  58.             }
  59.             if(array_key_exists('customerHasVatIdBilling'$value)) {
  60.                 $value = ['customerHasVatId' => $value['customerHasVatIdBilling']];
  61.             } elseif(array_key_exists('customerHasVatIdShipping'$value)) {
  62.                 $value = ['customerHasVatId' => $value['customerHasVatIdShipping']];
  63.             } else {
  64.                 continue;
  65.             }
  66.             $changedRuleConditions[] = [
  67.                 'id' => $ruleConditionEntity->getId(),
  68.                 'type' => 'customerHasVatIdRule',
  69.                 'value' => $value
  70.             ];
  71.         }
  72.         if(!empty($changedRuleConditions)) {
  73.             $ruleConditionRepository->upsert($changedRuleConditions$context);
  74.         }
  75.     }
  76. }