QcodeAdd.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. * @return string|\think\response\Json
  20. * @throws \think\Exception
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. * @throws \think\exception\DbException
  24. */
  25. public function index()
  26. {
  27. $company = new QcodeCompany();
  28. $product = new QcodeProduct();
  29. $userinfo = Session::get('admin');
  30. $data = [
  31. 'nickname' => $userinfo['company_name'],
  32. 'postcode' => $userinfo['postcode'],
  33. 'mobile' => $userinfo['mobile'],
  34. 'printer_code' => $userinfo['printer_code'],
  35. 'company_address' => $userinfo['company_address']
  36. ];
  37. //创建公司唯一id,进行创建公司对用表名
  38. $user_company_value = Db::name('admin')->where('id', $userinfo['id'])->value('company');
  39. if (empty($user_company_value)) {
  40. $max_company = Db::name('admin')->max('company');
  41. $new_company_value = $max_company ? ($max_company + 1) : 1;
  42. Db::name('admin')->where('id', $userinfo['id'])->update(['company' => $new_company_value]);
  43. } else {
  44. $new_company_value = $user_company_value;
  45. }
  46. $tableNames = [
  47. $new_company_value . '_qcode_bach',
  48. $new_company_value . '_qcode_company',
  49. $new_company_value . '_qcode_large',
  50. $new_company_value . '_qcode_liushui',
  51. $new_company_value . '_qcode_small',
  52. $new_company_value . '_reset_flow',
  53. ];
  54. foreach ($tableNames as $tableName) {
  55. try {
  56. Db::connect('mongodb')->name($tableName)->insert(['init' => 1]);
  57. // 删除测试数据(可选,如果不想保留这个测试数据)
  58. Db::connect('mongodb')->name($tableName)->where('init', 1)->delete();
  59. } catch (\Exception $e) {
  60. echo "创建集合 {$tableName} 失败:" . $e->getMessage();
  61. }
  62. }
  63. // $product_id = $company->name((int)$userinfo['company'].'_'.'qcode_company')->where('delete_time','')->column('product_id');
  64. // echo "<pre>";
  65. // print_r($product_id);
  66. // echo "<pre>";
  67. // $product_name = [];
  68. // foreach ($product_id as $v){
  69. // $list = $product->where('_id',$v)->where('delete_time','')->find();
  70. // $product_name[$list['product_code']] = $list['product_name'];
  71. // }
  72. // echo "<pre>";
  73. // print_r($product_name);
  74. // echo "<pre>";
  75. // $this->view->assign('product', $product_name);
  76. // 1. 获取所有产品的 ID
  77. $product_id = $company
  78. ->name((int)$userinfo['company'] . '_' . 'qcode_company')
  79. ->where('delete_time', '')
  80. ->column('product_id');
  81. // 2. 查找产品信息:构建 product_code => 全部信息数组
  82. $product_info = [];
  83. foreach ($product_id as $v) {
  84. $list = $product
  85. ->where('_id', $v)
  86. ->where('delete_time', '')
  87. ->find();
  88. if ($list) {
  89. // 记录完整产品信息
  90. $product_info[$list['product_code']] = [
  91. 'jjcp_cpmc' => $list['product_name'], // 产品名称
  92. 'jjcp_cpdh' => $list['product_code'], // 产品代码
  93. ];
  94. }
  95. }
  96. // 3. 从 Mongo 查询 jjcp_cpmc 名称
  97. $mongo = \think\Db::connect('mongodb');
  98. $mongo_products = $mongo
  99. ->name('finished_products')
  100. ->field('jjcp_cpmc, jjcp_cpdh, order_ddbh, jjcp_gdbh, jjcp_sl')
  101. ->select();
  102. // 4. 比对名称,匹配成功的组装完整 vo 结构
  103. $matched_products = [];
  104. foreach ($product_info as $code => $item) {
  105. foreach ($mongo_products as $mp) {
  106. if (trim($item['jjcp_cpmc']) === trim($mp['jjcp_cpmc'])) {
  107. $matched_products[] = [
  108. 'jjcp_cpmc' => $item['jjcp_cpmc'],
  109. 'jjcp_cpdh' => $item['jjcp_cpdh'],
  110. 'order_ddbh' => $mp['order_ddbh'], // 从 MongoDB 查找
  111. 'jjcp_gdbh' => $mp['jjcp_gdbh'], // 从 MongoDB 查找
  112. 'jjcp_sl' => $mp['jjcp_sl'], // 从 MongoDB 查找
  113. 'product_code' => $code,
  114. ];
  115. }
  116. }
  117. }
  118. $this->view->assign('product', $matched_products);
  119. $this->view->assign('row',$data);
  120. return $this->view->fetch();
  121. }
  122. /**
  123. * 获取产品信息
  124. * @return \think\response\Json
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\ModelNotFoundException
  127. * @throws \think\exception\DbException
  128. */
  129. public function product(){
  130. $QcodeProduct = new QcodeProduct();
  131. $ResetFlow = new ResetFlow();
  132. $userinfo = Session::get('admin');
  133. if ($this->request->isAjax() === false){
  134. $this->error('请求错误');
  135. }
  136. $product_code = input('product_code');
  137. if (empty($product_code)){
  138. $this->error('参数错误');
  139. }
  140. $product = $QcodeProduct->where('product_code',$product_code)->find();
  141. if (empty($product)){
  142. $this->error('未找到该产品信息');
  143. }
  144. $flow = $ResetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',substr(json_encode($product['_id']),9,-2))->find();
  145. $row = [
  146. 'temple' => $product['temple'],
  147. 'flow' => isset($flow['l_flow'])?(int)$flow['l_flow']+1:'',
  148. 'bach' => isset($flow['bach_num'])?(int)$flow['bach_num']+1:'',
  149. ];
  150. if (!empty($flow)){
  151. $row['flow'] = $flow['l_flow']+1;
  152. $row['bach'] = $flow['bach_num']+1;
  153. }
  154. return json(['code'=>1,'data'=>$row]);
  155. }
  156. /**
  157. * 增加新批次
  158. * @return void
  159. * @throws \think\db\exception\DataNotFoundException
  160. * @throws \think\db\exception\ModelNotFoundException
  161. * @throws \think\exception\DbException
  162. */
  163. public function add()
  164. {
  165. $product = new QcodeProduct();
  166. $bach = new QcodeBach();
  167. $large = new QcodeLarge();
  168. $small = new QcodeSmall();
  169. $liushui = new QcodeLiushui();
  170. $resetFlow = new ResetFlow();
  171. $QcodeCompany = new QcodeCompany();
  172. if ($this->request->isAjax() === false){
  173. $this->error('请求错误');
  174. }
  175. $rows = input('row');
  176. if (empty($rows)){
  177. $this->error('参数错误');
  178. }
  179. $rows = json_decode($rows);
  180. $data = [];
  181. foreach ($rows as $value){
  182. foreach ($value as $k=>$v){
  183. $data[$k] = $v;
  184. }
  185. }
  186. if ($data['danwei'] == 1){
  187. $num = $data['number'];
  188. $tray_num = $data['tray_num'];
  189. $tray_num1 = $data['tray_num'];
  190. $box_number = $data['box_number'];
  191. //$small_num = (int)ceil((int)总数量(张/个)/(int)张数);
  192. $small_num = (int)ceil((int)$data['number']/(int)$data['box_number']);
  193. $large_num = (int)ceil($small_num/$tray_num);
  194. }else{
  195. $num = 0;
  196. $tray_num = $data['volume_num'];
  197. $small_num = $data['small_num'];
  198. $large_num = (int)ceil($small_num/$tray_num);
  199. $box_number = 1;
  200. $tray_num1 = 1;
  201. }
  202. $userinfo = Session::get('admin');
  203. $productList = $product->where('product_code',$data['product_code'])->find();
  204. $arr = [
  205. 'product_id' => $productList['_id'],
  206. 'create_time' => time(),
  207. 'delect_time' => '',
  208. 'sync_flag' => 0,
  209. ];
  210. $productres = $QcodeCompany->save($arr);
  211. if ($productres === 0){
  212. $this->error('添加失败');
  213. }
  214. if (empty($productList)){
  215. $this->error('未找到该产品数据');
  216. }
  217. $batchList = [
  218. 'supplier_name' => $data['company_name'],
  219. 'supplier_code' => $userinfo['printer_code'],
  220. 'supplier_id' => $userinfo['id'],
  221. 'matter_name' => $productList['product_name'],
  222. 'matter_no' => $productList['product_code'],//生产批次号
  223. 'order_ddbh' => $data['order_ddbh'],//销售订单号
  224. 'matter_id' => substr(json_encode($productList['_id']),9,-2),
  225. 'matter_type' => $productList['temple'],
  226. 'manufacture_date' => (int)date('ymd',strtotime($data['manufacture_date'])),
  227. 'print_date' => (int)date('ymd',strtotime($data['print_date'])),
  228. 'danwei' => $data['danwei'],//单位
  229. 'num' => $num,//总数量(张/个)
  230. 'tray_num' => $tray_num1,//每层箱数
  231. 'box_num' => $box_number,//每托层数
  232. 'total_boxes' => $data['total_boxes'],//每托盘箱数
  233. 'small_num' => $small_num,//每托盘箱数
  234. 'pallet_height' => $data['pallet_height'],//每托高度
  235. 'pallet_length' => $data['pallet_length'],//托盘规格
  236. 'pallet_width' => $data['pallet_width'],//托盘规格
  237. 'larger_num' => $large_num,//大件(总托数)
  238. 'l_reservation' => '',
  239. 'l_flow' => $data['big_liushui'],
  240. 'l_weight' => $data['big_weight'],
  241. 's_flow' => $data['small_start_liushui'],
  242. 's_weight' => $data['small_weight'],
  243. 's_reservation' => '',
  244. 'bach_status' => 0,
  245. 'userid' => $userinfo['id'],
  246. 'bach_num' => $data['batch'],
  247. 'large_endnum' => $data['big_liushui'] + $data['box_num'] -1,
  248. 'create_time' => time(),//新增时间
  249. ];
  250. $res = $bach->save($batchList);
  251. if ($res === 0){
  252. $this->error('添加失败');
  253. }
  254. $flow = [
  255. 'l_flow' => (int)$batchList['large_endnum'],
  256. 'bach_num' => $batchList['bach_num'],
  257. ];
  258. if ($resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_id'])->find()){
  259. $resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_id'])->update($flow);
  260. }else{
  261. $flow['product_id'] = $batchList['matter_id'];
  262. $resetFlow->save($flow);
  263. }
  264. $last_id = $bach->getLastInsID();
  265. if ($last_id){
  266. //插入大小二维码数据
  267. //二维码数据不变区域
  268. // echo "<pre>";
  269. // print_r($batchList['matter_type']);
  270. // echo "<pre>";
  271. // echo "<pre>";
  272. // print_r($batchList['supplier_code']);
  273. // echo "<pre>";
  274. // echo "<pre>";
  275. // print_r('9');
  276. // echo "<pre>";
  277. // echo "<pre>";
  278. // print_r($batchList['matter_no']);
  279. // echo "<pre>";
  280. // echo "<pre>";
  281. // print_r($batchList['manufacture_date']);
  282. // echo "<pre>";
  283. // echo "<pre>";
  284. // print_r($batchList['print_date']);
  285. // echo "<pre>";
  286. $fixed_code = '';
  287. $fixed_code.=$this->intTochar($batchList['matter_type'],2);//2位 辅料种类编码
  288. // $fixed_code .= substr($this->intTochar($batchList['supplier_code'], 12), 1);//12位 供应商编码 // 去掉第一位0
  289. $fixed_code .= $this->intTochar($batchList['supplier_code'], 21);//21位 供应商编码
  290. $fixed_code .= '9'; // 补0一位
  291. $fixed_code.=$this->intTochar($batchList['matter_no'],10);//10位 辅料编码
  292. $fixed_code.=$batchList['manufacture_date'];//6位 生产日期
  293. $print_code=$batchList['print_date'];//6位 打码日期
  294. // echo "<pre>";
  295. // print_r($fixed_code);
  296. // echo "<pre>";
  297. // echo "<pre>";
  298. // print_r($print_code);
  299. // echo "<pre>";
  300. $small_liushui = [
  301. 'onlycode' => 'AB92'.$fixed_code.$print_code,
  302. 'last_num' => 0,
  303. 'user_id' => $userinfo['company'],
  304. 'stype' => 2,
  305. 'dateTime' => time(),
  306. ];
  307. $whereSmall = [
  308. 'onlycode' => $small_liushui['onlycode'],
  309. 'user_id' => $userinfo['company'],
  310. ];
  311. if ($liushui->name($userinfo['company'].'_'.'qcode_liushui')->where($whereSmall)->find()){
  312. //小件二维码存在,更新小件二维码最后流水号
  313. $lastNum = $liushui->name($userinfo['company'].'_'.'qcode_liushui')->where($whereSmall)->find();
  314. }else{
  315. //小件二维码不存在,新增记录
  316. $liushui->save($small_liushui);
  317. $lastNum['last_num'] = 0;
  318. }
  319. //循环插入大件二维码数据
  320. for ($i=0;$i<$batchList['larger_num'];$i++){
  321. $large = new QcodeLarge();
  322. $l_flow = $this->intTochar($batchList['l_flow']+$i,6);
  323. $l_weight = $this->intTochar($batchList['l_weight']*100,6);
  324. $l_reservation = $this->intTochar($batchList['bach_num'],10);
  325. $l_reservation = $l_reservation.'0000000000';
  326. $remainder = $batchList['small_num'] - $batchList['tray_num'] * $i;//最后一托盘小件数量
  327. if ($remainder < $batchList['tray_num']){
  328. $small_n = $this->intTochar($remainder,3);//3位小件数量,不足补0
  329. }else{
  330. $small_n = $this->intTochar($batchList['tray_num'],3);
  331. }
  332. $l_num = 0;
  333. if ($data['danwei'] == 1){
  334. //以箱为单位时
  335. $l_num = $small_n * $batchList['box_num'];
  336. }
  337. //大件二维码数据
  338. $code_data = $this->CodeData('AB92',$fixed_code,$small_n,$print_code,$l_flow,$l_weight,'2',$l_reservation);
  339. //大码数据信息
  340. $l_data = [
  341. 'bach_id' => $last_id,
  342. 'code' => $code_data['code'],
  343. 'code_cp1' => $code_data['code_cp1'],
  344. 'code_cp2' => $code_data['code_cp2'],
  345. 'print_date' => $print_code,
  346. 'create_time' => time(),
  347. 'p_nums' => 0,
  348. 'userid' =>$userinfo['id'],
  349. 'l_weight' =>$batchList['l_weight']*100,
  350. 'l_num' =>$l_num,
  351. 'l_status' => 0,
  352. 'l_print' => 0
  353. ];
  354. $l_res = $large->save($l_data);
  355. if ($l_res === 0){
  356. $this->error('大件码插入失败');
  357. }
  358. $large_id = $large->getLastInsID();
  359. if ($large_id){
  360. // //小件码循环插入
  361. for ($j=0;$j<$tray_num and ($j+$i*$tray_num)<$batchList['small_num'];$j++){
  362. $small = new QcodeSmall();
  363. $s_flow = $this->intTochar($batchList['s_flow']+$j+$i*$tray_num+$lastNum['last_num'],6);//小件码序号从1开始
  364. $s_weight = $this->intTochar($batchList['s_weight'],6);
  365. $small_sign = '000';
  366. $s_reservation = $this->intTochar($batchList['bach_num'],10);
  367. $s_reservation = $s_reservation . '0000000000';
  368. //生成小件码
  369. $small_code_data = $this->CodeData('AB92',$fixed_code,$small_sign,$print_code,$s_flow,$s_weight,'1',$s_reservation);
  370. //小码数据
  371. $s_data = [
  372. 'large_id'=>$large_id,
  373. 'bach_id'=>$last_id,
  374. 'code'=>$small_code_data['code'],
  375. 'code_cp1'=>$small_code_data['code_cp1'],
  376. 'code_cp2'=>$small_code_data['code_cp2'],
  377. 'l_flow'=>$j+1,
  378. 'print_date'=>$print_code,
  379. 'create_time'=>time(),
  380. 'p_nums'=>0,
  381. 'userid'=>$userinfo['id'],//小码绑定用户id
  382. 's_weight'=>$batchList['s_weight'],//单个小件重量
  383. 'status' => 0,
  384. ];
  385. $s_res = $small->save($s_data);
  386. if ($s_res === 0){
  387. $this->error('小件码插入失败');
  388. }
  389. }
  390. }else{
  391. $this->error('添加失败');
  392. }
  393. }
  394. }
  395. $liushui_res = $liushui->name($userinfo['id'].'_'.'qcode_liushui')->where($whereSmall)->update(['last_num'=>$batchList['small_num']]);
  396. if ($liushui_res === false){
  397. $this->error('添加失败');
  398. }
  399. $this->success('成功');
  400. }
  401. /**
  402. * 编码补位
  403. * @param $num
  404. * @param $len
  405. * @return string
  406. */
  407. function intTochar($num=0,$len){
  408. //规定的不足的时候自动补足零
  409. $code=(string)$num;
  410. $buwei='';
  411. if(strlen($code)<$len){
  412. for($i=strlen($code);$i<$len;$i++){
  413. $buwei.='0';
  414. }
  415. }
  416. return $buwei.$code;
  417. }
  418. /**
  419. * 二维码编码生成
  420. * @param $sign
  421. * @param $fixed_code
  422. * @param $small_num
  423. * @param $print_date
  424. * @param $flow
  425. * @param $weight
  426. * @param $large_sign
  427. * @param $reservation
  428. * @return array
  429. */
  430. function CodeData($sign,$fixed_code,$small_num,$print_date,$flow,$weight,$large_sign,$reservation){
  431. $code=$sign;//4 位固定标志位
  432. $code.=$fixed_code; // 固定字符串
  433. $code.=$small_num;//3位 小件数量
  434. $code.=$print_date;//6 位 日期
  435. $code.=$flow;//6位打印流水号
  436. $code.=$weight;//6位辅料重量
  437. $code.=$large_sign;//大小件标示位
  438. $code.=$reservation;//20 位 预留号
  439. //大码数据信息
  440. $data=[
  441. 'code'=>str_replace(" ","",$code),
  442. 'code_cp1'=>$print_date.$flow,
  443. 'code_cp2'=>$weight.$reservation,//20位补充
  444. 'print_date'=>time(),
  445. 'p_nums'=>0,
  446. ];
  447. return $data;
  448. }
  449. }