<?php 
 
$___notjson = 1;

ini_set('display_errors', true);
error_reporting(E_ALL);
header('Content-Type: text/html; charser=utf-8');
date_default_timezone_set ('Europe/Moscow');

$key = '';
foreach ( $_COOKIE as $name=>$value ) {
    if ( substr($name, -3) == 'SID' ) {
        $key = md5('cdek' . $value);
        break;
    }
}

if ( !isset($_REQUEST['k']) || empty($_REQUEST['k']) || $_REQUEST['k'] != $key ) {
    die('Доступ запрещен');
}

if ( !isset($_REQUEST['price']) && !isset($_REQUEST['radioId']) ) exit;
$radioId = $_REQUEST['radioId'];

require_once 'classes/Cdek_class.php';
require_once 'classes/Api_class.php';
$cdek = new Cdek();
$options = $cdek->includeOptions();
if (empty($options['cdekId'])) $cdek->calcError('Не задан город отправителя.');

$weight = 0;
$w = 0;
$h = 0;
$l = 0;
$volume = 0;
$corobCost = 0;
$allW = array();
$allL = array();

if ( !empty($options['consumer_key']) && !empty($options['consumer_secret']) && !empty($options['oauth_token']) && !empty($options['oauth_token_secret']) && !empty($options['url']) ) {

    if ( $options['dimensions1'] == 0 && $options['dimensionsW'] != 0 && $options['dimensionsH'] != 0 && $options['dimensionsL'] != 0
         || $options['dimensions1'] == 1 && $options['dimensionsWHL'] != 0 ) {

        $api = new Api(array(
            'consumer_key' => $options['consumer_key'],
            'consumer_secret' => $options['consumer_secret'],
            'oauth_token' => $options['oauth_token'],
            'oauth_token_secret' => $options['oauth_token_secret'],
            'url' => $options['url']
        ));
        //$apiResult = $api->get('/shop/basket/'); //print_r($apiResult);

		$allUrl = explode(',', $_POST['url']);
		$allCnt = explode(',', $_POST['cnt']);
		$paramArray = array();
		$k = 0;
		foreach ( $allUrl as $value ) {
			$id = explode('/', $value);
			$apiResult = $api->get('/shop/request', array(
				'page' => 'viewgoods',
				'id' => $id[2],
			));//print_r($apiResult); exit;
			if ( isset( $apiResult['error']['msg'] ) ) $cdek->calcError('<b>Ошибка uAPI: ' . $apiResult['error']['msg'] . '</b>');

			if ( strpos($apiResult['success']['entry_weight']['weight_raw'], ',') ) $apiResult['success']['entry_weight']['weight_raw'] = str_replace(',', '.', $apiResult['success']['entry_weight']['weight_raw']);
			if ($options['typeweight'] == 0) {
			    $weight = $apiResult['success']['entry_weight']['weight_raw']/1000;
            }
			else $weight = $apiResult['success']['entry_weight']['weight_raw'];
			if ( $weight == 0 ) {
				if ( is_numeric($options['weightDefault']) && $options['weightDefault'] > 0 ) {
					$weight = $options['weightDefault'] / 1000;
				} else {
					$weight = 1;
				}
			}
            if ( $weight < 0.1 ) $weight = 0.1;
			if ( $options['WeightFromWeight'] == 1 ) {
				if ( $weight < 3 && is_numeric($options['wfw3']) ) $paramArray[$k]['weight'] = $weight + ($options['wfw3']/1000);
				else if ( $weight >= 3 && $weight < 6 && is_numeric($options['wfw36']) ) $paramArray[$k]['weight'] = $weight + ($options['wfw36']/1000);
				else if ( $weight >= 6 && $weight < 9 && is_numeric($options['wfw69']) ) $paramArray[$k]['weight'] = $weight + ($options['wfw69']/1000);
				else if ( $weight >= 9 && $weight < 12 && is_numeric($options['wfw912']) ) $paramArray[$k]['weight'] = $weight + ($options['wfw912']/1000);
				else if ( $weight >= 12 && $weight < 15 && is_numeric($options['wfw1215']) ) $paramArray[$k]['weight'] = $weight + ($options['wfw1215']/1000);
				else if ( $weight >= 15 && $weight < 18 && is_numeric($options['wfw1518']) ) $paramArray[$k]['weight'] = $weight + ($options['wfw1518']/1000);
				else if ( $weight >= 18 && $weight < 21 && is_numeric($options['wfw1821']) ) $paramArray[$k]['weight'] = $weight + ($options['wfw1821']/1000);
				else if ( $weight >= 21 && $weight < 25 && is_numeric($options['wfw2125']) ) $paramArray[$k]['weight'] = $weight + ($options['wfw2125']/1000);
				else if ( $weight >= 25 && $weight < 30 && is_numeric($options['wfw2530']) ) $paramArray[$k]['weight'] = $weight + ($options['wfw2530']/1000);
				else if ( $weight >= 30 && $weight < 50 && is_numeric($options['wfw3050']) ) $paramArray[$k]['weight'] = $weight + ($options['wfw3050']/1000);
				else if ( $weight >= 50 && is_numeric($options['wfw50100']) ) $paramArray[$k]['weight'] = $weight + ($options['wfw50100']/1000);
			} else {
				if ( is_numeric($options['weightpack']) && $options['weightpack'] > 0 ) $paramArray[$k]['weight'] = $weight + ($options['weightpack']/1000);
				else $paramArray[$k]['weight'] = $weight;
			}
			if ( $_REQUEST['weight'] == 0 ) $weight = $paramArray[$k]['weight'] * $_REQUEST['cnt'];
			if ( $options['priceFromWeight'] == 1 && $options['placeCheck'] == 1 ) {
				if ( $paramArray[$k]['weight'] < 3 && is_numeric($options['pfw3']) ) $corobCost = $corobCost + $options['pfw3'] * $allCnt[$k];
				else if ( $paramArray[$k]['weight'] > 3 && $paramArray[$k]['weight'] < 6 && is_numeric($options['pfw36']) ) $corobCost = $corobCost + $options['pfw36'] * $allCnt[$k];
				else if ( $paramArray[$k]['weight'] > 6 && $paramArray[$k]['weight'] < 9 && is_numeric($options['pfw69']) ) $corobCost = $corobCost + $options['pfw69'] * $allCnt[$k];
				else if ( $paramArray[$k]['weight'] > 9 && $paramArray[$k]['weight'] < 12 && is_numeric($options['pfw912']) ) $corobCost = $corobCost + $options['pfw912'] * $allCnt[$k];
				else if ( $paramArray[$k]['weight'] > 12 && $paramArray[$k]['weight'] < 15 && is_numeric($options['pfw1215']) ) $corobCost = $corobCost + $options['pfw1215'] * $allCnt[$k];
				else if ( $paramArray[$k]['weight'] > 15 && $paramArray[$k]['weight'] < 18 && is_numeric($options['pfw1518']) ) $corobCost = $corobCost + $options['pfw1518'] * $allCnt[$k];
				else if ( $paramArray[$k]['weight'] > 18 && $paramArray[$k]['weight'] < 21 && is_numeric($options['pfw1821']) ) $corobCost = $corobCost + $options['pfw1821'] * $allCnt[$k];
				else if ( $paramArray[$k]['weight'] > 21 && $paramArray[$k]['weight'] < 25 && is_numeric($options['pfw2125']) ) $corobCost = $corobCost + $options['pfw2125'] * $allCnt[$k];
				else if ( $paramArray[$k]['weight'] > 25 && $paramArray[$k]['weight'] < 30 && is_numeric($options['pfw2530']) ) $corobCost = $corobCost + $options['pfw2530'] * $allCnt[$k];
				else if ( $paramArray[$k]['weight'] > 30 && $paramArray[$k]['weight'] < 50 && is_numeric($options['pfw3050']) ) $corobCost = $corobCost + $options['pfw3050'] * $allCnt[$k];
				else if ( $paramArray[$k]['weight'] > 50 && is_numeric($options['pfw50100']) ) $corobCost = $corobCost + $options['pfw50100'] * $allCnt[$k];
			} else {
				if ( is_numeric($options['package']) && $options['package'] > 0 ) $corobCost = $corobCost + $options['package'] * $allCnt[$k];
			}

			if ( $options['volumeCheck'] == 1 ) {
				$field = $options['selectVolumeField'];
				if (!empty($apiResult['success']['entry_others']["other$field"]) ) {
					if ( strpos($apiResult['success']['entry_others']["other$field"], ',') ) $apiResult['success']['entry_others']["other$field"] = str_replace(',', '.', $apiResult['success']['entry_others']["other$field"]);
					if ($options['typeDimensions'] == 1) {
						if ( $options['placeCheck'] == 1 ) $paramArray[$k]['volume'] = $apiResult['success']['entry_others']["other$field"] / 1000000000;
						else $volume = $volume + ($apiResult['success']['entry_others']["other$field"] / 1000000000 * $allCnt[$k]);
					} else if ($options['typeDimensions'] == 2) {
						if ( $options['placeCheck'] == 1 ) $paramArray[$k]['volume'] = $apiResult['success']['entry_others']["other$field"] / 1000000;
						else $volume = $volume + ($apiResult['success']['entry_others']["other$field"] / 1000000 * $allCnt[$k]);
					} else {
						if ( $options['placeCheck'] == 1 ) $paramArray[$k]['volume'] = $apiResult['success']['entry_others']["other$field"];
						else $volume = $volume + $apiResult['success']['entry_others']["other$field"] * $allCnt[$k];
					}
				}
			} else {
				if ($options['dimensions1'] == 0) {

					$field = $options['dimensionsW'];
					if ( strpos($apiResult['success']['entry_others']["other$field"], ',') ) $apiResult['success']['entry_others']["other$field"] = str_replace(',', '.', $apiResult['success']['entry_others']["other$field"]);
					if (!empty($apiResult['success']['entry_others']["other$field"]) && $apiResult['success']['entry_others']["other$field"] != 0
						&& is_numeric($apiResult['success']['entry_others']["other$field"])
					) {
						if ($options['typeDimensions'] == 1) {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['w'] = $apiResult['success']['entry_others']["other$field"] / 10;
							else $allW[] = $apiResult['success']['entry_others']["other$field"] / 10 ;//$w = $w + ($apiResult['success']['entry_others']["other$field"] / 10 * $allCnt[$k]);
						} else if ($options['typeDimensions'] == 3) {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['w'] = $apiResult['success']['entry_others']["other$field"] * 100;
							else $allW[] = $apiResult['success']['entry_others']["other$field"] * 100;//$w = $w + ($apiResult['success']['entry_others']["other$field"] * 100 * $allCnt[$k]);
						} else {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['w'] = $apiResult['success']['entry_others']["other$field"];
							else $allW[] = $apiResult['success']['entry_others']["other$field"];//$w = $w + $apiResult['success']['entry_others']["other$field"] * $allCnt[$k];
						}
					} else if ( is_numeric($options['dD1']) && $options['dD1'] > 0 ) {
						if ( $options['placeCheck'] == 1 ) $paramArray[$k]['w'] = $options['dD1'];
						else $allW[] = $options['dD1'];//$w = $w + $options['dD1'] * $allCnt[$k];
					} else {
						if ( $options['placeCheck'] == 1 ) $paramArray[$k]['w'] = 10;
						else $allW[] = 10;// $w = $w + 10 * $allCnt[$k];
					}

					$field = $options['dimensionsH'];
					if ( strpos($apiResult['success']['entry_others']["other$field"], ',') ) $apiResult['success']['entry_others']["other$field"] = str_replace(',', '.', $apiResult['success']['entry_others']["other$field"]);
					if (!empty($apiResult['success']['entry_others']["other$field"]) && $apiResult['success']['entry_others']["other$field"] != 0
						&& is_numeric($apiResult['success']['entry_others']["other$field"])
					) {
						if ($options['typeDimensions'] == 1) {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['h'] = $apiResult['success']['entry_others']["other$field"] / 10;
							else $h = $h + ($apiResult['success']['entry_others']["other$field"] / 10 * $allCnt[$k]);
						} else if ($options['typeDimensions'] == 3) {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['h'] = $apiResult['success']['entry_others']["other$field"] * 100;
							else $h = $h + ($apiResult['success']['entry_others']["other$field"] * 100 * $allCnt[$k]);
						} else {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['h'] = $apiResult['success']['entry_others']["other$field"];
							else $h = $h + $apiResult['success']['entry_others']["other$field"] * $allCnt[$k];
						}
					} else if (is_numeric($options['dD2']) && $options['dD2'] > 0) {
						if ( $options['placeCheck'] == 1 ) $paramArray[$k]['h'] = $options['dD2'];
						else $h = $h + $options['dD2'] * $allCnt[$k];
					} else {
						if ( $options['placeCheck'] == 1 ) $paramArray[$k]['h'] = 10;
						else $h = $h + 10 * $allCnt[$k];
					}

					$field = $options['dimensionsL'];
					if ( strpos($apiResult['success']['entry_others']["other$field"], ',') ) $apiResult['success']['entry_others']["other$field"] = str_replace(',', '.', $apiResult['success']['entry_others']["other$field"]);
					if (!empty($apiResult['success']['entry_others']["other$field"]) && $apiResult['success']['entry_others']["other$field"] != 0
						&& is_numeric($apiResult['success']['entry_others']["other$field"])
					) {
						if ($options['typeDimensions'] == 1) {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['l'] = $apiResult['success']['entry_others']["other$field"] / 10;
							else $allL[] = $apiResult['success']['entry_others']["other$field"] / 10;//$l = $l + ($apiResult['success']['entry_others']["other$field"] / 10 * $allCnt[$k]);
						} else if ($options['typeDimensions'] == 3) {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['l'] = $apiResult['success']['entry_others']["other$field"] * 100;
							else $allL[] = $apiResult['success']['entry_others']["other$field"] * 100;//$l = $l + ($apiResult['success']['entry_others']["other$field"] * 100 * $allCnt[$k]);
						} else {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['l'] = $apiResult['success']['entry_others']["other$field"];
							else $allL[] = $apiResult['success']['entry_others']["other$field"];//$l = $l + $apiResult['success']['entry_others']["other$field"] * $allCnt[$k];
						}
					} else if (is_numeric($options['dD3']) && $options['dD3'] > 0) {
						if ( $options['placeCheck'] == 1 ) $paramArray[$k]['l'] = $options['dD3'];
						else $allL[] = $options['dD3'];//$l = $l + $options['dD3'] * $allCnt[$k];
					} else {
                        $allL[] = 10;//$l = $l + 10 * $allCnt[$k];
					}

				} else {
					$field = $options['dimensionsWHL'];
					$gab = explode($options['delimiter'], $apiResult['success']['entry_others']["other$field"]);

					if (!empty($gab[0]) && $gab[0] != 0) {
						if ($options['typeDimensions'] == 1) {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['w'] = $gab[0] / 10;
							else $allW[] = $gab[0] / 10;//$w = $w + ($gab[0] / 10 * $allCnt[$k]);
						} else if ($options['typeDimensions'] == 3) {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['w'] = $gab[0] * 100;
							else $allW[] = $gab[0] * 100;//$w = $w + ($gab[0] * 100 * $allCnt[$k]);
						} else {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['w'] = $gab[0];
							else $allW[] = $gab[0];//$w = $w + $gab[0] * $allCnt[$k];
						}
					} else if (is_numeric($options['dD1']) && $options['dD1'] > 0) {
						if ( $options['placeCheck'] == 1 ) $paramArray[$k]['w'] = $options['dD1'];
						else $allW[] = $options['dD1'] / 10;// $w = $w + $options['dD1'] * $allCnt[$k];
					} else {
						if ( $options['placeCheck'] == 1 ) $paramArray[$k]['w'] = 10;
						else $allW[] = 10; //$w = $w + 10 * $allCnt[$k];
					}

					if (!empty($gab[1]) && $gab[1] != 0) {
						if ($options['typeDimensions'] == 1) {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['h'] = $gab[1] / 10;
							else $h = $h + ($gab[1] / 10 * $allCnt[$k]);
						} else if ($options['typeDimensions'] == 3) {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['h'] = $gab[1] * 100;
							else $h = $h + ($gab[1] * 100 * $allCnt[$k]);
						} else {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['h'] = $gab[1];
							else $h = $h + $gab[1] * $allCnt[$k];
						}
					} else if (is_numeric($options['dD2']) && $options['dD2'] > 0) {
						if ( $options['placeCheck'] == 1 ) $paramArray[$k]['h'] = $options['dD2'];
						else $h = $h + $options['dD2'] * $allCnt[$k];
					} else {
						if ( $options['placeCheck'] == 1 ) $paramArray[$k]['h'] = 10;
						else $h = $h + 10 * $allCnt[$k];
					}

					if (!empty($gab[2]) && $gab[2] != 0) {
						if ($options['typeDimensions'] == 1) {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['l'] = $gab[2] / 10;
							else $allL[] = $gab[2] / 10;//$l = $l + ($gab[2] / 10 * $allCnt[$k]);
						} else if ($options['typeDimensions'] == 3) {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['l'] = $gab[2] * 100;
							else $allL[] = $gab[2] * 100;//$l = $l + ($gab[2] * 100 * $allCnt[$k]);
						} else {
							if ( $options['placeCheck'] == 1 ) $paramArray[$k]['l'] = $gab[2];
							else $allL[] = $gab[2];//$l = $l + $gab[2] * $allCnt[$k];
						}
					} else if (is_numeric($options['dD3']) && $options['dD3'] > 0) {
						if ( $options['placeCheck'] == 1 ) $paramArray[$k]['l'] = $options['dD3'];
						else $allL[] = $options['dD3'];//$l = $l + $options['dD3'] * $allCnt[$k];
					} else {
						if ( $options['placeCheck'] == 1 ) $paramArray[$k]['l'] = 10;
						else $allL[] = 10;//$l = $l + 10 * $allCnt[$k];
					}

				}
			}

			$paramArray[$k]['cnt'] = $allCnt[$k];
			$k++;
		}

    }

} else {
        if (is_numeric($options['dD1'])) $w = $options['dD1']; else $w = 10;
        if (is_numeric($options['dD2'])) $h = $options['dD2']; else $h = 10;
        if (is_numeric($options['dD3'])) $l = $options['dD3']; else $l = 10;
    $Dimensions = '';
}
//print_r($paramArray);

