<?php declare(strict_types=1);
namespace Compra\EsiSW6;
use Compra\EsiSW6\Administration\Service\ConfigurationService;
use Compra\EsiSW6\Import\Service\ImportService;
use Compra\FoundationSW6\Core\Content\Mail\MailService;
use Compra\FoundationSW6\Core\System\CustomFieldService;
use Shopware\Core\Framework\Api\Context\SystemSource;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class CompraEsiSW6 extends Plugin
{
public function build(ContainerBuilder $container): void
{
$container->setParameter('compra_esi_s_w6.plugin_dir', $this->getPath());
parent::build($container);
}
/**
* @param InstallContext $installContext
*/
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->addCustomFields($installContext->getContext());
// handle update functions
$this->handleUpdates($installContext);
$mailService = $this->container->get(MailService::class);
if (!$mailService) {
return;
}
$this->removeMailTemplates($mailService, $installContext->getContext());
$this->createMailTemplates($mailService, $installContext->getContext());
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
$this->addCustomFields($updateContext->getContext());
$configurationService = $this->container->get(ConfigurationService::class);
if ($configurationService) {
$configurationService->reloadEsiConfig();
}
// handle update functions
$this->handleUpdates($updateContext);
$mailService = $this->container->get(MailService::class);
if (!$mailService) {
return;
}
$this->removeMailTemplates($mailService, $updateContext->getContext());
$this->createMailTemplates($mailService, $updateContext->getContext());
}
/**
* @param UninstallContext $uninstallContext
* @throws InconsistentCriteriaIdsException
*/
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
$mailService = $this->container->get(MailService::class);
if (!$mailService || $uninstallContext->keepUserData()) {
return;
}
$this->removeMailTemplates($mailService, $uninstallContext->getContext());
}
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext);
$configurationService = $this->container->get(ConfigurationService::class);
if ($configurationService) {
$configurationService->reloadEsiConfig();
}
}
/**
* Function to create the custom fields for the plugin.
*
* @param $context
*/
public function addCustomFields($context): void
{
$customFieldService = $this->container->get(CustomFieldService::class);
if (!$customFieldService) {
return;
}
try {
$customFieldService->createOrUpdateCustomFields([
[
'name' => 'compra_is_esi_source',
'type' => CustomFieldTypes::BOOL
],
[
'name' => 'compra_is_esi_deleted',
'type' => CustomFieldTypes::BOOL
],
[
'name' => 'compra_media_file_hash',
'type' => CustomFieldTypes::TEXT
],
[
'name' => 'compra_eevo_lfdnr', // tables: country, product, tax
'type' => CustomFieldTypes::INT
],
[
'name' => 'compra_eevo_landkuerzel', // table country
'type' => CustomFieldTypes::TEXT
],
[
'name' => 'compra_esi_xml_created', // order
'type' => CustomFieldTypes::BOOL
],
[
'name' => 'compra_esi_xml_export', // customer, address
'type' => CustomFieldTypes::BOOL
],
[
'name' => 'compra_esi_xml_export_address', // customer
'type' => CustomFieldTypes::BOOL
],
[
'name' => 'compra_esi_request_password_uuid',
'type' => CustomFieldTypes::TEXT
],
[
'name' => 'compra_media_is_missing', // media file not found or corrupted
'type' => CustomFieldTypes::BOOL
],
[
'name' => 'compra_media_is_missing_error', // error message for media file not found or corrupted
'type' => CustomFieldTypes::TEXT
],
[
'name' => 'compra_eevo_ansprechpartner_lfdnr', // e.g. for b2b_debtor_contact
'type' => CustomFieldTypes::INT
],
[
'name' => 'compra_eevo_debtor_customernumber', // e.g. for b2b_debtor_contact
'type' => CustomFieldTypes::TEXT
]
], $context);
// custom field set for customer, customer_address and order_address
$customFieldService->createOrUpdateCustomFieldSets([
[
'name' => 'compra_esi_customer_and_address',
'config' => [
'label' => [
'de-DE' => 'COMPRA ESI Kunde + Adresse',
'en-GB' => 'COMPRA ESI customer + address'
]
],
'relations' => [
[
'entityName' => 'customer'
],
[
'entityName' => 'customer_address'
],
[
'entityName' => 'order_address'
]
],
'customFields' => [
[
'name' => 'compra_eevo_kndnr',
'type' => CustomFieldTypes::INT,
'config' => [
'type' => 'number',
'label' => [
'de-DE' => 'Kndnr',
'en-GB' => 'Kndnr'
],
'componentName' => 'sw-field',
'customFieldType' => 'number',
'customFieldPosition' => 0
]
]
]
]
], $context);
// custom field set for customer_address and order_address
$customFieldService->createOrUpdateCustomFieldSets([
[
'name' => 'compra_esi_address',
'config' => [
'label' => [
'de-DE' => 'COMPRA ESI Adresse',
'en-GB' => 'COMPRA ESI address'
]
],
'relations' => [
[
'entityName' => 'customer_address'
],
[
'entityName' => 'order_address'
]
],
'customFields' => [
[
'name' => 'compra_is_eevo_address',
'type' => CustomFieldTypes::BOOL,
'config' => [
'type' => 'checkbox',
'label' => [
'de-DE' => 'Ist eEvolution Adresse',
'en-GB' => 'Is eEvolution address'
],
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
'customFieldPosition' => 0
]
],
[
'name' => 'compra_eevo_address_type',
'type' => CustomFieldTypes::TEXT,
'config' => [
'type' => 'text',
'label' => [
'de-DE' => 'Adresstyp',
'en-GB' => 'Address type'
],
'componentName' => 'sw-field',
'customFieldPosition' => 1
]
],
[
'name' => 'compra_eevo_address_lfdnr',
'type' => CustomFieldTypes::INT,
'config' => [
'type' => 'number',
'label' => [
'de-DE' => 'Adress lfdnr.',
'en-GB' => 'Address lfdnr.'
],
'componentName' => 'sw-field',
'customFieldType' => 'number',
'customFieldPosition' => 2
]
],
[
'name' => 'compra_eevo_abw_kdnr1',
'type' => CustomFieldTypes::TEXT,
'config' => [
'type' => 'text',
'label' => [
'de-DE' => 'Abweichende kdnr1',
'en-GB' => 'Divergent kdnr1'
],
'componentName' => 'sw-field',
'customFieldPosition' => 3
]
]
]
]
], $context);
// custom field set order
$customFieldService->createOrUpdateCustomFieldSets([
[
'name' => 'compra_esi_order_fields',
'config' => [
'label' => [
'de-DE' => 'COMPRA ESI Bestellung',
'en-GB' => 'COMPRA ESI Order'
]
],
'relations' => [
[
'entityName' => 'order'
],
],
'customFields' => [
[
'name' => 'compra_eevo_auftragsnummer',
'type' => CustomFieldTypes::TEXT,
'config' => [
'type' => 'text',
'label' => [
'de-DE' => 'eEvolution Auftragsnummer',
'en-GB' => 'eEvolution Order Number'
],
'componentName' => 'sw-field',
'customFieldPosition' => 0
]
],
[
'name' => 'compra_eevo_import_timestamp',
'type' => CustomFieldTypes::TEXT,
'config' => [
'type' => 'text',
'label' => [
'de-DE' => 'Einlesezeit',
'en-GB' => 'Insert Time'
],
'componentName' => 'sw-field',
'customFieldPosition' => 1
]
],
[
'name' => 'compra_eevo_rechnungsnummer',
'type' => CustomFieldTypes::TEXT,
'config' => [
'type' => 'text',
'label' => [
'de-DE' => 'eEvolution Rechnungsnummer(n)',
'en-GB' => 'eEvolution Invoice Number(s)'
],
'componentName' => 'sw-field',
'customFieldPosition' => 2
]
],
[
'name' => 'compra_eevo_rechnungsdruck',
'type' => CustomFieldTypes::TEXT,
'config' => [
'type' => 'text',
'label' => [
'de-DE' => 'Rechnungsdatum',
'en-GB' => 'Invoice Date'
],
'componentName' => 'sw-field',
'customFieldPosition' => 3
]
]
]
]
], $context);
// custom field to export orders based on payment method status
$customFieldService->createOrUpdateCustomFieldSets([
[
'name' => 'compra_esi_payment_method',
'config' => [
'label' => [
'de-DE' => 'COMPRA ESI Zahlungsart',
'en-GB' => 'COMPRA ESI payment method'
]
],
'relations' => [
[
'entityName' => 'payment_method'
],
],
'customFields' => [
[
'name' => 'compra_ignore_export_payment_status',
'type' => CustomFieldTypes::BOOL,
'config' => [
'type' => 'checkbox',
'label' => [
'de-DE' => 'Bestellung unabhängig von Zahlungsstatus exportieren',
'en-GB' => 'Ignore payment status for order export'
],
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
'customFieldPosition' => 0
]
]
]
]
], $context);
} catch (\Exception $e) {}
}
/**
* @param MailService $mailService
* @param Context $context
*/
private function createMailTemplates(MailService $mailService, Context $context): void
{
try {
// import error report
$mailService->createMailTemplate(
'compra.esiImportError',
[
'de-DE' => [
'name' => "COMPRA ESI Import Error Report"
],
'en-GB' => [
'name' => "COMPRA ESI Import Error Report"
]
],
[
'de-DE' => [
'senderName' => "{{ salesChannel.name }}",
'subject' => "Fehler beim ESI Import",
'description' => "COMPRA ESI Import Error Report"
],
'en-GB' => [
'senderName' => "{{ salesChannel.name }}",
'subject' => "Error during ESI Import",
'description' => "COMPRA ESI Import Error Report"
]
],
[
'salesChannel' => 'sales_channel'
],
$this->getPath() . '/Resources/mails/compraEsiImportError',
'compraEsiImportError',
$context
);
} catch (\Exception $e) {}
}
/**
* @param MailService $mailService
* @param Context $context
*/
private function removeMailTemplates(MailService $mailService, Context $context): void
{
$mailService->removeMailTemplate('compra.esiImportError', $context);
}
/**
* @param InstallContext|UpdateContext $context
*/
private function handleUpdates($context): void
{
if (!$context instanceof InstallContext) {
// only execute for Install or Update action
return;
}
$currentVersion = $context->getCurrentPluginVersion();
$updateVersion = null;
if ($context instanceof UpdateContext) {
$updateVersion = $context->getUpdatePluginVersion();
}
// handle updates
// 1.2.3
if (version_compare($currentVersion, '1.2.3', '<')
&& (!$context instanceof UpdateContext || version_compare($updateVersion, '1.2.3', '>='))) {
$this->update_1_2_3($context);
}
}
/**
* Function to handle update 1.2.3:
* Update existing ESI user and set admin = true because it's necessary for setting the indexing-behavior.
*
* @param InstallContext $installContext
*/
private function update_1_2_3(InstallContext $installContext): void
{
$importService = $this->container->get(ImportService::class);
if (!$importService) {
return;
}
// try to get the ESI user ID
$esiUserId = $importService->getEsiUser($installContext->getContext());
if (!$esiUserId) {
return;
}
try {
// If ESI user already exists, update and set admin = true
$importService->updateEsiUser([
[
'id' => $esiUserId,
'admin' => true
]
], new Context(new SystemSource()));
} catch (\Exception $e) {}
}
}