load->library('session'); $this->load->_model('Model_productdescribe','productdescribe'); $this->load->_model('Model_product','product'); $this->load->_model('Model_productprice','productprice'); $this->load->_model('Model_typeclass','typeclass'); $this->load->_model('Model_excel','excel'); $this->load->_model('Model_whlabel','whlabel'); } //定义方法的调用规则 获取URI第二段值 public function _remap($arg,$arg_array) { if($arg == 'add')//添加 { $this->_add($arg_array); } else if($arg == 'edit')//修改 { $this->_edit($arg_array); } else if($arg == 'del')//修改 { $this->_del(); } else if($arg == 'money') { $this->_money($arg_array); } else { $this->_index($arg_array); } } //管理 public function _index($arg_array) { $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); $grade = $this->input->post('grade',true); $size = $this->input->post('size',true); $lowe = $this->input->post('lowe',true); $color = $this->input->post('color',true); $sku = $this->input->post('sku',true); $arg_array = $this->input->post('arg_array',true); $where = "1=1 "; if($category) { $where .= " and features like '%-$category-%'"; } if($grade) { $where .= " and features like '%-$grade-%'"; } if($size) { $where .= " and features like '%-$size-%'"; } if($lowe) { $where .= " and features like '%-$lowe-%'"; } if($color) { $where .= " and features like '%-$color-%'"; } if($sku) { $where .= " and sku like '%$sku%'"; } if($arg_array || $arg_array != 0) { $where .= " and purchase = '$arg_array'"; } //数据排序 $order_str = "id asc"; if(empty($page)) { $start = 0; $perpage = 1; } else { $start = ($page - 1)*$perpage; } //取得信息列表 $info_list = $this->productdescribe->find_all($where,'id,sku,title,money',$order_str,$start,$perpage); $total = $this->productdescribe->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; } if(isset($arg_array[0])) { $arg_array = $arg_array[0]; } else { $arg_array = ""; } $this->data['arg_array'] = $arg_array; $this->_Template('productdescribe',$this->data); } //添加 public function _add($arg_array) { $post = $this->input->post(NULL, TRUE); if(isset($post['category'])) { if($this->productdescribe->insert($post)) { echo json_encode(array('msg'=>'修改成功','success'=>true));exit; } else { echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit; } } $this->_Template('productdescribe_add',$this->data); } //修改 public function _edit($arg_array) { $post = $this->input->post(NULL, TRUE); if(isset($post['id'])) { $id = $this->input->post('id',true); if($this->productdescribe->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]; $productdescribe = $this->productdescribe->read($arg_array); $this->data['productdescribe'] = $productdescribe; $this->_Template('productdescribe_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->productdescribe->remove($v); } echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true)); } } //数量调整模板导入 public function _money($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(); $sku = $value['0']; if($sku == "" || $value['3'] == "" || !isset($arg_array[0])) { continue; } $d = $this->whlabel->get_sku($sku); $p = $this->productdescribe->get_sku($sku); if(!$d)//如果没有这个SKU { $ed[] = array($sku.'-库存中不存在此SKU'); $j++; continue; } if(!is_numeric($value['3'])) { $ed[] = array($sku.'-'.$value['3'].'-金额错误!'); $j++; continue; } if(isset($p['money']) && $p['money'] != $value['3']) { $this->productdescribe->save(array('money'=>$value['3']),$p['id']); } else if(!isset($p['money'])) { $this->productdescribe->insert(array('sku'=>$sku,'number'=>$d['number'],'features'=>$d['features'],'title'=>$d['title'],'time'=>$time,'money'=>$value['3'],'purchase'=>$arg_array[0])); } } if ($this->db->trans_status() === FALSE) { $this->db->trans_commit(); echo json_encode(array('msg'=>'导入失败,请重试,','error'=>$error,'success'=>true));exit; } else { $this->db->trans_commit(); 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; } } } } }