printcode.php 11 KB

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