Amazonbarcode.php 18 KB

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