bebeboutik/modules/soflexibilite/functions/set_carriers_shipping_cost.php
Marion Muszynski ffbb3232b9 modif soflex
2016-05-09 17:43:57 +02:00

161 lines
6.3 KiB
PHP

<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to a commercial license from Common-Services Co., Ltd.
* Use, copy, modification or distribution of this source file without written
* license agreement from the SARL SMC is strictly forbidden.
* In order to obtain a license, please contact us: contact@common-services.com
* ...........................................................................
* INFORMATION SUR LA LICENCE D'UTILISATION
*
* L'utilisation de ce fichier source est soumise a une licence commerciale
* concedee par la societe Common-Services Co., Ltd.
* Toute utilisation, reproduction, modification ou distribution du present
* fichier source sans contrat de licence ecrit de la part de la Common-Services Co. Ltd. est
* expressement interdite.
* Pour obtenir une licence, veuillez contacter Common-Services Co., Ltd. a l'adresse: contact@common-services.com
* ...........................................................................
*
* @package soflexibilite
* @author Alexandre D. <debuss-a>
* @copyright Copyright (c) 2011-2015 Common Services Co Ltd - 90/25 Sukhumvit 81 - 10260 Bangkok - Thailand
* @license Commercial license
* Support by mail : support.soflexibilite@common-services.com
*/
require_once(dirname(__FILE__).'/../../../config/config.inc.php');
require_once(dirname(__FILE__).'/../../../init.php');
require_once dirname(__FILE__).'/../classes/SoFlexibiliteTools.php';
require_once dirname(__FILE__).'/../classes/SoFlexibiliteRangeWeight.php';
ob_start();
Context::getContext()->shop->setContext(Shop::CONTEXT_ALL);
/*$shipping_cost_json_link = 'https://dl.dropboxusercontent.com/u/60698220/tarifs.json';
$shipping_cost = SoFlexibiliteTools::file_get_contents($shipping_cost_json_link);
$status = true;
try {
$shipping_cost = Tools::jsonDecode($shipping_cost, true);
} catch (Exception $e) {*/
$status = false;
$shipping_cost = null;
/*}*/
if (is_array($shipping_cost) && count($shipping_cost)) {
$id_carriers = Configuration::getMultiple(
array('SOFLEXIBILITE_DOM_ID', 'SOFLEXIBILITE_DOS_ID', 'SOFLEXIBILITE_BPR_ID', 'SOFLEXIBILITE_A2P_ID'),
null,
null,
null
);
if (!is_array($id_carriers) || !count($id_carriers)) {
$status = false;
} else {
foreach ($id_carriers as $key => $id_carrier) {
switch ($key) {
case 'SOFLEXIBILITE_DOM_ID':
$mode = 'DOM';
break;
case 'SOFLEXIBILITE_DOS_ID':
$mode = 'DOS';
break;
case 'SOFLEXIBILITE_BPR_ID':
$mode = 'BPR';
break;
case 'SOFLEXIBILITE_A2P_ID':
$mode = 'A2P';
break;
default:
$mode = null;
}
$carrier = new Carrier((int)$id_carrier);
if (!Validate::isLoadedObject($carrier)) {
printf('Impossible de charger le transporteur [%s].<br>', $mode);
continue;
}
// Delete carrier previous range weight
$range_weights = RangeWeight::getRanges($carrier->id);
foreach ($range_weights as $range_weight) {
$range = new RangeWeight((int)$range_weight['id_range_weight']);
if (Validate::isLoadedObject($range)) {
$range->delete();
}
}
// Add the new range weight
foreach ($shipping_cost as $iso_country => $ranges) {
if (!in_array($iso_country, array('FR', 'BE', 'LU', 'NL', 'DE', 'GB', 'ES'))) {
printf('[%s] not an accepted ISO', $iso_country);
continue;
}
$id_zone = Country::getIdZone((int)Country::getByIso($iso_country));
if (!$id_zone) {
printf('Error, unable to get id_zone for country [%s] ==> Skipped<br>', $iso_country);
continue;
} elseif ($id_zone && !$carrier->getZone((int)$id_zone)) {
$carrier->addZone((int)$id_zone);
}
$price_list = array();
$previous_range = 0;
foreach ($ranges as $weight => $costs) {
if (!RangeWeight::rangeExist($carrier->id, $previous_range, min(30, $weight + 0.001))) {
$range_w = new SoFlexibiliteRangeWeight();
$range_w->id_carrier = $carrier->id;
$range_w->delimiter1 = (float)$previous_range;
$range_w->delimiter2 = (float)min(30, $weight + 0.001);
$range_w->addWithoutCallToAddDeliveryPrice();
$previous_range = $range_w->delimiter2;
} else {
$range_w = new stdClass();
$range_w->id = (int)Db::getInstance()->getValue(
'SELECT `id_range_weight`
FROM `'._DB_PREFIX_.'range_weight` rw
WHERE `id_carrier` = '.(int)$carrier->id.'
AND `delimiter1` = '.(float)$previous_range.'
AND `delimiter2` = '.(float)min(30, $weight + 0.001)
);
$previous_range = (float)min(30, $weight + 0.001);
}
// Add price to range
if (Validate::isLoadedObject($range_w)) {
$price_list[] = array(
'id_shop' => null,
'id_shop_group' => null,
'id_range_price' => null,
'id_range_weight' => (int)$range_w->id,
'id_carrier' => (int)$carrier->id,
'id_zone' => (int)$id_zone,
'price' => (float)$costs[$mode]
);
}
}
$carrier->addDeliveryPrice($price_list, true);
}
}
}
}
$output = ob_get_clean();
$callback = Tools::getValue('callback');
$json = array(
'output' => $output,
'status' => $status
);
die($callback.'('.Tools::jsonEncode($json).')');