m0_70156489 1 month ago
parent
commit
fae709f392
2 changed files with 35 additions and 32 deletions
  1. 6 9
      application/admin/controller/QcodeAdd.php
  2. 29 23
      application/admin/controller/QcodeBach.php

+ 6 - 9
application/admin/controller/QcodeAdd.php

@@ -203,7 +203,7 @@ class QcodeAdd extends Backend
         ];
 //echo "<pre>";
 //print_r($batchList);
-//echo "<pre>";
+//echo "<pre>";die;
         $res = $bach->save($batchList);
         if ($res === 0){
             $this->error('添加失败');
@@ -370,15 +370,12 @@ class QcodeAdd extends Backend
      * @return string
      */
     function intTochar($num=0,$len){
-        //规定的不足的时候自动补足零
-        $code=(string)$num;
-        $buwei='';
-        if(strlen($code)<$len){
-            for($i=strlen($code);$i<$len;$i++){
-                $buwei.='0';
-            }
+        //规定的不足的时候自动补足零,超出时截断,避免二维码字段错位
+        $code = (string)$num;
+        if (strlen($code) > $len) {
+            $code = substr($code, -$len);
         }
-        return $buwei.$code;
+        return str_pad($code, $len, '0', STR_PAD_LEFT);
     }
 
     /**

+ 29 - 23
application/admin/controller/QcodeBach.php

@@ -754,9 +754,7 @@ class QcodeBach extends Backend
                             'l_flow' => ltrim(substr($code, 53, 6), '0'),
                             'qrcode' => $code,
                             'pCode' => $this->qrcode($code),
-                            'batch' => (substr($code, 76, 10) == '0000000000') ?
-                                ltrim(substr($code, 66, 10), '0') :
-                                ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0')
+                            'batch' => $this->getPrintBatch($bachArr),
                         ];
 
                         // 数量处理
@@ -829,9 +827,7 @@ class QcodeBach extends Backend
                     'l_flow' => ltrim(substr($code, 53, 6), '0'),
                     'qrcode' => $code,
                     'pCode' => $this->qrcode($code),
-                    'batch' => (substr($code, 76, 10) == '0000000000') ?
-                        ltrim(substr($code, 66, 10), '0') :
-                        ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0')
+                    'batch' => $this->getPrintBatch($bachArr),
                 ];
 
                 if ($rowData['l_num'] === null) {
@@ -938,9 +934,7 @@ class QcodeBach extends Backend
                                 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
                                 'qrcode' => $code,
 //                          'pCode' => $this->qrcode($code), // 如需启用二维码图像,取消注释
-                                'batch' => (substr($code, 76, 10) == '0000000000') ?
-                                    ltrim(substr($code, 66, 10), '0') :
-                                    ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
+                                'batch' => $this->getPrintBatch($bachArr),
                             ];
                             $key++;
                         }
@@ -964,9 +958,10 @@ class QcodeBach extends Backend
 
             $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
             $bach = $this->model->name($tableBach)->where('_id', json_decode($large, true)['bach_id'])->find();
+            $bachArr = json_decode($bach, true);
 
-            $company_name = json_decode($bach, true)['supplier_name'];
-            $product_name = json_decode($bach, true)['matter_name'];
+            $company_name = $bachArr['supplier_name'];
+            $product_name = $bachArr['matter_name'];
 
             foreach ($ids as $value) {
                 $large = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
@@ -987,9 +982,7 @@ class QcodeBach extends Backend
                         'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
                         'qrcode' => $code,
 //                  'pCode' => $this->qrcode($code),
-                        'batch' => (substr($code, 76, 10) == '0000000000') ?
-                            ltrim(substr($code, 66, 10), '0') :
-                            ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
+                        'batch' => $this->getPrintBatch($bachArr),
                     ];
                     $key++;
                 }
@@ -1062,9 +1055,7 @@ class QcodeBach extends Backend
                         'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
                         'qrcode' => $code,
                         // 'pCode' => $this->qrcode($code),
-                        'batch' => (substr($code, 76, 10) == '0000000000') ?
-                            ltrim(substr($code, 66, 10), '0') :
-                            ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
+                        'batch' => $this->getPrintBatch($bach),
                     ];
 
                     break; // 查到就退出循环
@@ -1099,9 +1090,7 @@ class QcodeBach extends Backend
                 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
                 'qrcode' => $code,
                 // 'pCode' => $this->qrcode($code),
-                'batch' => (substr($code, 76, 10) == '0000000000') ?
-                    ltrim(substr($code, 66, 10), '0') :
-                    ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
+                'batch' => $this->getPrintBatch($bach),
             ];
         }
 
@@ -1199,9 +1188,13 @@ class QcodeBach extends Backend
                     $code = json_decode($large,true)['code'];
 
                     if($code){
-                        $code1 = substr($code,0,59);
-                        $code2 = substr($code,65,85);
-                        $data['code'] = $code1.str_pad(intval($req['row']['numn'])*100,6, '0', STR_PAD_LEFT).$code2;
+                        $weightVal = intval(round($req['row']['numn'] * 100));
+                        if ($weightVal > 999999) {
+                            $this->error('重量超出编码范围');
+                        }
+                        $code1 = substr($code, 0, 59);
+                        $code2 = substr($code, 65);
+                        $data['code'] = $code1 . str_pad((string)$weightVal, 6, '0', STR_PAD_LEFT) . $code2;
                         //修改code和大件重量
                         $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$val)->update($data);
                     }
@@ -1589,4 +1582,17 @@ class QcodeBach extends Backend
 //        echo "成功插入 " . count($differences) . " 条记录";
 //    }
 
+    /**
+     * 打印标签批次号(优先批次表,避免code字段错位解析)
+     * @param array $bachArr
+     * @return string
+     */
+    private function getPrintBatch($bachArr)
+    {
+        if (!is_array($bachArr) || !isset($bachArr['bach_num']) || $bachArr['bach_num'] === '') {
+            return '';
+        }
+        return (string)$bachArr['bach_num'];
+    }
+
 }