Skip to content

Commit

Permalink
fix: fix error when loading order which has delivery options
Browse files Browse the repository at this point in the history
also fixes various other errors
  • Loading branch information
EdieLemoine committed Oct 4, 2021
1 parent be51225 commit 56722a4
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 116 deletions.
95 changes: 55 additions & 40 deletions controllers/admin/AdminMyParcelBELabelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use Gett\MyparcelBE\Constant;
use Gett\MyparcelBE\Database\Table;
use Gett\MyparcelBE\DeliveryOptions\DeliveryOptions;
use Gett\MyparcelBE\DeliveryOptions\DeliveryOptionsFromSaveConceptAdapter;
use Gett\MyparcelBE\Factory\Consignment\ConsignmentFactory;
use Gett\MyparcelBE\Label\LabelOptionsResolver;
use Gett\MyparcelBE\Logger\Logger;
Expand All @@ -26,13 +25,11 @@
require_once _PS_MODULE_DIR_ . 'myparcelbe/vendor/autoload.php';
}

/**
* @property \MyParcelBE $module
*/
class AdminMyParcelBELabelController extends ModuleAdminController
{
public function __construct()
{
parent::__construct();
}

public function initContent()
{
parent::initContent();
Expand All @@ -44,7 +41,6 @@ public function initContent()
}

die();
//Tools::redirectAdmin($this->context->link->getAdminLink('AdminOrders'));
}

public function processReturn()
Expand Down Expand Up @@ -464,10 +460,7 @@ public function returnAjaxResponse($response = [], $idOrder = null)
}

if ($idOrder) {
$psVersion = '';
if (version_compare(_PS_VERSION_, '1.7.7.0', '>=')) {
$psVersion = '-177';
}

$labelList = OrderLabel::getOrderLabels((int) $idOrder, []);
$labelListHtml = $this->context->smarty->createData(
$this->context->smarty
Expand All @@ -478,7 +471,7 @@ public function returnAjaxResponse($response = [], $idOrder = null)
]);

$labelListHtmlTpl = $this->context->smarty->createTemplate(
$this->module->getTemplatePath('views/templates/admin/hook/label-list' . $psVersion . '.tpl'),
$this->getTemplatePath() . 'hook/label-list' . $this->getTemplateSuffix() . '.tpl',
$labelListHtml
);

Expand Down Expand Up @@ -658,24 +651,31 @@ public function ajaxProcessDeleteLabel()
$this->returnAjaxResponse();
}

