Warehouseyl.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Warehouseyl extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_warehouseyl','warehouseyl');
  7. $this->load->_model('Model_excel','excel');
  8. }
  9. //定义方法的调用规则 获取URI第二段值
  10. public function _remap($arg,$arg_array)
  11. {
  12. if($arg == 'add')//添加
  13. {
  14. $this->_add();
  15. }
  16. else if($arg == 'edit')//修改
  17. {
  18. $this->_edit($arg_array);
  19. }
  20. else if($arg == 'del')//修改
  21. {
  22. $this->_del();
  23. }
  24. else if($arg == 'rows')//修改
  25. {
  26. $this->_rows();
  27. }
  28. else if($arg == 'presetedit')//修改
  29. {
  30. $this->_presetedit($arg_array);
  31. }
  32. else if($arg == 'dcexcel')
  33. {
  34. $this->_dcexcel();
  35. }
  36. else if($arg == 'drmb')
  37. {
  38. $this->_drmb();
  39. }
  40. else
  41. {
  42. $this->_index();
  43. }
  44. }
  45. //管理
  46. public function _index()
  47. {
  48. $post = $this->input->post(NULL, TRUE);
  49. if(isset($post['page']))
  50. {
  51. $page = $this->input->post('page',true);
  52. $perpage = $this->input->post('perpage',true);
  53. $sku = $this->input->post('sku',true);
  54. $classtitle = $this->input->post('classtitle',true);
  55. $title = $this->input->post('title',true);
  56. $state = $this->input->post('state',true);
  57. $ktime = $this->input->post('ktime',true);
  58. $jtime = $this->input->post('jtime',true);
  59. $ktime = strtotime($ktime);
  60. $jtime = strtotime($jtime);
  61. $where = "1=1 ";
  62. //数据排序
  63. $order_str = "id asc";
  64. if(empty($page))
  65. {
  66. $start = 0;
  67. $perpage = 1;
  68. }
  69. else
  70. {
  71. $start = ($page - 1)*$perpage;
  72. }
  73. if($sku)
  74. {
  75. $where .= " and sku like '%$sku%'";
  76. }
  77. if($classtitle)
  78. {
  79. $where .= " and classtitle like '%$classtitle%'";
  80. }
  81. if($title)
  82. {
  83. $where .= " and title like '%$title%'";
  84. }
  85. if($state != '')
  86. {
  87. $where .= " and state = '$state'";
  88. }
  89. if($ktime && $jtime)
  90. {
  91. if($state == '')
  92. {
  93. $where .= " and time > '$ktime' and time < '$jtime'";
  94. }
  95. else if($state == '0')
  96. {
  97. $where .= " and rtime > '$ktime' and rtime < '$jtime'";
  98. }
  99. else if($state == '1')
  100. {
  101. $where .= " and ctime > '$ktime' and ctime < '$jtime'";
  102. }
  103. }
  104. //取得信息列表
  105. $info_list = $this->warehouseyl->find_all($where,'id,sku,title,specs,size,text',$order_str);
  106. $rows = array();$list = array();
  107. foreach ($info_list as $key=>$value)
  108. {
  109. $rows[strtolower($value['sku'])] = $value;//所有数据sku
  110. }
  111. $rowsdq = array_slice($rows,$start,$perpage);
  112. //格式化数据
  113. foreach ($rowsdq as $key=>$value)
  114. {
  115. $r = $this->warehouseyl->find_count($where.' and sku = "'.$value['sku'].'" and state = "0"');
  116. $c = $this->warehouseyl->find_count($where.' and sku = "'.$value['sku'].'" and state = "1"');
  117. $rowsdq[$key]['r'] = $r+$c;
  118. $rowsdq[$key]['c'] = $c;
  119. $rowsdq[$key]['j'] = $r;
  120. }
  121. $total = $this->warehouseyl->find_count($where);
  122. $pagenum = ceil($total/$perpage);
  123. $over = $total-($start+$perpage);
  124. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($rowsdq));
  125. echo json_encode($rows);exit;
  126. }
  127. $this->_Template('warehouseyl',$this->data);
  128. }
  129. //添加
  130. public function _add()
  131. {
  132. $post = $this->input->post(NULL, TRUE);
  133. if(isset($post['sku']))
  134. {
  135. $time = time();
  136. $num = $this->input->post('num',true);
  137. if($num < 1)
  138. {
  139. echo json_encode(array('msg'=>'添加数量不正确!','success'=>false));exit;
  140. }
  141. $post['time'] = $time;
  142. $post['rtime'] = $time;
  143. $this->db->trans_begin();
  144. for($i=0;$i<$num;$i++)
  145. {
  146. $this->warehouseyl->insert($post);
  147. }
  148. if ($this->db->trans_status() === FALSE)
  149. {
  150. $this->db->trans_rollback();
  151. echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
  152. }
  153. else
  154. {
  155. $this->db->trans_commit();
  156. echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
  157. }
  158. }
  159. $this->_Template('warehouseyl_add',$this->data);
  160. }
  161. //修改
  162. public function _edit($arg_array)
  163. {
  164. $post = $this->input->post(NULL, TRUE);
  165. if(isset($post['id']))
  166. {
  167. $time = time();
  168. $num = $this->input->post('num',true);
  169. if($num < 1)
  170. {
  171. echo json_encode(array('msg'=>'出库数量不正确!','success'=>false));exit;
  172. }
  173. $id = $this->input->post('id',true);
  174. $wyl = $this->warehouseyl->read($id);
  175. $warehouseyl = $this->warehouseyl->find_all("sku = '".$wyl['sku']."' and state = '0'");
  176. if($num > count($warehouseyl))
  177. {
  178. echo json_encode(array('msg'=>'出库数量大于库存数量!','success'=>false));exit;
  179. }
  180. $this->db->trans_begin();
  181. for($i=0;$i<$num;$i++)
  182. {
  183. $this->warehouseyl->save(array('ctime'=>$time,'time'=>$time,'state'=>1),$warehouseyl[$i]['id']);
  184. }
  185. if ($this->db->trans_status() === FALSE)
  186. {
  187. $this->db->trans_rollback();
  188. echo json_encode(array('msg'=>'操作失败,请重试','success'=>false));exit;
  189. }
  190. else
  191. {
  192. $this->db->trans_commit();
  193. echo json_encode(array('msg'=>'操作成功','success'=>true));exit;
  194. }
  195. }
  196. $arg_array = $arg_array[0];
  197. $warehouseyl = $this->warehouseyl->read($arg_array);
  198. $num = $this->warehouseyl->find_count("sku = '".$warehouseyl['sku']."' and state = '0'");
  199. $warehouseyl['num'] = $num;
  200. $this->data['warehouseyl'] = $warehouseyl;
  201. $this->_Template('warehouseyl_edit',$this->data);
  202. }
  203. //删除
  204. public function _del()
  205. {
  206. $post = $this->input->post(NULL, TRUE);
  207. if(isset($post['s']))
  208. {
  209. $id_arr = $this->input->post('s');
  210. $id_arr = explode(',',$id_arr);
  211. if(!$id_arr)
  212. {
  213. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  214. }
  215. //循环删除记录
  216. foreach ($id_arr as $v)
  217. {
  218. $this->warehouseyl->remove($v);
  219. }
  220. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  221. }
  222. }
  223. public function _presetedit($arg_array)
  224. {
  225. $dir = '/data/excel/'.date('Ymd',time()).'/';
  226. $config['upload_path'] = '.'.$dir ;
  227. $config['file_name'] = date('Ymd_His_',time()).rand(1000,9999);
  228. $config['allowed_types'] = 'xls|xlsx|csv';
  229. $config['max_size'] = 10240;
  230. $this->load->library('upload', $config);
  231. $this->upload->initialize($config);
  232. if ($this->upload->do_upload('userfile'))
  233. {
  234. $full_path = $dir.$this->upload->data('file_name');
  235. $fileName = '.' . $full_path;
  236. if (!file_exists($fileName))
  237. {
  238. echo json_encode(array('msg'=>"上传失败,请重试",'success'=>false));exit;
  239. }
  240. else
  241. {
  242. libxml_use_internal_errors(true);
  243. require_once "./data/excel/PHPExcel/IOFactory.php";
  244. $phpExcel = PHPExcel_IOFactory::load($fileName);// 载入当前文件
  245. $phpExcel->setActiveSheetIndex(0);// 设置为默认表
  246. $sheetCount = $phpExcel->getSheetCount();// 获取表格数量
  247. $row = $phpExcel->getActiveSheet()->getHighestRow();// 获取行数
  248. $column = $phpExcel->getActiveSheet()->getHighestColumn();// 获取列数
  249. ++$column;//如果列数大于26行
  250. $list = array();
  251. for ($i = 2; $i <= $row; $i++) // 行数循环
  252. {
  253. $data = array();
  254. for ($c = 'A'; $c != $column; $c++) // 列数循环
  255. {
  256. $data[] = $phpExcel->getActiveSheet()->getCell($c . $i)->getValue();
  257. }
  258. $list[] = $data;
  259. }
  260. }
  261. $i = 0;$j = 0;$ed = array();
  262. foreach ($list as $key=>$value)
  263. {
  264. $time = time();
  265. if($value['0'] == "")
  266. {
  267. continue;
  268. }
  269. $sku = $value['0'];
  270. $title = $value['1'];
  271. $specs = $value['2'];
  272. $size = $value['3'];
  273. $text = $value['4'];
  274. $num = $value['5'];//正数增加负数扣除
  275. $d = $this->warehouseyl->get_sku($sku);
  276. if($num < 0)//扣减库存
  277. {
  278. if(!$d)//如果没有这个SKU
  279. {
  280. $ed[] = array($sku.'-库存中不存在此SKU');
  281. $j++;
  282. continue;
  283. }
  284. $data = $this->warehouseyl->find_all('sku = "'.$sku.'" state = "0"');
  285. if(count($data) < abs($num))
  286. {
  287. $ed[] = array($sku.'-扣减数量大于库存剩余数量!');
  288. $j++;
  289. continue;
  290. }
  291. for($i=0;$i<abs($num);$i++)
  292. {
  293. $this->warehouseyl->save(array('state'=>1,'ctime'=>$time,'time'=>$time),$data[$i]['id']);
  294. }
  295. }
  296. else if($num > 0)//增加库存
  297. {
  298. $post['sku'] = $sku;
  299. $post['title'] = $title;
  300. $post['specs'] = $specs;
  301. $post['size'] = $size;
  302. $post['text'] = $text;
  303. $post['state'] = 0;
  304. $post['rtime'] = $time;
  305. $post['time'] = $time;
  306. for($i=0;$i<$num;$i++)
  307. {
  308. $this->warehouseyl->insert($post);
  309. }
  310. }
  311. sleep(1);//防止入库时间一样,需要延迟1秒
  312. }
  313. if($j > 0)
  314. {
  315. $tt = date('Ymd',time());
  316. $title = '库存导入错误信息-'.$tt;
  317. $titlename = "<table border=1><tr><td>错误详情</td></tr></table>";
  318. $tail = "\n";
  319. $filename = $title.".xls";
  320. $ecl = $this->excel->get_fz3($ed,$titlename,$filename,$tail);
  321. $dir = '/data/excel/'.$time.'/';
  322. $file_name = 'error_'.$time.rand(1000,9999);
  323. if(!is_dir('.'.$dir))mkdir('.'.$dir,0777);
  324. $myfile = fopen(".".$dir.$file_name.".xls", "w") or die();
  325. fwrite($myfile,$ecl);
  326. fclose($myfile);
  327. $error = $dir.$file_name.'.xls';
  328. echo json_encode(array('msg'=>'导入成功,'.$j.'条异常,','error'=>$error,'success'=>true));exit;
  329. }
  330. else
  331. {
  332. echo json_encode(array('msg'=>'导入成功!','error'=>1,'success'=>true));exit;
  333. }
  334. }
  335. }
  336. public function _dcexcel()
  337. {
  338. if(isset($_GET['excel']))
  339. {
  340. $ktime = $this->input->get('ktime',true);
  341. $jtime = $this->input->get('jtime',true);
  342. $ktime = strtotime($ktime);
  343. $jtime = strtotime($jtime);
  344. $where = "1=1";
  345. if($ktime && $jtime)
  346. {
  347. $where .= " and time > '$ktime' and time < '$jtime'";
  348. }
  349. //数据排序
  350. $order_str = "time desc";
  351. $info_list = $this->warehouseyl->find_all($where,'sku,title,specs,size,text',$order_str);
  352. $rows = array();$list = array();
  353. foreach ($info_list as $key=>$value)
  354. {
  355. $rows[strtolower($value['sku'])] = $value;//所有数据sku
  356. }
  357. $rowsdq = $rows;
  358. //格式化数据
  359. foreach ($rowsdq as $key=>$value)
  360. {
  361. $r = $this->warehouseyl->find_count($where.' and sku = "'.$value['sku'].'" and state = "0"');
  362. $c = $this->warehouseyl->find_count($where.' and sku = "'.$value['sku'].'" and state = "1"');
  363. $rowsdq[$key]['r'] = $r+$c;
  364. $rowsdq[$key]['c'] = $c;
  365. $rowsdq[$key]['j'] = $r;
  366. }
  367. $title = date('Y-m-d',$ktime).'至'.date('Y-m-d',$jtime).'详情';
  368. $titlename = "<table border=1>
  369. <tr align='center'>
  370. <td>编码</td>
  371. <td>产品名称</td>
  372. <td>规格</td>
  373. <td>尺寸</td>
  374. <td>描述</td>
  375. <td>入库</td>
  376. <td>出库</td>
  377. <td>结余</td>
  378. </tr>
  379. </table>";
  380. $filename = $title.".xls";
  381. $tail = "";
  382. $this->excel->get_fz2($rowsdq,$titlename,$filename,$tail);
  383. }
  384. }
  385. public function _drmb()
  386. {
  387. $title = '导入模板';
  388. $titlename = "<table border=1>
  389. <tr align='center'>
  390. <td>编码</td>
  391. <td>产品名称</td>
  392. <td>规格</td>
  393. <td>尺寸</td>
  394. <td>描述</td>
  395. <td>数量</td>
  396. </tr>
  397. <tr align='center'>
  398. <td>1001</td>
  399. <td>生料A</td>
  400. <td>缅甸发</td>
  401. <td>20</td>
  402. <td>仓发</td>
  403. <td>5</td>
  404. </tr>
  405. </table>";
  406. $filename = $title.".xls";
  407. $tail = "";
  408. $this->excel->get_fz2($rowsdq,$titlename,$filename,$tail);
  409. }
  410. }