QcodeBach.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\QcodeCompany;
  4. use app\admin\model\QcodeProduct;
  5. use app\admin\model\QcodeGsmc;
  6. use app\admin\model\QcodeLarge;
  7. use app\admin\model\QcodeSmall;
  8. use app\common\controller\Backend;
  9. use \think\Session;
  10. use think\Db;
  11. /**
  12. *
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class QcodeBach extends Backend
  17. {
  18. /**
  19. * Product模型对象
  20. * @var \app\admin\model\QcodeBach
  21. */
  22. protected $model = null;
  23. protected $multiFields = 'status';
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = new \app\admin\model\QcodeBach();
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. //当前是否为关联查询
  40. $this->relationSearch = false;
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags', 'trim']);
  43. return $this->view->fetch();
  44. }
  45. /**
  46. * 查看
  47. */
  48. public function table1()
  49. {
  50. //当前是否为关联查询
  51. $this->relationSearch = false;
  52. //设置过滤方法
  53. $this->request->filter(['strip_tags', 'trim']);
  54. if ($this->request->isAjax()) {
  55. //如果发送的来源是Selectpage,则转发到Selectpage
  56. if ($this->request->request('keyField')) {
  57. return $this->selectpage();
  58. }
  59. $userInfo = Session::get('admin');
  60. $company_id = (int)$userInfo['company'];
  61. $where = [
  62. 'delete_time'=> ''
  63. ];
  64. $req = input();
  65. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  66. $order = $req['order'];
  67. $offset = $req['offset'];
  68. $limit = $req['limit'];
  69. // 构造模糊查询条件
  70. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  71. // $filter = ['field' => $regex];
  72. $filter = json_decode($req['filter'], true);
  73. foreach ($filter as $k => $v){
  74. $where[$k] = new \MongoDB\BSON\Regex($v);
  75. }
  76. $total = $this->model->name($company_id.'_'."qcode_bach")->where($where)
  77. ->order($sort,$order)
  78. ->skip($offset)
  79. ->select();
  80. $list = $this->model->name($company_id.'_'."qcode_bach")->where($where)
  81. ->order($sort,$order)
  82. ->limit($limit)
  83. ->skip($offset)
  84. ->select();
  85. foreach ($list as $k=>$v) {
  86. $oid = $v['_id']->jsonSerialize();
  87. $list[$k]['id'] = $oid['$oid'];
  88. }
  89. $result = array("total" => count($total), "rows" => $list);
  90. return json($result);
  91. }
  92. return $this->view->fetch();
  93. }
  94. /**
  95. * 查看
  96. */
  97. public function table2()
  98. {
  99. //当前是否为关联查询
  100. $this->relationSearch = false;
  101. //设置过滤方法
  102. $this->request->filter(['strip_tags', 'trim']);
  103. if ($this->request->isAjax()) {
  104. //如果发送的来源是Selectpage,则转发到Selectpage
  105. if ($this->request->request('keyField')) {
  106. return $this->selectpage();
  107. }
  108. $userInfo = Session::get('admin');
  109. $company_id = (int)$userInfo['company'];
  110. $where = [
  111. // 'company_id'=>(int)$userInfo['company'],
  112. 'delete_time'=> ''
  113. ];
  114. $req = input();
  115. if($req['filter']=='{}'){
  116. $result = array("total" => 0, "rows" => []);
  117. return json($result);
  118. }
  119. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  120. $order = $req['order'];
  121. $offset = $req['offset'];
  122. $limit = $req['limit'];
  123. // 构造模糊查询条件
  124. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  125. // $filter = ['field' => $regex];
  126. $filter = json_decode($req['filter'], true);
  127. foreach ($filter as $k => $v){
  128. $where[$k] = new \MongoDB\BSON\Regex($v);
  129. }
  130. $db = new QcodeLarge();
  131. $total = $db->name($company_id.'_'."qcode_large")->where($where)
  132. ->order($sort,$order)
  133. ->skip($offset)
  134. ->select();
  135. $list = $db->name($company_id.'_'."qcode_large")->where($where)
  136. ->order($sort,$order)
  137. ->limit($limit)
  138. ->skip($offset)
  139. ->select();
  140. $qcodeSmall = new QcodeSmall();
  141. foreach ($list as $k=>$v) {
  142. $oid = $v['_id']->jsonSerialize();
  143. $list[$k]['id'] = $oid['$oid'];
  144. //设置当前托盘号
  145. $list[$k]['l_flow'] = ltrim(substr($v['code'],53,6),'0');
  146. //查询小件数量
  147. $small_num = $qcodeSmall->name($company_id.'_'."qcode_small")
  148. ->where('large_id',$oid['$oid'])
  149. ->count();
  150. $list[$k]['small_num'] = $small_num;
  151. //设置当前大件重量
  152. $list[$k]['l_weight'] = floatval($v['l_weight'])/100;
  153. }
  154. $result = array("total" => count($total), "rows" => $list);
  155. return json($result);
  156. }
  157. return $this->view->fetch();
  158. }
  159. /**
  160. * 小件列表
  161. */
  162. public function small()
  163. {
  164. //设置过滤方法
  165. $this->request->filter(['strip_tags', 'trim']);
  166. $req = $this->request->param();
  167. if ($this->request->isAjax()) {
  168. //如果发送的来源是Selectpage,则转发到Selectpage
  169. if ($this->request->request('keyField')) {
  170. return $this->selectpage();
  171. }
  172. $userInfo = Session::get('admin');
  173. $company_id = (int)$userInfo['company'];
  174. $where = [
  175. 'delete_time'=> ''
  176. ];
  177. $req = input();
  178. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  179. $order = $req['order'];
  180. $offset = $req['offset'];
  181. $limit = $req['limit'];
  182. // 构造模糊查询条件
  183. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  184. // $filter = ['field' => $regex];
  185. $filter = json_decode($req['filter'], true);
  186. foreach ($filter as $k => $v){
  187. $where[$k] = new \MongoDB\BSON\Regex($v);
  188. }
  189. $qcodeSmall = new QcodeSmall();
  190. $total = $qcodeSmall->name($company_id.'_'."qcode_small")->where($where)
  191. ->order($sort,$order)
  192. ->skip($offset)
  193. ->select();
  194. $list = $qcodeSmall->name($company_id.'_'."qcode_small")->where($where)
  195. ->order($sort,$order)
  196. ->limit($limit)
  197. ->skip($offset)
  198. ->select();
  199. foreach ($list as $k=>$v) {
  200. $oid = $v['_id']->jsonSerialize();
  201. $list[$k]['id'] = $oid['$oid'];
  202. $list[$k]['l_flow'] = $req['l_flow'].'-'.$v['l_flow'];
  203. }
  204. $result = array("total" => count($total), "rows" => $list);
  205. return json($result);
  206. }
  207. return $this->view->fetch();
  208. }
  209. /**
  210. * 修改小件导出状态
  211. */
  212. public function small_status()
  213. {
  214. $req = $this->request->param();
  215. $ids = $req['ids'];
  216. $userInfo = Session::get('admin');
  217. $company_id = (int)$userInfo['company'];
  218. $qcodeSmall = new QcodeSmall();
  219. $status = $qcodeSmall->name($company_id.'_'."qcode_small")->where('_id', $ids)->value('status');
  220. if ($status === 0) {
  221. $newStatus = 1;
  222. } elseif ($status === 1) {
  223. $newStatus = 0;
  224. } else {
  225. // 处理其他情况,如果有需要
  226. }
  227. $bool = $qcodeSmall->name($company_id.'_'."qcode_small")->where('_id', $req['ids'])
  228. ->update(['status' => $newStatus]);
  229. if ($bool){
  230. $this->success("切换成功", null, ['id' => $ids]);
  231. }else{
  232. $this->error("切换失败", null, ['id' => $ids]);
  233. }
  234. }
  235. /**
  236. * 自动打码(大件)
  237. */
  238. public function print_l()
  239. {
  240. //设置过滤方法
  241. $this->request->filter(['strip_tags', 'trim']);
  242. $req = $this->request->param();
  243. if ($this->request->isAjax()) {
  244. parse_str($req['data'],$req);
  245. $ids = explode(',',$req['row']['ids']);
  246. $type = $req['row']['type'];
  247. $numn = $req['row']['numn'];
  248. $userInfo = Session::get('admin');
  249. $company_id = (int)$userInfo['company'];
  250. $qcodeLarge = new QcodeLarge();
  251. $qcodeSmall = new QcodeSmall();
  252. $qcodeProduct = new QcodeProduct();
  253. $large = $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$ids[0])->find();
  254. $bach = $this->model->name($company_id.'_'."qcode_bach")->where('_id',json_decode($large,true)['bach_id'])->find();
  255. $row = $qcodeProduct->where('product_code',json_decode($bach,true)['matter_no'])->find();
  256. $company_name = json_decode($bach,true)['supplier_name'];
  257. $product_name = json_decode($bach,true)['matter_name'];
  258. // $date = json_decode($bach,true)['manufacture_date'];
  259. // $batch = json_decode($bach,true)['bach_num'];
  260. $main_unit = json_decode($row,true)['main_unit'];
  261. $sec_unit = json_decode($row,true)['sec_unit'];
  262. $proportion = json_decode($row,true)['proportion'];
  263. $rows = [];
  264. //查询打印大件码所需数据
  265. foreach ($ids as $key=>$value){
  266. $row = $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$value)->find();
  267. $row = json_decode($row,true);
  268. $code = $row['code'];
  269. $rows[$key]['company_name'] = $company_name;
  270. $rows[$key]['product_name'] = $product_name;
  271. $rows[$key]['sqrcd'] = $qcodeSmall->name($company_id.'_'."qcode_small")->where('large_id',$value)->count();
  272. $rows[$key]['main_unit'] = $main_unit;
  273. $rows[$key]['sec_unit'] = $sec_unit;
  274. $rows[$key]['date'] = substr_replace(substr_replace('20'.substr($code,38,6), '-', 4, 0), '-', 7, 0);
  275. $rows[$key]['l_flow'] = ltrim(substr($code,53,6),'0');
  276. $rows[$key]['qrcode'] = $code;
  277. $rows[$key]['pCode'] = $this->qrcode($code);
  278. //批次号特殊情况判断
  279. if (substr($code,76,10)=='0000000000'){
  280. $rows[$key]['batch'] = ltrim(substr($code,66,10),'0');
  281. }else{
  282. $rows[$key]['batch'] = ltrim(substr($code,66,10),'0').'、'.ltrim(substr($code,76,10),'0');
  283. }
  284. //转换关系判断
  285. if($row['l_num']===null){
  286. //走转换关系
  287. if ($proportion){
  288. $rows[$key]['num'] = $rows[$key]['sqrcd']*$proportion;
  289. $rows[$key]['num'] = floor($rows[$key]['num'] * 100) / 100;
  290. }else{
  291. $rows[$key]['num'] = '';
  292. }
  293. }else if($row['l_num']==0){
  294. //判断是否是公斤
  295. if($main_unit=='公斤'){
  296. //公斤使用l_weight
  297. $rows[$key]['num'] = $row['l_weight'];
  298. }else{
  299. //不是公斤走转换关系
  300. if ($proportion){
  301. $rows[$key]['num'] = $rows[$key]['sqrcd']*$proportion;
  302. $rows[$key]['num'] = floor($rows[$key]['num'] * 100) / 100;
  303. }else{
  304. $rows[$key]['num'] = '';
  305. }
  306. }
  307. }else{
  308. //箱, 使用l_num
  309. $rows[$key]['num'] = $row['l_num'];
  310. }
  311. }
  312. $data = [
  313. 'type'=>$type,
  314. 'numn'=>$numn,
  315. 'rows'=>$rows,
  316. 'ids'=>$ids,
  317. ];
  318. $this->success('成功','',$data);
  319. }
  320. $this->view->assign('ids',$req['ids']);
  321. return $this->view->fetch();
  322. }
  323. /**
  324. * 自动打码(小件)
  325. */
  326. public function print_s()
  327. {
  328. //设置过滤方法
  329. $this->request->filter(['strip_tags', 'trim']);
  330. $req = $this->request->param();
  331. if ($this->request->isAjax()) {
  332. parse_str($req['data'],$req);
  333. $ids = explode(',',$req['row']['ids']);
  334. $type = $req['row']['type'];
  335. $numn = $req['row']['numn'];
  336. $userInfo = Session::get('admin');
  337. $company_id = (int)$userInfo['company'];
  338. $qcodeLarge = new QcodeLarge();
  339. $qcodeSmall = new QcodeSmall();
  340. $large = $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$ids[0])->find();
  341. $bach = $this->model->name($company_id.'_'."qcode_bach")->where('_id',json_decode($large,true)['bach_id'])->find();
  342. $company_name = json_decode($bach,true)['supplier_name'];
  343. $product_name = json_decode($bach,true)['matter_name'];
  344. //获取全部小件id
  345. $rows = [];
  346. $key = 0;
  347. foreach ($ids as $value){
  348. $large = $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$value)->find();
  349. $large = json_decode($large,true);
  350. $arr = $qcodeSmall->name($company_id.'_'."qcode_small")->where('large_id',$value)->column('_id');
  351. //查询打印大件码所需数据
  352. foreach ($arr as $v){
  353. $small = $qcodeSmall->name($company_id.'_'."qcode_small")->where('_id',$v->jsonSerialize()['$oid'])->find();
  354. $small = json_decode($small,true);
  355. $code = $small['code'];
  356. $rows[$key]['company_name'] = $company_name;
  357. $rows[$key]['product_name'] = $product_name;
  358. $rows[$key]['date'] = substr_replace(substr_replace('20'.substr($code,38,6), '-', 4, 0), '-', 7, 0);
  359. $rows[$key]['l_flow'] = ltrim(substr($large['code'],53,6),'0').'-'.$small['l_flow'];
  360. $rows[$key]['qrcode'] = $code;
  361. // $rows[$key]['pCode'] = $this->qrcode($code);
  362. //批次号特殊情况判断
  363. if (substr($code,76,10)=='0000000000'){
  364. $rows[$key]['batch'] = ltrim(substr($code,66,10),'0');
  365. }else{
  366. $rows[$key]['batch'] = ltrim(substr($code,66,10),'0').'、'.ltrim(substr($code,76,10),'0');
  367. }
  368. $key++;
  369. }
  370. }
  371. $data = [
  372. 'type'=>$type,
  373. 'numn'=>$numn,
  374. 'rows'=>$rows,
  375. 'ids'=>$ids,
  376. ];
  377. $this->success('成功','',$data);
  378. }
  379. $this->view->assign('ids',$req['ids']);
  380. return $this->view->fetch();
  381. }
  382. /**
  383. * 手动打码(小件)
  384. */
  385. public function print_ls()
  386. {
  387. //设置过滤方法
  388. $this->request->filter(['strip_tags', 'trim']);
  389. $req = $this->request->param();
  390. if ($this->request->isAjax()) {
  391. parse_str($req['data'],$req);
  392. $ids = explode(',',$req['row']['ids']);
  393. $type = $req['row']['type'];
  394. $numn = $req['row']['numn'];
  395. $userInfo = Session::get('admin');
  396. $company_id = (int)$userInfo['company'];
  397. $qcodeLarge = new QcodeLarge();
  398. $qcodeSmall = new QcodeSmall();
  399. $small = $qcodeSmall->name($company_id.'_'."qcode_small")->where('_id',$ids[0])->find();
  400. $small = json_decode($small,true);
  401. $large = $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$small['large_id'])->find();
  402. $large = json_decode($large,true);
  403. $bach = $this->model->name($company_id.'_'."qcode_bach")->where('_id',$small['bach_id'])->find();
  404. $company_name = json_decode($bach,true)['supplier_name'];
  405. $product_name = json_decode($bach,true)['matter_name'];
  406. //获取全部小件id
  407. $rows = [];
  408. $code = $small['code'];
  409. $rows[0]['company_name'] = $company_name;
  410. $rows[0]['product_name'] = $product_name;
  411. $rows[0]['date'] = substr_replace(substr_replace('20'.substr($code,38,6), '-', 4, 0), '-', 7, 0);
  412. $rows[0]['l_flow'] = ltrim(substr($large['code'],53,6),'0').'-'.$small['l_flow'];
  413. $rows[0]['qrcode'] = $code;
  414. // $rows[0]['pCode'] = $this->qrcode($code);
  415. //批次号特殊情况判断
  416. if (substr($code,76,10)=='0000000000'){
  417. $rows[0]['batch'] = ltrim(substr($code,66,10),'0');
  418. }else{
  419. $rows[0]['batch'] = ltrim(substr($code,66,10),'0').'、'.ltrim(substr($code,76,10),'0');
  420. }
  421. $data = [
  422. 'type'=>$type,
  423. 'numn'=>$numn,
  424. 'rows'=>$rows,
  425. 'ids'=>$ids,
  426. ];
  427. $this->success('成功','',$data);
  428. }
  429. $this->view->assign('ids',$req['ids']);
  430. return $this->view->fetch();
  431. }
  432. /**
  433. * 设置打印数量
  434. */
  435. public function set_num()
  436. {
  437. $req = $this->request->param();
  438. //获取数据
  439. $status = $req['status'];
  440. $num = (int)$req['num'];
  441. $ids = $req['ids'];
  442. //获取company_id
  443. $userInfo = Session::get('admin');
  444. $company_id = (int)$userInfo['company'];
  445. //自动打印大件
  446. if($status==1){
  447. $qcodeLarge = new QcodeLarge();
  448. foreach ($ids as $v){
  449. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$v)->setInc('p_nums',$num);
  450. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$v)->setField('l_print','1');
  451. }
  452. }elseif($status==2){
  453. $qcodeSmall = new QcodeSmall();
  454. foreach ($ids as $v){
  455. $qcodeSmall->name($company_id.'_'."qcode_small")->where('large_id',$v)->setInc('p_nums',$num);
  456. }
  457. }elseif($status==3){
  458. $qcodeSmall = new QcodeSmall();
  459. $qcodeSmall->name($company_id.'_'."qcode_small")->where('_id',$ids[0])->setInc('p_nums',$num);
  460. }
  461. }
  462. public function vo2()
  463. {
  464. $num1 = 0;
  465. $num2 = 1;
  466. if($num2<=$num1){
  467. $this->error('同步num1到num2之间的数据, 要求num2大于num1');
  468. }
  469. // 连接到其他数据库
  470. $config = [
  471. 'type' => 'mysql',
  472. 'hostname' => '127.0.0.1',
  473. 'database' => 'dm_7in6_com',
  474. 'username' => 'root',
  475. 'password' => 'root',
  476. 'charset' => 'utf8mb4',
  477. 'prefix' => 'qr_',
  478. ];
  479. $db = Db::connect($config);
  480. //查询主表记录
  481. $rows1 = $db->name('qcode_bach')
  482. ->limit($num1,$num2-$num1)
  483. ->order('id desc')
  484. ->select();
  485. $userInfo = Session::get('admin');
  486. $company_id = (int)$userInfo['company'];
  487. foreach($rows1 as $v){
  488. //1. 查询mongodb中是否存在该条记录
  489. $bool = $this->model->name($company_id.'_'."qcode_bach")->where('oid_id',$v['id'])->find();
  490. if ($bool) continue;
  491. //2. 获取设置主表数据
  492. $row = $v;
  493. unset($row['id']);
  494. $row = array_merge(['oid_id'=>$v['id']],$row);
  495. //3. 插入主表记录到mongodb中
  496. $qcodeBach = new \app\admin\model\QcodeBach();
  497. $qcodeBach->save($row);
  498. $pid = $qcodeBach->getLastInsID();
  499. $rows2 = $db->name('qcode_large')
  500. ->where('bach_id',$v['id'])
  501. ->select();
  502. foreach ($rows2 as $value){
  503. //1. 查询mongodb中是否存在该条记录
  504. $qcodeLarge = new QcodeLarge();
  505. $bool = $qcodeLarge->name($company_id.'_'."qcode_large")->where('oid_id',$value['id'])->find();
  506. if ($bool) continue;
  507. $row = $value;
  508. $row['old_bach_id'] = $value['bach_id'];
  509. $row['bach_id'] = $pid;
  510. $row['old_id'] = $value['id'];
  511. unset($row['id']);
  512. $qcodeLarge->save($row);
  513. $pid3 = $qcodeLarge->getLastInsID();
  514. $rows3 = $db->name('qcode_small')
  515. ->where('large_id',$value['id'])
  516. ->select();
  517. foreach ($rows3 as $value3){
  518. //1. 查询mongodb中是否存在该条记录
  519. $qcodeSmall = new QcodeSmall();
  520. $bool = $qcodeSmall->name($company_id.'_'."qcode_large")->where('oid_id',$value3['id'])->find();
  521. if ($bool) continue;
  522. $row = $value3;
  523. $row['old_bach_id'] = $value3['bach_id'];
  524. $row['old_large_id'] = $value3['large_id'];
  525. $row['old_id'] = $value3['id'];
  526. $row['bach_id'] = $pid;
  527. $row['large_id'] = $pid3;
  528. unset($row['id']);
  529. $qcodeSmall->save($row);
  530. }
  531. }
  532. }
  533. $this->success('成功');
  534. }
  535. /**
  536. * 新增
  537. */
  538. public function add()
  539. {
  540. if ($this->request->isAjax()){
  541. $req = $this->request->post();
  542. if(!isset($req['row']['product_code']) || empty($req['row']['product_code'])){
  543. $this->error('请填写编号');
  544. }else{
  545. $data = [
  546. 'product_code'=>$req['row']['product_code'],
  547. 'delete_time'=>'',
  548. ];
  549. if ($this->model->where($data)->find()){
  550. $this->error('编号已存在');
  551. }
  552. }
  553. if(!isset($req['row']['product_name']) || empty($req['row']['product_name'])){
  554. $this->error('请填写名称');
  555. }else{
  556. $data = [
  557. 'product_name'=>$req['row']['product_name'],
  558. 'delete_time'=>'',
  559. ];
  560. if ($this->model->where($data)->find()){
  561. $this->error('名称已存在');
  562. }
  563. }
  564. if(!isset($req['row']['temple']) || empty($req['row']['temple'])){
  565. $this->error('请填写辅料代号');
  566. }
  567. if(!isset($req['row']['main_unit'])){
  568. $this->error('请填写主计量单位');
  569. }
  570. if(!isset($req['row']['sec_unit'])){
  571. $this->error('请填写辅计量单位');
  572. }
  573. if(!isset($req['row']['proportion'])){
  574. $this->error('请填写换算关系');
  575. }
  576. $data = [
  577. 'product_code' =>$req['row']['product_code'],
  578. 'product_name' =>$req['row']['product_name'],
  579. 'temple' =>$req['row']['temple'],
  580. 'main_unit' =>$req['row']['main_unit'],
  581. 'sec_unit' =>$req['row']['sec_unit'],
  582. 'proportion' =>$req['row']['proportion'],
  583. 'create_time' =>date('Y-m-d H:i:s'),
  584. ];
  585. //插入数据
  586. $re = $this->model->save($data);
  587. if($re){
  588. $this->success('成功');
  589. }else{
  590. $this->error('失败');
  591. }
  592. }
  593. return $this->view->fetch();
  594. }
  595. /**
  596. * 编辑
  597. */
  598. public function edit($ids = NULL)
  599. {
  600. $req = $this->request->param();
  601. if ($this->request->isAjax()){
  602. parse_str($req['data'],$req);
  603. if(!isset($req['row']['type']) || empty($req['row']['type'])){
  604. $this->error('请选择修改类型');
  605. }
  606. if(!isset($req['row']['numn']) || empty($req['row']['numn'])){
  607. $this->error('请填写修改数量');
  608. }
  609. $ids = explode(',',$req['row']['ids']);
  610. $data = [
  611. 'update_time'=>time(),
  612. 'sync_flag'=>0
  613. ];
  614. $userInfo = Session::get('admin');
  615. $company_id = (int)$userInfo['company'];
  616. $qcodeLarge = new QcodeLarge();
  617. if($req['row']['type']==1){//修改大件重量
  618. $data['l_weight'] = intval($req['row']['numn'])*100;
  619. foreach($ids as $val){
  620. //查询code
  621. $large = $qcodeLarge->name($company_id.'_'."qcode_large")->field('code')->where('_id',$val)->find();
  622. $code = json_decode($large,true)['code'];
  623. if($code){
  624. $code1 = substr($code,0,59);
  625. $code2 = substr($code,65,85);
  626. $data['code'] = $code1.str_pad(intval($req['row']['numn'])*100,6, '0', STR_PAD_LEFT).$code2;
  627. //修改code和大件重量
  628. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$val)->update($data);
  629. }
  630. }
  631. $this->success('修改成功');
  632. }
  633. if($req['row']['type']==2){//修改大件总张数
  634. $data['l_num'] = intval($req['row']['numn']);
  635. foreach($ids as $val){
  636. //查询l_num, 判断是否可以修改总张数
  637. $large = $qcodeLarge->name($company_id.'_'."qcode_large")->field('l_num')->where('_id',$val)->find();
  638. $l_num = json_decode($large,true)['l_num'];
  639. if($l_num > 0 || $l_num==null){
  640. //可以修改
  641. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$val)->update($data);
  642. }else{
  643. $this->error('该产品不能修改大件数量');
  644. }
  645. }
  646. $this->success('修改成功');
  647. }
  648. }
  649. $this->view->assign('ids',$req['ids']);
  650. return $this->view->fetch();
  651. }
  652. /**
  653. * 删除
  654. */
  655. public function del($ids = NULL)
  656. {
  657. $userInfo = Session::get('admin');
  658. $company_id = (int)$userInfo['company'];
  659. $qcodeLarge = new QcodeLarge();
  660. $qcodeSmall = new QcodeSmall();
  661. $data = [
  662. 'delete_time'=>date('Y-m-d H:i:s')
  663. ];
  664. $bool1 = $this->model->name($company_id.'_'."qcode_bach")->where('_id',$ids)->update($data);
  665. $bool2 = $qcodeLarge->name($company_id.'_'."qcode_large")->where('bach_id',$ids)->update($data);
  666. $bool3 = $qcodeSmall->name($company_id.'_'."qcode_small")->where('bach_id',$ids)->update($data);
  667. if($bool1!==false && $bool2!==false && $bool3!==false){
  668. $this->success('删除成功');
  669. }else{
  670. $this->error('删除失败');
  671. }
  672. }
  673. /**
  674. * 绑定
  675. */
  676. public function bind($ids = NULL)
  677. {
  678. $userInfo = Session::get('admin');
  679. $company_id = (int)$userInfo['company'];
  680. $db = new QcodeCompany();
  681. //查询是否已经添加
  682. $row = $db->name($company_id.'_'."qcode_company")
  683. ->where(['delete_time'=>'','product_id'=>$ids])
  684. ->find();
  685. if($row) $this->success('已添加');
  686. $data = [
  687. 'product_id'=>$ids,
  688. 'create_time'=>date('Y-m-d H:i:s'),
  689. ];
  690. //插入qcode_company数据
  691. $bool = $db->save($data);
  692. if($bool){
  693. $this->success('添加成功');
  694. }else{
  695. $this->error('添加失败');
  696. }
  697. }
  698. /**
  699. * 解绑
  700. */
  701. public function unbind($ids = NULL)
  702. {
  703. $userInfo = Session::get('admin');
  704. $company_id = (int)$userInfo['company'];
  705. $db = new QcodeCompany();
  706. //查询是否已经添加
  707. $bool = $db->name($company_id.'_'."qcode_company")
  708. ->where(['delete_time'=>'','product_id'=>$ids])
  709. ->update(['delete_time'=>date('Y-m-d H:i:s')]);
  710. if($bool){
  711. $this->success('删除成功');
  712. }else{
  713. $this->error('删除失败');
  714. }
  715. }
  716. /**
  717. * 补打标签
  718. */
  719. public function reprint()
  720. {
  721. if ($this->request->isAjax()) {
  722. $data = $this->request->param();
  723. parse_str($data['data'],$req);
  724. if(!isset($req['row']['nickname']) || empty($req['row']['nickname'])){
  725. $this->error('请填写公司名称');
  726. }
  727. if((!isset($req['row']['product_name']) || empty($req['row']['product_name'])) && (!isset($req['row']['product_code']) || empty($req['row']['product_code']))){
  728. $this->error('请填写辅料名称或辅料编号');
  729. }
  730. if(!isset($req['row']['sqrcd']) || empty($req['row']['sqrcd'])){
  731. $this->error('请填写配盘数');
  732. }
  733. if(!isset($req['row']['num']) || empty($req['row']['num'])){
  734. $this->error('请填写数量');
  735. }
  736. //查询公司信息
  737. $qcodeGsmc = new QcodeGsmc();
  738. $print_code = $qcodeGsmc
  739. ->field('id, nickname, print_code')
  740. ->where('nickname',$req['row']['nickname'])
  741. ->find();
  742. if (!$print_code) $this->error('未查到公司信息');
  743. $print_code = json_decode($print_code,true);
  744. //查询产品信息
  745. $qcodeProduct = new QcodeProduct();
  746. if(!empty($req['row']['product_code'])){
  747. $sel = $qcodeProduct
  748. ->field('id, product_name, product_code, temple')
  749. ->where('product_code',$req['row']['product_code'])
  750. ->find();
  751. }else{
  752. $sel = $qcodeProduct
  753. ->field('id, product_name, product_code, temple')
  754. ->where('product_name',$req['row']['product_name'])
  755. ->find();
  756. }
  757. if (!$sel) $this->error('未查到辅料信息');
  758. $sel = json_decode($sel,true);
  759. $data = [
  760. 'company_name'=>$req['row']['nickname'],
  761. 'product_name'=>$sel['product_name'],
  762. 'sqrcd'=>$req['row']['sqrcd'],
  763. 'num'=>$req['row']['num'],
  764. 'main_unit'=>'盘',
  765. 'sec_unit'=>'万米',
  766. 'date'=>date('Y-m-d'),
  767. 'batch'=>substr(date('Ymd'),2,6),
  768. 'l_flow'=>1,
  769. ];
  770. $num = str_pad($req['row']['num'], 3, '0', STR_PAD_LEFT);
  771. $qrcode = 'AB'.'92'.$sel['temple'].'0'.$print_code['print_code'].('000'.$sel['product_code']).$data['batch'].$num.$data['batch'].'000001'.'000000'.'2'.'0000'.$data['batch'].'0000000000';
  772. $data['pCode'] = $this->qrcode($qrcode);
  773. $data['qrcode'] = $qrcode;
  774. $this->success('成功','',$data);
  775. }
  776. return $this->view->fetch();
  777. }
  778. //公司搜索
  779. public function sel_n()
  780. {
  781. $name = $this->request->param()['q_word'][0];
  782. $where = [
  783. 'nickname'=>new \MongoDB\BSON\Regex($name),
  784. 'delete_time'=>'',
  785. ];
  786. $qcodeGsmc = new QcodeGsmc();
  787. $sel = $qcodeGsmc
  788. ->field('nickname')
  789. ->where($where)
  790. ->limit(0,10)
  791. ->select();
  792. return json(['list' => $sel, 'total' => count($sel)]);
  793. }
  794. //辅料名称搜索
  795. public function sel_p()
  796. {
  797. $name = $this->request->param()['q_word'][0];
  798. $where = [
  799. 'product_name'=>new \MongoDB\BSON\Regex($name),
  800. 'delete_time'=>'',
  801. ];
  802. $qcodeProduct = new QcodeProduct();
  803. $sel = $qcodeProduct
  804. ->field('product_name, product_code')
  805. ->where($where)
  806. ->limit(0,10)
  807. ->select();
  808. foreach ($sel as $k=>$v) {
  809. $oid = $v['_id']->jsonSerialize();
  810. $sel[$k]['id'] = $oid['$oid'];
  811. }
  812. return json(['list' => $sel, 'total' => count($sel)]);
  813. }
  814. //辅料编码搜索
  815. public function sel_c()
  816. {
  817. $name = $this->request->param()['q_word'][0];
  818. $where = [
  819. 'product_code'=>new \MongoDB\BSON\Regex($name),
  820. 'delete_time'=>'',
  821. ];
  822. $qcodeProduct = new QcodeProduct();
  823. $sel = $qcodeProduct
  824. ->field('id, product_name, product_code')
  825. ->where($where)
  826. ->limit(0,10)
  827. ->select();
  828. foreach ($sel as $k=>$v) {
  829. $oid = $v['_id']->jsonSerialize();
  830. $sel[$k]['id'] = $oid['$oid'];
  831. }
  832. return json(['list' => $sel, 'total' => count($sel)]);
  833. }
  834. /**
  835. * 二维码生成类
  836. */
  837. public function qrcode($url)//二维码生成类
  838. {
  839. $url=$url;
  840. $level=3;
  841. $size=6;
  842. Vendor('phpqrcode.phpqrcode');//加载生成二维码的核心类
  843. $errorCorrectionLevel =intval($level) ;//容错级别
  844. $matrixPointSize = intval($size);//生成图片大小
  845. //生成二维码图片
  846. $object = new \QRcode();
  847. //打开缓冲区
  848. ob_start();
  849. $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
  850. //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。
  851. $imageString = base64_encode(ob_get_contents());
  852. //关闭缓冲区
  853. ob_end_clean();
  854. //把生成的base64字符串返回给前端
  855. // $data = array(
  856. // 'labelcode'=>$url,
  857. // 'code'=>200,
  858. // 'data'=>$imageString,
  859. // 'product_code'=>$url
  860. // );
  861. return 'data:image/png;base64,'.$imageString;
  862. }
  863. }