<?php
/*
=====================================================
 Cs-Cart 2.0.8 Nulled By KenDesign
-----------------------------------------------------
 www.freeshareall.com - www.freeshareall.net
-----------------------------------------------------
 KenDesign Team
=====================================================
*/


//
// $Id: can.php 7774 2009-07-31 09:47:01Z zeke $
//

if ( !defined('AREA') ) { die('Access denied'); }

function fn_get_can_rates($code, $weight_data, $location, &$auth, $shipping_settings, $package_info, $origination)
{
	static $cached_rates = array();

	if ($shipping_settings['can_enabled'] != 'Y') {
		return false;
	}

	$cached_rate_id = fn_generate_cached_rate_id($weight_data, $origination);

	if (!empty($cached_rates[$cached_rate_id])) {
		if (!empty($cached_rates[$cached_rate_id][$code])) {
			return array('cost' => $cached_rates[$cached_rate_id][$code]);
		} else {
			return false;
		}
	}

	$merchant_id = $shipping_settings['can']['merchant_id'];
	$length = $shipping_settings['can']['length'];
	$width = $shipping_settings['can']['width'];
	$height = $shipping_settings['can']['height'];

	$origination_postal = $origination['zipcode'];
	$destination_postal = $location['zipcode'];
	$destination_country = $location['country'];
	$destination_city = $location['city'];
	$destination_state = $location['state'];

	$total_cost = $package_info['C'];
	$weight = $weight_data['full_pounds'];
	$amount = '1';

	$lang = (CART_LANGUAGE == 'FR') ? 'fr' : 'en';

	$request[]=<<<XML
<?xml version="1.0" ?>
<eparcel>
	<language>$lang</language>
	<ratesAndServicesRequest>
		<merchantCPCID>$merchant_id</merchantCPCID>
		<fromPostalCode>$origination_postal</fromPostalCode>
		<turnAroundTime> 24 </turnAroundTime>
		<itemsPrice>$total_cost</itemsPrice>
		<lineItems>
			<item>
				<quantity>$amount</quantity>
				<weight>$weight</weight>
				<length>$length</length>
				<width>$width</width>
				<height>$height</height>
				<description>ggrtye</description>
				<readyToShip/>
			</item>
		</lineItems>
		<city>$destination_city</city>
		<provOrState>$destination_state</provOrState>
		<country>$destination_country</country>
		<postalCode>$destination_postal</postalCode>
	</ratesAndServicesRequest>
</eparcel>
XML;

	list($header, $result) = fn_http_request('POST', 'http://sellonline.canadapost.ca:30000', $request);

	$rates = fn_can_get_rates($result, $code);

	if (empty($cached_rates[$cached_rate_id]) && !empty($rates['cost'])) {
		$cached_rates[$cached_rate_id] = $rates['cost'];
	}

	if (!empty($rates['cost'])) {
		return array('cost' => $rates['cost']);
	} else {
		if (defined('SHIPPING_DEBUG')) {
			return array('error' => ((!empty($rates['error'])) ? $rates['error'] : fn_get_lang_var('service_not_available')));
		}
	}

	return false;

}

function fn_can_get_rates($result, $code) 
{
	$doc = new XMLDocument();
	$xp = new XMLParser();
	$xp->setDocument($doc);
	$xp->parse($result);
	$doc = $xp->getDocument();
	$rates = array();

	if (is_object($doc->root)) {
		$root = $doc->getRoot();

		if ($root->getElementByName('ratesAndServicesResponse')) {

		$service_rates = $root->getElementByName('ratesAndServicesResponse');
		$shipment = $service_rates->getElementsByName('product');
		
			for ($i = 0; $i < count($shipment); $i++) {
				$id = $shipment[$i]->getAttribute("id");

				if (!empty($id) && $id > 0) {
					$rates[$id] = $shipment[$i]->getValueByPath("rate");
					unset($id);
				}
			}

			$results['cost'] = $rates[$code];

		} elseif ($root->getElementByName('error')) {
			$results['error'] = $root->getValueByPath('/error/statusMessage');
		}
	}
	return $results;
}

?>