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;$iwarehouseyl->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 = "
错误详情
"; $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 = "
编码 产品名称 规格 尺寸 描述 入库 出库 结余
"; $filename = $title.".xls"; $tail = ""; $this->excel->get_fz2($rowsdq,$titlename,$filename,$tail); } } public function _drmb() { $title = '导入模板'; $titlename = "
编码 产品名称 规格 尺寸 描述 数量
1001 生料A 缅甸发 20 仓发 5
"; $filename = $title.".xls"; $tail = ""; $this->excel->get_fz2($rowsdq,$titlename,$filename,$tail); } }