cqp 17 jam lalu
induk
melakukan
f6fddce955
1 mengubah file dengan 97 tambahan dan 39 penghapusan
  1. 97 39
      app/Service/U8ThirtyPartyDatabaseServerService.php

+ 97 - 39
app/Service/U8ThirtyPartyDatabaseServerService.php

@@ -643,7 +643,7 @@ class U8ThirtyPartyDatabaseServerService extends Service
                     ->lockForUpdate()
                     ->first();
 
-                if (!$identity) return [false, "未在 UA_Identity 中找到账套 {$accId} 的 mv 类型配置"];
+                if (!$identity) return [false, "未在 UA_Identity 中找到账套 {$accId} 的领料申请单(mv)类型配置"];
 
                 // 拿到当前的“起始种子”
                 $currentSeed = (int)$identity->iChildId;
@@ -685,100 +685,158 @@ class U8ThirtyPartyDatabaseServerService extends Service
         try {
             $db = $this->safeDb();
 
-            return $db->transaction(function () use ($mainId, $insertData,$db) {
+            $accId = '200';
+            return $db->transaction(function () use ($mainId, $insertData, $db, $accId) {
 
-                // 1. 删除旧明细
+                // 1. 获取并锁定当前的流水号 (cVouchType 为 PuArrival)
+                // 使用 lockForUpdate() 确保在事务结束前,U8 软件或其他脚本拿不到这个号
+                $identity = $db->table('UFSystem.dbo.UA_Identity')
+                    ->where('cAcc_Id', $accId)
+                    ->where('cVouchType', 'PuArrival')
+                    ->lockForUpdate()
+                    ->first();
+
+                if (!$identity) {
+                    return [false, "未在 UA_Identity 中找到账套 {$accId} 的采购到货单(PuArrival)配置"];
+                }
+
+                // 获取当前流水种子
+                $currentSeed = (int)$identity->iChildId;
+
+                // 2. 删除旧明细 (PU_ArrivalVouchs)
                 $db->table('PU_ArrivalVouchs')
                     ->where('ID', $mainId)
                     ->delete();
 
-                // 2. 获取新的起始 Autoid
-                $maxAutoid = $db->table('PU_ArrivalVouchs')
+                // 3. 准备新数据并分配 Autoid
+                $newData = [];
+                foreach ($insertData as $row) {
+                    $currentSeed++; // 种子自增
+
+                    // 这里的 1000000000 是 U8 常用的年度前缀偏移量,请确保与你账套一致
+                    $row['Autoid'] = 1000000000 + $currentSeed;
+                    $row['ID'] = $mainId;
+                    $newData[] = $row;
+                }
+
+                // 4. 插入新明细
+                if (!empty($newData)) {
+                    $db->table('PU_ArrivalVouchs')->insert($newData);
+                }
+
+                // 5. 【关键】同步更新系统流水表,确保 U8 软件开单不撞号
+                $db->statement("
+                UPDATE UFSystem.dbo.UA_Identity 
+                SET iChildId = ? 
+                WHERE cAcc_Id = ? AND cVouchType = 'PuArrival'
+            ", [$currentSeed, $accId]);
+
+                return [true, ''];
+            });
+        } catch (\Throwable $e) {
+            return [false, "采购到货单明细重建失败: " . $e->getMessage()];
+        }
+    }
+
+    public function rebuildBjDetails($mainId, $insertData)
+    {
+        try {
+            $db = $this->safeDb();
+
+            return $db->transaction(function () use ($mainId, $insertData, $db) {
+
+                // 1. 删除旧明细
+                $db->table('QMINSPECTVOUCHERS')
+                    ->where('ID', $mainId)
+                    ->delete();
+
+                $maxAutoid = $db->table('QMINSPECTVOUCHERS')
                         ->max('Autoid') ?? 0;
 
-                // 3. 分配新 Autoid 并插入
-                foreach ($insertData as &$row) {
+                // 过滤掉不可插入的字段,特别是 UFTS
+                foreach ($insertData as $key => $row) {
                     $maxAutoid++;
+
+                    // 彻底清理:移除 UFTS 字段,防止插入报错及 JSON 转换报错
+                    unset($row['UFTS']);
+
                     $row['Autoid'] = $maxAutoid;
+                    $insertData[$key] = $row;
                 }
 
-                $db->table('PU_ArrivalVouchs')
+                // 4. 批量插入
+                $db->table('QMINSPECTVOUCHERS')
                     ->insert($insertData);
 
-                return [true, ''];
+                return [true, '操作成功'];
             });
         } catch (\Throwable $e) {
-            return [false, "重建明细失败: " . $e->getMessage()];
+            // 这里返回错误信息时,确保不包含二进制内容
+            return [false, "重组报检单失败: " . $e->getMessage()];
         }
     }
 
-    public function rebuildLLDetails1($mainId, $insertData)
+    public function rebuildDhDetails1($mainId, $insertData)
     {
         try {
             $db = $this->safeDb();
 
             return $db->transaction(function () use ($mainId, $insertData,$db) {
 
-                // 1. 删除旧明细 (领料申请单子表)
-                $db->table('MaterialAppVouchs')
+                // 1. 删除旧明细
+                $db->table('PU_ArrivalVouchs')
                     ->where('ID', $mainId)
                     ->delete();
 
-                // 2. 获取新的起始 autoid
-                $maxAutoid = $db->table('MaterialAppVouchs')
-                        ->max('AutoID') ?? 0;
+                // 2. 获取新的起始 Autoid
+                $maxAutoid = $db->table('PU_ArrivalVouchs')
+                        ->max('Autoid') ?? 0;
 
-                // 3. 分配新 autoid 并插入
+                // 3. 分配新 Autoid 并插入
                 foreach ($insertData as &$row) {
                     $maxAutoid++;
-                    $row['AutoID'] = $maxAutoid;
+                    $row['Autoid'] = $maxAutoid;
                 }
 
-                $db->table('MaterialAppVouchs')
+                $db->table('PU_ArrivalVouchs')
                     ->insert($insertData);
 
                 return [true, ''];
             });
         } catch (\Throwable $e) {
-            return [false, "重组领料申请单失败: " . $e->getMessage()];
+            return [false, "重建明细失败: " . $e->getMessage()];
         }
     }
 
-    public function rebuildBjDetails($mainId, $insertData)
+    public function rebuildLLDetails1($mainId, $insertData)
     {
         try {
             $db = $this->safeDb();
 
-            return $db->transaction(function () use ($mainId, $insertData, $db) {
+            return $db->transaction(function () use ($mainId, $insertData,$db) {
 
-                // 1. 删除旧明细
-                $db->table('QMINSPECTVOUCHERS')
+                // 1. 删除旧明细 (领料申请单子表)
+                $db->table('MaterialAppVouchs')
                     ->where('ID', $mainId)
                     ->delete();
 
-                $maxAutoid = $db->table('QMINSPECTVOUCHERS')
-                        ->max('Autoid') ?? 0;
+                // 2. 获取新的起始 autoid
+                $maxAutoid = $db->table('MaterialAppVouchs')
+                        ->max('AutoID') ?? 0;
 
-                // 过滤掉不可插入的字段,特别是 UFTS
-                foreach ($insertData as $key => $row) {
+                // 3. 分配新 autoid 并插入
+                foreach ($insertData as &$row) {
                     $maxAutoid++;
-
-                    // 彻底清理:移除 UFTS 字段,防止插入报错及 JSON 转换报错
-                    unset($row['UFTS']);
-
-                    $row['Autoid'] = $maxAutoid;
-                    $insertData[$key] = $row;
+                    $row['AutoID'] = $maxAutoid;
                 }
 
-                // 4. 批量插入
-                $db->table('QMINSPECTVOUCHERS')
+                $db->table('MaterialAppVouchs')
                     ->insert($insertData);
 
-                return [true, '操作成功'];
+                return [true, ''];
             });
         } catch (\Throwable $e) {
-            // 这里返回错误信息时,确保不包含二进制内容
-            return [false, "重组报检单失败: " . $e->getMessage()];
+            return [false, "重组领料申请单失败: " . $e->getMessage()];
         }
     }
 }