first commit
This commit is contained in:
347
modules/dpdpoland/controllers/arrange_pickup.controller.php
Normal file
347
modules/dpdpoland/controllers/arrange_pickup.controller.php
Normal file
@@ -0,0 +1,347 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandArrangePickUpController Responsible for Arrange Pickup page view and actions
|
||||
*/
|
||||
class DpdPolandArrangePickUpController extends DpdPolandController
|
||||
{
|
||||
/**
|
||||
* Current file name
|
||||
*/
|
||||
const FILENAME = 'arrange_pickup.controller';
|
||||
|
||||
/**
|
||||
* @var array Pickup data
|
||||
*/
|
||||
private $data = array();
|
||||
|
||||
/**
|
||||
* @var array Arrange Pickup fields visible in this page
|
||||
*/
|
||||
private $rules = array();
|
||||
|
||||
/**
|
||||
* DpdPolandArrangePickUpController class constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->rules = array(
|
||||
'customerCompany' => array(
|
||||
'validate' => 'isAnything',
|
||||
'fieldname' => $this->l('Customer company name'),
|
||||
'required' => true
|
||||
),
|
||||
'customerName' => array(
|
||||
'validate' => 'isAnything',
|
||||
'fieldname' => $this->l('Customer name and surname'),
|
||||
'required' => true
|
||||
),
|
||||
'customerPhone' => array(
|
||||
'validate' => 'isAnything',
|
||||
'fieldname' => $this->l('Customer tel. No.'),
|
||||
'required' => true
|
||||
),
|
||||
'pickupDate' => array(
|
||||
'validate' => 'isDate',
|
||||
'fieldname' => $this->l('Date of pickup'),
|
||||
'required' => true
|
||||
),
|
||||
'pickupTime' => array(
|
||||
'validate' => 'isAnything',
|
||||
'fieldname' => $this->l('Timeframe of pickup'),
|
||||
'required' => true
|
||||
),
|
||||
'orderType' => array(
|
||||
'validate' => 'isAnything',
|
||||
'fieldname' => $this->l('Shipment type'),
|
||||
'required' => true
|
||||
),
|
||||
'orderContent' => array(
|
||||
'validate' => 'isAnything',
|
||||
'fieldname' => $this->l('General shipment content'),
|
||||
'required' => false
|
||||
),
|
||||
'dox' => array(
|
||||
'validate' => 'isAnything',
|
||||
'fieldname' => $this->l('Envelopes'),
|
||||
'required' => false
|
||||
),
|
||||
'doxCount' => array(
|
||||
'validate' => 'isUnsignedInt',
|
||||
'fieldname' => $this->l('Number of envelopes'),
|
||||
'required' => true,
|
||||
'dependency' => 'dox'
|
||||
),
|
||||
'parcels' => array(
|
||||
'validate' => 'isUnsignedInt',
|
||||
'fieldname' => $this->l('Parcels'),
|
||||
'required' => false
|
||||
),
|
||||
'parcelsCount' => array(
|
||||
'validate' => 'isUnsignedInt',
|
||||
'fieldname' => $this->l('Number of parcels'),
|
||||
'required' => true,
|
||||
'dependency' => 'parcels'
|
||||
),
|
||||
'parcelsWeight' => array(
|
||||
'validate' => 'isUnsignedFloat',
|
||||
'fieldname' => $this->l('Summary weight'),
|
||||
'required' => true,
|
||||
'dependency' => 'parcels'
|
||||
),
|
||||
'parcelMaxWeight' => array(
|
||||
'validate' => 'isUnsignedFloat',
|
||||
'fieldname' => $this->l('Weight of the heaviest item'),
|
||||
'required' => true,
|
||||
'dependency' => 'parcels'
|
||||
),
|
||||
'parcelMaxHeight' => array(
|
||||
'validate' => 'isUnsignedFloat',
|
||||
'fieldname' => $this->l('Height of the tallest item'),
|
||||
'required' => true,
|
||||
'dependency' => 'parcels'
|
||||
),
|
||||
'parcelMaxDepth' => array(
|
||||
'validate' => 'isUnsignedFloat',
|
||||
'fieldname' => $this->l('Length of the largest item'),
|
||||
'required' => true,
|
||||
'dependency' => 'parcels'
|
||||
),
|
||||
'parcelMaxWidth' => array(
|
||||
'validate' => 'isUnsignedFloat',
|
||||
'fieldname' => $this->l('Width of the longest item'),
|
||||
'required' => true,
|
||||
'dependency' => 'parcels'
|
||||
),
|
||||
'pallet' => array(
|
||||
'validate' => 'isAnything',
|
||||
'fieldname' => $this->l('Pallets'),
|
||||
'required' => false
|
||||
),
|
||||
'palletsCount' => array(
|
||||
'validate' => 'isUnsignedInt',
|
||||
'fieldname' => $this->l('Number of pallets'),
|
||||
'required' => true,
|
||||
'dependency' => 'pallet'
|
||||
),
|
||||
'palletsWeight' => array(
|
||||
'validate' => 'isUnsignedFloat',
|
||||
'fieldname' => $this->l('Summary weight'),
|
||||
'required' => true,
|
||||
'dependency' => 'pallet'
|
||||
),
|
||||
'palletMaxWeight' => array(
|
||||
'validate' => 'isUnsignedFloat',
|
||||
'fieldname' => $this->l('Weight of the heaviest item'),
|
||||
'required' => true,
|
||||
'dependency' => 'pallet'
|
||||
),
|
||||
'palletMaxHeight' => array(
|
||||
'validate' => 'isUnsignedFloat',
|
||||
'fieldname' => $this->l('Height of the tallest item'),
|
||||
'required' => true,
|
||||
'dependency' => 'pallet'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns arrange pickup data parameter
|
||||
*
|
||||
* @param string $name Parameter name
|
||||
* @return mixed|null Arrange pickup data value
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
return isset($this->data[$name]) ? $this->data[$name] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if given date is weekend
|
||||
*
|
||||
* @param datetime $date Date
|
||||
* @return bool Given date is weekend
|
||||
*/
|
||||
public static function isWeekend($date)
|
||||
{
|
||||
return (date('N', strtotime($date)) >= 6);
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays Arrange Pickup page content
|
||||
*
|
||||
* @return string Page content in HTML
|
||||
*/
|
||||
public function getPage()
|
||||
{
|
||||
$date = date('Y-m-d');
|
||||
$pickup_date = (Tools::getValue('pickupDate') ? Tools::getValue('pickupDate') :
|
||||
($this->isWeekend($date) ? date('Y-m-d', strtotime('next monday')) : $date));
|
||||
$sender_addresses = DpdPolandSenderAddress::getAddresses();
|
||||
|
||||
$this->context->smarty->assign(array(
|
||||
'settings' => new DpdPolandConfiguration,
|
||||
'pickupDate' => $pickup_date,
|
||||
'sender_addresses' => $sender_addresses
|
||||
));
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.6', '>='))
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/arrange_pickup_16.tpl');
|
||||
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/arrange_pickup.tpl');
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if time frames are valid
|
||||
*
|
||||
* @param array $timeframes Time frames
|
||||
* @param int $poland_time_in_seconds Poland country current time in seconds
|
||||
* @param bool $is_today Is time frames of today
|
||||
*/
|
||||
public static function validateTimeframes(&$timeframes, $poland_time_in_seconds, $is_today)
|
||||
{
|
||||
$count_timeframes = count($timeframes);
|
||||
|
||||
for ($i = 0; $i < $count_timeframes; $i++)
|
||||
{
|
||||
if (!isset($timeframes[$i]['range']))
|
||||
{
|
||||
unset($timeframes[$i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$end_time = explode('-', $timeframes[$i]['range']);
|
||||
$end_time = strtotime($end_time[1]);
|
||||
|
||||
if ($is_today && round(abs($end_time - $poland_time_in_seconds) / 60) < 120)
|
||||
unset($timeframes[$i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates additional time frame which wraps other time frames from first to last
|
||||
*
|
||||
* @param array $pickup_timeframes Time frames
|
||||
* @return bool|string Additional time frame
|
||||
*/
|
||||
public static function createExtraTimeframe($pickup_timeframes)
|
||||
{
|
||||
if (!$pickup_timeframes || !isset($pickup_timeframes[0]['range']))
|
||||
return false;
|
||||
|
||||
$extra_time_from = null;
|
||||
$extra_time_to = null;
|
||||
|
||||
foreach ($pickup_timeframes as $frame)
|
||||
{
|
||||
if (isset($frame['range']))
|
||||
{
|
||||
list($pickup_time_from, $pickup_time_to) = explode('-', $frame['range']);
|
||||
|
||||
if (!$extra_time_from || (str_replace(':', '', $pickup_time_from) < str_replace(':', '', $extra_time_from)))
|
||||
$extra_time_from = $pickup_time_from;
|
||||
if (!$extra_time_to || (str_replace(':', '', $pickup_time_to) > str_replace(':', '', $extra_time_to)))
|
||||
$extra_time_to = $pickup_time_to;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$extra_time_from || !$extra_time_to)
|
||||
return false;
|
||||
|
||||
if ($extra_time_from.'-'.$extra_time_to == $pickup_timeframes[0]['range'])
|
||||
return false;
|
||||
|
||||
return $extra_time_from.'-'.$extra_time_to;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects and returns Arrange Pickup data
|
||||
*
|
||||
* @return array Arrange Pickup data
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
if (!$this->data)
|
||||
foreach (array_keys($this->rules) as $element)
|
||||
$this->data[$element] = Tools::getValue($element);
|
||||
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if Arrange Pickup parameters are valid
|
||||
*
|
||||
* @return bool Arrange Pickup parameters are valid
|
||||
*/
|
||||
public function validate()
|
||||
{
|
||||
$date = Tools::getValue('pickupDate');
|
||||
|
||||
if (!Validate::isDateFormat($date))
|
||||
{
|
||||
self::$errors[] = $this->l('Wrong date format');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (strtotime($date) < strtotime(date('Y-m-d')))
|
||||
{
|
||||
self::$errors[] = $this->l('Date can not be earlier than').' '.date('Y-m-d');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->isWeekend($date))
|
||||
{
|
||||
self::$errors[] = $this->l('Weekends can not be chosen');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->dox && !$this->parcels && !$this->pallet)
|
||||
{
|
||||
self::$errors[] = $this->l('At least one service must be selected');
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($this->rules as $element => $rules)
|
||||
{
|
||||
if (!isset($rules['dependency']) || (isset($rules['dependency']) && $this->{$rules['dependency']}))
|
||||
{
|
||||
if ($rules['required'] && !$this->$element)
|
||||
{
|
||||
self::$errors[] = sprintf($this->l('The "%s" field is required.'), $rules['fieldname']);
|
||||
return false;
|
||||
}
|
||||
elseif ($this->$element && method_exists('Validate', $rules['validate']) &&
|
||||
!call_user_func(array('Validate', $rules['validate']), $this->$element))
|
||||
{
|
||||
self::$errors[] = sprintf($this->l('The "%s" field is invalid.'), $rules['fieldname']);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
236
modules/dpdpoland/controllers/configuration.controller.php
Normal file
236
modules/dpdpoland/controllers/configuration.controller.php
Normal file
@@ -0,0 +1,236 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandConfigurationController Responsible for setting page view and actions
|
||||
*/
|
||||
class DpdPolandConfigurationController extends DpdPolandController
|
||||
{
|
||||
/**
|
||||
* @var array Available services (carriers) IDs
|
||||
*/
|
||||
public $available_services_ids = array();
|
||||
|
||||
/**
|
||||
* Name of settings saving action
|
||||
*/
|
||||
const SETTINGS_SAVE_ACTION = 'saveModuleSettings';
|
||||
|
||||
/**
|
||||
* Current file name
|
||||
*/
|
||||
const FILENAME = 'configuration.controller';
|
||||
|
||||
/**
|
||||
* Displays settings page content
|
||||
*
|
||||
* @return string Settings page content in HTML
|
||||
*/
|
||||
public function getSettingsPage()
|
||||
{
|
||||
$configuration_obj = new DpdPolandConfiguration();
|
||||
|
||||
$payment_modules = array();
|
||||
foreach (DpdPoland::getPaymentModules() as $payment_module)
|
||||
{
|
||||
$module = Module::getInstanceByName($payment_module['name']);
|
||||
|
||||
if (!Validate::isLoadedObject($module))
|
||||
continue;
|
||||
|
||||
$payment_modules[] = array(
|
||||
'displayName' => $module->displayName,
|
||||
'name' => $payment_module['name']
|
||||
);
|
||||
}
|
||||
|
||||
$this->context->smarty->assign(array(
|
||||
'saveAction' => $this->module_instance->module_url,
|
||||
'settings' => $configuration_obj,
|
||||
'payer_numbers' => DpdPolandPayerNumber::getPayerNumbers(),
|
||||
'payment_modules' => $payment_modules,
|
||||
'zones' => Zone::getZones(),
|
||||
'carrier_zones' => array(
|
||||
'classic' => $this->getZonesForCarrier(DpdPolandConfiguration::CARRIER_CLASSIC_ID),
|
||||
'standard' => $this->getZonesForCarrier(DpdPolandConfiguration::CARRIER_STANDARD_ID),
|
||||
'standard_cod' => $this->getZonesForCarrier(DpdPolandConfiguration::CARRIER_STANDARD_COD_ID),
|
||||
'pudo' => $this->getZonesForCarrier(DpdPolandConfiguration::CARRIER_PUDO_ID)
|
||||
)
|
||||
));
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.6', '>='))
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/configuration_16.tpl');
|
||||
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/configuration.tpl');
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects and returns carrier zones
|
||||
*
|
||||
* @param int|string $carrier_type Carrier type
|
||||
* @return array Carrier zones
|
||||
*/
|
||||
private function getZonesForCarrier($carrier_type)
|
||||
{
|
||||
require_once(_DPDPOLAND_CONTROLLERS_DIR_.'service.php');
|
||||
|
||||
$id_carrier = (int)Configuration::get($carrier_type);
|
||||
$carrier = DpdPolandService::getCarrierByReference((int)$id_carrier);
|
||||
|
||||
if (Validate::isLoadedObject($carrier))
|
||||
$id_carrier = $carrier->id;
|
||||
|
||||
$carrier_zones = DpdPolandConfiguration::getCarrierZones((int)$id_carrier);
|
||||
$carrier_zones_list = array();
|
||||
|
||||
foreach ($carrier_zones as $zone)
|
||||
$carrier_zones_list[] = $zone['id_zone'];
|
||||
|
||||
return $carrier_zones_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates selected DPD sevices (carriers)
|
||||
* Deletes other DPD carriers
|
||||
*/
|
||||
public function createDeleteCarriers()
|
||||
{
|
||||
require_once(_DPDPOLAND_CONTROLLERS_DIR_.'service.php');
|
||||
require_once(_DPDPOLAND_CONTROLLERS_DIR_.'dpd_classic.service.php');
|
||||
require_once(_DPDPOLAND_CONTROLLERS_DIR_.'dpd_standard.service.php');
|
||||
require_once(_DPDPOLAND_CONTROLLERS_DIR_.'dpd_standard_cod.service.php');
|
||||
require_once(_DPDPOLAND_CONTROLLERS_DIR_.'dpd_pudo.service.php');
|
||||
|
||||
if (Tools::getValue(DpdPolandConfiguration::CARRIER_CLASSIC))
|
||||
{
|
||||
if (!DpdPolandCarrierClassicService::install())
|
||||
self::$errors[] = $this->l('Could not save DPD international shipment (DPD Classic) service');
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!DpdPolandCarrierClassicService::delete())
|
||||
self::$errors[] = $this->l('Could not delete DPD international shipment (DPD Classic) service');
|
||||
}
|
||||
|
||||
if (Tools::getValue(DpdPolandConfiguration::CARRIER_STANDARD))
|
||||
{
|
||||
if (!DpdPolandCarrierStandardService::install())
|
||||
self::$errors[] = $this->l('Could not save DPD domestic shipment - Standard service');
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!DpdPolandCarrierStandardService::delete())
|
||||
self::$errors[] = $this->l('Could not delete DPD domestic shipment - Standard service');
|
||||
}
|
||||
|
||||
if (Tools::getValue(DpdPolandConfiguration::CARRIER_STANDARD_COD))
|
||||
{
|
||||
if (!DpdPolandCarrierStandardCODService::install())
|
||||
self::$errors[] = $this->l('Could not save DPD domestic shipment - Standard with COD service');
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!DpdPolandCarrierStandardCODService::delete())
|
||||
self::$errors[] = $this->l('Could not delete DPD domestic shipment - Standard with COD service');
|
||||
}
|
||||
|
||||
if (Tools::getValue(DpdPolandConfiguration::CARRIER_PUDO))
|
||||
{
|
||||
if (!DpdPolandCarrierPudoService::install())
|
||||
self::$errors[] = $this->l('Could not save DPD Poland Reception Point Pickup service');
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!DpdPolandCarrierPudoService::delete())
|
||||
self::$errors[] = $this->l('Could not delete DPD Poland Reception Point Pickup service');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if settings are valid before saving them
|
||||
*/
|
||||
public function validateSettings()
|
||||
{
|
||||
if (!Tools::getValue(DpdPolandConfiguration::LOGIN))
|
||||
self::$errors[] = $this->l('Login can not be empty');
|
||||
|
||||
if (!Tools::getValue(DpdPolandConfiguration::PASSWORD))
|
||||
self::$errors[] = $this->l('Password can not be empty');
|
||||
elseif (!Validate::isPasswd(Tools::getValue(DpdPolandConfiguration::PASSWORD)))
|
||||
self::$errors[] = $this->l('Password is not valid');
|
||||
|
||||
if (!Tools::getValue(DpdPolandConfiguration::CLIENT_NUMBER))
|
||||
self::$errors[] = $this->l('Default client number must be set');
|
||||
|
||||
if (Tools::isSubmit(DpdPolandConfiguration::CARRIER_STANDARD_COD))
|
||||
{
|
||||
$checked = false;
|
||||
|
||||
foreach (DpdPoland::getPaymentModules() as $payment_module)
|
||||
if (Tools::isSubmit(DpdPolandConfiguration::COD_MODULE_PREFIX.$payment_module['name']))
|
||||
$checked = true;
|
||||
|
||||
if (!$checked)
|
||||
self::$errors[] = $this->l('At least one COD payment method must be checked');
|
||||
}
|
||||
|
||||
if (!Tools::getValue(DpdPolandConfiguration::WEIGHT_CONVERSATION_RATE))
|
||||
self::$errors[] = $this->l('Weight conversation rate can not be empty');
|
||||
elseif (!Validate::isUnsignedFloat(Tools::getValue(DpdPolandConfiguration::WEIGHT_CONVERSATION_RATE)))
|
||||
self::$errors[] = $this->l('Weight conversation rate is not valid');
|
||||
|
||||
if (!Tools::getValue(DpdPolandConfiguration::DIMENSION_CONVERSATION_RATE))
|
||||
self::$errors[] = $this->l('Dimension conversation rate can not be empty');
|
||||
elseif (!Validate::isUnsignedFloat(Tools::getValue(DpdPolandConfiguration::DIMENSION_CONVERSATION_RATE)))
|
||||
self::$errors[] = $this->l('Dimension conversation rate is not valid');
|
||||
|
||||
if (!Tools::getValue(DpdPolandConfiguration::CUSTOMER_FID))
|
||||
self::$errors[] = $this->l('Customer FID can not be empty');
|
||||
elseif (!ctype_alnum(Tools::getValue(DpdPolandConfiguration::CUSTOMER_FID)))
|
||||
self::$errors[] = $this->l('Customer FID is not valid');
|
||||
|
||||
if (!Tools::getValue(DpdPolandConfiguration::WS_URL))
|
||||
self::$errors[] = $this->l('Web Services URL can not be empty');
|
||||
elseif (!Validate::isUrl(Tools::getValue(DpdPolandConfiguration::WS_URL)))
|
||||
self::$errors[] = $this->l('Web Services URL is not valid');
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves settings into database
|
||||
*/
|
||||
public function saveSettings()
|
||||
{
|
||||
if (DpdPolandConfiguration::saveConfiguration())
|
||||
{
|
||||
if (!DpdPolandConfiguration::saveZonesForCarriers())
|
||||
DpdPoland::addFlashError($this->l('Settings saved successfully but could not assign zones for carriers'));
|
||||
else
|
||||
{
|
||||
DpdPoland::addFlashMessage($this->l('Settings saved successfully'));
|
||||
Tools::redirectAdmin($this->module_instance->module_url.'&menu=configuration');
|
||||
}
|
||||
}
|
||||
else
|
||||
DpdPoland::addFlashError($this->l('Could not save settings'));
|
||||
}
|
||||
}
|
||||
206
modules/dpdpoland/controllers/controller.php
Normal file
206
modules/dpdpoland/controllers/controller.php
Normal file
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandController Responsible for view and actions in module pages
|
||||
*/
|
||||
class DpdPolandController
|
||||
{
|
||||
/**
|
||||
* @var Context Context object
|
||||
*/
|
||||
protected $context;
|
||||
|
||||
/**
|
||||
* @var Module Module instance
|
||||
*/
|
||||
protected $module_instance;
|
||||
|
||||
/**
|
||||
* @var array Available paginations
|
||||
*/
|
||||
protected $pagination = array(10, 20, 50, 100, 300);
|
||||
|
||||
/**
|
||||
* @var int Default pagination
|
||||
*/
|
||||
private $default_pagination = 50;
|
||||
|
||||
/**
|
||||
* @var array Collected pages errors
|
||||
*/
|
||||
public static $errors = array();
|
||||
|
||||
/**
|
||||
* @var array Collected pages notices
|
||||
*/
|
||||
public static $notices = array();
|
||||
|
||||
/**
|
||||
* @var string Child class name
|
||||
*/
|
||||
private $child_class_name;
|
||||
|
||||
|
||||
/**
|
||||
* DpdPolandController class constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->context = Context::getContext();
|
||||
$this->module_instance = Module::getInstanceByName('dpdpoland');
|
||||
$this->child_class_name = get_class($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates texts
|
||||
*
|
||||
* @param string $text Text
|
||||
* @return string Translated text
|
||||
*/
|
||||
protected function l($text)
|
||||
{
|
||||
$child_class_name = $this->child_class_name;
|
||||
$reflection = new ReflectionClass($child_class_name);
|
||||
$filename = $reflection->hasConstant('FILENAME') ?
|
||||
$reflection->getConstant('FILENAME') : false;
|
||||
|
||||
return $this->module_instance->l($text, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects and formats lists filter parameters
|
||||
*
|
||||
* @param array $keys_array Filter keys
|
||||
* @param string $table Database table name
|
||||
* @return string Formatted filter query
|
||||
*/
|
||||
protected function getFilterQuery($keys_array = array(), $table)
|
||||
{
|
||||
$sql = '';
|
||||
|
||||
foreach ($keys_array as $key)
|
||||
if ($this->context->cookie->__isset($table.'Filter_'.$key))
|
||||
{
|
||||
$value = $this->context->cookie->{$table.'Filter_'.$key};
|
||||
if (Validate::isSerializedArray($value))
|
||||
{
|
||||
$date = $this->module_instance->unSerialize($value);
|
||||
|
||||
if (!empty($date[0]))
|
||||
$sql .= '`'.bqSQL($key).'` > "'.pSQL($date[0]).'" AND ';
|
||||
|
||||
if (!empty($date[1]))
|
||||
$sql .= '`'.bqSQL($key).'` < "'.pSQL($date[1]).'" AND ';
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($value != '')
|
||||
$sql .= '`'.bqSQL($key).'` LIKE "%'.pSQL($value).'%" AND ';
|
||||
}
|
||||
}
|
||||
|
||||
if ($sql)
|
||||
$sql = ' HAVING '.Tools::substr($sql, 0, -4); // remove 'AND ' from the end of query
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares list data to be displayed in page
|
||||
*
|
||||
* @param array $keys_array Filter keys
|
||||
* @param string $table Database table name
|
||||
* @param string $model Class name
|
||||
* @param string $default_order_by Default sorting value
|
||||
* @param string $default_order_way Default sorting way value
|
||||
* @param string $menu_page Current menu page
|
||||
* @param null $selected_pagination
|
||||
*/
|
||||
public function prepareListData($keys_array, $table, $model, $default_order_by, $default_order_way, $menu_page)
|
||||
{
|
||||
if (Tools::isSubmit('submitFilterButton'.$table))
|
||||
{
|
||||
foreach ($_POST as $key => $value)
|
||||
{
|
||||
if (strpos($key, $table.'Filter_') !== false) // looking for filter values in $_POST
|
||||
{
|
||||
if (is_array($value))
|
||||
$this->context->cookie->$key = serialize($value);
|
||||
else
|
||||
$this->context->cookie->$key = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Tools::isSubmit('submitReset'.$table))
|
||||
{
|
||||
foreach ($keys_array as $key)
|
||||
{
|
||||
if ($this->context->cookie->__isset($table.'Filter_'.$key))
|
||||
{
|
||||
$this->context->cookie->__unset($table.'Filter_'.$key);
|
||||
$_POST[$table.'Filter_'.$key] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.6', '>='))
|
||||
$page = (int)Tools::getValue('submitFilterButton'.$table);
|
||||
else
|
||||
$page = (int)Tools::getValue('submitFilter'.$table);
|
||||
|
||||
if (!$page)
|
||||
$page = 1;
|
||||
|
||||
$selected_pagination = (int)Tools::getValue('pagination', $this->default_pagination);
|
||||
$start = ($selected_pagination * $page) - $selected_pagination;
|
||||
|
||||
$order_by = Tools::getValue($table.'OrderBy', $default_order_by);
|
||||
$order_way = Tools::getValue($table.'OrderWay', $default_order_way);
|
||||
|
||||
$filter = $this->getFilterQuery($keys_array, $table);
|
||||
|
||||
$table_data = $model->getList($order_by, $order_way, $filter, $start, $selected_pagination);
|
||||
$list_total = count($model->getList($order_by, $order_way, $filter, null, null));
|
||||
|
||||
$total_pages = ceil($list_total / $selected_pagination);
|
||||
|
||||
if (!$total_pages)
|
||||
$total_pages = 1;
|
||||
|
||||
$this->context->smarty->assign(array(
|
||||
'full_url' => $this->module_instance->module_url.'&menu='.$menu_page.'&'.$table.'OrderBy='.$order_by.'&'.$table.'OrderWay='.$order_way,
|
||||
'table_data' => $table_data,
|
||||
'page' => $page,
|
||||
'selected_pagination' => $selected_pagination,
|
||||
'pagination' => $this->pagination,
|
||||
'total_pages' => $total_pages,
|
||||
'list_total' => $list_total,
|
||||
'filters_has_value' => (bool)$filter,
|
||||
'order_by' => $order_by,
|
||||
'order_way' => $order_way,
|
||||
'order_link' => 'index.php?controller=AdminOrders&vieworder&token='.Tools::getAdminTokenLite('AdminOrders')
|
||||
));
|
||||
}
|
||||
}
|
||||
191
modules/dpdpoland/controllers/countryList.controller.php
Normal file
191
modules/dpdpoland/controllers/countryList.controller.php
Normal file
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandCountryListController Responsible for DPD countries list view and actions
|
||||
*/
|
||||
class DpdPolandCountryListController extends DpdPolandController
|
||||
{
|
||||
/**
|
||||
* Countries list order by value
|
||||
*/
|
||||
const DEFAULT_ORDER_BY = 'id_country';
|
||||
|
||||
/**
|
||||
* Countries list order way value
|
||||
*/
|
||||
const DEFAULT_ORDER_WAY = 'asc';
|
||||
|
||||
/**
|
||||
* Current file name
|
||||
*/
|
||||
const FILENAME = 'countryList.controller';
|
||||
|
||||
/**
|
||||
* Success message after changing country enable / disable value
|
||||
*/
|
||||
public function displaySuccessStatusChangingMessage()
|
||||
{
|
||||
$page = (int)Tools::getValue('submitFilterCountries');
|
||||
|
||||
if (!$page)
|
||||
$page = 1;
|
||||
|
||||
$selected_pagination = (int)Tools::getValue('pagination', $this->pagination[0]);
|
||||
$order_by = Tools::getValue('CountryOrderBy', self::DEFAULT_ORDER_BY);
|
||||
$order_way = Tools::getValue('CountryOrderWay', self::DEFAULT_ORDER_WAY);
|
||||
|
||||
DpdPoland::addFlashMessage($this->l('Country status changed successfully'));
|
||||
|
||||
$redirect_url = $this->module_instance->module_url;
|
||||
$redirect_url .= '&menu=country_list&pagination='.$selected_pagination;
|
||||
$redirect_url .= '&CountryOrderBy='.$order_by;
|
||||
$redirect_url .= '&CountryOrderWay='.$order_way;
|
||||
$redirect_url .= '&submitFilterCountries='.$page;
|
||||
|
||||
die(Tools::redirectAdmin($redirect_url));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets enable / disable value for multiple countries
|
||||
*
|
||||
* @param array $countries Selected countries
|
||||
* @param bool $disable Is action to disable countries
|
||||
*/
|
||||
public function changeEnabledMultipleCountries($countries = array(), $disable = false)
|
||||
{
|
||||
foreach ($countries as $id_country)
|
||||
if (!$this->changeEnabled((int)$id_country, $disable))
|
||||
self::$errors[] = sprintf($this->l('Could not change country status, ID: %s'), $id_country);
|
||||
|
||||
if (!empty(self::$errors))
|
||||
{
|
||||
$this->module_instance->outputHTML(
|
||||
$this->module_instance->displayErrors(
|
||||
self::$errors
|
||||
)
|
||||
);
|
||||
|
||||
reset(self::$errors);
|
||||
}
|
||||
else
|
||||
{
|
||||
$page = (int)Tools::getValue('submitFilterCountries');
|
||||
|
||||
if (!$page)
|
||||
$page = 1;
|
||||
|
||||
$selected_pagination = (int)Tools::getValue('pagination', $this->pagination[0]);
|
||||
$order_by = Tools::getValue('CountryOrderBy', self::DEFAULT_ORDER_BY);
|
||||
$order_way = Tools::getValue('CountryOrderWay', self::DEFAULT_ORDER_WAY);
|
||||
|
||||
DpdPoland::addFlashMessage($this->l('Selected countries statuses changed successfully'));
|
||||
|
||||
$redirect_url = $this->module_instance->module_url;
|
||||
$redirect_url .= '&menu=country_list&pagination='.$selected_pagination;
|
||||
$redirect_url .= '&CountryOrderBy='.$order_by;
|
||||
$redirect_url .= '&CountryOrderWay='.$order_way;
|
||||
$redirect_url .= '&submitFilterCountries='.$page;
|
||||
|
||||
die(Tools::redirectAdmin($redirect_url));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes country status
|
||||
*
|
||||
* @param int $id_country Country ID
|
||||
* @param bool $disable Disable or enable action
|
||||
* @return bool Status changed successfully
|
||||
*/
|
||||
public function changeEnabled($id_country, $disable = false)
|
||||
{
|
||||
$country_obj = new DpdPolandCountry(DpdPolandCountry::getIdByCountry((int)$id_country));
|
||||
$country_obj->enabled = $disable ? 0 : 1;
|
||||
$country_obj->id_country = (int)$id_country;
|
||||
return $country_obj->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares list data to be displayed in page
|
||||
*
|
||||
* @return string Page content in HTML
|
||||
*/
|
||||
public function getListHTML()
|
||||
{
|
||||
$keys_array = array('id_country', 'name', 'iso_code', 'enabled');
|
||||
$this->prepareListData($keys_array, 'Countries', new DpdPolandCountry(), self::DEFAULT_ORDER_BY, self::DEFAULT_ORDER_WAY, 'country_list');
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.6', '>='))
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/country_list_16.tpl');
|
||||
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/country_list.tpl');
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables specific DPD countries during module installation
|
||||
*
|
||||
* @return bool Countries disabled successfully
|
||||
*/
|
||||
public static function disableDefaultCountries()
|
||||
{
|
||||
$context = Context::getContext();
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.5', '<'))
|
||||
{
|
||||
foreach (Country::getCountries((int)$context->language->id) as $country)
|
||||
if (!in_array($country['iso_code'], DpdPolandCountry::$default_enabled_countries)
|
||||
&& !self::disableCountryById($context, (int)$country['id_country']))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
foreach (array_keys(Shop::getShops()) as $id_shop)
|
||||
foreach (Country::getCountriesByIdShop($id_shop, Configuration::get('PS_LANG_DEFAULT')) as $country)
|
||||
if (!in_array($country['iso_code'], DpdPolandCountry::$default_enabled_countries)
|
||||
&& !self::disableCountryById($context, (int)$country['id_country'], (int)$id_shop))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables specific DPD country
|
||||
*
|
||||
* @param Context $context Context object
|
||||
* @param int $id_country Country ID
|
||||
* @param null|int $id_shop Shop ID
|
||||
* @return bool Country disabled successfully
|
||||
*/
|
||||
private static function disableCountryById($context, $id_country, $id_shop = null)
|
||||
{
|
||||
if ($id_shop === null)
|
||||
$id_shop = (int)$context->shop->id;
|
||||
|
||||
$dpdpoland_country = new DpdPolandCountry();
|
||||
$dpdpoland_country->id_country = (int)$id_country;
|
||||
$dpdpoland_country->id_shop = (int)$id_shop;
|
||||
$dpdpoland_country->enabled = 0;
|
||||
|
||||
return $dpdpoland_country->save();
|
||||
}
|
||||
}
|
||||
669
modules/dpdpoland/controllers/csv.controller.php
Normal file
669
modules/dpdpoland/controllers/csv.controller.php
Normal file
@@ -0,0 +1,669 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandCSVController Responsible for price rules page view and actions
|
||||
*/
|
||||
class DpdPolandCSVController extends DpdPolandController
|
||||
{
|
||||
/**
|
||||
* Poland country ISO code
|
||||
*/
|
||||
const POLAND_ISO_CODE = 'PL';
|
||||
|
||||
/**
|
||||
* @var array CSV file titles
|
||||
*/
|
||||
private $csv_titles = array();
|
||||
|
||||
/**
|
||||
* Name of action to save CSV file
|
||||
*/
|
||||
const SETTINGS_SAVE_CSV_ACTION = 'saveModuleCSVSettings';
|
||||
|
||||
/**
|
||||
* Name of action to download CSV file
|
||||
*/
|
||||
const SETTINGS_DOWNLOAD_CSV_ACTION = 'downloadModuleCSVSettings';
|
||||
|
||||
/**
|
||||
* Name of action to delete price rules
|
||||
*/
|
||||
const SETTINGS_DELETE_CSV_ACTION = 'deleteModuleCSVSettings';
|
||||
|
||||
/**
|
||||
* Current file name
|
||||
*/
|
||||
const FILENAME = 'csv.controller';
|
||||
|
||||
/**
|
||||
* Index of CSV file first line where CSV rules are written (not titles)
|
||||
*/
|
||||
const DEFAULT_FIRST_LINE_INDEX = 2;
|
||||
|
||||
/**
|
||||
* Error message type for invalid usage of star symbol
|
||||
*/
|
||||
const STAR_COUNTRY_ERROR = 1;
|
||||
|
||||
/**
|
||||
* Invalid country error type
|
||||
*/
|
||||
const PRESTASHOP_COUNTRY_ERROR = 2;
|
||||
|
||||
/**
|
||||
* Error type of invalid Poland country
|
||||
*/
|
||||
const PL_COUNTRY_ERROR = 3;
|
||||
|
||||
/**
|
||||
* Error type for invalid countries which are not Poland
|
||||
*/
|
||||
const NOT_PL_COUNTRY_ERROR = 4;
|
||||
|
||||
/**
|
||||
* DpdPolandCSVController class constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->setCSVTitles();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares CSV file titles
|
||||
*/
|
||||
private function setCSVTitles()
|
||||
{
|
||||
$this->csv_titles = array(
|
||||
'iso_country' => $this->l('Country'),
|
||||
'price_from' => $this->l('Cart price from (PLN)'),
|
||||
'price_to' => $this->l('Cart price to (PLN)'),
|
||||
'weight_from' => $this->l('Parcel weight from (kg)'),
|
||||
'weight_to' => $this->l('Parcel weight to (kg)'),
|
||||
'parcel_price' => $this->l('Parcel price (PLN)'),
|
||||
'id_carrier' => $this->l('Carrier'),
|
||||
'cod_price' => $this->l('COD cost (PLN)')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays price rules page content
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCSVPage()
|
||||
{
|
||||
$selected_pagination = Tools::getValue('pagination', '20');
|
||||
$page = Tools::getValue('current_page', '1');
|
||||
$start = ($selected_pagination * $page) - $selected_pagination;
|
||||
|
||||
$selected_products_data = DpdPolandCSV::getAllData($start, $selected_pagination);
|
||||
$list_total = count(DpdPolandCSV::getAllData());
|
||||
$pagination = array(20, 50, 100, 300);
|
||||
|
||||
$total_pages = ceil($list_total / $selected_pagination);
|
||||
|
||||
if (!$total_pages)
|
||||
$total_pages = 1;
|
||||
|
||||
$this->context->smarty->assign(array(
|
||||
'saveAction' => $this->module_instance->module_url.'&menu=csv',
|
||||
'csv_data' => $selected_products_data,
|
||||
'page' => $page,
|
||||
'total_pages' => $total_pages,
|
||||
'pagination' => $pagination,
|
||||
'list_total' => $list_total,
|
||||
'selected_pagination' => $selected_pagination
|
||||
));
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.6', '>='))
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/csv_16.tpl');
|
||||
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/csv.tpl');
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all price rules for current shop
|
||||
*/
|
||||
public function deleteCSV()
|
||||
{
|
||||
if (DpdPolandCSV::deleteAllData())
|
||||
DpdPoland::addFlashMessage($this->l('Price rules deleted successfully'));
|
||||
else
|
||||
DpdPoland::addFlashError($this->l('Price rules could not be deleted'));
|
||||
|
||||
Tools::redirectAdmin($this->module_instance->module_url.'&menu=csv');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates CSV file according to saved price rules
|
||||
*/
|
||||
public function generateCSV()
|
||||
{
|
||||
$csv_data = array($this->csv_titles);
|
||||
$csv_data = array_merge($csv_data, DpdPolandCSV::getCSVData());
|
||||
$this->arrayToCSV($csv_data, _DPDPOLAND_CSV_FILENAME_.'.csv', _DPDPOLAND_CSV_DELIMITER_);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets CSV rules to be downloadable as CSV data
|
||||
*
|
||||
* @param array $array Price rules
|
||||
* @param string $filename CSV file name
|
||||
* @param string $delimiter CSV file delimiter
|
||||
*/
|
||||
private function arrayToCSV($array, $filename, $delimiter)
|
||||
{
|
||||
// open raw memory as file so no temp files needed, you might run out of memory though
|
||||
$f = fopen('php://memory', 'w');
|
||||
// loop over the input array
|
||||
foreach ($array as $line)
|
||||
{
|
||||
// generate csv lines from the inner arrays
|
||||
fputcsv($f, $line, $delimiter);
|
||||
}
|
||||
// rewrind the "file" with the csv lines
|
||||
fseek($f, 0);
|
||||
// tell the browser it's going to be a csv file
|
||||
header('Content-Type: application/csv; charset=utf-8');
|
||||
// tell the browser we want to save it instead of displaying it
|
||||
header('Content-Disposition: attachement; filename="'.$filename.'"');
|
||||
// make php send the generated csv lines to the browser
|
||||
fpassthru($f);
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates uploaded CSV file data
|
||||
*
|
||||
* @param array $csv_data CSV data
|
||||
* @return array|bool Error messages | CSV data is valid
|
||||
*/
|
||||
public function validateCSVData($csv_data)
|
||||
{
|
||||
$errors = array();
|
||||
|
||||
if (!$this->validateCSVStructure($csv_data))
|
||||
{
|
||||
$errors[] = $this->l('Wrong CSV file structure or empty lines');
|
||||
return $errors;
|
||||
}
|
||||
|
||||
if (!$this->validatePLNCurrency())
|
||||
{
|
||||
$errors[] = $this->l('PLN currency must be installed before CSV import.');
|
||||
return $errors;
|
||||
}
|
||||
|
||||
$countries_validation = $this->validateCSVCountries($csv_data);
|
||||
|
||||
if ($countries_validation !== true)
|
||||
{
|
||||
if (!empty($countries_validation[0]))
|
||||
$errors[] = $this->l('Country: country code does not exist in your PrestaShop system. Invalid lines:').' '.$countries_validation[0];
|
||||
if (!empty($countries_validation[1]))
|
||||
$errors[] = sprintf($this->l('Country: country "*" can be used only with carrier ID %d. Invalid lines:'), _DPDPOLAND_CLASSIC_ID_).
|
||||
' '.$countries_validation[1];
|
||||
if (!empty($countries_validation[2]))
|
||||
$errors[] = sprintf($this->l('Country: country code PL can be used only with carrier ID %1$d or %2$d. Invalid lines:'),
|
||||
_DPDPOLAND_STANDARD_ID_, _DPDPOLAND_STANDARD_COD_ID_).' '.$countries_validation[2];
|
||||
if (!empty($countries_validation[3]))
|
||||
$errors[] = sprintf($this->l('Country: international shipping is compatible only with carrier ID %d. Invalid lines:'),
|
||||
_DPDPOLAND_CLASSIC_ID_).' '.$countries_validation[3];
|
||||
}
|
||||
|
||||
$price_from_validation = $this->validateCSVPriceFrom($csv_data);
|
||||
|
||||
if ($price_from_validation !== true)
|
||||
$errors[] = $this->l('Price (From): invalid lines:').' '.$price_from_validation;
|
||||
|
||||
$price_to_validation = $this->validateCSVPriceTo($csv_data);
|
||||
|
||||
if ($price_to_validation !== true)
|
||||
$errors[] = $this->l('Price (To): invalid lines:').' '.$price_to_validation;
|
||||
|
||||
$weight_from_validation = $this->validateCSVWeightFrom($csv_data);
|
||||
|
||||
if ($weight_from_validation !== true)
|
||||
$errors[] = $this->l('Weight (From): invalid lines:').' '.$weight_from_validation;
|
||||
|
||||
$weight_to_validation = $this->validateCSVWeightTo($csv_data);
|
||||
|
||||
if ($weight_to_validation !== true)
|
||||
$errors[] = $this->l('Weight (To): invalid lines:').' '.$weight_to_validation;
|
||||
|
||||
$parcel_prices_validation = $this->validateCSVParcelPrices($csv_data);
|
||||
|
||||
if ($parcel_prices_validation !== true)
|
||||
$errors[] = $this->l('Parcel price (PLN): invalid lines:').' '.$parcel_prices_validation;
|
||||
|
||||
$carrier_validation = $this->validateCSVCarriers($csv_data);
|
||||
|
||||
if ($carrier_validation !== true)
|
||||
$errors[] = $this->l('Carrier: invalid lines:').' '.$carrier_validation;
|
||||
|
||||
$cod_price = $this->validateCSVCODPrice($csv_data);
|
||||
|
||||
if ($cod_price !== true)
|
||||
{
|
||||
if (!empty($cod_price[0]))
|
||||
$errors[] = $this->l('COD cost (PLN): Value should be >=0. Invalid lines:').' '.$cod_price[0];
|
||||
if (!empty($cod_price[1]))
|
||||
$errors[] = $this->l('COD cost (PLN): It\'s possible to use COD only with "DPD domestic shipment - Standard with COD". Leave empty COD field otherwise. Invalid lines:').' '.$cod_price[1];
|
||||
if (!empty($cod_price[2]))
|
||||
$errors[] = $this->l('COD cost (PLN): COD is available only in Poland. Leave empty COD field otherwise. Invalid lines:').' '.$cod_price[2];
|
||||
}
|
||||
|
||||
if (!empty($errors))
|
||||
return $errors;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if PLN currency is installed
|
||||
*
|
||||
* @return bool PLN currency exists in PrestaShop
|
||||
*/
|
||||
private function validatePLNCurrency()
|
||||
{
|
||||
return (bool)Currency::getIdByIsoCode('PLN');
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if CSV file structure is valid
|
||||
*
|
||||
* @param array $csv_data CSV data
|
||||
* @return bool CSV file structure is valid
|
||||
*/
|
||||
private function validateCSVStructure($csv_data)
|
||||
{
|
||||
$csv_data_count = count($csv_data);
|
||||
for ($i = 0; $i < $csv_data_count; $i++)
|
||||
if (!isset($csv_data[$i][DpdPolandCSV::COLUMN_COD_PRICE]))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates country column in CSV data
|
||||
*
|
||||
* @param array $csv_data CSV data
|
||||
* @return array|bool Countries are valid
|
||||
*/
|
||||
private function validateCSVCountries($csv_data)
|
||||
{
|
||||
$csv_data_count = count($csv_data); //number of CSV data lines without titles
|
||||
$wrong_countries = '';
|
||||
$wrong_star_countries = '';
|
||||
$wrong_pl_countries = '';
|
||||
$wrong_not_pl_countries = '';
|
||||
|
||||
for ($i = 0; $i < $csv_data_count; $i++)
|
||||
{
|
||||
$country_validation = $this->validateCSVCountry($csv_data[$i][DpdPolandCSV::COLUMN_COUNTRY], $csv_data[$i][DpdPolandCSV::COLUMN_CARRIER]);
|
||||
|
||||
if ($country_validation !== true)
|
||||
{
|
||||
switch ($country_validation)
|
||||
{
|
||||
case self::STAR_COUNTRY_ERROR:
|
||||
$wrong_star_countries .= ($i + self::DEFAULT_FIRST_LINE_INDEX).', ';
|
||||
break;
|
||||
case self::PRESTASHOP_COUNTRY_ERROR:
|
||||
$wrong_countries .= ($i + self::DEFAULT_FIRST_LINE_INDEX).', ';
|
||||
break;
|
||||
case self::PL_COUNTRY_ERROR:
|
||||
$wrong_pl_countries .= ($i + self::DEFAULT_FIRST_LINE_INDEX).', ';
|
||||
break;
|
||||
case self::NOT_PL_COUNTRY_ERROR:
|
||||
$wrong_not_pl_countries .= ($i + self::DEFAULT_FIRST_LINE_INDEX).', ';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($wrong_countries))
|
||||
$wrong_countries = Tools::substr($wrong_countries, 0, -2); //remove last two symbols (comma & space)
|
||||
|
||||
if (!empty($wrong_star_countries))
|
||||
$wrong_star_countries = Tools::substr($wrong_star_countries, 0, -2); //remove last two symbols (comma & space)
|
||||
|
||||
if (!empty($wrong_pl_countries))
|
||||
$wrong_pl_countries = Tools::substr($wrong_pl_countries, 0, -2); //remove last two symbols (comma & space)
|
||||
|
||||
if (!empty($wrong_not_pl_countries))
|
||||
$wrong_not_pl_countries = Tools::substr($wrong_not_pl_countries, 0, -2); //remove last two symbols (comma & space)
|
||||
|
||||
return empty($wrong_countries) && empty($wrong_star_countries) && empty($wrong_pl_countries) && empty($wrong_not_pl_countries) ? true :
|
||||
array($wrong_countries, $wrong_star_countries, $wrong_pl_countries, $wrong_not_pl_countries);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates CSV rule country
|
||||
*
|
||||
* @param string $iso_code Country ISO code
|
||||
* @param int $id_carrier Carrier ID
|
||||
* @return bool|int Country is valid
|
||||
*/
|
||||
private function validateCSVCountry($iso_code, $id_carrier)
|
||||
{
|
||||
if ($iso_code === '*')
|
||||
if ($id_carrier == _DPDPOLAND_CLASSIC_ID_)
|
||||
return true;
|
||||
else
|
||||
return self::STAR_COUNTRY_ERROR;
|
||||
|
||||
if (!Country::getByIso($iso_code))
|
||||
return self::PRESTASHOP_COUNTRY_ERROR;
|
||||
|
||||
if ($iso_code == self::POLAND_ISO_CODE)
|
||||
if ($id_carrier == _DPDPOLAND_STANDARD_ID_ ||
|
||||
$id_carrier == _DPDPOLAND_STANDARD_COD_ID_ ||
|
||||
$id_carrier == _DPDPOLAND_PUDO_ID_)
|
||||
return true;
|
||||
else
|
||||
return self::PL_COUNTRY_ERROR;
|
||||
else
|
||||
if ($id_carrier == _DPDPOLAND_CLASSIC_ID_)
|
||||
return true;
|
||||
else
|
||||
return self::NOT_PL_COUNTRY_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates Price From field
|
||||
*
|
||||
* @param array $csv_data CSV file data
|
||||
* @return bool|string Price From field is valid
|
||||
*/
|
||||
private function validateCSVPriceFrom($csv_data)
|
||||
{
|
||||
$wrong_prices = '';
|
||||
$csv_data_count = count($csv_data);
|
||||
|
||||
for ($i = 0; $i < $csv_data_count; $i++)
|
||||
if (!Validate::isFloat($csv_data[$i][DpdPolandCSV::COLUMN_PRICE_FROM]) ||
|
||||
$csv_data[$i][DpdPolandCSV::COLUMN_PRICE_FROM] < 0)
|
||||
$wrong_prices .= ($i + self::DEFAULT_FIRST_LINE_INDEX).', ';
|
||||
|
||||
if (!empty($wrong_prices))
|
||||
$wrong_prices = Tools::substr($wrong_prices, 0, -2); //remove last two symbols (comma & space)
|
||||
|
||||
return empty($wrong_prices) ? true : $wrong_prices;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates Price To field
|
||||
*
|
||||
* @param array $csv_data CSV file data
|
||||
* @return bool|string Price To field is valid
|
||||
*/
|
||||
private function validateCSVPriceTo($csv_data)
|
||||
{
|
||||
$wrong_prices = '';
|
||||
$csv_data_count = count($csv_data);
|
||||
|
||||
for ($i = 0; $i < $csv_data_count; $i++)
|
||||
if (!Validate::isFloat($csv_data[$i][DpdPolandCSV::COLUMN_PRICE_TO]) ||
|
||||
$csv_data[$i][DpdPolandCSV::COLUMN_PRICE_TO] <= 0 ||
|
||||
$csv_data[$i][DpdPolandCSV::COLUMN_PRICE_TO] < $csv_data[$i][DpdPolandCSV::COLUMN_PRICE_FROM])
|
||||
$wrong_prices .= ($i + self::DEFAULT_FIRST_LINE_INDEX).', ';
|
||||
|
||||
if (!empty($wrong_prices))
|
||||
$wrong_prices = Tools::substr($wrong_prices, 0, -2); //remove last two symbols (comma & space)
|
||||
|
||||
return empty($wrong_prices) ? true : $wrong_prices;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates Weight From field
|
||||
*
|
||||
* @param array $csv_data CSV file data
|
||||
* @return bool|string Weight From field is valid
|
||||
*/
|
||||
private function validateCSVWeightFrom($csv_data)
|
||||
{
|
||||
$wrong_weights = '';
|
||||
$csv_data_count = count($csv_data);
|
||||
|
||||
for ($i = 0; $i < $csv_data_count; $i++)
|
||||
if (!Validate::isFloat($csv_data[$i][DpdPolandCSV::COLUMN_WEIGHT_FROM]) ||
|
||||
$csv_data[$i][DpdPolandCSV::COLUMN_WEIGHT_FROM] < 0)
|
||||
$wrong_weights .= ($i + self::DEFAULT_FIRST_LINE_INDEX).', ';
|
||||
|
||||
if (!empty($wrong_weights))
|
||||
$wrong_weights = Tools::substr($wrong_weights, 0, -2); //remove last two symbols (comma & space)
|
||||
|
||||
return empty($wrong_weights) ? true : $wrong_weights;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates Weight To field
|
||||
*
|
||||
* @param array $csv_data CSV file data
|
||||
* @return bool|string Weight To field is valid
|
||||
*/
|
||||
private function validateCSVWeightTo($csv_data)
|
||||
{
|
||||
$wrong_weights = '';
|
||||
$csv_data_count = count($csv_data);
|
||||
for ($i = 0; $i < $csv_data_count; $i++)
|
||||
if (!Validate::isFloat($csv_data[$i][DpdPolandCSV::COLUMN_WEIGHT_TO]) ||
|
||||
$csv_data[$i][DpdPolandCSV::COLUMN_WEIGHT_TO] <= 0 ||
|
||||
$csv_data[$i][DpdPolandCSV::COLUMN_WEIGHT_TO] < $csv_data[$i][DpdPolandCSV::COLUMN_WEIGHT_FROM])
|
||||
$wrong_weights .= ($i + self::DEFAULT_FIRST_LINE_INDEX).', ';
|
||||
|
||||
if (!empty($wrong_weights))
|
||||
$wrong_weights = Tools::substr($wrong_weights, 0, -2); //remove last two symbols (comma & space)
|
||||
|
||||
return empty($wrong_weights) ? true : $wrong_weights;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates carrier price field
|
||||
*
|
||||
* @param array $csv_data CSV file data
|
||||
* @return bool|string carrier price field is valid
|
||||
*/
|
||||
private function validateCSVParcelPrices($csv_data)
|
||||
{
|
||||
$wrong_prices = '';
|
||||
$csv_data_count = count($csv_data);
|
||||
for ($i = 0; $i < $csv_data_count; $i++)
|
||||
if (!Validate::isFloat($csv_data[$i][DpdPolandCSV::COLUMN_PARCEL_PRICE]) ||
|
||||
$csv_data[$i][DpdPolandCSV::COLUMN_PARCEL_PRICE] < 0)
|
||||
$wrong_prices .= ($i + self::DEFAULT_FIRST_LINE_INDEX).', ';
|
||||
|
||||
if (!empty($wrong_prices))
|
||||
$wrong_prices = Tools::substr($wrong_prices, 0, -2); //remove last two symbols (comma & space)
|
||||
|
||||
return empty($wrong_prices) ? true : $wrong_prices;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates carrier ID field
|
||||
*
|
||||
* @param array $csv_data CSV file data
|
||||
* @return bool|string Carrier ID field is valid
|
||||
*/
|
||||
private function validateCSVCarriers($csv_data)
|
||||
{
|
||||
$available_methods = array(_DPDPOLAND_STANDARD_ID_, _DPDPOLAND_STANDARD_COD_ID_, _DPDPOLAND_CLASSIC_ID_, _DPDPOLAND_PUDO_ID_);
|
||||
$wrong_methods = '';
|
||||
$csv_data_count = count($csv_data);
|
||||
|
||||
for ($i = 0; $i < $csv_data_count; $i++)
|
||||
if (!in_array($csv_data[$i][DpdPolandCSV::COLUMN_CARRIER], $available_methods) && $csv_data[$i][DpdPolandCSV::COLUMN_CARRIER] !== '*')
|
||||
$wrong_methods .= ($i + self::DEFAULT_FIRST_LINE_INDEX).', ';
|
||||
|
||||
if (!empty($wrong_methods))
|
||||
$wrong_methods = Tools::substr($wrong_methods, 0, -2); //remove last two symbols (comma & space)
|
||||
|
||||
return empty($wrong_methods) ? true : $wrong_methods;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates COD additional price field
|
||||
*
|
||||
* @param array $csv_data CSV file data
|
||||
* @return array|bool COD additional price field is valid
|
||||
*/
|
||||
private function validateCSVCODPrice($csv_data)
|
||||
{
|
||||
$wrong_price = '';
|
||||
$wrong_price_method = '';
|
||||
$wrong_price_country = '';
|
||||
$csv_data_count = count($csv_data);
|
||||
|
||||
for ($i = 0; $i < $csv_data_count; $i++)
|
||||
{
|
||||
if (!empty($csv_data[$i][DpdPolandCSV::COLUMN_COD_PRICE]))
|
||||
{
|
||||
if (!Validate::isFloat($csv_data[$i][DpdPolandCSV::COLUMN_COD_PRICE]) ||
|
||||
$csv_data[$i][DpdPolandCSV::COLUMN_COD_PRICE] < 0)
|
||||
$wrong_price .= ($i + self::DEFAULT_FIRST_LINE_INDEX).', ';
|
||||
else
|
||||
{
|
||||
if ($csv_data[$i][DpdPolandCSV::COLUMN_CARRIER] != _DPDPOLAND_STANDARD_COD_ID_)
|
||||
$wrong_price_method .= ($i + self::DEFAULT_FIRST_LINE_INDEX).', ';
|
||||
|
||||
if ($csv_data[$i][DpdPolandCSV::COLUMN_COUNTRY] !== self::POLAND_ISO_CODE)
|
||||
$wrong_price_country .= ($i + self::DEFAULT_FIRST_LINE_INDEX).', ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($wrong_price))
|
||||
$wrong_price = Tools::substr($wrong_price, 0, -2); //remove last two symbols (comma & space)
|
||||
|
||||
if (!empty($wrong_price_method))
|
||||
$wrong_price_method = Tools::substr($wrong_price_method, 0, -2); //remove last two symbols (comma & space)
|
||||
|
||||
if (!empty($wrong_price_country))
|
||||
$wrong_price_country = Tools::substr($wrong_price_country, 0, -2); //remove last two symbols (comma & space)
|
||||
|
||||
return empty($wrong_price) && empty($wrong_price_method) && empty($wrong_price_country) ? true :
|
||||
array($wrong_price, $wrong_price_method, $wrong_price_country);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if uploaded file has no errors and if extension is 'csv'.
|
||||
*
|
||||
* @param string $file_name File name
|
||||
* @return bool Uploaded file is valid
|
||||
*/
|
||||
private function isUploadedCsvValid($file_name)
|
||||
{
|
||||
if (!isset($_FILES[$file_name]))
|
||||
return false;
|
||||
|
||||
if (!empty($_FILES[$file_name]['error']))
|
||||
return false;
|
||||
|
||||
if (!preg_match('/.*\.csv$/i', $_FILES[$file_name]['name']))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads CSV file line by line and collects it's data
|
||||
*
|
||||
* @return array|bool CSV file content
|
||||
*/
|
||||
public function readCSVData()
|
||||
{
|
||||
if (!$this->isUploadedCsvValid(DpdPolandCSV::CSV_FILE))
|
||||
return false;
|
||||
|
||||
$csv_data = array();
|
||||
$row = 0;
|
||||
|
||||
if (($handle = fopen($_FILES[DpdPolandCSV::CSV_FILE]['tmp_name'], 'r')) !== false) {
|
||||
while (($data = fgetcsv($handle, 1000, _DPDPOLAND_CSV_DELIMITER_)) !== false) {
|
||||
if (!$data) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$csv_data_line = array();
|
||||
$row++;
|
||||
|
||||
if ($row == 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$num = count($data);
|
||||
$row++;
|
||||
|
||||
for ($i = 0; $i < $num; $i++) {
|
||||
$csv_data_line[] = $data[$i];
|
||||
}
|
||||
|
||||
$csv_data[] = $csv_data_line;
|
||||
}
|
||||
|
||||
fclose($handle);
|
||||
}
|
||||
|
||||
return $csv_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves price rules into database
|
||||
*
|
||||
* @param array $csv_data Valid CSV data
|
||||
* @return bool Price rules saved successfully
|
||||
*/
|
||||
public function saveCSVData($csv_data)
|
||||
{
|
||||
if (!DpdPolandCSV::deleteAllData()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($csv_data as $data)
|
||||
{
|
||||
$csv_obj = new DpdPolandCSV();
|
||||
$csv_obj->id_shop = (int)$this->context->shop->id;
|
||||
$csv_obj->iso_country = $data[DpdPolandCSV::COLUMN_COUNTRY];
|
||||
$csv_obj->price_from = $data[DpdPolandCSV::COLUMN_PRICE_FROM];
|
||||
$csv_obj->price_to = $data[DpdPolandCSV::COLUMN_PRICE_TO];
|
||||
$csv_obj->weight_from = $data[DpdPolandCSV::COLUMN_WEIGHT_FROM];
|
||||
$csv_obj->weight_to = $data[DpdPolandCSV::COLUMN_WEIGHT_TO];
|
||||
$csv_obj->parcel_price = $data[DpdPolandCSV::COLUMN_PARCEL_PRICE];
|
||||
$csv_obj->id_carrier = $data[DpdPolandCSV::COLUMN_CARRIER];
|
||||
$csv_obj->cod_price = $data[DpdPolandCSV::COLUMN_COD_PRICE];
|
||||
|
||||
if (!$csv_obj->save()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
120
modules/dpdpoland/controllers/dpd_classic.service.php
Normal file
120
modules/dpdpoland/controllers/dpd_classic.service.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandCarrierClassicService Responsible for specific DPD service (carrier) management
|
||||
*/
|
||||
class DpdPolandCarrierClassicService extends DpdPolandService
|
||||
{
|
||||
/**
|
||||
* Current file name
|
||||
*/
|
||||
const FILENAME = 'dpd_classic.service';
|
||||
|
||||
/**
|
||||
* Installs specific DPD service (carrier)
|
||||
*
|
||||
* @return bool Carrier installed successfully
|
||||
*/
|
||||
public static function install()
|
||||
{
|
||||
$id_carrier = (int)Configuration::get(DpdPolandConfiguration::CARRIER_CLASSIC_ID);
|
||||
$carrier = self::getCarrierByReference((int)$id_carrier);
|
||||
|
||||
if ($id_carrier && Validate::isLoadedObject($carrier))
|
||||
if (!$carrier->deleted)
|
||||
return true;
|
||||
else
|
||||
{
|
||||
$carrier->deleted = 0;
|
||||
return (bool)$carrier->save();
|
||||
}
|
||||
|
||||
$carrier_classic = new DpdPolandCarrierClassicService();
|
||||
|
||||
$carrier = new Carrier();
|
||||
$carrier->name = $carrier_classic->module_instance->l('DPD international shipment (DPD Classic)', self::FILENAME);
|
||||
$carrier->active = 1;
|
||||
$carrier->is_free = 0;
|
||||
$carrier->shipping_handling = 1;
|
||||
$carrier->shipping_external = 1;
|
||||
$carrier->shipping_method = 1;
|
||||
$carrier->max_width = 0;
|
||||
$carrier->max_height = 0;
|
||||
$carrier->max_depth = 0;
|
||||
$carrier->max_weight = 0;
|
||||
$carrier->grade = 0;
|
||||
$carrier->is_module = 1;
|
||||
$carrier->need_range = 1;
|
||||
$carrier->range_behavior = 1;
|
||||
$carrier->external_module_name = $carrier_classic->module_instance->name;
|
||||
$carrier->url = _DPDPOLAND_TRACKING_URL_;
|
||||
|
||||
$delay = array();
|
||||
|
||||
foreach (Language::getLanguages(false) as $language)
|
||||
$delay[$language['id_lang']] = $carrier_classic->module_instance->l('DPD international shipment (DPD Classic)', self::FILENAME);
|
||||
|
||||
$carrier->delay = $delay;
|
||||
|
||||
if (!$carrier->save())
|
||||
return false;
|
||||
|
||||
$dpdpoland_carrier = new DpdPolandCarrier();
|
||||
$dpdpoland_carrier->id_carrier = (int)$carrier->id;
|
||||
$dpdpoland_carrier->id_reference = (int)$carrier->id;
|
||||
|
||||
if (!$dpdpoland_carrier->save())
|
||||
return false;
|
||||
|
||||
if (!copy(_DPDPOLAND_IMG_DIR_.DpdPolandCarrierClassicService::IMG_DIR.'/'._DPDPOLAND_CLASSIC_ID_.'.'.
|
||||
DpdPolandCarrierClassicService::IMG_EXTENSION, _PS_SHIP_IMG_DIR_.'/'.(int)$carrier->id.'.jpg'))
|
||||
return false;
|
||||
|
||||
$range_obj = $carrier->getRangeObject();
|
||||
$range_obj->id_carrier = (int)$carrier->id;
|
||||
$range_obj->delimiter1 = 0;
|
||||
$range_obj->delimiter2 = 1;
|
||||
|
||||
if (!$range_obj->save())
|
||||
return false;
|
||||
|
||||
if (!self::assignCustomerGroupsForCarrier($carrier))
|
||||
return false;
|
||||
|
||||
if (!Configuration::updateValue(DpdPolandConfiguration::CARRIER_CLASSIC_ID, (int)$carrier->id))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes DPD carrier
|
||||
*
|
||||
* @return bool DPD carrier deleted successfully
|
||||
*/
|
||||
public static function delete()
|
||||
{
|
||||
return (bool)self::deleteCarrier((int)Configuration::get(DpdPolandConfiguration::CARRIER_CLASSIC_ID));
|
||||
}
|
||||
}
|
||||
120
modules/dpdpoland/controllers/dpd_pudo.service.php
Normal file
120
modules/dpdpoland/controllers/dpd_pudo.service.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandCarrierPudoService Responsible for specific DPD service (carrier) management
|
||||
*/
|
||||
class DpdPolandCarrierPudoService extends DpdPolandService
|
||||
{
|
||||
/**
|
||||
* Current file name
|
||||
*/
|
||||
const FILENAME = 'dpd_pudo.service';
|
||||
|
||||
/**
|
||||
* Installs specific DPD service (carrier)
|
||||
*
|
||||
* @return bool Carrier installed successfully
|
||||
*/
|
||||
public static function install()
|
||||
{
|
||||
$id_carrier = (int)Configuration::get(DpdPolandConfiguration::CARRIER_PUDO_ID);
|
||||
$carrier = self::getCarrierByReference((int)$id_carrier);
|
||||
|
||||
if ($id_carrier && Validate::isLoadedObject($carrier))
|
||||
if (!$carrier->deleted)
|
||||
return true;
|
||||
else
|
||||
{
|
||||
$carrier->deleted = 0;
|
||||
return (bool)$carrier->save();
|
||||
}
|
||||
|
||||
$carrier_pudo = new DpdPolandCarrierPudoService();
|
||||
|
||||
$carrier = new Carrier();
|
||||
$carrier->name = $carrier_pudo->module_instance->l('DPD Poland Reception Point Pickup', self::FILENAME);
|
||||
$carrier->active = 1;
|
||||
$carrier->is_free = 0;
|
||||
$carrier->shipping_handling = 1;
|
||||
$carrier->shipping_external = 1;
|
||||
$carrier->shipping_method = 1;
|
||||
$carrier->max_width = 0;
|
||||
$carrier->max_height = 0;
|
||||
$carrier->max_depth = 0;
|
||||
$carrier->max_weight = 0;
|
||||
$carrier->grade = 0;
|
||||
$carrier->is_module = 1;
|
||||
$carrier->need_range = 1;
|
||||
$carrier->range_behavior = 1;
|
||||
$carrier->external_module_name = $carrier_pudo->module_instance->name;
|
||||
$carrier->url = _DPDPOLAND_TRACKING_URL_;
|
||||
|
||||
$delay = array();
|
||||
|
||||
foreach (Language::getLanguages(false) as $language)
|
||||
$delay[$language['id_lang']] = $carrier_pudo->module_instance->l('DPD Poland Reception Point Pickup', self::FILENAME);
|
||||
|
||||
$carrier->delay = $delay;
|
||||
|
||||
if (!$carrier->save())
|
||||
return false;
|
||||
|
||||
$dpdpoland_carrier = new DpdPolandCarrier();
|
||||
$dpdpoland_carrier->id_carrier = (int)$carrier->id;
|
||||
$dpdpoland_carrier->id_reference = (int)$carrier->id;
|
||||
|
||||
if (!$dpdpoland_carrier->save())
|
||||
return false;
|
||||
|
||||
if (!copy(_DPDPOLAND_IMG_DIR_.DpdPolandCarrierPudoService::IMG_DIR.'/'._DPDPOLAND_PUDO_ID_.'.'.
|
||||
DpdPolandCarrierPudoService::IMG_EXTENSION, _PS_SHIP_IMG_DIR_.'/'.(int)$carrier->id.'.jpg'))
|
||||
return false;
|
||||
|
||||
$range_obj = $carrier->getRangeObject();
|
||||
$range_obj->id_carrier = (int)$carrier->id;
|
||||
$range_obj->delimiter1 = 0;
|
||||
$range_obj->delimiter2 = 1;
|
||||
|
||||
if (!$range_obj->save())
|
||||
return false;
|
||||
|
||||
if (!self::assignCustomerGroupsForCarrier($carrier))
|
||||
return false;
|
||||
|
||||
if (!Configuration::updateValue(DpdPolandConfiguration::CARRIER_PUDO_ID, (int)$carrier->id))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes DPD carrier
|
||||
*
|
||||
* @return bool DPD carrier deleted successfully
|
||||
*/
|
||||
public static function delete()
|
||||
{
|
||||
return (bool)self::deleteCarrier((int)Configuration::get(DpdPolandConfiguration::CARRIER_PUDO_ID));
|
||||
}
|
||||
}
|
||||
120
modules/dpdpoland/controllers/dpd_standard.service.php
Normal file
120
modules/dpdpoland/controllers/dpd_standard.service.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandCarrierStandardService Responsible for specific DPD service (carrier) management
|
||||
*/
|
||||
class DpdPolandCarrierStandardService extends DpdPolandService
|
||||
{
|
||||
/**
|
||||
* Current file name
|
||||
*/
|
||||
const FILENAME = 'dpd_standard.service';
|
||||
|
||||
/**
|
||||
* Installs specific DPD service (carrier)
|
||||
*
|
||||
* @return bool Carrier installed successfully
|
||||
*/
|
||||
public static function install()
|
||||
{
|
||||
$id_carrier = (int)Configuration::get(DpdPolandConfiguration::CARRIER_STANDARD_ID);
|
||||
$carrier = self::getCarrierByReference((int)$id_carrier);
|
||||
|
||||
if ($id_carrier && Validate::isLoadedObject($carrier))
|
||||
if (!$carrier->deleted)
|
||||
return true;
|
||||
else
|
||||
{
|
||||
$carrier->deleted = 0;
|
||||
return (bool)$carrier->save();
|
||||
}
|
||||
|
||||
$carrier_standard = new DpdPolandCarrierStandardService();
|
||||
|
||||
$carrier = new Carrier();
|
||||
$carrier->name = $carrier_standard->module_instance->l('DPD domestic shipment - Standard', self::FILENAME);
|
||||
$carrier->active = 1;
|
||||
$carrier->is_free = 0;
|
||||
$carrier->shipping_handling = 1;
|
||||
$carrier->shipping_external = 1;
|
||||
$carrier->shipping_method = 1;
|
||||
$carrier->max_width = 0;
|
||||
$carrier->max_height = 0;
|
||||
$carrier->max_depth = 0;
|
||||
$carrier->max_weight = 0;
|
||||
$carrier->grade = 0;
|
||||
$carrier->is_module = 1;
|
||||
$carrier->need_range = 1;
|
||||
$carrier->range_behavior = 1;
|
||||
$carrier->external_module_name = $carrier_standard->module_instance->name;
|
||||
$carrier->url = _DPDPOLAND_TRACKING_URL_;
|
||||
|
||||
$delay = array();
|
||||
|
||||
foreach (Language::getLanguages(false) as $language)
|
||||
$delay[$language['id_lang']] = $carrier_standard->module_instance->l('DPD domestic shipment - Standard', self::FILENAME);
|
||||
|
||||
$carrier->delay = $delay;
|
||||
|
||||
if (!$carrier->save())
|
||||
return false;
|
||||
|
||||
$dpdpoland_carrier = new DpdPolandCarrier();
|
||||
$dpdpoland_carrier->id_carrier = (int)$carrier->id;
|
||||
$dpdpoland_carrier->id_reference = (int)$carrier->id;
|
||||
|
||||
if (!$dpdpoland_carrier->save())
|
||||
return false;
|
||||
|
||||
if (!copy(_DPDPOLAND_IMG_DIR_.DpdPolandCarrierStandardService::IMG_DIR.'/'._DPDPOLAND_STANDARD_ID_.'.'.
|
||||
DpdPolandCarrierStandardService::IMG_EXTENSION, _PS_SHIP_IMG_DIR_.'/'.(int)$carrier->id.'.jpg'))
|
||||
return false;
|
||||
|
||||
$range_obj = $carrier->getRangeObject();
|
||||
$range_obj->id_carrier = (int)$carrier->id;
|
||||
$range_obj->delimiter1 = 0;
|
||||
$range_obj->delimiter2 = 1;
|
||||
|
||||
if (!$range_obj->save())
|
||||
return false;
|
||||
|
||||
if (!self::assignCustomerGroupsForCarrier($carrier))
|
||||
return false;
|
||||
|
||||
if (!Configuration::updateValue(DpdPolandConfiguration::CARRIER_STANDARD_ID, (int)$carrier->id))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes DPD carrier
|
||||
*
|
||||
* @return bool DPD carrier deleted successfully
|
||||
*/
|
||||
public static function delete()
|
||||
{
|
||||
return (bool)self::deleteCarrier((int)Configuration::get(DpdPolandConfiguration::CARRIER_STANDARD_ID));
|
||||
}
|
||||
}
|
||||
120
modules/dpdpoland/controllers/dpd_standard_cod.service.php
Normal file
120
modules/dpdpoland/controllers/dpd_standard_cod.service.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandCarrierStandardCODService Responsible for specific DPD service (carrier) management
|
||||
*/
|
||||
class DpdPolandCarrierStandardCODService extends DpdPolandService
|
||||
{
|
||||
/**
|
||||
* Current file name
|
||||
*/
|
||||
const FILENAME = 'dpd_standard_cod.service';
|
||||
|
||||
/**
|
||||
* Installs specific DPD service (carrier)
|
||||
*
|
||||
* @return bool Carrier installed successfully
|
||||
*/
|
||||
public static function install()
|
||||
{
|
||||
$id_carrier = (int)Configuration::get(DpdPolandConfiguration::CARRIER_STANDARD_COD_ID);
|
||||
$carrier = self::getCarrierByReference((int)$id_carrier);
|
||||
|
||||
if ($id_carrier && Validate::isLoadedObject($carrier))
|
||||
if (!$carrier->deleted)
|
||||
return true;
|
||||
else
|
||||
{
|
||||
$carrier->deleted = 0;
|
||||
return (bool)$carrier->save();
|
||||
}
|
||||
|
||||
$carrier_standard_cod = new DpdPolandCarrierStandardCODService();
|
||||
|
||||
$carrier = new Carrier();
|
||||
$carrier->name = $carrier_standard_cod->module_instance->l('DPD domestic shipment - Standard with COD', self::FILENAME);
|
||||
$carrier->active = 1;
|
||||
$carrier->is_free = 0;
|
||||
$carrier->shipping_handling = 1;
|
||||
$carrier->shipping_external = 1;
|
||||
$carrier->shipping_method = 1;
|
||||
$carrier->max_width = 0;
|
||||
$carrier->max_height = 0;
|
||||
$carrier->max_depth = 0;
|
||||
$carrier->max_weight = 0;
|
||||
$carrier->grade = 0;
|
||||
$carrier->is_module = 1;
|
||||
$carrier->need_range = 1;
|
||||
$carrier->range_behavior = 1;
|
||||
$carrier->external_module_name = $carrier_standard_cod->module_instance->name;
|
||||
$carrier->url = _DPDPOLAND_TRACKING_URL_;
|
||||
|
||||
$delay = array();
|
||||
|
||||
foreach (Language::getLanguages(false) as $language)
|
||||
$delay[$language['id_lang']] = $carrier_standard_cod->module_instance->l('DPD domestic shipment - Standard with COD', self::FILENAME);
|
||||
|
||||
$carrier->delay = $delay;
|
||||
|
||||
if (!$carrier->save())
|
||||
return false;
|
||||
|
||||
$dpdpoland_carrier = new DpdPolandCarrier();
|
||||
$dpdpoland_carrier->id_carrier = (int)$carrier->id;
|
||||
$dpdpoland_carrier->id_reference = (int)$carrier->id;
|
||||
|
||||
if (!$dpdpoland_carrier->save())
|
||||
return false;
|
||||
|
||||
if (!copy(_DPDPOLAND_IMG_DIR_.DpdPolandCarrierStandardCODService::IMG_DIR.'/'._DPDPOLAND_STANDARD_COD_ID_.'.'.
|
||||
DpdPolandCarrierStandardCODService::IMG_EXTENSION, _PS_SHIP_IMG_DIR_.'/'.(int)$carrier->id.'.jpg'))
|
||||
return false;
|
||||
|
||||
$range_obj = $carrier->getRangeObject();
|
||||
$range_obj->id_carrier = (int)$carrier->id;
|
||||
$range_obj->delimiter1 = 0;
|
||||
$range_obj->delimiter2 = 1;
|
||||
|
||||
if (!$range_obj->save())
|
||||
return false;
|
||||
|
||||
if (!self::assignCustomerGroupsForCarrier($carrier))
|
||||
return false;
|
||||
|
||||
if (!Configuration::updateValue(DpdPolandConfiguration::CARRIER_STANDARD_COD_ID, (int)$carrier->id))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes DPD carrier
|
||||
*
|
||||
* @return bool DPD carrier deleted successfully
|
||||
*/
|
||||
public static function delete()
|
||||
{
|
||||
return (bool)self::deleteCarrier((int)Configuration::get(DpdPolandConfiguration::CARRIER_STANDARD_COD_ID));
|
||||
}
|
||||
}
|
||||
29
modules/dpdpoland/controllers/index.php
Normal file
29
modules/dpdpoland/controllers/index.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
|
||||
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
|
||||
|
||||
header("Cache-Control: no-store, no-cache, must-revalidate");
|
||||
header("Cache-Control: post-check=0, pre-check=0", false);
|
||||
header("Pragma: no-cache");
|
||||
|
||||
header("Location: ../");
|
||||
exit;
|
||||
167
modules/dpdpoland/controllers/manifest.webservice.php
Normal file
167
modules/dpdpoland/controllers/manifest.webservice.php
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandManifestWS Responsible for manifest management
|
||||
*/
|
||||
class DpdPolandManifestWS extends DpdPolandWS
|
||||
{
|
||||
/**
|
||||
* Generates manifest
|
||||
*
|
||||
* @param DpdPolandManifest $manifest Manifest object
|
||||
* @param string $output_doc_format Document format
|
||||
* @param string $output_doc_page_format Document page format
|
||||
* @param string $policy Policy type
|
||||
* @return bool
|
||||
*/
|
||||
public function generate(DpdPolandManifest $manifest, $output_doc_format, $output_doc_page_format, $policy)
|
||||
{
|
||||
$package = $manifest->getPackageInstance();
|
||||
$packages = $manifest->getPackages();
|
||||
$package_number = null;
|
||||
|
||||
foreach ($packages as $id_package_ws)
|
||||
{
|
||||
$current_package = new DpdPolandPackage((int)$id_package_ws);
|
||||
|
||||
if ($package_number === null)
|
||||
$package_number = $current_package->payerNumber;
|
||||
elseif ($package_number !== $current_package->payerNumber)
|
||||
$package_number = 'null';
|
||||
}
|
||||
|
||||
$params = array(
|
||||
'dpdServicesParamsV1' => array(
|
||||
'pickupAddress' => $package->getSenderAddress(),
|
||||
'policy' => $policy,
|
||||
'session' => array(
|
||||
'sessionId' => (int)$package->sessionId,
|
||||
'sessionType' => $package->getSessionType()
|
||||
)
|
||||
),
|
||||
'outputDocFormatV1' => $output_doc_format,
|
||||
'outputDocPageFormatV1' => $output_doc_page_format
|
||||
);
|
||||
|
||||
if ($manifest->id_manifest_ws)
|
||||
$params['dpdServicesParamsV1']['documentId'] = (int)$manifest->id_manifest_ws;
|
||||
|
||||
$result = $this->generateProtocolV2($params);
|
||||
|
||||
if (isset($result['documentData']) ||
|
||||
(isset($result['session']) && isset($result['session']['statusInfo']) && $result['session']['statusInfo']['status'] == 'OK'))
|
||||
{
|
||||
if (!$manifest->id_manifest_ws)
|
||||
$manifest->id_manifest_ws = (int)$result['documentId'];
|
||||
|
||||
if (!$manifest->getPackageIdWsByManifestIdWs($manifest->id_manifest_ws) && !$manifest->save())
|
||||
return false;
|
||||
|
||||
return $result['documentData'];
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates multiple manifests for selected packages
|
||||
*
|
||||
* @param array $package_ids Packages IDs
|
||||
* @param string $output_doc_format Document format
|
||||
* @param string $output_doc_page_format Document page format
|
||||
* @param string $policy Policy type
|
||||
* @return bool Multiple manifests generated successfully
|
||||
*/
|
||||
public function generateMultiple($package_ids, $output_doc_format = 'PDF', $output_doc_page_format = 'LBL_PRINTER', $policy = 'STOP_ON_FIRST_ERROR')
|
||||
{
|
||||
$session_type = '';
|
||||
$package_number = null;
|
||||
|
||||
foreach ($package_ids as $id_package_ws)
|
||||
{
|
||||
$package = new DpdPolandPackage((int)$id_package_ws);
|
||||
|
||||
if (!$session_type || $session_type == $package->getSessionType())
|
||||
{
|
||||
$session_type = $package->getSessionType();
|
||||
|
||||
if ($package_number === null)
|
||||
$package_number = $package->payerNumber;
|
||||
elseif ($package_number !== $package->payerNumber)
|
||||
$package_number = 'null';
|
||||
}
|
||||
else
|
||||
{
|
||||
self::$errors[] = $this->l('Manifests of DOMESTIC shipments cannot be mixed with INTERNATIONAL shipments');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$params = array(
|
||||
'dpdServicesParamsV1' => array(
|
||||
'pickupAddress' => $package->getSenderAddress(),
|
||||
'policy' => $policy,
|
||||
'session' => array(
|
||||
'sessionType' => $session_type,
|
||||
'packages' => array()
|
||||
)
|
||||
),
|
||||
'outputDocFormatV1' => $output_doc_format,
|
||||
'outputDocPageFormatV1' => $output_doc_page_format
|
||||
);
|
||||
|
||||
foreach ($package_ids as $id_package_ws)
|
||||
{
|
||||
$params['dpdServicesParamsV1']['session']['packages'][] = array(
|
||||
'packageId' => (int)$id_package_ws
|
||||
);
|
||||
}
|
||||
|
||||
$result = $this->generateProtocolV2($params);
|
||||
|
||||
if (isset($result['session']) && isset($result['session']['statusInfo']) && $result['session']['statusInfo']['status'] == 'OK')
|
||||
{
|
||||
foreach ($package_ids as $id_package_ws)
|
||||
{
|
||||
$manifest = new DpdPolandManifest;
|
||||
$manifest->id_manifest_ws = (int)$result['documentId'];
|
||||
$manifest->id_package_ws = (int)$id_package_ws;
|
||||
|
||||
if (!$manifest->save())
|
||||
return false;
|
||||
}
|
||||
|
||||
return $result['documentData'];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isset($result['session']['statusInfo']['description']))
|
||||
self::$errors[] = $result['session']['statusInfo']['description'];
|
||||
elseif (isset($result['session']['statusInfo']['status']))
|
||||
self::$errors[] = $result['session']['statusInfo']['status'];
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
201
modules/dpdpoland/controllers/manifestList.controller.php
Normal file
201
modules/dpdpoland/controllers/manifestList.controller.php
Normal file
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandManifestListController Responsible for manifests list view and management
|
||||
*/
|
||||
class DpdPolandManifestListController extends DpdPolandController
|
||||
{
|
||||
/**
|
||||
* Default list sorting type
|
||||
*/
|
||||
const DEFAULT_ORDER_BY = 'date_add';
|
||||
|
||||
/**
|
||||
* Default list sorting way
|
||||
*/
|
||||
const DEFAULT_ORDER_WAY = 'desc';
|
||||
|
||||
/**
|
||||
* Current file name
|
||||
*/
|
||||
const FILENAME = 'manifestList.controller';
|
||||
|
||||
/**
|
||||
* Prints a single manifest
|
||||
*
|
||||
* @param int|string $id_manifest_ws Manifest ID
|
||||
* @return bool Manifest printed successfully
|
||||
*/
|
||||
public function printManifest($id_manifest_ws)
|
||||
{
|
||||
if (is_array($id_manifest_ws))
|
||||
{
|
||||
if (empty($id_manifest_ws))
|
||||
return false;
|
||||
|
||||
if (file_exists(_PS_MODULE_DIR_.'dpdpoland/manifest_duplicated.pdf') &&
|
||||
!unlink(_PS_MODULE_DIR_.'dpdpoland/manifest_duplicated.pdf'))
|
||||
{
|
||||
$error_message = $this->l('Could not delete old PDF file. Please check module permissions');
|
||||
$error = $this->module_instance->displayError($error_message);
|
||||
|
||||
return $this->module_instance->outputHTML($error);
|
||||
}
|
||||
|
||||
foreach ($id_manifest_ws as $id)
|
||||
{
|
||||
$manifest = new DpdPolandManifest;
|
||||
$manifest->id_manifest_ws = $id;
|
||||
|
||||
if ($pdf_file_contents = $manifest->generate())
|
||||
{
|
||||
if (file_exists(_PS_MODULE_DIR_.'dpdpoland/manifest_'.(int)$id.'.pdf') &&
|
||||
!unlink(_PS_MODULE_DIR_.'dpdpoland/manifest_'.(int)$id.'.pdf'))
|
||||
{
|
||||
$error_message = $this->l('Could not delete old PDF file. Please check module permissions');
|
||||
$error = $this->module_instance->displayError($error_message);
|
||||
return $this->module_instance->outputHTML($error);
|
||||
}
|
||||
|
||||
$fp = fopen(_PS_MODULE_DIR_.'dpdpoland/manifest_'.(int)$id.'.pdf', 'a');
|
||||
|
||||
if (!$fp)
|
||||
{
|
||||
$error_message = $this->l('Could not create PDF file. Please check module folder permissions');
|
||||
$error = $this->module_instance->displayError($error_message);
|
||||
return $this->module_instance->outputHTML($error);
|
||||
}
|
||||
|
||||
fwrite($fp, $pdf_file_contents);
|
||||
fclose($fp);
|
||||
}
|
||||
else
|
||||
{
|
||||
$error_message = $this->module_instance->displayError(reset(DpdPolandManifestWS::$errors));
|
||||
return $this->module_instance->outputHTML($error_message);
|
||||
}
|
||||
}
|
||||
|
||||
include_once(_PS_MODULE_DIR_.'dpdpoland/libraries/PDFMerger/PDFMerger.php');
|
||||
|
||||
$pdf = new PDFMerger;
|
||||
|
||||
foreach ($id_manifest_ws as $id)
|
||||
{
|
||||
$manifest_pdf_path = _PS_MODULE_DIR_.'dpdpoland/manifest_'.(int)$id.'.pdf';
|
||||
$pdf->addPDF($manifest_pdf_path, 'all');
|
||||
}
|
||||
|
||||
$pdf->merge('file', _PS_MODULE_DIR_.'dpdpoland/manifest_duplicated.pdf');
|
||||
|
||||
ob_end_clean();
|
||||
header('Content-type: application/pdf');
|
||||
header('Content-Disposition: attachment; filename="manifests_'.time().'.pdf"');
|
||||
readfile(_PS_MODULE_DIR_.'dpdpoland/manifest_duplicated.pdf');
|
||||
$this->deletePDFFiles($id_manifest_ws);
|
||||
exit;
|
||||
}
|
||||
|
||||
$manifest = new DpdPolandManifest;
|
||||
$manifest->id_manifest_ws = $id_manifest_ws;
|
||||
|
||||
if ($pdf_file_contents = $manifest->generate())
|
||||
{
|
||||
if (file_exists(_PS_MODULE_DIR_.'dpdpoland/manifest.pdf') && !unlink(_PS_MODULE_DIR_.'dpdpoland/manifest.pdf'))
|
||||
{
|
||||
$error_message = $this->l('Could not delete old PDF file. Please check module permissions');
|
||||
$error = $this->module_instance->displayError($error_message);
|
||||
return $this->module_instance->outputHTML($error);
|
||||
}
|
||||
|
||||
if (file_exists(_PS_MODULE_DIR_.'dpdpoland/manifest_duplicated.pdf') && !unlink(_PS_MODULE_DIR_.'dpdpoland/manifest_duplicated.pdf'))
|
||||
{
|
||||
$error_message = $this->l('Could not delete old PDF file. Please check module permissions');
|
||||
$error = $this->module_instance->displayError($error_message);
|
||||
return $this->module_instance->outputHTML($error);
|
||||
}
|
||||
|
||||
$fp = fopen(_PS_MODULE_DIR_.'dpdpoland/manifest.pdf', 'a');
|
||||
if (!$fp)
|
||||
{
|
||||
$error_message = $this->l('Could not create PDF file. Please check module folder permissions');
|
||||
$error = $this->module_instance->displayError($error_message);
|
||||
return $this->module_instance->outputHTML($error);
|
||||
}
|
||||
|
||||
fwrite($fp, $pdf_file_contents);
|
||||
fclose($fp);
|
||||
|
||||
include_once(_PS_MODULE_DIR_.'dpdpoland/libraries/PDFMerger/PDFMerger.php');
|
||||
$pdf = new PDFMerger;
|
||||
$pdf->addPDF(_PS_MODULE_DIR_.'dpdpoland/manifest.pdf', 'all');
|
||||
$pdf->merge('file', _PS_MODULE_DIR_.'dpdpoland/manifest_duplicated.pdf');
|
||||
|
||||
ob_end_clean();
|
||||
header('Content-type: application/pdf');
|
||||
header('Content-Disposition: attachment; filename="manifests_'.time().'.pdf"');
|
||||
readfile(_PS_MODULE_DIR_.'dpdpoland/manifest_duplicated.pdf');
|
||||
|
||||
$this->deletePDFFiles($id_manifest_ws);
|
||||
exit;
|
||||
}
|
||||
else
|
||||
$this->module_instance->outputHTML($this->module_instance->displayError(reset(DpdPolandManifestWS::$errors)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes generated PDF files after merging them into a single document
|
||||
*
|
||||
* @param int|string $id_manifest_ws Manifest ID
|
||||
*/
|
||||
private function deletePDFFiles($id_manifest_ws)
|
||||
{
|
||||
$manifests = array('manifest', 'manifest_duplicated');
|
||||
|
||||
if (is_array($id_manifest_ws))
|
||||
foreach ($id_manifest_ws as $id)
|
||||
$manifests[] = 'manifest_'.(int)$id;
|
||||
else
|
||||
$manifests[] = 'manifest_'.(int)$id_manifest_ws;
|
||||
|
||||
foreach ($manifests as $manifest)
|
||||
if (file_exists(_PS_MODULE_DIR_.'dpdpoland/'.$manifest.'.pdf') && is_writable(_PS_MODULE_DIR_.'dpdpoland/'.$manifest.'.pdf'))
|
||||
unlink(_PS_MODULE_DIR_.'dpdpoland/'.$manifest.'.pdf');
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays manifests list content
|
||||
*
|
||||
* @return string Manifests list content in HTML
|
||||
*/
|
||||
public function getListHTML()
|
||||
{
|
||||
$keys_array = array('id_manifest_ws', 'count_parcels', 'count_orders', 'date_add');
|
||||
$this->prepareListData($keys_array, 'Manifests', new DpdPolandManifest, self::DEFAULT_ORDER_BY, self::DEFAULT_ORDER_WAY, 'manifest_list');
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.6', '<'))
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/manifest_list.tpl');
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/manifest_list_16.tpl');
|
||||
}
|
||||
}
|
||||
116
modules/dpdpoland/controllers/messages.controller.php
Normal file
116
modules/dpdpoland/controllers/messages.controller.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class DpdPolandMessagesController Responsible for flash messages management
|
||||
*/
|
||||
class DpdPolandMessagesController extends DpdPolandController
|
||||
{
|
||||
/**
|
||||
* Name of success message saved in cookie
|
||||
*/
|
||||
const DPD_POLAND_SUCCESS_MESSAGE = 'dpd_poland_success_message';
|
||||
|
||||
/**
|
||||
* Name of error message saved in cookie
|
||||
*/
|
||||
const DPD_POLAND_ERROR_MESSAGE = 'dpd_poland_error_message';
|
||||
|
||||
/**
|
||||
* @var Cookie Cookie object instance
|
||||
*/
|
||||
private $cookie;
|
||||
|
||||
/**
|
||||
* DpdPolandMessagesController class constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->cookie = new Cookie(_DPDPOLAND_COOKIE_);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves success message into Cookie
|
||||
*
|
||||
* @param string $message Success message text
|
||||
*/
|
||||
public function setSuccessMessage($message)
|
||||
{
|
||||
if (!is_array($message))
|
||||
$this->cookie->{self::DPD_POLAND_SUCCESS_MESSAGE} = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves error message into Cookie
|
||||
*
|
||||
* @param string $message Error message text
|
||||
*/
|
||||
public function setErrorMessage($message)
|
||||
{
|
||||
$old_message = $this->cookie->{self::DPD_POLAND_ERROR_MESSAGE};
|
||||
if ($old_message && Validate::isSerializedArray($old_message))
|
||||
{
|
||||
if (version_compare(_PS_VERSION_, '1.5', '<'))
|
||||
$old_message = unserialize($old_message);
|
||||
else
|
||||
$old_message = Tools::unSerialize($old_message);
|
||||
$message = array_merge($message, $old_message);
|
||||
}
|
||||
|
||||
if (is_array($message))
|
||||
$this->context->cookie->{self::DPD_POLAND_ERROR_MESSAGE} = serialize($message);
|
||||
else
|
||||
$this->cookie->{self::DPD_POLAND_ERROR_MESSAGE} = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects and returns success message
|
||||
* Removes success message from Cookie
|
||||
*
|
||||
* @return string Success message
|
||||
*/
|
||||
public function getSuccessMessage()
|
||||
{
|
||||
$message = $this->cookie->{self::DPD_POLAND_SUCCESS_MESSAGE};
|
||||
$this->cookie->__unset(self::DPD_POLAND_SUCCESS_MESSAGE);
|
||||
return $message ? $message : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects and returns error message
|
||||
* Removes error message from Cookie
|
||||
*
|
||||
* @return array|string Error message(s)
|
||||
*/
|
||||
public function getErrorMessage()
|
||||
{
|
||||
$message = $this->cookie->{self::DPD_POLAND_ERROR_MESSAGE};
|
||||
if (Validate::isSerializedArray($message))
|
||||
if (version_compare(_PS_VERSION_, '1.5', '<'))
|
||||
$message = unserialize($message);
|
||||
else
|
||||
$message = Tools::unSerialize($message);
|
||||
$this->cookie->__unset(self::DPD_POLAND_ERROR_MESSAGE);
|
||||
if (is_array($message))
|
||||
return array_unique($message);
|
||||
return $message ? array($message) : '';
|
||||
}
|
||||
}
|
||||
586
modules/dpdpoland/controllers/package.webservice.php
Normal file
586
modules/dpdpoland/controllers/package.webservice.php
Normal file
@@ -0,0 +1,586 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandPackageWS Responsible for management via WebServices
|
||||
*/
|
||||
class DpdPolandPackageWS extends DpdPolandWS
|
||||
{
|
||||
/**
|
||||
* Current file name
|
||||
*/
|
||||
const FILENAME = 'Package';
|
||||
|
||||
/**
|
||||
* @var array Parcels data used for WebServices
|
||||
*/
|
||||
private $parcels = array();
|
||||
|
||||
/**
|
||||
* @var array Services data used for WebServices
|
||||
*/
|
||||
private $services = array();
|
||||
|
||||
/**
|
||||
* @var array Sender data used for WebServices
|
||||
*/
|
||||
private $sender = array();
|
||||
|
||||
/**
|
||||
* @var array Receiver data used for WebServices
|
||||
*/
|
||||
private $receiver = array();
|
||||
|
||||
/**
|
||||
* Sets parcels data
|
||||
*
|
||||
* @param array $parcel Parcel data
|
||||
* @param string $additional_info Additional shipment info
|
||||
*/
|
||||
public function addParcel($parcel, $additional_info)
|
||||
{
|
||||
$parcel = array(
|
||||
'content' => $parcel['content'],
|
||||
'customerData1' => $additional_info,
|
||||
'customerData2' => null,
|
||||
'customerData3' => null,
|
||||
'reference' => Tools::strtoupper(Tools::passwdGen(9, 'NO_NUMERIC')).'_'.(int)$parcel['number'],
|
||||
'sizeX' => (float)$parcel['length'],
|
||||
'sizeY' => (float)$parcel['width'],
|
||||
'sizeZ' => (float)$parcel['height'],
|
||||
'weight' => (float)$parcel['weight']
|
||||
);
|
||||
|
||||
$this->parcels[] = $parcel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects error messages from WebServices
|
||||
*
|
||||
* @param array $response Response from WebServices
|
||||
* @param string $error_key Error code
|
||||
* @param array $errors Collected errors
|
||||
* @return array Error messages
|
||||
*/
|
||||
private function getErrorsByKey($response, $error_key, $errors = array())
|
||||
{
|
||||
if (!empty($response))
|
||||
foreach ($response as $key => $value)
|
||||
if (is_object($value) || is_array($value))
|
||||
$errors = $this->getErrorsByKey($value, $error_key, $errors);
|
||||
elseif ($key == $error_key)
|
||||
$errors[] = $value;
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates package
|
||||
*
|
||||
* @param DpdPolandPackage $package_obj Package object
|
||||
* @return bool Package created successfully
|
||||
*/
|
||||
public function create(DpdPolandPackage $package_obj)
|
||||
{
|
||||
if ($result = $this->createRemotely($package_obj))
|
||||
{
|
||||
if (isset($result['Status']) && $result['Status'] == 'OK')
|
||||
{
|
||||
$package = $result['Packages']['Package'];
|
||||
$package_obj->id_package_ws = (int)$package['PackageId'];
|
||||
$package_obj->sessionId = (int)$result['SessionId'];
|
||||
|
||||
if (!$package_obj->save())
|
||||
self::$errors[] = $this->l('Package was successfully created but we were unable to save its data locally');
|
||||
|
||||
return $package;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isset($result['Packages']['InvalidFields']))
|
||||
$errors = $result['Packages']['InvalidFields'];
|
||||
elseif (isset($result['Packages']['Package']['ValidationDetails']))
|
||||
$errors = $result['Packages']['Package']['ValidationDetails'];
|
||||
elseif (isset($result['faultcode']) && isset($result['faultstring']))
|
||||
$errors = $result['faultcode'].' : '.$result['faultstring'];
|
||||
else
|
||||
{
|
||||
$errors = array();
|
||||
|
||||
if ($error_ids = $this->getErrorsByKey($result, 'ErrorId'))
|
||||
{
|
||||
$language = new DpdPolandLanguage();
|
||||
|
||||
foreach ($error_ids as $id_error)
|
||||
$errors[] = $language->getTranslation($id_error);
|
||||
}
|
||||
elseif ($error_messages = $this->getErrorsByKey($result, 'Info'))
|
||||
{
|
||||
foreach ($error_messages as $message)
|
||||
$errors[] = $message;
|
||||
}
|
||||
|
||||
$errors = reset($errors);
|
||||
|
||||
if (!$errors)
|
||||
$errors = $this->module_instance->displayName.' : '.$this->l('Unknown error');
|
||||
}
|
||||
|
||||
if ($errors)
|
||||
{
|
||||
$errors = (array)$errors;
|
||||
$errors = (array_values($errors) === $errors) ? $errors : array($errors); // array must be multidimentional
|
||||
|
||||
foreach ($errors as $error)
|
||||
{
|
||||
if (isset($error['ValidationInfo']['Info']))
|
||||
self::$errors[] = $error['ValidationInfo']['Info'];
|
||||
elseif (isset($error['info']))
|
||||
self::$errors[] = $error['info'];
|
||||
elseif (isset($error['ValidationInfo']) && is_array($error['ValidationInfo'])) {
|
||||
$errors_formatted = reset($error['ValidationInfo']);
|
||||
|
||||
if (isset($errors_formatted['ErrorId'])) {
|
||||
$language = new DpdPolandLanguage();
|
||||
$error_message = $language->getTranslation($errors_formatted['ErrorId']);
|
||||
|
||||
if (!$error_message) {
|
||||
$error_message = isset($errors_formatted['Info']) ? $errors_formatted['Info'] :
|
||||
$this->l('Unknown error occured');
|
||||
}
|
||||
|
||||
self::$errors[] = $error_message;
|
||||
} elseif (isset($errors_formatted['Info'])) {
|
||||
self::$errors[] = $errors_formatted['Info'];
|
||||
}
|
||||
} else {
|
||||
self::$errors[] = $error;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
self::$errors[] = $errors;
|
||||
|
||||
DpdPolandLog::addError($errors);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates package remotely
|
||||
*
|
||||
* @param DpdPolandPackage $package_obj Package object
|
||||
* @param string $payerType Payer type
|
||||
* @return bool Package created successfully
|
||||
*/
|
||||
private function createRemotely(DpdPolandPackage $package_obj, $payerType = 'THIRD_PARTY')
|
||||
{
|
||||
if (!$this->prepareReceiverAddress($package_obj))
|
||||
return false;
|
||||
|
||||
$payer_number = Tools::getValue('dpdpoland_PayerNumber');
|
||||
|
||||
$this->prepareSenderAddress($package_obj->id_sender_address);
|
||||
$this->prepareServicesData($package_obj);
|
||||
|
||||
$params = array(
|
||||
'openUMLFeV3' => array(
|
||||
'packages' => array(
|
||||
'parcels' => $this->parcels,
|
||||
'payerType' => $payerType,
|
||||
'thirdPartyFID' => $payer_number,
|
||||
'receiver' => $this->receiver,
|
||||
'ref1' => $package_obj->ref1,
|
||||
'ref2' => $package_obj->ref2,
|
||||
'ref3' => _DPDPOLAND_REFERENCE3_,
|
||||
'reference' => null,
|
||||
'sender' => $this->sender,
|
||||
'services' => $this->services,
|
||||
)
|
||||
),
|
||||
'pkgNumsGenerationPolicyV1' => 'STOP_ON_FIRST_ERROR',
|
||||
'langCode' => 'PL'
|
||||
);
|
||||
|
||||
return $this->generatePackagesNumbersV4($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats receiver address and prepares it to be used via WebServices
|
||||
*
|
||||
* @param DpdPolandPackage $package_obj Package object
|
||||
* @return bool Receiver address prepared without errors
|
||||
*/
|
||||
private function prepareReceiverAddress(DpdPolandPackage $package_obj)
|
||||
{
|
||||
$address = new Address((int)$package_obj->id_address_delivery);
|
||||
|
||||
if (Validate::isLoadedObject($address))
|
||||
{
|
||||
$customer = new Customer((int)$address->id_customer);
|
||||
|
||||
if (Validate::isLoadedObject($customer))
|
||||
{
|
||||
$this->receiver = array(
|
||||
'address' => $address->address1.' '.$address->address2,
|
||||
'city' => $address->city,
|
||||
'company' => $address->company,
|
||||
'countryCode' => Country::getIsoById((int)$address->id_country),
|
||||
'email' => $customer->email,
|
||||
'fid' => null,
|
||||
'name' => $address->firstname.' '.$address->lastname,
|
||||
'phone' => isset($address->phone) && !empty(trim($address->phone)) ? $address->phone : $address->phone_mobile,
|
||||
'postalCode' => DpdPoland::convertPostcode($address->postcode)
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
self::$errors[] = $this->l('Customer does not exists');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
self::$errors[] = $this->l('Receiver address does not exists');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats sender address and prepares it to be used via WebServices
|
||||
*
|
||||
* @param null|int $id_sender_address Address ID
|
||||
*/
|
||||
private function prepareSenderAddress($id_sender_address = null)
|
||||
{
|
||||
$sender_address = new DpdPolandSenderAddress((int)$id_sender_address);
|
||||
|
||||
$this->sender = array(
|
||||
'address' => $sender_address->address,
|
||||
'city' => $sender_address->city,
|
||||
'company' => $sender_address->company,
|
||||
'countryCode' => DpdPoland::POLAND_ISO_CODE,
|
||||
'email' => $sender_address->email,
|
||||
'name' => $sender_address->name,
|
||||
'phone' => $sender_address->phone,
|
||||
'postalCode' => DpdPoland::convertPostcode($sender_address->postcode)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats data and prepares it to be used via WebServices
|
||||
*
|
||||
* @param DpdPolandPackage $package_obj Package object
|
||||
*/
|
||||
private function prepareServicesData(DpdPolandPackage $package_obj)
|
||||
{
|
||||
if ($package_obj->cod_amount !== null)
|
||||
{
|
||||
$this->services['cod'] = array(
|
||||
'amount' => $package_obj->cod_amount,
|
||||
'currency' => _DPDPOLAND_CURRENCY_ISO_
|
||||
);
|
||||
}
|
||||
|
||||
if ($package_obj->declaredValue_amount !== null)
|
||||
{
|
||||
$this->services['declaredValue'] = array(
|
||||
'amount' => $package_obj->declaredValue_amount,
|
||||
'currency' => _DPDPOLAND_CURRENCY_ISO_
|
||||
);
|
||||
}
|
||||
|
||||
if ($package_obj->cud)
|
||||
{
|
||||
$this->services['cud'] = 1;
|
||||
}
|
||||
|
||||
if ($package_obj->rod)
|
||||
{
|
||||
$this->services['rod'] = 1;
|
||||
}
|
||||
|
||||
if ($package_obj->dpde)
|
||||
{
|
||||
$this->services['dpdExpress'] = 1;
|
||||
}
|
||||
|
||||
if ($package_obj->dpdnd)
|
||||
{
|
||||
$this->services['guarantee'] = array('type' => 'DPDNEXTDAY');
|
||||
}
|
||||
|
||||
if ($package_obj->duty)
|
||||
{
|
||||
$this->services['duty'] = array(
|
||||
'amount' => $package_obj->duty_amount,
|
||||
'currency' => $package_obj->duty_currency
|
||||
);
|
||||
}
|
||||
|
||||
// DPD PUDO SERVICE DATA PREPARATION
|
||||
$order = new Order($package_obj->id_order);
|
||||
// First get pudo carrier id
|
||||
$id_pudo_carrier = Configuration::get(DpdPolandConfiguration::CARRIER_PUDO_ID);
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.5', '<')) {
|
||||
$id_order_carrier = (int)DpdPolandCarrier::getReferenceByIdCarrier((int)$order->id_carrier);
|
||||
} else {
|
||||
$carrier = new Carrier($order->id_carrier);
|
||||
$id_order_carrier = $carrier->id_reference;
|
||||
}
|
||||
|
||||
// Check if order has pudo service as carrier
|
||||
if ($id_order_carrier == $id_pudo_carrier && Tools::getValue('dpdpoland_SessionType') == 'pudo') {
|
||||
// Get pudo code from pudo_cart mappings table
|
||||
$pudoCode = Db::getInstance()->getValue('
|
||||
SELECT `pudo_code`
|
||||
FROM `'._DB_PREFIX_.'dpdpoland_pudo_cart`
|
||||
WHERE `id_cart` = '.(int)$order->id_cart.'
|
||||
');
|
||||
|
||||
if ($pudoCode) {
|
||||
$this->services['dpdPickup'] = array(
|
||||
'pudo' => $pudoCode,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects and returns sender address
|
||||
*
|
||||
* @param null|int $id_sender_address Sender address ID
|
||||
* @return array Sender address
|
||||
*/
|
||||
public function getSenderAddress($id_sender_address = null)
|
||||
{
|
||||
if (!$this->sender)
|
||||
$this->prepareSenderAddress($id_sender_address);
|
||||
|
||||
return $this->sender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates multiple labels for selected packages
|
||||
*
|
||||
* @param array $waybills Packages waybills
|
||||
* @param string $outputDocPageFormat Document page format
|
||||
* @param string $session_type Session type
|
||||
* @return bool Multiple labels generated successfully
|
||||
*/
|
||||
public function generateMultipleLabels($waybills, $outputDocPageFormat, $session_type, $outputLabelType)
|
||||
{
|
||||
if (!in_array($outputDocPageFormat, array(DpdPolandConfiguration::PRINTOUT_FORMAT_A4, DpdPolandConfiguration::PRINTOUT_FORMAT_LABEL)))
|
||||
$outputDocPageFormat = DpdPolandConfiguration::PRINTOUT_FORMAT_A4;
|
||||
|
||||
$this->prepareSenderAddress();
|
||||
|
||||
$session = array(
|
||||
'packages' => array(
|
||||
'parcels' => array(
|
||||
|
||||
)
|
||||
),
|
||||
'sessionType' => $session_type
|
||||
);
|
||||
|
||||
foreach ($waybills as $waybill) {
|
||||
$session['packages']['parcels'][] = array('waybill' => $waybill);
|
||||
}
|
||||
|
||||
$params = array(
|
||||
'dpdServicesParamsV1' => array(
|
||||
'policy' => 'IGNORE_ERRORS',
|
||||
'session' => $session
|
||||
),
|
||||
'outputDocFormatV1' => 'PDF',
|
||||
'outputDocPageFormatV1' => $outputDocPageFormat,
|
||||
'outputLabelType' => $outputLabelType,
|
||||
'pickupAddress' => $this->sender
|
||||
);
|
||||
|
||||
if (!$result = $this->generateSpedLabelsV4($params)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($result['session']) && $result['session']['statusInfo']['status'] == 'OK')
|
||||
{
|
||||
return $result['documentData'];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isset($result['session']['statusInfo']['status'])) {
|
||||
self::$errors[] = $result['session']['statusInfo']['status'];
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$error = isset($result['session']['packages']['statusInfo']['description']) ?
|
||||
$result['session']['packages']['statusInfo']['description'] :
|
||||
$result['session']['statusInfo']['description'];
|
||||
self::$errors[] = $error;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates labels for package
|
||||
*
|
||||
* @param DpdPolandPackage $package Package object
|
||||
* @param string $outputDocFormat Document format
|
||||
* @param string $outputDocPageFormat Document page format
|
||||
* @param $outputLabelType
|
||||
* @param string $policy Policy type
|
||||
* @return bool Labels generated successfully
|
||||
*/
|
||||
public function generateLabels(DpdPolandPackage $package, $outputDocFormat, $outputDocPageFormat, $policy, $outputLabelType)
|
||||
{
|
||||
if (!in_array($outputDocPageFormat, array(DpdPolandConfiguration::PRINTOUT_FORMAT_A4, DpdPolandConfiguration::PRINTOUT_FORMAT_LABEL)))
|
||||
$outputDocPageFormat = DpdPolandConfiguration::PRINTOUT_FORMAT_A4;
|
||||
|
||||
$this->prepareSenderAddress();
|
||||
|
||||
$params = array(
|
||||
'dpdServicesParamsV1' => array(
|
||||
'policy' => $policy,
|
||||
'session' => array(
|
||||
'sessionId' => (int)$package->sessionId,
|
||||
'sessionType' => $package->getSessionType()
|
||||
)
|
||||
),
|
||||
'outputDocFormatV1' => $outputDocFormat,
|
||||
'outputDocPageFormatV1' => $outputDocPageFormat,
|
||||
'outputLabelType' => $outputLabelType,
|
||||
'pickupAddress' => $this->sender
|
||||
);
|
||||
|
||||
if (!$result = $this->generateSpedLabelsV4($params))
|
||||
return false;
|
||||
|
||||
if (isset($result['session']) && $result['session']['statusInfo']['status'] == 'OK')
|
||||
{
|
||||
$package->labels_printed = 1;
|
||||
$package->update();
|
||||
return $result['documentData'];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isset($result['session']['statusInfo']['status'])) {
|
||||
self::$errors[] = $result['session']['statusInfo']['status'];
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$error = isset($result['session']['packages']['statusInfo']['description']) ?
|
||||
$result['session']['packages']['statusInfo']['description'] :
|
||||
$result['session']['statusInfo']['description'];
|
||||
self::$errors[] = $error;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates multiple labels for selected packages
|
||||
*
|
||||
* @param array $package_ids Packages IDs
|
||||
* @param string $outputDocFormat Document format
|
||||
* @param string $outputDocPageFormat Document page format
|
||||
* @param $outputLabelType
|
||||
* @param string $policy Policy type
|
||||
* @return bool Labels generated successfully
|
||||
*/
|
||||
public function generateLabelsForMultiplePackages($package_ids, $outputDocFormat, $outputDocPageFormat, $policy, $outputLabelType)
|
||||
{
|
||||
$sessionType = '';
|
||||
$packages = array();
|
||||
|
||||
foreach ($package_ids as $id_package_ws)
|
||||
{
|
||||
$package = new DpdPolandPackage((int)$id_package_ws);
|
||||
|
||||
if (!$sessionType || $sessionType == $package->getSessionType())
|
||||
$sessionType = $package->getSessionType();
|
||||
else
|
||||
{
|
||||
self::$errors[] = $this->l('Manifests of DOMESTIC shipments cannot be mixed with INTERNATIONAL shipments');
|
||||
return false;
|
||||
}
|
||||
|
||||
$packages[] = array(
|
||||
'packageId' => (int)$id_package_ws
|
||||
);
|
||||
}
|
||||
|
||||
$this->prepareSenderAddress();
|
||||
|
||||
$params = array(
|
||||
'dpdServicesParamsV1' => array(
|
||||
'policy' => $policy,
|
||||
'session' => array(
|
||||
'packages' => $packages,
|
||||
'sessionType' => $sessionType
|
||||
)
|
||||
),
|
||||
'outputDocFormatV1' => $outputDocFormat,
|
||||
'outputDocPageFormatV1' => $outputDocPageFormat,
|
||||
'outputLabelType' => $outputLabelType,
|
||||
'pickupAddress' => $this->sender
|
||||
);
|
||||
|
||||
if (!$result = $this->generateSpedLabelsV4($params))
|
||||
return false;
|
||||
|
||||
if (isset($result['session']['statusInfo']['status']) && $result['session']['statusInfo']['status'] == 'OK')
|
||||
{
|
||||
foreach ($packages as $id_package_ws)
|
||||
{
|
||||
$package = new DpdPolandPackage($id_package_ws);
|
||||
$package->labels_printed = 1;
|
||||
$package->update();
|
||||
}
|
||||
|
||||
return $result['documentData'];
|
||||
}
|
||||
else
|
||||
{
|
||||
$packages = $result['session']['statusInfo'];
|
||||
$packages = (array_values($packages) === $packages) ? $packages : array($packages); // array must be multidimentional
|
||||
|
||||
foreach ($packages as $package)
|
||||
if (isset($package['description']))
|
||||
self::$errors[] = $package['description'];
|
||||
elseif (isset($package['status']))
|
||||
self::$errors[] = $package['status'];
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
273
modules/dpdpoland/controllers/packageList.controller.php
Normal file
273
modules/dpdpoland/controllers/packageList.controller.php
Normal file
@@ -0,0 +1,273 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandPackageListController Responsible for packages list view and actions
|
||||
*/
|
||||
class DpdPolandPackageListController extends DpdPolandController
|
||||
{
|
||||
/**
|
||||
* Default list sorting criteria
|
||||
*/
|
||||
const DEFAULT_ORDER_BY = 'date_add';
|
||||
|
||||
/**
|
||||
* Default list sorting way
|
||||
*/
|
||||
const DEFAULT_ORDER_WAY = 'desc';
|
||||
|
||||
/**
|
||||
* Current file name
|
||||
*/
|
||||
const FILENAME = 'packageList.controller';
|
||||
|
||||
/**
|
||||
* Prints manifests from packages list
|
||||
*
|
||||
* @param object $module_instance Module instance
|
||||
* @return bool Manifest printed successfully
|
||||
*/
|
||||
public static function printManifest($module_instance)
|
||||
{
|
||||
$cookie = Context::getContext()->cookie;
|
||||
|
||||
if (isset($cookie->dpdpoland_packages_ids))
|
||||
{
|
||||
if (version_compare(_PS_VERSION_, '1.5', '<'))
|
||||
$package_ids = unserialize(Context::getContext()->cookie->dpdpoland_packages_ids);
|
||||
else
|
||||
$package_ids = Tools::unSerialize(Context::getContext()->cookie->dpdpoland_packages_ids);
|
||||
|
||||
unset($cookie->dpdpoland_packages_ids);
|
||||
$cookie->write();
|
||||
|
||||
$separated_packages = DpdPolandPackage::separatePackagesBySession($package_ids);
|
||||
$international_packages = $separated_packages['INTERNATIONAL'];
|
||||
$domestic_packages = $separated_packages['DOMESTIC'];
|
||||
$manifest_ids = array();
|
||||
|
||||
if ($international_packages)
|
||||
$manifest_ids[] = DpdPolandManifest::getManifestIdWsByPackageIdWs($international_packages[0]);
|
||||
|
||||
if ($domestic_packages)
|
||||
$manifest_ids[] = DpdPolandManifest::getManifestIdWsByPackageIdWs($domestic_packages[0]);
|
||||
|
||||
require_once(_DPDPOLAND_CONTROLLERS_DIR_.'manifestList.controller.php');
|
||||
|
||||
$manifest_controller = new DpdPolandManifestListController();
|
||||
|
||||
return $manifest_controller->printManifest($manifest_ids);
|
||||
}
|
||||
|
||||
if ($package_ids = Tools::getValue('PackagesBox'))
|
||||
{
|
||||
if (!DpdPolandManifest::validateSenderAddresses($package_ids))
|
||||
{
|
||||
$error_message = $module_instance->l('Manifests can not have different sender addresses', self::FILENAME);
|
||||
$error = $module_instance->displayError($error_message);
|
||||
|
||||
return $module_instance->outputHTML($error);
|
||||
}
|
||||
|
||||
$separated_packages = DpdPolandPackage::separatePackagesBySession($package_ids);
|
||||
$international_packages = $separated_packages['INTERNATIONAL'];
|
||||
$domestic_packages = $separated_packages['DOMESTIC'];
|
||||
|
||||
if ($international_packages)
|
||||
{
|
||||
$manifest = new DpdPolandManifest;
|
||||
|
||||
if (!$manifest->generateMultiple($international_packages))
|
||||
{
|
||||
$error = $module_instance->displayError(reset(DpdPolandManifestWS::$errors));
|
||||
|
||||
return $module_instance->outputHTML($error);
|
||||
}
|
||||
}
|
||||
|
||||
if ($domestic_packages)
|
||||
{
|
||||
$manifest = new DpdPolandManifest;
|
||||
|
||||
if (!$manifest->generateMultiple($domestic_packages))
|
||||
{
|
||||
$error = $module_instance->displayError(reset(DpdPolandManifestWS::$errors));
|
||||
|
||||
return $module_instance->outputHTML($error);
|
||||
}
|
||||
}
|
||||
|
||||
$cookie->dpdpoland_packages_ids = serialize($package_ids);
|
||||
$redirect_uri = $module_instance->module_url.'&menu=packages_list';
|
||||
|
||||
die(Tools::redirectAdmin($redirect_uri));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates label from packages list
|
||||
*
|
||||
* @param object $package Package object
|
||||
* @param object $module_instance Module instance
|
||||
* @param array $packages Packages IDs
|
||||
* @param string $printout_format Printout format (label, A4)
|
||||
* @param string $filename Label file name
|
||||
* @return bool Label created successfully
|
||||
*/
|
||||
private static function createLabelPDFDocument($package, $module_instance, $packages, $printout_format, $filename)
|
||||
{
|
||||
if (!$pdf_file_contents = $package->generateLabelsForMultiplePackages($packages, 'PDF', $printout_format))
|
||||
{
|
||||
$error = $module_instance->displayError(reset(DpdPolandPackageWS::$errors));
|
||||
|
||||
return $error;
|
||||
}
|
||||
|
||||
if (file_exists(_PS_MODULE_DIR_.'dpdpoland/'.$filename) && !unlink(_PS_MODULE_DIR_.'dpdpoland/'.$filename))
|
||||
{
|
||||
$error_message = $module_instance->l('Could not delete old PDF file. Please check module folder permissions', self::FILENAME);
|
||||
$error = $module_instance->displayError($error_message);
|
||||
|
||||
return $error;
|
||||
}
|
||||
|
||||
$international_pdf = fopen(_PS_MODULE_DIR_.'dpdpoland/'.$filename, 'w');
|
||||
|
||||
if (!$international_pdf)
|
||||
{
|
||||
$error_message = $module_instance->l('Could not create PDF file. Please check module folder permissions', self::FILENAME);
|
||||
$error = $module_instance->displayError($error_message);
|
||||
|
||||
return $error;
|
||||
}
|
||||
|
||||
fwrite($international_pdf, $pdf_file_contents);
|
||||
fclose($international_pdf);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints multiple label for selected packages
|
||||
*
|
||||
* @param string $printout_format Printout format (label, A4)
|
||||
* @return mixed Error messages
|
||||
*/
|
||||
public static function printLabels($printout_format)
|
||||
{
|
||||
$module_instance = Module::getinstanceByName('dpdpoland');
|
||||
|
||||
if ($package_ids = Tools::getValue('PackagesBox'))
|
||||
{
|
||||
$package = new DpdPolandPackage;
|
||||
|
||||
$separated_packages = DpdPolandPackage::separatePackagesBySession($package_ids);
|
||||
$international_packages = $separated_packages['INTERNATIONAL'];
|
||||
$domestic_packages = $separated_packages['DOMESTIC'];
|
||||
|
||||
if ($international_packages)
|
||||
{
|
||||
$result = self::createLabelPDFDocument($package, $module_instance, $international_packages, $printout_format, 'international_labels.pdf');
|
||||
|
||||
if ($result !== true)
|
||||
return $module_instance->outputHTML($result);
|
||||
}
|
||||
|
||||
if ($domestic_packages)
|
||||
{
|
||||
$result = self::createLabelPDFDocument($package, $module_instance, $domestic_packages, $printout_format, 'domestic_labels.pdf');
|
||||
|
||||
if ($result !== true)
|
||||
return $module_instance->outputHTML($result);
|
||||
}
|
||||
|
||||
include_once(_PS_MODULE_DIR_.'dpdpoland/libraries/PDFMerger/PDFMerger.php');
|
||||
|
||||
$pdf = new PDFMerger;
|
||||
|
||||
if ($international_packages && $domestic_packages)
|
||||
{
|
||||
if (file_exists(_PS_MODULE_DIR_.'dpdpoland/labels_multisession.pdf') && !unlink(_PS_MODULE_DIR_.'dpdpoland/labels_multisession.pdf'))
|
||||
{
|
||||
$error_message = $module_instance->l('Could not delete old PDF file. Please check module folder permissions', self::FILENAME);
|
||||
$error = $module_instance->displayError($error_message);
|
||||
|
||||
return $module_instance->outputHTML($error);
|
||||
}
|
||||
|
||||
$international_pdf_path = _PS_MODULE_DIR_.'dpdpoland/international_labels.pdf';
|
||||
$domestic_pdf_path = _PS_MODULE_DIR_.'dpdpoland/domestic_labels.pdf';
|
||||
$multisession_pdf_path = _PS_MODULE_DIR_.'dpdpoland/labels_multisession.pdf';
|
||||
$pdf->addPDF($international_pdf_path, 'all')->addPDF($domestic_pdf_path, 'all')->merge('file', $multisession_pdf_path);
|
||||
}
|
||||
|
||||
ob_end_clean();
|
||||
header('Content-type: application/pdf');
|
||||
header('Content-Disposition: attachment; filename="labels_'.time().'.pdf"');
|
||||
|
||||
if ($international_packages && $domestic_packages)
|
||||
readfile(_PS_MODULE_DIR_.'dpdpoland/labels_multisession.pdf');
|
||||
elseif ($international_packages)
|
||||
readfile(_PS_MODULE_DIR_.'dpdpoland/international_labels.pdf');
|
||||
elseif ($domestic_packages)
|
||||
readfile(_PS_MODULE_DIR_.'dpdpoland/domestic_labels.pdf');
|
||||
else
|
||||
{
|
||||
$error_message = $module_instance->l('No labels were found', self::FILENAME);
|
||||
$error = $module_instance->displayError($error_message);
|
||||
return $module_instance->outputHTML($error);
|
||||
}
|
||||
|
||||
self::deletePDFFiles();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes existing PDF files which were used to merge them onto a single document
|
||||
*/
|
||||
private static function deletePDFFiles()
|
||||
{
|
||||
$labels = array('labels_multisession', 'international_labels', 'domestic_labels');
|
||||
|
||||
foreach ($labels as $label)
|
||||
if (file_exists(_PS_MODULE_DIR_.'dpdpoland/'.$label.'.pdf') && is_writable(_PS_MODULE_DIR_.'dpdpoland/'.$label.'.pdf'))
|
||||
unlink(_PS_MODULE_DIR_.'dpdpoland/'.$label.'.pdf');
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares list data to be displayed in page
|
||||
*
|
||||
* @return string Page content in HTML
|
||||
*/
|
||||
public function getList()
|
||||
{
|
||||
$keys_array = array('date_add', 'id_order', 'package_number', 'count_parcel', 'receiver', 'country', 'postcode', 'city', 'address');
|
||||
$this->prepareListData($keys_array, 'Packages', new DpdPolandPackage, self::DEFAULT_ORDER_BY, self::DEFAULT_ORDER_WAY, 'packages_list');
|
||||
$this->context->smarty->assign('order_link', 'index.php?controller=AdminOrders&vieworder&token='.Tools::getAdminTokenLite('AdminOrders'));
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.6', '>='))
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/package_list_16.tpl');
|
||||
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/package_list.tpl');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandParcelHistoryController Responsible for parcel history list page view and actions
|
||||
*/
|
||||
class DpdPolandParcelHistoryController extends DpdPolandController
|
||||
{
|
||||
/**
|
||||
* Default list sorting criteria
|
||||
*/
|
||||
const DEFAULT_ORDER_BY = 'date_add';
|
||||
|
||||
/**
|
||||
* Default list sorting way
|
||||
*/
|
||||
const DEFAULT_ORDER_WAY = 'desc';
|
||||
|
||||
/**
|
||||
* @var string Tracking URL address
|
||||
*/
|
||||
private $tracking_link = 'https://tracktrace.dpd.com.pl/parcelDetails?typ=1&p1=';
|
||||
|
||||
/**
|
||||
* Prepares list data to be displayed in page
|
||||
*
|
||||
* @return string Page content in HTML
|
||||
*/
|
||||
public function getList()
|
||||
{
|
||||
$keys_array = array('id_order', 'id_parcel', 'receiver', 'country', 'postcode', 'city', 'address', 'date_add');
|
||||
$this->prepareListData($keys_array, 'ParcelHistories', new DpdPolandParcel(),
|
||||
self::DEFAULT_ORDER_BY, self::DEFAULT_ORDER_WAY, 'parcel_history_list');
|
||||
$this->context->smarty->assign('tracking_link', $this->tracking_link);
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.6', '<'))
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/parcel_history_list.tpl');
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/parcel_history_list_16.tpl');
|
||||
}
|
||||
}
|
||||
345
modules/dpdpoland/controllers/pickup.webservice.php
Normal file
345
modules/dpdpoland/controllers/pickup.webservice.php
Normal file
@@ -0,0 +1,345 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandPickup Responsible for Arrange Pickup actions management
|
||||
*/
|
||||
class DpdPolandPickup extends DpdPolandWS
|
||||
{
|
||||
/**
|
||||
* @var int Pickup ID
|
||||
*/
|
||||
public $id_pickup;
|
||||
|
||||
/**
|
||||
* @var date|string Pickup date
|
||||
*/
|
||||
public $pickupDate;
|
||||
|
||||
/**
|
||||
* @var date|string Pickup time
|
||||
*/
|
||||
public $pickupTime;
|
||||
|
||||
/**
|
||||
* @var string Order type (international, domestic)
|
||||
*/
|
||||
public $orderType;
|
||||
|
||||
/**
|
||||
* @var bool Pickup for envelope
|
||||
*/
|
||||
public $dox = false;
|
||||
|
||||
/**
|
||||
* @var int|string Documents count
|
||||
*/
|
||||
public $doxCount;
|
||||
|
||||
/**
|
||||
* @var bool Pickup for parcels
|
||||
*/
|
||||
public $parcels = false;
|
||||
|
||||
/**
|
||||
* @var int|string Parcels count
|
||||
*/
|
||||
public $parcelsCount;
|
||||
|
||||
/**
|
||||
* @var float|string Parcels weight
|
||||
*/
|
||||
public $parcelsWeight;
|
||||
|
||||
/**
|
||||
* @var float|string Parcel max weight
|
||||
*/
|
||||
public $parcelMaxWeight;
|
||||
|
||||
/**
|
||||
* @var float|string Parcel max height
|
||||
*/
|
||||
public $parcelMaxHeight;
|
||||
|
||||
/**
|
||||
* @var float|string Parcel max depth
|
||||
*/
|
||||
public $parcelMaxDepth;
|
||||
|
||||
/**
|
||||
* @var float|string Parcel max width
|
||||
*/
|
||||
public $parcelMaxWidth;
|
||||
|
||||
/**
|
||||
* @var bool Pickup for pallet
|
||||
*/
|
||||
public $pallet = false;
|
||||
|
||||
/**
|
||||
* @var int|string Pallets count
|
||||
*/
|
||||
public $palletsCount;
|
||||
|
||||
/**
|
||||
* @var float|string Pallets weight
|
||||
*/
|
||||
public $palletsWeight;
|
||||
|
||||
/**
|
||||
* @var float|string Pallet max weight
|
||||
*/
|
||||
public $palletMaxWeight;
|
||||
|
||||
/**
|
||||
* @var float|string Pallet max height
|
||||
*/
|
||||
public $palletMaxHeight;
|
||||
|
||||
public $customerName;
|
||||
|
||||
public $customerCompany;
|
||||
|
||||
public $customerPhone;
|
||||
|
||||
/**
|
||||
* Is it a standard parcel
|
||||
* Always must be true
|
||||
*/
|
||||
const STANDARD_PARCEL = true;
|
||||
|
||||
/**
|
||||
* Makes web services call to arrange a pickup
|
||||
*
|
||||
* @param string $operationType Operation type
|
||||
* @param bool $waybillsReady Are waybills ready
|
||||
* @return bool Pickup arranged successfully
|
||||
*/
|
||||
public function arrange($operationType = 'INSERT', $waybillsReady = true)
|
||||
{
|
||||
list($pickupTimeFrom, $pickupTimeTo) = explode('-', $this->pickupTime);
|
||||
|
||||
$settings = new DpdPolandConfiguration;
|
||||
|
||||
$params = array(
|
||||
'dpdPickupParamsV3' => array(
|
||||
'operationType' => $operationType,
|
||||
'orderType' => $this->orderType,
|
||||
'pickupCallSimplifiedDetails' => array(
|
||||
'packagesParams' => $this->getPackagesParams(),
|
||||
'pickupCustomer' => array(
|
||||
'customerFullName' => $this->customerName,
|
||||
'customerName' => $this->customerCompany,
|
||||
'customerPhone' => $this->customerPhone
|
||||
),
|
||||
'pickupPayer' => array(
|
||||
'payerName' => $settings->client_name,
|
||||
'payerNumber' => $settings->client_number
|
||||
),
|
||||
'pickupSender' => $this->getSenderAddress()
|
||||
),
|
||||
'pickupDate' => $this->pickupDate,
|
||||
'pickupTimeFrom' => $pickupTimeFrom,
|
||||
'pickupTimeTo' => $pickupTimeTo,
|
||||
'waybillsReady' => $waybillsReady
|
||||
)
|
||||
);
|
||||
|
||||
$result = $this->packagesPickupCallV4($params);
|
||||
|
||||
if (isset($result['statusInfo']) && isset($result['statusInfo']['errorDetails']))
|
||||
{
|
||||
$errors = $result['statusInfo']['errorDetails'];
|
||||
$errors = (array_values($errors) === $errors) ? $errors : array($errors); // array must be multidimentional
|
||||
foreach ($errors as $error)
|
||||
self::$errors[] = sprintf($this->l('Error code: %s, fields: %s'), $error['code'], $error['fields']);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($result['orderNumber']))
|
||||
{
|
||||
$this->id_pickup = (int)$result['orderNumber'];
|
||||
Configuration::updateValue('DPDPOLAND_CONFIGURATION_OK', true);
|
||||
return true;
|
||||
}
|
||||
self::$errors[] = $this->l('Order number is undefined');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns formatted sender address
|
||||
*
|
||||
* @return array Sender address
|
||||
*/
|
||||
private function getSenderAddress()
|
||||
{
|
||||
$id_sender_address = (int)Tools::getValue('sender_address_selection');
|
||||
$sender_address = new DpdPolandSenderAddress((int)$id_sender_address);
|
||||
|
||||
return array(
|
||||
'senderAddress' => $sender_address->address,
|
||||
'senderCity' => $sender_address->city,
|
||||
'senderFullName' => $sender_address->name,
|
||||
'senderName' => $sender_address->name,
|
||||
'senderPhone' => $sender_address->phone,
|
||||
'senderPostalCode' => DpdPoland::convertPostcode($sender_address->postcode),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create array with pickup packages (envelopes, pallets or parcels) data for web services call
|
||||
*
|
||||
* @return array Formatted WebServices parameters
|
||||
*/
|
||||
private function getPackagesParams()
|
||||
{
|
||||
return array_merge(
|
||||
$this->getEnvelopesParams(),
|
||||
$this->getPalletsParams(),
|
||||
$this->getParcelsParams()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array with envelopes data prepared for web services call
|
||||
* In order to send envelopes, both conditions must be met:
|
||||
* 1. Envelopes chosen
|
||||
* 2. Envelopes count > 0
|
||||
* Otherwise envelopes count will be 0.
|
||||
* 'dox' parameter always must bet set to 0 - requirement by DPD Poland
|
||||
*
|
||||
* @return array Envelopes parameters
|
||||
*/
|
||||
private function getEnvelopesParams()
|
||||
{
|
||||
$result = array(
|
||||
'dox' => 0, // always false even if envelopes are sent
|
||||
'doxCount' => 0
|
||||
);
|
||||
|
||||
if ($this->dox && (int)$this->doxCount)
|
||||
$result['doxCount'] = (int)$this->doxCount;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array with envelopes data prepared for web services call
|
||||
* In order to send pallets, both conditions must be met:
|
||||
* 1. Pallets chosen
|
||||
* 2. Pallets count > 0
|
||||
*
|
||||
* @return array Pallets parameters
|
||||
*/
|
||||
private function getPalletsParams()
|
||||
{
|
||||
$result = array(
|
||||
'pallet' => 0,
|
||||
'palletMaxHeight' => '',
|
||||
'palletMaxWeight' => '',
|
||||
'palletsCount' => 0,
|
||||
'palletsWeight' => ''
|
||||
);
|
||||
|
||||
if ($this->pallet && (int)$this->palletsCount)
|
||||
{
|
||||
$result['pallet'] = 1;
|
||||
$result['palletMaxHeight'] = $this->palletMaxHeight;
|
||||
$result['palletMaxWeight'] = $this->palletMaxWeight;
|
||||
$result['palletsCount'] = (int)$this->palletsCount;
|
||||
$result['palletsWeight'] = $this->palletsWeight;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array with parcels data prepared for web services call
|
||||
* If envelopes or pallets are sent without parcels then parcels should have all params set to 1
|
||||
* In order to send parcels, both conditions must be met:
|
||||
* 1. Parcels chosen
|
||||
* 2. Parcels count > 0
|
||||
*
|
||||
* @return array Parcels parameters
|
||||
*/
|
||||
private function getParcelsParams()
|
||||
{
|
||||
$result = array(
|
||||
'parcelsCount' => 0,
|
||||
'standardParcel' => self::STANDARD_PARCEL, // Always must be true
|
||||
'parcelMaxDepth' => '',
|
||||
'parcelMaxHeight' => '',
|
||||
'parcelMaxWeight' => '',
|
||||
'parcelMaxWidth' => '',
|
||||
'parcelsWeight' => ''
|
||||
);
|
||||
|
||||
// If no parcels but envelopes or pallets are chosen then parcels all values should be 1
|
||||
if (!$this->parcels && ($this->dox || $this->pallet))
|
||||
{
|
||||
$result['parcelsCount'] = 1;
|
||||
$result['standardParcel'] = self::STANDARD_PARCEL; // Always must be true
|
||||
$result['parcelMaxDepth'] = 1;
|
||||
$result['parcelMaxHeight'] = 1;
|
||||
$result['parcelMaxWeight'] = 1;
|
||||
$result['parcelMaxWidth'] = 1;
|
||||
$result['parcelsWeight'] = 1;
|
||||
}
|
||||
elseif ($this->parcels && (int)$this->parcelsCount)
|
||||
{
|
||||
$result['parcelsCount'] = (int)$this->parcelsCount;
|
||||
$result['standardParcel'] = self::STANDARD_PARCEL; // Always must be true
|
||||
$result['parcelMaxDepth'] = $this->parcelMaxDepth;
|
||||
$result['parcelMaxHeight'] = $this->parcelMaxHeight;
|
||||
$result['parcelMaxWeight'] = $this->parcelMaxWeight;
|
||||
$result['parcelMaxWidth'] = $this->parcelMaxWidth;
|
||||
$result['parcelsWeight'] = $this->parcelsWeight;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available pickup time frames for a particular date
|
||||
*
|
||||
* @return bool Are any available time frames
|
||||
*/
|
||||
public function getCourierTimeframes()
|
||||
{
|
||||
$id_sender_address = (int)Tools::getValue('sender_address_selection');
|
||||
$sender_address = new DpdPolandSenderAddress((int)$id_sender_address);
|
||||
|
||||
$params = array(
|
||||
'senderPlaceV1' => array(
|
||||
'countryCode' => DpdPoland::POLAND_ISO_CODE,
|
||||
'zipCode' => DpdPoland::convertPostcode($sender_address->postcode)
|
||||
)
|
||||
);
|
||||
|
||||
$result = $this->getCourierOrderAvailabilityV1($params);
|
||||
|
||||
if (!isset($result['ranges']) && !self::$errors)
|
||||
self::$errors[] = $this->l('Cannot get TimeFrames from webservices. Please check if sender\'s postal code is typed in correctly');
|
||||
|
||||
return (isset($result['ranges'])) ? $result['ranges'] : false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandSenderAddressListController Responsible for
|
||||
*/
|
||||
class DpdPolandPickupHistoryListController extends DpdPolandController
|
||||
{
|
||||
/**
|
||||
* Default list sorting criteria
|
||||
*/
|
||||
const DEFAULT_ORDER_BY = 'order_number';
|
||||
|
||||
/**
|
||||
* Default list sorting way
|
||||
*/
|
||||
const DEFAULT_ORDER_WAY = 'desc';
|
||||
|
||||
/**
|
||||
* Current file name
|
||||
*/
|
||||
const FILENAME = 'manifestList.controller';
|
||||
|
||||
/**
|
||||
* Prepares list data to be displayed in page
|
||||
*
|
||||
* @return string Page content in HTML
|
||||
*/
|
||||
public function getListHTML()
|
||||
{
|
||||
$keys_array = array('id_pickup_history', 'order_number', 'sender_address', 'sender_company', 'sender_name', 'sender_phone', 'pickup_date', 'pickup_time', 'type', 'envelope', 'package', 'pallet');
|
||||
$this->prepareListData($keys_array, 'PickupHistory', new DpdPolandPickupHistory, self::DEFAULT_ORDER_BY, self::DEFAULT_ORDER_WAY, 'pickup_history');
|
||||
|
||||
$this->context->smarty->assign('form_url', $this->module_instance->module_url . '&menu=pickup_history_form');
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.6', '<'))
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_ . 'admin/pickup_history_list.tpl');
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_ . 'admin/pickup_history_list_16.tpl');
|
||||
}
|
||||
|
||||
/**
|
||||
* Save pickup
|
||||
* @param $pickup
|
||||
* @param $id_sender_address
|
||||
* @throws PrestaShopException
|
||||
*/
|
||||
public function save($pickup)
|
||||
{
|
||||
$id_sender_address = (int)Tools::getValue('sender_address_selection');
|
||||
$sender_address = new DpdPolandSenderAddress((int)$id_sender_address);
|
||||
|
||||
$pickup_history = new DpdPolandPickupHistory;
|
||||
$pickup_history->order_number = $pickup->id_pickup;
|
||||
$pickup_history->sender_address = $sender_address->alias;
|
||||
$pickup_history->sender_company = $pickup->customerCompany;
|
||||
$pickup_history->sender_name = $pickup->customerName;
|
||||
$pickup_history->sender_phone = $pickup->customerPhone;
|
||||
$pickup_history->pickup_date = $pickup->pickupDate;
|
||||
$pickup_history->pickup_time = $pickup->pickupTime;
|
||||
$pickup_history->type = $pickup->orderType;
|
||||
$pickup_history->envelope = $pickup->doxCount;
|
||||
$pickup_history->package = $pickup->parcelsCount;
|
||||
$pickup_history->package_weight_all = $pickup->parcelsWeight;
|
||||
$pickup_history->package_heaviest_weight = $pickup->parcelMaxWeight;
|
||||
$pickup_history->package_heaviest_width = $pickup->parcelMaxWidth;
|
||||
$pickup_history->package_heaviest_length = $pickup->parcelMaxDepth;
|
||||
$pickup_history->package_heaviest_height = $pickup->parcelMaxHeight;
|
||||
$pickup_history->pallet = $pickup->palletsCount;
|
||||
$pickup_history->pallet_weight = $pickup->palletsWeight;
|
||||
$pickup_history->pallet_heaviest_weight = $pickup->palletMaxWeight;
|
||||
$pickup_history->pallet_heaviest_height = $pickup->palletMaxHeight;
|
||||
$pickup_history->id_shop = (int)$this->context->shop->id;
|
||||
|
||||
$pickup_history->save();
|
||||
}
|
||||
}
|
||||
183
modules/dpdpoland/controllers/senderAddressForm.controller.php
Normal file
183
modules/dpdpoland/controllers/senderAddressForm.controller.php
Normal file
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandSenderAddressFormController Responsible for sender address form page view and actions
|
||||
*/
|
||||
class DpdPolandSenderAddressFormController extends DpdPolandController
|
||||
{
|
||||
/**
|
||||
* Current file name
|
||||
*/
|
||||
const FILENAME = 'configuration.controller';
|
||||
|
||||
/**
|
||||
* @var int Sender address
|
||||
*/
|
||||
private $id_sender_address = 0;
|
||||
|
||||
/**
|
||||
* DpdPolandSenderAddressFormController class constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
if (Tools::isSubmit('editSenderAddress')) {
|
||||
$this->id_sender_address = (int)Tools::getValue('id_sender_address');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reacts at page actions
|
||||
*/
|
||||
public function controllerActions()
|
||||
{
|
||||
if (Tools::isSubmit('saveSenderAddress')) {
|
||||
$this->saveSenderAddress();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares form data to be displayed in page
|
||||
*
|
||||
* @return string Form data in HTML
|
||||
*/
|
||||
public function getForm()
|
||||
{
|
||||
$sender_address = new DpdPolandSenderAddress((int)$this->id_sender_address);
|
||||
|
||||
$this->context->smarty->assign(array(
|
||||
'object' => $sender_address,
|
||||
'saveAction' => $this->module_instance->module_url.'&menu=sender_address_form'
|
||||
));
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.6', '>='))
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/sender_address_form_16.tpl');
|
||||
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/sender_address_form.tpl');
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves sender address in database
|
||||
*/
|
||||
private function saveSenderAddress()
|
||||
{
|
||||
$id_sender_address = (int)Tools::getValue('id_sender_address');
|
||||
|
||||
$address = new DpdPolandSenderAddress((int)$id_sender_address);
|
||||
$address->alias = Tools::getValue('alias');
|
||||
$address->company = Tools::getValue('company');
|
||||
$address->name = Tools::getValue('name');
|
||||
$address->phone = Tools::getValue('phone');
|
||||
$address->postcode = Tools::getValue('postcode');
|
||||
$address->city = Tools::getValue('city');
|
||||
$address->address = Tools::getValue('address');
|
||||
$address->email = Tools::getValue('email');
|
||||
$address->id_shop = (int)$this->context->shop->id;
|
||||
|
||||
if ($this->validateSenderAddress($address)) {
|
||||
$messages_controller = new DpdPolandMessagesController();
|
||||
|
||||
if ($address->save()) {
|
||||
$messages_controller->setSuccessMessage(
|
||||
$this->module_instance->l('Address saved successfully', self::FILENAME)
|
||||
);
|
||||
} else {
|
||||
$messages_controller->setErrorMessage(
|
||||
$this->module_instance->l('Could not save address', self::FILENAME)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Tools::redirectAdmin($this->module_instance->module_url.'&menu=sender_address');
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if sender address fields are valid
|
||||
*
|
||||
* @param DpdPolandSenderAddress $address Sender address object
|
||||
* @return bool Sender address is valid
|
||||
*/
|
||||
private function validateSenderAddress(DpdPolandSenderAddress $address)
|
||||
{
|
||||
$errors = array();
|
||||
|
||||
if ($address->alias === '') {
|
||||
$errors[] = $this->module_instance->l('Alias name is required', self::FILENAME);
|
||||
} elseif (!Validate::isGenericName($address->alias)) {
|
||||
$errors[] = $this->module_instance->l('Alias name is invalid', self::FILENAME);
|
||||
}
|
||||
|
||||
if ($address->company === '') {
|
||||
$errors[] = $this->module_instance->l('Company name is required', self::FILENAME);
|
||||
} elseif (!Validate::isGenericName($address->company)) {
|
||||
$errors[] = $this->module_instance->l('Company name is invalid', self::FILENAME);
|
||||
}
|
||||
|
||||
if ($address->name === '') {
|
||||
$errors[] = $this->module_instance->l('Name / Surname is required', self::FILENAME);
|
||||
} elseif (!Validate::isName($address->name)) {
|
||||
$errors[] = $this->module_instance->l('Name / Surname is invalid', self::FILENAME);
|
||||
}
|
||||
|
||||
if ($address->phone === '') {
|
||||
$errors[] = $this->module_instance->l('Phone is required', self::FILENAME);
|
||||
} elseif (!Validate::isPhoneNumber($address->phone)) {
|
||||
$errors[] = $this->module_instance->l('Phone number is invalid', self::FILENAME);
|
||||
}
|
||||
|
||||
if ($address->postcode === '') {
|
||||
$errors[] = $this->module_instance->l('Postcode is required', self::FILENAME);
|
||||
} elseif (!Validate::isPostCode($address->postcode)) {
|
||||
$errors[] = $this->module_instance->l('Postcode is invalid', self::FILENAME);
|
||||
}
|
||||
|
||||
if ($address->city === '') {
|
||||
$errors[] = $this->module_instance->l('City is required', self::FILENAME);
|
||||
} elseif (!Validate::isCityName($address->city)) {
|
||||
$errors[] = $this->module_instance->l('City is invalid', self::FILENAME);
|
||||
}
|
||||
|
||||
if ($address->address === '') {
|
||||
$errors[] = $this->module_instance->l('Address is required', self::FILENAME);
|
||||
} elseif (!Validate::isAddress($address->address)) {
|
||||
$errors[] = $this->module_instance->l('Address is invalid', self::FILENAME);
|
||||
}
|
||||
|
||||
if ($address->email === '') {
|
||||
$errors[] = $this->module_instance->l('Email is required', self::FILENAME);
|
||||
} elseif (!Validate::isEmail($address->email)) {
|
||||
$errors[] = $this->module_instance->l('Email is invalid', self::FILENAME);
|
||||
}
|
||||
|
||||
if ($errors) {
|
||||
$messages_controller = new DpdPolandMessagesController();
|
||||
$messages_controller->setErrorMessage(serialize($errors));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandSenderAddressListController Responsible for
|
||||
*/
|
||||
class DpdPolandSenderAddressListController extends DpdPolandController
|
||||
{
|
||||
/**
|
||||
* Default list sorting criteria
|
||||
*/
|
||||
const DEFAULT_ORDER_BY = 'date_add';
|
||||
|
||||
/**
|
||||
* Default list sorting way
|
||||
*/
|
||||
const DEFAULT_ORDER_WAY = 'desc';
|
||||
|
||||
/**
|
||||
* Current file name
|
||||
*/
|
||||
const FILENAME = 'manifestList.controller';
|
||||
|
||||
/**
|
||||
* Reacts at page actions
|
||||
*/
|
||||
public function controllerActions()
|
||||
{
|
||||
if (Tools::isSubmit('deleteSenderAddress')) {
|
||||
$this->deleteSenderAddress();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares list data to be displayed in page
|
||||
*
|
||||
* @return string Page content in HTML
|
||||
*/
|
||||
public function getListHTML()
|
||||
{
|
||||
$keys_array = array('id_sender_address', 'company', 'name', 'city', 'email', 'alias');
|
||||
$this->prepareListData($keys_array, 'SenderAddress', new DpdPolandSenderAddress, self::DEFAULT_ORDER_BY, self::DEFAULT_ORDER_WAY, 'sender_address');
|
||||
|
||||
$this->context->smarty->assign('form_url', $this->module_instance->module_url.'&menu=sender_address_form');
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.6', '<'))
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/sender_address_list.tpl');
|
||||
return $this->context->smarty->fetch(_DPDPOLAND_TPL_DIR_.'admin/sender_address_list_16.tpl');
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes sender address
|
||||
*/
|
||||
private function deleteSenderAddress()
|
||||
{
|
||||
$id_sender_address = (int)Tools::getValue('id_sender_address');
|
||||
$sender_address = new DpdPolandSenderAddress((int)$id_sender_address);
|
||||
$messages_controller = new DpdPolandMessagesController();
|
||||
|
||||
if ($sender_address->delete()) {
|
||||
$messages_controller->setSuccessMessage(
|
||||
$this->module_instance->l('Address deleted successfully', self::FILENAME)
|
||||
);
|
||||
} else {
|
||||
$messages_controller->setErrorMessage(
|
||||
$this->module_instance->l('Could not delete address', self::FILENAME)
|
||||
);
|
||||
}
|
||||
|
||||
Tools::redirectAdmin($this->module_instance->module_url.'&menu=sender_address');
|
||||
}
|
||||
}
|
||||
146
modules/dpdpoland/controllers/service.php
Normal file
146
modules/dpdpoland/controllers/service.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
/**
|
||||
* Class DpdPolandService Responsible for DPD services (carriers) management
|
||||
*/
|
||||
class DpdPolandService
|
||||
{
|
||||
/**
|
||||
* DPD carriers images directory name
|
||||
*/
|
||||
const IMG_DIR = 'DPD_services';
|
||||
|
||||
/**
|
||||
* DPD carriers images extension
|
||||
*/
|
||||
const IMG_EXTENSION = 'jpg';
|
||||
|
||||
/**
|
||||
* @var Module Module instance
|
||||
*/
|
||||
protected $module_instance;
|
||||
|
||||
/**
|
||||
* DpdPolandService class constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->module_instance = Module::getInstanceByName('dpdpoland');
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete existing carrier
|
||||
*
|
||||
* @param int $id_carrier ID of carrier to delete
|
||||
* @return boolean Carrier deleted successfully
|
||||
*/
|
||||
protected static function deleteCarrier($id_carrier)
|
||||
{
|
||||
if (!$id_carrier)
|
||||
return true;
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.5', '<'))
|
||||
{
|
||||
$id_carrier = (int)DpdPolandCarrier::getIdCarrierByReference((int)$id_carrier);
|
||||
$carrier = new Carrier((int)$id_carrier);
|
||||
}
|
||||
else
|
||||
$carrier = Carrier::getCarrierByReference($id_carrier);
|
||||
|
||||
if (!Validate::isLoadedObject($carrier))
|
||||
return true;
|
||||
|
||||
if ($carrier->deleted)
|
||||
return true;
|
||||
|
||||
$carrier->deleted = 1;
|
||||
|
||||
return (bool)$carrier->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns PrestaShop customer groups for carrier on PS 1.4
|
||||
*
|
||||
* @param int $id_carrier Carrier ID
|
||||
* @param array $groups Groups IDs
|
||||
* @return bool Groups successfully assigned for carrier
|
||||
*/
|
||||
protected static function setGroups14($id_carrier, $groups)
|
||||
{
|
||||
foreach ($groups as $id_group)
|
||||
if (!Db::getInstance()->execute('
|
||||
INSERT INTO `'._DB_PREFIX_.'carrier_group`
|
||||
(`id_carrier`, `id_group`)
|
||||
VALUES
|
||||
("'.(int)$id_carrier.'", "'.(int)$id_group.'")
|
||||
'))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns customer groups for carrier on PS 1.5 and PS 1.6
|
||||
*
|
||||
* @param Carrier $carrier Carrier object
|
||||
* @return bool Customer groups successfully assigned for carrier
|
||||
*/
|
||||
protected static function assignCustomerGroupsForCarrier($carrier)
|
||||
{
|
||||
$groups = array();
|
||||
|
||||
foreach (Group::getGroups((int)Context::getContext()->language->id) as $group)
|
||||
$groups[] = $group['id_group'];
|
||||
|
||||
if (version_compare(_PS_VERSION_, '1.5.5', '<'))
|
||||
{
|
||||
if (!self::setGroups14((int)$carrier->id, $groups))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
if (!$carrier->setGroups($groups))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns carrier according to it's reference
|
||||
*
|
||||
* @param string|int $reference Carrier reference
|
||||
* @return bool|Carrier Carrier object
|
||||
*/
|
||||
public static function getCarrierByReference($reference)
|
||||
{
|
||||
if (version_compare(_PS_VERSION_, '1.5', '<'))
|
||||
{
|
||||
$id_carrier = (int)DpdPolandCarrier::getIdCarrierByReference($reference);
|
||||
$carrier = new Carrier((int)$id_carrier);
|
||||
}
|
||||
else
|
||||
$carrier = Carrier::getCarrierByReference((int)$reference);
|
||||
|
||||
return $carrier;
|
||||
}
|
||||
}
|
||||
295
modules/dpdpoland/controllers/webservice.php
Normal file
295
modules/dpdpoland/controllers/webservice.php
Normal file
@@ -0,0 +1,295 @@
|
||||
<?php
|
||||
/**
|
||||
* 2019 DPD Polska Sp. z o.o.
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* prestashop@dpd.com.pl so we can send you a copy immediately.
|
||||
*
|
||||
* @author DPD Polska Sp. z o.o.
|
||||
* @copyright 2019 DPD Polska Sp. z o.o.
|
||||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
* International Registered Trademark & Property of DPD Polska Sp. z o.o.
|
||||
*/
|
||||
|
||||
if (!defined('_PS_VERSION_'))
|
||||
exit;
|
||||
|
||||
require_once(_PS_MODULE_DIR_.'dpdpoland/dpdpoland.lang.php');
|
||||
require_once(_DPDPOLAND_CLASSES_DIR_.'Log.php');
|
||||
|
||||
/**
|
||||
* Class DpdPolandWS Responsible for WebService calls management
|
||||
*/
|
||||
class DpdPolandWS extends DpdPolandController
|
||||
{
|
||||
/**
|
||||
* @var SoapClient Instance of SoapClient class
|
||||
*/
|
||||
private $client;
|
||||
|
||||
/**
|
||||
* @var array WebServices parameters
|
||||
*/
|
||||
private $params = array();
|
||||
|
||||
/**
|
||||
* @var string Last called function payload
|
||||
*/
|
||||
private $lastCalledFunctionPayload;
|
||||
|
||||
/**
|
||||
* @var string Last called function name
|
||||
*/
|
||||
private $lastCalledFunctionName;
|
||||
|
||||
/**
|
||||
* @var array Last called function arguments
|
||||
*/
|
||||
private $lastCalledFunctionArgs = array();
|
||||
|
||||
/**
|
||||
* Current file name
|
||||
*/
|
||||
const FILENAME = 'dpdpoland.ws';
|
||||
|
||||
/**
|
||||
* Debug file name setting name
|
||||
*/
|
||||
const DEBUG_FILENAME = 'DPDPOLAND_DEBUG_FILENAME';
|
||||
|
||||
/**
|
||||
* Bool Debug data should be displayed in PopUp window or logs file
|
||||
*/
|
||||
const DEBUG_POPUP = false;
|
||||
|
||||
/**
|
||||
* Debug file name symbols count
|
||||
*/
|
||||
const DEBUG_FILENAME_LENGTH = 16;
|
||||
|
||||
/**
|
||||
* DpdPolandWS class constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$settings = new DpdPolandConfiguration;
|
||||
|
||||
$this->params = array(
|
||||
'authDataV1' => array(
|
||||
'login' => $settings->login,
|
||||
'masterFid' => $settings->customer_fid,
|
||||
'password' => $settings->password
|
||||
)
|
||||
);
|
||||
|
||||
try
|
||||
{
|
||||
$this->client = new SoapClient($settings->ws_url, array('trace' => true));
|
||||
return $this->client;
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
DpdPolandLog::addError('URL: '.$settings->ws_url.' - '.$e->getMessage());
|
||||
self::$errors[] = $e->getMessage();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function used to make a request via WebServices
|
||||
*
|
||||
* @param string $function_name Function name
|
||||
* @param array $arguments Function arguments
|
||||
* @return array|bool Response
|
||||
*/
|
||||
public function __call($function_name, $arguments)
|
||||
{
|
||||
$result = null;
|
||||
|
||||
$this->lastCalledFunctionName = $function_name;
|
||||
$this->lastCalledFunctionArgs = $arguments;
|
||||
|
||||
if (isset($arguments[0]) && is_array($arguments[0]))
|
||||
{
|
||||
$this->params = array_merge($this->params, $arguments[0]);
|
||||
|
||||
try
|
||||
{
|
||||
if (!$result = $this->client->$function_name($this->params))
|
||||
self::$errors[] = $this->l('Could not connect to webservice server. Please check webservice URL');
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
DpdPolandLog::addError('function_name: '.$function_name.' - message: '.$e->getMessage());
|
||||
self::$errors[] = $e->getMessage();
|
||||
}
|
||||
|
||||
if (isset($result->return))
|
||||
$result = $result->return;
|
||||
|
||||
if (isset($result->faultstring))
|
||||
self::$errors[] = $result->faultstring;
|
||||
|
||||
if (_DPDPOLAND_DEBUG_MODE_)
|
||||
$this->debug($result);
|
||||
|
||||
return $this->objectToArray($result);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Response should be used as array
|
||||
*
|
||||
* @param object|array $response Response from WebServices
|
||||
* @return array Formatted response
|
||||
*/
|
||||
private function objectToArray($response)
|
||||
{
|
||||
if (!is_object($response) && !is_array($response))
|
||||
return $response;
|
||||
|
||||
return array_map(array($this, 'objectToArray'), (array)$response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates debug file if it is still not created
|
||||
*
|
||||
* @return string Debug file name
|
||||
*/
|
||||
private function createDebugFileIfNotExists()
|
||||
{
|
||||
if ((!$debug_filename = Configuration::get(self::DEBUG_FILENAME)) || !$this->isDebugFileName($debug_filename))
|
||||
{
|
||||
$debug_filename = Tools::passwdGen(self::DEBUG_FILENAME_LENGTH).'.html';
|
||||
Configuration::updateValue(self::DEBUG_FILENAME, $debug_filename);
|
||||
}
|
||||
|
||||
if (!file_exists(_DPDPOLAND_MODULE_DIR_.$debug_filename))
|
||||
{
|
||||
$file = fopen(_DPDPOLAND_MODULE_DIR_.$debug_filename, 'w');
|
||||
fclose($file);
|
||||
}
|
||||
|
||||
return $debug_filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if string can be used as debug file name
|
||||
*
|
||||
* @param string $debug_filename Debug file name
|
||||
* @return bool String can be used as debug file name
|
||||
*/
|
||||
private function isDebugFileName($debug_filename)
|
||||
{
|
||||
return Tools::strlen($debug_filename) == (int)self::DEBUG_FILENAME_LENGTH + 5 && preg_match('#^[a-zA-Z0-9]+\.html$#', $debug_filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds data into debug file
|
||||
*
|
||||
* @param null $result Request / Response / Errors from / to WebsServices
|
||||
*/
|
||||
private function debug($result = null)
|
||||
{
|
||||
$debug_html = '';
|
||||
|
||||
if ($this->lastCalledFunctionName)
|
||||
{
|
||||
$debug_html .= '<h2 style="padding: 10px 0 10px 0; display: block; border-top: solid 2px #000000; border-bottom: solid 2px #000000;">
|
||||
['.date('Y-m-d H:i:s').']</h2><h2>Function \''.$this->lastCalledFunctionName.'\' params
|
||||
</h2><pre>';
|
||||
$debug_html .= print_r($this->lastCalledFunctionArgs, true);
|
||||
$debug_html .= '</pre>';
|
||||
}
|
||||
|
||||
if ($this->lastCalledFunctionPayload = (string)$this->client->__getLastRequest())
|
||||
$debug_html .= '<h2>Request</h2><pre>'.$this->displayPayload().'</pre>';
|
||||
|
||||
if ($result)
|
||||
{
|
||||
if ($err = $this->getError())
|
||||
$debug_html .= '<h2>Error</h2><pre>'.$err.'</pre>';
|
||||
else
|
||||
{
|
||||
$result = print_r($result, true);
|
||||
|
||||
$debug_html .= '<h2>Response</h2><pre>';
|
||||
$debug_html .= strip_tags($result);
|
||||
$debug_html .= '</pre>';
|
||||
}
|
||||
}
|
||||
else
|
||||
$debug_html .= '<h2>Errors</h2><pre>'.print_r(self::$errors, true).'</pre>';
|
||||
|
||||
if ($debug_html)
|
||||
{
|
||||
$debug_filename = $this->createDebugFileIfNotExists();
|
||||
|
||||
$current_content = Tools::file_get_contents(_DPDPOLAND_MODULE_DIR_.$debug_filename);
|
||||
@file_put_contents(_DPDPOLAND_MODULE_DIR_.$debug_filename, $debug_html.$current_content, LOCK_EX);
|
||||
|
||||
if (self::DEBUG_POPUP)
|
||||
{
|
||||
echo '
|
||||
<div id="_invertus_ws_console" style="display:none">
|
||||
'.$debug_html.'
|
||||
</div>
|
||||
<script language=javascript>
|
||||
_invertus_ws_console = window.open("","Invertus WS debugging console","width=680,height=600,resizable,scrollbars=yes");
|
||||
_invertus_ws_console.document.write("<HTML><TITLE>Invertus WS debugging console</TITLE><BODY bgcolor=#ffffff>");
|
||||
_invertus_ws_console.document.write(document.getElementById("_invertus_ws_console").innerHTML);
|
||||
_invertus_ws_console.document.write("</BODY></HTML>");
|
||||
_invertus_ws_console.document.close();
|
||||
document.getElementById("_invertus_ws_console").remove();
|
||||
</script>';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Only for debugging purposes
|
||||
*
|
||||
* @return string Last called function payload
|
||||
*/
|
||||
private function displayPayload()
|
||||
{
|
||||
$xml = preg_replace('/(>)(<)(\/*)/', "$1\n$2$3", $this->lastCalledFunctionPayload);
|
||||
$token = strtok($xml, "\n");
|
||||
$result = '';
|
||||
$pad = 0;
|
||||
$matches = array();
|
||||
while ($token !== false)
|
||||
{
|
||||
if (preg_match('/.+<\/\w[^>]*>$/', $token, $matches))
|
||||
$indent = 0;
|
||||
elseif (preg_match('/^<\/\w/', $token, $matches))
|
||||
{
|
||||
$pad -= 4;
|
||||
$indent = 0;
|
||||
}
|
||||
elseif (preg_match('/^<\w[^>]*[^\/]>.*$/', $token, $matches))
|
||||
$indent = 4;
|
||||
else
|
||||
$indent = 0;
|
||||
|
||||
$line = str_pad($token, Tools::strlen($token) + $pad, ' ', STR_PAD_LEFT);
|
||||
$result .= $line."\n";
|
||||
$token = strtok("\n");
|
||||
$pad += $indent;
|
||||
}
|
||||
|
||||
return htmlentities($result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user