Skip to content

Commit

Permalink
fix(regression): fix errors in order list (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Dec 20, 2021
1 parent cb8bf47 commit bd99240
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 274 deletions.
320 changes: 126 additions & 194 deletions controllers/admin/AdminMyParcelBELabelController.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private function getLargeFormat(Collection $data): ?bool
*/
private function getValue(Collection $data, string $name)
{
return $data->firstWhere('name', $name)['value'];
return $data->firstWhere('name', $name)['value'] ?? null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Carrier/PackageFormatCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getOrderPackageFormat(Order $order): int
$largePackageTypeIndex = Constant::PACKAGE_FORMAT_LARGE_INDEX;

if ($productPackageFormats) {
if (isset($productPackageFormats[$largePackageTypeIndex])) {
if (in_array($largePackageTypeIndex, $productPackageFormats)) {
return $largePackageTypeIndex;
}

Expand Down
2 changes: 1 addition & 1 deletion src/DeliveryOptions/AbstractSettingsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ protected function getTable(): string
throw new Exception('Static property $table must be set');
}

return Table::withPrefix(static::$table);
return static::$table;
}
}
61 changes: 61 additions & 0 deletions src/DeliveryOptions/DeliveryOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Gett\MyparcelBE\DeliveryOptions;

use Exception;
use Gett\MyparcelBE\Adapter\DeliveryOptionsFromDefaultExportSettingsAdapter;
use Gett\MyparcelBE\Adapter\DeliveryOptionsFromFormAdapter;
use Gett\MyparcelBE\Database\Table;
use Gett\MyparcelBE\DeliverySettings\DeliverySettingsRepository;
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter;
Expand Down Expand Up @@ -125,6 +127,28 @@ public static function save(int $cartId, array $deliveryOptions = [], array $ext
);
}

/**
* @param \Gett\MyparcelBE\Model\Core\Order $order
* @param array $values
*
* @return \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter
* @throws \Exception
*/
public static function updateDeliveryOptions(
\Gett\MyparcelBE\Model\Core\Order $order,
array $values = []
): AbstractDeliveryOptionsAdapter {
$deliveryOptionsToMerge = self::getDeliveryOptionsAdapters($order);

if (! empty($values)) {
$deliveryOptionsToMerge[] = new DeliveryOptionsFromFormAdapter($values);
}

$deliveryOptions = DeliveryOptionsMerger::create(...$deliveryOptionsToMerge);
self::save($order->getIdCart(), $deliveryOptions->toArray());
return $deliveryOptions;
}

/**
* @param \PrestaShop\PrestaShop\Adapter\Entity\DbQuery $query
*
Expand All @@ -144,6 +168,43 @@ private static function executeQuery(DbQuery $query): array
return json_decode($result, true);
}

/**
* @param int $psCarrierId
*
* @return \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter
* @throws \PrestaShopDatabaseException
* @throws \Exception
*/
private static function getDefaultExportDeliveryOptions(int $psCarrierId): AbstractDeliveryOptionsAdapter
{
$defaultExportSettings = DefaultExportSettingsRepository::getInstance()
->getByCarrier($psCarrierId);

return new DeliveryOptionsFromDefaultExportSettingsAdapter($defaultExportSettings);
}

/**
* @param \Gett\MyparcelBE\Model\Core\Order $order
*
* @return \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter[]
* @throws \PrestaShopDatabaseException
* @throws \Exception
*/
private static function getDeliveryOptionsAdapters(\Gett\MyparcelBE\Model\Core\Order $order): array
{
$deliveryOptionsArray = [];

$defaultExportDeliveryOptions = self::getDefaultExportDeliveryOptions($order->getIdCarrier());
$deliveryOptionsArray[] = $defaultExportDeliveryOptions;

$orderDeliveryOptions = self::getFromOrder($order->getId());
if ($orderDeliveryOptions) {
$deliveryOptionsArray[] = $orderDeliveryOptions;
}

return $deliveryOptionsArray;
}

