Skip to content

Commit

Permalink
Merge pull request #19581 from matthieu-rolland/status-fix
Browse files Browse the repository at this point in the history
Fix bugs occuring when an order state is deleted but still assigned to orders
  • Loading branch information
Progi1984 committed Jun 19, 2020
2 parents c217744 + d1dd9d6 commit 7f37c46
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 21 deletions.
24 changes: 24 additions & 0 deletions classes/ObjectModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,30 @@ public function deleteSelection($ids)
return $result;
}

/**
* Does a soft delete on current object, using the "deleted" field in DB
* If the model object has no "deleted" property or no "deleted" definition field it will throw an exception
*
* @return bool
*
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
public function softDelete()
{
$definitions = ObjectModel::getDefinition($this);

if (empty($definitions['fields']['deleted'])) {
throw new PrestaShopException('Field "deleted" is missing from definition in object model ' . get_class($this));
}
if (!array_key_exists('deleted', get_object_vars($this))) {
throw new PrestaShopException('Property "deleted" is missing in object model ' . get_class($this));
}
$this->deleted = 1;

return $this->update();
}

/**
* Toggles object status in database.
*
Expand Down
3 changes: 2 additions & 1 deletion classes/order/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,11 +926,12 @@ public static function getCustomerOrders($id_customer, $show_hidden_status = fal
$context = Context::getContext();
}

$orderStates = OrderState::getOrderStates((int) $context->language->id);
$orderStates = OrderState::getOrderStates((int) $context->language->id, false);
$indexedOrderStates = [];
foreach ($orderStates as $orderState) {
$indexedOrderStates[$orderState['id_order_state']] = $orderState;
}

$res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT o.*,
(SELECT SUM(od.`product_quantity`) FROM `' . _DB_PREFIX_ . 'order_detail` od WHERE od.`id_order` = o.`id_order`) nb_products,
Expand Down
11 changes: 7 additions & 4 deletions classes/order/OrderState.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,22 @@ class OrderStateCore extends ObjectModel
* Get all available order statuses.
*
* @param int $id_lang Language id for status name
* @param bool $getDeletedStates
*
* @return array Order statuses
*/
public static function getOrderStates($id_lang)
public static function getOrderStates($id_lang, $filterDeleted = true)
{
$deletedStates = $filterDeleted ? ' WHERE deleted = 0' : '';
$cache_id = 'OrderState::getOrderStates_' . (int) $id_lang;
$cache_id .= $filterDeleted ? '_filterDeleted' : '';

if (!Cache::isStored($cache_id)) {
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT *
FROM `' . _DB_PREFIX_ . 'order_state` os
LEFT JOIN `' . _DB_PREFIX_ . 'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = ' . (int) $id_lang . ')
WHERE deleted = 0
ORDER BY `name` ASC');
LEFT JOIN `' . _DB_PREFIX_ . 'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = ' . (int) $id_lang . ')'
. $deletedStates . ' ORDER BY `name` ASC');
Cache::store($cache_id, $result);

