load->library('session');
		$this->load->_model('Model_whlabel','whlabel');
		$this->load->_model('Model_warehouse','warehouse');
		$this->load->_model('Model_purchase','purchase');
		$this->load->_model('Model_excel','excel');
		$this->load->_model('Model_fullorder','fullorder');
		$this->load->_model('Model_fullordertt','fullordertt');
		$this->load->_model('Model_fullorder_smt','fullorder_smt');
		$this->load->_model('Model_fullordersmt','fullordersmt');
		$this->load->_model('Model_express','express');
		$this->load->_model('Model_shop','shop');
		$this->load->_model('Model_typeclass','typeclass');
		$this->load->_model('Model_country','country');
		$this->load->_model('Model_warehouse','warehouse');
		$this->load->_model('Model_kdniao','kdniao');
		$this->load->_model('Model_notice','notice');
		$this->load->_model('Model_dhl','dhl');
		$this->load->_model('Model_shop','shop');
		$this->load->_model('Model_fullorderexcel','fullorderexcel');
		$this->load->_model('Model_ljg','ljg');
		$this->load->_model('Model_usps','usps');
		$this->load->_model('Model_specialstock','specialstock');
		$this->load->_model('Model_whlabellabel','whlabellabel');
		$this->load->_model('Model_whlabelwz','whlabelwz');
		$this->load->_model('Model_whlabelbarcode','whlabelbarcode');
		$this->load->_model('Model_bh','bh');
	}
	//定义方法的调用规则 获取URI第二段值
    public function _remap($arg,$arg_array)
    {
		if($arg == 'xr')//在库标签
        {
             $this->_xr();
        }
		else if($arg == 'exceljs')
        {
             $this->_exceljs();
        }
		else if($arg == 'uck')
        {
             $this->_uck();
        }
		else
		{
			 $this->_index();
		}
    }
	//进销存浏览
	public function _index()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['page']))  
		{
		    $page = $this->input->post('page',true);
		    $perpage = $this->input->post('perpage',true);
			$category = $this->input->post('category',true);
			$size = $this->input->post('size',true);
			$grade = $this->input->post('grade',true);
			$color = $this->input->post('color',true);
			$lowe = $this->input->post('lowe',true);
			$sku = $this->input->post('sku',true);
			$warehouse = $this->input->post('warehouse',true);
			$usa = $this->input->post('usa',true);
			$purchase = $this->input->post('purchase',true);
			$lacetype = $this->input->post('lacetype',true);
			$state = $this->input->post('state',true);
			$px = $this->input->post('px',true);
			$ktime = $this->input->post('ktime',true);
			$jtime = $this->input->post('jtime',true);
			//$ktime = strtotime($ktime);
			//$jtime = strtotime($jtime);
			$where = "1=1";$gj = "";$ck = "";
			if($lacetype)
            {
                $where  .= " and features like '%-$lacetype-%'";
            }
			if($category)
            {
                $where  .= " and features like '%-$category-%'";
            }
			if($size)
            {
                $where  .= " and features like '%-$size-%'";
            }
			if($grade)
            {
                $where  .= " and features like '%-$grade-%'";
            }
			if($color)
            {
                $where  .= " and features like '%-$color-%'";
            }
			if($lowe)
            {
                $where  .= " and features like '%-$lowe-%'";
            }
			if($usa)
            {
				if(!$warehouse)
				{
                    $where  .= " and (warehouse = '5' or warehouse = '8')";
				}
            }
			if($warehouse)
            {
                $where  .= " and warehouse = '$warehouse'";
            }
			if($purchase)
            {
                $where  .= " and purchase = '$purchase'";
            }
			if($state)
            {
                $where  .= " and state = '$state'";
            }
			
			if($ktime && $jtime)
            {
                $gj  = " and enter > '$ktime' and enter < '$jtime'";
				$ck  = " and outk > '$ktime' and outk < '$jtime'";
            }
            //数据排序
            if($px == 'ksts')
			{
				$order_str = "ksts asc,jybh desc";
				$where .= " and jybh > 0";
			}
			else
			{
				$order_str = $px." desc";
			}
            if(empty($page))
		    {
                $start = 0;
		    	$perpage = 1;
            }
		    else
		    {
                $start = ($page - 1)*$perpage;
            }
			if($sku)
            {
                $where  .= " and sku like '%$sku%'";
				$info_list = $this->bh->find_all($where,'id,sku,title,t1,t3,t7,t14,t30,t60,t90,jybh,ksts',$order_str,$start,$perpage);
            }
			else
			{
				$info_list = $this->bh->find_all($where,'id,sku,title,t1,t3,t7,t14,t30,t60,t90,jybh,ksts',$order_str,$start,$perpage);
			}
            foreach ($info_list as $key=>$value) 
		    {
				if($value['jybh'] == '0')
				{
					$info_list[$key]['jybh'] = '无';
				}
			}
			$rows = array();$list = array();
		    $total = count($this->bh->find_all($where));
		    $pagenum = ceil($total/$perpage);
		    $over = $total-($start+$perpage);
		    $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
		    echo json_encode($rows);exit;
		}
		$this->_Template('bh',$this->data);
	}
	
	//进销存浏览
	public function _xr()//数据写入
	{
	    $this->db->query("delete from crowd_bh");
		$post = $this->input->post(NULL, TRUE);
		if(isset($_GET['xr']))  
		{
			$info_list = $this->whlabel->find_pc("warehouse = '5'",'sku','warehouse,sku,title,number');
			$this->db->trans_begin();
			foreach ($info_list as $key=>$value) 
		    {
				$c = $this->whlabel->find_count('warehouse = "5" and sku = "'.$value['sku'].'" and state = 0 and warehouse = "'.$value['warehouse'].'" and number = "'.$value['number'].'"');
				$info_list[$key]['t1'] = 0;
				$info_list[$key]['t3'] = 0;
				$info_list[$key]['t7'] = 0;
				$info_list[$key]['t14'] = 0;
				$info_list[$key]['t30'] = 0;
				$info_list[$key]['t60'] = 0;
				$info_list[$key]['t90'] = 0;
				$fullorder = $this->fullorder->find_all("whlabel like '%".$value['number']."%' and librarytime > '".(strtotime(date("Y-m-d",time()))-90*24*3600)."'",'whlabel,librarytime');
				$fullordersmt = $this->fullordersmt->find_all("whlabel like '%".$value['number']."%' and librarytime > '".(strtotime(date("Y-m-d",time()))-90*24*3600)."'",'whlabel,librarytime');
				$data = array_merge($fullorder,$fullordersmt);
				foreach ($data as $val) 
		        {
					$dt = explode('|',trim($val['whlabel'],'|'));
					foreach ($dt as $v) 
		            {
						$v = explode('-',$v);
						if($v[0] == $value['number'])
						{
							if($val['librarytime'] > (strtotime(date("Y-m-d",time()))-24*3600))
							{
								$info_list[$key]['t1'] += $v[1];
							}
							if($val['librarytime'] > (strtotime(date("Y-m-d",time()))-3*24*3600))
							{
								$info_list[$key]['t3'] += $v[1];
							}
							if($val['librarytime'] > (strtotime(date("Y-m-d",time()))-7*24*3600))
							{
								$info_list[$key]['t7'] += $v[1];
							}
							if($val['librarytime'] > (strtotime(date("Y-m-d",time()))-14*24*3600))
							{
								$info_list[$key]['t14'] += $v[1];
							}
							if($val['librarytime'] > (strtotime(date("Y-m-d",time()))-30*24*3600))
							{
								$info_list[$key]['t30'] += $v[1];
							}
							if($val['librarytime'] > (strtotime(date("Y-m-d",time()))-60*24*3600))
							{
								$info_list[$key]['t60'] += $v[1];
							}
							if($val['librarytime'] > (strtotime(date("Y-m-d",time()))-90*24*3600))
							{
								$info_list[$key]['t90'] += $v[1];
							}
						}
					}
					
				}
				if($info_list[$key]['t30'] > 0)
				{
				    $mt = $info_list[$key]['t30']/30;
				    $info_list[$key]['ksts'] = intval($c/$mt);
				    $info_list[$key]['jybh'] = ($info_list[$key]['ksts'] < 30)?($info_list[$key]['t30'] - $c):0;
				}
				else
				{
					$info_list[$key]['ksts'] = 0;
				    $info_list[$key]['jybh'] = 0;
				}
				$this->bh->insert($info_list[$key]);
		    }
			if ($this->db->trans_status() === TRUE)
            {
			    $this->db->trans_commit();
			    echo 'OK';
		    }
		    else
		    {
			    $this->db->trans_rollback();
			    echo 'orver';
		    }
		}
	}
	
	public function _exceljs()
	{
		if(isset($_GET['excel']))  
		{
			$page = $this->input->get('page',true);
		    $perpage = $this->input->get('perpage',true);
			$category = $this->input->get('category',true);
			$size = $this->input->get('size',true);
			$grade = $this->input->get('grade',true);
			$color = $this->input->get('color',true);
			$lowe = $this->input->get('lowe',true);
			$sku = $this->input->get('sku',true);
			$warehouse = $this->input->get('warehouse',true);
			$usa = $this->input->get('usa',true);
			$purchase = $this->input->get('purchase',true);
			$lacetype = $this->input->get('lacetype',true);
			$state = $this->input->get('state',true);
			$px = $this->input->get('px',true);
			$ktime = $this->input->get('ktime',true);
			$jtime = $this->input->get('jtime',true);
			//$ktime = strtotime($ktime);
			//$jtime = strtotime($jtime);
			$where = "1=1";$gj = "";$ck = "";
			if($lacetype)
            {
                $where  .= " and features like '%-$lacetype-%'";
            }
			if($category)
            {
                $where  .= " and features like '%-$category-%'";
            }
			if($size)
            {
                $where  .= " and features like '%-$size-%'";
            }
			if($grade)
            {
                $where  .= " and features like '%-$grade-%'";
            }
			if($color)
            {
                $where  .= " and features like '%-$color-%'";
            }
			if($lowe)
            {
                $where  .= " and features like '%-$lowe-%'";
            }
			if($usa)
            {
				if(!$warehouse)
				{
                    $where  .= " and (warehouse = '5' or warehouse = '8')";
				}
            }
			if($warehouse)
            {
                $where  .= " and warehouse = '$warehouse'";
            }
			if($purchase)
            {
                $where  .= " and purchase = '$purchase'";
            }
			if($state)
            {
                $where  .= " and state = '$state'";
            }
			
			if($ktime && $jtime)
            {
                $gj  = " and enter > '$ktime' and enter < '$jtime'";
				$ck  = " and outk > '$ktime' and outk < '$jtime'";
            }
            //数据排序
			if($px == 'ksts')
			{
				$order_str = "ksts asc,jybh desc";
				$where .= " and jybh > 0";
			}
			else
			{
				$order_str = $px." desc";
			}
            if(empty($page))
		    {
                $start = 0;
		    	$perpage = 1;
            }
		    else
		    {
                $start = ($page - 1)*$perpage;
            }
			if($sku)
            {
                $where  .= " and sku like '%$sku%'";
				$info_list = $this->bh->find_all($where,'sku,title,t1,t3,t7,t14,t30,t60,t90,jybh,ksts',$order_str);
            }
			else
			{
				$info_list = $this->bh->find_all($where,'sku,title,t1,t3,t7,t14,t30,t60,t90,jybh,ksts',$order_str);
			}
			/**
			foreach ($info_list as $key=>$value) 
		    {
				$warehouse = $this->warehouse->read($value['warehouse']);
				$info_list[$key]['warehouse'] = $warehouse['title'];
			}
			**/
			foreach ($info_list as $key=>$value) 
		    {
				if($value['jybh'] == '0')
				{
					$info_list[$key]['jybh'] = '无';
				}
			}
            $title = "备货详情 ".date("Y-m-d",time()); 
            $titlename = "
            
			| SKU | 产品名称 | 昨天销量 | 3天销量 | 7天销量 | 14天销量 | 30天销量 | 60天销量 | 90天销量 | 建议备货 | 可售天数 | 
            
"; 
            $filename = $title.".xls";
			$tail = "";
            $this->excel->get_fz2($info_list,$titlename,$filename,$tail);
	    }
	}
	
	public function _uck()
	{
		if(isset($_GET['excel']))
		{
			$tc = array();
			$typeclass = $this->typeclass->find_all();
			foreach ($typeclass as $v) 
		    {
				$tc[$v['id']] = $v['zh'];
			}
			$data = array();$t = array();$rq = '';
			$ts = strtotime(date("Y-m-d",time()))-strtotime(date("Y-m",time()));
			$ts = $ts/(24*3600);
			for($i=0;$i<$ts;$i++)
			{
				$t[] = date("Y-m-d",strtotime('-'.($ts-$i).' day'));
				$rq .=  "".date("Y-m-d",strtotime('-'.($ts-$i).' day'))."";
			}
			$info_list = $this->whlabel->find_pc("warehouse = '5'",'sku','warehouse,sku,features');
			foreach ($info_list as $k=>$v) 
		    {
				$features = explode('-',trim($v['features'],'-'));
				if(!isset($features[3]) || !isset($features[4]))
				{
					continue;
				}
				$data[$k][0] = $tc[$features[0]];//类型
				$data[$k][1] = ($features[3] != 57)?$tc[$features[3]]:'';//颜色,自然色不显示
				$data[$k][2] = $tc[$features[4]];//曲度
				$data[$k][3] = '';
				if(isset($data[$k][1]))
				{
					$data[$k][3] .= $data[$k][1];
				}
				if(isset($data[$k][2]))
				{
					$data[$k][3] .= $data[$k][2];
				}
				if(isset($features[5]))
				{
					$data[$k][3] .= $tc[$features[5]];
				}
				if(isset($features[2]))
				{
					$data[$k][3] .= $tc[$features[2]];
				}
				//$data[$k][3] 颜色,曲度,头套种类,尺寸
				$data[$k][4] = $v['sku'];
				$data[$k][5] = $this->whlabel->find_count('sku = "'.$v['sku'].'" and warehouse = "'.$v['warehouse'].'" and features = "'.$v['features'].'" and (state = 0 or (state = 1 and outk > '.strtotime(date("Y-m",time())).'))');//月初库存
				$data[$k][] = $this->whlabel->find_count('warehouse = "5" and sku = "'.$v['sku'].'" and state = 0 and warehouse = "'.$v['warehouse'].'" and features = "'.$v['features'].'"');//剩余库存
				$data[$k][] = '';
				$data[$k][] = '';
				$byck = 0;
				foreach ($t as $vv) 
		        {
					$sl = $this->whlabel->find_count('sku = "'.$v['sku'].'" and warehouse = "'.$v['warehouse'].'" and features = "'.$v['features'].'" and state = 1 and outk > '.strtotime($vv).' and outk < '.(strtotime($vv)+24*3600));
					$data[$k][] = ($sl > 0)?''.$sl.'':'';
					$byck += $sl;
				}
				$data[$k][] = ($byck > 0)?$byck:'';
			}
			
            $title = "详情 ".date("Y-m-d",time());
            $titlename = " | 
            
			| 类别 | 颜色 | 曲度 | 名称 | SKU | 月初库存 | 剩余库存 | 在途数量 | 预达日期".
			$rq
			." | 本月出库 | 
            
"; 
            $filename = $title.".xls";
			$tail = "";
            $this->excel->get_fz2($data,$titlename,$filename,$tail);
	    }
	}
}