if ( isset($_REQUEST['weight']) && $_REQUEST['weight'] != 0 && $options['placeCheck'] == 0 && !isset($paramArray[0]['weight']) || empty($paramArray) ) {
    if ( strpos($_REQUEST['weight'], ',') ) $_REQUEST['weight'] = str_replace(',', '.', $_REQUEST['weight']);
    if ( $options['typeweight'] == 0 ) {
        $weight = $_REQUEST['weight']/1000;
    } else {
        $weight = $_REQUEST['weight'];
    }
	if ( $weight == 0 ) {
		$weight = 1;
		if ( is_numeric($options['weightDefault']) ) $weight = $options['weightDefault'] / 1000;
	}
    if ( $options['WeightFromWeight'] == 1 ) {
        if ( $weight < 3 && is_numeric($options['wfw3']) ) $weight = $weight + ($options['wfw3']/1000);
        else if ( $weight >= 3 && $weight < 6 && is_numeric($options['wfw36']) ) $weight = $weight + ($options['wfw36']/1000);
        else if ( $weight >= 6 && $weight < 9 && is_numeric($options['wfw69']) ) $weight = $weight + ($options['wfw69']/1000);
        else if ( $weight >= 9 && $weight < 12 && is_numeric($options['wfw912']) ) $weight = $weight + ($options['wfw912']/1000);
        else if ( $weight >= 12 && $weight < 15 && is_numeric($options['wfw1215']) ) $weight = $weight + ($options['wfw1215']/1000);
        else if ( $weight >= 15 && $weight < 18 && is_numeric($options['wfw1518']) ) $weight = $weight + ($options['wfw1518']/1000);
        else if ( $weight >= 18 && $weight < 21 && is_numeric($options['wfw1821']) ) $weight = $weight + ($options['wfw1821']/1000);
        else if ( $weight >= 21 && $weight < 25 && is_numeric($options['wfw2125']) ) $weight = $weight + ($options['wfw2125']/1000);
        else if ( $weight >= 25 && $weight < 30 && is_numeric($options['wfw2530']) ) $weight = $weight + ($options['wfw2530']/1000);
        else if ( $weight >= 30 && $weight < 50 && is_numeric($options['wfw3050']) ) $weight = $weight + ($options['wfw3050']/1000);
        else if ( $weight >= 50 && is_numeric($options['wfw50100']) ) $weight = $weight + ($options['wfw50100']/1000);
    } else {
        if ( is_numeric($options['weightpack']) ) $weight = $weight + ($options['weightpack']/1000);
    }
} else if ( $options['placeCheck'] == 0 && !isset($paramArray[0]['weight']) ) {
    $weightpack = 0;
    $weightDefault = 1;
    if ( is_numeric($options['weightpack']) ) $weightpack = $options['weightpack'] / 1000;
    if ( is_numeric($options['weightDefault']) ) $weightDefault = $options['weightDefault'] / 1000;
    $weight = $weightpack + $weightDefault;
	echo $weight;
}

