Browse Source

first commit

liuhairui 5 months ago
parent
commit
783a073656

+ 165 - 116
application/admin/controller/Deliver.php

@@ -32,110 +32,9 @@ class Deliver extends Backend
     /**
      * 大件发货
      */
-    public function lager()
-    {
-
-        $company = new QcodeCompany();
-        $product = new QcodeProduct();
-        $large = new QcodeLarge();
-        $bach = new QcodeBach();
-        $small = new QcodeSmall();
-
-        $this->request->filter(['strip_tags', 'trim']);
-
-        if (false === $this->request->isAjax()) {
-            return $this->view->fetch();
-        }
-
-        if ($this->request->request('keyField')) {
-            return $this->selectpage();
-        }
-
-        $userInfo = Session::get('admin');
-        $where = ['delete_time'=> ''];
-        $filter = input('filter');
-        $filter = json_decode($filter, true);
-
-        $whereList = ['delete_time'=>''];
-
-        if (isset($filter['matter_name'])){
-            $whereList['matter_no'] = $filter['matter_name'];
-        }
-        if (isset($filter['bach'])){
-            $whereList['bach_num'] = $filter['bach'];
-        }
-        if (isset($filter['manufacture_date'])){
-            $begin = substr($filter['manufacture_date'], 0, 19);
-            $end = substr($filter['manufacture_date'], 22);
-            $begin = (int)date('ymd', strtotime($begin));
-            $end = (int)date('ymd', strtotime($end));
-            $whereList['manufacture_date'] = ['between', [$begin, $end]];
-        }
-
-        // 查出相关 bach_id
-        $bach_list = $bach->name($userInfo['company'].'_qcode_bach')->where($whereList)->column('_id');
-        $bach_id = [];
-        foreach ($bach_list as $v){
-            $id = substr(json_encode($v), 9, -2);
-            $bach_id[] = $id;
-        }
-
-//        // 获取分页参数
-        $offset = input('offset', 0);  // 当前页数
-        $limit = input('limit', 10);   // 每页多少条
-
-        // 总数量
-        $total = $large->name($userInfo['company'].'_qcode_large')
-            ->where('l_status', 0)
-            ->where($where)
-            ->whereIn('bach_id', $bach_id)
-            ->count();
-
-        // 当前页数据
-        $large_list = $large->name($userInfo['company'].'_qcode_large')
-            ->where($where)
-            ->where('l_status', 0)
-            ->whereIn('bach_id', $bach_id)
-            ->limit($limit)
-            ->skip($offset)
-            ->select();
-
-        // 数据处理
-        $list = [];
-        foreach ($large_list as $k => $v) {
-            $bach_detail = $bach->name($userInfo['company'].'_qcode_bach')->where('_id', $v['bach_id'])->find();
-
-            $list[$k]['id'] = substr(json_encode($v['_id']), 9, -2);
-            $list[$k]['l_flow'] = $bach_detail['l_flow'];
-            $list[$k]['bach'] = $bach_detail['bach_num'];
-            $list[$k]['num'] = $bach_detail['num'];
-            $list[$k]['matter_name'] = $bach_detail['matter_name'];
-            $list[$k]['total_boxes'] = $bach_detail['total_boxes'];
-            $list[$k]['tray_num'] = $bach_detail['tray_num'];
-            $list[$k]['box_num'] = $bach_detail['box_num'];
-            $list[$k]['pallet_height'] = $bach_detail['pallet_height'];
-            $list[$k]['pallet_length'] = $bach_detail['pallet_length'];
-            $list[$k]['pallet_width'] = $bach_detail['pallet_width'];
-            $list[$k]['larger_num'] = $bach_detail['larger_num'];
-            $list[$k]['manufacture_date'] = date('Y-m-d', strtotime('20'.$bach_detail['manufacture_date']));
-            $list[$k]['code'] = $v['code'];
-            $list[$k]['l_flow'] = ltrim(substr($v['code'], 53, 6), '0');
-
-            $small_num = $small->name($userInfo['company'].'_qcode_small')
-                ->where('large_id', $list[$k]['id'])
-                ->count();
-            $list[$k]['small_num'] = $small_num;
-        }
-
-        // 总数与分页
-//        $list = array_slice($list, $offset, $limit);
-
-        // 返回分页数据(关键!不要再做分组排序!)
-        return json(['total' => $total, 'rows' => $list]);
-    }
-
 //    public function lager()
 //    {
+//
 //        $company = new QcodeCompany();
 //        $product = new QcodeProduct();
 //        $large = new QcodeLarge();
@@ -156,6 +55,7 @@ class Deliver extends Backend
 //        $where = ['delete_time'=> ''];
 //        $filter = input('filter');
 //        $filter = json_decode($filter, true);
+//
 //        $whereList = ['delete_time'=>''];
 //
 //        if (isset($filter['matter_name'])){
@@ -180,10 +80,10 @@ class Deliver extends Backend
 //            $bach_id[] = $id;
 //        }
 //
-//        // 获取分页参数
-//        $offset = input('offset', 0);
-//        $limit = input('limit', 10);
-//        $skip = $offset;
+////        // 获取分页参数
+//        $offset = input('offset', 0);  // 当前页数
+//        $limit = input('limit', 10);   // 每页多少条
+//
 //        // 总数量
 //        $total = $large->name($userInfo['company'].'_qcode_large')
 //            ->where('l_status', 0)
@@ -193,24 +93,52 @@ class Deliver extends Backend
 //
 //        // 当前页数据
 //        $large_list = $large->name($userInfo['company'].'_qcode_large')
-//            ->order('l_flow')
 //            ->where($where)
 //            ->where('l_status', 0)
 //            ->whereIn('bach_id', $bach_id)
-////            ->skip($skip)
-////            ->limit($limit)
+//            ->limit($limit)
+//            ->skip($offset)
 //            ->select();
+//
 //        // 数据处理
 //        $list = [];
 //        foreach ($large_list as $k => $v) {
 //            $bach_detail = $bach->name($userInfo['company'].'_qcode_bach')->where('_id', $v['bach_id'])->find();
 //
+//            $larger_num = (int)$bach_detail['larger_num'];        // 总箱数
+//            $boxes_per_pallet = (int)$bach_detail['total_boxes']; // 每托盘箱数
+//            $small_num = (int)$bach_detail['small_num'];          // 每箱数量
+//
+//            // 计算总托盘数和最后一托的箱数
+//            $full_pallets = floor($larger_num / $boxes_per_pallet);
+//            $last_pallet_boxes = $larger_num % $boxes_per_pallet;
+//            $total_pallets = $full_pallets + ($last_pallet_boxes > 0 ? 1 : 0);
+//
+//            // 当前大件是第几托
+//            $current_flow = (int) ltrim(substr($v['code'], 53, 6), '0');
+//
+//            // 默认每托盘箱数
+//            $total_boxes = $boxes_per_pallet;
+//
+//            // 如果是最后一托且有余数,修正箱数
+//            if ($current_flow === $total_pallets && $last_pallet_boxes > 0) {
+//                $total_boxes = $last_pallet_boxes;
+//            }
+//
+//
 //            $list[$k]['id'] = substr(json_encode($v['_id']), 9, -2);
 //            $list[$k]['l_flow'] = $bach_detail['l_flow'];
 //            $list[$k]['bach'] = $bach_detail['bach_num'];
 //            $list[$k]['num'] = $bach_detail['num'];
+//            $list[$k]['small_num'] = $bach_detail['small_num'];
+//            $list[$k]['order_ddbh'] = $bach_detail['order_ddbh'];
+//
+//            $list[$k]['danwei'] = $bach_detail['danwei'];
+//
+//            $list[$k]['cpbm'] = $bach_detail['cpbm'];
 //            $list[$k]['matter_name'] = $bach_detail['matter_name'];
-//            $list[$k]['total_boxes'] = $bach_detail['total_boxes'];
+////            $list[$k]['total_boxes'] = $bach_detail['total_boxes'];
+//            $list[$k]['total_boxes'] = $total_boxes;
 //            $list[$k]['tray_num'] = $bach_detail['tray_num'];
 //            $list[$k]['box_num'] = $bach_detail['box_num'];
 //            $list[$k]['pallet_height'] = $bach_detail['pallet_height'];
@@ -221,15 +149,136 @@ class Deliver extends Backend
 //            $list[$k]['code'] = $v['code'];
 //            $list[$k]['l_flow'] = ltrim(substr($v['code'], 53, 6), '0');
 //
-//            $small_num = $small->name($userInfo['company'].'_qcode_small')
-//                ->where('large_id', $list[$k]['id'])
-//                ->count();
-//            $list[$k]['small_num'] = $small_num;
+////            $small_num = $small->name($userInfo['company'].'_qcode_small')
+////                ->where('large_id', $list[$k]['id'])
+////                ->count();
+////            $list[$k]['small_num'] = $small_num;
 //        }
-//
-//        // 返回分页数据(关键!不要再做分组排序!)
+//        // 总数与分页
+////        $list = array_slice($list, $offset, $limit);
 //        return json(['total' => $total, 'rows' => $list]);
 //    }
