Skip to content

Commit

Permalink
fix: dpz weight classes automatically selected and retained (#53)
Browse files Browse the repository at this point in the history
Co-authored-by: Joeri van Veen <joeri@myparcel.nl>
Co-authored-by: Edie Lemoine <edie@myparcel.nl>
  • Loading branch information
3 people authored and RichardPerdaan committed Nov 11, 2021
1 parent a274f3f commit e398582
Show file tree
Hide file tree
Showing 31 changed files with 840 additions and 195 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
"scripts": {
"format": "vendor/bin/php-cs-fixer fix ."
}
}
}
56 changes: 31 additions & 25 deletions controllers/admin/AdminMyParcelBELabelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

use Gett\MyparcelBE\Constant;
use Gett\MyparcelBE\Database\Table;
use Gett\MyparcelBE\DeliveryOptions\DeliveryOptions;
use Gett\MyparcelBE\DeliverySettings\DeliverySettings;
use Gett\MyparcelBE\DeliverySettings\ExtraOptions;
use Gett\MyparcelBE\Factory\Consignment\ConsignmentFactory;
use Gett\MyparcelBE\Label\LabelOptionsResolver;
use Gett\MyparcelBE\Logger\ApiLogger;
use Gett\MyparcelBE\Logger\Logger;
use Gett\MyparcelBE\Model\Core\Order;
use Gett\MyparcelBE\Module\Carrier\Provider\CarrierSettingsProvider;
use Gett\MyparcelBE\Module\Carrier\Provider\DeliveryOptionsProvider;
use Gett\MyparcelBE\Module\Tools\Tools;
Expand All @@ -15,12 +17,15 @@
use Gett\MyparcelBE\Service\DeliverySettingsProvider;
use Gett\MyparcelBE\Service\ErrorMessage;
use Gett\MyparcelBE\Service\MyparcelStatusProvider;
use Gett\MyparcelBE\Service\Order\OrderTotalWeight;
use MyParcelNL\Sdk\src\Exception\InvalidConsignmentException;
use MyParcelNL\Sdk\src\Factory\ConsignmentFactory as ConsignmentFactorySdk;
use MyParcelNL\Sdk\src\Factory\DeliveryOptionsAdapterFactory;
use MyParcelNL\Sdk\src\Helper\MyParcelCollection;
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
use MyParcelNL\Sdk\src\Model\Consignment\DPDConsignment;
use MyParcelNL\Sdk\src\Model\Consignment\PostNLConsignment;
use PrestaShop\PrestaShop\Adapter\Entity\Db;

if (file_exists(_PS_MODULE_DIR_ . 'myparcelbe/vendor/autoload.php')) {
require_once _PS_MODULE_DIR_ . 'myparcelbe/vendor/autoload.php';
Expand Down Expand Up @@ -388,42 +393,40 @@ public function ajaxProcessSaveConcept(): void
}

$orderId = (int) $postValues['id_order'];
$currency = Currency::getDefaultCurrency();
$deliveryOptionsArray = DeliveryOptions::queryByOrderId($orderId);
$deliveryOptions = Tools::arrayToObject($deliveryOptionsArray) ?? new stdClass();
$order = new Order($orderId);
$deliveryOptionsArray = DeliverySettings::getDeliveryOptionsFromOrder($order)->toArray();
$extraOptions = new ExtraOptions($postValues);

