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

PES-1251, PES-2068, PES-2080 Default weight and packet dimensions #78

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ CREATE TABLE IF NOT EXISTS `#__virtuemart_shipment_plg_zasilkovna` (
`zasilkovna_packet_id` decimal(10,0),
`zasilkovna_packet_price` decimal(15,2),
`weight` decimal(10,4),
`length` smallint unsigned,
`width` smallint unsigned,
`height` smallint unsigned,
`branch_id` decimal(10,0),
`branch_currency` char(5),
`branch_name_street` varchar(500),
Expand Down
13 changes: 10 additions & 3 deletions install.zasilkovna.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Joomla\CMS\Installer\Adapter\PluginAdapter;
use VirtueMartModelZasilkovna\ConfigConstants;

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
Expand Down Expand Up @@ -203,7 +204,7 @@ public function postflight($route, $adapter)
require_once VMPATH_ROOT . '/plugins/vmshipment/zasilkovna/zasilkovna.php';
}

$this->createCronToken();
$this->createConfigurationDefaults();
if ($route === 'update' && $this->fromVersion && version_compare($this->fromVersion, '1.2.0', '<')) {
$this->migratePricingRules();
}
Expand Down Expand Up @@ -592,11 +593,11 @@ private function removeAdministratorFiles() {
}