return $result;
Expand Down
4 changes: 2 additions & 2 deletions controllers/admin/AdminStatusesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct()
$this->table = 'order_state';
$this->className = 'OrderState';
$this->lang = true;
$this->deleted = false;
$this->deleted = true;
$this->colorOnBackground = false;
$this->multishop_context = Shop::CONTEXT_ALL;
$this->imageType = 'gif';
Expand Down Expand Up @@ -622,7 +622,7 @@ public function postProcess()
if (!$order_state->isRemovable()) {
$this->errors[] = $this->trans('For security reasons, you cannot delete default order statuses.', [], 'Admin.Shopparameters.Notification');
} else {
return parent::postProcess();
return $order_state->softDelete();
}
} elseif (Tools::isSubmit('submitBulkdelete' . $this->table)) {
if (!$this->access('delete')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/OrderState/OrderStateDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ final class OrderStateDataProvider implements OrderStateDataProviderInterface
*/
public function getOrderStates($languageId)
{
return OrderState::getOrderStates($languageId);
return OrderState::getOrderStates($languageId, false);
}
}
24 changes: 21 additions & 3 deletions src/Core/Form/ChoiceProvider/OrderStateByIdChoiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@

namespace PrestaShop\PrestaShop\Core\Form\ChoiceProvider;

use PrestaShop\PrestaShop\Core\Form\ConfigurableFormChoiceProviderInterface;
use PrestaShop\PrestaShop\Core\Form\FormChoiceAttributeProviderInterface;
use PrestaShop\PrestaShop\Core\Form\FormChoiceProviderInterface;
use PrestaShop\PrestaShop\Core\Order\OrderStateDataProviderInterface;
use PrestaShop\PrestaShop\Core\Util\ColorBrightnessCalculator;
use Symfony\Component\Translation\TranslatorInterface;

/**
* Class OrderStateByIdChoiceProvider provides order state choices with ID values.
*/
final class OrderStateByIdChoiceProvider implements FormChoiceProviderInterface, FormChoiceAttributeProviderInterface
final class OrderStateByIdChoiceProvider implements FormChoiceProviderInterface, FormChoiceAttributeProviderInterface, ConfigurableFormChoiceProviderInterface
{
/**
* @var int language ID
Expand All @@ -51,31 +53,46 @@ final class OrderStateByIdChoiceProvider implements FormChoiceProviderInterface,
*/
private $colorBrightnessCalculator;

/**
* @var TranslatorInterface
*/
private $translator;

/**
* @param int $languageId language ID
* @param OrderStateDataProviderInterface $orderStateDataProvider
* @param ColorBrightnessCalculator $colorBrightnessCalculator
* @param TranslatorInterface $translator
*/
public function __construct(
$languageId,
OrderStateDataProviderInterface $orderStateDataProvider,
ColorBrightnessCalculator $colorBrightnessCalculator
ColorBrightnessCalculator $colorBrightnessCalculator,
TranslatorInterface $translator
) {
$this->languageId = $languageId;
$this->orderStateDataProvider = $orderStateDataProvider;
$this->colorBrightnessCalculator = $colorBrightnessCalculator;
$this->translator = $translator;
}

/**
* Get order state choices.
*
* @param array $options
*
* @return array
*/
public function getChoices()
public function getChoices(array $options = [])
{
$orderStates = $this->orderStateDataProvider->getOrderStates($this->languageId);
$choices = [];

foreach ($orderStates as $orderState) {
if ($orderState['deleted'] == 1 && (empty($options['current_state']) || $options['current_state'] != $orderState['id_order_state'])) {
continue;
}
$orderState['name'] .= $orderState['deleted'] == 1 ? ' ' . $this->translator->trans('(deleted)', [], 'Admin.Global') : '';
$choices[$orderState['name']] = $orderState['id_order_state'];
}

Expand All @@ -93,6 +110,7 @@ public function getChoicesAttributes()
$attrs = [];

foreach ($orderStates as $orderState) {
$orderState['name'] .= $orderState['deleted'] == 1 ? ' ' . $this->translator->trans('(deleted)', [], 'Admin.Global') : '';
$attrs[$orderState['name']]['data-background-color'] = $orderState['color'];
$attrs[$orderState['name']]['data-is-bright'] = $this->colorBrightnessCalculator->isBright($orderState['color']);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Grid/Column/Type/Common/ChoiceColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace PrestaShop\PrestaShop\Core\Grid\Column\Type\Common;

use PrestaShop\PrestaShop\Core\Form\FormChoiceProviderInterface;
use PrestaShop\PrestaShop\Core\Form\ConfigurableFormChoiceProviderInterface;
use PrestaShop\PrestaShop\Core\Grid\Column\AbstractColumn;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand Down Expand Up @@ -59,7 +59,7 @@ protected function configureOptions(OptionsResolver $resolver): void
'color_field' => '',
'record_route_params' => [],
])
->setAllowedTypes('choice_provider', FormChoiceProviderInterface::class)
->setAllowedTypes('choice_provider', ConfigurableFormChoiceProviderInterface::class)
->setAllowedTypes('field', ['string', 'int', 'bool'])
->setAllowedTypes('color_field', 'string')
->setAllowedTypes('route', 'string')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,46 @@

namespace PrestaShopBundle\Form\Admin\Sell\Order;

use PrestaShop\PrestaShop\Core\Form\ConfigurableFormChoiceProviderInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;

class UpdateOrderStatusType extends AbstractType
{
/**
* @var array
* @var ConfigurableFormChoiceProviderInterface
*/
private $statusChoices;
private $statusChoiceProvider;

/**
* @var array
*/
private $statusChoiceAttributes;

/**
* @param array $statusChoices
* @param ConfigurableFormChoiceProviderInterface $statusChoices
* @param array $statusChoiceAttributes
*/
public function __construct(
array $statusChoices,
ConfigurableFormChoiceProviderInterface $statusChoiceProvider,
array $statusChoiceAttributes
) {
$this->statusChoices = $statusChoices;
$this->statusChoiceProvider = $statusChoiceProvider;
$this->statusChoiceAttributes = $statusChoiceAttributes;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$choiceProviderParams = [];
if (!empty($options['data']['new_order_status_id'])) {
$choiceProviderParams = ['current_state' => $options['data']['new_order_status_id']];
}
$builder
->add('new_order_status_id', ChoiceType::class, [
'required' => false,
'placeholder' => false,
'choices' => $this->statusChoices,
'choices' => $this->statusChoiceProvider->getChoices($choiceProviderParams),
'choice_attr' => $this->statusChoiceAttributes,
'translation_domain' => false,
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ services:
form.type.order.update_order_status:
class: 'PrestaShopBundle\Form\Admin\Sell\Order\UpdateOrderStatusType'
arguments:
- '@=service("prestashop.core.form.choice_provider.order_state_by_id").getChoices()'
- '@prestashop.core.form.choice_provider.order_state_by_id'
- '@=service("prestashop.core.form.choice_provider.order_state_by_id").getChoicesAttributes()'
tags:
- { name: form.type }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ services:
- '@=service("prestashop.adapter.legacy.context").getLanguage().id'
- '@prestashop.adapter.data_provider.order_state'
- '@prestashop.core.util.color_brightness_calculator'
- '@translator'

prestashop.core.form.choice_provider.invoice_model_by_name:
class: 'PrestaShop\PrestaShop\Core\Form\ChoiceProvider\InvoiceModelByNameChoiceProvider'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*#}

{% set choices = column.options.choice_provider.getChoices() %}
{% set choices = column.options.choice_provider.getChoices(record) %}
{% set selectedChoice = record[column.options.field] %}
{% set selectedChoiceName = '' %}
{% set routeParams = record|array_pluck(column.options.record_route_params) %}
Expand Down

0 comments on commit 7f37c46

Please sign in to comment.