Răsfoiți Sursa

面料入库优化

unknown 1 zi în urmă
părinte
comite
45fc06a8bb
1 a modificat fișierele cu 81 adăugiri și 49 ștergeri
  1. 81 49
      application/api/controller/WorkOrderSpotCheck.php

+ 81 - 49
application/api/controller/WorkOrderSpotCheck.php

@@ -2980,33 +2980,51 @@ class WorkOrderSpotCheck extends Api{
             $this->error('请求错误');
         }
 
-        $param = Request::instance()->post();
+        $param = $this->request->post();
+        if (empty($param)) {
+            $input = file_get_contents('php://input');
+            $decoded = json_decode($input, true);
+            if (is_array($decoded)) {
+                $param = $decoded;
+            }
+        }
         if (empty($param)) {
             $this->error('参数错误');
         }
+        // 单条对象转为列表
+        if (!isset($param[0]) || !is_array($param[0])) {
+            if (isset($param['物料编码']) || isset($param['order_id'])) {
+                $param = [$param];
+            }
+        }
 
         \db()->startTrans();
 
         try {
             $sysRq = date('Y-m-d H:i:s');
 
-            // 获取最大批次号(兼容 GDGL 前缀)
             $lastBatch = \db('物料_库存')
-                ->where('批次号', 'like', 'GDGL%')
-                ->orderRaw('CAST(SUBSTRING(批次号, 5) AS UNSIGNED) DESC')
+                ->where('批次号', 'like', 'PCH%')
+                ->orderRaw('CAST(SUBSTRING(批次号, 4) AS UNSIGNED) DESC')
                 ->value('批次号');
 
-            $currentNumber = $lastBatch ? intval(substr($lastBatch, 4)) : 0;
+            $currentNumber = 0;
+            if (!empty($lastBatch) && preg_match('/^PCH(\d+)$/i', $lastBatch, $match)) {
+                $currentNumber = intval($match[1]);
+            }
 
-            $insertInventory = [];
-            $insertLogs = [];
+            $processed = 0;
 
             foreach ($param as $item) {
-                if (empty($item['物料编码']) || empty($item['关联编号']) || !isset($item['number']) || !is_numeric($item['number']) || $item['number'] <= 0) {
+                if (!is_array($item)) {
+                    continue;
+                }
+                if (empty($item['物料编码']) || empty($item['关联编号']) || !isset($item['number']) || !is_numeric($item['number']) || floatval($item['number']) <= 0) {
                     continue;
                 }
 
-                $qty = intval($item['number']);
+                $qty = floatval($item['number']);
+                $processed++;
 
                 // 唯一KEY:物料编码 + 物料名称 + 关联号 + 未删除
                 $existingBatch = \db('物料_库存')
@@ -3016,83 +3034,97 @@ class WorkOrderSpotCheck extends Api{
                     ->whereNull('Mod_rq')
                     ->value('批次号');
 
+                if (!$existingBatch && !empty($item['批次号'])) {
+                    $existingBatch = \db('物料_库存')
+                        ->where('批次号', $item['批次号'])
+                        ->whereNull('Mod_rq')
+                        ->value('批次号');
+                }
+
                 if ($existingBatch) {
-                    $updateData = [
-                        '入仓总量'   => Db::raw('入仓总量 + ' . $qty),
-                        '库存数量'   => Db::raw('库存数量 + ' . $qty),
-                        '实际门幅'   => $item['实际门幅'],
+                    $inventory = \db('物料_库存')
+                        ->where('批次号', $existingBatch)
+                        ->field('库存数量,入仓总量')
+                        ->find();
+
+                    $inventoryData = [
+                        '关联号'     => $item['关联编号'],
+                        '物料编号'   => $item['物料编码'],
+                        '物料名称'   => $item['物料名称'],
+                        '入仓总量'   => (float)$inventory['入仓总量'] + $qty,
+                        '库存数量'   => (float)$inventory['库存数量'] + $qty,
+                        '实际门幅'   => $item['实际门幅'] ?? '',
                         'departname' => $item['departname'],
                         'remark'     => $item['remark'] ?? '',
+                        'sys_id'     => $item['sys_id'],
                         'sys_rq'     => $sysRq,
                     ];
                     $sql = \db('物料_库存')
                         ->where('批次号', $existingBatch)
                         ->fetchSql(true)
-                        ->update($updateData);
+                        ->update($inventoryData);
                     \db()->query($sql);
 
                     $BatchNumber = $existingBatch;
                 } else {
                     $currentNumber++;
-                    $BatchNumber = 'GDGL' . $currentNumber;
+                    $BatchNumber = 'PCH' . $currentNumber;
 
-                    $insertInventory[] = [
+                    $inventoryData = [
                         '批次号'     => $BatchNumber,
                         '关联号'     => $item['关联编号'],
                         '物料编号'   => $item['物料编码'],
                         '物料名称'   => $item['物料名称'],
                         '入仓总量'   => $qty,
                         '库存数量'   => $qty,
-                        '单位'       => $item['单位'],
-                        '实际门幅'   => $item['实际门幅'],
+                        '单位'       => $item['单位'] ?? '',
+                        '实际门幅'   => $item['实际门幅'] ?? '',
                         '状态'       => 1,
                         'departname' => $item['departname'],
                         'remark'     => $item['remark'] ?? '',
                         'sys_id'     => $item['sys_id'],
                         'sys_rq'     => $sysRq,
                     ];
+                    $sql = \db('物料_库存')->fetchSql(true)->insert($inventoryData);
+                    \db()->query($sql);
                 }
 
-                $insertLogs[] = [
-                    '批次号'           => $BatchNumber,
-                    'order_id'         => $item['order_id'],
-                    '款号'             => $item['款号'],
-                    '物料编码'         => $item['物料编码'],
-                    '物料名称'         => $item['物料名称'],
-                    '库存数量'         => $qty,
-                    'number'           => $qty,
-                    'rq'               => $item['rq'],
-                    'name'             => $item['name'],
-                    'sys_id'           => $item['sys_id'],
-                    'receipt_number'   => $item['receipt_number'],
-                    'departname'       => $item['departname'],
-                    'remark'           => $item['remark'] ?? '',
-                    '客户编号'         => $item['客户编号'],
-                    '款式'             => $item['款式'],
-                    'sys_rq'           => $sysRq,
+                $reportData = [
+                    '批次号'         => $BatchNumber,
+                    'order_id'       => $item['order_id'],
+                    '款号'           => $item['款号'],
+                    '物料编码'       => $item['物料编码'],
+                    '物料名称'       => $item['物料名称'],
+                    '库存数量'       => $qty,
+                    'number'         => $qty,
+                    'rq'             => $item['rq'],
+                    'name'           => $item['name'],
+                    'sys_id'         => $item['sys_id'],
+                    'receipt_number' => $item['receipt_number'],
+                    'departname'     => $item['departname'],
+                    'remark'         => $item['remark'] ?? '',
+                    '客户编号'       => $item['客户编号'],
+                    '款式'           => $item['款式'],
+                    'sys_rq'         => $sysRq,
                 ];
-            }
-
-            if (empty($insertLogs)) {
-                \db()->rollback();
-                $this->error('无有效入库数据');
-            }
-
-            if (!empty($insertInventory)) {
-                $sql = \db('物料_库存')->fetchSql(true)->insertAll($insertInventory);
+                $sql = \db('库存_出入库明细')->fetchSql(true)->insert($reportData);
                 \db()->query($sql);
             }
 
-            $sql = \db('库存_出入库明细')->fetchSql(true)->insertAll($insertLogs);
-            \db()->query($sql);
+            if ($processed === 0) {
+                throw new \Exception('无有效入库数据');
+            }
 
             \db()->commit();
-            $this->success('入库成功');
-
+        } catch (\think\exception\HttpResponseException $e) {
+            throw $e;
         } catch (\Exception $e) {
             \db()->rollback();
-            $this->error('操作失败:' . $e->getMessage());
+            $msg = trim($e->getMessage());
+            $this->error('操作失败:' . ($msg !== '' ? $msg : '未知错误'));
         }
+
+        $this->success('入库成功');
     }
 
 //    public function inputStash()