public function ajaxProcessUpdateDeliveryOptions()
/**
* @throws \PrestaShopException
* @throws \PrestaShopDatabaseException
* @throws \SmartyException
* @throws \Exception
*/
public function ajaxProcessUpdateDeliveryOptions(): void
{
$psVersion = '';
if (version_compare(_PS_VERSION_, '1.7.7.0', '>=')) {
$psVersion = '-177';
}
$postValues = Tools::getAllValues();
$options = $postValues['myparcel-delivery-options'] ?? null;
$action = $postValues['action'] ?? null;
$order = new Order($postValues['id_order'] ?? 0);
if ($action === 'updateDeliveryOptions' && !empty($options) && !empty($order->id_cart)) {
Db::getInstance(_PS_USE_SQL_SLAVE_)->insert(
Table::TABLE_DELIVERY_SETTINGS,
['id_cart' => $order->id_cart, 'delivery_settings' => pSQL($options)],
false,
true,
Db::REPLACE
);
$options = $postValues['myparcel-delivery-options'] ?? null;
$action = $postValues['action'] ?? null;
$order = new \Gett\MyparcelBE\Model\Core\Order($postValues['id_order'] ?? 0);

if ('updateDeliveryOptions' === $action && ! empty($options) && ! empty($order->id_cart)) {
Db::getInstance(_PS_USE_SQL_SLAVE_)
->insert(
Table::TABLE_DELIVERY_SETTINGS,
[
'id_cart' => $order->id_cart,
'delivery_settings' => pSQL($options),
],
false,
true,
Db::REPLACE
);
} else {
$this->errors[] = $this->module->l(
'Error updating the delivery options.',
Expand All @@ -684,25 +684,26 @@ public function ajaxProcessUpdateDeliveryOptions()
}

$deliveryOptionsProvider = new DeliveryOptionsProvider();
$deliveryOptions = $deliveryOptionsProvider->provide($order->id);
$deliveryOptions = $deliveryOptionsProvider->provide($order->getId());
$carrierSettingsProvider = new CarrierSettingsProvider($this->module);
$currency = Currency::getDefaultCurrency();
$labelOptionsResolver = new LabelOptionsResolver();
$currency = Currency::getDefaultCurrency();
$labelOptionsResolver = new LabelOptionsResolver();

$labelConceptHtml = $this->context->smarty->createData($this->context->smarty);
$labelConceptHtml->assign([
'deliveryOptions' => json_decode(json_encode($deliveryOptions), true),
'carrierSettings' => $carrierSettingsProvider->provide($order->id_carrier),
'date_warning_display' => $deliveryOptionsProvider->provideWarningDisplay($order->id),
'isBE' => $this->module->isBE(),
'currencySign' => $currency->getSign(),
'labelOptions' => $labelOptionsResolver->getLabelOptions([
'id_order' => (int) $order->id,
'id_carrier' => (int) $order->id_carrier,
'deliveryOptions' => $deliveryOptions,
'carrierSettings' => $carrierSettingsProvider->provide($order->getIdCarrier()),
'date_warning_display' => $deliveryOptionsProvider->provideWarningDisplay($order->getId()),
'isBE' => $this->module->isBE(),
'currencySign' => $currency->getSign(),
'labelOptions' => $labelOptionsResolver->getLabelOptions([
'id_order' => $order->getId(),
'id_carrier' => $order->getIdCarrier(),
]),
]);

$labelConceptHtmlTpl = $this->context->smarty->createTemplate(
$this->module->getTemplatePath('views/templates/admin/hook/label-concept' . $psVersion . '.tpl'),
$this->getTemplatePath() . 'hook/label-concept' . $this->getTemplateSuffix() . '.tpl',
$labelConceptHtml
);

Expand Down Expand Up @@ -903,4 +904,18 @@ public function ajaxProcessGetDeliverySettings()
echo json_encode($params);
exit;
}

/**
* @return string
*/
private function getTemplateSuffix(): string
{
$suffix = '';

if (version_compare(_PS_VERSION_, '1.7.7.0', '>=')) {
$suffix = '-177';
}

return $suffix;
}
}
2 changes: 1 addition & 1 deletion src/DeliveryOptions/DeliveryOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function getFromOrder($orderOrId): ?AbstractDeliveryOptionsAdapter
$deliveryOptions = self::queryByOrder($order);

if ($deliveryOptions) {
return DeliveryOptionsAdapterFactory::create(Tools::objectToArray($deliveryOptions));
return DeliveryOptionsAdapterFactory::create($deliveryOptions);
}

return null;
Expand Down
7 changes: 4 additions & 3 deletions src/Factory/Consignment/ConsignmentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
use Gett\MyparcelBE\Adapter\DeliveryOptionsFromOrderAdapter;
use Gett\MyparcelBE\Carrier\PackageTypeCalculator;
use Gett\MyparcelBE\Constant;
use Gett\MyparcelBE\Service\Consignment\ConsignmentNormalizer;
use OrderLabel;
use Gett\MyparcelBE\Module\Carrier\Provider\CarrierSettingsProvider;
use Gett\MyparcelBE\Service\CarrierConfigurationProvider;
use Gett\MyparcelBE\Service\Consignment\ConsignmentNormalizer;
use Gett\MyparcelBE\Service\Order\OrderTotalWeight;
use Gett\MyparcelBE\Service\ProductConfigurationProvider;
use Module;
Expand All @@ -28,6 +27,7 @@
use MyParcelNL\Sdk\src\Model\MyParcelCustomsItem;
use MyParcelNL\Sdk\src\Support\Arr;
use Order;
use OrderLabel;
use Tools;
use Validate;

Expand Down Expand Up @@ -140,12 +140,13 @@ public function fromOrder(array $order): MyParcelCollection

/**
* @return array
* @throws \Exception
*/
private function getUserAgent(): array
{
return [
'PrestaShop' => _PS_VERSION_,
'MyParcelBE-PrestaShop' => MyParcelBE::VERSION,
'MyParcelBE-PrestaShop' => MyParcelBE::getModule()->version,
];
}

Expand Down
41 changes: 41 additions & 0 deletions src/Model/Core/Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Gett\MyparcelBE\Model\Core;

use OrderCore;

/**
* Overridden core Order class to allow circumventing some bugs when doing strict typing.
*/
class Order extends OrderCore
{
/**
* @var int|string
* @deprecated This is more often than not a string instead of int. Use getId() instead.
*/
public $id;

/**
* @var int|string
* @deprecated This is more often than not a string instead of int. Use getIdCarrier() instead.
*/
public $id_carrier;

/**
* @return int
*/
public function getId(): int
{
return (int) $this->id;
}

/**
* @return int
*/
public function getIdCarrier(): int
{
return (int) $this->id_carrier;
}
}
57 changes: 43 additions & 14 deletions src/Module/Carrier/Provider/DeliveryOptionsProvider.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,67 @@
<?php

declare(strict_types=1);

namespace Gett\MyparcelBE\Module\Carrier\Provider;

use DateTime;
use Gett\MyparcelBE\DeliveryOptions\DeliveryOptions;
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\DeliveryOptionsV3Adapter;

class DeliveryOptionsProvider
{
protected $nextDeliveryDate;
/**
* @var DateTime
*/
protected $deliveryDate;

public function provide(int $orderId)
/**
* @var DateTime
*/
protected $nextDeliveryDate;

/**
* @param int $orderId
*
* @return array - Delivery options array
* @throws \Exception
*/
public function provide(int $orderId): array
{
$deliveryOptions = DeliveryOptions::queryByOrderId($orderId);
if (empty($deliveryOptions)) {
$deliveryOptions = json_decode(json_encode(['date' => '']));
$deliveryOptions = DeliveryOptions::getFromOrder($orderId);

if (! $deliveryOptions) {
$deliveryOptions = new DeliveryOptionsV3Adapter();
}
$this->nextDeliveryDate = new DateTime('tomorrow');// TODO: get next available delivery date
if (!empty($deliveryOptions->date)) {
$this->deliveryDate = new DateTime($deliveryOptions->date);
$deliveryOptions->date = $this->deliveryDate->format('Y-m-d');

$deliveryOptionsArray = $deliveryOptions->toArray();

if ($deliveryOptions->getDate()) {
$this->deliveryDate = new DateTime($deliveryOptions->getDate());
$this->nextDeliveryDate = new DateTime('tomorrow'); // TODO: get next available delivery date

if ($this->nextDeliveryDate > $this->deliveryDate) {
$deliveryOptions->date = $this->nextDeliveryDate->format('Y-m-d');
$deliveryOptionsArray['date'] = $this->nextDeliveryDate->format('Y-m-d');
}
} else {
$deliveryOptions->date = $this->nextDeliveryDate->format('Y-m-d');
$deliveryOptionsArray['date'] = $this->nextDeliveryDate->format('Y-m-d');
}

return $deliveryOptions;
// Prestashop's formatDate function, (which is used in templates) can't handle our date formats.
$deliveryOptionsArray['date'] = (new DateTime($deliveryOptionsArray['date']))->format('Y-m-d');

return $deliveryOptionsArray;
}

public function provideWarningDisplay(int $orderId)
/**
* @param int $orderId
*
* @return bool
* @throws \Exception
*/
public function provideWarningDisplay(int $orderId): bool
{
if (!$this->nextDeliveryDate) {
if (! $this->nextDeliveryDate) {
$this->provide($orderId);
}

Expand Down
Loading

0 comments on commit 56722a4

Please sign in to comment.