-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #732 from coreshop/issue-707
Disable Customer Deletion if bounded Orders are available
- Loading branch information
Showing
7 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
@order @order_customer_deletion | ||
Feature: Create a new order | ||
|
||
Background: | ||
Given the site operates on a store in "Austria" | ||
And the site operates on locale "en" | ||
And the site has a tax rate "AT" with "20%" rate | ||
And the site has a tax rule group "AT" | ||
And the tax rule group has a tax rule for country "Austria" with tax rate "AT" | ||
And the site has a product "T-Shirt" priced at 2000 | ||
And the product has the tax rule group "AT" | ||
And the site has a customer "some-customer@something.com" | ||
And the customer "some-customer@something.com" has an address with country "Austria", "4600", "Wels", "Freiung", "9-11/N3" | ||
|
||
And the cart belongs to customer "some-customer@something.com" | ||
|
||
Scenario: Create a new order and delete the customer | ||
Given I add the product "T-Shirt" to my cart | ||
And the cart ships to customer "some-customer@something.com" address with postcode "4600" | ||
And the cart invoices to customer "some-customer@something.com" address with postcode "4600" | ||
And I create an order from my cart | ||
Then It should throw an error deleting the customer "some-customer@something.com" | ||
|
||
Scenario: Just delete an Customer Account | ||
Given the site has a customer "some-other@something.com" | ||
And the customer "some-other@something.com" has an address with country "Austria", "4600", "Wels", "Freiung", "9-11/N3" | ||
Then It should not throw an error deleting the customer "some-other@something.com" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/CoreShop/Bundle/CoreBundle/EventListener/CustomerOrderDeletionListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* CoreShop. | ||
* | ||
* This source file is subject to the GNU General Public License version 3 (GPLv3) | ||
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt | ||
* files that are distributed with this source code. | ||
* | ||
* @copyright Copyright (c) 2015-2017 Dominik Pfaffenbauer (https://www.pfaffenbauer.at) | ||
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3) | ||
*/ | ||
|
||
namespace CoreShop\Bundle\CoreBundle\EventListener; | ||
|
||
use CoreShop\Component\Core\Model\CustomerInterface; | ||
use CoreShop\Component\Order\Repository\OrderRepositoryInterface; | ||
use Pimcore\Event\Model\DataObjectDeleteInfoEvent; | ||
use Pimcore\Event\Model\DataObjectEvent; | ||
|
||
final class CustomerOrderDeletionListener | ||
{ | ||
/** | ||
* @var OrderRepositoryInterface | ||
*/ | ||
private $orderRepository; | ||
|
||
/** | ||
* @param OrderRepositoryInterface $orderRepository | ||
*/ | ||
public function __construct( | ||
OrderRepositoryInterface $orderRepository | ||
) { | ||
$this->orderRepository = $orderRepository; | ||
} | ||
|
||
/** | ||
* @param DataObjectDeleteInfoEvent $event | ||
*/ | ||
public function checkCustomerDeletionAllowed(DataObjectDeleteInfoEvent $event) | ||
{ | ||
$object = $event->getObject(); | ||
|
||
if (!$object instanceof CustomerInterface) { | ||
return; | ||
} | ||
|
||
$hasOrders = $this->orderRepository->hasCustomerOrders($object); | ||
|
||
if ($hasOrders) { | ||
$event->setDeletionAllowed(false); | ||
$event->setReason('Cannot delete a customer with orders'); | ||
} | ||
} | ||
|
||
/** | ||
* @param DataObjectEvent $event | ||
*/ | ||
public function checkCustomerOrdersBeforeDeletion(DataObjectEvent $event) | ||
{ | ||
$object = $event->getObject(); | ||
|
||
if (!$object instanceof CustomerInterface) { | ||
return; | ||
} | ||
|
||
$hasOrders = $this->orderRepository->hasCustomerOrders($object); | ||
|
||
if ($hasOrders) { | ||
throw new \InvalidArgumentException(sprintf('Cannot delete a customer with orders')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters