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 4 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\ConfigurationValidator;

// 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 (ConfigurationValidator::CONFIG_DEFAULTS as $key => $value) {
if (!isset($config[$key])) {
Mrakor marked this conversation as resolved.
Show resolved Hide resolved
$config[$key] = $value;
}
}

$model->updateConfig($config);
}

Expand Down
66 changes: 53 additions & 13 deletions media/admin/com_virtuemart/controllers/zasilkovna.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
class VirtuemartControllerZasilkovna extends VmController
{
const ZASILKOVNA_LIMITATIONS_REMOVED_NOTICE_DISMISSED = 'zasilkovna_limitations_removed_notice_dismissed';
public const FROM_POST = 'fromPost';
Copy link

Choose a reason for hiding this comment

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

raději než konstanty, definuj třídu ConfigSessionStorage, která dědí SessionStorage a přetěžuje její metody tak, aby ukládala do vlastního namespace s fixním id a key, takže konstanty nebudou potřeba

Copy link
Contributor Author

Choose a reason for hiding this comment

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

musel jsem to predelat, ukazalo se, ze soucasne metody prace se session budou v J5 jinak.

public const FORM_VALUES = 'formValues';

/** @var \VirtueMartModelZasilkovna\Order\Detail */
private $orderDetail;
Expand Down Expand Up @@ -70,25 +72,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 +290,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\SessionStorage(JFactory::getSession(), 'packeteryConfig');
$configStorage->set(self::FROM_POST, self::FORM_VALUES, $data);
Mrakor marked this conversation as resolved.
Show resolved Hide resolved

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

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

if (!$formValidator->isValid()) {
$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->getValidData()));
$configStorage->clear(self::FROM_POST, self::FORM_VALUES);

return new FlashMessage(JText::_('PLG_VMSHIPMENT_PACKETERY_CONFIG_SAVED'), FlashMessage::TYPE_MESSAGE);
}
}
2 changes: 1 addition & 1 deletion media/admin/com_virtuemart/models/zasilkovna.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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
76 changes: 54 additions & 22 deletions media/admin/com_virtuemart/models/zasilkovna_orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,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 Down Expand Up @@ -189,6 +195,16 @@ public function submitToZasilkovna($orders_id_arr) {
'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 @@ -334,9 +350,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 +446,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 +465,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 +510,6 @@ protected function prepareForExport($orders_arr)
$street = $streetMatches[1];
}


$phone = "";
foreach (array('phone_2', 'phone_1') as $field)
{
Expand All @@ -488,8 +520,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 +538,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
Loading