From 668a1ec1d6759a200715cb41b32d72491b2230af Mon Sep 17 00:00:00 2001 From: markernst97 Date: Thu, 9 Feb 2023 10:23:57 +0100 Subject: [PATCH 1/7] feat: add options for insurance to Belgium --- src/Constant.php | 144 +++++++++--------- .../Configuration/Form/CarriersForm.php | 53 +++---- src/Module/Upgrade/Upgrade1_8_2.php | 92 +++++++++++ upgrade/upgrade-1.8.2.php | 9 ++ 4 files changed, 198 insertions(+), 100 deletions(-) create mode 100644 src/Module/Upgrade/Upgrade1_8_2.php create mode 100644 upgrade/upgrade-1.8.2.php diff --git a/src/Constant.php b/src/Constant.php index bc936238..ca612d61 100755 --- a/src/Constant.php +++ b/src/Constant.php @@ -9,13 +9,13 @@ class Constant { - public const AGE_CHECK_CONFIGURATION_NAME = 'MYPARCELBE_AGE_CHECK'; - public const API_KEY_CONFIGURATION_NAME = 'MYPARCELBE_API_KEY'; + public const AGE_CHECK_CONFIGURATION_NAME = 'MYPARCELBE_AGE_CHECK'; + public const API_KEY_CONFIGURATION_NAME = 'MYPARCELBE_API_KEY'; public const API_LOGGING_CONFIGURATION_NAME = 'MYPARCELBE_API_LOGGING'; /** @deprecated use CarrierBpost::NAME */ - public const BPOST_CARRIER_NAME = CarrierBpost::NAME; - public const BPOST_CONFIGURATION_NAME = 'MYPARCELBE_BPOST'; - public const CARRIER_CONFIGURATION_FIELDS = [ + public const BPOST_CARRIER_NAME = CarrierBpost::NAME; + public const BPOST_CONFIGURATION_NAME = 'MYPARCELBE_BPOST'; + public const CARRIER_CONFIGURATION_FIELDS = [ self::CARRIER_CONFIGURATION_FIELD_CARRIER_TYPE, 'dropOffDays', self::CUTOFF_EXCEPTIONS, @@ -51,8 +51,8 @@ class Constant self::INSURANCE_CONFIGURATION_NAME, self::INSURANCE_CONFIGURATION_FROM_PRICE, self::INSURANCE_CONFIGURATION_MAX_AMOUNT, + self::INSURANCE_CONFIGURATION_MAX_AMOUNT_BE, self::INSURANCE_CONFIGURATION_MAX_AMOUNT_EU, - self::INSURANCE_CONFIGURATION_BELGIUM, self::ONLY_RECIPIENT_CONFIGURATION_NAME, 'return_' . self::PACKAGE_TYPE_CONFIGURATION_NAME, 'return_' . self::ONLY_RECIPIENT_CONFIGURATION_NAME, @@ -63,16 +63,17 @@ class Constant 'return_' . self::INSURANCE_CONFIGURATION_NAME, 'return_' . self::INSURANCE_CONFIGURATION_FROM_PRICE, 'return_' . self::INSURANCE_CONFIGURATION_MAX_AMOUNT, + 'return_' . self::INSURANCE_CONFIGURATION_MAX_AMOUNT_BE, 'return_' . self::INSURANCE_CONFIGURATION_MAX_AMOUNT_EU, 'return_label_description', ]; - public const CARRIER_CONFIGURATION_FIELD_CARRIER_TYPE = 'carrierType'; - public const CARRIER_CONFIGURATION_MAP = [ + public const CARRIER_CONFIGURATION_FIELD_CARRIER_TYPE = 'carrierType'; + public const CARRIER_CONFIGURATION_MAP = [ CarrierPostNL::class => Constant::POSTNL_CONFIGURATION_NAME, CarrierBpost::class => Constant::BPOST_CONFIGURATION_NAME, CarrierDPD::class => Constant::DPD_CONFIGURATION_NAME, ]; - public const CARRIER_EXCLUSIVE = [ + public const CARRIER_EXCLUSIVE = [ 'POSTNL' => [ 'ALLOW_STANDARD_FORM' => ['BE' => true, 'NL' => true], 'dropOffDays' => ['BE' => true, 'NL' => true], @@ -208,51 +209,52 @@ class Constant 'return_' . self::RETURN_PACKAGE_CONFIGURATION_NAME => ['BE' => false, 'NL' => false], ], ]; - public const CONCEPT_FIRST = 'MYPARCELBE_CONCEPT_FIRST'; - public const CONSIGNMENT_INIT_PARAMS_FOR_CHECKING_ONLY = [self::LABEL_CHECK_ONLY => true]; - public const CUSTOMS_CODE_CONFIGURATION_NAME = 'MYPARCELBE_CUSTOMS_CODE'; - public const CUSTOMS_FORM_CONFIGURATION_NAME = 'MYPARCELBE_CUSTOMS_FORM'; - public const CUSTOMS_FORM_CONFIGURATION_OPTION_ADD = 'Add'; - public const CUSTOMS_FORM_CONFIGURATION_OPTION_SKIP = 'Skip'; - public const CUSTOMS_ORIGIN_CONFIGURATION_NAME = 'MYPARCELBE_CUSTOMS_ORIGIN'; - public const CUTOFF_EXCEPTIONS = 'cutoff_exceptions'; - public const DEFAULT_CUSTOMS_CODE_CONFIGURATION_NAME = 'MYPARCELBE_DEFAULT_CUSTOMS_CODE'; - public const DEFAULT_CUSTOMS_ORIGIN_CONFIGURATION_NAME = 'MYPARCELBE_DEFAULT_CUSTOMS_ORIGIN'; - public const DEFAULT_CUTOFF_TIME = '17:00'; - public const DEFAULT_INSURANCE_POSSIBILITIES = [self::INSURANCE_CONFIGURATION_NONE]; - public const DELIVERED_ORDER_STATUS_CONFIGURATION_NAME = 'MYPARCELBE_DELIVERED_ORDER_STATUS'; + public const CONCEPT_FIRST = 'MYPARCELBE_CONCEPT_FIRST'; + public const CONSIGNMENT_INIT_PARAMS_FOR_CHECKING_ONLY = [self::LABEL_CHECK_ONLY => true]; + public const CUSTOMS_CODE_CONFIGURATION_NAME = 'MYPARCELBE_CUSTOMS_CODE'; + public const CUSTOMS_FORM_CONFIGURATION_NAME = 'MYPARCELBE_CUSTOMS_FORM'; + public const CUSTOMS_FORM_CONFIGURATION_OPTION_ADD = 'Add'; + public const CUSTOMS_FORM_CONFIGURATION_OPTION_SKIP = 'Skip'; + public const CUSTOMS_ORIGIN_CONFIGURATION_NAME = 'MYPARCELBE_CUSTOMS_ORIGIN'; + public const CUTOFF_EXCEPTIONS = 'cutoff_exceptions'; + public const DEFAULT_CUSTOMS_CODE_CONFIGURATION_NAME = 'MYPARCELBE_DEFAULT_CUSTOMS_CODE'; + public const DEFAULT_CUSTOMS_ORIGIN_CONFIGURATION_NAME = 'MYPARCELBE_DEFAULT_CUSTOMS_ORIGIN'; + public const DEFAULT_CUTOFF_TIME = '17:00'; + public const DEFAULT_INSURANCE_POSSIBILITIES = [self::INSURANCE_CONFIGURATION_NONE]; + public const DELIVERED_ORDER_STATUS_CONFIGURATION_NAME = 'MYPARCELBE_DELIVERED_ORDER_STATUS'; public const DELIVERY_OPTIONS_PRICE_FORMAT_CONFIGURATION_NAME = 'MYPARCELBE_DELIVERY_OPTIONS_PRICE_FORMAT'; - public const DELIVERY_OPTIONS_PRICE_FORMAT_SURCHARGE = 'surcharge'; - public const DELIVERY_OPTIONS_PRICE_FORMAT_TOTAL_PRICE = 'total_price'; + public const DELIVERY_OPTIONS_PRICE_FORMAT_SURCHARGE = 'surcharge'; + public const DELIVERY_OPTIONS_PRICE_FORMAT_TOTAL_PRICE = 'total_price'; /** @deprecated use CarrierDPD::NAME */ - public const DPD_CARRIER_NAME = CarrierDPD::NAME; - public const DPD_CONFIGURATION_NAME = 'MYPARCELBE_DPD'; - public const EXCLUSIVE_FIELDS_NL = [ + public const DPD_CARRIER_NAME = CarrierDPD::NAME; + public const DPD_CONFIGURATION_NAME = 'MYPARCELBE_DPD'; + public const EXCLUSIVE_FIELDS_NL = [ self::SENT_ORDER_STATE_FOR_DIGITAL_STAMPS_CONFIGURATION_NAME, self::SHARE_CUSTOMER_EMAIL_CONFIGURATION_NAME, ]; public const IGNORE_ORDER_STATUS_CONFIGURATION_NAME = 'MYPARCELBE_IGNORE_ORDER_STATUS'; - public const INSURANCE_CONFIGURATION_BELGIUM = 'MYPARCELBE_INSURANCE_BELGIUM'; + public const INSURANCE_CONFIGURATION_BELGIUM = 'MYPARCELBE_INSURANCE_BELGIUM'; public const INSURANCE_CONFIGURATION_BELGIUM_AMOUNT = 500; - public const INSURANCE_CONFIGURATION_FROM_PRICE = 'MYPARCELBE_INSURANCE_FROM_PRICE'; - public const INSURANCE_CONFIGURATION_MAX_AMOUNT = 'MYPARCELBE_INSURANCE_MAX_AMOUNT'; - public const INSURANCE_CONFIGURATION_MAX_AMOUNT_EU = 'MYPARCELBE_INSURANCE_MAX_AMOUNT_EU'; // Kg - public const INSURANCE_CONFIGURATION_NAME = 'MYPARCELBE_INSURANCE'; - public const INSURANCE_CONFIGURATION_NONE = 0; + public const INSURANCE_CONFIGURATION_FROM_PRICE = 'MYPARCELBE_INSURANCE_FROM_PRICE'; + public const INSURANCE_CONFIGURATION_MAX_AMOUNT = 'MYPARCELBE_INSURANCE_MAX_AMOUNT'; + public const INSURANCE_CONFIGURATION_MAX_AMOUNT_BE = 'MYPARCELBE_INSURANCE_MAX_AMOUNT_BE'; + public const INSURANCE_CONFIGURATION_MAX_AMOUNT_EU = 'MYPARCELBE_INSURANCE_MAX_AMOUNT_EU'; // Kg + public const INSURANCE_CONFIGURATION_NAME = 'MYPARCELBE_INSURANCE'; + public const INSURANCE_CONFIGURATION_NONE = 0; /** * Maximum characters length of item description. */ - public const ITEM_DESCRIPTION_MAX_LENGTH = 50; - public const LABEL_CHECK_ONLY = 'labelCheckOnly'; + public const ITEM_DESCRIPTION_MAX_LENGTH = 50; + public const LABEL_CHECK_ONLY = 'labelCheckOnly'; public const LABEL_CREATED_ORDER_STATUS_CONFIGURATION_NAME = 'MYPARCELBE_LABEL_CREATED_ORDER_STATUS'; - public const LABEL_DESCRIPTION_CONFIGURATION_NAME = 'MYPARCELBE_LABEL_DESCRIPTION'; - public const LABEL_OPEN_DOWNLOAD_CONFIGURATION_NAME = 'MYPARCELBE_LABEL_OPEN_DOWNLOAD'; - public const LABEL_POSITION_CONFIGURATION_NAME = 'MYPARCELBE_LABEL_POSITION'; - public const LABEL_PROMPT_POSITION_CONFIGURATION_NAME = 'MYPARCELBE_LABEL_PROMPT_POSITION'; + public const LABEL_DESCRIPTION_CONFIGURATION_NAME = 'MYPARCELBE_LABEL_DESCRIPTION'; + public const LABEL_OPEN_DOWNLOAD_CONFIGURATION_NAME = 'MYPARCELBE_LABEL_OPEN_DOWNLOAD'; + public const LABEL_POSITION_CONFIGURATION_NAME = 'MYPARCELBE_LABEL_POSITION'; + public const LABEL_PROMPT_POSITION_CONFIGURATION_NAME = 'MYPARCELBE_LABEL_PROMPT_POSITION'; public const LABEL_SCANNED_ORDER_STATUS_CONFIGURATION_NAME = 'MYPARCELBE_LABEL_SCANNED_ORDER_STATUS'; - public const LABEL_SIZE_CONFIGURATION_NAME = 'MYPARCELBE_LABEL_SIZE'; - public const MAIL_FALLBACK_LANGUAGE = 'en'; - public const NL_DAYS = [ + public const LABEL_SIZE_CONFIGURATION_NAME = 'MYPARCELBE_LABEL_SIZE'; + public const MAIL_FALLBACK_LANGUAGE = 'en'; + public const NL_DAYS = [ 1 => 'maandag', 2 => 'dinsdag', 3 => 'woensdag', @@ -261,7 +263,7 @@ class Constant 6 => 'zaterdag', 0 => 'zondag', ]; - public const NL_MONTHS = [ + public const NL_MONTHS = [ 1 => 'januari', 2 => 'februari', 3 => 'maart', @@ -275,32 +277,32 @@ class Constant 11 => 'november', 12 => 'december', ]; - public const ONLY_RECIPIENT_CONFIGURATION_NAME = 'MYPARCELBE_RECIPIENT_ONLY'; - public const ORDER_DESCRIPTION_MAX_LENGTH = 45; - public const ORDER_NOTIFICATION_AFTER_CONFIGURATION_NAME = 'MYPARCELBE_ORDER_NOTIFICATION_AFTER'; - public const PACKAGE_FORMAT_AUTOMATIC = 3; - public const PACKAGE_FORMAT_CONFIGURATION_NAME = 'MYPARCELBE_PACKAGE_FORMAT'; + public const ONLY_RECIPIENT_CONFIGURATION_NAME = 'MYPARCELBE_RECIPIENT_ONLY'; + public const ORDER_DESCRIPTION_MAX_LENGTH = 45; + public const ORDER_NOTIFICATION_AFTER_CONFIGURATION_NAME = 'MYPARCELBE_ORDER_NOTIFICATION_AFTER'; + public const PACKAGE_FORMAT_AUTOMATIC = 3; + public const PACKAGE_FORMAT_CONFIGURATION_NAME = 'MYPARCELBE_PACKAGE_FORMAT'; // Field used in _carrier_configuration to link myparcel carrier to prestashop carrier. - public const PACKAGE_FORMAT_LARGE = 2; - public const PACKAGE_FORMAT_LARGE_INDEX = 2; - public const PACKAGE_FORMAT_NORMAL = 1; - public const PACKAGE_TYPES = [ + public const PACKAGE_FORMAT_LARGE = 2; + public const PACKAGE_FORMAT_LARGE_INDEX = 2; + public const PACKAGE_FORMAT_NORMAL = 1; + public const PACKAGE_TYPES = [ AbstractConsignment::PACKAGE_TYPE_PACKAGE => AbstractConsignment::PACKAGE_TYPE_PACKAGE_NAME, AbstractConsignment::PACKAGE_TYPE_MAILBOX => AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME, AbstractConsignment::PACKAGE_TYPE_LETTER => AbstractConsignment::PACKAGE_TYPE_LETTER_NAME, AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP => AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP_NAME, ]; public const PACKAGE_TYPE_CONFIGURATION_NAME = 'MYPARCELBE_PACKAGE_TYPE'; - public const PACKAGE_TYPE_DIGITAL_STAMP = AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP; - public const PACKAGE_TYPE_LETTER = AbstractConsignment::PACKAGE_TYPE_LETTER; - public const PACKAGE_TYPE_MAILBOX = AbstractConsignment::PACKAGE_TYPE_MAILBOX; - public const PACKAGE_TYPE_PACKAGE = AbstractConsignment::PACKAGE_TYPE_PACKAGE; - public const PACKAGE_TYPE_WEIGHT_LIMIT = 2; + public const PACKAGE_TYPE_DIGITAL_STAMP = AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP; + public const PACKAGE_TYPE_LETTER = AbstractConsignment::PACKAGE_TYPE_LETTER; + public const PACKAGE_TYPE_MAILBOX = AbstractConsignment::PACKAGE_TYPE_MAILBOX; + public const PACKAGE_TYPE_PACKAGE = AbstractConsignment::PACKAGE_TYPE_PACKAGE; + public const PACKAGE_TYPE_WEIGHT_LIMIT = 2; /** @deprecated use CarrierPostNL::NAME */ - public const POSTNL_CARRIER_NAME = CarrierPostNL::NAME; - public const POSTNL_CONFIGURATION_NAME = 'MYPARCELBE_POSTNL'; - public const REQUIRED_LABEL_KEYS = [ + public const POSTNL_CARRIER_NAME = CarrierPostNL::NAME; + public const POSTNL_CONFIGURATION_NAME = 'MYPARCELBE_POSTNL'; + public const REQUIRED_LABEL_KEYS = [ 'person', 'iso_code', 'full_street', @@ -312,12 +314,12 @@ class Constant 'id_country', 'reference', ]; - public const RETURN_PACKAGE_CONFIGURATION_NAME = 'MYPARCELBE_RETURN_PACKAGE'; + public const RETURN_PACKAGE_CONFIGURATION_NAME = 'MYPARCELBE_RETURN_PACKAGE'; public const SENT_ORDER_STATE_FOR_DIGITAL_STAMPS_CONFIGURATION_NAME = 'MYPARCELBE_SENT_ORDER_STATE_FOR_DIGITAL_STAMPS'; - public const SHARE_CUSTOMER_EMAIL_CONFIGURATION_NAME = 'MYPARCELBE_SHARE_CUSTOMER_EMAIL'; - public const SHARE_CUSTOMER_PHONE_CONFIGURATION_NAME = 'MYPARCELBE_SHARE_CUSTOMER_PHONE'; - public const SIGNATURE_REQUIRED_CONFIGURATION_NAME = 'MYPARCELBE_SIGNATURE_REQUIRED'; - public const SINGLE_LABEL_CREATION_OPTIONS = [ + public const SHARE_CUSTOMER_EMAIL_CONFIGURATION_NAME = 'MYPARCELBE_SHARE_CUSTOMER_EMAIL'; + public const SHARE_CUSTOMER_PHONE_CONFIGURATION_NAME = 'MYPARCELBE_SHARE_CUSTOMER_PHONE'; + public const SIGNATURE_REQUIRED_CONFIGURATION_NAME = 'MYPARCELBE_SIGNATURE_REQUIRED'; + public const SINGLE_LABEL_CREATION_OPTIONS = [ 'packageType' => self::PACKAGE_TYPE_CONFIGURATION_NAME, 'packageFormat' => self::PACKAGE_FORMAT_CONFIGURATION_NAME, 'onlyRecipient' => self::ONLY_RECIPIENT_CONFIGURATION_NAME, @@ -326,7 +328,7 @@ class Constant 'signatureRequired' => self::SIGNATURE_REQUIRED_CONFIGURATION_NAME, 'insurance' => self::INSURANCE_CONFIGURATION_NAME, ]; - public const SINGLE_LABEL_RETURN_OPTIONS = [ + public const SINGLE_LABEL_RETURN_OPTIONS = [ 'packageType' => 'return_' . self::PACKAGE_TYPE_CONFIGURATION_NAME, 'packageFormat' => 'return_' . self::PACKAGE_FORMAT_CONFIGURATION_NAME, 'onlyRecipient' => 'return_' . self::ONLY_RECIPIENT_CONFIGURATION_NAME, @@ -336,11 +338,11 @@ class Constant 'insurance' => 'return_' . self::INSURANCE_CONFIGURATION_NAME, 'labelDescription' => 'return_label_description', ]; - public const STATUS_CHANGE_MAIL_CONFIGURATION_NAME = 'MYPARCELBE_STATUS_CHANGE_MAIL'; - public const USE_ADDRESS2_AS_STREET_NUMBER_CONFIGURATION_NAME = 'MYPARCELBE_USE_ADDRESS2_AS_STREET_NUMBER'; - public const WEBHOOK_HASH_CONFIGURATION_NAME = 'MYPARCELBE_WEBHOOK_HASH'; - public const WEBHOOK_ID_CONFIGURATION_NAME = 'MYPARCELBE_WEBHOOK_ID'; - public const WEEK_DAYS = [ + public const STATUS_CHANGE_MAIL_CONFIGURATION_NAME = 'MYPARCELBE_STATUS_CHANGE_MAIL'; + public const USE_ADDRESS2_AS_STREET_NUMBER_CONFIGURATION_NAME = 'MYPARCELBE_USE_ADDRESS2_AS_STREET_NUMBER'; + public const WEBHOOK_HASH_CONFIGURATION_NAME = 'MYPARCELBE_WEBHOOK_HASH'; + public const WEBHOOK_ID_CONFIGURATION_NAME = 'MYPARCELBE_WEBHOOK_ID'; + public const WEEK_DAYS = [ 1 => 'monday', 2 => 'tuesday', 3 => 'wednesday', diff --git a/src/Module/Configuration/Form/CarriersForm.php b/src/Module/Configuration/Form/CarriersForm.php index e2f7afde..4505f6ec 100644 --- a/src/Module/Configuration/Form/CarriersForm.php +++ b/src/Module/Configuration/Form/CarriersForm.php @@ -502,6 +502,27 @@ static function ($value) use ($currency) { 'class' => 'col-lg-2', ]; + $fields[] = [ + 'tab' => $tabId, + 'type' => 'select', + 'label' => $this->module->l('Max insured amount for Belgium', 'carriers'), + 'name' => $prefix . Constant::INSURANCE_CONFIGURATION_MAX_AMOUNT_BE, + 'options' => [ + 'query' => array_map( + static function ($value) use ($currency) { + return [ + 'value' => $value, + 'label' => $currency->getSign() . ' ' . $value, + ]; + }, + $insurancePossibilities + ), + 'id' => 'value', + 'name' => 'label', + ], + 'class' => 'col-lg-2', + ]; + $insurancePossibilitiesEU = $this->getInsurancePossibilities($myParcelCarrier, 'EU'); $fields[] = [ 'tab' => $tabId, @@ -523,32 +544,6 @@ static function ($value) use ($currency) { ], 'class' => 'col-lg-2', ]; - - if (! $prefix && $this->module->isNL()) { - $fields[] = [ - 'tab' => $tabId, - 'type' => 'switch', - 'is_bool' => true, - 'values' => [ - [ - 'id' => $prefix . Constant::INSURANCE_CONFIGURATION_BELGIUM . '_on', - 'value' => 1, - 'label' => $this->module->l('Yes', 'carriers'), - ], - [ - 'id' => $prefix . Constant::INSURANCE_CONFIGURATION_BELGIUM . '_off', - 'value' => 0, - 'label' => $this->module->l('No', 'carriers'), - ], - ], - 'label' => $this->module->l('Insure towards Belgium', 'carriers'), - 'name' => $prefix . Constant::INSURANCE_CONFIGURATION_BELGIUM, - 'desc' => $this->module->l( - 'When this setting is "on", packages from NL to BE will be insured for a maximum of € 500.', - 'carriers' - ), - ]; - } } return $fields; @@ -1281,9 +1276,9 @@ private function getList() $helper->table = 'carrier'; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->module->name . '&menu=' . Tools::getValue( - 'menu', - 0 - ); + 'menu', + 0 + ); $helper->colorOnBackground = true; $helper->no_link = true; diff --git a/src/Module/Upgrade/Upgrade1_8_2.php b/src/Module/Upgrade/Upgrade1_8_2.php new file mode 100644 index 00000000..f86cf9c6 --- /dev/null +++ b/src/Module/Upgrade/Upgrade1_8_2.php @@ -0,0 +1,92 @@ +addInsuranceOptionsBeForCarriers(); + } + + /** + * Adds insurance_from_price and insurance_max_amount to carrier configuration if not already present + * + * @throws \PrestaShopDatabaseException + */ + private function addInsuranceOptionsBeForCarriers(): void + { + $this->carrierConfigurationTable = Table::withPrefix(Table::TABLE_CARRIER_CONFIGURATION); + $configName = Constant::INSURANCE_CONFIGURATION_NAME; + + $query = <<carrierConfigurationTable WHERE name LIKE '%$configName' +SQL; + $records = new Collection($this->db->executeS($query)); + + foreach ($records as $record) { + $carrierId = (int) $record['id_carrier']; + + $this->insertOption($carrierId, Constant::INSURANCE_CONFIGURATION_MAX_AMOUNT_BE, '0'); + $this->insertOption($carrierId, 'return_' . Constant::INSURANCE_CONFIGURATION_MAX_AMOUNT_BE, '0'); + } + } + + /** + * @param int $carrierId + * @param string $optionName + * @param string $optionValue + * + * @return bool + */ + private function addOption(int $carrierId, string $optionName, string $optionValue): bool + { + $query = <<carrierConfigurationTable (id_carrier, name, value) VALUES ($carrierId, '$optionName', '$optionValue'); +SQL; + return $this->db->execute($query); + } + + /** + * Inserts option with $optionValue for carrier if it does not exist yet. Leaves the current option value otherwise. + * + * @param int $carrierId + * @param string $optionName + * @param string $optionValue + * + * @return void + * @throws \PrestaShopDatabaseException + */ + private function insertOption(int $carrierId, string $optionName, string $optionValue): void + { + if (! $this->optionExists($carrierId, $optionName)) { + $this->addOption($carrierId, $optionName, $optionValue); + } + } + + /** + * @param int $carrierId + * @param string $optionName + * + * @return bool + * @throws \PrestaShopDatabaseException + */ + private function optionExists(int $carrierId, string $optionName): bool + { + $query = <<carrierConfigurationTable WHERE id_carrier = $carrierId AND name = '$optionName' +SQL; + return (new Collection($this->db->executeS($query)))->isNotEmpty(); + } +} diff --git a/upgrade/upgrade-1.8.2.php b/upgrade/upgrade-1.8.2.php new file mode 100644 index 00000000..3c13a7ce --- /dev/null +++ b/upgrade/upgrade-1.8.2.php @@ -0,0 +1,9 @@ +upgrade(Gett\MyparcelBE\Module\Upgrade\Upgrade1_8_::class); +} From a6103ccd2ffefec7bfecfa52dd06d46da423afb7 Mon Sep 17 00:00:00 2001 From: markernst97 Date: Thu, 9 Feb 2023 10:24:20 +0100 Subject: [PATCH 2/7] feat: add options for insurance to Belgium --- src/Label/LabelOptionsResolver.php | 128 ++++++++++++++++------------- 1 file changed, 69 insertions(+), 59 deletions(-) diff --git a/src/Label/LabelOptionsResolver.php b/src/Label/LabelOptionsResolver.php index c926c638..d58da89a 100644 --- a/src/Label/LabelOptionsResolver.php +++ b/src/Label/LabelOptionsResolver.php @@ -14,6 +14,7 @@ use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter; use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment; use MyParcelNL\Sdk\src\Support\Arr; +use Throwable; class LabelOptionsResolver { @@ -29,41 +30,8 @@ class LabelOptionsResolver ]; /** - * @param \Gett\MyparcelBE\Model\Core\Order $order - * - * @return false|string - * @throws \PrestaShopDatabaseException - * @throws \Exception - */ - public function getLabelOptionsJson(Order $order) - { - return json_encode($this->getLabelOptions($order)); - } - - /** - * @param \Gett\MyparcelBE\Model\Core\Order $order - * - * @return array - * @throws \PrestaShopDatabaseException - * @throws \Exception - */ - public function getLabelOptions(Order $order): array - { - $deliveryOptions = OrderSettingsFactory::create($order) - ->getDeliveryOptions(); - - return array_merge( - $this->getShipmentOptions($deliveryOptions, $order->getProducts(), $order->getIdCarrier()), - [ - 'package_type' => $this->getPackageType($order, $deliveryOptions), - 'package_format' => $this->getPackageFormat($order, $deliveryOptions), - ] - ); - } - - /** - * @param \Gett\MyparcelBE\Model\Core\Order $order - * @param \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter|null $deliveryOptions + * @param \Gett\MyparcelBE\Model\Core\Order $order + * @param \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter|null $deliveryOptions * * @return array * @throws \PrestaShopDatabaseException @@ -96,9 +64,9 @@ public function getDeliveryOptions(Order $order, ?AbstractDeliveryOptionsAdapter } /** - * @param \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter $deliveryOptions - * @param int $packageType - * @param \Gett\MyparcelBE\Model\Core\Order $order + * @param \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter $deliveryOptions + * @param int $packageType + * @param \Gett\MyparcelBE\Model\Core\Order $order * * @return int the amount in euro for which the package should be insured */ @@ -107,25 +75,30 @@ public function getInsurance(AbstractDeliveryOptionsAdapter $deliveryOptions, in $psCarrierId = $order->getIdCarrier(); try { - $fromPrice = CarrierConfigurationProvider::get($psCarrierId, Constant::INSURANCE_CONFIGURATION_FROM_PRICE); - $maxAmount = CarrierConfigurationProvider::get($psCarrierId, Constant::INSURANCE_CONFIGURATION_MAX_AMOUNT); + $fromPrice = CarrierConfigurationProvider::get( + $psCarrierId, + Constant::INSURANCE_CONFIGURATION_FROM_PRICE + ); + $maxAmount = CarrierConfigurationProvider::get( + $psCarrierId, + Constant::INSURANCE_CONFIGURATION_MAX_AMOUNT + ); $consignment = (new ConsignmentFactory(Constant::CONSIGNMENT_INIT_PARAMS_FOR_CHECKING_ONLY)) ->fromOrder( $order, - OrderSettingsFactory::create($order)->getDeliveryOptions() + OrderSettingsFactory::create($order) + ->getDeliveryOptions() ) ->first(); $consignment->setPackageType($packageType); - } catch (\Throwable $e) { + } catch (Throwable $e) { return Constant::INSURANCE_CONFIGURATION_NONE; } if (CountryService::isPostNLShipmentFromNLToBE($consignment)) { try { - if (CarrierConfigurationProvider::get($psCarrierId, Constant::INSURANCE_CONFIGURATION_BELGIUM)) { - return Constant::INSURANCE_CONFIGURATION_BELGIUM_AMOUNT; - } - } catch (\Throwable $e) { + return CarrierConfigurationProvider::get($psCarrierId, Constant::INSURANCE_CONFIGURATION_MAX_AMOUNT_BE); + } catch (Throwable $e) { return Constant::INSURANCE_CONFIGURATION_NONE; } } @@ -136,12 +109,16 @@ public function getInsurance(AbstractDeliveryOptionsAdapter $deliveryOptions, in $grandTotal = $order->getTotalProductsWithTaxes(); - if ($grandTotal < $fromPrice || ! $consignment->canHaveShipmentOption(AbstractConsignment::SHIPMENT_OPTION_INSURANCE)) { + if ($grandTotal < $fromPrice + || ! $consignment->canHaveShipmentOption( + AbstractConsignment::SHIPMENT_OPTION_INSURANCE + )) { return Constant::INSURANCE_CONFIGURATION_NONE; } if ($deliveryOptions->getShipmentOptions()) { - $insuredAmount = $deliveryOptions->getShipmentOptions()->getInsurance(); + $insuredAmount = $deliveryOptions->getShipmentOptions() + ->getInsurance(); } $insuredAmount = (int) min($insuredAmount ?? $grandTotal, $maxAmount ?? Constant::INSURANCE_CONFIGURATION_NONE); @@ -150,21 +127,36 @@ public function getInsurance(AbstractDeliveryOptionsAdapter $deliveryOptions, in } /** - * @param int $threshold - * @param array $allowedValues this must be an indexed array with values sorted from low to high + * @param \Gett\MyparcelBE\Model\Core\Order $order * - * @return int lowest allowed value that is higher than or equal to threshold, or the highest allowed value + * @return array + * @throws \PrestaShopDatabaseException + * @throws \Exception */ - private function getHighestAllowedValue(int $threshold, array $allowedValues): int + public function getLabelOptions(Order $order): array { - foreach ($allowedValues as $allowedValue) { - if ($allowedValue < $threshold) { - continue; - } - return $allowedValue; - } + $deliveryOptions = OrderSettingsFactory::create($order) + ->getDeliveryOptions(); - return Arr::last($allowedValues); + return array_merge( + $this->getShipmentOptions($deliveryOptions, $order->getProducts(), $order->getIdCarrier()), + [ + 'package_type' => $this->getPackageType($order, $deliveryOptions), + 'package_format' => $this->getPackageFormat($order, $deliveryOptions), + ] + ); + } + + /** + * @param \Gett\MyparcelBE\Model\Core\Order $order + * + * @return false|string + * @throws \PrestaShopDatabaseException + * @throws \Exception + */ + public function getLabelOptionsJson(Order $order) + { + return json_encode($this->getLabelOptions($order)); } /** @@ -186,6 +178,24 @@ private function anyProductHasSetting(array $products, string $setting): bool return (bool) $product; } + /** + * @param int $threshold + * @param array $allowedValues this must be an indexed array with values sorted from low to high + * + * @return int lowest allowed value that is higher than or equal to threshold, or the highest allowed value + */ + private function getHighestAllowedValue(int $threshold, array $allowedValues): int + { + foreach ($allowedValues as $allowedValue) { + if ($allowedValue < $threshold) { + continue; + } + return $allowedValue; + } + + return Arr::last($allowedValues); + } + /** * @param \Gett\MyparcelBE\Model\Core\Order $order * @param null|\MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter $deliveryOptions From 9931fb593a84bc83074e58d340e8db34c0813365 Mon Sep 17 00:00:00 2001 From: Mark Ernst <33826576+Mark-Ernst@users.noreply.github.com> Date: Mon, 13 Feb 2023 09:14:34 +0100 Subject: [PATCH 3/7] Update upgrade/upgrade-1.8.2.php Co-authored-by: Joeri van Veen --- upgrade/upgrade-1.8.2.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/upgrade/upgrade-1.8.2.php b/upgrade/upgrade-1.8.2.php index 3c13a7ce..00ea3d84 100644 --- a/upgrade/upgrade-1.8.2.php +++ b/upgrade/upgrade-1.8.2.php @@ -5,5 +5,5 @@ */ function upgrade_module_1_8_2(MyParcelBE $module): bool { - return $module->upgrade(Gett\MyparcelBE\Module\Upgrade\Upgrade1_8_::class); + return $module->upgrade(Gett\MyparcelBE\Module\Upgrade\Upgrade1_8_2::class); } From 4c44613bdf1df7b9bf78821d544bb9f84c0ad5c8 Mon Sep 17 00:00:00 2001 From: markernst97 Date: Mon, 13 Feb 2023 11:50:31 +0100 Subject: [PATCH 4/7] feat: add options for insurance to Belgium --- .../Configuration/Form/CarriersForm.php | 42 ++++++++++--------- src/Module/Upgrade/Upgrade1_8_2.php | 4 ++ 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/Module/Configuration/Form/CarriersForm.php b/src/Module/Configuration/Form/CarriersForm.php index 4505f6ec..bc0f3316 100644 --- a/src/Module/Configuration/Form/CarriersForm.php +++ b/src/Module/Configuration/Form/CarriersForm.php @@ -502,26 +502,28 @@ static function ($value) use ($currency) { 'class' => 'col-lg-2', ]; - $fields[] = [ - 'tab' => $tabId, - 'type' => 'select', - 'label' => $this->module->l('Max insured amount for Belgium', 'carriers'), - 'name' => $prefix . Constant::INSURANCE_CONFIGURATION_MAX_AMOUNT_BE, - 'options' => [ - 'query' => array_map( - static function ($value) use ($currency) { - return [ - 'value' => $value, - 'label' => $currency->getSign() . ' ' . $value, - ]; - }, - $insurancePossibilities - ), - 'id' => 'value', - 'name' => 'label', - ], - 'class' => 'col-lg-2', - ]; + if ($this->module->isNL() && CarrierPostNL::NAME === $myParcelCarrier->getName()) { + $fields[] = [ + 'tab' => $tabId, + 'type' => 'select', + 'label' => $this->module->l('Max insured amount for Belgium', 'carriers'), + 'name' => $prefix . Constant::INSURANCE_CONFIGURATION_MAX_AMOUNT_BE, + 'options' => [ + 'query' => array_map( + static function ($value) use ($currency) { + return [ + 'value' => $value, + 'label' => $currency->getSign() . ' ' . $value, + ]; + }, + $insurancePossibilities + ), + 'id' => 'value', + 'name' => 'label', + ], + 'class' => 'col-lg-2', + ]; + } $insurancePossibilitiesEU = $this->getInsurancePossibilities($myParcelCarrier, 'EU'); $fields[] = [ diff --git a/src/Module/Upgrade/Upgrade1_8_2.php b/src/Module/Upgrade/Upgrade1_8_2.php index f86cf9c6..d7a925b9 100644 --- a/src/Module/Upgrade/Upgrade1_8_2.php +++ b/src/Module/Upgrade/Upgrade1_8_2.php @@ -17,6 +17,10 @@ class Upgrade1_8_2 extends AbstractUpgrade */ public function upgrade(): void { + if ($this->module->isBE()) { + return; + } + $this->addInsuranceOptionsBeForCarriers(); } From 3c3987889825eb60be460cf24ed7fd7a81781828 Mon Sep 17 00:00:00 2001 From: joerivanveen Date: Thu, 2 Mar 2023 17:29:24 +0100 Subject: [PATCH 5/7] fix: get the insurance possibilities from the sdk consignment --- src/Module/Hooks/AdminPanelRenderService.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Module/Hooks/AdminPanelRenderService.php b/src/Module/Hooks/AdminPanelRenderService.php index 3f80aa7e..2f8d85ae 100644 --- a/src/Module/Hooks/AdminPanelRenderService.php +++ b/src/Module/Hooks/AdminPanelRenderService.php @@ -220,12 +220,6 @@ protected function getConsignmentOptions( return []; } - if (CountryService::isPostNLShipmentFromNLToBE($consignment)) { - return $consignmentOptions + [ - 'insuranceOptions' => [Constant::INSURANCE_CONFIGURATION_BELGIUM_AMOUNT], - ]; - } - $isStandardDelivery = $consignment->getDeliveryType() === AbstractConsignment::DELIVERY_TYPE_STANDARD; $consignmentOptions += [ 'insuranceOptions' => $consignment->getInsurancePossibilities($consignment->country), From 671584f03cf913feca1736b96e17f748b7533c6f Mon Sep 17 00:00:00 2001 From: joerivanveen Date: Mon, 6 Mar 2023 10:12:20 +0100 Subject: [PATCH 6/7] fix: execute the migration when module is nl --- src/Module/Upgrade/Upgrade1_8_2.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Module/Upgrade/Upgrade1_8_2.php b/src/Module/Upgrade/Upgrade1_8_2.php index d7a925b9..c836737b 100644 --- a/src/Module/Upgrade/Upgrade1_8_2.php +++ b/src/Module/Upgrade/Upgrade1_8_2.php @@ -6,6 +6,7 @@ use Gett\MyparcelBE\Constant; use Gett\MyparcelBE\Database\Table; +use MyParcelBE; use MyParcelNL\Sdk\src\Support\Collection; class Upgrade1_8_2 extends AbstractUpgrade @@ -17,7 +18,7 @@ class Upgrade1_8_2 extends AbstractUpgrade */ public function upgrade(): void { - if ($this->module->isBE()) { + if (MyParcelBE::getModule()->isBE()) { return; } From c10fdcac92a59c6be15cefa1d77c60ebb0a0fc01 Mon Sep 17 00:00:00 2001 From: joerivanveen Date: Mon, 6 Mar 2023 11:06:46 +0100 Subject: [PATCH 7/7] fix: require current sdk for correct insurance possibilities --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index a3e449e4..9757c700 100755 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "require": { "php": ">=7.2.0", "myparcelnl/pdk": "~1.34.0", - "myparcelnl/sdk": "~7.6.1" + "myparcelnl/sdk": "~7.7.2" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.0" @@ -27,4 +27,4 @@ "config": { "prepend-autoloader": false } -} \ No newline at end of file +}