<?php declare(strict_types=1);
namespace Acris\CountryTax;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\System\Snippet\SnippetEntity;
class AcrisCountryTaxCS extends Plugin
{
const CUSTOM_FIELD_SET_NAME = 'acris_country_tax';
public function install(InstallContext $context): void
{
$this->addCustomFields($context->getContext());
}
private function addCustomFields(Context $context): void
{
$this->checkForExistingCustomFieldSnippets($context);
$customFieldSet = $this->container->get('custom_field_set.repository');
if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name', self::CUSTOM_FIELD_SET_NAME)), $context)->count() > 0) return;
$customFieldSet->create([[
'name' => self::CUSTOM_FIELD_SET_NAME,
'config' => [
'label' => [
'de-DE' => 'Land Steuerberechnung',
'en-GB' => 'Country Tax Calculation'
]
],
'relations' => [
[
'entityName' => 'country'
]
],
'customFields' => [
['name' => 'acris_country_tax_net_calculation', 'type' => CustomFieldTypes::BOOL,
'config' => [
'componentName' => 'sw-field',
'type' => 'checkbox',
'customFieldType' => 'checkbox',
'customFieldPosition' => 1,
'label' => [
'de-DE' => 'Bruttopreis Berechnung ausgehend vom Netto Preis',
'en-GB' => 'Gross price calculation based on net price'
],
'helpText' => [
'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.',
'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 €.'
]
]]
],
]], $context);
}
public function uninstall(UninstallContext $context): void
{
if ($context->keepUserData()) {
return;
}
$this->removeCustomFields($context->getContext());
}
private function removeCustomFields(Context $context): void
{
/* Check for snippets if they exist for custom fields */
$this->checkForExistingCustomFieldSnippets($context);
$customFieldSet = $this->container->get('custom_field_set.repository');
$id = $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name', self::CUSTOM_FIELD_SET_NAME)), $context)->firstId();
if($id) $customFieldSet->delete([['id' => $id]], $context);
}
private function checkForExistingCustomFieldSnippets(Context $context)
{
/** @var EntityRepositoryInterface $snippetRepository */
$snippetRepository = $this->container->get('snippet.repository');
$criteria = new Criteria();
$criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
new EqualsFilter('translationKey', 'customFields.' . 'acris_country_tax_net_calculation')
]));
/** @var EntitySearchResult $searchResult */
$searchResult = $snippetRepository->search($criteria, $context);
if ($searchResult->count() > 0) {
$snippetIds = [];
/** @var SnippetEntity $snippet */
foreach ($searchResult->getEntities()->getElements() as $snippet) {
$snippetIds[] = [
'id' => $snippet->getId()
];
}
if (!empty($snippetIds)) {
$snippetRepository->delete($snippetIds, $context);
}
}
}
}