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 273 home delivery #43

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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 CHANGE_LOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
x.x.x - Added: home delivery support
- Updated: external pickup points are no longer available with internal pickup points

1.4.0 - Added: manual trigger for carrier list update via Packeta API
- Added: carrier v4 import

Expand Down
1 change: 1 addition & 0 deletions install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CREATE TABLE IF NOT EXISTS `#__virtuemart_shipment_plg_zasilkovna` (
`virtuemart_order_id` int(11) unsigned,
`virtuemart_shipmentmethod_id` mediumint(1) unsigned,
`order_number` char(32),
`shipping_method` varchar(255),
`zasilkovna_packet_id` decimal(10,0),
`zasilkovna_packet_price` decimal(15,2),
`weight` decimal(10,4),
Expand Down
38 changes: 38 additions & 0 deletions install.zasilkovna.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,45 @@ public function postflight($route, JAdapterInstance $adapter) {
if ($route === 'update' && $this->fromVersion && version_compare($this->fromVersion, '1.2.0', '<')) {
$this->migratePricingRules();
}

$this->fixShipmentMethods();
IlIIlIlIl1 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Adds packeteryCarrierId param for VM3 shipment methods related to Packeta.
*/
private function fixShipmentMethods() {
/** @var \VirtueMartModelZasilkovna $model */
$model = VmModel::getModel('zasilkovna');
$methodIds = $model->getShipmentMethodIds();

foreach ($methodIds as $methodId) {
$params = $model->getShipmentMethodTransformedParams($methodId);

if (array_key_exists(\VirtueMartModelZasilkovna\ShipmentMethod::CARRIER_ID, $params)) {
continue;
}

$zasMethod = $model->getPacketeryShipmentMethod($methodId);
$fieldList = new JFormFieldVmZasilkovnaCarriers();
$options = $fieldList->getOptionsForPacketeryShipmentMethod($zasMethod);
$firstCarrierId = $fieldList->getFirstCarrierId($options);

if (empty($options) || $firstCarrierId !== \VirtueMartModelZasilkovna\Carrier\Repository::FORM_FIELD_PACKETA_PICKUP_POINTS) {
$model->updateShipmentMethodParams($methodId, [
\VirtueMartModelZasilkovna\ShipmentMethod::CARRIER_ID => ''
]);
$model->publishShipmentMethods([$methodId], 0);
echo 'Unable to attach carrier to shipment method ' . $methodId . '. Shipment method was unpublished.';
continue;
}

$model->updateShipmentMethodParams($methodId, [
\VirtueMartModelZasilkovna\ShipmentMethod::CARRIER_ID => $firstCarrierId
]);
}
}

/**
* @param string $tableLike
* @return bool
Expand Down Expand Up @@ -477,6 +514,7 @@ private function removeAdministratorFiles() {
recurse_delete($vm_admin_path . DS . 'views' . DS . 'zasilkovna' . DS);
recurse_delete($vm_admin_path . DS . 'controllers' . DS . 'zasilkovna.php');
recurse_delete($vm_admin_path . DS . 'fields' . DS . 'vmzasilkovnacountries.php');
recurse_delete($vm_admin_path . DS . 'fields' . DS . 'vmzasilkovnacarriers.php');
recurse_delete(JPATH_ADMINISTRATOR . DS . 'language' . DS . 'en-GB' . DS . 'en-GB.plg_vmshipment_zasilkovna.ini');
recurse_delete(JPATH_ADMINISTRATOR . DS . 'language' . DS . 'cs-CZ' . DS . 'cs-CZ.plg_vmshipment_zasilkovna.ini');
recurse_delete(JPATH_ADMINISTRATOR . DS . 'language' . DS . 'sk-SK' . DS . 'sk-SK.plg_vmshipment_zasilkovna.ini', true);
Expand Down
86 changes: 86 additions & 0 deletions media/admin/com_virtuemart/fields/vmzasilkovnacarriers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
defined('_JEXEC') or die();

JFormHelper::loadFieldClass('list');
jimport('joomla.form.formfield');

class JFormFieldVmZasilkovnaCarriers extends JFormFieldList {

/** @var string */
protected $type = 'vmZasilkovnaCarriers';

/** @var \VirtueMartModelZasilkovna\Carrier\Repository */
private $carrierRepository;

/** @var \VirtueMartModelZasilkovna */
private $model;

public function __construct($form = null) {
parent::__construct($form);
$this->carrierRepository = new \VirtueMartModelZasilkovna\Carrier\Repository();
$this->model = \VmModel::getModel('zasilkovna');
}

/**
* @param array $options
* @return string
*/
public function getFirstCarrierId(array $options) {
$carrierOptionsKeys = array_keys($options);
$carrierOptionsKey = array_shift($carrierOptionsKeys);
return (string) $carrierOptionsKey;
}

/**
* @param int $shipmentMethodId
* @param bool $includePrompt
* @return array
*/
public function getOptionsForShipment($shipmentMethodId, $includePrompt = false) {
$zasMethod = $this->model->getPacketeryShipmentMethod($shipmentMethodId);
return $this->getOptionsForPacketeryShipmentMethod($zasMethod, $includePrompt);
}

/**
* @param \VirtueMartModelZasilkovna\ShipmentMethod $zasMethod
* @param bool $includePrompt
* @return array
*/
public function getOptionsForPacketeryShipmentMethod(\VirtueMartModelZasilkovna\ShipmentMethod $zasMethod, $includePrompt = false) {
$allowedCountryCodes = $this->model->getAllowedCountryCodes($zasMethod);
$blockedCountryCodes = $this->model->getBlockedCountryCodes($zasMethod);
$carriers = $this->carrierRepository->getAllActiveCarriers($allowedCountryCodes, $blockedCountryCodes);
IlIIlIlIl1 marked this conversation as resolved.
Show resolved Hide resolved

$fields = [];

if ($includePrompt) {
$fields[] = JHtml::_('select.option', '', JText::_('PLG_VMSHIPMENT_PACKETERY_SELECT_CARRIER'));
}

if ($this->model->hasPacketaPickupPointCountryCode($allowedCountryCodes, $blockedCountryCodes)) {
$fields[\VirtueMartModelZasilkovna\Carrier\Repository::FORM_FIELD_PACKETA_PICKUP_POINTS] = JHtml::_('select.option', \VirtueMartModelZasilkovna\Carrier\Repository::FORM_FIELD_PACKETA_PICKUP_POINTS, JText::_('PLG_VMSHIPMENT_PACKETERY_PICKUP_POINT_OPTION_LABEL'));
}

foreach ($carriers as $carrier) {
$fields[$carrier['id']] = JHtml::_('select.option', $carrier['id'], $carrier['name']);
}

return $fields;
}

/**
* @return array
*/
protected function getOptions() {
IlIIlIlIl1 marked this conversation as resolved.
Show resolved Hide resolved
$shipmentMethodId = null;
$input = JFactory::getApplication()->input;
if ($input->getString('view') === 'shipmentmethod') {
$shipmentIdArray = $input->get('cid', null, 'array');
if ($shipmentIdArray && count($shipmentIdArray) === 1) {
$shipmentMethodId = (int) array_pop($shipmentIdArray);
}
}

return $this->getOptionsForShipment($shipmentMethodId, true);
}
}
119 changes: 119 additions & 0 deletions media/admin/com_virtuemart/models/zasilkovna.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,125 @@ public function publishShipmentMethods($ids, $value = 1)
$db->execute();
}

