Printcode.php 11 KB

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