Skip to content

Commit

Permalink
Merge branch 'NEXT-11193/create-migration-for-vat-handling' into 'mas…
Browse files Browse the repository at this point in the history
…ter'

NEXT-11193 - Create migration for VAT handling Epic

Closes NEXT-11193

See merge request shopware/6/product/platform!3491
  • Loading branch information
Stefan Sluiter committed Dec 1, 2020
2 parents 6f73238 + 4c8ba5b commit f623c6a
Show file tree
Hide file tree
Showing 17 changed files with 824 additions and 3 deletions.
10 changes: 10 additions & 0 deletions changelog/_unreleased/2020-10-19-vat-handling-for-companies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: VAT handling for companies
issue: NEXT-10559
flag: FEATURE_NEXT_10559
---
# Core
* Added `vat_ids` column and moved `vat_id` data from `customer_address` to `customer` table.
* Added `company_tax_free`, `check_vat_id_pattern` and `vat_id_pattern` columns to `country` table.
* Added `VAT ID field required` to login/registration setting.
* Added `Shopware\Core\Checkout\Customer\DataAbstractionLayer\CustomerVatIdsDeprecationUpdater`.
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,19 @@ public function filterByCountryStateId(string $id): self
});
}

/**
* @feature-deprecated (flag:FEATURE_NEXT_10559) tag:v6.4.0 - Will be removed and use CustomerCollection:getListVatIds() instead
*/
public function getVatIds(): array
{
return $this->fmap(function (CustomerAddressEntity $customerAddress) {
return $customerAddress->getVatId();
});
}

/**
* @feature-deprecated (flag:FEATURE_NEXT_10559) tag:v6.4.0 - Will be removed and use CustomerCollection:filterByVatId() instead
*/
public function filterByVatId(string $id): self
{
return $this->filter(function (CustomerAddressEntity $customerAddress) use ($id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\CustomFields;
use Shopware\Core\Framework\DataAbstractionLayer\Field\FkField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Deprecated;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\PrimaryKey;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Required;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\SearchRanking;
Expand Down Expand Up @@ -66,7 +67,7 @@ protected function defineFields(): FieldCollection
(new StringField('street', 'street'))->addFlags(new Required(), new SearchRanking(SearchRanking::HIGH_SEARCH_RANKING)),
new StringField('department', 'department'),
new StringField('title', 'title'),
new StringField('vat_id', 'vatId'),
(new StringField('vat_id', 'vatId'))->addFlags(new Deprecated('v4', 'v4')),
new StringField('phone_number', 'phoneNumber'),
(new StringField('additional_address_line1', 'additionalAddressLine1'))->addFlags(new SearchRanking(SearchRanking::MIDDLE_SEARCH_RANKING)),
(new StringField('additional_address_line2', 'additionalAddressLine2'))->addFlags(new SearchRanking(SearchRanking::MIDDLE_SEARCH_RANKING)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class CustomerAddressEntity extends Entity
protected $street;

/**
* @feature-deprecated (flag:FEATURE_NEXT_10559) tag:v6.4.0 - Will be removed
*
* @var string|null
*/
protected $vatId;
Expand Down Expand Up @@ -238,11 +240,17 @@ public function setStreet(string $street): void
$this->street = $street;
}

/**
* @feature-deprecated (flag:FEATURE_NEXT_10559) tag:v6.4.0 - Will be removed
*/
public function getVatId(): ?string
{
return $this->vatId;
}

/**
* @feature-deprecated (flag:FEATURE_NEXT_10559) tag:v6.4.0 - Will be removed
*/
public function setVatId(string $vatId): void
{
$this->vatId = $vatId;
Expand Down
20 changes: 20 additions & 0 deletions src/Core/Checkout/Customer/CustomerCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ public function getDefaultShippingAddress(): CustomerAddressCollection
);
}

/**
* @internal (flag:FEATURE_NEXT_10559)
*/
public function getListVatIds(): array
{
return $this->fmap(function (CustomerEntity $customer) {
return $customer->getVatIds();
});
}

/**
* @internal (flag:FEATURE_NEXT_10559)
*/
public function filterByVatId(string $id): self
{
return $this->filter(function (CustomerEntity $customer) use ($id) {
return in_array($id, $customer->getVatIds() ?? [], true);
});
}

public function getApiAlias(): string
{
return 'customer_collection';
Expand Down
7 changes: 7 additions & 0 deletions src/Core/Checkout/Customer/CustomerDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\WriteProtected;
use Shopware\Core\Framework\DataAbstractionLayer\Field\IdField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\IntField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ListField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToManyAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToManyIdField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToOneAssociationField;
Expand Down Expand Up @@ -150,6 +151,12 @@ protected function defineFields(): FieldCollection
);
}

if (Feature::isActive('FEATURE_NEXT_10559')) {
$fields->add(
new ListField('vat_ids', 'vatIds', StringField::class)
);
}

return $fields;
}
}
23 changes: 23 additions & 0 deletions src/Core/Checkout/Customer/CustomerEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ class CustomerEntity extends Entity
*/
protected $title;