if ( $options['priceFromWeight'] == 1 && $options['placeCheck'] == 0 || empty($paramArray) ) {
    if ( $weight < 3 && is_numeric($options['pfw3']) ) $corobCost = $options['pfw3'];
    else if ( $weight > 3 && $weight < 6 && is_numeric($options['pfw36']) ) $corobCost = $options['pfw36'];
    else if ( $weight > 6 && $weight < 9 && is_numeric($options['pfw69']) ) $corobCost = $options['pfw69'];
    else if ( $weight > 9 && $weight < 12 && is_numeric($options['pfw912']) ) $corobCost = $options['pfw912'];
    else if ( $weight > 12 && $weight < 15 && is_numeric($options['pfw1215']) ) $corobCost = $options['pfw1215'];
    else if ( $weight > 15 && $weight < 18 && is_numeric($options['pfw1518']) ) $corobCost = $options['pfw1518'];
    else if ( $weight > 18 && $weight < 21 && is_numeric($options['pfw1821']) ) $corobCost = $options['pfw1821'];
    else if ( $weight > 21 && $weight < 25 && is_numeric($options['pfw2125']) ) $corobCost = $options['pfw2125'];
    else if ( $weight > 25 && $weight < 30 && is_numeric($options['pfw2530']) ) $corobCost = $options['pfw2530'];
    else if ( $weight > 30 && $weight < 50 && is_numeric($options['pfw3050']) ) $corobCost = $options['pfw3050'];
    else if ( $weight > 50 && is_numeric($options['pfw50100']) ) $corobCost = $options['pfw50100'];
} else {
    if ( is_numeric($options['package']) && $options['package'] > 0 && $options['placeCheck'] == 0 ) $corobCost = $options['package'];
}

