Skip to content

Commit

Permalink
PES-316: gram support (#144)
Browse files Browse the repository at this point in the history
* PES-316: gram support

* PES-316: gram support - CR

* PES-316: gram support change log update

* PES-316: gram support change log update

Co-authored-by: Filip Jiskra <filip.jiskra@zasilkovna.cz>
Co-authored-by: packeta <57401091+packeta@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 15, 2021
1 parent e32efbb commit baee70e
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 14 deletions.
1 change: 1 addition & 0 deletions packetery/CHANGE_LOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
2.1.10 - Fixed: widget opening in order administration
- Fixed: Fix: weight units - accepting capital letters
2.1.11 - Updated: order weight editing in Packeta order administration: saving only changed values, better display of results
- Added: Product weight in grams is now supported. If the weight of the products is in grams, the module converts the weight of the shipment to kilograms before exporting it.
- Updated: pickup point update changed to carrier update, improving speed and reliability
- Fixed: no more displaying of selected pickup point on confirmation page if the order is not a pickup point order
- Updated: wider support for modified checkout templates (selector to hide widget button update),
Expand Down
4 changes: 3 additions & 1 deletion packetery/libs/Order/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Packetery\Order;

use Packetery\Weight\Converter;

class Ajax
{
/** @var OrderRepository */
Expand Down Expand Up @@ -52,7 +54,7 @@ public function actionSetWeights()
if ($weight === null || is_numeric($weight)) {
if ($weight === null) {
$order = new \Order($orderId);
$result[$orderId]['value'] = $order->getTotalWeight();
$result[$orderId]['value'] = Converter::getKilograms($order->getTotalWeight());
}
if ($weight != $storedWeightsAssoc[$orderId]) {
$this->orderRepository->updateWeight($orderId, $weight);
Expand Down
56 changes: 56 additions & 0 deletions packetery/libs/Weight/Converter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Packetery\Weight;

class Converter
{
/** @var string */
const UNIT_KILOGRAMS = 'kg';

/** @var string */
const UNIT_GRAMS = 'g';

/** @var array[] */
private static $mapping = [
self::UNIT_KILOGRAMS => [ // to kilos
self::UNIT_KILOGRAMS => 1,
self::UNIT_GRAMS => 0.001, // from grams
]
];

/**
* @param mixed $value
* @return float|null
*/
public static function getKilograms($value)
{
return self::convert($value, \Configuration::get('PS_WEIGHT_UNIT'), self::UNIT_KILOGRAMS);
}

/**
* @param mixed $value
* @param string $unit
* @param string $targetUnit
* @return float|int
*/
private static function convert($value, $unit, $targetUnit)
{
$unit = strtolower($unit);
$value = (float)$value;

if (!isset(self::$mapping[$targetUnit][$unit])) {
return null;
}

return $value * self::$mapping[$targetUnit][$unit];
}

/**
* @return bool
*/
public static function isKgConvertionSupported()
{
$value = self::getKilograms(1.0);
return $value !== null;
}
}
6 changes: 3 additions & 3 deletions packetery/packetery.api.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@

use Packetery\Exceptions\SenderGetReturnRoutingException;
use Packetery\Order\OrderRepository;
use Packetery\Weight\Converter;

include_once(dirname(__file__) . '/packetery.class.php');

class PacketeryApi
{
const API_WSDL_URL = 'https://www.zasilkovna.cz/api/soap-php-bugfix.wsdl';
const PACKET_WEIGHT_UNIT = 'kg';

/*LABELS*/
public static function downloadPdfAjax()
Expand Down Expand Up @@ -298,12 +298,12 @@ public static function createPacket($order)
'eshop' => $shop_name,
);

if (strtolower(Configuration::get('PS_WEIGHT_UNIT')) === self::PACKET_WEIGHT_UNIT) {
if (Converter::isKgConvertionSupported()) {
if ($packetery_order['weight'] !== null) {
// used saved if set
$packet_attributes['weight'] = $packetery_order['weight'];
} else {
$packet_attributes['weight'] = $order->getTotalWeight();
$packet_attributes['weight'] = Converter::getKilograms($order->getTotalWeight());
}
}

Expand Down
7 changes: 4 additions & 3 deletions packetery/packetery.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

use Packetery\Exceptions\SenderGetReturnRoutingException;
use Packetery\Weight\Converter;

require_once(dirname(__FILE__) . '../../../config/config.inc.php');
require_once(dirname(__FILE__) . '../../../classes/Cookie.php');
Expand Down Expand Up @@ -208,7 +209,7 @@ public static function loadWeightToOrders(array $orders)
foreach ($orders as $index => $order) {
if ($order['weight'] === null) {
$orderInstance = new \Order($order['id_order']);
$order['weight'] = $orderInstance->getTotalWeight();
$order['weight'] = Converter::getKilograms($orderInstance->getTotalWeight());
$orders[$index] = $order;
}
}
Expand Down Expand Up @@ -254,12 +255,12 @@ public static function collectOrdersDataForCsvExport($order_ids)
$currency = new Currency($order->id_currency);

$weight = '';
if (strtolower(Configuration::get('PS_WEIGHT_UNIT')) === PacketeryApi::PACKET_WEIGHT_UNIT) {
if (Converter::isKgConvertionSupported()) {
if ($packeteryOrder['weight'] !== null) {
// used saved if set
$weight = $packeteryOrder['weight'];
} else {
$weight = $order->getTotalWeight();
$weight = Converter::getKilograms($order->getTotalWeight());
}
}

Expand Down
7 changes: 4 additions & 3 deletions packetery/packetery.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Packetery\Payment\PaymentRepository;
use Packetery\Hooks\ActionObjectOrderUpdateBefore;
use Packetery\Carrier\CarrierTools;
use Packetery\Weight\Converter;

include_once(dirname(__file__).'/packetery.class.php');
include_once(dirname(__file__).'/packetery.api.php');
Expand Down Expand Up @@ -280,7 +281,7 @@ public function getContent()
array('content' => $this->l('Address delivery'), 'key' => 'is_ad', 'bool' => true,'center' => true),
array('content' => $this->l('Exported'), 'key' => 'exported', 'bool' => true, 'center' => true),
array('content' => $this->l('Tracking number'), 'key' => 'tracking_number', 'center' => true),
array('content' => $this->l('Weight'), 'key' => 'weight', 'center' => true),
array('content' => $this->l('Weight (kg)'), 'key' => 'weight', 'center' => true),
),
'rows' => $packetery_orders,
'url_params' => array('configure' => $this->name),
Expand Down Expand Up @@ -405,11 +406,11 @@ public function getContent()
$this->context->smarty->assign('baseuri', $base_uri);

$usedWeightUnit = strtolower(Configuration::get('PS_WEIGHT_UNIT'));
if ($usedWeightUnit !== PacketeryApi::PACKET_WEIGHT_UNIT) {
if (Converter::isKgConvertionSupported() === false) {
$messages = [
[
'text' => sprintf(
$this->l('The default weight unit for your store is: %s. When exporting packets, the module will not state its weight for the packet. If you want to export the weight of the packet, you need to set the default unit to kg.'),
$this->l('The default weight unit for your store is: %s. When exporting packets, the module will not state its weight for the packet. If you want to export the weight of the packet, you need to set the default unit to kg or g.'),
$usedWeightUnit
),
'class' => 'info',
Expand Down
4 changes: 2 additions & 2 deletions packetery/translations/cs.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
$_MODULE['<{packetery}prestashop>packetery_5ef3da0443efa6318d07751b9b680941'] = 'Doručení na adresu';
$_MODULE['<{packetery}prestashop>packetery_0001316ad0c7eb0f1a8e298fd6e5d070'] = 'Exportováno';
$_MODULE['<{packetery}prestashop>packetery_5068c162a60b5859f973f701333f45c5'] = 'Trasovací číslo';
$_MODULE['<{packetery}prestashop>packetery_8c489d0946f66d17d73f26366a4bf620'] = 'Hmotnost';
$_MODULE['<{packetery}prestashop>packetery_91721604210524b7051d99c4c8478715'] = 'Hmotnost (kg)';
$_MODULE['<{packetery}prestashop>packetery_0ace50d3fdc56a8b3ecf3532baa0990c'] = 'Výdejní místa Zásilkovny';
$_MODULE['<{packetery}prestashop>packetery_85ad5aeec9dfb9e0650de5fbfecd88da'] = 'Všechna výdejní místa (Zásilkovna + dopravci)';
$_MODULE['<{packetery}prestashop>packetery_b718adec73e04ce3ec720dd11a06a308'] = 'ID';
Expand Down Expand Up @@ -54,7 +54,7 @@
$_MODULE['<{packetery}prestashop>packetery_43e4c4c34b72c8ee0421d21983d8d8a7'] = 'Nevyexportováno! Chyba:';
$_MODULE['<{packetery}prestashop>packetery_584ad3e893aa1c232f67116fc2686325'] = 'Počet nastavených hmotností';
$_MODULE['<{packetery}prestashop>packetery_047c5943be09f5e6a0711ff242153735'] = 'Počet chyb během nastavování hmotností';
$_MODULE['<{packetery}prestashop>packetery_88a4478629eacd06dbbe1a47306a39b9'] = 'Výchozí jednotka hmotnosti pro Váš obchod je: %s. Při exportu zásilek modul nebude u zásilky uvádět její hmotnost. Pokud chcete exportovat hmotnost zásilky je potřeba nastavit výchozí jednotku na kg.';
$_MODULE['<{packetery}prestashop>packetery_91c3f498fbbc9f6961c22401cdb145bf'] = 'Výchozí jednotka hmotnosti pro Váš obchod je: %s. Při exportu zásilek modul nebude u zásilky uvádět její hmotnost. Pokud chcete exportovat hmotnost zásilky je potřeba nastavit výchozí jednotku na kg nebo g.';
$_MODULE['<{packetery}prestashop>packetery_b18ce70ed15a7a117141d6ee14f9138c'] = 'Výdejní místo bylo úspěšně změněno.';
$_MODULE['<{packetery}prestashop>packetery_f1111e5bc2fc07a2985e5273341fbd7b'] = 'Výdejní místo se nepodařilo změnit.';
$_MODULE['<{packetery}prestashop>packetery_47838d234bae5339a17c704bb605d01a'] = 'U objednávky není vybrané žádné výdejní místo. Zásilku nebude možné exportovat do Zásilkovny.';
Expand Down
4 changes: 2 additions & 2 deletions packetery/translations/sk.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
$_MODULE['<{packetery}prestashop>packetery_5ef3da0443efa6318d07751b9b680941'] = 'Doručovacia adresa';
$_MODULE['<{packetery}prestashop>packetery_0001316ad0c7eb0f1a8e298fd6e5d070'] = 'Exportované';
$_MODULE['<{packetery}prestashop>packetery_5068c162a60b5859f973f701333f45c5'] = 'Sledovacie číslo';
$_MODULE['<{packetery}prestashop>packetery_8c489d0946f66d17d73f26366a4bf620'] = 'Hmotnost';
$_MODULE['<{packetery}prestashop>packetery_91721604210524b7051d99c4c8478715'] = 'Hmotnost (kg)';
$_MODULE['<{packetery}prestashop>packetery_0ace50d3fdc56a8b3ecf3532baa0990c'] = 'Výdejní místa Zásilkovny';
$_MODULE['<{packetery}prestashop>packetery_85ad5aeec9dfb9e0650de5fbfecd88da'] = 'Všechna výdejní místa (Zásilkovna + dopravci)';
$_MODULE['<{packetery}prestashop>packetery_b718adec73e04ce3ec720dd11a06a308'] = 'ID';
Expand Down Expand Up @@ -54,7 +54,7 @@
$_MODULE['<{packetery}prestashop>packetery_43e4c4c34b72c8ee0421d21983d8d8a7'] = 'neexportované. Chyba:';
$_MODULE['<{packetery}prestashop>packetery_584ad3e893aa1c232f67116fc2686325'] = 'Počet nastavených hmotností';
$_MODULE['<{packetery}prestashop>packetery_047c5943be09f5e6a0711ff242153735'] = 'Počet chyb během nastavování hmotností';
$_MODULE['<{packetery}prestashop>packetery_88a4478629eacd06dbbe1a47306a39b9'] = 'Výchozí jednotka hmotnosti pro Váš obchod je: %s. Při exportu zásilek modul nebude u zásilky uvádět její hmotnost. Pokud chcete exportovat hmotnost zásilky je potřeba nastavit výchozí jednotku na kg.';
$_MODULE['<{packetery}prestashop>packetery_91c3f498fbbc9f6961c22401cdb145bf'] = 'Výchozí jednotka hmotnosti pro Váš obchod je: %s. Při exportu zásilek modul nebude u zásilky uvádět její hmotnost. Pokud chcete exportovat hmotnost zásilky je potřeba nastavit výchozí jednotku na kg nebo g.';
$_MODULE['<{packetery}prestashop>packetery_b18ce70ed15a7a117141d6ee14f9138c'] = 'Výdejní místo bylo úspěšně změněno.';
$_MODULE['<{packetery}prestashop>packetery_f1111e5bc2fc07a2985e5273341fbd7b'] = 'Výdejní místo se nepodařilo změnit.';
$_MODULE['<{packetery}prestashop>packetery_47838d234bae5339a17c704bb605d01a'] = 'U objednávky není vybrané žádné výdejní místo. Zásilku nebude možné exportovat do Zásilkovny.';
Expand Down

0 comments on commit baee70e

Please sign in to comment.