/**
* @param \Order|int $orderOrId
*
Expand Down
134 changes: 73 additions & 61 deletions src/Factory/Consignment/ConsignmentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@

namespace Gett\MyparcelBE\Factory\Consignment;

use BadMethodCallException;
use Configuration;
use Country;
use DateInterval;
use DateTime;
use Exception;
use Gett\MyparcelBE\Carrier\PackageTypeCalculator;
use Gett\MyparcelBE\Collection\ConsignmentCollection;
use Gett\MyparcelBE\Constant;
use Gett\MyparcelBE\Logger\Logger;
use Gett\MyparcelBE\DeliveryOptions\DeliveryOptions;
use Gett\MyparcelBE\Model\Core\Order;
use Gett\MyparcelBE\Module\Carrier\Provider\CarrierSettingsProvider;
use Gett\MyparcelBE\Service\CarrierService;
use Gett\MyparcelBE\Service\Consignment\ConsignmentNormalizer;
use Gett\MyparcelBE\Service\Order\OrderTotalWeight;
use Gett\MyparcelBE\Service\Platform\PlatformServiceFactory;
use Gett\MyparcelBE\Service\ProductConfigurationProvider;
use Module;
use MyParcelBE;
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter;
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractShipmentOptionsAdapter;
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\DeliveryOptionsFromOrderAdapter;
use MyParcelNL\Sdk\src\Factory\DeliveryOptionsAdapterFactory;
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
use MyParcelNL\Sdk\src\Model\MyParcelCustomsItem;
use MyParcelNL\Sdk\src\Support\Arr;
use OrderLabel;
use Tools;

class ConsignmentFactory
{
private const FORMAT_TIMESTAMP = 'Y-m-d H:i:s';

/**
* @var string
*/
Expand All @@ -56,7 +54,7 @@ class ConsignmentFactory
private $request;

/**
* @var \Module
* @var \MyParcelBE
*/
private $module;

Expand All @@ -71,11 +69,11 @@ class ConsignmentFactory
private $orderData;

/**
* @param string $apiKey
* @param array $request
* @param Module $module
* @param string $apiKey
* @param array $request
* @param \MyParcelBE $module
*/
public function __construct(string $apiKey, array $request, Module $module)
public function __construct(string $apiKey, array $request, MyParcelBE $module)
{
$this->api_key = $apiKey;
$this->module = $module;
Expand Down Expand Up @@ -112,6 +110,7 @@ public function fromOrders(array $orders): ConsignmentCollection
* @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
* @throws \Exception
*/
public function fromOrder(array $order, AbstractDeliveryOptionsAdapter $deliveryOptions = null): ConsignmentCollection
{
Expand Down Expand Up @@ -142,28 +141,6 @@ private function getShipmentOptions(): ?AbstractShipmentOptionsAdapter
return $this->deliveryOptions->getShipmentOptions();
}

/**
* @param array $order
*
* @return void
* @throws \Exception
*/
private function setDeliveryOptions(array $order): void
{
$deliveryOptionsData = json_decode($order['delivery_settings'], true);

try {
// Create new instance from known json
$this->deliveryOptions = DeliveryOptionsAdapterFactory::create((array) $deliveryOptionsData);
} catch (BadMethodCallException $e) {
Logger::addLog($e->getMessage());

// Create new instance from unknown json data
$deliveryOptions = (new ConsignmentNormalizer((array) $deliveryOptionsData))->normalize();
$this->deliveryOptions = new DeliveryOptionsFromOrderAdapter($deliveryOptions);
}
}

/**
* @param array $order
* @param null|\MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter $deliveryOptions
Expand All @@ -183,13 +160,11 @@ private function setOrderData(array $order, AbstractDeliveryOptionsAdapter $deli
if ($deliveryOptions) {
$this->deliveryOptions = $deliveryOptions;
} else {
$this->setDeliveryOptions($order);
$this->deliveryOptions = DeliveryOptions::updateDeliveryOptions($this->orderObject);
}
}

/**
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
* @throws \Exception
*/
private function setBaseData(): void
Expand All @@ -207,9 +182,14 @@ private function setBaseData(): void

/**
* @return int
* @throws \PrestaShopDatabaseException
*/
private function getPackageType(): int
{
if ($this->module->isBE()) {
return AbstractConsignment::PACKAGE_TYPE_PACKAGE;
}

$packageType = $this->request['packageType'] ?? (new PackageTypeCalculator())->getOrderPackageType($this->orderObject);

if (! isset($this->carrierSettings['delivery']['packageType'][(int) $packageType])) {
Expand All @@ -230,24 +210,20 @@ private function getDeliveryDate(): ?string
return null;
}

$timestamp = strtotime($date);
$deliveryDateTime = date('Y-m-d H:i:s', $timestamp);
$deliveryDate = date('Y-m-d', $timestamp);
$dateOfToday = date('Y-m-d');
$dateOfTomorrow = date('Y-m-d H:i:s', strtotime('now +1 day'));

if ($deliveryDate <= $dateOfToday) {
return $dateOfTomorrow;
}

return $deliveryDateTime;
return $this->fixPastDeliveryDate($date);
}

/**
* @return int
*/
private function getDeliveryType(): int
{
if ($this->module->isBE()) {
return $this->consignment->getDeliveryType() < AbstractConsignment::DELIVERY_TYPE_PICKUP
? AbstractConsignment::DELIVERY_TYPE_STANDARD
: AbstractConsignment::DELIVERY_TYPE_PICKUP;
}

return $this->deliveryOptions->getDeliveryTypeId() ?? AbstractConsignment::DELIVERY_TYPE_STANDARD;
}

Expand Down Expand Up @@ -336,11 +312,14 @@ private function setShipmentOptions(): void
*/
private function hasSignature(): bool
{
$shipmentOptions = $this->deliveryOptions->getShipmentOptions();
$hasSignature = $shipmentOptions && $shipmentOptions->hasSignature()
&& $this->consignment->canHaveShipmentOption(
AbstractConsignment::SHIPMENT_OPTION_SIGNATURE
);
$canHaveSignature = $this->consignment->canHaveShipmentOption(AbstractConsignment::SHIPMENT_OPTION_SIGNATURE);
$isHomeCountry = $this->consignment->getCountry() === $this->module->getModuleCountry();
$shipmentOptions = $this->deliveryOptions->getShipmentOptions();

$hasSignature = $shipmentOptions
&& $canHaveSignature
&& $isHomeCountry
&& $shipmentOptions->hasSignature();

return $this->consignment->getDeliveryType() === AbstractConsignment::DELIVERY_TYPE_PICKUP || $hasSignature;
}
Expand Down Expand Up @@ -455,6 +434,32 @@ private function createConsignment(): void
->generateConsignment($carrier);
}