if ( empty($Dimensions) && $options['placeCheck'] == 0 || empty($paramArray) ) {
    if ( $options['dimFromWeight'] == 1 ) {
        if ( $weight < 3 && is_numeric($options['dD031']) && is_numeric($options['dD032']) && is_numeric($options['dD033']) ) {
            $w = $options['dD031']; $h = $options['dD032']; $l = $options['dD033'];
        } else if ( $weight > 3 && $weight < 6 && is_numeric($options['dD361']) && is_numeric($options['dD362']) && is_numeric($options['dD363']) ) {
            $w = $options['dD361']; $h = $options['dD362']; $l = $options['dD363'];
        } else if ( $weight > 6 && $weight < 9 && is_numeric($options['dD691']) && is_numeric($options['dD692']) && is_numeric($options['dD693']) ) {
            $w = $options['dD691']; $h = $options['dD692']; $l = $options['dD693'];
        } else if ( $weight > 9 && $weight < 12 && is_numeric($options['dD9121']) && is_numeric($options['dD9122']) && is_numeric($options['dD9123']) ) {
            $w = $options['dD9121']; $h = $options['dD9122']; $l = $options['dD9123'];
        } else if ( $weight > 12 && $weight < 15 && is_numeric($options['dD12151']) && is_numeric($options['dD12152']) && is_numeric($options['dD12153']) ) {
            $w = $options['dD12151']; $h = $options['dD12152']; $l = $options['dD12153'];
        } else if ( $weight > 15 && $weight < 18 && is_numeric($options['dD15181']) && is_numeric($options['dD15182']) && is_numeric($options['dD15183']) ) {
            $w = $options['dD15181']; $h = $options['dD15182']; $l = $options['dD15183'];
        } else if ( $weight > 18 && $weight < 21 && is_numeric($options['dD18211']) && is_numeric($options['dD18212']) && is_numeric($options['dD18213']) ) {
            $w = $options['dD18211']; $h = $options['dD18212']; $l = $options['dD18213'];
        } else if ( $weight > 21 && $weight < 25 && is_numeric($options['dD21251']) && is_numeric($options['dD21252']) && is_numeric($options['dD21253']) ) {
            $w = $options['dD21251']; $h = $options['dD21252']; $l = $options['dD21253'];
        } else if ( $weight > 25 && $weight < 30 && is_numeric($options['dD25301']) && is_numeric($options['dD25302']) && is_numeric($options['dD25303']) ) {
            $w = $options['dD25301']; $h = $options['dD25302']; $l = $options['dD25303'];
        } else if ( $weight > 30 && $weight < 50 && is_numeric($options['dD30501']) && is_numeric($options['dD30502']) && is_numeric($options['dD30503']) ) {
            $w = $options['dD30501']; $h = $options['dD30502']; $l = $options['dD30503'];
        } else if ( $weight > 50 && is_numeric($options['dDlast1']) && is_numeric($options['dDlast2']) && is_numeric($options['dDlast3']) ) {
            $w = $options['dDlast1']; $h = $options['dDlast2']; $l = $options['dDlast3'];
        }
    }
}

