Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use default export settings when creating shipment #63

Merged
merged 2 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 51 additions & 9 deletions controllers/admin/AdminMyParcelBELabelController.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

use Gett\MyparcelBE\Adapter\DeliveryOptionsFromDefaultExportSettingsAdapter;
use Gett\MyparcelBE\Adapter\DeliveryOptionsFromFormAdapter;
use Gett\MyparcelBE\Collection\ConsignmentCollection;
use Gett\MyparcelBE\Constant;
use Gett\MyparcelBE\DeliveryOptions\DefaultExportSettingsRepository;
use Gett\MyparcelBE\DeliveryOptions\DeliveryOptions;
use Gett\MyparcelBE\DeliveryOptions\DeliveryOptionsMerger;
use Gett\MyparcelBE\DeliverySettings\DeliverySettings;
Expand All @@ -11,6 +13,7 @@
use Gett\MyparcelBE\Logger\ApiLogger;
use Gett\MyparcelBE\Logger\Logger;
use Gett\MyparcelBE\Model\Core\Order;
use Gett\MyparcelBE\Model\Webhook\StatusChangeWebhookPayload;
use Gett\MyparcelBE\Module\Carrier\Provider\CarrierSettingsProvider;
use Gett\MyparcelBE\Module\Carrier\Provider\DeliveryOptionsProvider;
use Gett\MyparcelBE\Module\Tools\Tools;
Expand All @@ -24,8 +27,6 @@
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter;
use MyParcelNL\Sdk\src\Exception\InvalidConsignmentException;
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
use MyParcelNL\Sdk\src\Model\Consignment\PostNLConsignment;
use \Gett\MyparcelBE\Model\Webhook\StatusChangeWebhookPayload;

if (file_exists(_PS_MODULE_DIR_ . 'myparcelbe/vendor/autoload.php')) {
require_once _PS_MODULE_DIR_ . 'myparcelbe/vendor/autoload.php';
Expand Down Expand Up @@ -437,8 +438,8 @@ public function ajaxProcessSaveConcept(): void
$this->returnAjaxResponse();
}

$orderId = (int) $postValues['id_order'];
$order = new Order($orderId);
$orderId = (int) $postValues['id_order'];
$order = new Order($orderId);

try {
$this->updateDeliveryOptions($order, $postValues);
Expand Down Expand Up @@ -716,7 +717,6 @@ public function ajaxProcessUpdateDeliveryOptions(): void
'date_warning_display' => $deliveryOptionsProvider->provideWarningDisplay($order->getId()),
'isBE' => $this->module->isBE(),
'currencySign' => $currency->getSign(),
'labelOptions' => $labelOptionsResolver->getLabelOptions($order),
'weight' => $weight,
'labelAmount' => $extraOptions->getLabelAmount(),
'digitalStampWeight' => $digitalStampWeight,
Expand Down Expand Up @@ -937,6 +937,49 @@ private function getParsedErrorMessage(Exception $e): string
return $parsedErrorMessage;
}

/**
* @param \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter $deliveryOptions
*
* @return \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter
* @throws \PrestaShopDatabaseException
* @throws \Exception
*/
private function getDefaultExportDeliveryOptions(
AbstractDeliveryOptionsAdapter $deliveryOptions
): AbstractDeliveryOptionsAdapter {
$myParcelCarrierName = $deliveryOptions->getCarrier() ?? PlatformServiceFactory::create()
->getDefaultCarrier()
->getName();

$prestaShopCarrier = CarrierService::getPrestashopCarrier($myParcelCarrierName);
$defaultExportSettings = DefaultExportSettingsRepository::getInstance()
->getByCarrier($prestaShopCarrier->id);

return new DeliveryOptionsFromDefaultExportSettingsAdapter($defaultExportSettings);
}