+    public function lager()
+    {
+        $company = new QcodeCompany();
+        $product = new QcodeProduct();
+        $large = new QcodeLarge();
+        $bach = new QcodeBach();
+        $small = new QcodeSmall();
+
+        $this->request->filter(['strip_tags', 'trim']);
+
+        if (false === $this->request->isAjax()) {
+            return $this->view->fetch();
+        }
+
+        if ($this->request->request('keyField')) {
+            return $this->selectpage();
+        }
+
+        $userInfo = Session::get('admin');
+        $where = ['delete_time' => ''];
+        $filter = input('filter');
+        $filter = json_decode($filter, true);
+
+        $whereList = ['delete_time' => ''];
+
+        if (isset($filter['matter_name'])) {
+            $whereList['matter_no'] = $filter['matter_name'];
+        }
+        if (isset($filter['bach'])) {
+            $whereList['bach_num'] = $filter['bach'];
+        }
+        if (isset($filter['manufacture_date'])) {
+            $begin = substr($filter['manufacture_date'], 0, 19);
+            $end = substr($filter['manufacture_date'], 22);
+            $begin = (int)date('ymd', strtotime($begin));
+            $end = (int)date('ymd', strtotime($end));
+            $whereList['manufacture_date'] = ['between', [$begin, $end]];
+        }
+
+        // 查出相关 bach_id
+        $bach_list = $bach->name($userInfo['company'].'_qcode_bach')->where($whereList)->column('_id');
+        $bach_id = [];
+        foreach ($bach_list as $v) {
+            $id = substr(json_encode($v), 9, -2);
+            $bach_id[] = $id;
+        }
+
+        // 获取分页参数
+        $offset = input('offset', 0);
+        $limit = input('limit', 10);
+
+        // 获取所有大件记录用于总量判断
+        $all_large_list = $large->name($userInfo['company'].'_qcode_large')
+            ->where($where)
+            ->where('l_status', 0)
+            ->whereIn('bach_id', $bach_id)
+            ->select();
+
+        // 统计总数量
+        $total = count($all_large_list);
+
+        // 分页
+        $large_list = array_slice($all_large_list, $offset, $limit);
+
+        $list = [];
+
+        foreach ($large_list as $k => $v) {
+            $bach_detail = $bach->name($userInfo['company'].'_qcode_bach')->where('_id', $v['bach_id'])->find();
+
+            $num = (int)$bach_detail['num'];                      // 总张数
+            $small_num = (int)$bach_detail['small_num'];          // 每箱张数
+            $boxes_per_pallet = (int)$bach_detail['total_boxes']; // 每托箱数
+
+            // 重新计算总箱数
+            $larger_num = (int)ceil($num / $small_num);
+
+            // 当前批次对应的总托数
+            $full_pallets = floor($larger_num / $boxes_per_pallet);
+            $last_pallet_boxes = $larger_num % $boxes_per_pallet;
+            $total_pallets = $full_pallets + ($last_pallet_boxes > 0 ? 1 : 0);
+
+            // 当前是第几托(code中解析)
+            $current_flow_str = substr($v['code'], 53, 6);
+            $current_flow = (int) ltrim($current_flow_str, '0');
+            $total_pallets = (int) $total_pallets;
+
+            // 是否是最后一托
+            $is_last = ($current_flow === $total_pallets) ? 1 : 0;
+
+            // 修正箱数
+            $total_boxes = $boxes_per_pallet;
+            if ($is_last && $last_pallet_boxes > 0) {
+                $total_boxes = $last_pallet_boxes;
+            }
+
+            $list[$k] = [
+                'id' => substr(json_encode($v['_id']), 9, -2),
+                'l_flow' => $current_flow,
+                'bach' => $bach_detail['bach_num'],
+                'num' => $num,
+                'small_num' => $small_num,
+                'order_ddbh' => $bach_detail['order_ddbh'],
+                'danwei' => $bach_detail['danwei'],
+                'cpbm' => $bach_detail['cpbm'],
+                'matter_name' => $bach_detail['matter_name'],
+                'total_boxes' => $total_boxes,
+                'tray_num' => $bach_detail['tray_num'],
+                'box_num' => $bach_detail['box_num'],
+                'pallet_height' => $bach_detail['pallet_height'],
+                'pallet_length' => $bach_detail['pallet_length'],
+                'pallet_width' => $bach_detail['pallet_width'],
+                'larger_num' => $larger_num,
+                'manufacture_date' => date('Y-m-d', strtotime('20' . $bach_detail['manufacture_date'])),
+                'code' => $v['code'],
+                'is_last' => $is_last,
+            ];
+        }
+
+        return json(['total' => $total, 'rows' => $list]);
+    }
+
     /**
      * 小件列表
      * @return \think\response\Json

+ 153 - 40
application/admin/controller/Finishedproduct.php

@@ -27,64 +27,175 @@ class Finishedproduct extends Backend
         $this->model = new \app\admin\model\Finishedproduct;
     }
 
+
+
     public function index()
     {
-        $search = input('');
+        $params = input('');
         $page = input('get.page', 1);
         $limit = input('get.limit', 10);
-        $mongo = \think\Db::connect('mongodb');
-
-        // 初始化查询条件
-        $where = [];
-
-        if (!empty($search['search'])) {
-            // 使用正则表达式来实现模糊匹配,'i' 表示忽略大小写
-            $where['jjcp_cpdh|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($search['search'], 'i');
+        $offset = ($page - 1) * $limit;
+
+        // 获取工单编号列表
+        $db3 = Db::connect(config('database.db3'));
+        $gdRows = $db3->query("SELECT DISTINCT `Gd_gdbh`, `Gd_客户代号`, `Gd_cpdh` FROM `工单_基本资料` WHERE `Gd_客户代号` = ?", ['J0031']);
+        $Gd_gdbhList = array_column($gdRows, 'Gd_gdbh');
+        $gdbhList = array_column($gdRows, 'Gd_cpdh');
+        if (empty($gdbhList)) {
+            $this->assign([
+                'data' => '',
+                'total' =>'',
+            ]);
         }
 
-        if($where){
-            $data = $mongo->name('finished_products')
-                ->limit($page, $limit)
-                ->where($where)
-                ->order('UniqId','desc')
-                ->where('jjcp_smb','末 板')
-                ->select();
-        }else{
-            $data = $mongo->name('finished_products')
-                ->limit($page, $limit)
-                ->where($where)
-                ->order('UniqId','desc')
-                ->where('jjcp_smb','末 板')
-                ->select();
+        // Mongo 查询
+        $mongo = \think\Db::connect('mongodb');
+        $where = ['jjcp_cpdh' => ['in', $gdbhList],'jjcp_gdbh' => ['in', $Gd_gdbhList], 'jjcp_smb' => '末 板'];
+        if (!empty($params['search'])) {
+            $where['成品编码|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($params['search'], 'i');
         }
 
-        $filtered = [];
+        // 1. 查询所有符合条件的成品数据(不分页)
+        $all_products = $mongo->name('finished_products')
+            ->where($where)
+            ->order('UniqId', 'desc')
+            ->select();
+
+        // 2. 按订单编号 + 工单编号分组聚合,并处理多个cpdh与成品编码
+        $grouped_products = [];
+        foreach ($all_products as $item) {
+
+            $originDateStr = $item['Sys_rq']; // 例如:'20/6/2025 16:01:43'
+            $dt = \DateTime::createFromFormat('d/m/Y H:i:s', $originDateStr);
+            $standardDate = $dt ? $dt->format('Y-m-d H:i:s') : null;
+
+            $group_key = $item['订单编号'] . '_' . $item['jjcp_gdbh'];
+
+            if (!isset($grouped_products[$group_key])) {
+                $grouped_products[$group_key] = [
+                    'order_ddbh' => $item['订单编号'],
+                    'gdbh' => $item['jjcp_gdbh'],
+                    'cpbm' => $item['成品编码'],
+                    'cpmc' => $item['成品名称'],
+                    'Sys_rq' => $standardDate,
+                    'sl' => 0, // 库存总数
+                    'cpdh_list' => [],
+                    'cpbm_list' => []
+                ];
+            }
+            $grouped_products[$group_key]['sl'] += (int)$item['jjcp_sl']; // 累计库存数量
+
+            // 收集不同的cpdh和cpbm(成品编码)
+            if (!in_array($item['jjcp_cpdh'], $grouped_products[$group_key]['cpdh_list'])) {
+                $grouped_products[$group_key]['cpdh_list'][] = $item['jjcp_cpdh'];
+            }
 
-        foreach ($data as $item) {
-            if (isset($item['jjcp_cpdh'], $item['成品编码']) && $item['jjcp_cpdh'] === $item['成品编码']) {
-                $filtered[] = $item;
+            if (!in_array($item['成品编码'], $grouped_products[$group_key]['cpbm_list'])) {
+                $grouped_products[$group_key]['cpbm_list'][] = $item['成品编码'];
             }
         }
 
-        $count = $mongo->name('finished_products')
-            ->where($where)
-            ->order('UniqId','desc')
-            ->where('jjcp_smb','末 板')
-            ->select();
+        // 格式化成用逗号连接的字符串
+        foreach ($grouped_products as &$group) {
+            $group['cpdh'] = implode(',', $group['cpdh_list']);
+            $group['cpbm'] = implode(',', $group['cpbm_list']);
+            unset($group['cpdh_list'], $group['cpbm_list']); // 清理临时字段
+        }
+
+        // 3. 将聚合后结果分页(稳定分页)
+        $all_grouped = array_values($grouped_products);
+        $total = count($all_grouped);
+        $paged_grouped = array_slice($all_grouped, $offset, $limit);
+
+        // 4. 获取库存出货数据并计算剩余数
+        foreach ($paged_grouped as &$prod) {
+            $inventory = $mongo->name('inventory_summary')->where([
+                'order_ddbh' => $prod['order_ddbh'],
+                'gdbh' => $prod['gdbh'],
+                'cpmc' => $prod['cpmc'],
+            ])->find();
+
+            // 出货数量(若无数据则为 0)
+            $total_chu_quantity = isset($inventory['total_chu_quantity']) ? (int)$inventory['total_chu_quantity'] : 0;
+            $prod['total_chu_quantity'] = $total_chu_quantity;
+
+            // 计算剩余数 = sl - 出货数
+            $prod['remaining_quantity'] = $prod['sl'] - $total_chu_quantity;
+        }
+        unset($prod);
 
         if (request()->isAjax()) {
             return json([
-                'data'  => $filtered,
-                'total' => count($count),
+                'code'  => 1,
+                'data'  => $paged_grouped,
+                'total' => $total,
                 'page'  => $page,
                 'limit' => $limit
             ]);
         }
 
-        $this->assign('data', $data);
+        $this->assign('data', $paged_grouped);
         return $this->fetch();
     }
 
+//    public function index()
+//    {
+//        $search = input('');
+//        $page = input('get.page', 1);
+//        $limit = input('get.limit', 10);
+//        $mongo = \think\Db::connect('mongodb');
+//
+//        // 初始化查询条件
+//        $where = [];
+//
+//        if (!empty($search['search'])) {
+//            // 使用正则表达式来实现模糊匹配,'i' 表示忽略大小写
+//            $where['成品编码|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($search['search'], 'i');
+//        }
+//
+//        if($where){
+//            $data = $mongo->name('finished_products')
+//                ->limit($page, $limit)
+//                ->where($where)
+//                ->order('UniqId','desc')
+//                ->where('jjcp_smb','末 板')
+//                ->select();
+//        }else{
+//            $data = $mongo->name('finished_products')
+//                ->limit($page, $limit)
+//                ->where($where)
+//                ->order('UniqId','desc')
+//                ->where('jjcp_smb','末 板')
+//                ->select();
+//        }
+//
+//        $filtered = [];
+//
+//        foreach ($data as $item) {
+//            if (isset($item['jjcp_cpdh'], $item['成品编码']) && $item['jjcp_cpdh'] === $item['成品编码']) {
+//                $filtered[] = $item;
+//            }
+//        }
+//
+//        $count = $mongo->name('finished_products')
+//            ->where($where)
+//            ->order('UniqId','desc')
+//            ->where('jjcp_smb','末 板')
+//            ->select();
+//
+//        if (request()->isAjax()) {
+//            return json([
+//                'data'  => $filtered,
+//                'total' => count($count),
+//                'page'  => $page,
+//                'limit' => $limit
+//            ]);
+//        }
+//
+//        $this->assign('data', $data);
+//        return $this->fetch();
+//    }
+
 
     public function finished()
     {
@@ -105,14 +216,16 @@ class Finishedproduct extends Backend
 
         foreach ($data['data'] as $item) {
             $insertList[] = [
-                'jjcp_cpmc' => $item['jjcp_cpmc'],
-                'jjcp_cpdh' => $item['jjcp_cpdh'],
-                'order_ddbh' => $item['order_ddbh'],
+                '成品名称' => $item['jjcp_cpmc'],
+                '成品编码' => $item['jjcp_cpbm'],
+                '订单编号' => $item['order_ddbh'],
                 'jjcp_gdbh' => $item['jjcp_gdbh'],
                 'jjcp_sl'   => intval($item['jjcp_sl']),
-                'jjcp_smb'  => '已入库',
+                'jjcp_smb'  => '末 板',
+                'Mod_rq'  => '',
                 'company'  => $userinfo['company'],
-                'created_at' => date('Y-m-d H:i:s')
+                'jjcp_sj' => date('Y-m-d H:i:s'),
+                'Sys_rq' => date('Y-m-d H:i:s')
             ];
         }
 

+ 124 - 292
application/admin/controller/QcodeAdd.php

@@ -15,14 +15,11 @@ use fast\Arr;
 use think\Db;
 use think\Session;
 
-class QcodeAdd extends Backend
-{
+class QcodeAdd extends Backend{
     /**
      * 首页展示
      */