if ( $options['placeCheck'] == 0 || empty($paramArray) ) {
    if ($w <= 0) $w = 10;
    if ($h <= 0) $h = 10;
    if ($l <= 0) $l = 10;
}

if ( !empty($allL) && !empty($allW) ) {
    $w = max($allW); //echo $w . '-';
    $l = max($allL); //echo $l; echo '-' . $h;
}

if ( !isset( $_REQUEST['clientId'] ) ) {
    $point = $cdek->getClientId($_REQUEST['id']);
    $clientId = $point['cityCode'];
} else {
    $clientId = $_REQUEST['clientId'];
    $point = '';
}

if ( !empty($options['account']) && !empty($options['password']) ) {
    $cdek->setAuth($options['account'], $options['password']);
}
$cdek->setMyCity($options['cdekId']);
$cdek->setClientCity($clientId);
if ( $options['userDelType'] == 0 ) {
    $cdek->setDeliveryId($options['type']);
} else {
    if ( isset($_REQUEST['id']) ) {
        if ( $options['type2'] == 1 ) {
            $cdek->setDeliveryId(2);
        } else {
            $cdek->setDeliveryId(4);
        }
    } else {
        $cdek->setDeliveryId($options['type2']);
    }
}
$priority = explode(',', $options['tariff_priority']);
foreach ( $priority as $value ) {
    $cdek->addTariffPriority($value);
}

