load->_model('Model_whlabel','whlabel');
		$this->load->_model('Model_warehouse','warehouse');
		$this->load->_model('Model_excel','excel');
		$this->load->_model('Model_whlabeltransport','whlabeltransport');
	}
	//定义方法的调用规则 获取URI第二段值
    public function _remap($arg,$arg_array)
    {
		if($arg == 'add')//添加
        {
             $this->_add();
        }
		else if($arg == 'edit')//修改
        {
             $this->_edit($arg_array);
        }
		else if($arg == 'del')//修改
        {
             $this->_del();
        }
		else if($arg == 'zj')//修改
        {
             $this->_zj();
        }
		else if($arg == 'list')//修改
        {
             $this->_list();
        }
		else if($arg == 'presetou')//修改
        {
             $this->_presetout($arg_array);
        }
		else if($arg == 'excel')
        {
             $this->_excel();
        }
		else if($arg == 'excelzh')
        {
             $this->_excelzh();//整合数据
        }
		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);
			$warehouse = $this->input->post('warehouse',true);
			$sku = $this->input->post('sku',true);
			$text = $this->input->post('text',true);
			$cz = $this->input->post('cz',true);
			$xztime = $this->input->post('xztime',true);
			$timetk = $this->input->post('timetk',true);
			$timetj = $this->input->post('timetj',true);
			$timetk = strtotime($timetk);
			$timetj = strtotime($timetj);
			$where = "1=1 ";
			if($warehouse)
            {
                $where  .= " and warehouse = '$warehouse'";
            }
			if($sku)
            {
                $where  .= " and sku = '$sku'";
            }
			if($text)
            {
                $where  .= " and text like '%$text%'";
            }
			if($cz != '')
            {
                $where  .= " and cz = '$cz'";
            }
			if($timetk && $timetj)
            {
                $where  .= " and ".$xztime." > '$timetk' and ".$xztime." < '$timetj'";
            }
            //数据排序
            $order_str = $xztime." desc";
            if(empty($page))
		    {
                $start = 0;
		    	$perpage = 1;
            }
		    else
		    {
                $start = ($page - 1)*$perpage;
            }
            $info_list = $this->whlabeltransport->find_all($where,'id,warehouse,sku,title,num,enter,stime,time,text,cz',$order_str,$start,$perpage);
			foreach ($info_list as $key=>$value) 
		    {
				$warehouse = $this->warehouse->read($value['warehouse']);
				$info_list[$key]['warehouse'] = $warehouse['title'];
				$info_list[$key]['enter'] = date('Y-m-d H:i:s',$value['enter']);
				$info_list[$key]['stime'] = date('Y-m-d',$value['stime']);
				if($value['time'] > 0)
				{
				    $info_list[$key]['time'] = date('Y-m-d H:i:s',$value['time']);
				}
				else
				{
					$info_list[$key]['time'] = "
";
				}
				if($value['cz'] == '0')
				{
					$info_list[$key]['cz'] = "
点击完成
";
				}
				else if($value['cz'] == '1')
				{
					$info_list[$key]['cz'] = "
已完成
";
				}
		    }
		    $total = $this->whlabeltransport->find_count($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('whlabeltransport',$this->data);
	}
	//添加
	public function _add()
	{
		$dir = '/data/excel/'.date('Ymd',time()).'/';
		$config['upload_path'] = '.'.$dir ;
		$config['file_name'] = date('Ymd_His_',time()).rand(1000,9999);
        $config['allowed_types'] = 'xls|xlsx|csv';
        $config['max_size'] = 10240;
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
        if ($this->upload->do_upload('userfile'))
        {
			$full_path = $dir.$this->upload->data('file_name');
			$fileName = '.' . $full_path;
            if (!file_exists($fileName)) 
			{
				echo json_encode(array('msg'=>"上传失败,请重试",'success'=>false));exit;
            }
			else
			{
				libxml_use_internal_errors(true);
				require_once "./data/excel/PHPExcel/IOFactory.php";
				$phpExcel = PHPExcel_IOFactory::load($fileName);// 载入当前文件
				$phpExcel->setActiveSheetIndex(0);// 设置为默认表
				$sheetCount = $phpExcel->getSheetCount();// 获取表格数量
				$row = $phpExcel->getActiveSheet()->getHighestRow();// 获取行数
				$column = $phpExcel->getActiveSheet()->getHighestColumn();// 获取列数
				++$column;//如果列数大于26行
				$list = array();
				for ($i = 2; $i <= $row; $i++) // 行数循环
				{
					$data = array();
                    for ($c = 'A'; $c != $column; $c++)  // 列数循环
	                {
						$data[] = $phpExcel->getActiveSheet()->getCell($c . $i)->getValue();
                    }
					$list[] = $data;
                }
			}
			$i = 0;$j = 0;$ed = array();
		    foreach ($list as $key=>$value)
		    {
				$time = time();
				if($value['0'] == "")
				{
					continue;
				}
				$warehouse = $this->warehouse->get_title($value['0']);
				//$d = $this->whlabel->get_sku($value['1']);//针对各个仓库
				$d = $this->whlabel->get_warehousesku(5,$value['1']);//针对各个仓库
				if($value['2'] =='' || $value['2'] < 1)
				{
					continue;
				}
				if(!$d)//如果没有这个SKU
				{
					$ed[] = array($value['1'].'-库存中不存在此SKU');
					$j++;
					continue;
				}
				if(!isset($warehouse['id']))
				{
					$ed[] = array($value['1'].'-仓库名错误!');
					$j++;
					continue;
				}
				if($value['3'] == '')
				{
					$ed[] = array($value['1'].'-没有填写预达日期!');
					$j++;
					continue;
				}
				$post['sku'] = $value['1'];
				$post['num'] = $value['2'];//数量
				$post['stime'] = strtotime($value['3']);//预计到达日期
				$post['text'] = $value['4'];//备注
				$post['enter'] = $time;//添加时间
				$post['shop'] = $d['shop'];
				$post['warehouse'] = $warehouse['id'];
				$post['purchase'] = $d['purchase'];
				$post['title'] = $d['title'];
				$post['number'] = $d['number'];
				$post['features'] = $d['features'];
				$post['retreatwarehouse'] = $d['retreatwarehouse'];
				$this->whlabeltransport->insert($post);
			}
			if($j > 0)
			{
				$tt = date('Ymd',time());
				$title = '导入错误信息-'.$tt;
        		$titlename = "
";
				$tail = "\n";
        		$filename = $title.".xls";
        		$ecl = $this->excel->get_fz3($ed,$titlename,$filename,$tail);
				$dir = '/data/excel/'.$time.'/';
				$file_name = 'error_'.$time.rand(1000,9999);
				if(!is_dir('.'.$dir))mkdir('.'.$dir,0777);
				$myfile = fopen(".".$dir.$file_name.".xls", "w") or die();
				fwrite($myfile,$ecl);
				fclose($myfile);
				$error = $dir.$file_name.'.xls';
				echo json_encode(array('msg'=>'导入成功,'.$j.'条异常,','error'=>$error,'success'=>true));exit;
			}
			else
			{
				echo json_encode(array('msg'=>'导入成功!','error'=>1,'success'=>true));exit;
			}
		}
	}
	//修改
	public function _edit($arg_array)
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['id']))
		{
			$id = $this->input->post('id',true);
			$data = $this->whlabeltransport->read($id);
			if($data['cz'] == '1')
			{
				echo json_encode(array('msg'=>'已完成的数据无法修改!','success'=>false));exit;
			}
			$post['num'] = $this->input->post('num',true);
			$post['text'] = $this->input->post('text',true);
			$stime = $this->input->post('stime',true);
			$post['stime'] = strtotime($stime);
			$post['edit'] = time();
			if($this->whlabeltransport->save($post,$id))
        	{
         		echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
       	 	}
       		else
        	{
           		echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
        	}
		}
		$arg_array = $arg_array[0];
		$whlabeltransport = $this->whlabeltransport->read($arg_array);
		$this->data['whlabeltransport'] = $whlabeltransport;
		$this->_Template('whlabeltransport_edit',$this->data);
	}
	
	public function _zj()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['id']))
		{
			$id = $this->input->post('id',true);
			$data = $this->whlabeltransport->read($id);
			if($data['cz'] == '0')
			{
				$list = array();$num = "";$title = "";$features = "";
				$order = $this->input->post('order',true);
				$shop = $this->input->post('shop',true);
				$label['shop'] = $data['shop'];
				$label['sku'] = $data['sku'];
				$label['warehouse'] = $data['warehouse'];
				$label['purchase'] = $data['purchase'];
				$label['retreatwarehouse'] = $data['retreatwarehouse'];
				$label['state'] = 0;
				$time = time();
				$label['title'] = $data['title'];
				$label['number'] = $data['number'];
				$label['features'] = $data['features'];
				$label['enter'] = $time;
				$label['time'] = $time;//操作时间
				if($label['warehouse'] == '5')
				{
					$yza = $this->whlabel->find_all("sku =  '".$label['sku']."' and warehouse = '".$label['warehouse']."'",'*','id desc');
					if($yza)
					{
						if($label['features'] != $yza[0]['features'])
						{
							echo json_encode(array('msg'=>'此SKU的产品信息和现有不一致!- 1','success'=>false));exit;
						}
					}
					/**
					$yzb = $this->whlabel->find_all("features =  '".$label['features']."' and warehouse = '".$label['warehouse']."'",'*','id desc');
					if($yzb)
					{
						if($label['sku'] != $yzb[0]['sku'])
						{
							echo json_encode(array('msg'=>'此产品信息的SKU和现有不一致! - 2','success'=>false));exit;
						}
					}
					**/
				}
				$this->db->trans_begin();
				for($i=0;$i<$data['num'];$i++)
			    {
					$label['label'] = time().rand(100,999).$i;//产品标签 辨别各个产品,暂不使用
					$label['cs'] = $id;//前期增加这个是在途库存添加的数据
					$label['rktype'] = 1;
				    $this->whlabel->insert($label);
			    }
				$this->whlabeltransport->save(array('time'=>$time,'cz'=>1),$data['id']);
				if ($this->db->trans_status() === TRUE)
        		{
					$this->db->trans_commit();
					echo json_encode(array('msg'=>'入库成功!','id'=>$id,'time'=>date('Y-m-d H:i:s',$time),'success'=>true));exit;
				}
				else
				{
					$this->db->trans_rollback();
					echo json_encode(array('msg'=>'入库失败,请重试!','success'=>false));exit;
				}
			}
			else
		    {
			    echo json_encode(array('msg'=>'错误!已操作入库','success'=>false));exit;
		    }
		}
	}
	//导出模板
	public function _presetout()
	{
		if(isset($_GET['excel']))  
		{
			$where = "sku != 'Preset'";
            //数据排序
            $order_str = "id desc";
            if(empty($page))
		    {
                $start = 0;
		    	$perpage = 1;
            }
		    else
		    {
                $start = ($page - 1)*$perpage;
            }
			$warehouse = $this->input->get('warehouse',true);
			if($warehouse)
			{
				$where .= " and warehouse = '".$warehouse."'";
			}
            //取得信息列表
            $info_list = $this->whlabel->find_pc($where,'warehouse,sku','*');
			$rows = array();$list = array();
			foreach ($info_list as $value) 
		    {
				$warehouse = $this->warehouse->read($value['warehouse']);
				$number = $value['number'];$ftime = time()-15*24*3600;
				$list[] = array($warehouse['title'],$value['sku'],'0','','');
		    }
            $title = "在途模板"; 
            $titlename = "
            
			| 仓库 | SKU | 数量 | 预达日期(格式:2022-01-01) | 备注 | 
            
"; 
            $filename = $title.".xls";
			$tail = "";
            $this->excel->get_fz2($list,$titlename,$filename,$tail);
	    }
	}
	
	public function _excel()
	{
		if(isset($_GET['excel']))  
		{
			$warehouse = $this->input->get('warehouse',true);
			$sku = $this->input->get('sku',true);
			$cz = $this->input->get('cz',true);
			$text = $this->input->post('text',true);
			$xztime = $this->input->get('xztime',true);
			$timetk = $this->input->get('timetk',true);
			$timetj = $this->input->get('timetj',true);
			$timetk = strtotime($timetk);
			$timetj = strtotime($timetj);
			$where = "1=1 ";
			if($warehouse)
            {
                $where  .= " and warehouse = '$warehouse'";
            }
			if($sku)
            {
                $where  .= " and sku = '$sku'";
            }
			if($text)
            {
                $where  .= " and text like '%$text%'";
            }
			if($cz != '')
            {
                $where  .= " and cz = '$cz'";
            }
			if($timetk && $timetj)
            {
                $where  .= " and ".$xztime." > '$timetk' and ".$xztime." < '$timetj'";
            }
            //数据排序
            $order_str = $xztime." desc";
            //取得信息列表
           $info_list = $this->whlabeltransport->find_all($where,'warehouse,sku,title,num,enter,stime,time,text');
			foreach ($info_list as $key=>$value) 
		    {
				$warehouse = $this->warehouse->read($value['warehouse']);
				$info_list[$key]['warehouse'] = $warehouse['title'];
				$info_list[$key]['enter'] = date('Y-m-d H:i:s',$value['enter']);
				$info_list[$key]['stime'] = date('Y-m-d',$value['stime']);
				if($value['time'] > 0)
				{
				    $info_list[$key]['time'] = date('Y-m-d H:i:s',$value['time']);
				}
				else
				{
					$info_list[$key]['time'] = "";
				}
		    }
            $title = "在途数据".date('Y-m-d',time()); 
            $titlename = "
            
			| 仓库 | SKU | 名称 | 数量 | 添加日期 | 预达日期 | 完成日期 | 备注 | 
            
"; 
            $filename = $title.".xls";
			$tail = "";
            $this->excel->get_fz2($info_list,$titlename,$filename,$tail);
	    }
	}
	
	public function _excelzh()
	{
		if(isset($_GET['excel']))  
		{
			$warehouse = $this->input->get('warehouse',true);
			$sku = $this->input->get('sku',true);
			$cz = $this->input->get('cz',true);
			$text = $this->input->post('text',true);
			$xztime = $this->input->get('xztime',true);
			$timetk = $this->input->get('timetk',true);
			$timetj = $this->input->get('timetj',true);
			$timetk = strtotime($timetk);
			$timetj = strtotime($timetj);
			$where = "time = '0' ";
			if($warehouse)
            {
                $where  .= " and warehouse = '$warehouse'";
            }
			if($sku)
            {
                $where  .= " and sku = '$sku'";
            }
			if($text)
            {
                $where  .= " and text like '%$text%'";
            }
			if($cz != '')
            {
                $where  .= " and cz = '$cz'";
            }
			if($timetk && $timetj)
            {
                $where  .= " and ".$xztime." > '$timetk' and ".$xztime." < '$timetj'";
            }
            //数据排序
            $order_str = $xztime." desc";
            //取得信息列表
			$zh = array();
           $info_list = $this->whlabeltransport->find_all($where,'warehouse,sku,title,num,features,enter,stime,text');
			foreach ($info_list as $v) 
		    {
				$c = $this->whlabel->find_count('state = 0 and warehouse = "'.$v['warehouse'].'" and features = "'.$v['features'].'"');
				$warehouse = $this->warehouse->read($v['warehouse']);
				$v['warehouse'] = $warehouse['title'];
				$v['enter'] = date('Y-m-d H:i:s',$v['enter']);
				$v['stime'] = date('Y-m-d',$v['stime']);
				if(isset($zh[$v['features']]))
				{
					$zh[$v['features']]['num'] += $v['num'];
				}
				else
				{
					$zh[$v['features']] = $v;
				}
				$zh[$v['features']]['features'] = $c;
		    }
            $title = "在途数据".date('Y-m-d',time()); 
            $titlename = "
            
			| 仓库 | SKU | 名称 | 在途数量 | 库存数量 | 添加在途日期 | 预达日期 | 备注 | 
            
"; 
            $filename = $title.".xls";
			$tail = "";
            $this->excel->get_fz2($zh,$titlename,$filename,$tail);
	    }
	}
	
	//批量完成
	public function _list()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['s']))  
		{
			$cw = '';$i = 0;
            $id_arr = $this->input->post('s');
            $id_arr =  explode(',',$id_arr);
            if(!$id_arr)
            {
                echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
            }
            //循环删除记录
            foreach ($id_arr as $v) 
		    {
			    $id = $v;
			    $data = $this->whlabeltransport->read($id);
			    if($data['cz'] == '0')
			    {
				    $list = array();$num = "";$title = "";$features = "";
				    $order = $this->input->post('order',true);
				    $shop = $this->input->post('shop',true);
				    $label['shop'] = $data['shop'];
				    $label['sku'] = $data['sku'];
				    $label['warehouse'] = $data['warehouse'];
				    $label['purchase'] = $data['purchase'];
				    $label['retreatwarehouse'] = $data['retreatwarehouse'];
				    $label['state'] = 0;
				    $time = time();
				    $label['title'] = $data['title'];
				    $label['number'] = $data['number'];
				    $label['features'] = $data['features'];
				    $label['enter'] = $time;
				    $label['time'] = $time;//操作时间
				    if($label['warehouse'] == '5')
				    {
					    $yza = $this->whlabel->find_all("sku =  '".$label['sku']."' and warehouse = '".$label['warehouse']."'");
					    if($yza)
					    {
						    if($label['features'] != $yza[0]['features'])
						    {
							    echo json_encode(array('msg'=>'此SKU的产品信息和现有不一致!','success'=>false));exit;
						    }
					    }
					    $yzb = $this->whlabel->find_all("features =  '".$label['features']."' and warehouse = '".$label['warehouse']."'");
					    if($yzb)
					    {
						    if($label['sku'] != $yzb[0]['sku'])
						    {
							    echo json_encode(array('msg'=>'此产品信息的SKU和现有不一致!','success'=>false));exit;
						    }
					    }
				    }
				    $this->db->trans_begin();
				    for($i=0;$i<$data['num'];$i++)
			        {
					    $label['label'] = time().rand(100,999).$i;//产品标签 辨别各个产品,暂不使用
					    $label['cs'] = 1;//前期增加这个是在途库存添加的数据
						$label['rktype'] = 1;
				        $this->whlabel->insert($label);
			        }
				    $this->whlabeltransport->save(array('time'=>$time,'cz'=>1),$data['id']);
				    if ($this->db->trans_status() === TRUE)
        		    {
					    $this->db->trans_commit();
						$i++;
				    }
				    else
				    {
					    $this->db->trans_rollback();
					    $cw .= $data['sku'].' | ';
				    }
			    }
            }
			if(count($id_arr) == $i)
			{
				echo json_encode(array('msg'=>'入库成功!','success'=>true));exit;
			}
			else
			{
				echo json_encode(array('msg'=>'有入库失败数据:'.$cw,'success'=>false));exit;
			}
		}
    }
	
	//删除
	public function _del()
	{
		$post = $this->input->post(NULL, TRUE);
		if(isset($post['s']))  
		{
            $id_arr = $this->input->post('s');
            $id_arr =  explode(',',$id_arr);
            if(!$id_arr)
            {
                echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
            }
            //循环删除记录
            foreach ($id_arr as $v) 
		    {
				$data = $this->whlabeltransport->read($v);
				if($data['cz'] == '0')
				{
                    $this->whlabeltransport->remove($v);
				}
				else
				{
					echo json_encode(array('msg'=>'有已完成数据!无法继续删除','success'=>false));exit;
				}
            }
            echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
		}
    }
}