try {
$order = new Order($orderId);
$deliveryOptions->date = $postValues['deliveryDate'] . 'T00:00:00.000Z';
$deliveryOptions->shipmentOptions = new stdClass(); // Reset shipment options

$deliveryOptionsArray['date'] = $postValues['deliveryDate'] . 'T00:00:00.000Z';
$deliveryOptionsArray['shipmentOptions'] = [];
foreach (Constant::SINGLE_LABEL_CREATION_OPTIONS as $key => $name) {
if (isset($postValues[$key])) {
switch ($key) {
case 'packageType':
$deliveryOptions->shipmentOptions->package_type = $postValues[$key];
$deliveryOptionsArray['packageType'] = Constant::PACKAGE_TYPES[$postValues[$key]] ?? AbstractConsignment::DEFAULT_PACKAGE_TYPE;
break;
case 'packageFormat':
if (Constant::PACKAGE_FORMATS[$postValues[$key]] == 'large') {
$deliveryOptions->shipmentOptions->large_format = true;
if ('large' === Constant::PACKAGE_FORMATS[$postValues[$key]]) {
$deliveryOptionsArray['shipmentOptions']['large_format'] = true;
}
break;
case 'onlyRecipient':
$deliveryOptions->shipmentOptions->only_recipient = true;
$deliveryOptionsArray['shipmentOptions']['only_recipient'] = true;
break;
case 'ageCheck':
$deliveryOptions->shipmentOptions->age_check = true;
$deliveryOptionsArray['shipmentOptions']['age_check'] = true;
break;
case 'returnUndelivered':
$deliveryOptions->shipmentOptions->return = true;
$deliveryOptionsArray['shipmentOptions']['return'] = true;
break;
case 'signatureRequired':
$deliveryOptions->shipmentOptions->signature = true;
$deliveryOptionsArray['shipmentOptions']['signature'] = true;
break;
case 'insurance':
$deliveryOptions->shipmentOptions->insurance = new stdClass();
$deliveryOptionsArray['shipmentOptions']['insurance'] = [];
if (isset($postValues['insuranceAmount'])) {
if (strpos($postValues['insuranceAmount'], 'amount') !== false) {
if (false !== strpos($postValues['insuranceAmount'], 'amount')) {
$insuranceValue = (int) str_replace(
'amount',
'',
Expand All @@ -432,20 +435,18 @@ public function ajaxProcessSaveConcept(): void
} else {
$insuranceValue = (int) ($postValues['insurance-amount-custom-value'] ?? 0);
}
$deliveryOptions->shipmentOptions->insurance->amount = $insuranceValue * 100; // cents
$deliveryOptions->shipmentOptions->insurance->currency = $currency->iso_code;
$deliveryOptionsArray['shipmentOptions']['insurance'] = $insuranceValue * 100;
}
break;
default:
break;
}
}
}
Db::getInstance(_PS_USE_SQL_SLAVE_)->update(
'myparcelbe_delivery_settings',
['delivery_settings' => pSQL(json_encode($deliveryOptions))],
'id_cart = ' . (int) $order->id_cart
);
$deliveryOptions = DeliveryOptionsAdapterFactory::create($deliveryOptionsArray);
DeliverySettings::setDeliveryOptionsForOrder($deliveryOptions, $order);
DeliverySettings::setExtraOptionsForOrder($extraOptions, $order);
DeliverySettings::persist();
} catch (Exception $e) {
$this->errors[] = $this->module->l('Error loading the delivery options.', 'adminlabelcontroller');
$this->errors[] = $e->getMessage();
Expand All @@ -463,7 +464,6 @@ public function returnAjaxResponse($response = [], $idOrder = null)
}

if ($idOrder) {

$labelList = OrderLabel::getOrderLabels((int) $idOrder, []);
$labelListHtml = $this->context->smarty->createData(
$this->context->smarty
Expand Down Expand Up @@ -693,6 +693,9 @@ public function ajaxProcessUpdateDeliveryOptions(): void
);
}

$weight = (new OrderTotalWeight())->convertWeightToGrams($order->getTotalWeight());
$extraOptions = DeliverySettings::getExtraOptionsFromOrder($order);
$digitalStampWeight = $extraOptions->getDigitalStampWeight() ?? $weight;
$deliveryOptionsProvider = new DeliveryOptionsProvider();
$deliveryOptions = $deliveryOptionsProvider->provide($order->getId());
$carrierSettingsProvider = new CarrierSettingsProvider($this->module);
Expand All @@ -710,6 +713,9 @@ public function ajaxProcessUpdateDeliveryOptions(): void
'id_order' => $order->getId(),
'id_carrier' => $order->getIdCarrier(),
]),
'weight' => $weight,
'labelAmount' => $extraOptions->getLabelAmount(),
'digitalStampWeight' => $digitalStampWeight,
]);