/**
* @param $shipmentMethodId
* @return \VirtueMartModelZasilkovna\ShipmentMethod
*/
public function getPacketeryShipmentMethod($shipmentMethodId) {
$model = \VmModel::getModel('shipmentmethod');
$shipment = $model->getShipment($shipmentMethodId);
return \VirtueMartModelZasilkovna\ShipmentMethod::fromRandom($shipment);
}

/**
* @param \VirtueMartModelZasilkovna\ShipmentMethod $zasMethod
* @return array
*/
public function getAllowedCountryCodes(\VirtueMartModelZasilkovna\ShipmentMethod $zasMethod) {
$allowedCountryIds = $zasMethod->getAllowedCountries();

$allowedCountryCodes = [];
if ($allowedCountryIds) {
foreach ($allowedCountryIds as $allowedCountryId) {
$allowedCountryCodes[] = \VirtueMartModelCountry::getCountryFieldByID($allowedCountryId, 'country_2_code');
}
}

return $allowedCountryCodes;
}

/**
* @param \VirtueMartModelZasilkovna\ShipmentMethod $zasMethod
* @return array
*/
public function getBlockedCountryCodes(\VirtueMartModelZasilkovna\ShipmentMethod $zasMethod) {
$blockingCountries = $zasMethod->getBlockingCountries();

$blockedCountryCodes = [];
if ($blockingCountries) {
foreach ($blockingCountries as $allowedCountryId) {
$blockedCountryCodes[] = \VirtueMartModelCountry::getCountryFieldByID($allowedCountryId, 'country_2_code');
}
}

return $blockedCountryCodes;
}

/**
* @param array $allowedCountryCodes
* @param array $blockedCountryCodes
* @return bool
*/
public function hasPacketaPickupPointCountryCode(array $allowedCountryCodes, array $blockedCountryCodes) {
return $this->hasShipmentCountryCodes(['CZ', 'SK', 'RO', 'HU'], $allowedCountryCodes, $blockedCountryCodes);
}

/**
* @param array $countryCodesToTest
* @param array $allowedCountryCodes
* @param array $blockedCountryCodes
* @return bool
*/
public function hasShipmentCountryCodes(array $countryCodesToTest, array $allowedCountryCodes, array $blockedCountryCodes) {
$baseCountriesDiff = array_diff($countryCodesToTest, $blockedCountryCodes);
$packetaCountries = array_intersect($baseCountriesDiff, $allowedCountryCodes);

if (!empty($packetaCountries) || (empty($allowedCountryCodes) && !empty($baseCountriesDiff))) {
return true;
}

return false;
}

