From dc94466f0f4abf70bbd8e7523ab1b052987eab8b Mon Sep 17 00:00:00 2001
From: spring <2396852758@qq.com>
Date: 星期六, 09 五月 2026 14:44:52 +0800
Subject: [PATCH] fix: 新增销售bug

---
 src/views/salesManagement/salesLedger/index.vue |  125 +++++++++++++++++++++++++++++++++--------
 1 files changed, 99 insertions(+), 26 deletions(-)

diff --git a/src/views/salesManagement/salesLedger/index.vue b/src/views/salesManagement/salesLedger/index.vue
index 569cdb1..a30d0fa 100644
--- a/src/views/salesManagement/salesLedger/index.vue
+++ b/src/views/salesManagement/salesLedger/index.vue
@@ -941,12 +941,13 @@
 
 const normalizeStockInventoryTree = (nodes = []) => {
   const normalizeNodeValue = (node) => {
-    // 鍚庣鏈夋椂浼氬嚭鐜� id=null 鐨勫眰绾э紝杩欓噷缁欎竴涓彲鐢ㄧ殑 key
-    if (node?.id !== null && node?.id !== undefined) return String(node.id);
-    if (node?.nodeType === "batch") return String(node.batchNo ?? node.label ?? "");
-    if (node?.nodeType === "customer") return String(node.customer ?? node.label ?? "");
-    if (node?.nodeType === "model") return String(node.productModelId ?? node.model ?? node.label ?? "");
-    return String(node.productName ?? node.label ?? "");
+    // 蹇呴』鐢ㄧ被鍨嬪墠缂�淇濊瘉鍏ㄥ眬鍞竴锛氬惁鍒欎細鍑虹幇銆屼骇鍝� id=325銆嶄笌銆岃鏍� productModelId=325銆嶉兘瑙勮寖鍖栨垚 "325"锛�
+    // findNodeObjByValue 鍏堝懡涓鏍艰妭鐐癸紝getModels 璇垽涓洪�変腑浜� model 浠庤�屼笉鍔犺浇瑙勬牸鍒楄〃锛堚厾鍨�2鍙�(GQ) 鍗虫渚嬶級銆�
+    if (node?.nodeType === "batch") return `batch:${String(node.batchNo ?? node.label ?? "").trim()}`;
+    if (node?.nodeType === "customer") return `customer:${String(node.customer ?? node.label ?? "").trim()}`;
+    if (node?.nodeType === "model") return `model:${String(node.productModelId ?? node.model ?? node.label ?? "").trim()}`;
+    if (node?.id !== null && node?.id !== undefined) return `product:${String(node.id)}`;
+    return `product:${String(node.productName ?? node.label ?? "").trim()}`;
   };
 
   const normalized = (list) =>
@@ -1005,6 +1006,49 @@
   return null;
 };
 