$labelConceptHtmlTpl = $this->context->smarty->createTemplate(
Expand Down
2 changes: 1 addition & 1 deletion myparcelbe.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function getOrderShippingCost($cart, $shipping_cost)
if ($deliverySettings) {
$deliverySettings = json_decode($deliverySettings, true);
} else {
$deliverySettings = \Gett\MyparcelBE\DeliveryOptions\DeliveryOptions::queryByCart((int) $cart->id);
$deliverySettings = \Gett\MyparcelBE\DeliverySettings\DeliveryOptions::queryByCart((int) $cart->id);
}

if (empty($deliverySettings)) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@
"browserslist": [
"defaults"
]
}
}
3 changes: 2 additions & 1 deletion src/Carrier/PackageTypeCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Gett\MyparcelBE\Module\Carrier\ExclusiveField;
use Gett\MyparcelBE\Service\CarrierConfigurationProvider;
use Gett\MyparcelBE\Service\ProductConfigurationProvider;
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
use Order;

class PackageTypeCalculator extends AbstractPackageCalculator
Expand Down Expand Up @@ -113,7 +114,7 @@ private function getCarrierPackageTypes(Carrier $carrier, string $countryIso): a
$exclusiveField = new ExclusiveField();
$carrierType = $exclusiveField->getCarrierType($carrier);
$packageTypes = [];
foreach (Constant::PACKAGE_TYPES as $packageType => $packageName) {
foreach (AbstractConsignment::PACKAGE_TYPES_IDS as $packageType) {
if ($exclusiveField->isAvailable(
$countryIso,
$carrierType,
Expand Down
12 changes: 12 additions & 0 deletions src/Constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Gett\MyparcelBE;

use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
use PrestaShopBundle\Form\Admin\Sell\Order\Invoices\GenerateByDateType;

class Constant
{
public const MENU_API_SETTINGS = 0;
Expand Down Expand Up @@ -32,6 +35,7 @@ class Constant
public const CUSTOMS_ORIGIN_CONFIGURATION_NAME = 'MYPARCELBE_CUSTOMS_ORIGIN';
public const DEFAULT_CUSTOMS_ORIGIN_CONFIGURATION_NAME = 'MYPARCELBE_DEFAULT_CUSTOMS_ORIGIN';
public const CUSTOMS_AGE_CHECK_CONFIGURATION_NAME = 'MYPARCELBE_CUSTOMS_AGE_CHECK';
public const DIGITAL_STAMP_WEIGHT_CONFIGURATION_NAME = 'MYPARCELBE_DIGITAL_STAMP_WEIGHT';

public const SINGLE_LABEL_CREATION_OPTIONS = [
'packageType' => self::PACKAGE_TYPE_CONFIGURATION_NAME,
Expand Down Expand Up @@ -64,6 +68,14 @@ class Constant
self::PACKAGE_TYPE_LETTER => 'letter',
self::PACKAGE_TYPE_DIGITAL_STAMP => 'digital stamp',
];
public const PACKAGE_TYPES_LEGACY_NAMES_IDS_MAP = [
'mailbox package' => AbstractConsignment::PACKAGE_TYPE_MAILBOX,
'digital stamp' => AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP,
AbstractConsignment::PACKAGE_TYPE_PACKAGE_NAME => AbstractConsignment::PACKAGE_TYPE_PACKAGE,
AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME => AbstractConsignment::PACKAGE_TYPE_MAILBOX,
AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP_NAME => AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP_NAME,
AbstractConsignment::PACKAGE_TYPE_LETTER_NAME => AbstractConsignment::PACKAGE_TYPE_LETTER,
];

public const PACKAGE_TYPE_WEIGHT_LIMIT = 2; // Kg
public const PACKAGE_FORMATS = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

declare(strict_types=1);

namespace Gett\MyparcelBE\DeliveryOptions;
namespace Gett\MyparcelBE\DeliverySettings;

use Db;
use DbQuery;
use Exception;
use Gett\MyparcelBE\Database\Table;
use Gett\MyparcelBE\Module\Tools\Tools;
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter;
use MyParcelNL\Sdk\src\Factory\DeliveryOptionsAdapterFactory;
use Order;
Expand Down Expand Up @@ -58,9 +54,13 @@ public static function getFromOrder($orderOrId): ?AbstractDeliveryOptionsAdapter
*/
public static function queryByCart(int $cartId): array
{
$query = self::getQuery();
$query->where('id_cart = ' . $cartId);
return self::executeQuery($query);
$deliveryOptions = DeliverySettingsRepository::getInstance()::getDeliveryOptionsByCartId($cartId);

if (! $deliveryOptions) {
return [];
}

return $deliveryOptions->toArray();
}

/**
Expand Down Expand Up @@ -89,25 +89,6 @@ public static function queryByOrderId(int $orderId): array
return self::queryByCart((int) $order->id_cart);
}

/**
* @param \DbQuery $query
*
* @return array
*/
private static function executeQuery(DbQuery $query): array
{
$query->orderBy('id_delivery_setting DESC');

$result = Db::getInstance(_PS_USE_SQL_SLAVE_)
->getValue($query);

if ('null' === $result || empty($result)) {
return [];
}

return json_decode($result, true);
}

/**
* @param \Order|int $orderOrId
*
Expand All @@ -128,15 +109,4 @@ private static function getOrder($orderOrId): Order

throw new Exception('Order or order_id must be passed.');
}

/**
* @return \DbQuery
*/
private static function getQuery(): DbQuery
{
$query = new DbQuery();
$query->select('delivery_settings');
$query->from(Table::TABLE_DELIVERY_SETTINGS);
return $query;
}
}
63 changes: 63 additions & 0 deletions src/DeliverySettings/DeliverySettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Gett\MyparcelBE\DeliverySettings;

use Gett\MyparcelBE\Model\Core\Order;
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter;

class DeliverySettings
{
/**
* @param \Gett\MyparcelBE\Model\Core\Order $order
*
* @return \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter
*/
public static function getDeliveryOptionsFromOrder(Order $order): AbstractDeliveryOptionsAdapter
{
return DeliverySettingsRepository::getInstance()::getDeliveryOptionsByCartId((int) $order->id_cart);
}

/**
* @param \Gett\MyparcelBE\Model\Core\Order $order
*
* @return \Gett\MyparcelBE\DeliverySettings\ExtraOptions
*/
public static function getExtraOptionsFromOrder(Order $order): ExtraOptions
{
return DeliverySettingsRepository::getInstance()::getExtraOptionsByCartId((int) $order->id_cart);
}

/**
* @throws \PrestaShopDatabaseException
*/
public static function persist(): void
{
DeliverySettingsRepository::getInstance()::persist();
}

/**
* @param \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter $deliveryOptions
* @param \Gett\MyparcelBE\Model\Core\Order $order
*
* @return void
*/
public static function setDeliveryOptionsForOrder(
AbstractDeliveryOptionsAdapter $deliveryOptions,
Order $order
): void {
DeliverySettingsRepository::getInstance()::setDeliveryOptionsForOrder($deliveryOptions, $order);
}

/**
* @param \Gett\MyparcelBE\DeliverySettings\ExtraOptions $extraOptions
* @param \Gett\MyparcelBE\Model\Core\Order $order
*
* @return void
*/
public static function setExtraOptionsForOrder(ExtraOptions $extraOptions, Order $order): void
{
DeliverySettingsRepository::getInstance()::setExtraOptionsForOrder($extraOptions, $order);
}
}
Loading

0 comments on commit e398582

Please sign in to comment.