Amazonbarcode.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. $this->load->_model('Model_amazonbarcodeitems','amazonbarcodeitems');
  9. }
  10. //定义方法的调用规则 获取URI第二段值
  11. public function _remap($arg,$arg_array)
  12. {
  13. if($arg == 'addexcel')
  14. {
  15. $this->_addexcel();
  16. }
  17. else if($arg == 'print')
  18. {
  19. $this->_print();
  20. }
  21. else if($arg == 'usaprint')
  22. {
  23. $this->_usaprint();
  24. }
  25. else if($arg == 'see')
  26. {
  27. $this->_see();
  28. }
  29. else if($arg == 'edit')
  30. {
  31. $this->_edit($arg_array);
  32. }
  33. else if($arg == 'del')
  34. {
  35. $this->_del();
  36. }else if($arg == 'outpage'){
  37. $this->_outPage();
  38. }else if($arg == 'outcount'){
  39. $this->_outCount($arg_array);
  40. }
  41. else
  42. {
  43. $this->_index();
  44. }
  45. }
  46. public function _see()
  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. $timetk = $this->input->post('timetk',true);
  54. $timetj = $this->input->post('timetj',true);
  55. $number = $this->input->post('number',true);
  56. $label = $this->input->post('label',true);
  57. $print = $this->input->post('print',true);
  58. $xztime = $this->input->post('xztime',true);
  59. $timetk = strtotime($timetk);
  60. $timetj = strtotime($timetj);
  61. $where = "1=1";
  62. if($number)
  63. {
  64. $where .= " and number = '$number'";
  65. }
  66. if($label)
  67. {
  68. $where .= " and label = '$label'";
  69. }
  70. if($print != '')
  71. {
  72. $where .= " and print = '$print'";
  73. }
  74. if($timetk && $timetj)
  75. {
  76. $where .= " and ".$xztime." > '$timetk' and ".$xztime." < '$timetj'";
  77. }
  78. //数据排序
  79. $order_str = $xztime." desc";
  80. if(empty($page))
  81. {
  82. $start = 0;
  83. $perpage = 1;
  84. }
  85. else
  86. {
  87. $start = ($page - 1)*$perpage;
  88. }
  89. $info_list = $this->amazonbarcode->find_all($where,'id,number,label,label2,num,time,addtime',$order_str,$start,$perpage);
  90. foreach ($info_list as $key=>$value)
  91. {
  92. if($value['time'] != 0)
  93. {
  94. $info_list[$key]['time'] = date('Y-m-d H:i',$value['time']);
  95. }
  96. else
  97. {
  98. $info_list[$key]['time'] = '';
  99. }
  100. if($value['addtime'] != 0)
  101. {
  102. $info_list[$key]['addtime'] = date('Y-m-d H:i',$value['addtime']);
  103. }
  104. else
  105. {
  106. $info_list[$key]['addtime'] = '';
  107. }
  108. $info_list[$key]['cz'] = "<span class='amazonbarprint' data-id='".$value['id']."' data-num='".$value['num']."'>国内</span><span style='margin-left:10px;background: #2ca8a1;' class='usaamazonbarprint' data-id='".$value['id']."' data-num='".$value['num']."'>美仓</span>";
  109. }
  110. $total = $this->amazonbarcode->find_count($where);
  111. $pagenum = ceil($total/$perpage);
  112. $over = $total-($start+$perpage);
  113. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  114. echo json_encode($rows);exit;
  115. }
  116. $this->_Template('amazonbarcode_see',$this->data);
  117. }
  118. public function _index()
  119. {
  120. $post = $this->input->post(NULL, TRUE);
  121. if(isset($post['page']))
  122. {
  123. $page = $this->input->post('page',true);
  124. $perpage = $this->input->post('perpage',true);
  125. $timetk = $this->input->post('timetk',true);
  126. $timetj = $this->input->post('timetj',true);
  127. $number = $this->input->post('number',true);
  128. $label = $this->input->post('label',true);
  129. $print = $this->input->post('print',true);
  130. $xztime = $this->input->post('xztime',true);
  131. $batch_no = $this->input->post('batch_no',true);
  132. $timetk = strtotime($timetk);
  133. $timetj = strtotime($timetj);
  134. $where = "1=1";
  135. if($batch_no)
  136. {
  137. $where .= " and batch_no = '$batch_no'";
  138. }
  139. if($number)
  140. {
  141. $where .= " and number = '$number'";
  142. }
  143. if($label)
  144. {
  145. $where .= " and label = '$label'";
  146. }
  147. if($print != '')
  148. {
  149. $where .= " and print = '$print'";
  150. }
  151. if($timetk && $timetj)
  152. {
  153. $where .= " and ".$xztime." > '$timetk' and ".$xztime." < '$timetj'";
  154. }
  155. //数据排序
  156. $order_str = $xztime." desc";
  157. if(empty($page))
  158. {
  159. $start = 0;
  160. $perpage = 1;
  161. }
  162. else
  163. {
  164. $start = ($page - 1)*$perpage;
  165. }
  166. $info_list = $this->amazonbarcode->find_all($where,'id,batch_no,number,label,label2,num,out_num,time,addtime',$order_str,$start,$perpage);
  167. foreach ($info_list as $key=>$value)
  168. {
  169. if($value['time'] != 0)
  170. {
  171. $info_list[$key]['time'] = date('Y-m-d H:i',$value['time']);
  172. }
  173. else
  174. {
  175. $info_list[$key]['time'] = '';
  176. }
  177. if($value['addtime'] != 0)
  178. {
  179. $info_list[$key]['addtime'] = date('Y-m-d H:i',$value['addtime']);
  180. }
  181. else
  182. {
  183. $info_list[$key]['addtime'] = '';
  184. }
  185. $info_list[$key]['out_num'] = $value['out_num']*1;
  186. if(empty($value['batch_no'])){
  187. $info_list[$key]['batch_no'] = '';
  188. }else if($value['batch_no'] == 'null'){
  189. $info_list[$key]['batch_no'] = '';
  190. }
  191. }
  192. $total = $this->amazonbarcode->find_count($where);
  193. $pagenum = ceil($total/$perpage);
  194. $over = $total-($start+$perpage);
  195. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  196. echo json_encode($rows);exit;
  197. }
  198. $this->_Template('amazonbarcode',$this->data);
  199. }
  200. public function _edit($arg_array)
  201. {
  202. $post = $this->input->post(NULL, TRUE);
  203. if(isset($post['id']))
  204. {
  205. $id = $this->input->post('id',true);
  206. $data = $this->amazonbarcode->read($id);
  207. if($data['time'] != 0)
  208. {
  209. echo json_encode(array('msg'=>'此单已打印,无法修改!','success'=>false));exit;
  210. }
  211. if(strlen($post['number']) > 25){
  212. echo json_encode(array('msg'=>'条码超出25位无法有效打印!','success'=>false));exit;
  213. }
  214. if($this->amazonbarcode->save($post,$id))
  215. {
  216. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  217. }
  218. else
  219. {
  220. echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
  221. }
  222. }
  223. $arg_array = $arg_array[0];
  224. $amazonbarcode = $this->amazonbarcode->read($arg_array);
  225. $this->data['amazonbarcode'] = $amazonbarcode;
  226. $this->_Template('amazonbarcode_edit',$this->data);
  227. }
  228. //删除
  229. public function _del()
  230. {
  231. $post = $this->input->post(NULL, TRUE);
  232. if(isset($post['s']))
  233. {
  234. $id_arr = $this->input->post('s');
  235. $id_arr = explode(',',$id_arr);
  236. if(!$id_arr)
  237. {
  238. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  239. }
  240. //循环删除记录
  241. foreach ($id_arr as $v)
  242. {
  243. $data = $this->amazonbarcode->read($v);
  244. if($data['time'] != 0)
  245. {
  246. //echo json_encode(array('msg'=>$data['title'].'<br>已打印,无法删除!','success'=>false));exit;
  247. }
  248. $this->amazonbarcode->remove($v);
  249. }
  250. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  251. }
  252. }
  253. public function _print()
  254. {
  255. $post = $this->input->post(NULL, TRUE);
  256. if(isset($post['s']))
  257. {
  258. $v = $this->input->post('s');
  259. $va = explode(',',rtrim($v,','));
  260. $n = $this->input->post('n');
  261. $sl = $this->input->post('sl');
  262. $text = array();$text['data'] = array();$time = time();
  263. $data = $this->amazonbarcode->read($va[$n]);
  264. if(strlen($data['number']) > 25){
  265. echo json_encode(array('msg'=>'打印条码超过25位,系统不支持!','success'=>false));exit;
  266. }
  267. $rows = array('number'=>$data['number'],'label'=>$data['label'],'label2'=>$data['label2'],'num'=>$sl,'data'=>$v,'n'=>$n-1);
  268. if($this->amazonbarcode->save(array('print'=>1,'time'=>$time),$data['id']))
  269. {
  270. echo json_encode(array('rows'=>($rows),'success'=>true));exit;
  271. }
  272. else
  273. {
  274. echo json_encode(array('msg'=>'数据写入异常,请重新打印!','success'=>false));exit;
  275. }
  276. }
  277. }
  278. public function _usaprint()
  279. {
  280. $post = $this->input->post(NULL, TRUE);
  281. if(isset($post['s']))
  282. {
  283. $v = $this->input->post('s');
  284. $va = explode(',',rtrim($v,','));
  285. $n = $this->input->post('n');
  286. $sl = $this->input->post('sl');
  287. $text = array();$text['data'] = array();$time = time();
  288. $data = $this->amazonbarcode->read($va[$n]);
  289. $rows = array('number'=>$data['number'],'usanumber'=>str_replace(array('#','*'),array('',''),$data['number']),'label'=>$data['label'],'label2'=>$data['label2'],'num'=>$sl,'data'=>$v,'n'=>$n-1);
  290. if($this->amazonbarcode->save(array('print'=>1,'time'=>$time),$data['id']))
  291. {
  292. echo json_encode(array('rows'=>($rows),'success'=>true));exit;
  293. }
  294. else
  295. {
  296. echo json_encode(array('msg'=>'数据写入异常,请重新打印!','success'=>false));exit;
  297. }
  298. }
  299. }
  300. public function _addexcel()
  301. {
  302. $dir = '/data/excel/'.date('Ymd',time()).'/';
  303. $config['upload_path'] = '.'.$dir ;
  304. $config['file_name'] = date('Ymd_His_',time()).rand(1000,9999);
  305. $config['allowed_types'] = 'xls|xlsx|csv';
  306. $config['max_size'] = 10240;
  307. $this->load->library('upload', $config);
  308. $this->upload->initialize($config);
  309. if ($this->upload->do_upload('userfile'))
  310. {
  311. $full_path = $dir.$this->upload->data('file_name');
  312. $fileName = '.' . $full_path;
  313. if (!file_exists($fileName))
  314. {
  315. echo json_encode(array('msg'=>"上传失败,请重试",'success'=>false));exit;
  316. }
  317. else
  318. {
  319. libxml_use_internal_errors(true);
  320. require_once "./data/excel/PHPExcel/IOFactory.php";
  321. $phpExcel = PHPExcel_IOFactory::load($fileName);// 载入当前文件
  322. $phpExcel->setActiveSheetIndex(0);// 设置为默认表
  323. $sheetCount = $phpExcel->getSheetCount();// 获取表格数量
  324. $row = $phpExcel->getActiveSheet()->getHighestRow();// 获取行数
  325. $column = $phpExcel->getActiveSheet()->getHighestColumn();// 获取列数
  326. ++$column;//如果列数大于26行
  327. $list = array();
  328. for ($i = 3; $i <= $row; $i++) // 行数循环
  329. {
  330. $data = array();
  331. for ($c = 'A'; $c != $column; $c++) // 列数循环
  332. {
  333. $data[] = $phpExcel->getActiveSheet()->getCell($c . $i)->getValue();
  334. }
  335. $list[] = $data;
  336. }
  337. }
  338. $i = 0;$j = 0;$ed = array();$time = time();
  339. $laster_info = $this->amazonbarcode->find("","","id desc");
  340. $batch_no = date('Ymd',time());
  341. if(empty($laster_info['batch_no'])){
  342. $batch_no = date('Ymd',time()).'-001';
  343. }else{
  344. $tmp_arr = explode('-',$laster_info['batch_no']);
  345. if($tmp_arr[0] == $batch_no){
  346. $tmp_num = ($tmp_arr[1]*1)+1;
  347. if($tmp_num < 10){
  348. $batch_no = date('Ymd',time()).'-00'.$tmp_num;
  349. }elseif($tmp_num < 100){
  350. $batch_no = date('Ymd',time()).'-0'.$tmp_num;
  351. }else{
  352. $batch_no = date('Ymd',time()).'-'.$tmp_num;
  353. }
  354. }else{
  355. $batch_no = date('Ymd',time()).'-001';
  356. }
  357. }
  358. foreach ($list as $key=>$value)
  359. {
  360. $time = time();
  361. if($value['0'] == "")
  362. {
  363. continue;
  364. }
  365. $post['batch_no'] = $batch_no;
  366. $post['number'] = $value['0'];
  367. $post['label'] = ($value['1'])?$value['1']:'';
  368. $post['label2'] = ($value['2'])?$value['2']:'';
  369. $post['num'] = $value['3'];
  370. $post['addtime'] = $time;
  371. if(!is_numeric($post['num']))
  372. {
  373. $ed[] = array($key.' 行,非数字!');
  374. $j++;
  375. continue;
  376. }
  377. if(strlen($value['0']) > 25){
  378. $ed[] = array($value['0']."--".$key.' 行,条码超出25位!');
  379. $j++;
  380. continue;
  381. }
  382. if($post['num'] < 1)
  383. {
  384. $ed[] = array($key.' 行,数量错误!');
  385. $j++;
  386. continue;
  387. }
  388. $this->amazonbarcode->insert($post);
  389. }
  390. if($j > 0)
  391. {
  392. $tt = date('Ymd',time());
  393. $title = '库存导入错误信息-'.$tt;
  394. $titlename = "<table border=1><tr><td>错误详情</td></tr></table>";
  395. $tail = "\n";
  396. $filename = $title.".xls";
  397. $ecl = $this->excel->get_fz3($ed,$titlename,$filename,$tail);
  398. $dir = '/data/excel/'.$time.'/';
  399. $file_name = 'error_'.$time.rand(1000,9999);
  400. if(!is_dir('.'.$dir))mkdir('.'.$dir,0777);
  401. $myfile = fopen(".".$dir.$file_name.".xls", "w") or die();
  402. fwrite($myfile,$ecl);
  403. fclose($myfile);
  404. $error = $dir.$file_name.'.xls';
  405. echo json_encode(array('msg'=>'导入成功,'.$j.'条异常,','error'=>$error,'success'=>true));exit;
  406. }
  407. else
  408. {
  409. echo json_encode(array('msg'=>'导入成功!','error'=>1,'success'=>true));exit;
  410. }
  411. }
  412. }
  413. //出库计数
  414. public function _outPage(){
  415. $post = $this->input->post(NULL, TRUE);
  416. if(isset($post['page']))
  417. {
  418. $page = $this->input->post('page',true);
  419. $perpage = $this->input->post('perpage',true);
  420. $timetk = $this->input->post('timetk',true);
  421. $timetj = $this->input->post('timetj',true);
  422. $number = $this->input->post('number',true);
  423. $label = $this->input->post('label',true);
  424. $print = $this->input->post('print',true);
  425. $xztime = $this->input->post('xztime',true);
  426. $batch_no = $this->input->post('batch_no',true);
  427. $timetk = strtotime($timetk);
  428. $timetj = strtotime($timetj);
  429. $where = "1=1";
  430. if($batch_no)
  431. {
  432. $where .= " and batch_no = '$batch_no'";
  433. }
  434. if($number)
  435. {
  436. $where .= " and number = '$number'";
  437. }
  438. if($label)
  439. {
  440. $where .= " and label = '$label'";
  441. }
  442. if($print != '')
  443. {
  444. $where .= " and print = '$print'";
  445. }
  446. if($timetk && $timetj)
  447. {
  448. $where .= " and ".$xztime." > '$timetk' and ".$xztime." < '$timetj'";
  449. }
  450. //数据排序
  451. $order_str = $xztime." desc";
  452. if(empty($page))
  453. {
  454. $start = 0;
  455. $perpage = 1;
  456. }
  457. else
  458. {
  459. $start = ($page - 1)*$perpage;
  460. }
  461. $info_list = $this->amazonbarcode->find_all($where,'id,batch_no,number,num,out_num,time,addtime',$order_str,$start,$perpage);
  462. foreach ($info_list as $key=>$value)
  463. {
  464. if($value['time'] != 0)
  465. {
  466. $info_list[$key]['time'] = date('Y-m-d H:i',$value['time']);
  467. }
  468. else
  469. {
  470. $info_list[$key]['time'] = '';
  471. }
  472. if($value['addtime'] != 0)
  473. {
  474. $info_list[$key]['addtime'] = date('Y-m-d H:i',$value['addtime']);
  475. }
  476. else
  477. {
  478. $info_list[$key]['addtime'] = '';
  479. }
  480. $info_list[$key]['out_num'] = $value['out_num']*1;
  481. if(empty($value['batch_no'])){
  482. $info_list[$key]['batch_no'] = '';
  483. }else if($value['batch_no'] == 'null'){
  484. $info_list[$key]['batch_no'] = '';
  485. }
  486. }
  487. $total = $this->amazonbarcode->find_count($where);
  488. $pagenum = ceil($total/$perpage);
  489. $over = $total-($start+$perpage);
  490. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  491. echo json_encode($rows);exit;
  492. }
  493. $this->_Template('amazonbarcode_count',$this->data);
  494. }
  495. public function _outCount($arg_array){
  496. $post = $this->input->post(NULL, TRUE);
  497. if(isset($post['id']))
  498. {
  499. if(!empty($post['id'])){
  500. $info = $this->amazonbarcode->read($post['id']);
  501. if(empty($info)){
  502. echo json_encode([
  503. "code"=>0,
  504. "msg"=>"请求数据不存在",
  505. "data"=>['num'=>0],
  506. ]);
  507. exit;
  508. }
  509. if(empty($info['time'])){
  510. echo json_encode([
  511. "code"=>0,
  512. "msg"=>"该标签未打印不能出库",
  513. "data"=>['num'=>0],
  514. ]);
  515. exit;
  516. }
  517. if($info['number'] != $post['number']){
  518. echo json_encode([
  519. "code"=>0,
  520. "msg"=>"条码异常计数失败,扫描条码【".$post['number']."】",
  521. "data"=>['num'=>0],
  522. ]);
  523. exit;
  524. }
  525. $info['out_num'] = $info['out_num']*1 +1;
  526. $this->amazonbarcode->save(array('out_num'=>$info['out_num']),$post['id']);
  527. $this->amazonbarcodeitems->insert([
  528. 'aid'=>(int)$post['id'],
  529. 'number'=>$info['number'],
  530. 'addtime'=>time()
  531. ]);
  532. echo json_encode([
  533. "code"=>1,
  534. "msg"=>"ok",
  535. "data"=>['num'=>$info['out_num']],
  536. ]);
  537. exit;
  538. }else{
  539. echo json_encode([
  540. "code"=>0,
  541. "msg"=>"请求参数有无",
  542. "data"=>['num'=>0],
  543. ]);
  544. exit;
  545. }
  546. }
  547. $arg_array = $arg_array[0];
  548. $amazonbarcode = $this->amazonbarcode->read($arg_array);
  549. $this->data['info'] = $amazonbarcode;
  550. $this->_Template('amazonbarcode_check',$this->data);
  551. }
  552. }