<?php
 /**
 *      [QKSHOP] (C) QIKEWEB Inc.
 *      This is NOT a freeware, use is subject to license terms
 */

class DeliveryModel extends BaseModel{
	public $msg='';
	function getTypeInfo($id){
		$sql = "select * from qk_delivery_type where id='$id'";
		$info = $this->db->getRow($sql);
		$config = $info['config'];
		$config = mb_convert_encoding($config,"gbk","utf-8");
		$info['config'] = unserialize($config);
		//print_r($info['config']);	//Array ( [firstprice] => 20 [firstunit] => 500 [continueprice] => 9 [continueunit] => 500 [confexpressions] => [setting] => setting_hda [dt_useexp] => 0 [has_cod] => 0 [defAreaFee] => 0 ) 
		return $info;
	}

	function editType($params){$params['act']='edit';return $this->queryType($params);}
	function addType($params){$params['act']='add';return $this->queryType($params);}
	function queryType($params){
		$act = $params['act'];
		$id = $params['id'];
		$name = $params['name'];
		$corpId = $params['corpId'];
		$sortId = $params['sortId'];
		$iStatus = $params['iStatus'];
		$detail = $params['detail'];
		$className = $params['className'];
		$firstunit = $params['firstunit'];
		$continueunit = $params['continueunit'];
		$firstprice = $params['firstprice'];
		$continueprice = $params['continueprice'];
		$config = array();
		$config['firstunit'] = $firstunit;
		$config['continueunit'] = $continueunit;
		$config['firstprice'] = $firstprice;
		$config['continueprice'] = $continueprice;
		$config = serialize($config);
		if(!$act){$this->msg='操作类型错误';return false;}
		if(!$name){$this->msg='物流名称不得为空';return false;}
		if($act=='add'){$sql = "insert into qk_delivery_type set ";$sqlEnd=",posttime=UNIX_TIMESTAMP()";}
		if($act=='edit'){$sql = "update qk_delivery_type set ";$sqlEnd=" where id='$id'";}
		if($sql){
			$sql .= " name='$name',corpId='$corpId',sortId='$sortId',iStatus='$iStatus',config='$config',detail='$detail',className='$className'".$sqlEnd;
			if($this->db->query($sql)){
				if($act=='add'){$this->msg='添加物流方式成功';}else{$this->msg='修改物流方式成功';}
				return true;
			}
		}
		return false;
	}
	function delType($id){
		$sql = "delete from qk_delivery_type where id='$id'";
		if($this->db->query($sql)){return true;}
		return false;
	}

	function getPrice($weight,$typeId,$cartInfo=false){//echo $weight;
		if(!$typeId){return 0;}
		$price = 0;
			$info = $this->getTypeInfo($typeId);
			$firstunit = $info['config']['firstunit'];
			$continueunit = $info['config']['continueunit'];
			$firstprice = $info['config']['firstprice'];
			$continueprice = $info['config']['continueprice'];
			if($weight<$firstunit){
				$price = $firstprice;
			}else{
				$chaochu = ceil(($weight-$firstunit)/$continueunit);
				$chaochuPrice = $chaochu*$continueprice;
				$price = $chaochuPrice + $firstprice;
			}
			//echo $price;
		$apiPrice = 0;
		if($info['className']){
			$className = $info['className'];
			require_once(ROOT_PATH."plugins/deliverytype/".$className.".class.php");
			$cls = new $className();
			$apiPrice = $cls->getPrice($weight,$cartInfo);
		}
		if($apiPrice && settype($apiPrice,"float") && $apiPrice!=0 && (is_float($apiPrice) || is_int($apiPrice))){
			return $apiPrice;
		}
		return $price;
	}

	function getAllType(){
		$sql = "select * from qk_delivery_type order by id desc";
		return $this->db->getAll($sql);
	}
	function delDeliveryCorp($id){
		if(is_numeric($id)){
			$sql = "delete from `qk_delivery_corp` where id='".$id."'";$this->db->query($sql);return true;
		}
		return false;
	}
	function ajaxColEditDeliveryCorp($param){
				$col = $param['col'];
				$val = $param['val'];
				$id = $param['id'];
				if($col && settype($id,"integer")){
					$sql = "update `qk_delivery_corp` set `$col`='$val' where id='".$id."'";
					if($this->db->query($sql)){
						return true;
					}
				}else{
					$this->msg = '参数不完整';
				}
		return false;
	}
	function addDeliveryCorp($param){
		$name = $param['name'];
		$website = $param['website'];
		$sortId = $param['sortId'];
		$iStatus = $param['iStatus'];
		if(!$sortId || !is_numeric($sortId)){$sortId=50;}
		if(!$iStatus || !is_numeric($iStatus)){$iStatus=0;}
		if(!$name){$this->msg='物流公司名不能为空';return false;}
		$sql = "insert into `qk_delivery_corp` set name='$name',website='$website',sortId='$sortId',iStatus='$iStatus'";
		$this->db->query($sql);
		return $this->db->insert_id();
	}
}

/*

Array
(
    [act] => edit
    [id] => 1
    [name] => EMS-中国邮政（示范）
    [corpId] => 1
    [sortId] => 50
    [firstunit] => 1000
    [continueunit] => 1000
    [firstprice] => 20
    [continueprice] => 9
    [iStatus] => 0
)
*/
?>