Skip to content

Commit

Permalink
Merge pull request #28799 from Progi1984/issue17903
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-rolland committed Aug 1, 2022
2 parents d2a3865 + 86cdadb commit 79b15c0
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 12 deletions.
6 changes: 6 additions & 0 deletions classes/pdf/HTMLTemplateDeliverySlip.php
Expand Up @@ -24,6 +24,8 @@
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

use PrestaShop\PrestaShop\Core\Util\Sorter;

/**
* @since 1.5
*/
Expand Down Expand Up @@ -126,6 +128,10 @@ public function getContent()
}
}

// Sort products by Reference ID (and if equals (like combination) by Supplier Reference)
$sorter = new Sorter();
$order_details = $sorter->natural($order_details, Sorter::ORDER_DESC, 'product_reference', 'product_supplier_reference');

$this->smarty->assign([
'order' => $this->order,
'order_details' => $order_details,
Expand Down
9 changes: 7 additions & 2 deletions classes/pdf/HTMLTemplateInvoice.php
Expand Up @@ -24,6 +24,8 @@
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

use PrestaShop\PrestaShop\Core\Util\Sorter;

/**
* @since 1.5
*/
Expand Down Expand Up @@ -238,6 +240,10 @@ public function getContent()
unset($order_detail); // don't overwrite the last order_detail later
}

// Sort products by Reference ID (and if equals (like combination) by Supplier Reference)
$sorter = new Sorter();
$order_details = $sorter->natural($order_details, Sorter::ORDER_DESC, 'product_reference', 'product_supplier_reference');

$cart_rules = $this->order->getCartRules();
$free_shipping = false;
foreach ($cart_rules as $key => $cart_rule) {
Expand Down Expand Up @@ -307,10 +313,9 @@ public function getContent()
$footer[$key] = Tools::ps_round($value, Context::getContext()->getComputingPrecision(), $this->order->round_mode);
}

/**
/*
* Need the $round_mode for the tests.
*/
$round_type = null;
switch ($this->order->round_type) {
case Order::ROUND_TOTAL:
$round_type = 'total';
Expand Down
9 changes: 8 additions & 1 deletion classes/pdf/HTMLTemplateOrderSlip.php
Expand Up @@ -24,6 +24,8 @@
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

use PrestaShop\PrestaShop\Core\Util\Sorter;

/**
* @since 1.5
*/
Expand Down Expand Up @@ -159,10 +161,15 @@ public function getContent()
}
}

$order_details = $this->order->products;
// Sort products by Reference ID (and if equals (like combination) by Supplier Reference)
$sorter = new Sorter();
$order_details = $sorter->natural($order_details, Sorter::ORDER_DESC, 'product_reference', 'product_supplier_reference');

$this->smarty->assign([
'order' => $this->order,
'order_slip' => $this->order_slip,
'order_details' => $this->order->products,
'order_details' => $order_details,
'cart_rules' => $this->order_slip->order_slip_type == 1 ? $this->order->getCartRules() : false,
'amount_choosen' => $this->order_slip->order_slip_type == 2 ? true : false,
'delivery_address' => $formatted_delivery_address,
Expand Down
5 changes: 5 additions & 0 deletions src/Adapter/Cart/QueryHandler/GetCartForViewingHandler.php
Expand Up @@ -40,6 +40,7 @@
use PrestaShop\PrestaShop\Core\Domain\Cart\QueryHandler\GetCartForViewingHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Cart\QueryResult\CartView;
use PrestaShop\PrestaShop\Core\Localization\Locale;
use PrestaShop\PrestaShop\Core\Util\Sorter;
use Product;
use StockAvailable;
use Validate;
Expand Down Expand Up @@ -117,6 +118,10 @@ public function handle(GetCartForViewing $query)
$total_shipping = $summary['total_shipping'];
}

// Sort products by Reference ID (and if equals (like combination) by Supplier Reference)
$sorter = new Sorter();
$products = $sorter->natural($products, Sorter::ORDER_DESC, 'reference', 'supplier_reference');

foreach ($products as &$product) {
if ($tax_calculation_method == PS_TAX_EXC) {
$product['product_price'] = $product['price'];
Expand Down
8 changes: 7 additions & 1 deletion src/Adapter/Order/QueryHandler/GetOrderPreviewHandler.php
Expand Up @@ -47,6 +47,7 @@
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderPreviewShippingDetails;
use PrestaShop\PrestaShop\Core\Domain\Order\ValueObject\OrderId;
use PrestaShop\PrestaShop\Core\Localization\Locale\Repository as LocaleRepository;
use PrestaShop\PrestaShop\Core\Util\Sorter;
use State;
use StockAvailable;
use Validate;
Expand Down Expand Up @@ -208,7 +209,12 @@ private function getProductDetails(Order $order): array

$taxCalculationMethod = $this->getOrderTaxCalculationMethod($order);

foreach ($order->getProductsDetail() as $detail) {
$orderDetails = $order->getProductsDetail();
// Sort products by Reference ID (and if equals (like combination) by Supplier Reference)
$sorter = new Sorter();
$orderDetails = $sorter->natural($orderDetails, Sorter::ORDER_DESC, 'product_reference', 'product_supplier_reference');

foreach ($orderDetails as $detail) {
$unitPrice = $detail['unit_price_tax_excl'];
$totalPrice = $detail['total_price_tax_excl'];

Expand Down
Expand Up @@ -43,10 +43,10 @@
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderProductCustomizationsForViewing;
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderProductForViewing;
use PrestaShop\PrestaShop\Core\Domain\Order\QueryResult\OrderProductsForViewing;
use PrestaShop\PrestaShop\Core\Domain\ValueObject\QuerySorting;
use PrestaShop\PrestaShop\Core\Image\Parser\ImageTagSourceParserInterface;
use PrestaShop\PrestaShop\Core\Localization\CLDR\ComputingPrecision;
use PrestaShop\PrestaShop\Core\Localization\Locale;
use PrestaShop\PrestaShop\Core\Util\Sorter;
use Product;
use Shop;
use StockAvailable;
Expand Down Expand Up @@ -175,13 +175,14 @@ public function handle(GetOrderProductsForViewing $query): OrderProductsForViewi

unset($product);

if (QuerySorting::DESC === $query->getProductsSorting()->getValue()) {
// reorder products by order_detail_id DESC
krsort($products);
} else {
// reorder products by order_detail_id ASC
ksort($products);
}
// Sort products by Reference ID (and if equals (like combination) by Supplier Reference)
$sorter = new Sorter();
$products = $sorter->natural(
$products,
$query->getProductsSorting()->getValue(),
'product_reference',
'product_supplier_reference'
);

$productsForViewing = [];

Expand Down
62 changes: 62 additions & 0 deletions src/Core/Util/Sorter.php
@@ -0,0 +1,62 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\PrestaShop\Core\Util;

class Sorter
{
public const ORDER_ASC = 'ASC';
public const ORDER_DESC = 'DESC';

/**
* @param array<array<string, mixed>> $array
* @param string $order
* @param string ...$criterias
*
* @return array
*/
public function natural(array $array, string $order, string ...$criterias): array
{
usort($array, function ($a, $b) use ($order, $criterias) {
$cmp = 0;
foreach ($criterias as $criteria) {
if (!isset($a[$criteria]) || !isset($b[$criteria])) {
return 0;
}
$cmp = strnatcmp($a[$criteria], $b[$criteria]);
if ($cmp !== 0) {
break;
}
}

return static::ORDER_DESC === $order ? $cmp : $cmp * -1;
});

return $array;
}
}

0 comments on commit 79b15c0

Please sign in to comment.