<?php declare(strict_types=1);
namespace Wexo\BusinessCentralOverride;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class WexoBusinessCentralOverride extends Plugin
{
//Customer custom field definitions
private const CUSTOM_SET_CUSTOMER = 'customer_business_central';
public const CUSTOM_FIELD_ALLOW_CREDIT = 'allow_credit';
public const CUSTOM_FIELD_GLN = 'gln';
public const CUSTOM_FIELD_CURRENCY_CODE = 'currency_code';
public const CUSTOM_FIELD_PRICE_GROUP = 'price_group';
public const CUSTOM_FIELD_DISCOUNT_GROUP = 'discount_group';
public const CUSTOM_FIELD_CONTACT_PERSON = 'contact_person';
public const CUSTOM_FIELD_HOMEPAGE = 'homepage';
//Integration keys
public const PRODUCT_SPECIAL_ITEM_KEY = 'Speciel_Item';
//Order export status
private const CUSTOM_SET_ORDER_EXPORT = 'order_export';
public const CUSTOM_FIELD_ORDER_EXPORTED_DATE = 'order_exported_date';
protected ?Context $context = null;
protected ?EntityRepository $customFieldSetRepository = null;
protected ?EntityRepository $customFieldRepository = null;
/**
* @param InstallContext $installContext
* @return void
*/
public function install(InstallContext $installContext): void
{
$this->updateCustomFields();
}
/**
* @param UpdateContext $updateContext
* @return void
*/
public function update(UpdateContext $updateContext): void
{
$this->updateCustomFields();
}
/**
* @return void
*/
public function updateCustomFields(): void
{
//Customer custom fields
$customFieldSet = $this->addCustomFieldSet(
self::CUSTOM_SET_CUSTOMER,
[
'da-DK' => 'Business Central',
'en-GB' => 'Business Central',
'de-DE' => 'Business Central'
],
[
'customer'
]
);
$this->addCustomField(
self::CUSTOM_FIELD_ALLOW_CREDIT,
CustomFieldTypes::BOOL,
[
'da-DK' => 'Tillad kredit',
'en-GB' => 'Allow credit',
'de-DE' => 'Kredit zulassen'
],
1,
$customFieldSet->getId()
);
$this->addCustomField(
self::CUSTOM_FIELD_GLN,
CustomFieldTypes::TEXT,
[
'da-DK' => 'GLN',
'en-GB' => 'GLN',
'de-DE' => 'GLN'
],
2,
$customFieldSet->getId()
);
$this->addCustomField(
self::CUSTOM_FIELD_CURRENCY_CODE,
CustomFieldTypes::TEXT,
[
'da-DK' => 'Valuta kode',
'en-GB' => 'Currency code',
'de-DE' => 'Währungscode'
],
3,
$customFieldSet->getId()
);
$this->addCustomField(
self::CUSTOM_FIELD_PRICE_GROUP,
CustomFieldTypes::TEXT,
[
'da-DK' => 'Prisgruppe',
'en-GB' => 'Price group',
'de-DE' => 'Preisgruppe'
],
4,
$customFieldSet->getId()
);
$this->addCustomField(
self::CUSTOM_FIELD_DISCOUNT_GROUP,
CustomFieldTypes::TEXT,
[
'da-DK' => 'Rabatgruppe',
'en-GB' => 'Discount group',
'de-DE' => 'Rabattgruppe'
],
5,
$customFieldSet->getId()
);
$this->addCustomField(
self::CUSTOM_FIELD_CONTACT_PERSON,
CustomFieldTypes::TEXT,
[
'da-DK' => 'Kontaktperson',
'en-GB' => 'Contact person',
'de-DE' => 'Gesprächspartner'
],
6,
$customFieldSet->getId()
);
$this->addCustomField(
self::CUSTOM_FIELD_HOMEPAGE,
CustomFieldTypes::TEXT,
[
'da-DK' => 'Hjemmeside',
'en-GB' => 'Homepage',
'de-DE' => 'Startseite'
],
7,
$customFieldSet->getId()
);
//Order export
$customFieldSet = $this->addCustomFieldSet(
self::CUSTOM_SET_ORDER_EXPORT,
[
'da-DK' => 'Business Central order export',
'en-GB' => 'Business Central order export',
'de-DE' => 'Business Central order export'
],
[
'order',
'order_line_item'
]
);
$this->addCustomField(
self::CUSTOM_FIELD_ORDER_EXPORTED_DATE,
CustomFieldTypes::DATETIME,
[
'da-DK' => 'Eksport dato',
'en-GB' => 'Export date',
'de-DE' => 'Exportdatum'
],
1,
$customFieldSet->getId()
);
}
/**
* @param string $name
* @param array $labels
* @param array $entityNames
* @return CustomFieldSetEntity
*/
protected function addCustomFieldSet(
string $name,
array $labels,
array $entityNames
): CustomFieldSetEntity {
$this->context = $this->context ?? Context::createDefaultContext();
if (! $this->customFieldSetRepository) {
$this->customFieldSetRepository = $this->container->get('custom_field_set.repository');
}
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $name));
/** @var CustomFieldSetEntity $customFieldSet */
$customFieldSet = $this->customFieldSetRepository->search(
$criteria,
$this->context
)->first();
if (! $customFieldSet) {
$relations = [];
foreach ($entityNames as $entityName) {
$relations[] = [
'entityName' => $entityName
];
}
$this->customFieldSetRepository->create(
[
[
'name' => $name,
'config' => [
'label' => $labels
],
'relations' => $relations,
],
],
$this->context
);
$customFieldSet = $this->customFieldSetRepository->search(
$criteria,
$this->context
)->first();
}
return $customFieldSet;
}
/**
* @param string $name
* @param string $type
* @param array $labels
* @param int $position
* @param string $customFieldSetId
*/
protected function addCustomField(
string $name,
string $type,
array $labels,
int $position,
string $customFieldSetId
): void {
$this->context = $this->context ?? Context::createDefaultContext();
if (! $this->customFieldRepository) {
$this->customFieldRepository = $this->container->get('custom_field.repository');
}
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $name));
$customFieldExists = $this->customFieldRepository->searchIds(
$criteria,
$this->context
)->firstId();
if (!$customFieldExists) {
if (! $this->customFieldSetRepository) {
$this->customFieldSetRepository = $this->container->get('custom_field_set.repository');
}
$this->customFieldSetRepository->upsert([
[
'id' => $customFieldSetId,
'customFields' => [
[
'name' => $name,
'type' => $type,
'config' => [
'label' => $labels,
'componentName' => 'sw-field',
'customFieldType' => $type == CustomFieldTypes::BOOL ? 'checkbox' : $type,
'customFieldPosition' => $position,
],
]
]
]
], $this->context);
}
}
}