-    public function index()
-    {
-
+    public function index(){
         //获取登录账号信息【厂商信息】
         $userinfo = Session::get('admin');
         $data = [
@@ -60,71 +57,22 @@ class QcodeAdd extends Backend
                 echo "创建集合 {$tableName} 失败:" . $e->getMessage();
             }
         }
-
-//        //统计产品信息数据
-//        $mongo = \think\Db::connect('mongodb');
-//        // 获取成品入仓表数据
-//        $mongo_products = $mongo
-//            ->name('finished_products')
-//            ->field('成品名称, jjcp_cpdh, 订单编号, jjcp_gdbh, jjcp_sl')
-//            ->order('UniqId','desc')
-//            ->where('jjcp_smb','末 板')
-//            ->limit(1,50)
-//            ->select();
-//
-//        $grouped_products = [];
-//        foreach ($mongo_products as $item) {
-//            $group_key = $item['订单编号'] . '_' . $item['jjcp_gdbh'];
-//            if (!isset($grouped_products[$group_key])) {
-//                $grouped_products[$group_key] = [
-//                    'order_ddbh' => $item['订单编号'],
-//                    'gdbh' => $item['jjcp_gdbh'],
-//                    'cpmc' => $item['成品名称'],
-////                    'cpdh' => $item['jjcp_cpdh'],
-//                    'ruku_sl' => 0,            // 入库数量
-//                    'remain_stock' => 0,       // 剩余库存
-//                    'sl' => 0
-//                ];
-//            }
-//            // 累加入库数量
-//            $grouped_products[$group_key]['ruku_sl'] += (int)$item['jjcp_sl'];
-//        }
-//        // 查询库存表并计算
-//        foreach ($grouped_products as $group_key => &$prod) {
-//            $where = [
-//                'order_ddbh' => $prod['order_ddbh'],
-//                'gdbh' => $prod['gdbh'],
-//                'cpmc' => $prod['cpmc'],
-//            ];
-//
-//            $inventory = $mongo->name('inventory_summary')->where($where)->find();
-//            $prod['remaining_quantity'] = $inventory ? (int)$inventory['remaining_quantity'] : 0;
-//            $prod['sl'] = $prod['remaining_quantity'] > 0 ? $prod['remaining_quantity'] : $prod['ruku_sl'];
-//        }
-//        unset($prod);
-//        $matched_products = array_values($grouped_products);
-//        $this->view->assign('product', $matched_products);
         return $this->view->fetch();
     }
 
 
     /**
      * 获取产品信息
-     * 查询入仓表【finished_products】
      */
