Files
2019-11-17 19:14:07 +01:00

196 lines
6.3 KiB
PHP

<?php
/**
* 2008-today Mediacom87
*
* NOTICE OF LICENSE
*
* Read in the module
*
* @author Mediacom87 <support@mediacom87.net>
* @copyright 2008-2016 Mediacom87
* @license define in the module
*/
class MedTntTrackingParcelClass
{
public function __construct($module)
{
$this->module = $module;
}
public function isoCode($domain = false)
{
$iso = $this->module->context->language->iso_code;
if ($iso == 'fr') {
return 'fr';
} elseif ($domain) {
return 'com';
} else {
return 'en';
}
}
public function medJsonModuleFile()
{
$conf = Configuration::getMultiple(array('MED_JSON_TIME', 'MED_JSON_FILE'));
if (!isset($conf['MED_JSON_TIME']) || $conf['MED_JSON_TIME'] < (time() - 604800)) {
Configuration::updateValue('MED_JSON_TIME', time());
$url_api = 'https://api-addons.prestashop.com/'
._PS_VERSION_
.'/contributor/all_products/'
.$this->module->module_key
.'/'
.$this->module->context->language->iso_code
.'/'
.$this->module->context->country->iso_code;
$conf['MED_JSON_FILE'] = Tools::file_get_contents($url_api);
Configuration::updateValue('MED_JSON_FILE', $conf['MED_JSON_FILE']);
}
$modules = Tools::jsonDecode($conf['MED_JSON_FILE'], true);
if (!is_array($modules) || isset($modules['errors'])) {
Configuration::updateValue('MED_JSON_TIME', 0);
return null;
} else {
return $modules;
}
}
public function medJsonModuleRate($id = false, $hash = false)
{
if (!$id || !$hash) {
return null;
}
$conf = Tools::jsonDecode(Configuration::get('MED_A_'.$id), true);
if (!isset($conf['time']) || $conf['time'] < (time() - 604800)) {
$conf['time'] = time();
$iso = $this->module->context->language->iso_code;
$country = $this->module->context->country->iso_code;
$url_api = 'https://api-addons.prestashop.com/'._PS_VERSION_
.'/contributor/product/'.$hash.'/'.$iso.'/'.$country;
$result = Tools::file_get_contents($url_api);
$module = Tools::jsonDecode($result, true);
if (isset($module['products'][0]['nbRates'])) {
$conf['nbRates'] = $module['products'][0]['nbRates'];
$conf['avgRate'] = $module['products'][0]['avgRate']*2*10;
$datas = Tools::jsonEncode($conf);
Configuration::updateValue('MED_A_'.$id, $datas);
} else {
$conf = null;
}
}
if (!is_array($conf)) {
Configuration::deleteByName('MED_A_'.$id);
return null;
} else {
return $conf;
}
}
public function medTestDir($dir, $recursive = false)
{
if (!is_writable($dir) || !$dh = opendir($dir)) {
return false;
}
if ($recursive) {
while (($file = readdir($dh)) !== false) {
if (@filetype($dir.$file) == 'dir' && $file != '.' && $file != '..') {
if (!self::testDir($dir.$file, true)) {
return false;
}
}
}
}
closedir($dh);
return true;
}
public function trackingByConsignments($tracking_id, $username = 'webservices@tnt.fr', $password = 'test')
{
$authheader = sprintf('
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>%s</wsse:Username>
<wsse:Password>%s</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>', htmlspecialchars($username), htmlspecialchars($password));
$authvars = new SoapVar($authheader, XSD_ANYXML);
$header = new SoapHeader("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", $authvars);
$file = "http://www.tnt.fr/service/tracking?wsdl";
$soapclient = new SoapClient($file, array(
'trace'=> 1,
'stream_context'=>stream_context_create(array(
'http'=> array(
'user_agent' => 'PHP/SOAP',
'accept' => 'application/xml'
)
))
));
$soapclient->__setSOAPHeaders(array($header));
$reponse = $soapclient->trackingByConsignment(array('parcelNumber' => $tracking_id));
if (property_exists($reponse, 'Parcel')) {
return $reponse->Parcel;
} else {
return null;
}
}
public function getOrderIdsByStatus($id_order_state)
{
$sql = 'SELECT o.id_order
FROM '._DB_PREFIX_.'orders o
LEFT JOIN '._DB_PREFIX_.'order_carrier oc ON o.id_order = oc.id_order
INNER JOIN '._DB_PREFIX_.'carrier c ON o.id_carrier = c.id_carrier
WHERE o.`current_state` = ' . (int)$id_order_state . '
AND c.url LIKE "%tnt%"
AND (o.shipping_number != "" OR oc.tracking_number != "")
AND DATE(o.date_upd) < NOW()
' . Shop::addSqlRestriction(false, 'o') . '
ORDER BY date_upd ASC';
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
$orders = array();
foreach ($result as $order) {
$orders[] = (int)$order['id_order'];
}
return $orders;
}
public function getIdOrderCarrier($idorder)
{
return Db::getInstance()->getValue('
SELECT `id_order_carrier`
FROM `'._DB_PREFIX_.'order_carrier`
WHERE `id_order` = ' . (int)$idorder);
}
public function changeIOrderStatus($id_order, $id_status)
{
$history = new OrderHistory();
$history->id_order = $id_order;
$history->changeIdOrderState((int)$id_status, (int)$id_order);
$history->addWithemail();
}
public function addDeliveryInformations($order_id, $message)
{
$msg = new Message();
$msg->message = $message;
$msg->id_order = (int)$order_id;
$msg->private = 1;
$msg->add();
}
}