QcodeAdd.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\QcodeCompany;
  4. use app\admin\model\QcodeGsmc;
  5. use app\admin\model\QcodeProduct;
  6. use app\common\controller\Backend;
  7. use app\admin\model\ResetFlow;
  8. use app\admin\model\QcodeLarge;
  9. use app\admin\model\QcodeBach;
  10. use app\admin\model\QcodeSmall;
  11. use app\admin\model\QcodeLiushui;
  12. use fast\Arr;
  13. use think\Db;
  14. use think\Session;
  15. class QcodeAdd extends Backend
  16. {
  17. /**
  18. * 首页展示
  19. */
  20. public function index()
  21. {
  22. //获取登录账号信息【厂商信息】
  23. $userinfo = Session::get('admin');
  24. $data = [
  25. 'nickname' => $userinfo['company_name'],
  26. 'postcode' => $userinfo['postcode'],
  27. 'mobile' => $userinfo['mobile'],
  28. 'printer_code' => $userinfo['printer_code'],
  29. 'company_address' => $userinfo['company_address']
  30. ];
  31. $this->view->assign('row',$data);
  32. //将对此账号创建公司唯一id,进行创建公司对应公司表名company id
  33. $user_company_value = Db::name('admin')->where('id', $userinfo['id'])->value('company');
  34. if (empty($user_company_value)) {
  35. $max_company = Db::name('admin')->max('company');
  36. $new_company_value = $max_company ? ($max_company + 1) : 1;
  37. Db::name('admin')->where('id', $userinfo['id'])->update(['company' => $new_company_value]);
  38. } else {
  39. $new_company_value = $user_company_value;
  40. }
  41. $tableNames = [
  42. $new_company_value . '_qcode_bach',
  43. $new_company_value . '_qcode_company',
  44. $new_company_value . '_qcode_large',
  45. $new_company_value . '_qcode_liushui',
  46. $new_company_value . '_qcode_small',
  47. $new_company_value . '_reset_flow',
  48. ];
  49. foreach ($tableNames as $tableName) {
  50. try {
  51. Db::connect('mongodb')->name($tableName)->insert(['init' => 1]);
  52. // 删除测试数据(可选,如果不想保留这个测试数据)
  53. Db::connect('mongodb')->name($tableName)->where('init', 1)->delete();
  54. } catch (\Exception $e) {
  55. echo "创建集合 {$tableName} 失败:" . $e->getMessage();
  56. }
  57. }
  58. // //统计产品信息数据
  59. // $mongo = \think\Db::connect('mongodb');
  60. // // 获取成品入仓表数据
  61. // $mongo_products = $mongo
  62. // ->name('finished_products')
  63. // ->field('成品名称, jjcp_cpdh, 订单编号, jjcp_gdbh, jjcp_sl')
  64. // ->order('UniqId','desc')
  65. // ->where('jjcp_smb','末 板')
  66. // ->limit(1,50)
  67. // ->select();
  68. //
  69. // $grouped_products = [];
  70. // foreach ($mongo_products as $item) {
  71. // $group_key = $item['订单编号'] . '_' . $item['jjcp_gdbh'];
  72. // if (!isset($grouped_products[$group_key])) {
  73. // $grouped_products[$group_key] = [
  74. // 'order_ddbh' => $item['订单编号'],
  75. // 'gdbh' => $item['jjcp_gdbh'],
  76. // 'cpmc' => $item['成品名称'],
  77. //// 'cpdh' => $item['jjcp_cpdh'],
  78. // 'ruku_sl' => 0, // 入库数量
  79. // 'remain_stock' => 0, // 剩余库存
  80. // 'sl' => 0
  81. // ];
  82. // }
  83. // // 累加入库数量
  84. // $grouped_products[$group_key]['ruku_sl'] += (int)$item['jjcp_sl'];
  85. // }
  86. // // 查询库存表并计算
  87. // foreach ($grouped_products as $group_key => &$prod) {
  88. // $where = [
  89. // 'order_ddbh' => $prod['order_ddbh'],
  90. // 'gdbh' => $prod['gdbh'],
  91. // 'cpmc' => $prod['cpmc'],
  92. // ];
  93. //
  94. // $inventory = $mongo->name('inventory_summary')->where($where)->find();
  95. // $prod['remaining_quantity'] = $inventory ? (int)$inventory['remaining_quantity'] : 0;
  96. // $prod['sl'] = $prod['remaining_quantity'] > 0 ? $prod['remaining_quantity'] : $prod['ruku_sl'];
  97. // }
  98. // unset($prod);
  99. // $matched_products = array_values($grouped_products);
  100. // $this->view->assign('product', $matched_products);
  101. return $this->view->fetch();
  102. }
  103. /**
  104. * 获取产品信息
  105. * 查询入仓表【finished_products】
  106. */
  107. public function product()
  108. {
  109. $params = input('');
  110. $page = max(1, (int)$params['page']);
  111. $limit = max(1, (int)$params['limit']);
  112. $offset = ($page - 1) * $limit;
  113. // 获取工单编号列表
  114. $db3 = Db::connect(config('database.db3'));
  115. $gdRows = $db3->query("SELECT DISTINCT `Gd_gdbh`, `Gd_客户代号`, `Gd_cpdh` FROM `工单_基本资料` WHERE `Gd_客户代号` = ?", ['J0031']);
  116. $Gd_gdbhList = array_column($gdRows, 'Gd_gdbh');
  117. $gdbhList = array_column($gdRows, 'Gd_cpdh');
  118. if (empty($gdbhList)) {
  119. $this->assign([
  120. 'data' => '',
  121. 'total' =>'',
  122. ]);
  123. }
  124. // Mongo 查询
  125. $mongo = \think\Db::connect('mongodb');
  126. $where = ['jjcp_cpdh' => ['in', $gdbhList],'jjcp_gdbh' => ['in', $Gd_gdbhList], 'jjcp_smb' => '末 板'];
  127. if (!empty($params['search'])) {
  128. $where['jjcp_cpdh|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($params['search'], 'i');
  129. }
  130. // 1. 查询所有符合条件的成品数据(不分页)
  131. $all_products = $mongo->name('finished_products')
  132. ->where($where)
  133. ->order('UniqId', 'desc')
  134. ->select();
  135. // 2. 按订单编号 + 工单编号分组聚合
  136. $grouped_products = [];
  137. foreach ($all_products as $item) {
  138. $group_key = $item['订单编号'] . '_' . $item['jjcp_gdbh'];
  139. if (!isset($grouped_products[$group_key])) {
  140. $grouped_products[$group_key] = [
  141. 'order_ddbh' => $item['订单编号'],
  142. 'gdbh' => $item['jjcp_gdbh'],
  143. 'cpmc' => $item['成品名称'],
  144. 'ruku_sl' => 0,
  145. 'remain_stock' => 0,
  146. 'sl' => 0
  147. ];
  148. }
  149. $grouped_products[$group_key]['sl'] += (int)$item['jjcp_sl'];
  150. }
  151. // 3. 将聚合后结果分页(稳定分页)
  152. $all_grouped = array_values($grouped_products);
  153. $total = count($all_grouped);
  154. $paged_grouped = array_slice($all_grouped, $offset, $limit);
  155. // 4. 获取库存数据
  156. foreach ($paged_grouped as &$prod) {
  157. $inventory = $mongo->name('inventory_summary')->where([
  158. 'order_ddbh' => $prod['order_ddbh'],
  159. 'gdbh' => $prod['gdbh'],
  160. 'cpmc' => $prod['cpmc'],
  161. ])->find();
  162. // $prod['remaining_quantity'] = $inventory ? (int)$inventory['remaining_quantity'] : 0;
  163. // $prod['sl'] = $prod['remaining_quantity'] > 0 ? $prod['remaining_quantity'] : $prod['ruku_sl'];
  164. }
  165. unset($prod);
  166. return json([
  167. 'code' => 1,
  168. 'data' => $paged_grouped,
  169. 'total' => $total,
  170. 'page' => $page,
  171. 'limit' => $limit
  172. ]);
  173. }
  174. // public function product()
  175. // {
  176. //
  177. // $db3 = Db::connect(config('database.db3'));
  178. // $query = $db3->query("SELECT * FROM `工单_基本资料` WHERE `Gd_客户代号` = ?", ['J0031']);
  179. //
  180. // echo "<pre>";
  181. // print_r($query);
  182. // echo "</pre>";
  183. // die;
  184. //
  185. // $params = input('');
  186. // $page = max(1, (int)$params['page']);
  187. // $limit = max(1, (int)$params['limit']);
  188. // $offset = ($page - 1) * $limit;
  189. //
  190. // $mongo = \think\Db::connect('mongodb');
  191. // $where = [];
  192. // if (!empty($params['search'])) {
  193. // // 使用正则表达式来实现模糊匹配,'i' 表示忽略大小写
  194. // $where['jjcp_cpdh|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($params['search'], 'i');
  195. // }
  196. //// if (!empty($where)) {
  197. //// $mongo_products = $mongo->name('finished_products')
  198. //// ->where($where)
  199. //// ->limit($offset, $limit)
  200. //// ->order('UniqId','desc')
  201. //// ->where('jjcp_smb','末 板')
  202. //// ->select();
  203. //// } else {
  204. //// // 如果没有条件,仍然执行查询,避免错误
  205. //// $mongo_products = $mongo->name('finished_products')
  206. //// ->limit(1, 10)
  207. //// ->order('UniqId','desc')
  208. //// ->where('jjcp_smb','末 板')
  209. //// ->select();
  210. //// }
  211. //
  212. //
  213. //
  214. //
  215. // $mongo_products = $mongo->name('finished_products')
  216. // ->where($where)
  217. // ->limit($offset, $limit)
  218. // ->order('UniqId','desc')
  219. // ->where('jjcp_smb','末 板')
  220. // ->select();
  221. // echo "<pre>";
  222. // print_r($mongo_products);
  223. // echo "<pre>";die;
  224. //
  225. // //计算【订单编号】【jjcp_gdbh】相同的数据进行统计汇总累加入库数量【jjcp_sl】
  226. // $grouped_products = [];
  227. // foreach ($mongo_products as $item) {
  228. // $group_key = $item['订单编号'] . '_' . $item['jjcp_gdbh'];
  229. // if (!isset($grouped_products[$group_key])) {
  230. // $grouped_products[$group_key] = [
  231. // 'order_ddbh' => $item['订单编号'],
  232. // 'gdbh' => $item['jjcp_gdbh'],
  233. // 'cpmc' => $item['成品名称'],
  234. //// 'cpdh' => $item['jjcp_cpdh'],
  235. // 'ruku_sl' => 0, // 入库数量
  236. // 'remain_stock' => 0, // 剩余库存
  237. // 'sl' => 0
  238. // ];
  239. // }
  240. // $grouped_products[$group_key]['ruku_sl'] += (int)$item['jjcp_sl'];
  241. // }
  242. // // 查询库存表并计算
  243. // foreach ($grouped_products as $group_key => &$prod) {
  244. // $where = [
  245. // 'order_ddbh' => $prod['order_ddbh'],
  246. // 'gdbh' => $prod['gdbh'],
  247. // 'cpmc' => $prod['cpmc'],
  248. // ];
  249. // $inventory = $mongo->name('inventory_summary')->where($where)->find();
  250. // $prod['remaining_quantity'] = $inventory ? (int)$inventory['remaining_quantity'] : 0;
  251. // $prod['sl'] = $prod['remaining_quantity'] > 0 ? $prod['remaining_quantity'] : $prod['ruku_sl'];
  252. // }
  253. // unset($prod);
  254. // $matched_products = array_values($grouped_products);
  255. //
  256. // // 获取总数
  257. // $total = $mongo->name('finished_products')
  258. // ->where($where)
  259. // ->count();
  260. //
  261. // return json([
  262. // 'code' => 1,
  263. // 'data' => $matched_products,
  264. // 'total' => $total,
  265. // 'page' => $page,
  266. // 'limit' => $limit
  267. // ]);
  268. // }
  269. // public function product(){
  270. // $QcodeProduct = new QcodeProduct();
  271. // $ResetFlow = new ResetFlow();
  272. // $userinfo = Session::get('admin');
  273. // if ($this->request->isAjax() === false){
  274. // $this->error('请求错误');
  275. // }
  276. // $product_code = input('product_code');
  277. //
  278. // if (empty($product_code)){
  279. // $this->error('参数错误');
  280. // }
  281. // $product = $QcodeProduct->where('product_code',$product_code)->find();
  282. // if (empty($product)){
  283. // $this->error('未找到该产品信息');
  284. // }
  285. //
  286. // $flow = $ResetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',substr(json_encode($product['_id']),9,-2))->find();
  287. // $row = [
  288. // 'temple' => $product['temple'],
  289. // 'flow' => isset($flow['l_flow'])?(int)$flow['l_flow']+1:'',
  290. // 'bach' => isset($flow['bach_num'])?(int)$flow['bach_num']+1:'',
  291. // ];
  292. // if (!empty($flow)){
  293. // $row['flow'] = $flow['l_flow']+1;
  294. // $row['bach'] = $flow['bach_num']+1;
  295. // }
  296. //
  297. // return json(['code'=>1,'data'=>$row]);
  298. // }
  299. /**
  300. * 增加新批次
  301. */
  302. public function add()
  303. {
  304. $product = new QcodeProduct();
  305. $bach = new QcodeBach();
  306. $large = new QcodeLarge();
  307. $small = new QcodeSmall();
  308. $liushui = new QcodeLiushui();
  309. $resetFlow = new ResetFlow();
  310. $QcodeCompany = new QcodeCompany();
  311. if ($this->request->isAjax() === false){
  312. $this->error('请求错误');
  313. }
  314. $rows = input('row');
  315. if (empty($rows)){
  316. $this->error('参数错误');
  317. }
  318. $rows = json_decode($rows);
  319. $data = [];
  320. foreach ($rows as $value){
  321. foreach ($value as $k=>$v){
  322. $data[$k] = $v;
  323. }
  324. }
  325. // echo "接口获取";
  326. // echo "<pre>";
  327. // print_r($data);
  328. // echo "<pre>";die;
  329. $mongo = \think\Db::connect('mongodb');
  330. // 构建查询条件
  331. $where = [
  332. 'gdbh' => $data['gdbh'],
  333. 'order_ddbh' => $data['order_ddbh'],
  334. // 'cpdh' => $data['cpdh'],
  335. 'cpmc' => $data['cpmc'],
  336. ];
  337. //插入字段
  338. $insertData = [
  339. 'gdbh' => $data['gdbh'],
  340. 'order_ddbh' => $data['order_ddbh'],
  341. // 'cpdh' => $data['cpdh'],
  342. 'cpmc' => $data['cpmc'],
  343. 'total_ru_quantity' => $data['sl'],//入库数量
  344. 'total_chu_quantity' => $data['number'],//出库数量
  345. 'remaining_quantity' => $data['sl'] - $data['number'],//剩余结存数量
  346. 'created_at' => date('Y-m-d H:i:s'),
  347. 'updated_at' => '',
  348. 'mod_rq' => '',
  349. 'company' => '',
  350. ];
  351. // 查询是否已存在记录
  352. $list = $mongo->name('inventory_summary')->where($where)->find();
  353. if ($list) {
  354. // 如果数据存在,更新库存汇总记录
  355. $ru = $list['total_ru_quantity'] + $data['sl'];//累计入库数量
  356. $chu = $list['total_chu_quantity'] + $data['sl'];//累计出库数量
  357. $jiecun = $data['sl'] - $data['number'];//剩余结存数量
  358. $updateResult = $mongo->name('inventory_summary')
  359. ->where($where)
  360. ->update([
  361. 'total_ru_quantity' => $ru,
  362. 'total_chu_quantity' => $chu,
  363. 'remaining_quantity' => $jiecun,
  364. 'updated_at' => date('Y-m-d H:i:s'),
  365. ]);
  366. } else {
  367. // 数据不存在则插入新记录
  368. $mongo->name('inventory_summary')->insert($insertData);
  369. }
  370. //记录库存明细
  371. $mongo->name('inventory_records')->insert($insertData);
  372. // // 调试输出(可删)
  373. // echo "<pre>"; print_r($list); echo "</pre>";
  374. // die;
  375. if ($data['danwei'] == 1){
  376. $num = $data['number'];
  377. $tray_num = $data['tray_num'];
  378. $tray_num1 = $data['tray_num'];
  379. $box_number = $data['box_number'];
  380. //$small_num = (int)ceil((int)总数量(张/个)/(int)张数);
  381. $small_num = (int)ceil((int)$data['number']/(int)$data['box_number']);
  382. $large_num = (int)ceil($small_num/$tray_num);
  383. }else{
  384. $num = 0;
  385. $tray_num = $data['volume_num'];
  386. $small_num = $data['small_num'];
  387. $large_num = (int)ceil($small_num/$tray_num);
  388. $box_number = 1;
  389. $tray_num1 = 1;
  390. }
  391. $userinfo = Session::get('admin');//获取用户信息
  392. $arr = [
  393. 'batch' => $data['batch'],
  394. 'create_time' => time(),
  395. 'delect_time' => '',
  396. 'sync_flag' => 0,
  397. ];
  398. $productres = $QcodeCompany->save($arr);
  399. if ($productres === 0){
  400. $this->error('添加失败');
  401. }
  402. $batchList = [
  403. 'userid' => $userinfo['id'],
  404. 'supplier_id' => $userinfo['id'],
  405. 'supplier_code' => $userinfo['printer_code'],
  406. 'supplier_name' => $data['company_name'],//公司名称
  407. 'matter_name' => $data['cpmc'],//产品名称
  408. 'matter_no' => $data['batch'],//生产批次号
  409. 'order_ddbh' => $data['order_ddbh'],//销售订单号
  410. 'matter_id' => $data['batch'],
  411. // 'matter_type' => $data['temple'],
  412. 'matter_type' => '01',
  413. 'manufacture_date' => (int)date('ymd',strtotime($data['manufacture_date'])),//生产日期
  414. 'print_date' => (int)date('ymd',strtotime($data['print_date'])),//打码日期
  415. 'danwei' => $data['danwei'],//单位
  416. 'num' => $num,//总数量(张/个)
  417. 'tray_num' => $tray_num1,//每层箱数
  418. 'box_num' => $box_number,//每托层数
  419. 'total_boxes' => $data['total_boxes'],//每托盘箱数
  420. 'small_num' => $small_num,//每托盘箱数
  421. 'pallet_height' => $data['pallet_height'],//每托高度
  422. 'pallet_length' => $data['pallet_length'],//托盘规格
  423. 'pallet_width' => $data['pallet_width'],//托盘规格
  424. 'larger_num' => $large_num,//大件(总托数)
  425. 'l_reservation' => '',
  426. 'l_flow' => $data['big_liushui'],
  427. 'l_weight' => $data['big_weight'],//大件重量
  428. 's_flow' => $data['small_start_liushui'],
  429. 's_weight' => $data['small_weight'],//小件重量
  430. 's_reservation' => '',
  431. 'bach_status' => 0,
  432. 'bach_num' => $data['batch'],
  433. 'large_endnum' => $data['big_liushui'] + $data['box_num'] -1,
  434. 'create_time' => time(),//新增时间
  435. ];
  436. $res = $bach->save($batchList);
  437. if ($res === 0){
  438. $this->error('添加失败');
  439. }
  440. $flow = [
  441. 'l_flow' => (int)$batchList['large_endnum'],
  442. 'bach_num' => $batchList['bach_num'],
  443. ];
  444. if ($resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_id'])->find()){
  445. $resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_id'])->update($flow);
  446. }else{
  447. $flow['product_id'] = $batchList['matter_id'];
  448. $resetFlow->save($flow);
  449. }
  450. $last_id = $bach->getLastInsID();
  451. if ($last_id){
  452. //插入大小二维码数据
  453. //二维码数据不变区域
  454. // echo "<pre>";
  455. // print_r($batchList);
  456. // echo "<pre>";
  457. // die;
  458. // echo "<pre>";
  459. // print_r($batchList['matter_type']);
  460. // echo "<pre>";
  461. // echo "<pre>";
  462. // print_r($batchList['supplier_code']);
  463. // echo "<pre>";
  464. // echo "<pre>";
  465. // print_r('9');
  466. // echo "<pre>";
  467. // echo "<pre>";
  468. // print_r($batchList['matter_no']);
  469. // echo "<pre>";
  470. // echo "<pre>";
  471. // print_r($batchList['manufacture_date']);
  472. // echo "<pre>";
  473. // echo "<pre>";
  474. // print_r($batchList['print_date']);
  475. // echo "<pre>";die;
  476. $fixed_code = '';
  477. $fixed_code.=$this->intTochar($batchList['matter_type'],2);//2位 辅料种类编码
  478. // $fixed_code .= substr($this->intTochar($batchList['supplier_code'], 12), 1);//12位 供应商编码 // 去掉第一位0
  479. $fixed_code .= $this->intTochar($batchList['supplier_code'], 21);//21位 供应商编码
  480. $fixed_code .= '9'; // 补0一位
  481. $fixed_code.=$this->intTochar($batchList['matter_no'],10);//10位 辅料编码
  482. $fixed_code.=$batchList['manufacture_date'];//6位 生产日期
  483. $print_code=$batchList['print_date'];//6位 打码日期
  484. $small_liushui = [
  485. 'onlycode' => 'AB92'.$fixed_code.$print_code,
  486. 'last_num' => 0,
  487. 'user_id' => $userinfo['company'],
  488. 'stype' => 2,
  489. 'dateTime' => time(),
  490. ];
  491. $whereSmall = [
  492. 'onlycode' => $small_liushui['onlycode'],
  493. 'user_id' => $userinfo['company'],
  494. ];
  495. if ($liushui->name($userinfo['company'].'_'.'qcode_liushui')->where($whereSmall)->find()){
  496. //小件二维码存在,更新小件二维码最后流水号
  497. $lastNum = $liushui->name($userinfo['company'].'_'.'qcode_liushui')->where($whereSmall)->find();
  498. }else{
  499. //小件二维码不存在,新增记录
  500. $liushui->save($small_liushui);
  501. $lastNum['last_num'] = 0;
  502. }
  503. //循环插入大件二维码数据
  504. for ($i=0;$i<$batchList['larger_num'];$i++){
  505. $large = new QcodeLarge();
  506. $l_flow = $this->intTochar($batchList['l_flow']+$i,6);
  507. $l_weight = $this->intTochar($batchList['l_weight']*100,6);
  508. $l_reservation = $this->intTochar($batchList['bach_num'],10);
  509. $l_reservation = $l_reservation.'0000000000';
  510. $remainder = $batchList['small_num'] - $batchList['tray_num'] * $i;//最后一托盘小件数量
  511. if ($remainder < $batchList['tray_num']){
  512. $small_n = $this->intTochar($remainder,3);//3位小件数量,不足补0
  513. }else{
  514. $small_n = $this->intTochar($batchList['tray_num'],3);
  515. }
  516. $l_num = 0;
  517. if ($data['danwei'] == 1){
  518. //以箱为单位时
  519. $l_num = $small_n * $batchList['box_num'];
  520. }
  521. //大件二维码数据
  522. $code_data = $this->CodeData('AB92',$fixed_code,$small_n,$print_code,$l_flow,$l_weight,'2',$l_reservation);
  523. //大码数据信息
  524. $l_data = [
  525. 'bach_id' => $last_id,
  526. 'code' => $code_data['code'],
  527. 'code_cp1' => $code_data['code_cp1'],
  528. 'code_cp2' => $code_data['code_cp2'],
  529. 'print_date' => $print_code,
  530. 'create_time' => time(),
  531. 'p_nums' => 0,
  532. 'userid' =>$userinfo['id'],
  533. 'l_weight' =>$batchList['l_weight']*100,
  534. 'l_num' =>$l_num,
  535. 'l_status' => 0,
  536. 'l_print' => 0
  537. ];
  538. $l_res = $large->save($l_data);
  539. if ($l_res === 0){
  540. $this->error('大件码插入失败');
  541. }
  542. $large_id = $large->getLastInsID();
  543. if ($large_id){
  544. // //小件码循环插入
  545. for ($j=0;$j<$tray_num and ($j+$i*$tray_num)<$batchList['small_num'];$j++){
  546. $small = new QcodeSmall();
  547. $s_flow = $this->intTochar($batchList['s_flow']+$j+$i*$tray_num+$lastNum['last_num'],6);//小件码序号从1开始
  548. $s_weight = $this->intTochar($batchList['s_weight'],6);
  549. $small_sign = '000';
  550. $s_reservation = $this->intTochar($batchList['bach_num'],10);
  551. $s_reservation = $s_reservation . '0000000000';
  552. //生成小件码
  553. $small_code_data = $this->CodeData('AB92',$fixed_code,$small_sign,$print_code,$s_flow,$s_weight,'1',$s_reservation);
  554. //小码数据
  555. $s_data = [
  556. 'large_id'=>$large_id,
  557. 'bach_id'=>$last_id,
  558. 'code'=>$small_code_data['code'],
  559. 'code_cp1'=>$small_code_data['code_cp1'],
  560. 'code_cp2'=>$small_code_data['code_cp2'],
  561. 'l_flow'=>$j+1,
  562. 'print_date'=>$print_code,
  563. 'create_time'=>time(),
  564. 'p_nums'=>0,
  565. 'userid'=>$userinfo['id'],//小码绑定用户id
  566. 's_weight'=>$batchList['s_weight'],//单个小件重量
  567. 'status' => 0,
  568. ];
  569. $s_res = $small->save($s_data);
  570. if ($s_res === 0){
  571. $this->error('小件码插入失败');
  572. }
  573. }
  574. }else{
  575. $this->error('添加失败');
  576. }
  577. }
  578. }
  579. $liushui_res = $liushui->name($userinfo['id'].'_'.'qcode_liushui')->where($whereSmall)->update(['last_num'=>$batchList['small_num']]);
  580. if ($liushui_res === false){
  581. $this->error('添加失败');
  582. }
  583. $this->success('成功');
  584. }
  585. /**
  586. * 编码补位
  587. * @param $num
  588. * @param $len
  589. * @return string
  590. */
  591. function intTochar($num=0,$len){
  592. //规定的不足的时候自动补足零
  593. $code=(string)$num;
  594. $buwei='';
  595. if(strlen($code)<$len){
  596. for($i=strlen($code);$i<$len;$i++){
  597. $buwei.='0';
  598. }
  599. }
  600. return $buwei.$code;
  601. }
  602. /**
  603. * 二维码编码生成
  604. * @param $sign
  605. * @param $fixed_code
  606. * @param $small_num
  607. * @param $print_date
  608. * @param $flow
  609. * @param $weight
  610. * @param $large_sign
  611. * @param $reservation
  612. * @return array
  613. */
  614. function CodeData($sign,$fixed_code,$small_num,$print_date,$flow,$weight,$large_sign,$reservation){
  615. $code=$sign;//4 位固定标志位
  616. $code.=$fixed_code; // 固定字符串
  617. $code.=$small_num;//3位 小件数量
  618. $code.=$print_date;//6 位 日期
  619. $code.=$flow;//6位打印流水号
  620. $code.=$weight;//6位辅料重量
  621. $code.=$large_sign;//大小件标示位
  622. $code.=$reservation;//20 位 预留号
  623. //大码数据信息
  624. $data=[
  625. 'code'=>str_replace(" ","",$code),
  626. 'code_cp1'=>$print_date.$flow,
  627. 'code_cp2'=>$weight.$reservation,//20位补充
  628. 'print_date'=>time(),
  629. 'p_nums'=>0,
  630. ];
  631. return $data;
  632. }
  633. }