/**
* @param string $deliveryDate
*
* @return string
*/
private function fixPastDeliveryDate(string $deliveryDate): string
{
$tomorrow = new DateTime('tomorrow');

try {
$deliveryDateObject = new DateTime($deliveryDate);
} catch (Exception $e) {
return $tomorrow->format(self::FORMAT_TIMESTAMP);
}

$oldDate = clone $deliveryDateObject;
$tomorrow->setTime(0, 0);
$oldDate->setTime(0, 0);

do {
$deliveryDateObject->add(new DateInterval('P1D'));
} while ($deliveryDateObject < $tomorrow || '0' === $deliveryDateObject->format('w'));

return $deliveryDateObject->format(self::FORMAT_TIMESTAMP);
}

/**
* @return AbstractConsignment
* @throws \PrestaShopDatabaseException
Expand All @@ -468,13 +473,7 @@ private function initConsignment(): AbstractConsignment
$this->setShipmentOptions();
$this->setPickupLocation();
$this->setCustomsDeclaration();

if (
isset($this->request['digitalStampWeight'])
&& AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP === $this->consignment->getPackageType()
) {
$this->consignment->setTotalWeight($this->request['digitalStampWeight']);
}
$this->setTotalWeight();

return $this->consignment;
}
Expand Down Expand Up @@ -533,4 +532,17 @@ private function getLabelParams(array $order, string $labelParams, string $label

return trim($labelParams);
}

/**
* @return void
*/
private function setTotalWeight(): void
{
if (
isset($this->request['digitalStampWeight'])
&& AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP === $this->consignment->getPackageType()
) {
$this->consignment->setTotalWeight($this->request['digitalStampWeight']);
}
}
}
4 changes: 3 additions & 1 deletion src/Label/LabelOptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ private function getPackageFormat(Order $order, ?AbstractDeliveryOptionsAdapter
*/
private function getPackageType(Order $order, ?AbstractDeliveryOptionsAdapter $deliveryOptions): int
{
$packageType = $deliveryOptions ? $deliveryOptions->getPackageTypeId() : null;
$packageType = $deliveryOptions && $deliveryOptions->getPackageType()
? $deliveryOptions->getPackageTypeId()
: null;
return $packageType ?? (new PackageTypeCalculator())->getOrderPackageType($order);
}

Expand Down
Loading

0 comments on commit bd99240

Please sign in to comment.