Your IP : 216.73.216.86


Current Path : /var/www/homesaver/www/bitrix/modules/ipol.sdek/classes/lib/
Upload File :
Current File : /var/www/homesaver/www/bitrix/modules/ipol.sdek/classes/lib/sdekShipment.php

<?php
	class sdekShipment{
	    public $accountId;
		// ������
		public $sender;
		public $receiver;

		// �����
		public $gabs;
		public $goods;

		// �����
		public $profiles;
		public $error = false;
		public $arErrors;

		// ������������

		function __construct($params=array()){
			if(!$this->checkField('RECEIVER',$params))
				$this->addError(GetMessage('IPOLSDEK_SHIPMENT_ERRRECEIVER'));
			if(!$this->checkField('ITEMS',$params) && !$this->checkField('GABS',$params))
				$this->addError(GetMessage('IPOLSDEK_SHIPMENT_ERRGOODS'));

			$this->sender    = ($this->checkField('SENDER',$params)) ? $params['SENDER'] : CDeliverySDEK::getHomeCity();
			$this->receiver  = $params['RECEIVER'];
			$this->accountId = ($this->checkField('ACCOUNT',$params)) ? $params['ACCOUNT'] : false;

			if($this->checkField('ITEMS',$params)){
				$this->goods = $params['ITEMS'];
				CDeliverySDEK::setGoods($this->goods);
				$this->gabs = CDeliverySDEK::$goods;
			}else
				$this->gabs = $params['GABS'];
		}

		function calcProfile($profile){
			if(!$this->getError()){
				CDeliverySDEK::$goods      = $this->gabs;
				CDeliverySDEK::$sdekSender = $this->sender;
				CDeliverySDEK::$sdekCity   = $this->receiver;

				foreach(GetModuleEvents(CDeliverySDEK::$MODULE_ID, "onBeforeRequestDelivery", true) as $arEvent)
					ExecuteModuleEventEx($arEvent,Array($profile));

				$cache = new Ipolh\SDEK\Bitrix\Entity\cache();
				$cachename = "calculate|$profile|".$this->sender."|".$this->receiver."|".implode('|',$this->gabs)."|".$this->accountId;

				if($cache->checkCache($cachename)){
					$result = $cache->getCache($cachename);
				}else{
					$result = CDeliverySDEK::formCalcRequest($profile,$this->accountId);
					if($result['success']){
						$cache->setCache($cachename,$result);
					}
				}

				if(!isset($this->profiles) || !is_array($this->profiles))
					$this->profiles = array();
				if($result['success']){
					$addTerm = intval(\Ipolh\SDEK\option::get('termInc'));
					$this->profiles[$profile] = array(
						'RESULT'   => 'OK',
						'PRICE'    => $result['price'],
						'CURRENCY'  => $result['currency'],
						'PRICE_CUR' => $result['priceByCurrency'],
						'TERMSBAZE' => array(
							'MIN' => $result['termMin'],
							'MAX' => $result['termMax']
						),
						'TERMS' => array(
							'MIN' => $result['termMin']+$addTerm,
							'MAX' => $result['termMax']+$addTerm
						),
						'TARIF' => $result['tarif']
					);

                    CDeliverySDEK::$lastCnt = $result['price'];
				}else{
					$erStr = '';
					if(!empty($result) && is_array($result)){
						foreach($result as $erCode => $erLabl)
							$erStr.="$erLabl ($erCode) ";
					}
					$this->profiles[$profile] = array(
						'RESULT' => 'ERROR',
						'TEXT'	 => CDeliverySDEK::zaDEjsonit($erStr)
					);
				}
			}else{
				$this->profiles[$profile] = array(
					'RESULT' => 'ERROR',
					'TEXT'	 => $this->getError()
				);
			}
		}

		function calcProfiles($arProfiles){
			foreach($arProfiles as $profile)
				$this->calcProfile($profile);
		}

		function compability(){
			if(!is_array($this->profiles))
				return false;
			$arReturn = array();
			foreach($this->profiles as $profile => $result)
				if($result['RESULT'] == 'OK')
					$arReturn[] = (is_numeric($profile)) ? \sdekHelper::defineTarif($profile) : $profile;
			return $arReturn;
		}

		private function checkField($wat,$src){
			return (array_key_exists($wat,$src) && $src[$wat]);
		}

		function getProfiles(){
			return $this->profiles;
		}

		function getProfile($profile){
			return (array_key_exists($profile,$this->profiles)) ? $this->profiles[$profile] : false;
		}

		function getProfileTarif($profile){
			return (is_array($this->profiles) && array_key_exists($profile,$this->profiles) && $this->profiles[$profile]['RESULT'] == 'OK') ? $this->profiles[$profile]['TARIF'] : false;
		}
		
		private function addError($errMess){
			$this->error = true;
			if(!isset($this->arErrors))
				$this->arErrors = array();
			$this->arErrors []= $errMess;
		}
		
		public function getError(){
			if(!$this->error)
				return false;
			else
				return implode(', ',$this->arErrors);
		}
	}
?>