/**
* Creates update carriers token.
* Creates update carriers token and other plugin configuration defaults.
*
* @return void
*/
private function createCronToken() {
private function createConfigurationDefaults(): void {
/** @var \VirtueMartModelZasilkovna $model */
$model = VmModel::getModel('zasilkovna');
$config = $model->loadConfig();
Expand All @@ -605,6 +606,12 @@ private function createCronToken() {
$config['cron_token'] = substr(sha1(rand()), 0, 16);
}

foreach (ConfigConstants::CONFIG_DEFAULTS as $key => $value) {
if (!array_key_exists($key, $config)) {
$config[$key] = $value;
}
}

$model->updateConfig($config);
}

Expand Down
64 changes: 51 additions & 13 deletions media/admin/com_virtuemart/controllers/zasilkovna.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,18 @@ public function updateCarriers()

/**
* Handle the save task.
* @param int $data
* @param mixed $data
* @throws Exception
*/
public function save($data = 0)
public function save($data = 0): void
{
vRequest::vmCheckToken();
$data = vRequest::getPost();
$message = null;

/** @var VirtueMartModelZasilkovna $model */
$model = VmModel::getModel('zasilkovna');
$currentData = $model->loadConfig();

if (strlen($data['zasilkovna_api_pass']) !== 32) {
$message = new FlashMessage(JText::_('PLG_VMSHIPMENT_PACKETERY_API_PASS_INVALID'), FlashMessage::TYPE_ERROR);
} else {
$model->updateConfig(array_replace_recursive($currentData, $data));
if ($data === 0) {
Mrakor marked this conversation as resolved.
Show resolved Hide resolved
$data = vRequest::getPost();
}

$message = $this->updateZasilkovnaConfig($data);

$redir = 'index.php?option=com_virtuemart';
$app = JFactory::getApplication();
if ($app->input->getString('task') === 'apply') {
Expand Down Expand Up @@ -295,4 +288,49 @@ private function getExportOrders()
{
return isset($_POST['exportOrders']) && is_array($_POST['exportOrders']) ? $_POST['exportOrders'] : [];
}

/**
* @param array<string, mixed> $data
* @return FlashMessage
*/
private function updateZasilkovnaConfig(array $data): FlashMessage
{
$configStorage = new VirtueMartModelZasilkovna\ConfigSessionStorage(JFactory::getSession(), 'packeteryConfig');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

namespace by měl být uvnitř třídy ConfigSessionStorage - je součástí zapouzdření ukládací logiky

a pojmenuj $configSessionStorage, ať je níže jasnější, kam ukládáme a kde mažeme (dtto ve view.html.php)


/** @var VirtueMartModelZasilkovna $model */
$model = VmModel::getModel('zasilkovna');
$currentData = $model->loadConfig();

$formValidator = new VirtueMartModelZasilkovna\ConfigurationValidator($data);
$formValidator->validate();

if (!$formValidator->isValid()) {
$configStorage->write($data);
$errors = $formValidator->getErrors();
$messages = [];

//$error is either error string translation key, or array where 1st element is sprintf template and others are sprintf arguments (all untranslated)
foreach ($errors as $formField => $error) {
if (!is_array($error)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proč je tohle lepší než volat JText::_() už ve validátoru a vracet jen stringy?
Pokud to jde, tak vracej rovnou hotové stringy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Za mě v tom, že takhle ten náš nový krásný, čistý validátor nemá žádnou závislost na joomlých věcech a tady v tom kontroleru už ta závislost je.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obecně souhlasím, že nemít závislosti na joomlích věcech je super, ale ne za cenu toho, že bychom pak měli takovýhle kód.

$messages[] = JText::_($error);
} else {
$messages[] = sprintf(
JText::_($error[0]),
...array_map(
static function ($item) {
return JText::_($item);
},
array_slice($error, 1))
);
}
}

return new FlashMessage(implode("<br>", $messages), FlashMessage::TYPE_ERROR);
}

$model->updateConfig(array_replace_recursive($currentData, $formValidator->normalize()));
$configStorage->flush();

return new FlashMessage(JText::_('PLG_VMSHIPMENT_PACKETERY_CONFIG_SAVED'), FlashMessage::TYPE_MESSAGE);
}
}
8 changes: 5 additions & 3 deletions media/admin/com_virtuemart/models/zasilkovna.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @link http://www.zasilkovna.cz
*/

use VirtueMartModelZasilkovna\ConfigConstants;

defined('_JEXEC') or die('Restricted access');

if(!class_exists('VmModel')) require(VMPATH_ADMIN . DS . 'helpers' . DS . 'vmmodel.php');
Expand All @@ -16,7 +18,7 @@
*/
class VirtueMartModelZasilkovna extends VmModel
{
const VERSION = '1.4.0';
const VERSION = '2.0.0';
Mrakor marked this conversation as resolved.
Show resolved Hide resolved
const PLG_NAME = 'zasilkovna';

const MAX_WEIGHT_DEFAULT = 5;
Expand Down Expand Up @@ -58,8 +60,8 @@ public function __construct()

$this->config = $this->loadConfig();

$this->api_pass = isset($this->config['zasilkovna_api_pass']) ? $this->config['zasilkovna_api_pass'] : '';
$this->api_key = isset($this->config['zasilkovna_api_pass']) ? substr($this->config['zasilkovna_api_pass'], 0, 16) : '';
$this->api_pass = $this->config[ConfigConstants::KEY_API_PASS] ?? '';
$this->api_key = isset($this->config[ConfigConstants::KEY_API_PASS]) ? substr($this->config[ConfigConstants::KEY_API_PASS], 0, 16) : '';
$this->_media_url = JURI::root(true) . "/media/com_zasilkovna/media/";
$this->_media_path = JPATH_SITE . DS . "media" . DS . "com_zasilkovna" . DS . "media" . DS;

Expand Down
81 changes: 57 additions & 24 deletions media/admin/com_virtuemart/models/zasilkovna_orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

if(!class_exists('VmModel')) require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'vmmodel.php');

use VirtueMartModelZasilkovna\ConfigConstants;
use VirtueMartModelZasilkovna\Label;

/**
Expand Down Expand Up @@ -158,7 +159,13 @@ public function printCarrierLabels(array $packetIds, Label\Format $format, $offs
exit;
}

public function submitToZasilkovna($orders_id_arr) {
/**
* @param string[] $orders_id_arr
* @return array<string,array<string,mixed>>
* @throws Exception
*/
public function submitToZasilkovna(array $orders_id_arr): array
{

$db = JFactory::getDBO();
$gw = new SoapClient(VirtueMartModelZasilkovna::PACKETA_WSDL);
Expand All @@ -185,10 +192,20 @@ public function submitToZasilkovna($orders_id_arr) {
'value' => $order['value'],
'weight' => $order['weight'],
'currency' => $order['currency'],
'eshop' => $sender_label = $this->zas_model->getConfig('zasilkovna_eshop_label'),
'eshop' => $sender_label = $this->zas_model->getConfig(ConfigConstants::KEY_ESHOP_LABEL),
'adultContent' => (int)$order['adult_content'] === 1,
);

$dimensions = ['length', 'width', 'height'];
$size = [];
foreach ($dimensions as $dimension) {
$size[$dimension] = $order[$dimension];
}

if ($order['length']) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tahle podmínka patří nad celé sestavování $size

$attributes['size'] = $size;
}

if (!empty($order['carrier_point'])) {
$attributes['carrierPickupPoint'] = $order['carrier_point'];
}
Expand Down Expand Up @@ -307,7 +324,7 @@ public function exportToCSV($orders_id_arr) {
}

$exportedOrders = array();
$sender_label = $this->zas_model->getConfig('zasilkovna_eshop_label');
$sender_label = $this->zas_model->getConfig(ConfigConstants::KEY_ESHOP_LABEL);

echo '"verze 5"'.PHP_EOL.PHP_EOL;

Expand All @@ -334,9 +351,9 @@ public function exportToCSV($orders_id_arr) {
$row['recipient_city'],
$row['recipient_zip'],
$row['carrier_point'],
$row['width'],
$row['height'],
$row['depth'],
$row['width'] ?? '',
$row['height'] ?? '',
$row['length'] ?? '',
);

echo "," . implode(',', $order) . PHP_EOL;
Expand Down Expand Up @@ -430,11 +447,16 @@ private function createConvertInstance() {
}
}

protected function prepareForExport($orders_arr)
/**
* @param string[] $orders_arr
* @return array<array<string,mixed>>
* @throws Exception
*/
protected function prepareForExport(array $orders_arr): array
{
if (!$orders_arr)
{
return;
return [];
}

$db = JFactory::getDBO();
Expand All @@ -444,16 +466,28 @@ protected function prepareForExport($orders_arr)
}

$ordersForINStatement = implode("','", $orderNumbers);
$q = "SELECT o.order_number,curr.currency_code_3 order_currency_name,
plg.zasilkovna_packet_price order_total,oi.first_name,oi.last_name,
oi_bt.email,IFNULL(oi.phone_1, oi_bt.phone_1) as phone_1,IFNULL(oi.phone_2, oi_bt.phone_2) as phone_2,plg.packet_cod,
plg.branch_id,plg.zasilkovna_packet_id, plg.carrier_pickup_point, plg.is_carrier,
plg.address as address, plg.adult_content AS adult_content, plg.city, plg.zip_code, plg.branch_currency, plg.weight FROM #__virtuemart_orders o ";
$q .= "INNER JOIN #__virtuemart_order_userinfos oi ON o.virtuemart_order_id=oi.virtuemart_order_id AND oi.address_type = IF(o.STsameAsBT = 1, 'BT', 'ST') ";
$q .= "INNER JOIN #__virtuemart_order_userinfos oi_bt ON o.virtuemart_order_id=oi_bt.virtuemart_order_id AND oi_bt.address_type = 'BT' ";
$q .= "INNER JOIN " . $this->zas_model->getDbTableName() . " plg ON plg.order_number=o.order_number ";
$q .= "LEFT JOIN #__virtuemart_currencies curr ON curr.virtuemart_currency_id=o.order_currency ";
$q .= " WHERE o.order_number IN ('" . $ordersForINStatement . "') GROUP BY o.order_number";

$q = sprintf(
"SELECT o.order_number, curr.currency_code_3 order_currency_name,
plg.zasilkovna_packet_price order_total, oi.first_name, oi.last_name,
oi_bt.email, IFNULL(oi.phone_1, oi_bt.phone_1) AS phone_1, IFNULL(oi.phone_2, oi_bt.phone_2) AS phone_2,
plg.packet_cod, plg.branch_id, plg.zasilkovna_packet_id, plg.carrier_pickup_point, plg.is_carrier,
plg.address AS address, plg.adult_content AS adult_content, plg.city, plg.zip_code, plg.branch_currency,
plg.weight, plg.width, plg.length, plg.height
FROM #__virtuemart_orders o
INNER JOIN #__virtuemart_order_userinfos oi
ON o.virtuemart_order_id = oi.virtuemart_order_id AND oi.address_type = IF(o.STsameAsBT = 1, 'BT', 'ST')
INNER JOIN #__virtuemart_order_userinfos oi_bt
ON o.virtuemart_order_id = oi_bt.virtuemart_order_id AND oi_bt.address_type = 'BT'
INNER JOIN %s plg ON plg.order_number = o.order_number
LEFT JOIN #__virtuemart_currencies curr ON curr.virtuemart_currency_id = o.order_currency
WHERE o.order_number IN ('%s')
GROUP BY o.order_number
",
$this->zas_model->getDbTableName(),
$ordersForINStatement
);

$db->setQuery($q);
$rows = $db->loadAssocList();

Expand All @@ -477,7 +511,6 @@ protected function prepareForExport($orders_arr)
$street = $streetMatches[1];
}


$phone = "";
foreach (array('phone_2', 'phone_1') as $field)
{
Expand All @@ -488,8 +521,8 @@ protected function prepareForExport($orders_arr)
}
}