/**
* @param int $methodId
* @return array
*/
public function getShipmentMethodTransformedParams($methodId) {
$db = JFactory::getDBO();
$db->setQuery(
"SELECT `shipment_params` FROM `#__virtuemart_shipmentmethods` WHERE `virtuemart_shipmentmethod_id` = ".(int)$methodId
);
$params = $db->loadResult();
$paramsExploded = explode('|', $params);
$paramsTransformed = [];

foreach ($paramsExploded as $item) {
list($key, $value) = explode('=', $item); // may include "

if (!$key) {
continue;
}

$paramsTransformed[$key] = json_decode($value, true);
}

return $paramsTransformed;
}

/**
* @param int $methodId
* @param array $newParams
* @return void
*/
public function updateShipmentMethodParams($methodId, array $newParams) {
$paramsTransformed = $this->getShipmentMethodTransformedParams($methodId);
$newParams = array_merge_recursive($newParams, $paramsTransformed);

$newParamsTransformed = [];
foreach ($newParams as $newParamKey => $newParam) {
$newParamsTransformed[] = $newParamKey . '=' . json_encode($newParam);
}

$newParamsTransformedImploded = implode('|', $newParamsTransformed);

$db = JFactory::getDBO();
$db->setQuery(
"UPDATE #__virtuemart_shipmentmethods SET shipment_params='" . $db->escape($newParamsTransformedImploded) . "' WHERE virtuemart_shipmentmethod_id=" . (int)$methodId
);
$db->execute();
}

/**
* @param $currency_id
* @return mixed
Expand Down
44 changes: 23 additions & 21 deletions media/admin/com_virtuemart/models/zasilkovna_orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class VirtueMartModelZasilkovna_orders extends VmModel
/** @var VirtueMartModelZasilkovna */
private $zas_model;

/** @var \VirtueMartModelZasilkovna\Carrier\Repository */
private $carrierRepository;

/**
* VirtueMartModelZasilkovna_orders constructor.
Expand All @@ -38,6 +40,7 @@ function __construct() {
$this->zas_model = VmModel::getModel('zasilkovna');
$this->setMainTable('orders');
$this->addvalidOrderingFieldName(array('order_name', 'payment_method', 'virtuemart_order_id'));
$this->carrierRepository = new \VirtueMartModelZasilkovna\Carrier\Repository();
}

public function printLabels($orders_id_arr, $format = 'A7 on A4', $offset = '0') {
Expand Down Expand Up @@ -116,6 +119,13 @@ public function submitToZasilkovna($orders_id_arr) {
$attributes['carrierPickupPoint'] = $order['carrier_point'];
}

if ($order['shipping_method'] === \VirtueMartModelZasilkovna\ShippingMethods::HOME_DELIVERY) {
$attributes['street'] = $order['recipient_street'];
$attributes['houseNumber'] = $order['recipient_house_number'];
$attributes['city'] = $order['recipient_city'];
$attributes['zip'] = $order['recipient_zip'];
}

$packet = $gw->createPacket($apiPassword, $attributes);
$q = "UPDATE " . $this->zas_model->getDbTableName() . " SET zasilkovna_packet_id=" . (int)$packet->id . " WHERE order_number = '" . $db->escape($order['order_number']) . "'; ";
$db->setQuery($q);
Expand Down Expand Up @@ -343,7 +353,7 @@ protected function prepareForExport($orders_arr)
}

$ordersForINStatement = implode("','", $orderNumbers);
$q = "SELECT o.order_number,curr.currency_code_3 order_currency_name,
$q = "SELECT o.order_number,plg.shipping_method,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,
Expand All @@ -361,22 +371,6 @@ protected function prepareForExport($orders_arr)
foreach($rows as $key => $row) {
$orderForExport = array();

$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)
{
Expand All @@ -398,6 +392,7 @@ protected function prepareForExport($orders_arr)
*/

$orderForExport['order_number'] = $row['order_number'];
$orderForExport['shipping_method'] = ($row['shipping_method'] ?: \VirtueMartModelZasilkovna\ShippingMethods::PICKUP_POINT_DELIVERY);
$orderForExport['recipient_firstname'] = $row['first_name'];
$orderForExport['recipient_lastname'] = $row['last_name'];
$orderForExport['recipient_company'] = "";
Expand All @@ -409,16 +404,23 @@ protected function prepareForExport($orders_arr)
$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'] = null;
$orderForExport['recipient_house_number'] = null;
$orderForExport['recipient_city'] = null;
$orderForExport['recipient_zip'] = null;
$orderForExport['carrier_point'] = $row['carrier_pickup_point'];
$orderForExport['width'] = "";
$orderForExport['height'] = "";
$orderForExport['depth'] = "";
$orderForExport['zasilkovna_packet_id'] = $row['zasilkovna_packet_id'];

if ($row['shipping_method'] === \VirtueMartModelZasilkovna\ShippingMethods::HOME_DELIVERY) {
$orderForExport['recipient_street'] = $row['address'];
$orderForExport['recipient_house_number'] = null;
$orderForExport['recipient_city'] = $row["city"];
$orderForExport['recipient_zip'] = $row['zip_code'];
}

$ordersForExport[] = $orderForExport;
}

Expand Down
Loading