Explorar el Código

员工超节损汇总优化

qiuenguang hace 10 meses
padre
commit
69606ec6e7
Se han modificado 1 ficheros con 91 adiciones y 14 borrados
  1. 91 14
      application/api/controller/OrderSuperLoss.php

+ 91 - 14
application/api/controller/OrderSuperLoss.php

@@ -1164,6 +1164,85 @@ class OrderSuperLoss extends Api
      * @throws \think\db\exception\ModelNotFoundException
      * @throws \think\db\exception\ModelNotFoundException
      * @throws \think\exception\DbException
      * @throws \think\exception\DbException
      */
      */
+//    public function EmployeeDataCollect()
+//    {
+//        if ($this->request->isPost() === false) {
+//            $this->error('请求错误');
+//        }
+//        $params = $this->request->post();
+//        if (empty($params)) {
+//            $this->error('参数错误');
+//        }
+//        $date = date('Ym', strtotime($params['date']));
+//        $list = \db('工单_质量考核汇总')
+//            ->alias('a')
+//            ->join('工单_印件资料 b', 'a.Gy0_gdbh = b.Yj_Gdbh AND a.印件及工序 = b.yj_Yjno')
+//            ->join('成品入仓 c','a.Gy0_gdbh = c.jjcp_gdbh and a.印件及工序 = c.jjcp_yjno')
+//            ->where('a.sys_ny', $date)
+//            // 修改质量考核条件为 0 或者 NULL
+//            ->where(function ($query) {
+//                $query->where('b.质量考核', 0)
+//                    ->whereOr('b.质量考核',null); // 检查为空
+//            })
+//            ->where('c.jjcp_smb','like','末%')
+//            ->select();
+//        $data = [];
+//
+//        foreach ($list as $item) {
+//            $workshop = $item["车间名称"] ?? '';
+//
+//            for ($i = 1; $i <= 8; $i++) {
+//                $bh = $item["sczl_bh{$i}"] ?? '';
+//                $xm = $item["Xm{$i}"] ?? '';
+//                $rateValue = $item["sczl_rate{$i}"] ?? '';
+//
+//                // 关键修改:rate 为空时默认为 1
+//                $rate = ($rateValue === '') ? 1 : (float)$rateValue;
+//
+//                if (!empty($bh)) {
+//                    $je = round((float)$item["CjsJe"] * $rate, 4);
+//
+//                    $data[] = [
+//                        "bh" => $bh,
+//                        "xm" => $xm,
+//                        "je" => (float)number_format($je, 4, '.', ''),
+//                        "cj" => $workshop
+//                    ];
+//                }
+//            }
+//        }
+//        $result = [];
+//
+//        foreach ($data as $item) {
+//            $cj = $item['cj'];
+//            $bh = $item['bh'];
+//            $je = round($item['je'], 2); // 保留两位小数
+//
+//            // 如果 cj 不存在于结果数组中,则初始化
+//            if (!isset($result[$cj])) {
+//                $result[$cj] = []; // 初始化车间数据
+//            }
+//
+//            // 如果 bh 已经存在于当前车间,累加 je
+//            if (isset($result[$cj][$bh])) {
+//                $result[$cj][$bh]['je'] += (float)$je; // 累加 je
+//            } else {
+//                // 如果 bh 不存在,则添加新的记录
+//                $result[$cj][$bh] = [
+//                    'bh' => $bh,
+//                    'xm' => $item['xm'],
+//                    'je' => (float)$je, // 保留为浮点数格式
+//                    'cj' => $cj
+//                ];
+//            }
+//        }
+//
+//        // 将每个车间的子数组变成索引数组
+//        foreach ($result as $cjName => &$items) {
+//            $items = array_values($items); // 重建索引
+//        }
+//        $this->success('成功', $result);
+//    }
     public function EmployeeDataCollect()
     public function EmployeeDataCollect()
     {
     {
         if ($this->request->isPost() === false) {
         if ($this->request->isPost() === false) {
@@ -1177,14 +1256,13 @@ class OrderSuperLoss extends Api
         $list = \db('工单_质量考核汇总')
         $list = \db('工单_质量考核汇总')
             ->alias('a')
             ->alias('a')
             ->join('工单_印件资料 b', 'a.Gy0_gdbh = b.Yj_Gdbh AND a.印件及工序 = b.yj_Yjno')
             ->join('工单_印件资料 b', 'a.Gy0_gdbh = b.Yj_Gdbh AND a.印件及工序 = b.yj_Yjno')
-            ->join('成品入仓 c','a.Gy0_gdbh = c.jjcp_gdbh and a.印件及工序 = c.jjcp_yjno')
+            ->join('成品入仓 c', 'a.Gy0_gdbh = c.jjcp_gdbh AND a.印件及工序 = c.jjcp_yjno')
             ->where('a.sys_ny', $date)
             ->where('a.sys_ny', $date)
-            // 修改质量考核条件为 0 或者 NULL
             ->where(function ($query) {
             ->where(function ($query) {
                 $query->where('b.质量考核', 0)
                 $query->where('b.质量考核', 0)
-                    ->whereOr('b.质量考核',null); // 检查为空
+                    ->whereOr('b.质量考核', null);
             })
             })
-            ->where('c.jjcp_smb','like','末%')
+            ->where('c.jjcp_smb', 'like', '末%')
             ->select();
             ->select();
         $data = [];
         $data = [];
 
 
@@ -1196,8 +1274,8 @@ class OrderSuperLoss extends Api
                 $xm = $item["Xm{$i}"] ?? '';
                 $xm = $item["Xm{$i}"] ?? '';
                 $rateValue = $item["sczl_rate{$i}"] ?? '';
                 $rateValue = $item["sczl_rate{$i}"] ?? '';
 
 
-                // 关键修改:rate 为空时默认为 1
-                $rate = ($rateValue === '') ? 1 : (float)$rateValue;
+                // 关键优化:如果分配比例为 0 或空,默认为 1
+                $rate = (empty($rateValue) || (float)$rateValue == 0) ? 1 : (float)$rateValue;
 
 
                 if (!empty($bh)) {
                 if (!empty($bh)) {
                     $je = round((float)$item["CjsJe"] * $rate, 4);
                     $je = round((float)$item["CjsJe"] * $rate, 4);
@@ -1211,6 +1289,7 @@ class OrderSuperLoss extends Api
                 }
                 }
             }
             }
         }
         }