//print_r($paramArray); exit;

if ( $options['placeCheck'] == 1 && !empty($paramArray) ) {
    if ( empty($paramArray) ) $cdek->calcError('Не удалось получить габариты или объем товара');
    foreach ($paramArray as $value) {
        if ( !isset($value['volume']) ) {
            if ( $value['cnt'] == 1 ) {
                $cdek->addGoodsItemBySize((!isset($value['weight']) || empty($value['weight']) || $value['weight'] == 0 || $value['weight'] < 100) ? 100 : $value['weight'], isset($value['w']) ? ceil($value['w']) : ceil($options['dD1']), isset($value['h']) ? ceil($value['h']) : ceil($options['dD2']), isset($value['l']) ? ceil($value['l']) : ceil($options['dD3']));
            } else {
                for ( $i = 1; $i <= $value['cnt']; $i++ ) {
                    $cdek->addGoodsItemBySize((!isset($value['weight']) || empty($value['weight']) || $value['weight'] == 0 || $value['weight'] < 100) ? 100 : $value['weight'], isset($value['w']) ? ceil($value['w']) : ceil($options['dD1']), isset($value['h']) ? ceil($value['h']) : ceil($options['dD2']), isset($value['l']) ? ceil($value['l']) : ceil($options['dD3']));
                }
            }
        } else {
            if ( $value['cnt'] == 1 ) {
                $cdek->addGoodsItemByVolume($value['weight'], $value['volume']);
            } else {
                for ( $i = 1; $i <= $value['cnt']; $i++ ) {
                    $cdek->addGoodsItemByVolume($value['weight'], $value['volume']);
                }
            }
        }
    }
} else {
	if ( isset($paramArray[0]['weight']) ) {
		$weight= 0;
		foreach ($paramArray as $value) {
			$weight = $weight + $value['weight'];
		}
	}
	if ($volume == 0) {
		$cdek->addGoodsItemBySize ($weight, ceil($w), ceil($h), ceil($l));
	} else {
		$cdek->addGoodsItemByVolume ($weight, $volume);
	}
}



