| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 | <?php defined('BASEPATH') OR exit('No direct script access allowed');class Warehouseyl extends Start_Controller {	public function __construct(){		parent::__construct();		$this->load->library('session');		$this->load->_model('Model_warehouseyl','warehouseyl');		$this->load->_model('Model_excel','excel');	}	//定义方法的调用规则 获取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 == 'rows')//修改        {             $this->_rows();        }		else if($arg == 'presetedit')//修改        {             $this->_presetedit($arg_array);        }		else if($arg == 'dcexcel')        {             $this->_dcexcel();        }		else if($arg == 'drmb')        {             $this->_drmb();        }		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);			$sku = $this->input->post('sku',true);			$classtitle = $this->input->post('classtitle',true);			$title = $this->input->post('title',true);			$state = $this->input->post('state',true);			$ktime = $this->input->post('ktime',true);			$jtime = $this->input->post('jtime',true);			$ktime = strtotime($ktime);			$jtime = strtotime($jtime);			$where = "1=1 ";            //数据排序            $order_str = "id asc";            if(empty($page))		    {                $start = 0;		    	$perpage = 1;            }		    else		    {                $start = ($page - 1)*$perpage;            }			if($sku)            {                $where  .= " and sku like '%$sku%'";            }			if($classtitle)            {                $where  .= " and classtitle like '%$classtitle%'";            }			if($title)            {                $where  .= " and title like '%$title%'";            }			if($state != '')            {                $where  .= " and state = '$state'";            }			if($ktime && $jtime)            {				if($state == '')				{                    $where  .= " and time > '$ktime' and time < '$jtime'";				}				else if($state == '0')				{					$where  .= " and rtime > '$ktime' and rtime < '$jtime'";				}				else if($state == '1')				{					$where  .= " and ctime > '$ktime' and ctime < '$jtime'";				}            }            //取得信息列表            $info_list = $this->warehouseyl->find_all($where,'id,sku,title,specs,size,text',$order_str);			$rows = array();$list = array();			foreach ($info_list as $key=>$value) 		    {				$rows[strtolower($value['sku'])] = $value;//所有数据sku		    }			$rowsdq = array_slice($rows,$start,$perpage);			 //格式化数据            foreach ($rowsdq as $key=>$value) 		    {				$r = $this->warehouseyl->find_count($where.' and sku = "'.$value['sku'].'" and state = "0"');				$c = $this->warehouseyl->find_count($where.' and sku = "'.$value['sku'].'" and state = "1"');				$rowsdq[$key]['r'] = $r+$c;				$rowsdq[$key]['c'] = $c;				$rowsdq[$key]['j'] = $r;            }		    $total = $this->warehouseyl->find_count($where);		    $pagenum = ceil($total/$perpage);		    $over = $total-($start+$perpage);		    $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($rowsdq));		    echo json_encode($rows);exit;		}		$this->_Template('warehouseyl',$this->data);	}	//添加	public function _add()	{		$post = $this->input->post(NULL, TRUE);		if(isset($post['sku']))		{			$time = time();			$num = $this->input->post('num',true);			if($num < 1)			{				echo json_encode(array('msg'=>'添加数量不正确!','success'=>false));exit;			}			$post['time'] = $time;			$post['rtime'] = $time;			$this->db->trans_begin();			for($i=0;$i<$num;$i++)			{				$this->warehouseyl->insert($post);			}			if ($this->db->trans_status() === FALSE)			{    			$this->db->trans_rollback();				echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;			}			else			{    			$this->db->trans_commit();				echo json_encode(array('msg'=>'添加成功','success'=>true));exit;			}		}		$this->_Template('warehouseyl_add',$this->data);	}	//修改	public function _edit($arg_array)	{		$post = $this->input->post(NULL, TRUE);		if(isset($post['id']))		{			$time = time();			$num = $this->input->post('num',true);			if($num < 1)			{				echo json_encode(array('msg'=>'出库数量不正确!','success'=>false));exit;			}			$id = $this->input->post('id',true);			$wyl = $this->warehouseyl->read($id);			$warehouseyl = $this->warehouseyl->find_all("sku = '".$wyl['sku']."' and state = '0'");			if($num > count($warehouseyl))			{				echo json_encode(array('msg'=>'出库数量大于库存数量!','success'=>false));exit;			}			$this->db->trans_begin();			for($i=0;$i<$num;$i++)		    {				$this->warehouseyl->save(array('ctime'=>$time,'time'=>$time,'state'=>1),$warehouseyl[$i]['id']);            }			if ($this->db->trans_status() === FALSE)			{    			$this->db->trans_rollback();				echo json_encode(array('msg'=>'操作失败,请重试','success'=>false));exit;			}			else			{    			$this->db->trans_commit();				echo json_encode(array('msg'=>'操作成功','success'=>true));exit;			}		}		$arg_array = $arg_array[0];		$warehouseyl = $this->warehouseyl->read($arg_array);		$num = $this->warehouseyl->find_count("sku = '".$warehouseyl['sku']."' and state = '0'");		$warehouseyl['num'] = $num;		$this->data['warehouseyl'] = $warehouseyl;		$this->_Template('warehouseyl_edit',$this->data);	}	//删除	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) 		    {                $this->warehouseyl->remove($v);            }            echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));		}    }		public function _presetedit($arg_array)	{		$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;				}				$sku = $value['0'];				$title =  $value['1'];				$specs =  $value['2'];				$size =  $value['3'];				$text =  $value['4'];				$num =  $value['5'];//正数增加负数扣除				$d = $this->warehouseyl->get_sku($sku);				if($num < 0)//扣减库存				{					if(!$d)//如果没有这个SKU					{						$ed[] = array($sku.'-库存中不存在此SKU');						$j++;						continue;					}					$data = $this->warehouseyl->find_all('sku = "'.$sku.'" state = "0"');					if(count($data) < abs($num))					{						$ed[] = array($sku.'-扣减数量大于库存剩余数量!');						$j++;						continue;					}					for($i=0;$i<abs($num);$i++)					{						$this->warehouseyl->save(array('state'=>1,'ctime'=>$time,'time'=>$time),$data[$i]['id']);					}				}				else if($num > 0)//增加库存				{					$post['sku'] = $sku;					$post['title'] = $title;					$post['specs'] = $specs;					$post['size'] = $size;					$post['text'] = $text;					$post['state'] = 0;					$post['rtime'] = $time;					$post['time'] = $time;					for($i=0;$i<$num;$i++)					{						$this->warehouseyl->insert($post);					}				}				sleep(1);//防止入库时间一样,需要延迟1秒			}			if($j > 0)			{				$tt = date('Ymd',time());				$title = '库存导入错误信息-'.$tt;        		$titlename = "<table border=1><tr><td>错误详情</td></tr></table>";				$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 _dcexcel()	{		if(isset($_GET['excel']))  		{			$ktime = $this->input->get('ktime',true);			$jtime = $this->input->get('jtime',true);			$ktime = strtotime($ktime);			$jtime = strtotime($jtime);			$where = "1=1";			if($ktime && $jtime)            {                $where  .= " and time > '$ktime' and time < '$jtime'";            }            //数据排序            $order_str = "time desc";			 $info_list = $this->warehouseyl->find_all($where,'sku,title,specs,size,text',$order_str);			$rows = array();$list = array();			foreach ($info_list as $key=>$value) 		    {				$rows[strtolower($value['sku'])] = $value;//所有数据sku		    }			$rowsdq = $rows;			 //格式化数据            foreach ($rowsdq as $key=>$value) 		    {				$r = $this->warehouseyl->find_count($where.' and sku = "'.$value['sku'].'" and state = "0"');				$c = $this->warehouseyl->find_count($where.' and sku = "'.$value['sku'].'" and state = "1"');				$rowsdq[$key]['r'] = $r+$c;				$rowsdq[$key]['c'] = $c;				$rowsdq[$key]['j'] = $r;            }								$title = date('Y-m-d',$ktime).'至'.date('Y-m-d',$jtime).'详情';                 $titlename = "<table border=1>                <tr align='center'>				<td>编码</td>                <td>产品名称</td>                <td>规格</td>			    <td>尺寸</td>				<td>描述</td>				<td>入库</td>				<td>出库</td>				<td>结余</td>                </tr>                </table>";                 $filename = $title.".xls";			    $tail = "";                $this->excel->get_fz2($rowsdq,$titlename,$filename,$tail);	    }	}		public function _drmb()	{				$title = '导入模板';                 $titlename = "<table border=1>                <tr align='center'>				<td>编码</td>                <td>产品名称</td>                <td>规格</td>			    <td>尺寸</td>				<td>描述</td>				<td>数量</td>                </tr>				<tr align='center'>				<td>1001</td>                <td>生料A</td>                <td>缅甸发</td>			    <td>20</td>				<td>仓发</td>				<td>5</td>                </tr>                </table>";                 $filename = $title.".xls";			    $tail = "";                $this->excel->get_fz2($rowsdq,$titlename,$filename,$tail);	    	}}
 |