/**
* @internal (flag:FEATURE_NEXT_10559)
*
* @var array|null
*/
protected $vatIds;

/**
* @var string|null
*/
Expand Down Expand Up @@ -472,6 +479,22 @@ public function setTitle(?string $title): void
$this->title = $title;
}

/**
* @internal (flag:FEATURE_NEXT_10559)
*/
public function getVatIds(): ?array
{
return $this->vatIds;
}

/**
* @internal (flag:FEATURE_NEXT_10559)
*/
public function setVatIds(?array $vatIds): void
{
$this->vatIds = $vatIds;
}

public function getActive(): bool
{
return $this->active;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexer;
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexingMessage;
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\ManyToManyIdFieldUpdater;
use Shopware\Core\Framework\Feature;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class CustomerIndexer extends EntityIndexer
Expand Down Expand Up @@ -40,18 +41,27 @@ class CustomerIndexer extends EntityIndexer
*/
private $eventDispatcher;

/**
* @feature-deprecated (flag:FEATURE_NEXT_10559) tag:v6.4.0 - property $customerVatIdsDeprecationUpdater will be removed in 6.4.0
*
* @var CustomerVatIdsDeprecationUpdater
*/
private $customerVatIdsDeprecationUpdater;

public function __construct(
IteratorFactory $iteratorFactory,
EntityRepositoryInterface $repository,
CacheClearer $cacheClearer,
ManyToManyIdFieldUpdater $manyToManyIdFieldUpdater,
EventDispatcherInterface $eventDispatcher
EventDispatcherInterface $eventDispatcher,
CustomerVatIdsDeprecationUpdater $customerVatIdsDeprecationUpdater
) {
$this->iteratorFactory = $iteratorFactory;
$this->repository = $repository;
$this->cacheClearer = $cacheClearer;
$this->manyToManyIdFieldUpdater = $manyToManyIdFieldUpdater;
$this->eventDispatcher = $eventDispatcher;
$this->customerVatIdsDeprecationUpdater = $customerVatIdsDeprecationUpdater;
}

public function getName(): string
Expand Down Expand Up @@ -80,6 +90,14 @@ public function update(EntityWrittenContainerEvent $event): ?EntityIndexingMessa
return null;
}

if (Feature::isActive('FEATURE_NEXT_10559')) {
$customerEvent = $event->getEventByEntityName(CustomerDefinition::ENTITY_NAME);

if ($customerEvent) {
$this->customerVatIdsDeprecationUpdater->updateByEvent($customerEvent);
}
}

return new CustomerIndexingMessage(array_values($updates), null, $event->getContext());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Checkout\Customer\DataAbstractionLayer;

use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\Uuid\Uuid;

/**
* @feature-deprecated (flag:FEATURE_NEXT_10559) tag:v6.4.0 - class will be removed in 6.4.0
*/
class CustomerVatIdsDeprecationUpdater
{
/**
* @var Connection
*/
private $connection;

public function __construct(Connection $connection)
{
$this->connection = $connection;
}

public function updateByEvent(EntityWrittenEvent $event): void
{
foreach ($event->getPayloads() as $payload) {
if (isset($payload['vatIds'])) {
$this->updateVatIdsInCustomer($payload['vatIds'], $payload['id']);
}
}
}

private function updateVatIdsInCustomer(array $vatIds, string $customerId): void
{
if (!empty($vatIds)) {
$this->connection->executeUpdate(
'UPDATE `customer_address` SET `vat_id` = :vatId
WHERE `customer_address`.`customer_id` = :customerId
AND (customer_address.vat_id <> :vatId OR customer_address.vat_id IS NULL)',
[
'vatId' => $vatIds[0],
'customerId' => Uuid::fromHexToBytes($customerId),
]
);
} else {
$this->connection->executeUpdate(
'UPDATE `customer_address` SET `vat_id` = NULL WHERE `customer_id` = :customerId',
['customerId' => Uuid::fromHexToBytes($customerId)]
);
}
}
}
5 changes: 5 additions & 0 deletions src/Core/Checkout/DependencyInjection/customer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,17 @@
<argument type="service" id="customer_group.repository"/>
</service>

<service id="Shopware\Core\Checkout\Customer\DataAbstractionLayer\CustomerVatIdsDeprecationUpdater">
<argument type="service" id="Doctrine\DBAL\Connection"/>
</service>

<service id="Shopware\Core\Checkout\Customer\DataAbstractionLayer\CustomerIndexer">
<argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory"/>
<argument type="service" id="customer.repository"/>
<argument type="service" id="Shopware\Core\Framework\Adapter\Cache\CacheClearer"/>
<argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\Indexing\ManyToManyIdFieldUpdater"/>
<argument type="service" id="event_dispatcher"/>
<argument type="service" id="Shopware\Core\Checkout\Customer\DataAbstractionLayer\CustomerVatIdsDeprecationUpdater"/>
<tag name="shopware.entity_indexer" priority="100"/>
</service>

Expand Down
Loading

0 comments on commit f623c6a

Please sign in to comment.