<?php
// +----------------------------------------------------------------------
// | Sunnyi 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.sunnyi.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 商易网络(admin@sunnyi.cn)
// +----------------------------------------------------------------------


class DeliveryModel extends MultiLangModel  {
   /**
    * 根据地区ID取出可用的配送方式
    * @param integer $id  地区ID
    */
   public function loadDelivery($id)
   {
   		$delivery = array();
   		$delivery_list = D("Delivery")->where("status=1")->findAll();
   		foreach($delivery_list as $v)
   		{
   			if($v['allow_default'] == 1&&D("DeliveryRegion")->where("delivery_id=".$v['id'])->count()==0)
   			{
   				//允许默认   				
   				array_push($delivery,$v['id']);
   			}
   			else 
   			{
   				
   				$delivery_region = D("DeliveryRegion")->where("delivery_id=".$v['id'])->findAll();
   				$tag = true; //是否未查询到
   				foreach($delivery_region as $vv)
   				{
   					$region_ids = explode(",",$vv['region_ids']);   					
   					$tmp_id = $id;
   					while(intval(D("RegionConf")->where("id=".$tmp_id)->getField("region_level"))>0)
   					{
   						if(in_array($tmp_id,$region_ids))
   						{
   							array_push($delivery,$v['id']);
	   						$tag = false;
	   						break;
   						}
   						else
   						{
   							$tmp_id = intval(D("RegionConf")->where("id=".$tmp_id)->getField("pid"));
   						}
   					}
   					
   				} 
   				if($tag)
   				{
   					if($v['allow_default'] == 1)
   					{
   						//允许默认		   				
		   				array_push($delivery,$v['id']);
   					}
   				}  				
   			}   			

   		}
   		return $delivery;
   }
}
?>