Stock.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class stock extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_stock','stock');
  7. $this->load->_model('Model_typeclass','typeclass');
  8. $this->load->_model('Model_warehouse','warehouse');
  9. $this->load->_model('Model_product','product');
  10. $this->load->_model('Model_whlabel','whlabel');
  11. }
  12. //定义方法的调用规则 获取URI第二段值
  13. public function _remap($arg,$arg_array)
  14. {
  15. if($arg == 'add')//添加
  16. {
  17. $this->_add();
  18. }
  19. else if($arg == 'edit')//修改
  20. {
  21. $this->_edit($arg_array);
  22. }
  23. else if($arg == 'del')//修改
  24. {
  25. $this->_del();
  26. }
  27. else if($arg == 'list')
  28. {
  29. $this->_list();
  30. }
  31. else
  32. {
  33. $this->_index();
  34. }
  35. }
  36. //管理
  37. public function _index()
  38. {
  39. $post = $this->input->post(NULL, TRUE);
  40. if(isset($post['page']))
  41. {
  42. $page = $this->input->post('page',true);
  43. $perpage = $this->input->post('perpage',true);
  44. $sku = $this->input->post('sku',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. $where = "1=1 ";
  51. if($sku)
  52. {
  53. $where .= " and sku = '$sku'";
  54. }
  55. if($category)
  56. {
  57. $where .= " and category = '$category'";
  58. }
  59. if($grade)
  60. {
  61. $where .= " and grade = '$grade'";
  62. }
  63. if($size)
  64. {
  65. $where .= " and size = '$size'";
  66. }
  67. if($lowe)
  68. {
  69. $where .= " and lowe = '$lowe'";
  70. }
  71. if($color)
  72. {
  73. $where .= " and color = '$color'";
  74. }
  75. //数据排序
  76. $order_str = "id asc";
  77. if(empty($page))
  78. {
  79. $start = 0;
  80. $perpage = 1;
  81. }
  82. else
  83. {
  84. $start = ($page - 1)*$perpage;
  85. }
  86. //取得信息列表
  87. $info_list = $this->stock->find_all($where,'id,warehouse,sku,title,znum,ynum',$order_str,$start,$perpage);
  88. foreach ($info_list as $key=>$value)
  89. {
  90. $warehouse = $this->warehouse->read($value['warehouse']);
  91. $info_list[$key]['warehouse'] = $warehouse['title'];
  92. }
  93. $total = $this->stock->find_count($where);
  94. $pagenum = ceil($total/$perpage);
  95. $over = $total-($start+$perpage);
  96. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  97. echo json_encode($rows);exit;
  98. }
  99. $this->_Template('stock',$this->data);
  100. }
  101. //添加
  102. public function _add()
  103. {
  104. $post = $this->input->post(NULL, TRUE);
  105. if(isset($post['category']))
  106. {
  107. $category = $this->input->post('category',true);
  108. $warehouse = $this->input->post('warehouse',true);
  109. $znum = $this->input->post('znum',true);
  110. $ynum = $this->input->post('ynum',true);
  111. $hairtype = explode(',',rtrim($this->input->post('hairtype',true),','));
  112. $grade = explode(',',rtrim($this->input->post('grade',true),','));
  113. $size = explode(',',rtrim($this->input->post('size',true),','));
  114. $color = explode(',',rtrim($this->input->post('color',true),','));
  115. $lowe = explode(',',rtrim($this->input->post('lowe',true),','));
  116. $title = "";
  117. $ps = array('warehouse'=>$warehouse,'znum'=>$znum,'ynum'=>$ynum);
  118. $rows = array(array($category),$hairtype,$grade,$size,$color,$lowe);
  119. $key = array('category','hairtype','grade','size','color','lowe');
  120. if($category == 126 || $category == 131|| $category == 132)
  121. {
  122. $number = $this->cp($ps,$rows,$key);
  123. }
  124. else if($category == 127)
  125. {
  126. $type = $this->input->post('type',true);
  127. $headroad = explode(',',rtrim($this->input->post('headroad',true),','));
  128. $density = explode(',',rtrim($this->input->post('density',true),','));
  129. $lacesize = explode(',',rtrim($this->input->post('lacesize',true),','));
  130. if($type == 195 || $type == 197 || $type == 199)
  131. {
  132. array_push($rows,array($type),$headroad,$density,$lacesize);
  133. array_push($key,'type','headroad','density','lacesize');
  134. $number = $this->cp($ps,$rows,$key);
  135. }
  136. else
  137. {
  138. array_push($rows,array($type),$headroad,$density);
  139. array_push($key,'type','headroad','density');
  140. $number = $this->cp($ps,$rows,$key);
  141. }
  142. }
  143. else if($category == 128)
  144. {
  145. $lacetype = $this->input->post('lacetype',true);
  146. $haircap = explode(',',rtrim($this->input->post('haircap',true),','));
  147. $density = explode(',',rtrim($this->input->post('density',true),','));
  148. array_push($rows,array($lacetype),$haircap,$density);
  149. array_push($key,'lacetype','haircap','density');
  150. $number = $this->cp($ps,$rows,$key);
  151. }
  152. else if($category == 129)
  153. {
  154. $wide = explode(',',rtrim($this->input->post('wide',true),','));
  155. array_push($rows,$wide);
  156. array_push($key,'wide');
  157. $number = $this->cp($ps,$rows,$key);
  158. }
  159. else if($category == 130)
  160. {
  161. $weight = explode(',',rtrim($this->input->post('weight',true),','));
  162. array_push($rows,$weight);
  163. array_push($key,'weight');
  164. $number = $this->cp($ps,$rows,$key);
  165. }
  166. else if($category == 133)
  167. {
  168. $package = explode(',',rtrim($this->input->post('package',true),','));
  169. $unitweight = explode(',',rtrim($this->input->post('unitweight',true),','));
  170. array_push($rows,$package,$unitweight);
  171. array_push($key,'package','unitweight');
  172. $number = $this->cp($ps,$rows,$key);
  173. }
  174. else if($category == 134)
  175. {
  176. $pieceweight = explode(',',rtrim($this->input->post('pieceweight',true),','));
  177. array_push($rows,$pieceweight);
  178. array_push($key,'pieceweight');
  179. $number = $this->cp($ps,$rows,$key);
  180. }
  181. if(isset($number))
  182. {
  183. echo json_encode(array('msg'=>'成功!重复:'.$number['0'].' 条,无配置:'.$number['1'].'条','success'=>true));exit;
  184. }
  185. else
  186. {
  187. echo json_encode(array('msg'=>'提交失败,请重试','success'=>true));exit;
  188. }
  189. }
  190. $this->_Template('stock_add',$this->data);
  191. }
  192. //修改
  193. public function _edit($arg_array)
  194. {
  195. $post = $this->input->post(NULL, TRUE);
  196. if(isset($post['id']))
  197. {
  198. $list = array();$num = "";$title = "";
  199. $id = $this->input->post('id',true);
  200. $data = $this->stock->read($id);
  201. foreach($data as $k=>$v)
  202. {
  203. $post[$k] = 0;
  204. }
  205. $post['id'] = $id;
  206. $post['sku'] = $this->input->post('sku',true);
  207. $post['warehouse'] = $this->input->post('warehouse',true);
  208. $post['znum'] = $this->input->post('znum',true);
  209. $post['ynum'] = $this->input->post('ynum',true);
  210. $category = $this->input->post('category',true);
  211. $list['category'] = $category;
  212. $list['hairtype'] = $this->input->post('hairtype',true);
  213. $list['grade'] = $this->input->post('grade',true);
  214. $list['size'] = $this->input->post('size',true);
  215. $list['color'] = $this->input->post('color',true);
  216. $list['lowe'] = $this->input->post('lowe',true);
  217. if($category == 127)
  218. {
  219. $list['type'] = $this->input->post('type',true);
  220. $list['headroad'] = $this->input->post('headroad',true);
  221. $list['density'] = $this->input->post('density',true);
  222. if($list['type'] == 195 || $list['type'] == 197 || $list['type'] == 199)
  223. {
  224. $list['lacesize'] = $this->input->post('lacesize',true);
  225. }
  226. }
  227. else if($category == 128)
  228. {
  229. $list['lacetype'] = $this->input->post('lacetype',true);
  230. $list['haircap'] = $this->input->post('haircap',true);
  231. $list['density'] = $this->input->post('density',true);
  232. }
  233. else if($category == 129)
  234. {
  235. $list['wide'] = $this->input->post('wide',true);
  236. }
  237. else if($category == 130)
  238. {
  239. $list['weight'] = $this->input->post('weight',true);
  240. }
  241. else if($category == 133)
  242. {
  243. $list['package'] = $this->input->post('package',true);
  244. $list['unitweight'] = $this->input->post('unitweight',true);
  245. }
  246. else if($category == 134)
  247. {
  248. $list['pieceweight'] = $this->input->post('pieceweight',true);
  249. }
  250. foreach($list as $k=>$v)
  251. {
  252. if($v != 0)
  253. {
  254. $num .=$v;
  255. $post[$k] = $v;
  256. $typeclass = $this->typeclass->read($v);
  257. $title = $title.$typeclass['title']." ";
  258. }
  259. }
  260. $post['title'] = rtrim($title,' ');
  261. $post['number'] = $num;
  262. $rows = $this->stock->get_number($num);
  263. if(!$rows || $rows['id'] == $id)
  264. {
  265. $this->stock->save($post,$id);
  266. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  267. }
  268. else
  269. {
  270. echo json_encode(array('msg'=>'修改失败,有重复数据','success'=>false));exit;
  271. }
  272. }
  273. $arg_array = $arg_array[0];
  274. $stock = $this->stock->read($arg_array);
  275. $this->data['stock'] = $stock;
  276. $this->_Template('stock_edit',$this->data);
  277. }
  278. //删除
  279. public function _del()
  280. {
  281. $post = $this->input->post(NULL, TRUE);
  282. if(isset($post['s']))
  283. {
  284. $id_arr = $this->input->post('s');
  285. $id_arr = explode(',',$id_arr);
  286. if(!$id_arr)
  287. {
  288. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  289. }
  290. //循环删除记录
  291. foreach ($id_arr as $v)
  292. {
  293. $this->stock->remove($v);
  294. }
  295. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  296. }
  297. }
  298. //笛卡儿积
  299. function cp($ps,$sets,$list)
  300. {
  301. $result = array();$j=0;$l="";
  302. for($i=0,$count=count($sets); $i<$count-1; $i++)
  303. {
  304. if($i==0)
  305. {
  306. $result = $sets[$i];
  307. }
  308. $tmp = array();
  309. foreach($result as $res)
  310. {
  311. foreach($sets[$i+1] as $set)
  312. {
  313. $tmp[] = $res.','.$set;
  314. }
  315. }
  316. $result = $tmp;
  317. }
  318. $this->db->trans_begin();
  319. foreach ($result as $v)
  320. {
  321. $post = array();$title = "";$num="";
  322. $data = explode(',',$v);
  323. for($i=0;$i<count($list); $i++)
  324. {
  325. $post[$list[$i]] = $data[$i];
  326. $typeclass = $this->typeclass->read($data[$i]);
  327. $title = $title.$typeclass['title']." ";
  328. if($data[$i] != 0)
  329. {
  330. $num .=$data[$i];//所有数据ID拼接
  331. }
  332. }
  333. foreach ($ps as $kp=>$vp)
  334. {
  335. $post[$kp] = $vp;
  336. }
  337. $post['number'] = $num;
  338. $info_list = $this->stock->get_number($num);//查询stock是否存在此拼接的数据ID
  339. if($info_list)
  340. {
  341. $j++;
  342. }
  343. else
  344. {
  345. $csku = $this->product->get_number($num);
  346. if($csku)
  347. {
  348. $post['sku'] = $csku['sku'];
  349. $post['title'] = rtrim($title,' ');
  350. $this->stock->insert($post);
  351. }
  352. else
  353. {
  354. $l++;
  355. }
  356. }
  357. }
  358. if ($this->db->trans_status() === FALSE)
  359. {
  360. $this->db->trans_commit();
  361. return NULL;
  362. }
  363. else
  364. {
  365. $this->db->trans_commit();
  366. return array('0'=>$j,'1'=>$l);
  367. }
  368. }
  369. //管理
  370. public function _list()
  371. {
  372. $post = $this->input->post(NULL, TRUE);
  373. if(isset($post['page']))
  374. {
  375. $page = $this->input->post('page',true);
  376. $perpage = $this->input->post('perpage',true);
  377. $sku = $this->input->post('sku',true);
  378. $category = $this->input->post('category',true);
  379. $grade = $this->input->post('grade',true);
  380. $size = $this->input->post('size',true);
  381. $lowe = $this->input->post('lowe',true);
  382. $color = $this->input->post('color',true);
  383. $where = "1=1 ";
  384. if($sku)
  385. {
  386. $where .= " and sku = '$sku'";
  387. }
  388. if($category)
  389. {
  390. $where .= " and category = '$category'";
  391. }
  392. if($grade)
  393. {
  394. $where .= " and grade = '$grade'";
  395. }
  396. if($size)
  397. {
  398. $where .= " and size = '$size'";
  399. }
  400. if($lowe)
  401. {
  402. $where .= " and lowe = '$lowe'";
  403. }
  404. if($color)
  405. {
  406. $where .= " and color = '$color'";
  407. }
  408. //数据排序
  409. $order_str = "id asc";
  410. if(empty($page))
  411. {
  412. $start = 0;
  413. $perpage = 1;
  414. }
  415. else
  416. {
  417. $start = ($page - 1)*$perpage;
  418. }
  419. //取得信息列表
  420. $info_list = $this->stock->find_all($where,'id,warehouse,sku,title,pieceweight,znum,ynum,number',$order_str,$start,$perpage);
  421. foreach ($info_list as $key=>$value)
  422. {
  423. $warehouse = $this->warehouse->read($value['warehouse']);
  424. $info_list[$key]['warehouse'] = $warehouse['title'];
  425. $n = $this->whlabel->find_count('sku = "'.$value['sku'].'"');
  426. $info_list[$key]['pieceweight'] = $n;
  427. if($value['znum'] > $n)
  428. {
  429. if($value['ynum'] > $n)
  430. {
  431. $info_list[$key]['number'] = "<font style='color:#f33'>".($value['znum']-$n)."</font>";
  432. }
  433. else
  434. {
  435. $info_list[$key]['number'] = $value['znum']-$n;
  436. }
  437. }
  438. }
  439. $total = $this->stock->find_count($where);
  440. $pagenum = ceil($total/$perpage);
  441. $over = $total-($start+$perpage);
  442. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  443. echo json_encode($rows);exit;
  444. }
  445. $this->_Template('stock_list',$this->data);
  446. }
  447. }