-    public function product()
-    {
+    public function product(){
         $params = input('');
         $page = max(1, (int)$params['page']);
         $limit = max(1, (int)$params['limit']);
         $offset = ($page - 1) * $limit;
 
-
-
         // 获取工单编号列表
         $db3 = Db::connect(config('database.db3'));
         $gdRows = $db3->query("SELECT DISTINCT `Gd_gdbh`, `Gd_客户代号`, `Gd_cpdh` FROM `工单_基本资料` WHERE `Gd_客户代号` = ?", ['J0031']);
-
         $Gd_gdbhList = array_column($gdRows, 'Gd_gdbh');
         $gdbhList = array_column($gdRows, 'Gd_cpdh');
         if (empty($gdbhList)) {
@@ -137,9 +85,8 @@ class QcodeAdd extends Backend
         // Mongo 查询
         $mongo = \think\Db::connect('mongodb');
         $where = ['jjcp_cpdh' => ['in', $gdbhList],'jjcp_gdbh' => ['in', $Gd_gdbhList], 'jjcp_smb' => '末 板'];
-
         if (!empty($params['search'])) {
-            $where['jjcp_cpdh|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($params['search'], 'i');
+            $where['成品编码|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($params['search'], 'i');
         }
 
         // 1. 查询所有符合条件的成品数据(不分页)
@@ -148,21 +95,39 @@ class QcodeAdd extends Backend
             ->order('UniqId', 'desc')
             ->select();
 
-        // 2. 按订单编号 + 工单编号分组聚合
+        // 2. 按订单编号 + 工单编号分组聚合,并处理多个cpdh与成品编码
         $grouped_products = [];
         foreach ($all_products as $item) {
             $group_key = $item['订单编号'] . '_' . $item['jjcp_gdbh'];
+
             if (!isset($grouped_products[$group_key])) {
                 $grouped_products[$group_key] = [
                     'order_ddbh' => $item['订单编号'],
                     'gdbh' => $item['jjcp_gdbh'],
+                    'cpbm' => $item['成品编码'],
                     'cpmc' => $item['成品名称'],
-                    'ruku_sl' => 0,
-                    'remain_stock' => 0,
-                    'sl' => 0
+                    'sl' => 0, // 库存总数
+                    'cpdh_list' => [],
+                    'cpbm_list' => []
                 ];
             }
-            $grouped_products[$group_key]['sl'] += (int)$item['jjcp_sl'];
+            $grouped_products[$group_key]['sl'] += (int)$item['jjcp_sl']; // 累计库存数量
+
+            // 收集不同的cpdh和cpbm(成品编码)
+            if (!in_array($item['jjcp_cpdh'], $grouped_products[$group_key]['cpdh_list'])) {
+                $grouped_products[$group_key]['cpdh_list'][] = $item['jjcp_cpdh'];
+            }
+
+            if (!in_array($item['成品编码'], $grouped_products[$group_key]['cpbm_list'])) {
+                $grouped_products[$group_key]['cpbm_list'][] = $item['成品编码'];
+            }
+        }
+
+        // 格式化成用逗号连接的字符串
+        foreach ($grouped_products as &$group) {
+            $group['cpdh'] = implode(',', $group['cpdh_list']);
+            $group['cpbm'] = implode(',', $group['cpbm_list']);
+            unset($group['cpdh_list'], $group['cpbm_list']); // 清理临时字段
         }
 
         // 3. 将聚合后结果分页(稳定分页)
@@ -170,7 +135,7 @@ class QcodeAdd extends Backend
         $total = count($all_grouped);
         $paged_grouped = array_slice($all_grouped, $offset, $limit);
 
-        // 4. 获取库存数据
+        // 4. 获取库存出货数据并计算剩余数
         foreach ($paged_grouped as &$prod) {
             $inventory = $mongo->name('inventory_summary')->where([
                 'order_ddbh' => $prod['order_ddbh'],
@@ -178,12 +143,14 @@ class QcodeAdd extends Backend
                 'cpmc' => $prod['cpmc'],
             ])->find();
 
-//            $prod['remaining_quantity'] = $inventory ? (int)$inventory['remaining_quantity'] : 0;
-//            $prod['sl'] = $prod['remaining_quantity'] > 0 ? $prod['remaining_quantity'] : $prod['ruku_sl'];
+            // 出货数量(若无数据则为 0)
+            $total_chu_quantity = isset($inventory['total_chu_quantity']) ? (int)$inventory['total_chu_quantity'] : 0;
+            $prod['total_chu_quantity'] = $total_chu_quantity;
 
+            // 计算剩余数 = sl - 出货数
+            $prod['remaining_quantity'] = $prod['sl'] - $total_chu_quantity;
         }
         unset($prod);
-
         return json([
             'code'  => 1,
             'data'  => $paged_grouped,
@@ -193,135 +160,6 @@ class QcodeAdd extends Backend
         ]);
     }
 
-
-//    public function product()
-//    {
-//
-//        $db3 = Db::connect(config('database.db3'));
-//        $query = $db3->query("SELECT * FROM `工单_基本资料` WHERE `Gd_客户代号` = ?", ['J0031']);
-//
-//        echo "<pre>";
-//        print_r($query);
-//        echo "</pre>";
-//        die;
-//
-//        $params = input('');
-//        $page = max(1, (int)$params['page']);
-//        $limit = max(1, (int)$params['limit']);
-//        $offset = ($page - 1) * $limit;
-//
-//        $mongo = \think\Db::connect('mongodb');
-//        $where = [];
-//        if (!empty($params['search'])) {
-//            // 使用正则表达式来实现模糊匹配,'i' 表示忽略大小写
-//            $where['jjcp_cpdh|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($params['search'], 'i');
-//        }
-////        if (!empty($where)) {
-////            $mongo_products = $mongo->name('finished_products')
-////                ->where($where)
-////                ->limit($offset, $limit)
-////                ->order('UniqId','desc')
-////                ->where('jjcp_smb','末 板')
-////                ->select();
-////        } else {
-////            // 如果没有条件,仍然执行查询,避免错误
-////            $mongo_products = $mongo->name('finished_products')
-////                ->limit(1, 10)
-////                ->order('UniqId','desc')
-////                ->where('jjcp_smb','末 板')
-////                ->select();
-////        }
-//
-//
-//
-//
-//        $mongo_products = $mongo->name('finished_products')
-//            ->where($where)
-//            ->limit($offset, $limit)
-//            ->order('UniqId','desc')
-//            ->where('jjcp_smb','末 板')
-//            ->select();
-//        echo "<pre>";
-//        print_r($mongo_products);
-//        echo "<pre>";die;
-//
-//        //计算【订单编号】【jjcp_gdbh】相同的数据进行统计汇总累加入库数量【jjcp_sl】
-//        $grouped_products = [];
-//        foreach ($mongo_products as $item) {
-//            $group_key = $item['订单编号'] . '_' . $item['jjcp_gdbh'];
-//            if (!isset($grouped_products[$group_key])) {
-//                $grouped_products[$group_key] = [
-//                    'order_ddbh' => $item['订单编号'],
-//                    'gdbh' => $item['jjcp_gdbh'],
-//                    'cpmc' => $item['成品名称'],
-////                    'cpdh' => $item['jjcp_cpdh'],
-//                    'ruku_sl' => 0,            // 入库数量
-//                    'remain_stock' => 0,       // 剩余库存
-//                    'sl' => 0
-//                ];
-//            }
-//            $grouped_products[$group_key]['ruku_sl'] += (int)$item['jjcp_sl'];
-//        }
-//        // 查询库存表并计算
-//        foreach ($grouped_products as $group_key => &$prod) {
-//            $where = [
-//                'order_ddbh' => $prod['order_ddbh'],
-//                'gdbh' => $prod['gdbh'],
-//                'cpmc' => $prod['cpmc'],
-//            ];
-//            $inventory = $mongo->name('inventory_summary')->where($where)->find();
-//            $prod['remaining_quantity'] = $inventory ? (int)$inventory['remaining_quantity'] : 0;
-//            $prod['sl'] = $prod['remaining_quantity'] > 0 ? $prod['remaining_quantity'] : $prod['ruku_sl'];
-//        }
-//        unset($prod);
-//        $matched_products = array_values($grouped_products);
-//
-//        // 获取总数
-//        $total = $mongo->name('finished_products')
-//            ->where($where)
-//            ->count();
-//
-//        return json([
-//            'code'  => 1,
-//            'data'  => $matched_products,
-//            'total' => $total,
-//            'page'  => $page,
-//            'limit' => $limit
-//        ]);
-//    }
-
-
-//    public function product(){
-//        $QcodeProduct = new QcodeProduct();
-//        $ResetFlow = new ResetFlow();
-//        $userinfo = Session::get('admin');
-//        if ($this->request->isAjax() === false){
-//            $this->error('请求错误');
-//        }
-//        $product_code = input('product_code');
-//
-//        if (empty($product_code)){
-//            $this->error('参数错误');
-//        }
-//        $product = $QcodeProduct->where('product_code',$product_code)->find();
-//        if (empty($product)){
-//            $this->error('未找到该产品信息');
-//        }
-//
-//        $flow = $ResetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',substr(json_encode($product['_id']),9,-2))->find();
-//        $row = [
-//            'temple' => $product['temple'],
-//            'flow' => isset($flow['l_flow'])?(int)$flow['l_flow']+1:'',
-//            'bach' => isset($flow['bach_num'])?(int)$flow['bach_num']+1:'',
-//        ];
-//        if (!empty($flow)){
-//            $row['flow'] = $flow['l_flow']+1;
-//            $row['bach'] = $flow['bach_num']+1;
-//        }
-//
-//        return json(['code'=>1,'data'=>$row]);
-//    }
-
     /**
      * 增加新批次
      */
@@ -350,47 +188,43 @@ class QcodeAdd extends Backend
                $data[$k] = $v;
            }
         }
-//        echo "接口获取";
-//        echo "<pre>";
-//        print_r($data);
-//        echo "<pre>";die;
 
+        //打印信息
+
+//        echo "接口获取";echo "<pre>";print_r($data);echo "<pre>";die;
 
 
+        //记录库存操作
         $mongo = \think\Db::connect('mongodb');
-        // 构建查询条件
         $where = [
             'gdbh' => $data['gdbh'],
             'order_ddbh' => $data['order_ddbh'],
-//            'cpdh' => $data['cpdh'],
             'cpmc' => $data['cpmc'],
         ];
-        //插入字段
+        //插入字段[inventory_summaryk库存明细]
         $insertData = [
             'gdbh'             => $data['gdbh'],
             'order_ddbh'             => $data['order_ddbh'],
-//            'cpdh'             => $data['cpdh'],
+            'cpbm'             => $data['cpbm'],
             'cpmc'             => $data['cpmc'],
-            'total_ru_quantity'   => $data['sl'],//入库数
-            'total_chu_quantity' => $data['number'],//出库数
-            'remaining_quantity'  => $data['sl'] - $data['number'],//剩余结存数量
+            'total_ru_quantity'   => $data['sl'],//入库数
+            'total_chu_quantity' => $data['number'],//出库数
+            'remaining_quantity'  => $data['remaining_quantity'] - $data['number'],//剩余数
             'created_at'       => date('Y-m-d H:i:s'),
             'updated_at'       => '',
             'mod_rq'           => '',
             'company'          => '',
         ];
-
-        // 查询是否已存在记录
+        // 查询库存汇总表数据是否存在记录
         $list = $mongo->name('inventory_summary')->where($where)->find();
         if ($list) {
             // 如果数据存在,更新库存汇总记录
-            $ru  = $list['total_ru_quantity'] + $data['sl'];//累计入库数量
-            $chu = $list['total_chu_quantity'] + $data['sl'];//累计出库数量
-            $jiecun  = $data['sl'] - $data['number'];//剩余结存数量
+            $chu = $list['total_chu_quantity'] + $data['number'];//累计出库数量
+            $jiecun  = $list['remaining_quantity'] - $data['number'];//剩余结存数量
             $updateResult = $mongo->name('inventory_summary')
                 ->where($where)
                 ->update([
-                    'total_ru_quantity'   => $ru,
+                    'total_ru_quantity'   => $data['sl'],
                     'total_chu_quantity' => $chu,
                     'remaining_quantity'  => $jiecun,
                     'updated_at'       => date('Y-m-d H:i:s'),
@@ -402,32 +236,45 @@ class QcodeAdd extends Backend
         //记录库存明细
         $mongo->name('inventory_records')->insert($insertData);
 
-//        // 调试输出(可删)
-//        echo "<pre>"; print_r($list); echo "</pre>";
-//        die;
-
-
+//        // 调试输出
+//        echo "<pre>"; print_r($list); echo "</pre>";die;
+
+
+
+//        if ($data['danwei'] == 1){
+//            $num = $data['number'];
+//            $tray_num = $data['tray_num'];
+//            $box_number = $data['box_number'];
+//            //$small_num = (int)ceil((int)总数量(张/个)/(int)张数);
+//            $small_num = (int)ceil((int)$data['number']/(int)$data['box_number']);
+//            $large_num = (int)ceil($small_num/$tray_num);
+//        }else{
+//            $num = 0;
+//            $tray_num = $data['volume_num'];
+//            $small_num = $data['small_num'];
+//            $large_num = (int)ceil($small_num/$tray_num);
+//            $box_number = 1;
+//            $tray_num1 = 1;
+//        }
 
-        if ($data['danwei'] == 1){
-            $num = $data['number'];
-            $tray_num = $data['tray_num'];
-            $tray_num1 = $data['tray_num'];
-            $box_number = $data['box_number'];
-            //$small_num = (int)ceil((int)总数量(张/个)/(int)张数);
-            $small_num = (int)ceil((int)$data['number']/(int)$data['box_number']);
-            $large_num = (int)ceil($small_num/$tray_num);
-        }else{
-            $num = 0;
-            $tray_num = $data['volume_num'];
-            $small_num = $data['small_num'];
-            $large_num = (int)ceil($small_num/$tray_num);
-            $box_number = 1;
-            $tray_num1 = 1;
+        switch ($data['danwei']) {
+            case 1:
+                $danwei = '个';
+                break;
+            case 2:
+                $danwei = '套';
+                break;
+            case 3:
+                $danwei = '张';
+                break;
+            default:
+                $danwei = '';
+                break;
         }
 
         $userinfo = Session::get('admin');//获取用户信息
         $arr = [
-            'batch' => $data['batch'],
+            'batch' => $data['gdbh'],
             'create_time' => time(),
             'delect_time' => '',
             'sync_flag' => 0,
@@ -439,92 +286,73 @@ class QcodeAdd extends Backend
 
 
         $batchList = [
-            'userid' => $userinfo['id'],
-            'supplier_id' => $userinfo['id'],
+            'userid' => $userinfo['id'],//登录用户id
+            'supplier_id' => $userinfo['id'],//登录用户id
             'supplier_code' => $userinfo['printer_code'],
 
             'supplier_name' => $data['company_name'],//公司名称
-            'matter_name' => $data['cpmc'],//产品名称
-            'matter_no' => $data['batch'],//生产批次号
+            'cpbm' => $data['cpbm'],//成品编码
+            'matter_name' => $data['cpmc'],//成品名称
+            'matter_no' => $data['gdbh'],//生产批次号-工单编号
             'order_ddbh' => $data['order_ddbh'],//销售订单号
-            'matter_id' => $data['batch'],
-//            'matter_type' => $data['temple'],
+            'matter_id' => $data['gdbh'],
             'matter_type' => '01',
             'manufacture_date' => (int)date('ymd',strtotime($data['manufacture_date'])),//生产日期
             'print_date' => (int)date('ymd',strtotime($data['print_date'])),//打码日期
 
-            'danwei' => $data['danwei'],//单位
-            'num' => $num,//总数量(张/个)
-            'tray_num' => $tray_num1,//每层箱数
-            'box_num' => $box_number,//每托层数
-            'total_boxes' => $data['total_boxes'],//每托盘箱数
-            'small_num' => $small_num,//每托盘箱数
+            'num_danwei' => $data['danwei'],//单位
+            'danwei' => $danwei,//单位
+            'num' => $data['number'],//总数量(张/个)
+            'small_num' => $data['box_number'],//每一箱数量
+            'tray_num' => $data['tray_num'],//每层箱数【箱/层】
+            'box_num' => $data['tray_number'],//每托层数【 层/托】
+            'total_boxes' => $data['total_boxes'],//每托盘箱数【箱/托】
+            'large_num' => $data['box_num'],//本次打包托盘数
+            'larger_num' => $data['small_num'],//总箱数
             'pallet_height' => $data['pallet_height'],//每托高度
             'pallet_length' => $data['pallet_length'],//托盘规格
             'pallet_width' => $data['pallet_width'],//托盘规格
-            'larger_num' => $large_num,//大件(总托数)
 
             'l_reservation' => '',
-            'l_flow' => $data['big_liushui'],
+            // 处理可能为空的流水/重量字段
+            'l_flow' => $data['big_liushui'],//大件流水
             'l_weight' => $data['big_weight'],//大件重量
-            's_flow' => $data['small_start_liushui'],
+            's_flow' => $data['small_start_liushui'],//小件流水
             's_weight' => $data['small_weight'],//小件重量
             's_reservation' => '',
             'bach_status' => 0,
-            'bach_num' => $data['batch'],
+            'bach_num' => $data['gdbh'],//生产批次号-工单编号
             'large_endnum' => $data['big_liushui'] + $data['box_num'] -1,
             'create_time' => time(),//新增时间
+            'delect_time' => '',//删除时间
         ];
+
         $res = $bach->save($batchList);
         if ($res === 0){
             $this->error('添加失败');
         }
 
         $flow = [
-            'l_flow' => (int)$batchList['large_endnum'],
-            'bach_num' => $batchList['bach_num'],
+            'l_flow' => (int)$batchList['large_num'],
+            'bach_num' => $batchList['matter_no'],
         ];
-
-        if ($resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_id'])->find()){
-            $resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_id'])->update($flow);
+        if ($resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_no'])->find()){
+            $resetFlow->name($userinfo['company'].'_'."reset_flow")->where('product_id',$batchList['matter_no'])->update($flow);
         }else{
-            $flow['product_id'] = $batchList['matter_id'];
+            $flow['product_id'] = $batchList['matter_no'];
             $resetFlow->save($flow);
         }
 
         $last_id = $bach->getLastInsID();
-
         if ($last_id){
-            //插入大小二维码数据
-            //二维码数据不变区域
-//            echo "<pre>";
-//            print_r($batchList);
-//            echo "<pre>";
-//            die;
-//            echo "<pre>";
-//            print_r($batchList['matter_type']);
-//            echo "<pre>";
-//            echo "<pre>";
-//            print_r($batchList['supplier_code']);
-//            echo "<pre>";
-//            echo "<pre>";
-//            print_r('9');
-//            echo "<pre>";
-//            echo "<pre>";
-//            print_r($batchList['matter_no']);
-//            echo "<pre>";
-//            echo "<pre>";
-//            print_r($batchList['manufacture_date']);
-//            echo "<pre>";
-//            echo "<pre>";
-//            print_r($batchList['print_date']);
-//            echo "<pre>";die;
+            //插入大小二维码数据 二维码数据不变区域
+//            echo "<pre>";print_r($batchList);echo "<pre>";die;
+
             $fixed_code = '';
             $fixed_code.=$this->intTochar($batchList['matter_type'],2);//2位  辅料种类编码
-//            $fixed_code .= substr($this->intTochar($batchList['supplier_code'], 12), 1);//12位  供应商编码 // 去掉第一位0
             $fixed_code .= $this->intTochar($batchList['supplier_code'], 21);//21位  供应商编码
             $fixed_code .= '9'; // 补0一位
-            $fixed_code.=$this->intTochar($batchList['matter_no'],10);//10位  辅料编码
+            $fixed_code.=$this->intTochar($batchList['cpbm'],10);//10位  辅料编码
             $fixed_code.=$batchList['manufacture_date'];//6位  生产日期
             $print_code=$batchList['print_date'];//6位  打码日期
 
@@ -551,25 +379,27 @@ class QcodeAdd extends Backend
             }
 
             //循环插入大件二维码数据
-            for ($i=0;$i<$batchList['larger_num'];$i++){
+            for ($i=0;$i<$data['box_num'];$i++){
                 $large = new QcodeLarge();
                 $l_flow = $this->intTochar($batchList['l_flow']+$i,6);
                 $l_weight = $this->intTochar($batchList['l_weight']*100,6);
-                $l_reservation = $this->intTochar($batchList['bach_num'],10);
+                $l_reservation = $this->intTochar($batchList['matter_no'],10);
                 $l_reservation = $l_reservation.'0000000000';
-                $remainder = $batchList['small_num'] - $batchList['tray_num'] * $i;//最后一托盘小件数量
+                $remainder = $batchList['num'] - $batchList['total_boxes'] * $i;//最后一托盘小件数量
+
                 if ($remainder < $batchList['tray_num']){
                     $small_n = $this->intTochar($remainder,3);//3位小件数量,不足补0
                 }else{
                     $small_n = $this->intTochar($batchList['tray_num'],3);
                 }
+
+//                $l_num = $small_n * $batchList['box_num'];
                 $l_num = 0;
-                if ($data['danwei'] == 1){
-                    //以箱为单位时
-                    $l_num = $small_n * $batchList['box_num'];
-                }
+
+
                 //大件二维码数据
                 $code_data = $this->CodeData('AB92',$fixed_code,$small_n,$print_code,$l_flow,$l_weight,'2',$l_reservation);
+
                 //大码数据信息
                 $l_data = [
                     'bach_id' => $last_id,
@@ -585,6 +415,7 @@ class QcodeAdd extends Backend
                     'l_status' => 0,
                     'l_print' => 0
                 ];
+
                 $l_res = $large->save($l_data);
                 if ($l_res === 0){
                     $this->error('大件码插入失败');
@@ -592,9 +423,9 @@ class QcodeAdd extends Backend
                 $large_id = $large->getLastInsID();
                 if ($large_id){
 //                    //小件码循环插入
-                    for ($j=0;$j<$tray_num and ($j+$i*$tray_num)<$batchList['small_num'];$j++){
+                    for ($j=0;$j<$data['tray_num'] and ($j+$i*$data['tray_num'])<$batchList['small_num'];$j++){
                         $small = new QcodeSmall();
-                        $s_flow = $this->intTochar($batchList['s_flow']+$j+$i*$tray_num+$lastNum['last_num'],6);//小件码序号从1开始
+                        $s_flow = $this->intTochar($batchList['s_flow']+$j+$i*$data['tray_num']+$lastNum['last_num'],6);//小件码序号从1开始
                         $s_weight = $this->intTochar($batchList['s_weight'],6);
                         $small_sign = '000';
                         $s_reservation = $this->intTochar($batchList['bach_num'],10);
@@ -631,7 +462,8 @@ class QcodeAdd extends Backend
         if ($liushui_res === false){
             $this->error('添加失败');
         }
-        $this->success('成功');
+        return json(['code' => 1, 'msg' => '成功', 'data' => '']);
+//        $this->success('成功');
     }
 
     /**

+ 51 - 6
application/admin/controller/QcodeBach.php

@@ -48,7 +48,7 @@ class QcodeBach extends Backend
         $this->relationSearch = false;
         //设置过滤方法
         $this->request->filter(['strip_tags', 'trim']);
-        
+
         return $this->view->fetch();
     }
 
@@ -152,6 +152,7 @@ class QcodeBach extends Backend
 //                ->limit($limit)
 //                ->skip($offset)
                 ->select();
+
             $qcodeSmall = new QcodeSmall();
             foreach ($list as $k=>$v) {
                 $oid = $v['_id']->jsonSerialize();
@@ -280,16 +281,20 @@ class QcodeBach extends Backend
             $qcodeSmall = new QcodeSmall();
             $qcodeProduct = new QcodeProduct();
             $large = $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$ids[0])->find();
+
             $bach = $this->model->name($company_id.'_'."qcode_bach")->where('_id',json_decode($large,true)['bach_id'])->find();
+
+//            matter_no.order_ddbh.matter_name
             $row = $qcodeProduct->where('product_code',json_decode($bach,true)['matter_no'])->find();
 
             $company_name = json_decode($bach,true)['supplier_name'];
             $product_name = json_decode($bach,true)['matter_name'];
-//            $date = json_decode($bach,true)['manufacture_date'];
-//            $batch = json_decode($bach,true)['bach_num'];
-            $main_unit = json_decode($row,true)['main_unit'];
-            $sec_unit = json_decode($row,true)['sec_unit'];
-            $proportion = json_decode($row,true)['proportion'];
+//            $main_unit = json_decode($row,true)['main_unit'];
+//            $sec_unit = json_decode($row,true)['sec_unit'];
+//            $proportion = json_decode($row,true)['proportion'];
+            $main_unit = '';
+            $sec_unit = json_decode($bach,true)['danwei'];
+            $proportion = json_decode($bach,true)['num'];
 
             $rows = [];
             //查询打印大件码所需数据
@@ -607,6 +612,46 @@ class QcodeBach extends Backend
         $qcodeLarge = new QcodeLarge();
         $qcodeSmall = new QcodeSmall();
 
+
+        $qcode_bach_list = $this->model->name($company_id.'_'."qcode_bach")->where('_id',$ids)->find();
+//        echo "<pre>";
+//        print_r($qcode_bach_list);
+//        echo "<pre>";
+
+        $mongo = \think\Db::connect('mongodb');
+
+
+        $where = [
+            'gdbh' => $qcode_bach_list['matter_no'],
+            'order_ddbh' => $qcode_bach_list['order_ddbh'],
+            'cpbm' => $qcode_bach_list['cpbm'],
+            'cpmc' => $qcode_bach_list['matter_name'],
+        ];
+        $inventory_summary = $mongo->name('inventory_summary')
+            ->where($where)
+            ->find();
+        $updateResult = $mongo->name('inventory_summary')
+            ->where($where)
+            ->update([
+                'total_ru_quantity' => $inventory_summary['total_ru_quantity'] - $qcode_bach_list['num'],
+                'total_chu_quantity' => $inventory_summary['total_chu_quantity'] - $qcode_bach_list['num'],
+                'remaining_quantity' => $inventory_summary['remaining_quantity'] + $qcode_bach_list['num'],
+                'updated_at'       => date('Y-m-d H:i:s'),
+            ]);
+
+        $where = [
+            'gdbh' => $qcode_bach_list['matter_no'],
+            'order_ddbh' => $qcode_bach_list['order_ddbh'],
+            'cpbm' => $qcode_bach_list['cpbm'],
+            'cpmc' => $qcode_bach_list['matter_name'],
+            'total_chu_quantity' => $qcode_bach_list['num'],
+        ];
+        $inventory_records_updateResult = $mongo->name('inventory_records')
+            ->where($where)
+            ->update([
+                'mod_rq' => date('Y-m-d H:i:s')
+            ]);
+
         $data = [
             'delete_time'=>time(),
         ];

+ 11 - 3
application/admin/controller/user/User.php

@@ -45,7 +45,7 @@ class User extends Backend
             }
             //list($where, $sort, $order, $offset, $limit) = $this->buildparams();
 
-            $this->model = $this->model;
+//            $this->model = $this->model;
                 //->where($where);
             $yearMonth = $this->request->param("yearMonth");
 
@@ -62,7 +62,7 @@ class User extends Backend
                 $this->model =  $this->model->where('cpmc',$test2 );
             }
             $test3 = $this->request->param("test3");
-            if(!empty($test2)){
+            if(!empty($test3)){
                 $this->model =  $this->model->where('order_ddbh',$test3 );
             }
             if(empty($yearMonth)){
@@ -82,6 +82,9 @@ class User extends Backend
         $this->assign("tree",$tree);
         return $this->view->fetch();
     }
+
+
+
     /**
      * 方法二:分级查询(推荐)
      */
@@ -175,6 +178,7 @@ class User extends Backend
         $order_ddbh = $this->request->get('order_ddbh', '');
         $gdbh = $this->request->get('gdbh', '');
         $cpmc = $this->request->get('cpmc', '');
+        $cpbm = $this->request->get('cpbm', '');
 
         $limit = $this->request->get('limit', 20);
         $offset = $this->request->get('offset', 0);
@@ -183,6 +187,7 @@ class User extends Backend
         if ($order_ddbh) $where['订单编号'] = $order_ddbh;
         if ($gdbh) $where['jjcp_gdbh'] = $gdbh;
         if ($cpmc) $where['成品名称'] = $cpmc;
+        if ($cpmc) $where['成品编码'] = $cpbm;
 
         // 获取工单编号列表
         $db3 = Db::connect(config('database.db3'));
@@ -205,7 +210,7 @@ class User extends Backend
 
         // 查询原始数据
         $rawList = $mongo->name('finished_products')
-            ->field('订单编号, 成品名称, jjcp_gdbh, jjcp_sl, jjcp_sj')
+            ->field('订单编号, 成品编码,成品名称, jjcp_gdbh, jjcp_sl, jjcp_sj')
             ->where($where)
             ->order('jjcp_sj','desc')
             ->limit($offset, $limit)
@@ -238,6 +243,7 @@ class User extends Backend
 
             $list[] = [
                 'order_ddbh' => $item['订单编号'] ?? null,
+                'cpbm' => $item['成品编码'] ?? null,
                 'cpmc' => $item['成品名称'] ?? null,
                 'gdbh' => $item['jjcp_gdbh'] ?? null,
                 'sl' => $sl,
@@ -257,6 +263,7 @@ class User extends Backend
         $order_ddbh = $this->request->get('order_ddbh', '');
         $gdbh = $this->request->get('gdbh', '');
         $cpmc = $this->request->get('cpmc', '');
+        $cpbm = $this->request->get('cpbm', '');
 
         $limit = $this->request->get('limit', 20);
         $offset = $this->request->get('offset', 0);
@@ -265,6 +272,7 @@ class User extends Backend
         if ($order_ddbh) $where['order_ddbh'] = $order_ddbh;
         if ($gdbh) $where['gdbh'] = $gdbh;
         if ($cpmc) $where['cpmc'] = $cpmc;
+        if ($cpmc) $where['cpbm'] = $cpbm;
 
         $mongo = \think\Db::connect('mongodb');
 

+ 2 - 2
application/admin/view/deliver/lager.html

@@ -20,9 +20,9 @@
                             <p>
                                 <span style="font-size: 18px;font-weight: bold">温馨提示:你已选中</span>
                                 <span style="font-size: 18px;font-weight: bold;color: red">0</span>
-                                <span style="font-size: 18px;font-weight: bold">箱,每层共包含了</span>
+                                <span style="font-size: 18px;font-weight: bold">箱,</span>
                                 <span style="font-size: 18px;font-weight: bold;color: red">0</span>
-                                <span style="font-size: 18px;font-weight: bold"></span>
+                                <span style="font-size: 18px;font-weight: bold"> (个/套/张)</span>
                             </p>
 <!--                            <a href="javascript:;" class="btn btn-success  btn-disabled disabled {:$auth->check('')?'':'hide'}" title="新增大件" ><i class="fa fa-pencil"></i>新增大件</a>-->
                         </div>

+ 19 - 15
application/admin/view/finishedproduct/index.html

@@ -231,11 +231,13 @@
         <thead>
         <tr>
             <th style="width: 15%">销售订单号</th>
-            <th style="width: 10%">工单编号</th>
-            <th style="width: 10%">产品代号</th>
-            <th style="width: 30%">产品名称</th>
-            <th style="width: 10%">数量</th>
-            <th style="width: 10%">入库日期</th>
+            <th style="width: 13%">工单编号</th>
+            <th style="width: 9%">成品编码</th>
+            <th style="width: 33%">成品名称</th>
+            <th style="width: 6%">入库数</th>
+            <th style="width: 6%">发货数</th>
+            <th style="width: 6%">剩余数</th>
+<!--            <th style="width: 11%">入库日期</th>-->
         </tr>
         </thead>
         <tbody id="tableBody">
@@ -338,12 +340,14 @@
             data.forEach(row => {
                 tbody.append(`
                     <tr>
-                        <td>${row.订单编号 || '-'}</td>
-                        <td>${row.jjcp_gdbh || '-'}</td>
-                        <td>${row.jjcp_cpdh || '-'}</td>
-                        <td>${row.jjcp_cpmc || '-'}</td>
-                        <td>${row.jjcp_sl || '0'}</td>
-                        <td>${row.Sys_rq}</td>
+                        <td>${row.order_ddbh || '-'}</td>
+                        <td>${row.gdbh || '-'}</td>
+                        <td>${row.cpbm || '-'}</td>
+                        <td>${row.cpmc || '-'}</td>
+                        <td>${row.sl || '0'}</td>
+                        <td>${row.total_chu_quantity || '0'}</td>
+                        <td>${row.remaining_quantity || '0'}</td>
+<!--                        <td>${row.Sys_rq}</td>-->
                     </tr>
                 `);
             });
@@ -497,15 +501,15 @@
                             <input type="text" name="jjcp_gdbh" class="form-control" required />
                         </div>
                         <div class="form-group">
-                            <label>产品代号</label>
-                            <input type="text" name="jjcp_cpdh" class="form-control" required />
+                            <label>成品编码</label>
+                            <input type="text" name="jjcp_cpbm" class="form-control" required />
                         </div>
                         <div class="form-group">
-                            <label>品名称</label>
+                            <label>品名称</label>
                             <input type="text" name="jjcp_cpmc" class="form-control" required />
                         </div>
                         <div class="form-group">
-                            <label>数</label>
+                            <label>入库数</label>
                             <input type="number" name="jjcp_sl" class="form-control" min="1" required />
                         </div>
                     </form>

+ 160 - 12
application/admin/view/index/login.html

@@ -8,6 +8,7 @@
             color: #999;
             background-color: #f1f4fd;
             background-size: cover;
+            /*background-image: url("/uploads/bg.png");*/
         }
 
         a {
@@ -46,7 +47,7 @@
             -webkit-border-radius: 50%;
             border-radius: 50%;
             margin: -93px auto 30px;
-            border: 5px solid #fff;
+            /*border: 5px solid #fff;*/
         }
 
         .profile-name-card {
@@ -79,27 +80,24 @@
     </style>
     <!--@formatter:off-->
     {if $background}
-        <style type="text/css">
-            body{
-                background-image: url('{$background}');
-            }
-        </style>
+    <style type="text/css">
+        body{
+            background-image: url('{$background}');
+        }
+    </style>
     {/if}
     <!--@formatter:on-->
 </head>
 <body>
 <div class="container">
     <div class="login-wrapper">
-        <br>
-        <br>
-        <div class="login-screen" style="margin-left: 400px">
+        <div class="login-screen">
             <div class="well">
                 <div class="login-head">
-                    <img src="__CDN__/assets/img/login-head.png" style="width:100%;"/>
-<!--                    <img src="/uploads/bg.png" style="width:100%;"/>-->
+                    <img src="__CDN__/uploads/login.png" style="width:100%;"/>
                 </div>
                 <div class="login-form">
-                    <img id="profile-img" class="profile-img-card" src="__CDN__/assets/img/avatar.png"/>
+                    <!--                    <img id="profile-img" class="profile-img-card" src="__CDN__/assets/img/avatar.png"/>-->
                     <p id="profile-name" class="profile-name-card"></p>
 
                     <form action="" method="post" id="login-form">
@@ -147,3 +145,153 @@
 {include file="common/script" /}
 </body>
 </html>
+
+<!--<!DOCTYPE html>-->
+<!--<html>-->
+<!--<head>-->
+<!--    {include file="common/meta" /}-->
+
+<!--    <style type="text/css">-->
+<!--        body {-->
+<!--            color: #999;-->
+<!--            background-color: #f1f4fd;-->
+<!--            background-size: cover;-->
+<!--        }-->
+
+<!--        a {-->
+<!--            color: #444;-->
+<!--        }-->
+
+
+<!--        .login-screen {-->
+<!--            max-width: 430px;-->
+<!--            padding: 0;-->
+<!--            margin: 100px auto 0 auto;-->
+
+<!--        }-->
+
+<!--        .login-screen .well {-->
+<!--            border-radius: 3px;-->
+<!--            -webkit-box-shadow: 0 0 30px rgba(0, 0, 0, 0.1);-->
+<!--            box-shadow: 0 0 30px rgba(0, 0, 0, 0.1);-->
+<!--            background: rgba(255, 255, 255, 1);-->
+<!--            border: none;-->
+<!--            /*overflow: hidden;*/-->
+<!--            padding: 0;-->
+<!--        }-->
+
+<!--        @media (max-width: 767px) {-->
+<!--            .login-screen {-->
+<!--                padding: 0 20px;-->
+<!--            }-->
+<!--        }-->
+
+<!--        .profile-img-card {-->
+<!--            width: 100px;-->
+<!--            height: 100px;-->
+<!--            display: block;-->
+<!--            -moz-border-radius: 50%;-->
+<!--            -webkit-border-radius: 50%;-->
+<!--            border-radius: 50%;-->
+<!--            margin: -93px auto 30px;-->
+<!--            border: 5px solid #fff;-->
+<!--        }-->
+
+<!--        .profile-name-card {-->
+<!--            text-align: center;-->
+<!--        }-->
+
+<!--        .login-head {-->
+<!--            background: #899fe1;-->
+<!--            border-radius: 3px 3px 0 0;-->
+<!--        }-->
+
+<!--        .login-form {-->
+<!--            padding: 40px 30px;-->
+<!--            position: relative;-->
+<!--            z-index: 99;-->
+<!--        }-->
+
+<!--        #login-form {-->
+<!--            margin-top: 20px;-->
+<!--        }-->
+
+<!--        #login-form .input-group {-->
+<!--            margin-bottom: 15px;-->
+<!--        }-->
+
+<!--        #login-form .form-control {-->
+<!--            font-size: 13px;-->
+<!--        }-->
+
+<!--    </style>-->
+<!--    &lt;!&ndash;@formatter:off&ndash;&gt;-->
+<!--    {if $background}-->
+<!--        <style type="text/css">-->
+<!--            body{-->
+<!--                background-image: url('{$background}');-->
+<!--            }-->
+<!--        </style>-->
+<!--    {/if}-->
+<!--    &lt;!&ndash;@formatter:on&ndash;&gt;-->
+<!--</head>-->
+<!--<body>-->
+<!--<div class="container">-->
+<!--    <div class="login-wrapper">-->
+<!--        <br>-->
+<!--        <br>-->
+<!--        <div class="login-screen" style="margin-left: 400px">-->
+<!--            <div class="well">-->
+<!--                <div class="login-head">-->
+<!--                    <img src="__CDN__/assets/img/login-head.png" style="width:100%;"/>-->
+<!--&lt;!&ndash;                    <img src="/uploads/bg.png" style="width:100%;"/>&ndash;&gt;-->
+<!--                </div>-->
+<!--                <div class="login-form">-->
+<!--                    <img id="profile-img" class="profile-img-card" src="__CDN__/assets/img/avatar.png"/>-->
+<!--                    <p id="profile-name" class="profile-name-card"></p>-->
+
+<!--                    <form action="" method="post" id="login-form">-->
+<!--                        &lt;!&ndash;@AdminLoginFormBegin&ndash;&gt;-->
+<!--                        <div id="errtips" class="hide"></div>-->
+<!--                        {:token()}-->
+<!--                        <div class="input-group">-->
+<!--                            <div class="input-group-addon"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></div>-->
+<!--                            <input type="text" class="form-control" id="pd-form-username" placeholder="{:__('Username')}" name="username" autocomplete="off" value="" data-rule="{:__('Username')}:required;username"/>-->
+<!--                        </div>-->
+
+<!--                        <div class="input-group">-->
+<!--                            <div class="input-group-addon"><span class="glyphicon glyphicon-lock" aria-hidden="true"></span></div>-->
+<!--                            <input type="password" class="form-control" id="pd-form-password" placeholder="{:__('Password')}" name="password" autocomplete="off" value="" data-rule="{:__('Password')}:required;password"/>-->
+<!--                        </div>-->
+<!--                        &lt;!&ndash;@CaptchaBegin&ndash;&gt;-->
+<!--                        {if $Think.config.fastadmin.login_captcha}-->
+<!--                        <div class="input-group">-->
+<!--                            <div class="input-group-addon"><span class="glyphicon glyphicon-option-horizontal" aria-hidden="true"></span></div>-->
+<!--                            <input type="text" name="captcha" class="form-control" placeholder="{:__('Captcha')}" data-rule="{:__('Captcha')}:required;length({$Think.config.captcha.length})" autocomplete="off"/>-->
+<!--                            <span class="input-group-addon" style="padding:0;border:none;cursor:pointer;">-->
+<!--                                    <img src="{:rtrim('__PUBLIC__', '/')}/index.php?s=/captcha" width="100" height="30" onclick="this.src = '{:rtrim('__PUBLIC__', '/')}/index.php?s=/captcha&r=' + Math.random();"/>-->
+<!--                            </span>-->
+<!--                        </div>-->
+<!--                        {/if}-->
+<!--                        &lt;!&ndash;@CaptchaEnd&ndash;&gt;-->
+<!--                        {if $keeyloginhours>0}-->
+<!--                        <div class="form-group checkbox">-->
+<!--                            <label class="inline" for="keeplogin" data-toggle="tooltip" title="{:__('The duration of the session is %s hours', $keeyloginhours)}">-->
+<!--                                <input type="checkbox" name="keeplogin" id="keeplogin" value="1"/>-->
+<!--                                {:__('Keep login')}-->
+<!--                            </label>-->
+<!--                        </div>-->
+<!--                        {/if}-->
+<!--                        <div class="form-group">-->
+<!--                            <button type="submit" class="btn btn-success btn-lg btn-block" style="background:#708eea;">{:__('Sign in')}</button>-->
+<!--                        </div>-->
+<!--                        &lt;!&ndash;@AdminLoginFormEnd&ndash;&gt;-->
+<!--                    </form>-->
+<!--                </div>-->
+<!--            </div>-->
+<!--        </div>-->
+<!--    </div>-->
+<!--</div>-->
+<!--{include file="common/script" /}-->
+<!--</body>-->
+<!--</html>-->

+ 170 - 103
application/admin/view/qcode_add/index.html

@@ -148,8 +148,11 @@
             <tr>
                 <th>销售订单号</th>
                 <th>工单编号</th>
-                <th>产品名称</th>
-                <th>结存数量</th>
+                <th>成品编码</th>
+                <th>成品名称</th>
+                <th>入库数</th>
+                <th>发货数</th>
+                <th>剩余数</th>
             </tr>
             </thead>
             <tbody id="tableBody">
@@ -167,16 +170,15 @@
     </fieldset>
 
     <fieldset style="width: 1200px;margin:10px 10px 0px 10px ;padding: 10px 10px 0px 10px ;border: solid thin #c0c0c0">
-        <legend  style="padding: 0px;margin: 0px">托盘参数</legend>
+<!--        <legend  style="padding: 0px;margin: 0px">托盘参数</legend>-->
         <table style="width: 100%;border: thin #ddd dashed;">
-            <caption style="padding: 0px;margin: 0px">标签序列配置</caption>
-            <tr>
-                <th style="width: 40%">字段</th>
-                <th>值</th>
-            </tr>
-
+<!--            <caption style="padding: 0px;margin: 0px">标签序列配置</caption>-->
+<!--            <tr>-->
+<!--                <th style="width: 40%">字段</th>-->
+<!--                <th>值</th>-->
+<!--            </tr>-->
             <tr>
-                <td><span>生产批次号</span></td>
+                <td><span>生产批次号/工单编号</span></td>
                 <td style="padding: 5px 0px 0px 5px"><input type="text" disabled class="form-control" placeholder="选择产品信息获取" id="c-batch" name="batch" style="width: 360px;"></td>
             </tr>
             <tr>
@@ -184,61 +186,86 @@
                 <td style="padding: 5px 0px 0px 5px"><input type="text" disabled class="form-control" placeholder="选择产品信息获取" id="c-order_ddbh" name="order_ddbh" style="width: 360px;"></td>
             </tr>
             <tr>
-                <td><span>工单编号</span></td>
-                <td style="padding: 5px 0px 0px 5px"><input type="text" disabled class="form-control" placeholder="选择产品信息获取" id="c-gdbh" name="gdbh" style="width: 360px;"></td>
+                <td><span>成品编码</span></td>
+                <td style="padding: 5px 0px 0px 5px"><input type="text" disabled class="form-control" placeholder="选择产品信息获取" id="c-cpbm" name="cpbm" style="width: 360px;"></td>
             </tr>
             <tr>
-                <td><span>品名称</span></td>
+                <td><span>品名称</span></td>
                 <td style="padding: 5px 0px 0px 5px"><input type="text" disabled class="form-control" placeholder="选择产品信息获取" id="c-cpmc" name="cpmc" style="width: 360px;"></td>
             </tr>
+            <tr style="display: none">
+                <td><span>入库数</span></td>
+                <td style="padding: 5px 0px 0px 5px"><input type="text"  disabled class="form-control" placeholder="选择产品信息获取" id="c-sl" name="sl" style="width: 360px;"></td>
+            </tr>
+            <tr style="display: none">
+                <td><span>发货数</span></td>
+                <td style="padding: 5px 0px 0px 5px"><input type="text" disabled class="form-control" placeholder="选择产品信息获取" id="c-total_chu_quantity" name="total_chu_quantity" style="width: 360px;"></td>
+            </tr>
             <tr>
-                <td><span>结存数量</span></td>
-                <td style="padding: 5px 0px 0px 5px"><input type="text" disabled class="form-control" placeholder="选择产品信息获取" id="c-sl" name="sl" style="width: 360px;"></td>
+                <td><span>剩余数</span></td>
+                <td style="padding: 5px 0px 0px 5px"><input type="text" disabled class="form-control" placeholder="选择产品信息获取" id="c-remaining_quantity" name="remaining_quantity" style="width: 360px;"></td>
             </tr>
-<!--            <tr>-->
-<!--                <td><span>产品代号</span></td>-->
-<!--                <td style="padding: 5px 0px 0px 5px"><input type="text" disabled class="form-control" id="c-cpdh" name="cpdh" style="width: 360px;"></td>-->
-<!--            </tr>-->
+
             <tr class="xiang">
-                <td><span>总数量(张)</span></td>
-                <td style="padding: 5px 0px 0px 5px"><input type="text" name="number" id="c-number" data-rule="required"></td>
+                <td><span>本次打包数</span></td>
+                <td style="padding: 5px 0px 0px 5px"><input type="text" oninput="validateAndCalculate()" name="number" id="c-number" data-rule="required"></td>
             </tr>
             <tr>
-                <td><span>选择单位</span></td>
+                <td><span>选择单位(个/套/张)</span></td>
                 <td style="padding: 5px 0px 0px 5px">
                     <select id="danwei" name="danwei" style="width: 200px;padding: 5px;">
-                        <option value="1">张</option>
-                        <!--<option value="2">卷</option>-->
+                        <option value="1">个</option>
+                        <option value="2">套</option>
+                        <option value="3">张</option>
                     </select>
                 </td>
             </tr>
+
+            <tr class="xiang">
+                <td><span>每一箱数量</span></td>
+                <td style="padding: 5px 0px 0px 5px">
+                    <select name="box_number" id="box_number" oninput="calculateTotalBoxes()" style="width:200px;padding: 5px">
+                        <option value="1">========选择=========</option>
+                        <option value="20">========20========</option>
+                        <option value="25">========25========</option>
+                        <option value="25">========40========</option>
+                        <option value="50">========50========</option>
+                        <option value="250">========250========</option>
+                        <option value="500">========500========</option>
+                        <option value="1000">=======1000========</option>
+                        <option value="2000">=======2000========</option>
+                        <option value="2500">=======2500========</option>
+                        <option value="3000">=======3000========</option>
+                        <option value="4000">=======4000========</option>
+                        <option value="5000">=======5000========</option>
+                        <option value="8000">=======8000========</option>
+                        <option value="10000">=======10000========</option>
+                        <option value="24000">=======24000========</option>
+                    </select>
+                </td>
+            </tr>
+
             <tr class="xiang">
                 <td><span>一托盘多少箱(箱*层)</span></td>
                 <td style="padding: 5px 0px 0px 5px">
                     <input type="text" id="tray_num"     oninput="calculateTotalBoxes()" style="width: 100px;" min="1" step="1" > 箱/层 ×
-                    <input type="text" id="box_number"   oninput="calculateTotalBoxes()" style="width: 100px;"  min="1" step="1" > 层/托 =
+                    <input type="text" id="tray_number"  oninput="calculateTotalBoxes()" style="width: 100px;"  min="1" step="1" > 层/托 =
                     <input type="text" id="total_boxes"  style="width: 100px;" disabled> 箱/托
                     <span style="color: red;font-size: 23px;"> *</span>
                 </td>
             </tr>
+
             <tr>
-                <td><span>每箱数(个或张)</span></td>
-                <td style="padding: 5px 0px 0px 5px"><input type="text" id="c-small_num" disabled name="small_num" data-rule="required"></td>
-            </tr>
-            <tr>
-                <td><span>每层箱数</span></td>
-                <td style="padding: 5px 0px 0px 5px">
-                    <input type="text" id="boxes_per_layer" name="boxes_per_layer" data-rule="required" disabled>
-                </td>
+                <td><span>本次打包托盘数(大件数)</span></td>
+                <td style="padding: 5px 0px 0px 5px"><input type="text" id="c-box_num" disabled name="box_num" ></td>
             </tr>
             <tr>
-                <td><span>每托层数</span></td>
-                <td style="padding: 5px 0px 0px 5px">
-                    <input type="text" id="layers_per_pallet" name="layers_per_pallet" data-rule="required" disabled>
-                </td>
+                <td><span>总箱数</span></td>
+                <td style="padding: 5px 0px 0px 5px"><input type="text" id="c-small_num" disabled name="small_num" data-rule="required"></td>
             </tr>
+
             <tr>
-                <td><span>每托高度</span></td>
+                <td><span>每托高度(米/m)</span></td>
                 <td style="padding: 5px 0px 0px 5px; position: relative;">
                     <input type="text" id="pallet_height" name="pallet_height"
                            style="width: 160px;" placeholder="" autocomplete="off">
@@ -301,56 +328,41 @@
                     <span style="color: red;font-size: 23px;"> *</span>
                 </td>
             </tr>
-            <tr>
-                <td><span>大件(总托数)</span></td>
-                <td style="padding: 5px 0px 0px 5px"><input type="text" id="c-box_num" name="box_num" ><span style="color: red;font-size: 23px;"> *</span></td>
-            </tr>
             <tr>
                 <td><span>生产日期</span></td>
                 <td style="padding: 5px 0px 0px 5px">
                     <input class="" id="c-manufacture_date" value="{:date('Y-m-d')}" style="width: 180px;" name="manufacture_date" type="date" >
-                    <span style="color: red;font-size: 23px;"> *</span>
                 </td>
             </tr>
             <tr>
                 <td><span>打码日期</span></td>
                 <td style="padding: 5px 0px 0px 5px">
                     <input class="" type="date" id="c-print_date" style="width: 180px"  name="print_date" value="{:date('Y-m-d')}">
-                    <span style="color: red;font-size: 23px;"> *</span>
                 </td>
             </tr>
-<!--            <tr>-->
-<!--                <td><span>生产批次号</span></td>-->
-<!--                <td>-->
-<!--                    <div style="display: flex; align-items: center;">-->
-<!--                        <input type="text" name="batch" id="c-batch" data-rule="required" disabled>-->
-<!--                        &lt;!&ndash;                        <button type="button" id="reset_batch" style="margin-left: 10px;">重置批次号</button>&ndash;&gt;-->
-<!--                        <span style="color: red; font-size: 23px; margin-left: 5px;">*</span>-->
-<!--                    </div>-->
-<!--                </td>-->
-<!--            </tr>-->
             <tr>
                 <td><span>大件开始流水号</span></td>
                 <td>
                     <div style="display: flex; align-items: center;">
-                        <input type="text" name="big_liushui" id="c-big_liushui" data-rule="required" disabled>
+                        <input type="text" name="big_liushui" id="c-big_liushui" disabled>
                         <button type="button" id="reset_liushui" style="margin-left: 10px;">重置流水号</button>
-                        <span style="color: red; font-size: 23px; margin-left: 5px;">*</span>
                     </div>
                 </td>
             </tr>
+
             <tr>
                 <td><span>大件重量(单位千克数值)有小数时保留2位小数</span></td>
                 <td style="padding: 5px 0px 0px 5px"><input type="text" id="c-big_weight" name="big_weight" data-rule="required"><span style="color: red;font-size: 23px;"> *</span></td>
             </tr>
             <tr>
                 <td><span>小件开始流水号</span></td>
-                <td style="padding: 5px 0px 0px 5px"><input type="text" id="c-small_start_liushui" name="small_start_liushui" value="1" data-rule="required"><span style="color: red;font-size: 23px;"> *</span></td>
+                <td style="padding: 5px 0px 0px 5px"><input type="text" id="c-small_start_liushui" name="small_start_liushui" value="1" ></td>
             </tr>
             <tr>
                 <td><span>小件重量(单位克 数值)</span></td>
                 <td style="padding: 5px 0px 0px 5px"><input type="text" id="c-small_weight" name="small_weight" data-rule="required"><span style="color: red;font-size: 23px;"> *</span></td>
             </tr>
+
         </table>
     </fieldset>
 
@@ -371,56 +383,64 @@
     $(function() {
         const $input = $('#pallet_height');
         const $list = $('#height-suggestions');
-
         // 显示下拉菜单
         $input.on('focus click', function () {
             $list.show();
         });
-
         // 点击选项
         $list.on('click', 'li', function () {
             $input.val($(this).text());
             $list.hide();
         });
-
         // 失焦隐藏
         $input.on('blur', function () {
             setTimeout(() => $list.hide(), 100); // 延迟防止刚点 li 时被关闭
         });
     });
 
-        let currentPage = 1;
-        let limit = 10;
-        let total = 0;
-        let selectedRow = null;
+    let currentPage = 1;
+    let limit = 10;
+    let total = 0;
+    let selectedRow = null;
+
+    // 绑定全局选择函数
+    window.selectRow = function (row) {
+        console.log('行数据:', row);
 
-        // 绑定全局选择函数
-        window.selectRow = function (row) {
         if (selectedRow) {
-        $(selectedRow).removeClass('selected-row');
-    }
+            $(selectedRow).removeClass('selected-row');
+        }
         $(row).addClass('selected-row');
         selectedRow = row;
 
         const rowData = {
-        gdbh: $(row).data('gdbh'),
-        order_ddbh: $(row).data('order_ddbh'),
-        // cpdh: $(row).data('cpdh'),
-        cpmc: $(row).data('cpmc'),
-        sl: $(row).data('sl')
-    };
+            gdbh: $(row).data('gdbh'),
+            order_ddbh: $(row).data('order_ddbh'),
+            cpbm: $(row).data('cpbm'),
+            cpmc: $(row).data('cpmc'),
+            sl: $(row).data('sl'),
+            total_chu_quantity: $(row).data('total_chu_quantity'),
+            remaining_quantity: $(row).data('remaining_quantity')
+        };
 
         console.log('选中的行数据:', rowData);
 
+        $('#c-batch').val(rowData['gdbh']);
         $('#c-order_ddbh').val(rowData['order_ddbh']);
-        $('#c-gdbh').val(rowData['gdbh']);
-        // $('#c-cpdh').val(rowData['cpdh']);
+        $('#c-cpbm').val(rowData['cpbm']);
         $('#c-cpmc').val(rowData['cpmc']);
         $('#c-sl').val(rowData['sl']);
-        $('#c-batch').val(rowData['gdbh']);
+        $('#c-total_chu_quantity').val(rowData['total_chu_quantity']);
+
+        // 新增这段判断
+        if (parseFloat(rowData['remaining_quantity']) >= 0) {
+            $('#c-remaining_quantity').val(rowData['remaining_quantity']);
+        } else {
+            $('#c-remaining_quantity').val('');
+        }
     };
 
-        function loadData(page = 1, keyword = '') {
+    function loadData(page = 1, keyword = '') {
         $.ajax({
             method: "GET",
             url: "/qcode_add/product", // 替换成你的接口路径
@@ -445,17 +465,26 @@
                     return;
                 }
 
+                $('#c-big_liushui').val(1)
+
                 data.forEach(item => {
-                    const tr = $(`
+                const tr = $(`
                         <tr onclick="selectRow(this)"
-                            data-order_ddbh="${item.order_ddbh || ''}"
-                            data-gdbh="${item.gdbh || ''}"
-                            data-cpmc="${item.cpmc || ''}"
-                            data-sl="${item.sl || ''}">
-                            <td>${item.order_ddbh || ''}</td>
-                            <td>${item.gdbh || ''}</td>
-                            <td>${item.cpmc || ''}</td>
-                            <td>${item.sl || ''}</td>
+                            data-order_ddbh="${item.order_ddbh}"
+                            data-gdbh="${item.gdbh}"
+                            data-cpbm="${item.cpbm}"
+                            data-cpmc="${item.cpmc}"
+                            data-sl="${item.sl}"
+                            data-total_chu_quantity="${item.total_chu_quantity}"
+                            data-remaining_quantity="${item.remaining_quantity}">
+
+                            <td>${item.order_ddbh}</td>
+                            <td>${item.gdbh}</td>
+                            <td>${item.cpbm}</td>
+                            <td>${item.cpmc}</td>
+                            <td>${item.sl}</td>
+                            <td>${item.total_chu_quantity}</td>
+                            <td>${item.remaining_quantity}</td>
                         </tr>`);
                     tbody.append(tr);
                 });
@@ -564,24 +593,62 @@
         }
     });
 
+    function validateAndCalculate() {
+        let input = document.getElementById('c-number');
+        let remainingInput = document.getElementById('c-remaining_quantity');
+        let max = parseInt(remainingInput.value, 10);
+
+        // 判断库存是否为空或非法
+        if (!remainingInput.value || isNaN(max)) {
+            input.value = '';
+            layer.msg('库存数量为空,无法输入!', {icon: 2});
+            return;
+        }
+
+        // 只允许输入数字(去除字母、负号、小数点等)
+        input.value = input.value.replace(/[^\d]/g, '');
+        let val = parseInt(input.value, 10);
+
+        // 判断是否超出库存
+        if (!isNaN(val) && val > max) {
+            layer.msg('输入值不能超过库存数量!', {icon: 0});
+            input.value = max;
+        }
+
+        // 调用原始计算逻辑
+        calculateTotalBoxes();
+    }
+
+
     //一托盘多少箱
     function calculateTotalBoxes() {
-        const boxesPerLayer = parseInt(document.getElementById("tray_num").value) || 0;
-        const layersPerTray = parseInt(document.getElementById("box_number").value) || 0;
-        const totalBoxes = boxesPerLayer * layersPerTray;
-        document.getElementById("total_boxes").value = totalBoxes;
-
-        var trayNum1 = $('#tray_num').val();
-        console.log('箱:', trayNum1);
-        $('#boxes_per_layer').val(trayNum1 ? trayNum1 : '');
-
-        var trayNum2 = $('#box_number').val();
-        console.log('层:', trayNum2);
-        $('#layers_per_pallet').val(trayNum2 ? trayNum2 : '');
-
-        var trayNum3 = $('#total_boxes').val();
-        console.log('总箱数:', trayNum3);
-        $('#c-small_num').val(trayNum3 ? trayNum3 : '');
+        // 获取用户输入
+        const number = parseInt(document.getElementById("c-number").value || 0);         // 本次打包数
+        const box_number = parseInt(document.getElementById("box_number").value || 0);   // 每一箱数量(个)
+        const tray_num = parseInt(document.getElementById("tray_num").value || 0);       // 每层的箱数(箱/层)
+        const tray_layer = parseInt(document.getElementById("tray_number").value || 0);  // 层数(层/托)
+        // 计算每托盘能放多少箱
+        const boxes_per_tray = tray_num * tray_layer;
+        document.getElementById("total_boxes").value = boxes_per_tray; // 自动填充 total_boxes(箱/托)字段
+        // 计算总箱数(向上取整)
+        // const totalBoxCount = Math.ceil(number / box_number);
+        const totalBoxCount = Math.ceil(number / box_number);
+        // 计算托盘数(向上取整)
+        const trayCount = Math.ceil(totalBoxCount / boxes_per_tray);
+
+        console.log("本次打包数:", number);
+        console.log("每箱数量:", box_number);
+        console.log("总箱数:", totalBoxCount);
+        console.log("每托盘箱数:", boxes_per_tray);
+        console.log("本次托盘数:", trayCount);
+        if(trayCount == 'Infinity'){
+            document.getElementById("c-box_num").value = 0;
+        }else{
+            document.getElementById("c-box_num").value = trayCount;
+            document.getElementById("c-small_num").value = totalBoxCount;
+        }
+        // 填入最终托盘数(大件数)
+        // $('#c-box_num').val(trayCount);
     }
 
 </script>

+ 3 - 1
application/admin/view/user/user/chuku.html

@@ -23,7 +23,8 @@
                 <th>序号</th>
                 <th>销售订单号</th>
                 <th>工单编号</th>
-                <th>产品名称</th>
+                <th>成品编码</th>
+                <th>成品名称</th>
                 <th>出库数量</th>
                 <th>出库时间</th>
             </tr>
@@ -34,6 +35,7 @@
                 <td>{$k}</td>
                 <td>{$row.order_ddbh}</td>
                 <td>{$row.gdbh}</td>
+                <td>{$row.cpbm}</td>
                 <td>{$row.cpmc}</td>
                 <td>{$row.total_chu_quantity}</td>
                 <td>{$row.created_at}</td>

+ 3 - 1
application/admin/view/user/user/ruku.html

@@ -23,7 +23,8 @@
                 <th>序号</th>
                 <th>销售订单号</th>
                 <th>工单编号</th>
-                <th>产品名称</th>
+                <th>成品编码</th>
+                <th>成品名称</th>
                 <th>入库数量</th>
                 <th>入库时间</th>
             </tr>
@@ -34,6 +35,7 @@
                 <td>{$k}</td>
                 <td>{$row.order_ddbh}</td>
                 <td>{$row.gdbh}</td>
+                <td>{$row.cpbm}</td>
                 <td>{$row.cpmc}</td>
                 <td>{$row.sl}</td>
                 <td>{$row.sj}</td>