if ($cdek->calculate() === true) {
    $res = $cdek->getResult();
    $price = $res['result']['price'];
    $min = $res['result']['deliveryPeriodMin'];
    $max = $res['result']['deliveryPeriodMax'];
    if ( is_numeric($options['deliveryMyDate']) && $options['deliveryMyDate'] > 0 ) {
        $minDate = date('d.m.Y', strtotime($res['result']['deliveryDateMin']) + 86400 * $options['deliveryMyDate']);
        $maxDate = date('d.m.Y', strtotime($res['result']['deliveryDateMax']) + 86400 * $options['deliveryMyDate']);
    } else {
        $minDate = date('d.m.Y', strtotime($res['result']['deliveryDateMin']));
        $maxDate = date('d.m.Y', strtotime($res['result']['deliveryDateMax']));
    }
    if ( $options['freeDelivery'] != 0 && $_REQUEST['price'] > $options['freeDelivery'] ) {
        $price = 0;
    } else if ( $options['typeCurrency'] == 'RUB' ) {
        if (is_numeric($options['markup']) && $options['markup'] > 0) $price = $price + $options['markup'];
		if (is_numeric($options['markupProc']) && $options['markupProc'] > 0) $price = $price + ($_REQUEST['price']*$options['markupProc']/100);
        if (is_numeric($options['work']) && $options['work'] > 0) $price = $price + $options['work'];
        if ( is_numeric($corobCost) && $corobCost > 0 ) $price = $price + $corobCost;
    } else {
        $price = $cdek->currencyConverter($options['typeCurrency'], $price);
        if (is_numeric($options['markup']) && $options['markup'] > 0) $price = $price + $options['markup'];
		if (is_numeric($options['markupProc']) && $options['markupProc'] > 0) $price = $price + ($_REQUEST['price']*$options['markupProc']/100);
        if (is_numeric($options['work']) && $options['work'] > 0) $price = $price + $options['work'];
        if ( is_numeric($corobCost) && $corobCost > 0 ) $price = $price + $corobCost;
    }
    $cdek->calcResponse(round($price, $options['roundRes']), $min, $max, $radioId, $point, $minDate, $maxDate);
} else {
    $err = $cdek->getError();
    if( isset($err['error']) && !empty($err) ) {
        $msg = '';
        foreach($err['error'] as $e) {
            $msg .= $e['text'] . '.<br />';
        }
        $cdek->calcError($msg);
    }
    $cdek->calcError('Непредвиденная ошибка:' . $msg);
}

?>