+
         $result = [];
         $result = [];
 
 
         foreach ($data as $item) {
         foreach ($data as $item) {
@@ -1218,29 +1297,27 @@ class OrderSuperLoss extends Api
             $bh = $item['bh'];
             $bh = $item['bh'];
             $je = round($item['je'], 2); // 保留两位小数
             $je = round($item['je'], 2); // 保留两位小数
 
 
-            // 如果 cj 不存在于结果数组中,则初始化
             if (!isset($result[$cj])) {
             if (!isset($result[$cj])) {
-                $result[$cj] = []; // 初始化车间数据
+                $result[$cj] = [];
             }
             }
 
 
-            // 如果 bh 已经存在于当前车间,累加 je
             if (isset($result[$cj][$bh])) {
             if (isset($result[$cj][$bh])) {
-                $result[$cj][$bh]['je'] += (float)$je; // 累加 je
+                $result[$cj][$bh]['je'] += (float)$je;
             } else {
             } else {
-                // 如果 bh 不存在,则添加新的记录
                 $result[$cj][$bh] = [
                 $result[$cj][$bh] = [
                     'bh' => $bh,
                     'bh' => $bh,
                     'xm' => $item['xm'],
                     'xm' => $item['xm'],
-                    'je' => (float)$je, // 保留为浮点数格式
+                    'je' => (float)$je,
                     'cj' => $cj
                     'cj' => $cj
                 ];
                 ];
             }
             }
         }
         }
 
 
-        // 将每个车间的子数组变成索引数组
+        // 将每个车间的子数组转换为索引数组
         foreach ($result as $cjName => &$items) {
         foreach ($result as $cjName => &$items) {
-            $items = array_values($items); // 重建索引
+            $items = array_values($items);
         }
         }
+
         $this->success('成功', $result);
         $this->success('成功', $result);
     }
     }