/**
* @param \Gett\MyparcelBE\Model\Core\Order $order
*
* @return array
* @throws \PrestaShopDatabaseException
* @throws \Exception
*/
private function getDeliveryOptionsArray(Order $order): array
{
$orderDeliveryOptions = DeliveryOptions::getFromOrder($order->getId());
$deliveryOptionsArray = [];

if ($orderDeliveryOptions) {
$defaultExportDeliveryOptions = $this->getDefaultExportDeliveryOptions($orderDeliveryOptions);
$deliveryOptionsArray[] = $defaultExportDeliveryOptions;
}

$deliveryOptionsArray[] = $orderDeliveryOptions;

return $deliveryOptionsArray;
}

/**
* @return string
*/
Expand Down Expand Up @@ -970,10 +1013,9 @@ private function logError(Exception $e): void
*/
private function updateDeliveryOptions(Order $order, array $values): AbstractDeliveryOptionsAdapter
{
$deliveryOptions = DeliveryOptionsMerger::create(
DeliveryOptions::getFromOrder($order->getId()),
new DeliveryOptionsFromFormAdapter($values)
);
$deliveryOptionsToMerge = $this->getDeliveryOptionsArray($order);
$deliveryOptionsToMerge[] = new DeliveryOptionsFromFormAdapter($values);
$deliveryOptions = DeliveryOptionsMerger::create(...$deliveryOptionsToMerge);

DeliveryOptions::save($order->getIdCart(), $deliveryOptions->toArray());
return $deliveryOptions;
Expand Down
82 changes: 82 additions & 0 deletions src/Adapter/DeliveryOptionsFromDefaultExportSettingsAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Gett\MyparcelBE\Adapter;

use Gett\MyparcelBE\Carrier\PackageTypeCalculator;
use Gett\MyparcelBE\Constant;
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter;
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\ShipmentOptionsV3Adapter;
use MyParcelNL\Sdk\src\Support\Collection;

class DeliveryOptionsFromDefaultExportSettingsAdapter extends AbstractDeliveryOptionsAdapter
{
/**
* @param \MyParcelNL\Sdk\src\Support\Collection $data
*
* @throws \Exception
*/
public function __construct(Collection $data)
{
$packageType = $this->intOrNull($data, Constant::PACKAGE_TYPE_CONFIGURATION_NAME);
$this->packageType = $packageType ? (new PackageTypeCalculator())->convertToName($packageType) : null;
$this->shipmentOptions = new ShipmentOptionsV3Adapter([
'age_check' => $this->boolOrNull($data, Constant::AGE_CHECK_CONFIGURATION_NAME),
'insurance' => $this->intOrNull($data, Constant::INSURANCE_CONFIGURATION_NAME),
'large_format' => $this->getLargeFormat($data),
'only_recipient' => $this->boolOrNull($data, Constant::ONLY_RECIPIENT_CONFIGURATION_NAME),
'return' => $this->boolOrNull($data, Constant::RETURN_PACKAGE_CONFIGURATION_NAME),
'signature' => $this->boolOrNull($data, Constant::SIGNATURE_REQUIRED_CONFIGURATION_NAME),
]);
}

/**
* @param \MyParcelNL\Sdk\src\Support\Collection $data
* @param string $name
*
* @return null|bool
*/
private function boolOrNull(Collection $data, string $name): ?bool
{
$value = $this->getValue($data, $name);

return $value ? (bool) $value : null;
}

/**
* @param \MyParcelNL\Sdk\src\Support\Collection $data
*
* @return null|bool
*/
private function getLargeFormat(Collection $data): ?bool
{
$format = $this->getValue($data, Constant::PACKAGE_FORMAT_CONFIGURATION_NAME);

return $format ? $format === Constant::PACKAGE_FORMAT_LARGE_INDEX : null;
}

/**
* @param \MyParcelNL\Sdk\src\Support\Collection $data
* @param string $name
*
* @return mixed
*/
private function getValue(Collection $data, string $name)
{
return $data->firstWhere('name', $name)['value'];
}

/**
* @param \MyParcelNL\Sdk\src\Support\Collection $data
* @param string $name
*
* @return null|int
*/
private function intOrNull(Collection $data, string $name): ?int
{
$value = $this->getValue($data, $name);

return $value ? (int) $value : null;
}
}
4 changes: 2 additions & 2 deletions src/Adapter/DeliveryOptionsFromFormAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function __construct(array $data = [])
*
* @return null|int
*/
private function getInsurance(array $data): ?int
private function getInsurance(array $data): int
{
$insurance = null;
$insurance = 0;

if (isset($data['insuranceAmount']) && $this->isEnabled($data['insurance'] ?? null)) {
$amount = str_replace('amount', '', $data['insuranceAmount']);
Expand Down
1 change: 1 addition & 0 deletions src/Constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class Constant
self::SENT_ORDER_STATE_FOR_DIGITAL_STAMPS_CONFIGURATION_NAME,
self::SHARE_CUSTOMER_EMAIL_CONFIGURATION_NAME,
];

public const CARRIER_EXCLUSIVE = [
'POSTNL' => [
'ALLOW_STANDARD_FORM' => ['BE' => true, 'NL' => true],
Expand Down
69 changes: 69 additions & 0 deletions src/DeliveryOptions/AbstractSettingsRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Gett\MyparcelBE\DeliveryOptions;

use Exception;
use Gett\MyparcelBE\Database\Table;
use Gett\MyparcelBE\Service\Concern\HasInstance;
use MyParcelNL\Sdk\src\Support\Collection;
use PrestaShop\PrestaShop\Adapter\Entity\Db;

abstract class AbstractSettingsRepository
{
use HasInstance;

/**
* @var \MyParcelNL\Sdk\src\Support\Collection
*/
protected static $data = [];

/**
* @var string
*/
protected static $table;

/**
* @return \MyParcelNL\Sdk\src\Support\Collection
* @throws \PrestaShopDatabaseException
* @throws \Exception
*/
public function get(): Collection
{
if (empty(self::$data)) {
$result = Db::getInstance()
->executeS($this->getQuery());

self::$data = new Collection($result);
}

return self::$data;
}

/**
* @return string
* @throws \Exception
*/
protected function getQuery(): string
{
$table = Table::withPrefix($this->getTable());

return <<<SQL
SELECT *
FROM $table
SQL;
}

/**
* @throws \Exception
*/
protected function getTable(): string
{
if (! static::$table) {
throw new Exception('Static property $table must be set');
}

return Table::withPrefix(static::$table);
}
}
30 changes: 30 additions & 0 deletions src/DeliveryOptions/DefaultExportSettingsRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Gett\MyparcelBE\DeliveryOptions;

use Gett\MyparcelBE\Database\Table;
use MyParcelNL\Sdk\src\Support\Collection;

class DefaultExportSettingsRepository extends AbstractSettingsRepository
{
/**
* @var string
*/
protected static $table = Table::TABLE_CARRIER_CONFIGURATION;

/**
* @param int|string $psCarrierId
*
* @return \MyParcelNL\Sdk\src\Support\Collection
* @throws \PrestaShopDatabaseException
*/
public function getByCarrier($psCarrierId): Collection
{
return $this->get()
->filter(static function (array $item) use ($psCarrierId): bool {
return $item['id_carrier'] === (string) $psCarrierId;
});
}
}
4 changes: 1 addition & 3 deletions src/Module/Carrier/Provider/DeliveryOptionsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ public function provide(int $orderId): array
$shipmentOptions = $deliveryOptions->getShipmentOptions();

if ($shipmentOptions && $shipmentOptions->getInsurance()) {
$deliveryOptionsArray['shipmentOptions']['insurance'] = [
'amount' => $shipmentOptions->getInsurance() * 100,
];
$deliveryOptionsArray['shipmentOptions']['insurance'] = $shipmentOptions->getInsurance() * 100;
}

if (isset($deliveryOptionsArray['date'])) {
Expand Down
Loading