custom/plugins/AcrisCountryTaxCS/src/AcrisCountryTaxCS.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CountryTax;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\System\CustomField\CustomFieldTypes;
  13. use Shopware\Core\System\Snippet\SnippetEntity;
  14. class AcrisCountryTaxCS extends Plugin
  15. {
  16.     const CUSTOM_FIELD_SET_NAME 'acris_country_tax';
  17.     public function install(InstallContext $context): void
  18.     {
  19.         $this->addCustomFields($context->getContext());
  20.     }
  21.     private function addCustomFields(Context $context): void
  22.     {
  23.         $this->checkForExistingCustomFieldSnippets($context);
  24.         $customFieldSet $this->container->get('custom_field_set.repository');
  25.         if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SET_NAME)), $context)->count() > 0) return;
  26.         $customFieldSet->create([[
  27.             'name' => self::CUSTOM_FIELD_SET_NAME,
  28.             'config' => [
  29.                 'label' => [
  30.                     'de-DE' => 'Land Steuerberechnung',
  31.                     'en-GB' => 'Country Tax Calculation'
  32.                 ]
  33.             ],
  34.             'relations' => [
  35.                 [
  36.                     'entityName' => 'country'
  37.                 ]
  38.             ],
  39.             'customFields' => [
  40.                 ['name' => 'acris_country_tax_net_calculation''type' => CustomFieldTypes::BOOL,
  41.                     'config' => [
  42.                         'componentName' => 'sw-field',
  43.                         'type' => 'checkbox',
  44.                         'customFieldType' => 'checkbox',
  45.                         'customFieldPosition' => 1,
  46.                         'label' => [
  47.                             'de-DE' => 'Bruttopreis Berechnung ausgehend vom Netto Preis',
  48.                             'en-GB' => 'Gross price calculation based on net price'
  49.                         ],
  50.                         'helpText' => [
  51.                             'de-DE' => 'Beispiel: Ein Artikel kostet 119 € bei einem Steuersatz von 19 %. Wird der Artikel nun in einem Land mit 20 % Steuern verkauft, kostet dieser wenn die Checkbox aktiviert wurde 120 €. Im Standard würde er ebenfalls 119 € kosten.',
  52.                             'en-GB' => 'Example: A product costs 119 € at a tax rate of 19%. If the item is now sold in a country with 20% tax, it will cost 120 € if the checkbox is activated. By default it would also cost 119 €.'
  53.                         ]
  54.                     ]]
  55.             ],
  56.         ]], $context);
  57.     }
  58.     public function uninstall(UninstallContext $context): void
  59.     {
  60.         if ($context->keepUserData()) {
  61.             return;
  62.         }
  63.         $this->removeCustomFields($context->getContext());
  64.     }
  65.     private function removeCustomFields(Context $context): void
  66.     {
  67.         /* Check for snippets if they exist for custom fields */
  68.         $this->checkForExistingCustomFieldSnippets($context);
  69.         $customFieldSet $this->container->get('custom_field_set.repository');
  70.         $id $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SET_NAME)), $context)->firstId();
  71.         if($id$customFieldSet->delete([['id' => $id]], $context);
  72.     }
  73.     private function checkForExistingCustomFieldSnippets(Context $context)
  74.     {
  75.         /** @var EntityRepositoryInterface $snippetRepository */
  76.         $snippetRepository $this->container->get('snippet.repository');
  77.         $criteria = new Criteria();
  78.         $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  79.             new EqualsFilter('translationKey''customFields.' 'acris_country_tax_net_calculation')
  80.         ]));
  81.         /** @var EntitySearchResult $searchResult */
  82.         $searchResult $snippetRepository->search($criteria$context);
  83.         if ($searchResult->count() > 0) {
  84.             $snippetIds = [];
  85.             /** @var SnippetEntity $snippet */
  86.             foreach ($searchResult->getEntities()->getElements() as $snippet) {
  87.                 $snippetIds[] = [
  88.                     'id' => $snippet->getId()
  89.                 ];
  90.             }
  91.             if (!empty($snippetIds)) {
  92.                 $snippetRepository->delete($snippetIds$context);
  93.             }
  94.         }
  95.     }
  96. }