create error message when a customer is deleted and already have orders.
Fix #199
This commit is contained in:
@@ -31,6 +31,8 @@ use Thelia\Core\Event\LostPasswordEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Core\Security\SecurityContext;
|
||||
use Thelia\Core\Template\ParserInterface;
|
||||
use Thelia\Core\Translation\Translator;
|
||||
use Thelia\Exception\CustomerException;
|
||||
use Thelia\Mailer\MailerFactory;
|
||||
use Thelia\Model\ConfigQuery;
|
||||
use Thelia\Model\Customer as CustomerModel;
|
||||
@@ -106,6 +108,10 @@ class Customer extends BaseAction implements EventSubscriberInterface
|
||||
{
|
||||
if (null !== $customer = $event->getCustomer()) {
|
||||
|
||||
if (true === $customer->hasOrder()) {
|
||||
throw new CustomerException(Translator::getInstance()->trans("Impossible to delete a customer who already have orders"));
|
||||
}
|
||||
|
||||
$customer->delete();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ use Thelia\Core\Security\Resource\AdminResources;
|
||||
use Thelia\Core\Event\Customer\CustomerCreateOrUpdateEvent;
|
||||
use Thelia\Core\Event\Customer\CustomerEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Exception\CustomerException;
|
||||
use Thelia\Form\CustomerCreateForm;
|
||||
use Thelia\Form\CustomerUpdateForm;
|
||||
use Thelia\Model\CustomerQuery;
|
||||
@@ -178,13 +179,14 @@ class CustomerController extends AbstractCrudController
|
||||
);
|
||||
}
|
||||
|
||||
protected function renderListTemplate($currentOrder)
|
||||
protected function renderListTemplate($currentOrder, $customParams = array())
|
||||
{
|
||||
return $this->render('customers', array(
|
||||
return $this->render('customers', array_merge(array(
|
||||
'customer_order' => $currentOrder,
|
||||
'display_customer' => 20,
|
||||
'page' => $this->getRequest()->get('page', 1)
|
||||
));
|
||||
), $customParams)
|
||||
);
|
||||
}
|
||||
|
||||
protected function redirectToListTemplate()
|
||||
@@ -203,4 +205,18 @@ class CustomerController extends AbstractCrudController
|
||||
{
|
||||
$this->redirectToRoute("admin.customer.update.view", $this->getEditionArguments());
|
||||
}
|
||||
|
||||
public function deleteAction()
|
||||
{
|
||||
try {
|
||||
parent::deleteAction();
|
||||
} catch (CustomerException $e) {
|
||||
$error_msg = $e->getMessage();
|
||||
}
|
||||
|
||||
return $this->renderListTemplate($this->getCurrentListOrder(), array(
|
||||
"removal_error" => true,
|
||||
"error_message" => $error_msg
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
34
core/lib/Thelia/Exception/CustomerException.php
Normal file
34
core/lib/Thelia/Exception/CustomerException.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Exception;
|
||||
|
||||
|
||||
/**
|
||||
* Class CustomerException
|
||||
* @package Thelia\Exception
|
||||
* @author Manuel Raynaud <mraynaud@openstudio.fr>
|
||||
*/
|
||||
class CustomerException extends \RuntimeException
|
||||
{
|
||||
}
|
||||
@@ -239,6 +239,15 @@ class Customer extends BaseCustomer implements UserInterface
|
||||
return $this->getRememberMeSerial();
|
||||
}
|
||||
|
||||
public function hasOrder()
|
||||
{
|
||||
$order = OrderQuery::create()
|
||||
->filterByCustomerId($this->getId())
|
||||
->count();
|
||||
|
||||
return $order > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
||||
@@ -20,6 +20,12 @@
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<div class="general-block-decorator">
|
||||
{if $removal_error }
|
||||
<div class="alert alert-danger fade in">
|
||||
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
|
||||
<h4 class="modal-title">{$error_message}</h4>
|
||||
</div>
|
||||
{/if}
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-condensed" id="customer_list">
|
||||
<caption>
|
||||
|
||||
Reference in New Issue
Block a user