+// 瑙勬牸灞傝妭鐐癸細姝e父鏈� nodeType===model锛涢儴鍒嗘帴鍙f紡鍐� nodeType 鏃堕潬 productModelId 璇嗗埆
+const isInventoryModelNode = (n) => {
+  if (!n) return false;
+  if (n.nodeType === "model") return true;
+  if (n.nodeType === "batch" || n.nodeType === "customer") return false;
+  return n.productModelId !== null && n.productModelId !== undefined;
+};
+
+// 瑙勬牸涓嬫媺銆佽〃鍗曢噷鐢ㄧ殑鏄暟鍊� productModelId锛岄渶鍦ㄦ暣妫垫爲涓寜 productModelId 鏌ユ壘锛堜笉鑳戒緷璧栧彲鑳戒笌浜у搧 id 鎾炶溅鐨勭函鏁板瓧 value锛�
+const findInventoryModelByProductModelId = (nodes = [], productModelId) => {
+  const target = String(productModelId ?? "").trim();
+  if (!target) return null;
+  for (const node of nodes || []) {
+    if (isInventoryModelNode(node) && String(node.productModelId ?? "").trim() === target) {
+      return node;
+    }
+    const found = findInventoryModelByProductModelId(node.children || [], productModelId);
+    if (found) return found;
+  }
+  return null;
+};
+
+// 搴撳瓨鏍戜负銆屼骇鍝� 鈫� 瀛愪骇鍝� 鈫� 瑙勬牸鍨嬪彿 鈫� 鎵规鈥︺�嶏紝閫変腑澶х被鎴栦腑闂翠骇鍝佽妭鐐规椂闇�鏀堕泦鏁存5瀛愭爲閲岀殑瑙勬牸鑺傜偣
+const collectDescendantModelNodes = (root) => {
+  if (!root) return [];
+  const models = [];
+  const seen = new Set();
+  const walk = (n) => {
+    if (!n) return;
+    if (isInventoryModelNode(n)) {
+      const key = String(n.productModelId ?? n.value ?? "");
+      if (key && !seen.has(key)) {
+        seen.add(key);
+        models.push(n);
+      }
+      return;
+    }
+    for (const c of n.children || []) walk(c);
+  };
+  walk(root);
+  return models;
+};
+
 // 鑾峰彇搴撳瓨鏍戯紙鐢ㄤ簬浜у搧澶х被/瑙勬牸鍨嬪彿鑱斿姩锛�
 const getProductOptions = async () => {
   // 杩斿洖 Promise锛屼究浜庡湪缂栬緫浜у搧鏃剁瓑寰呭姞杞藉畬鎴�
@@ -1019,20 +1063,40 @@
 };
 // 鑾峰彇tree瀛愭暟鎹紙鍏堥�変骇鍝侊紝鍐嶉�夎鏍煎瀷鍙凤級
 const getModels = (value) => {
+  if (value === null || value === undefined || value === "") {
+    modelOptions.value = [];
+    productForm.value.productModelId = null;
+    productForm.value.specificationModel = "";
+    productForm.value.uidNo = "";
+    productForm.value.unit = "";
+    productForm.value.batchNo = "";
+    productForm.value.customer = "";
+    productForm.value.taxInclusiveUnitPrice = "";
+    productForm.value.taxInclusiveTotalPrice = "";
+    productForm.value.taxExclusiveTotalPrice = "";
+    batchNoOptions.value = [];
+    supplierOptions.value = [];
+    batchNodeByBatchNo = new Map();
+    return;
+  }
+
   const node = findNodeObjByValue(stockInventoryAllTree, value);
   if (!node) return;
-  if (node.nodeType !== "product") return;
+  // 鏍戦噷鍙�夌殑鏄骇鍝�/瀛愮洰褰曪紱浠呮帓闄ょ湡姝g殑鍙惰涔夎妭鐐癸紙閮ㄥ垎鏁版嵁婕忓啓 nodeType:product 鏃朵笉鑳藉崱姝伙級
+  if (node.nodeType === "model" || node.nodeType === "batch" || node.nodeType === "customer") return;
 
   // 閫夋嫨浜у搧鍚庯紝閲嶇疆涓嬫父瀛楁
   productForm.value.productCategory = node.label;
-  modelOptions.value = (node.children || [])
-      .filter((c) => c.nodeType === "model")
-      .map((m) => ({
-        id: m.value,
-        model: m.model ?? m.label ?? "",
-        unit: m.unit ?? "",
-        uidNo: m.uidNo ?? m.identifierCode ?? "",
-      }));
+  const modelNodes = collectDescendantModelNodes(node);
+  modelOptions.value = modelNodes.map((m) => ({
+    id:
+        m.productModelId !== null && m.productModelId !== undefined
+            ? m.productModelId
+            : Number(String(m.value).replace(/^model:/, "")) || m.value,
+    model: m.model ?? m.label ?? "",
+    unit: m.unit ?? "",
+    uidNo: m.uidNo ?? m.identifierCode ?? "",
+  }));
 
   productForm.value.productModelId = null;
   productForm.value.specificationModel = "";
@@ -1052,13 +1116,20 @@
 
 // 瑙勬牸鍨嬪彿閫夋嫨鍚庯細鍥炴樉 UID锛屽苟鐢熸垚鈥滄壒鍙蜂笅鎷夆��
 const getProductModel = (value) => {
-  const modelNode = findNodeObjByValue(stockInventoryAllTree, value);
-  if (!modelNode || modelNode.nodeType !== "model") return;
+  if (value === null || value === undefined || value === "") return;
+  let modelNode = findNodeObjByValue(stockInventoryAllTree, value);
+  if (!modelNode || !isInventoryModelNode(modelNode)) {
+    modelNode = findInventoryModelByProductModelId(stockInventoryAllTree, value);
+  }
+  if (!modelNode || !isInventoryModelNode(modelNode)) return;
 
   const prevBatchNo = productForm.value.batchNo;
   const prevCustomer = productForm.value.customer;
 
-  productForm.value.productModelId = modelNode.value;
+  productForm.value.productModelId =
+      modelNode.productModelId !== null && modelNode.productModelId !== undefined
+          ? modelNode.productModelId
+          : Number(String(modelNode.value).replace(/^model:/, "")) || null;
   productForm.value.specificationModel = modelNode.model ?? modelNode.label ?? "";
   // 鏈変簺鎺ュ彛/鏍戞暟鎹噷鍙兘涓嶅寘鍚� unit锛岃繖绉嶆儏鍐典笅涓嶈瑕嗙洊缂栬緫鏃跺凡鍥炴樉鐨勫��
   const nextUnit = modelNode.unit ?? "";
@@ -1446,14 +1517,16 @@
       const categoryKey = findNodeIdByLabel(productOptions.value, productForm.value.productCategory);
       if (categoryKey) {
         const categoryNode = findNodeObjByValue(stockInventoryAllTree, categoryKey);
-        const models = (categoryNode?.children || [])
-            .filter((n) => n.nodeType === "model")
-            .map((m) => ({
-              id: m.value,
-              model: m.model ?? m.label ?? "",
-              unit: m.unit ?? "",
-              uidNo: m.uidNo ?? m.identifierCode ?? "",
-            }));
+        const modelNodes = collectDescendantModelNodes(categoryNode);
+        const models = modelNodes.map((m) => ({
+          id:
+              m.productModelId !== null && m.productModelId !== undefined
+                  ? m.productModelId
+                  : Number(String(m.value).replace(/^model:/, "")) || m.value,
+          model: m.model ?? m.label ?? "",
+          unit: m.unit ?? "",
+          uidNo: m.uidNo ?? m.identifierCode ?? "",
+        }));
         modelOptions.value = models;
 
         // 鏍规嵁褰撳墠瑙勬牸鍨嬪彿鍥炴樉

--
Gitblit v1.9.3