Amazonbarcode.php 11 KB

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