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-2174, PES-2187 Plugin table reafactor #89

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions CHANGE_LOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Added: possibility to submit packet and print label from order detail
- Added: Configurable auto-submit of the packets data after change of order status
- Added: possibility to choose external pick-up point carrier
- Updated: refactored Packeta plugin table and handling of recipient's data

1.3.1 - Fixed: SQL input escaping added in places where SQL is constructed
- Updated: Schema migration simplified by using GenericTableUpdater
Expand Down
7 changes: 0 additions & 7 deletions install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ CREATE TABLE IF NOT EXISTS `#__virtuemart_shipment_plg_zasilkovna` (
`branch_name_street` varchar(500),
`is_carrier` smallint(1) NOT NULL DEFAULT '0',
`carrier_pickup_point` varchar(40),
`email` varchar(255),
`phone` varchar(255),
`first_name` varchar(255),
`last_name` varchar(255),
`address` varchar(255),
`city` varchar(255),
`zip_code` varchar(255),
`virtuemart_country_id` varchar(255),
`adult_content` smallint(1) DEFAULT '0',
`is_cod` smallint(1),
Expand Down
20 changes: 19 additions & 1 deletion install.zasilkovna.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function preflight($route, $adapter) {
recurse_delete($media_path);

$this->removeAdministratorFiles();
$this->dropObsoleteTableColumns();
}
}

Expand Down Expand Up @@ -634,5 +635,22 @@ private function removeBackendMenuEntries()
$db->setQuery($query);
$db->execute();
}
}

private function dropObsoleteTableColumns()
{
if ($this->fromVersion && version_compare($this->fromVersion, '1.3.1', '>')) {
$columnsToDrop = ['first_name', 'last_name', 'address', 'city', 'zip', 'email', 'phone'];

$db = JFactory::getDBO();
$db->setQuery('SHOW COLUMNS FROM #__virtuemart_shipment_plg_zasilkovna');
$columns = $db->loadColumn(0);

foreach ($columnsToDrop as $column) {
if (in_array($column, $columns, true)) {
$db->setQuery('ALTER TABLE `#__virtuemart_shipment_plg_zasilkovna` DROP COLUMN ' . $db->quoteName($column));
$db->execute();
}
}
}
}
}
162 changes: 66 additions & 96 deletions media/admin/com_virtuemart/models/zasilkovna_orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
if(!class_exists('VmModel')) require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'vmmodel.php');

use VirtueMartModelZasilkovna\Label;
use VirtueMartModelZasilkovna\Order\AddressProvider;

/**
* Class VirtueMartModelZasilkovna_orders
Expand Down Expand Up @@ -454,11 +455,14 @@ private function createConvertInstance() {
}
}

/**
* @param string[] $orders_arr
* @return array<string|string>
*/
protected function prepareForExport($orders_arr)
{
if (!$orders_arr)
{
return;
if (!$orders_arr) {
return [];
}

$db = JFactory::getDBO();
Expand All @@ -467,25 +471,32 @@ protected function prepareForExport($orders_arr)
$orderNumbers[] = $db->escape($orderNumber);
}

$ordersForINStatement = implode("','", $orderNumbers);
$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.zasilkovna_packet_price AS order_total,
IFNULL(ui.email, ui_bt.email) AS email,
IFNULL(ui.first_name, ui_bt.first_name) AS first_name,
IFNULL(ui.last_name, ui_bt.last_name) AS last_name,
IFNULL(ui.phone_1, ui_bt.phone_1) AS phone_1,
IFNULL(ui.phone_2, ui_bt.phone_2) AS phone_2,
IFNULL(ui.address_1, ui_bt.address_1) AS address_1,
IFNULL(ui.address_2, ui_bt.address_2) AS address_2,
IFNULL(ui.city, ui_bt.city) AS city,
IFNULL(ui.zip, ui_bt.zip) AS zip,
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.adult_content AS adult_content, 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'
LEFT JOIN #__virtuemart_order_userinfos ui
ON o.virtuemart_order_id = ui.virtuemart_order_id AND ui.address_type = 'ST'
LEFT JOIN #__virtuemart_order_userinfos ui_bt
ON o.virtuemart_order_id = ui_bt.virtuemart_order_id AND ui_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
implode("','", $orderNumbers)
);

