Printcode.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Printcode extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->_model('Model_print_code','printcode');
  6. $this->load->_model('Model_excel','excel');
  7. }
  8. //定义方法的调用规则 获取URI第二段值
  9. public function _remap($arg,$arg_array)
  10. {
  11. if($arg == 'addexcel')
  12. {
  13. $this->_addexcel();
  14. }
  15. else if($arg == 'print')
  16. {
  17. $this->_print();
  18. }
  19. else if($arg == 'see')
  20. {
  21. $this->_see();
  22. }
  23. else if($arg == 'yz')
  24. {
  25. $this->_yz();
  26. }
  27. else if($arg == 'add')
  28. {
  29. $this->_add();
  30. }
  31. else if($arg == 'edit')
  32. {
  33. $this->_edit($arg_array);
  34. }
  35. else if($arg == 'del')
  36. {
  37. $this->_del();
  38. }
  39. else
  40. {
  41. $this->_index();
  42. }
  43. }
  44. public function _see()
  45. {
  46. $post = $this->input->post(NULL, TRUE);
  47. if(isset($post['page']))
  48. {
  49. $page = $this->input->post('page',true);
  50. $perpage = $this->input->post('perpage',true);
  51. $timetk = $this->input->post('timetk',true);
  52. $timetj = $this->input->post('timetj',true);
  53. $number = $this->input->post('number',true);
  54. $label = $this->input->post('label',true);
  55. $print = $this->input->post('print',true);
  56. $xztime = $this->input->post('xztime',true);
  57. $timetk = strtotime($timetk);
  58. $timetj = strtotime($timetj);
  59. $where = "1=1";
  60. if($number)
  61. {
  62. $where .= " and number = '$number'";
  63. }
  64. if($label)
  65. {
  66. $where .= " and label = '$label'";
  67. }
  68. if($print != '')
  69. {
  70. $where .= " and print = '$print'";
  71. }
  72. if($timetk && $timetj)
  73. {
  74. $where .= " and ".$xztime." > '$timetk' and ".$xztime." < '$timetj'";
  75. }
  76. //数据排序
  77. $order_str = $xztime." desc";
  78. if(empty($page))
  79. {
  80. $start = 0;
  81. $perpage = 1;
  82. }
  83. else
  84. {
  85. $start = ($page - 1)*$perpage;
  86. }
  87. $info_list = $this->printcode->find_all($where,'id,title,number,label,label2,num,time,addtime',$order_str,$start,$perpage);
  88. foreach ($info_list as $key=>$value)
  89. {
  90. if($value['time'] != 0)
  91. {
  92. $info_list[$key]['time'] = date('Y-m-d H:i',$value['time']);
  93. }
  94. else
  95. {
  96. $info_list[$key]['time'] = '';
  97. }
  98. if($value['addtime'] != 0)
  99. {
  100. $info_list[$key]['addtime'] = date('Y-m-d H:i',$value['addtime']);
  101. }
  102. else
  103. {
  104. $info_list[$key]['addtime'] = '';
  105. }
  106. $info_list[$key]['cz'] = "<span class='amazonbarprint' data-id='".$value['id']."' data-num='".$value['num']."'>打印</span>";
  107. }
  108. $total = $this->printcode->find_count($where);
  109. $pagenum = ceil($total/$perpage);
  110. $over = $total-($start+$perpage);
  111. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  112. echo json_encode($rows);exit;
  113. }
  114. $this->_Template('printcode_see',$this->data);
  115. }
  116. public function _index()
  117. {
  118. $post = $this->input->post(NULL, TRUE);
  119. if(isset($post['page']))
  120. {
  121. $page = $this->input->post('page',true);
  122. $perpage = $this->input->post('perpage',true);
  123. $timetk = $this->input->post('timetk',true);
  124. $timetj = $this->input->post('timetj',true);
  125. $number = $this->input->post('number',true);
  126. $label = $this->input->post('label',true);
  127. $print = $this->input->post('print',true);
  128. $xztime = $this->input->post('xztime',true);
  129. $timetk = strtotime($timetk);
  130. $timetj = strtotime($timetj);
  131. $where = "1=1";
  132. if($number)
  133. {
  134. $where .= " and number = '$number'";
  135. }
  136. if($label)
  137. {
  138. $where .= " and label = '$label'";
  139. }
  140. if($print != '')
  141. {
  142. $where .= " and print = '$print'";
  143. }
  144. if($timetk && $timetj)
  145. {
  146. $where .= " and ".$xztime." > '$timetk' and ".$xztime." < '$timetj'";
  147. }
  148. //数据排序
  149. $order_str = $xztime." desc";
  150. if(empty($page))
  151. {
  152. $start = 0;
  153. $perpage = 1;
  154. }
  155. else
  156. {
  157. $start = ($page - 1)*$perpage;
  158. }
  159. $info_list = $this->printcode->find_all($where,'id,title,number,label,label2,num,time,addtime',$order_str,$start,$perpage);
  160. foreach ($info_list as $key=>$value)
  161. {
  162. if($value['time'] != 0)
  163. {
  164. $info_list[$key]['time'] = date('Y-m-d H:i',$value['time']);
  165. }
  166. else
  167. {
  168. $info_list[$key]['time'] = '';
  169. }
  170. if($value['addtime'] != 0)
  171. {
  172. $info_list[$key]['addtime'] = date('Y-m-d H:i',$value['addtime']);
  173. }
  174. else
  175. {
  176. $info_list[$key]['addtime'] = '';
  177. }
  178. }
  179. $total = $this->printcode->find_count($where);
  180. $pagenum = ceil($total/$perpage);
  181. $over = $total-($start+$perpage);
  182. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  183. echo json_encode($rows);exit;
  184. }
  185. $this->_Template('printcode',$this->data);
  186. }
  187. public function _yz()
  188. {
  189. $this->_Template('printcode_yz',$this->data);
  190. }
  191. public function _add()
  192. {
  193. $post = $this->input->post(NULL, TRUE);
  194. if(isset($post['num']))
  195. {
  196. $post['num'] = $this->input->post('num');
  197. $post['addtime'] = time();
  198. if(!is_numeric($post['num']))
  199. {
  200. echo json_encode(array('msg'=>'数量必须是数字!','success'=>false));exit;
  201. }
  202. if($this->printcode->insert($post))
  203. {
  204. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  205. }
  206. else
  207. {
  208. echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
  209. }
  210. }
  211. $this->_Template('printcode_add',$this->data);
  212. }
  213. public function _edit($arg_array)
  214. {
  215. $post = $this->input->post(NULL, TRUE);
  216. if(isset($post['id']))
  217. {
  218. $id = $this->input->post('id',true);
  219. $data = $this->printcode->read($id);
  220. $post['num'] = $this->input->post('num');
  221. if(!is_numeric($post['num']))
  222. {
  223. echo json_encode(array('msg'=>'数量必须是数字!','success'=>false));exit;
  224. }
  225. if($data['time'] != 0)
  226. {
  227. echo json_encode(array('msg'=>'此单已打印,无法修改!','success'=>false));exit;
  228. }
  229. if($this->printcode->save($post,$id))
  230. {
  231. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  232. }
  233. else
  234. {
  235. echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
  236. }
  237. }
  238. $arg_array = $arg_array[0];
  239. $printcode = $this->printcode->read($arg_array);
  240. $this->data['printcode'] = $printcode;
  241. $this->_Template('printcode_edit',$this->data);
  242. }
  243. //删除
  244. public function _del()
  245. {
  246. $post = $this->input->post(NULL, TRUE);
  247. if(isset($post['s']))
  248. {
  249. $id_arr = $this->input->post('s');
  250. $id_arr = explode(',',$id_arr);
  251. if(!$id_arr)
  252. {
  253. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  254. }
  255. //循环删除记录
  256. foreach ($id_arr as $v)
  257. {
  258. $data = $this->printcode->read($v);
  259. if($data['time'] != 0)
  260. {
  261. echo json_encode(array('msg'=>$data['title'].'<br>已打印,无法删除!','success'=>false));exit;
  262. }
  263. $this->printcode->remove($v);
  264. }
  265. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  266. }
  267. }
  268. public function _print()
  269. {
  270. $post = $this->input->post(NULL, TRUE);
  271. if(isset($post['s']))
  272. {
  273. $v = $this->input->post('s');
  274. $va = explode(',',rtrim($v,','));
  275. $n = $this->input->post('n');
  276. $sl = $this->input->post('sl');
  277. $text = array();$text['data'] = array();$time = time();
  278. $data = $this->printcode->read($va[$n]);
  279. $rows = array('number'=>$data['number'],'label'=>$data['label'],'label2'=>$data['label2'],'num'=>$sl,'data'=>$v,'n'=>$n-1);
  280. if($this->printcode->save(array('print'=>1,'time'=>$time),$data['id']))
  281. {
  282. echo json_encode(array('rows'=>($rows),'success'=>true));exit;
  283. }
  284. else
  285. {
  286. echo json_encode(array('msg'=>'数据写入异常,请重新打印!','success'=>false));exit;
  287. }
  288. }
  289. }
  290. public function _addexcel()
  291. {
  292. $dir = '/data/excel/'.date('Ymd',time()).'/';
  293. $config['upload_path'] = '.'.$dir ;
  294. $config['file_name'] = date('Ymd_His_',time()).rand(1000,9999);
  295. $config['allowed_types'] = 'xls|xlsx|csv';
  296. $config['max_size'] = 10240;
  297. $this->load->library('upload', $config);
  298. $this->upload->initialize($config);
  299. if ($this->upload->do_upload('userfile'))
  300. {
  301. $full_path = $dir.$this->upload->data('file_name');
  302. $fileName = '.' . $full_path;
  303. if (!file_exists($fileName))
  304. {
  305. echo json_encode(array('msg'=>"上传失败,请重试",'success'=>false));exit;
  306. }
  307. else
  308. {
  309. libxml_use_internal_errors(true);
  310. require_once "./data/excel/PHPExcel/IOFactory.php";
  311. $phpExcel = PHPExcel_IOFactory::load($fileName);// 载入当前文件
  312. $phpExcel->setActiveSheetIndex(0);// 设置为默认表
  313. $sheetCount = $phpExcel->getSheetCount();// 获取表格数量
  314. $row = $phpExcel->getActiveSheet()->getHighestRow();// 获取行数
  315. $column = $phpExcel->getActiveSheet()->getHighestColumn();// 获取列数
  316. ++$column;//如果列数大于26行
  317. $list = array();
  318. for ($i = 3; $i <= $row; $i++) // 行数循环
  319. {
  320. $data = array();
  321. for ($c = 'A'; $c != $column; $c++) // 列数循环
  322. {
  323. $data[] = $phpExcel->getActiveSheet()->getCell($c . $i)->getValue();
  324. }
  325. $list[] = $data;
  326. }
  327. }
  328. $i = 0;$j = 0;$ed = array();$time = time();
  329. foreach ($list as $key=>$value)
  330. {
  331. $time = time();
  332. if($value['0'] == "")
  333. {
  334. continue;
  335. }
  336. $post['title'] = $value['1'];
  337. $post['number'] = $value['3'];
  338. $post['label'] = $value['0'];
  339. $post['label2'] = $value['2'];
  340. $post['num'] = $value['4'];
  341. $post['addtime'] = $time;
  342. if(!is_numeric($post['num']))
  343. {
  344. $ed[] = array($key.' 行,非数字!');
  345. $j++;
  346. continue;
  347. }
  348. if($post['num'] < 1)
  349. {
  350. $ed[] = array($key.' 行,数量错误!');
  351. $j++;
  352. continue;
  353. }
  354. $this->printcode->insert($post);
  355. }
  356. if($j > 0)
  357. {
  358. $tt = date('Ymd',time());
  359. $title = '库存导入错误信息-'.$tt;
  360. $titlename = "<table border=1><tr><td>错误详情</td></tr></table>";
  361. $tail = "\n";
  362. $filename = $title.".xls";
  363. $ecl = $this->excel->get_fz3($ed,$titlename,$filename,$tail);
  364. $dir = '/data/excel/'.$time.'/';
  365. $file_name = 'error_'.$time.rand(1000,9999);
  366. if(!is_dir('.'.$dir))mkdir('.'.$dir,0777);
  367. $myfile = fopen(".".$dir.$file_name.".xls", "w") or die();
  368. fwrite($myfile,$ecl);
  369. fclose($myfile);
  370. $error = $dir.$file_name.'.xls';
  371. echo json_encode(array('msg'=>'导入成功,'.$j.'条异常,','error'=>$error,'success'=>true));exit;
  372. }
  373. else
  374. {
  375. echo json_encode(array('msg'=>'导入成功!','error'=>1,'success'=>true));exit;
  376. }
  377. }
  378. }
  379. }