$orderForExport['order_number'] = $row['order_number'];
$orderForExport['recipient_firstname'] = $row['first_name'];
$orderForExport['order_number'] = $row['order_number'];
$orderForExport['recipient_firstname'] = $row['first_name'];
$orderForExport['recipient_lastname'] = $row['last_name'];
$orderForExport['recipient_company'] = "";
$orderForExport['recipient_email'] = $row['email'];
Expand All @@ -506,9 +539,9 @@ protected function prepareForExport($orders_arr)
$orderForExport['recipient_zip'] = $row['zip_code'];
$orderForExport['carrier_point'] = $row['carrier_pickup_point'];
$orderForExport['is_carrier'] = $row['is_carrier'];
$orderForExport['width'] = "";
$orderForExport['height'] = "";
$orderForExport['depth'] = "";
$orderForExport['width'] = $row['width'];
$orderForExport['height'] = $row['height'];
$orderForExport['length'] = $row['length'];
$orderForExport['zasilkovna_packet_id'] = $row['zasilkovna_packet_id'];

$ordersForExport[] = $orderForExport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,7 @@ public function getActiveHdCarriersForPublishedCountries()
return $db->loadAssocList();
}

/**
* @param int $carrierId
* @return null|\stdClass
*/
public function getCarrierById($carrierId)
public function getCarrierById(int $carrierId): ?\stdClass
{
$db = \JFactory::getDBO();
$db->setQuery(
Expand All @@ -114,6 +110,7 @@ public function getCarrierById($carrierId)
vzc.name,
vzc.country,
vzc.deleted,
vzc.requires_size,
vc.virtuemart_country_id AS vm_country
FROM #__virtuemart_zasilkovna_carriers vzc
LEFT JOIN #__virtuemart_countries vc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace VirtueMartModelZasilkovna;

class ConfigConstants
{
public const KEY_API_PASS = 'zasilkovna_api_pass';
public const KEY_ESHOP_LABEL = 'zasilkovna_eshop_label';
public const KEY_USE_DEFAULT_WEIGHT = 'zasilkovna_use_default_weight';
public const KEY_DEFAULT_WEIGHT = 'zasilkovna_default_weight';
public const KEY_USE_DEFAULT_DIMENSIONS = 'zasilkovna_use_default_dimensions';
public const KEY_DEFAULT_LENGTH = 'zasilkovna_default_length';
public const KEY_DEFAULT_WIDTH = 'zasilkovna_default_width';
public const KEY_DEFAULT_HEIGHT = 'zasilkovna_default_height';
public const KEY_PAYMENT_METHOD_PREFIX = 'zasilkovna_payment_method_';

public const CONFIG_DEFAULTS = [
self::KEY_USE_DEFAULT_WEIGHT => false,
self::KEY_DEFAULT_WEIGHT => '',
self::KEY_USE_DEFAULT_DIMENSIONS => false,
self::KEY_DEFAULT_LENGTH => '',
self::KEY_DEFAULT_WIDTH => '',
self::KEY_DEFAULT_HEIGHT => '',
self::KEY_API_PASS => '',
self::KEY_ESHOP_LABEL => '',
];
}
Loading