$db->setQuery($q);
Expand All @@ -495,49 +506,24 @@ protected function prepareForExport($orders_arr)
$ordersForExport = array();
foreach($rows as $key => $row) {
$orderForExport = array();
$address = AddressProvider::fromUserInfoArray($row);

$streetMatches = array();

$match = preg_match('/^(.*[^0-9]+) (([1-9][0-9]*)\/)?([1-9][0-9]*[a-cA-C]?)$/', $row['address'], $streetMatches);

if (!$match) {
$houseNumber = null;
$street = $row['address'];
} elseif (!isset($streetMatches[4])) {
$houseNumber = null;
$street = $streetMatches[1];
} else {
$houseNumber = (!empty($streetMatches[3])) ? $streetMatches[3] . "/" . $streetMatches[4] : $streetMatches[4];
$street = $streetMatches[1];
}


$phone = "";
foreach (array('phone_2', 'phone_1') as $field)
{
$phone_n = $this->normalizePhone($row[$field]);
if (NULL !== $phone_n)
{
$phone = $phone_n;
}
}

$orderForExport['order_number'] = $row['order_number'];
$orderForExport['recipient_firstname'] = $row['first_name'];
$orderForExport['recipient_lastname'] = $row['last_name'];
$orderForExport['order_number'] = $row['order_number'];
$orderForExport['recipient_firstname'] = $address->getFirstName();
$orderForExport['recipient_lastname'] = $address->getLastName();
$orderForExport['recipient_company'] = "";
$orderForExport['recipient_email'] = $row['email'];
$orderForExport['recipient_phone'] = $phone;
$orderForExport['recipient_email'] = $address->getEmail();
$orderForExport['recipient_phone'] = $address->getNormalizedPhone();
$orderForExport['packet_cod'] = $row['packet_cod'];
$orderForExport['currency'] = $row["order_currency_name"];
$orderForExport['value'] = $row['order_total'];
$orderForExport['weight'] = $row['weight'];
$orderForExport['point_id'] = $row['branch_id'];
$orderForExport['adult_content'] = $row['adult_content'];
$orderForExport['recipient_street'] = $street;
$orderForExport['recipient_house_number'] = $houseNumber;
$orderForExport['recipient_city'] = $row["city"];
$orderForExport['recipient_zip'] = $row['zip_code'];
$orderForExport['recipient_street'] = $address->getStreet();
$orderForExport['recipient_house_number'] = $address->getHouseNumber();
$orderForExport['recipient_city'] = $address->getCity();
$orderForExport['recipient_zip'] = $address->getZip();
$orderForExport['carrier_point'] = $row['carrier_pickup_point'];
$orderForExport['is_carrier'] = $row['is_carrier'];
$orderForExport['width'] = $row['width'];
Expand Down Expand Up @@ -574,30 +560,6 @@ private function csv_escape($s) {
}
*/


/**
* Validates phone number and returns NULL if not valid
* @param string|null $value
* @return string|null
*/
private function normalizePhone($value)
{
if (is_null($value) || $value === '') {
return null;
}

$value = str_replace(' ', '', trim($value));

// only + and numbers are allowed
if (preg_match('/^\+?\d+$/', $value) !== 1)
{
$value = '';
}

return ($value ?: NULL);
}


/**
* This function gets the orderId, for anonymous users
*
Expand Down Expand Up @@ -726,11 +688,6 @@ public function getOrder($virtuemart_order_id) {
return $order;
}

public function getZasilkovnaOrdersList() {
return $this->getOrdersListByShipment(0);
}


/*
Copy _ALL_ orders to zasilkovna table
(to be able to edit and submit all orders to zasilkovna)
Expand Down Expand Up @@ -760,14 +717,23 @@ public function getOrdersListByShipment($shipment_id = 0) {
if(!$res) return array();

//$this->_noLimit = $noLimit;
$select = " o.*, CONCAT_WS(' ',u.first_name,u.middle_name,u.last_name) AS order_name "
. ',pm.payment_name AS payment_method';
$from = $this->getZasilkovnaOrdersListQuery();
$select .= ', plg.printed_label,plg.address,plg.city,plg.zip_code,plg.virtuemart_country_id, plg.branch_id,
plg.exported AS exported,plg.is_cod AS is_cod, plg.packet_cod AS packet_cod, plg.branch_currency, plg.branch_name_street AS name_street,
brnch.country as country, plg.adult_content, plg.email, u_bt.email AS billing_email,
IF(IFNULL(u.phone_1, u_bt.phone_1) <> "", IFNULL(u.phone_1, u_bt.phone_1), IFNULL(u.phone_2, u_bt.phone_2)) as phone,
plg.zasilkovna_packet_id,plg.zasilkovna_packet_price,plg.weight ';

$select = /** @lang SQL */
' o.*, pm.payment_name AS payment_method, plg.printed_label,plg.virtuemart_country_id,
plg.branch_id, plg.exported AS exported,plg.is_cod AS is_cod, plg.packet_cod AS packet_cod, plg.branch_currency,
plg.branch_name_street AS name_street, brnch.country AS country, plg.adult_content,
IFNULL(ui.email, ui_bt.email) AS email,
IFNULL(ui.first_name, ui_bt.first_name) AS first_name,
IFNULL(ui.last_name, ui_bt.last_name) AS last_name,
IFNULL(ui.phone_1, ui_bt.phone_1) AS phone_1,
IFNULL(ui.phone_2, ui_bt.phone_2) AS phone_2,
IFNULL(ui.address_1, ui_bt.address_1) AS address_1,
IFNULL(ui.address_2, ui_bt.address_2) AS address_2,
IFNULL(ui.city, ui_bt.city) AS city,
IFNULL(ui.zip, ui_bt.zip) AS zip,
plg.zasilkovna_packet_id,plg.zasilkovna_packet_price,plg.weight ';

if($shipment_id == self::ALL_ORDERS) {
//no where statement => select all
;
Expand All @@ -792,7 +758,7 @@ public function getOrdersListByShipment($shipment_id = 0) {

$search = '"%' . $this->_db->getEscaped($search, true) . '%"';

$where[] = ' ( u.first_name LIKE ' . $search . ' OR u.middle_name LIKE ' . $search . ' OR u.last_name LIKE ' . $search . ' OR `order_number` LIKE ' . $search . ')';
$where[] = ' ( ui.first_name LIKE ' . $search . ' OR ui.middle_name LIKE ' . $search . ' OR ui.last_name LIKE ' . $search . ' OR `order_number` LIKE ' . $search . ')';
}


Expand Down Expand Up @@ -820,22 +786,26 @@ public function getOrdersListByShipment($shipment_id = 0) {
}

/**
* List of tables to include for the product query
* List of tables to include for the order query
*
* @author Zasilkovna
* @return string
*/
private function getZasilkovnaOrdersListQuery() {
$db = JFactory::getDBO();

return ' FROM #__virtuemart_orders as o
LEFT JOIN #__virtuemart_order_userinfos as u
ON u.virtuemart_order_id = o.virtuemart_order_id AND u.address_type = IF(o.STsameAsBT = 1, "BT", "ST")
LEFT JOIN #__virtuemart_order_userinfos u_bt
ON u_bt.virtuemart_order_id = o.virtuemart_order_id AND u_bt.address_type = "BT"
LEFT JOIN #__virtuemart_paymentmethods_' . $db->escape(VMLANG) . ' as pm
ON o.virtuemart_paymentmethod_id = pm.virtuemart_paymentmethod_id
RIGHT JOIN ' . $this->zas_model->getDbTableName() . ' as plg ON plg.order_number=o.order_number
LEFT JOIN #__virtuemart_zasilkovna_carriers as brnch ON brnch.id=plg.branch_id';
return sprintf(
/** @lang SQL */
' FROM #__virtuemart_orders AS o
LEFT JOIN #__virtuemart_order_userinfos AS ui
ON ui.virtuemart_order_id = o.virtuemart_order_id AND ui.address_type = "ST"
LEFT JOIN #__virtuemart_order_userinfos ui_bt
ON ui_bt.virtuemart_order_id = o.virtuemart_order_id AND ui_bt.address_type = "BT"
LEFT JOIN #__virtuemart_paymentmethods_%s AS pm
ON o.virtuemart_paymentmethod_id = pm.virtuemart_paymentmethod_id
RIGHT JOIN %s AS plg ON plg.order_number=o.order_number
LEFT JOIN #__virtuemart_zasilkovna_carriers AS brnch ON brnch.id=plg.branch_id',
$db->escape(VMLANG),
$this->zas_model->getDbTableName()
);
}

/**
Expand Down
Loading