Productdescribe.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Productdescribe extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_productdescribe','productdescribe');
  7. $this->load->_model('Model_product','product');
  8. $this->load->_model('Model_productprice','productprice');
  9. $this->load->_model('Model_typeclass','typeclass');
  10. $this->load->_model('Model_excel','excel');
  11. $this->load->_model('Model_whlabel','whlabel');
  12. }
  13. //定义方法的调用规则 获取URI第二段值
  14. public function _remap($arg,$arg_array)
  15. {
  16. if($arg == 'add')//添加
  17. {
  18. $this->_add($arg_array);
  19. }
  20. else if($arg == 'edit')//修改
  21. {
  22. $this->_edit($arg_array);
  23. }
  24. else if($arg == 'del')//修改
  25. {
  26. $this->_del();
  27. }
  28. else if($arg == 'money')
  29. {
  30. $this->_money($arg_array);
  31. }
  32. else
  33. {
  34. $this->_index($arg_array);
  35. }
  36. }
  37. //管理
  38. public function _index($arg_array)
  39. {
  40. $post = $this->input->post(NULL, TRUE);
  41. if(isset($post['page']))
  42. {
  43. $page = $this->input->post('page',true);
  44. $perpage = $this->input->post('perpage',true);
  45. $category = $this->input->post('category',true);
  46. $grade = $this->input->post('grade',true);
  47. $size = $this->input->post('size',true);
  48. $lowe = $this->input->post('lowe',true);
  49. $color = $this->input->post('color',true);
  50. $sku = $this->input->post('sku',true);
  51. $arg_array = $this->input->post('arg_array',true);
  52. $where = "1=1 ";
  53. if($category)
  54. {
  55. $where .= " and features like '%-$category-%'";
  56. }
  57. if($grade)
  58. {
  59. $where .= " and features like '%-$grade-%'";
  60. }
  61. if($size)
  62. {
  63. $where .= " and features like '%-$size-%'";
  64. }
  65. if($lowe)
  66. {
  67. $where .= " and features like '%-$lowe-%'";
  68. }
  69. if($color)
  70. {
  71. $where .= " and features like '%-$color-%'";
  72. }
  73. if($sku)
  74. {
  75. $where .= " and sku like '%$sku%'";
  76. }
  77. if($arg_array || $arg_array != 0)
  78. {
  79. $where .= " and purchase = '$arg_array'";
  80. }
  81. //数据排序
  82. $order_str = "id asc";
  83. if(empty($page))
  84. {
  85. $start = 0;
  86. $perpage = 1;
  87. }
  88. else
  89. {
  90. $start = ($page - 1)*$perpage;
  91. }
  92. //取得信息列表
  93. $info_list = $this->productdescribe->find_all($where,'id,sku,title,money',$order_str,$start,$perpage);
  94. $total = $this->productdescribe->find_count($where);
  95. $pagenum = ceil($total/$perpage);
  96. $over = $total-($start+$perpage);
  97. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  98. echo json_encode($rows);exit;
  99. }
  100. if(isset($arg_array[0]))
  101. {
  102. $arg_array = $arg_array[0];
  103. }
  104. else
  105. {
  106. $arg_array = "";
  107. }
  108. $this->data['arg_array'] = $arg_array;
  109. $this->_Template('productdescribe',$this->data);
  110. }
  111. //添加
  112. public function _add($arg_array)
  113. {
  114. $post = $this->input->post(NULL, TRUE);
  115. if(isset($post['category']))
  116. {
  117. if($this->productdescribe->insert($post))
  118. {
  119. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  120. }
  121. else
  122. {
  123. echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
  124. }
  125. }
  126. $this->_Template('productdescribe_add',$this->data);
  127. }
  128. //修改
  129. public function _edit($arg_array)
  130. {
  131. $post = $this->input->post(NULL, TRUE);
  132. if(isset($post['id']))
  133. {
  134. $id = $this->input->post('id',true);
  135. if($this->productdescribe->save($post,$id))
  136. {
  137. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  138. }
  139. else
  140. {
  141. echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
  142. }
  143. }
  144. $arg_array = $arg_array[0];
  145. $productdescribe = $this->productdescribe->read($arg_array);
  146. $this->data['productdescribe'] = $productdescribe;
  147. $this->_Template('productdescribe_edit',$this->data);
  148. }
  149. //删除
  150. public function _del()
  151. {
  152. $post = $this->input->post(NULL, TRUE);
  153. if(isset($post['s']))
  154. {
  155. $id_arr = $this->input->post('s');
  156. $id_arr = explode(',',$id_arr);
  157. if(!$id_arr)
  158. {
  159. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  160. }
  161. //循环删除记录
  162. foreach ($id_arr as $v)
  163. {
  164. $this->productdescribe->remove($v);
  165. }
  166. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  167. }
  168. }
  169. //数量调整模板导入
  170. public function _money($arg_array)
  171. {
  172. $dir = '/data/excel/'.date('Ymd',time()).'/';
  173. $config['upload_path'] = '.'.$dir ;
  174. $config['file_name'] = date('Ymd_His_',time()).rand(1000,9999);
  175. $config['allowed_types'] = 'xls|xlsx|csv';
  176. $config['max_size'] = 10240;
  177. $this->load->library('upload', $config);
  178. $this->upload->initialize($config);
  179. if ($this->upload->do_upload('userfile'))
  180. {
  181. $full_path = $dir.$this->upload->data('file_name');
  182. $fileName = '.' . $full_path;
  183. if (!file_exists($fileName))
  184. {
  185. echo json_encode(array('msg'=>"上传失败,请重试",'success'=>false));exit;
  186. }
  187. else
  188. {
  189. libxml_use_internal_errors(true);
  190. require_once "./data/excel/PHPExcel/IOFactory.php";
  191. $phpExcel = PHPExcel_IOFactory::load($fileName);// 载入当前文件
  192. $phpExcel->setActiveSheetIndex(0);// 设置为默认表
  193. $sheetCount = $phpExcel->getSheetCount();// 获取表格数量
  194. $row = $phpExcel->getActiveSheet()->getHighestRow();// 获取行数
  195. $column = $phpExcel->getActiveSheet()->getHighestColumn();// 获取列数
  196. ++$column;//如果列数大于26行
  197. $list = array();
  198. for ($i = 2; $i <= $row; $i++) // 行数循环
  199. {
  200. $data = array();
  201. for ($c = 'A'; $c != $column; $c++) // 列数循环
  202. {
  203. $data[] = $phpExcel->getActiveSheet()->getCell($c . $i)->getValue();
  204. }
  205. $list[] = $data;
  206. }
  207. }
  208. $i = 0;$j = 0;$ed = array();
  209. foreach ($list as $key=>$value)
  210. {
  211. $time = time();
  212. $sku = $value['0'];
  213. if($sku == "" || $value['3'] == "" || !isset($arg_array[0]))
  214. {
  215. continue;
  216. }
  217. $d = $this->whlabel->get_sku($sku);
  218. $p = $this->productdescribe->get_sku($sku);
  219. if(!$d)//如果没有这个SKU
  220. {
  221. $ed[] = array($sku.'-库存中不存在此SKU');
  222. $j++;
  223. continue;
  224. }
  225. if(!is_numeric($value['3']))
  226. {
  227. $ed[] = array($sku.'-'.$value['3'].'-金额错误!');
  228. $j++;
  229. continue;
  230. }
  231. if(isset($p['money']) && $p['money'] != $value['3'])
  232. {
  233. $this->productdescribe->save(array('money'=>$value['3']),$p['id']);
  234. }
  235. else if(!isset($p['money']))
  236. {
  237. $this->productdescribe->insert(array('sku'=>$sku,'number'=>$d['number'],'features'=>$d['features'],'title'=>$d['title'],'time'=>$time,'money'=>$value['3'],'purchase'=>$arg_array[0]));
  238. }
  239. }
  240. if ($this->db->trans_status() === FALSE)
  241. {
  242. $this->db->trans_commit();
  243. echo json_encode(array('msg'=>'导入失败,请重试,','error'=>$error,'success'=>true));exit;
  244. }
  245. else
  246. {
  247. $this->db->trans_commit();
  248. if($j > 0)
  249. {
  250. $tt = date('Ymd',time());
  251. $title = '库存导入错误信息-'.$tt;
  252. $titlename = "<table border=1><tr><td>错误详情</td></tr></table>";
  253. $tail = "\n";
  254. $filename = $title.".xls";
  255. $ecl = $this->excel->get_fz3($ed,$titlename,$filename,$tail);
  256. $dir = '/data/excel/'.$time.'/';
  257. $file_name = 'error_'.$time.rand(1000,9999);
  258. if(!is_dir('.'.$dir))mkdir('.'.$dir,0777);
  259. $myfile = fopen(".".$dir.$file_name.".xls", "w") or die();
  260. fwrite($myfile,$ecl);
  261. fclose($myfile);
  262. $error = $dir.$file_name.'.xls';
  263. echo json_encode(array('msg'=>'导入成功,'.$j.'条异常,','error'=>$error,'success'=>true));exit;
  264. }
  265. else
  266. {
  267. echo json_encode(array('msg'=>'导入成功!','error'=>1,'success'=>true));exit;
  268. }
  269. }
  270. }
  271. }
  272. }