From 3472db9338911b13eacb30bebb5862b35a9a630a Mon Sep 17 00:00:00 2001
From: llj <132905093+newwwwwwtree@users.noreply.github.com>
Date: 星期一, 09 二月 2026 16:08:36 +0800
Subject: [PATCH] 单据上传修复

---
 WebAPI/Controllers/成本管理/CB_ItemMoneyBillController.cs |  122 ++++++++++++++++++++++++++--------------
 1 files changed, 80 insertions(+), 42 deletions(-)

diff --git "a/WebAPI/Controllers/\346\210\220\346\234\254\347\256\241\347\220\206/CB_ItemMoneyBillController.cs" "b/WebAPI/Controllers/\346\210\220\346\234\254\347\256\241\347\220\206/CB_ItemMoneyBillController.cs"
index 8f9774e..0c5e92c 100644
--- "a/WebAPI/Controllers/\346\210\220\346\234\254\347\256\241\347\220\206/CB_ItemMoneyBillController.cs"
+++ "b/WebAPI/Controllers/\346\210\220\346\234\254\347\256\241\347\220\206/CB_ItemMoneyBillController.cs"
@@ -1011,8 +1011,8 @@
 
                 for (int i = 0; i <= provisional.Rows.Count - 1; i++)
                 {
-                    string HBillNo= DBUtility.ClsPub.CreateBillCode("1802", ref DBUtility.ClsPub.sExeReturnInfo, true);
-                    provisional.Rows[i]["鍗曟嵁鍙�"] = HBillNo;
+                    //string HBillNo= DBUtility.ClsPub.CreateBillCode("1802", ref DBUtility.ClsPub.sExeReturnInfo, true);
+                    //provisional.Rows[i]["鍗曟嵁鍙�"] = HBillNo;
                     string HDepNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["閮ㄩ棬浠g爜"].ToString());
                     string HDepName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["閮ㄩ棬"].ToString());
 
@@ -1024,7 +1024,7 @@
                     string HCostItemNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["鎴愭湰椤圭洰浠g爜"].ToString());
                     string HCostItemName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["鎴愭湰椤圭洰"].ToString());
 
-                    string HEntryID = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["瀛愯〃鍐呯爜"].ToString());
+                    //string HEntryID = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["瀛愯〃鍐呯爜"].ToString());
 
                     string HDepNumber_sub = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["瀛愰儴闂ㄤ唬鐮�"].ToString());
                     string HDepName_sub = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["瀛愰儴闂�"].ToString());
@@ -1036,8 +1036,8 @@
                     string HPrice = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["鍗曚环"].ToString());
                     string HMoney = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["閲戦"].ToString());
                     string HWaster = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["鎹熻�楅噺"].ToString());
-                    string HRelationQty = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["鍏宠仈鏁伴噺"].ToString());
-                    string HRelationMoney = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["鍏宠仈閲戦"].ToString());
+                    //string HRelationQty = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["鍏宠仈鏁伴噺"].ToString());
+                    //string HRelationMoney = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["鍏宠仈閲戦"].ToString());
                     string remark = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["澶囨敞"].ToString());
 
                     //鑾峰彇鐪熷疄琛屾暟
@@ -1070,8 +1070,8 @@
             var error = "";
 
             //鏌ヨ娌℃湁鐨勫垪
-            if (!provisional.Columns.Contains("瀛愯〃鍐呯爜"))
-                error += "娌℃湁鎵惧埌銆愬瓙琛ㄥ唴鐮併�戠殑鏍囬,";
+            //if (!provisional.Columns.Contains("瀛愯〃鍐呯爜"))
+            //    error += "娌℃湁鎵惧埌銆愬瓙琛ㄥ唴鐮併�戠殑鏍囬,";
 
             if (!provisional.Columns.Contains("鐗╂枡缂栫爜"))
                 error += "娌℃湁鎵惧埌銆愮墿鏂欑紪鐮併�戠殑鏍囬,";
@@ -1084,7 +1084,6 @@
         [HttpPost]
         public object Excel_btnSave([FromBody] JObject sMainSub)
         {
-            
             var _value = sMainSub["sMainSub"].ToString();
             string msg1 = _value.ToString();
             string[] sArray = msg1.Split(new string[] { "&鍜�" }, StringSplitOptions.RemoveEmptyEntries);
@@ -1121,7 +1120,16 @@
                 var EmpNames = list.Select(x => x["鑱屽憳"].ToString()).Distinct().ToList();
                 var CostNumbers = list.Select(x => x["鎴愭湰椤圭洰浠g爜"].ToString()).Distinct().ToList();
                 var CostNames = list.Select(x => x["鎴愭湰椤圭洰"].ToString()).Distinct().ToList();
+                var ICMONumbers = list.Select(x => x["鐢熶骇璁㈠崟鍙�"].ToString()).Distinct().ToList();
 
+                // 2. 鎵归噺鏌ヨ鐢熶骇璁㈠崟鍐呯爜
+                string ICMOQuery = $"SELECT HInterID,HBillNo FROM Sc_ICMOBillMain WHERE HBillNo IN ({string.Join(",", ICMONumbers.Select(s => $"'{s.Replace("'", "''")}'"))})";
+                DataSet icmoData = oCN.RunProcReturn(ICMOQuery, "Sc_ICMOBillMain");
+                Dictionary<string, int> ICMODict = new Dictionary<string, int>();
+                foreach (DataRow row in icmoData.Tables[0].Rows)
+                {
+                    ICMODict[row["HBillNo"].ToString()] = (int)row["HInterID"];
+                }
 
                 // 2. 鎵归噺鏌ヨ涓诲瓙琛ㄩ儴闂ㄤ俊鎭�
                 string supplierQuery = $"SELECT HItemID, HName FROM Gy_Department WHERE HName IN ({string.Join(",", HDeptNames.Select(s => $"'{s.Replace("'", "''")}'"))})";
@@ -1134,7 +1142,7 @@
 
                 // 2.1 鎵归噺鏌ヨ涓诲瓙琛ㄩ儴闂ㄤ俊鎭�
                 string supplierQuery_sub = $"SELECT HItemID, HName FROM Gy_Department WHERE HName IN ({string.Join(",", HDeptNames_sub.Select(s => $"'{s.Replace("'", "''")}'"))})";
-                DataSet supData_sub = oCN.RunProcReturn(supplierQuery, "Gy_Department");
+                DataSet supData_sub = oCN.RunProcReturn(supplierQuery_sub, "Gy_Department");
                 Dictionary<string, int> supplierDict_sub = new Dictionary<string, int>();
                 foreach (DataRow row in supData_sub.Tables[0].Rows)
                 {
@@ -1172,7 +1180,6 @@
                     string key = $"{row["HName"].ToString()}";
                     CostDict[key] = (int)row["HItemID"];
                 }
-               
 
                 // 4. 鏌ヨ鐢ㄦ埛淇℃伅
                 DataSet emp = oCN.RunProcReturn($"SELECT Czybm FROM Gy_Czygl WHERE Czymc = '{user.Replace("'", "''")}'", "Gy_Czygl");
@@ -1196,6 +1203,7 @@
                     var item = list[i];
                     int lineNumber = i + 1; // 琛屽彿浠�1寮�濮�
 
+                    string ICMOcheck = item["鐢熶骇璁㈠崟鍙�"];
                     string supplier = item["閮ㄩ棬"];
                     string materialNumber = item["鐗╂枡缂栫爜"];
                     string materialName = item["鐗╂枡鍚嶇О"];
@@ -1211,6 +1219,11 @@
                     if (!materialDict.ContainsKey(materialKey))
                     {
                         missingMaterialMessages.Add($"绗瑊lineNumber}琛岀殑鐗╂枡銆恵materialName}(缂栫爜:{materialNumber})銆�");
+                    }
+                    // 妫�鏌ョ敓浜ц鍗�
+                    if (!string.IsNullOrEmpty(ICMOcheck) && !ICMODict.ContainsKey(ICMOcheck))
+                    {
+                        missingMaterialMessages.Add($"绗瑊lineNumber}琛岀殑鐢熶骇璁㈠崟銆恵ICMOcheck}銆�");
                     }
                 }
 
@@ -1251,59 +1264,84 @@
                     existRecords.Add($"{row["HMaterID"]}_{row["HDeptID_sub"]}");
                 }
 
+                // 7. 鎸夌収閮ㄩ棬浠g爜銆侀儴闂ㄣ�佽亴鍛樹唬鐮併�佽亴鍛樸�佺敓浜ц鍗曞彿杩涜鍒嗙粍
+                var groupedData = list.GroupBy(item => new
+                {
+                    閮ㄩ棬浠g爜 = item["閮ㄩ棬浠g爜"],
+                    閮ㄩ棬 = item["閮ㄩ棬"],
+                    鑱屽憳浠g爜 = item["鑱屽憳浠g爜"],
+                    鑱屽憳 = item["鑱屽憳"],
+                    鐢熶骇璁㈠崟鍙� = item["鐢熶骇璁㈠崟鍙�"]
+                }).ToList();
+
                 StringBuilder insertSql = new StringBuilder();
                 StringBuilder insertSql_sub = new StringBuilder();
-                StringBuilder updateSql = new StringBuilder();
-                StringBuilder updateSql_sub = new StringBuilder();
                 string currentDate = DateTime.Today.ToString("yyyy-MM-dd");
-                int HEntryID = 1;
-                foreach (Dictionary<string, string> item in list)
+
+                foreach (var group in groupedData)
                 {
-                    int HSupplierID = supplierDict_sub[item["閮ㄩ棬"]];
-                    int EmpID = EmpDict[item["鑱屽憳"]];//
-                    int HCostID = CostDict[item["鎴愭湰椤圭洰"]] ;//EmpDict[item["鑱屽憳"]];
-                    int HMaterID = materialDict[$"{item["鐗╂枡缂栫爜"]}_{item["鐗╂枡鍚嶇О"]}"];
-                    string recordKey = $"{HMaterID}_{HSupplierID}";
+                    var firstItem = group.First();
+
+                    // 鑾峰彇缁勪俊鎭�
+                    int HSupplierID = supplierDict_sub[firstItem["閮ㄩ棬"]];
+                    int EmpID = EmpDict[firstItem["鑱屽憳"]];
+                    string ICMO = firstItem["鐢熶骇璁㈠崟鍙�"];
+                    int ICMOInterID = !string.IsNullOrEmpty(ICMO) && ICMODict.ContainsKey(ICMO) ? ICMODict[ICMO] : 0;
+
+                    // 鐢熸垚涓昏〃ID鍜屽崟鎹彿锛堟瘡缁勭敓鎴愪竴涓級
+                    string HBillNo = DBUtility.ClsPub.CreateBillCode("1802", ref DBUtility.ClsPub.sExeReturnInfo, true);
                     long HInterID = DBUtility.ClsPub.CreateBillID("1802", ref DBUtility.ClsPub.sExeReturnInfo);
-                    if (!existRecords.Contains(recordKey))
+
+                    // 妫�鏌ヨ缁勬槸鍚﹀凡瀛樺湪锛堟牴鎹儴闂↖D鍜岀墿鏂橧D缁勫悎锛�
+                    bool groupExists = false;
+                    foreach (var item in group)
                     {
-                        
+                        int HMaterID = materialDict[$"{item["鐗╂枡缂栫爜"]}_{item["鐗╂枡鍚嶇О"]}"];
+                        string recordKey = $"{HMaterID}_{HSupplierID}";
+                        if (existRecords.Contains(recordKey))
+                        {
+                            groupExists = true;
+                            break;
+                        }
+                    }
+
+                    if (!groupExists)
+                    {
+                        // 鎻掑叆涓昏〃锛堟瘡缁勬彃鍏ヤ竴娆★級
                         insertSql.AppendLine($"INSERT INTO CB_ItemMoneyBillMain  ([HInterID], [HBillNo], [HDate], [HYear], [HPeriod], [HBillType], [HBillSubType], [HBillStatus], [HCheckItemNowID], [HCheckItemNextID], [HCheckFlowID], [HRemark], [HBacker], [HBackDate], [HBackRemark], [HChecker], [HCheckDate], [HMaker], [HMakeDate], [HUpDater], [HUpDateDate], [HCloseMan], [HCloseDate], [HCloseType], [HDeleteMan], [HDeleteDate], [HMainSourceBillType], [HMainSourceInterID], [HMainSourceEntryID], [HMainSourceBillNo], [HPrintQty],  [HEmpID], [HDeptID], [HICMOInterID], [HMaterID], [HICMOBillNo], [HExplanation], [HInnerBillNo], [HBeginDate], [HEndDate], [HOrgID]) VALUES (");
-                        insertSql.AppendLine($"'{HInterID}', '{item["鍗曟嵁鍙�"]}', '{DateTime.Now}', {DateTime.Now.Year}, {DateTime.Now.Month}, {"1802"}, '{"1802"}', '{"1"}',  '{"0"}', '{"0"}', '{"0"}', '{"excel瀵煎叆"}', '{""}', '{DateTime.Now}', '{""}', '{""}', '{DateTime.Now}', '{""}', '{DateTime.Now}', '{""}', '{DateTime.Now}', '{""}', '{DateTime.Now}', '{"0"}', '{""}', '{DateTime.Now}', '{""}', '{"0"}', '{"0"}', '{""}', '{"0"}', '{EmpID}', '{HSupplierID}', '{"0"}','{HMaterID}','{"0"}','{""}','{"0"}','{DateTime.Now}','{DateTime.Now}','{"100038"}');");
+                        insertSql.AppendLine($"'{HInterID}', '{HBillNo}', '{DateTime.Now}', {DateTime.Now.Year}, {DateTime.Now.Month}, {"1802"}, '{"1802"}', '{"1"}',  '{"0"}', '{"0"}', '{"0"}', '{"excel瀵煎叆"}', '{""}', '{DateTime.Now}', '{""}', '{""}', '{DateTime.Now}', '{""}', '{DateTime.Now}', '{""}', '{DateTime.Now}', '{""}', '{DateTime.Now}', '{"0"}', '{""}', '{DateTime.Now}', '{""}', '{"0"}', '{"0"}', '{""}', '{"0"}', '{EmpID}', '{HSupplierID}', '{ICMOInterID}','{0}','{ICMO}','{""}','{"0"}','{DateTime.Now}','{DateTime.Now}','{"100038"}');");
 
-                        insertSql_sub.AppendLine($"INSERT INTO [CB_ItemMoneyBillSub] ([HInterID], [HEntryID], [HCloseMan], [HEntryCloseDate], [HCloseType], [HRemark],[HSourceInterID], [HSourceEntryID], [HSourceBillNo], [HSourceBillType],[HRelationQty], [HRelationMoney], [HBillNo_bak], [HCostItemID],[HDeptID], [HQty], [HPrice], [HMoney],[HMaterID], [HWaster], [HDate], [HBeginBalance], [HEndBalance], [HTimes]) VALUES (");
-                        insertSql_sub.AppendLine($"'{HInterID}', '{HEntryID}', '{""}', '{DateTime.Now}', '{"0"}', '{"excel瀵煎叆"}','{"0"}', '{"0"}',  '{"0"}', '{"0"}','{item["鍏宠仈鏁伴噺"]}', '{item["鍏宠仈閲戦"]}', '{""}', '{HCostID}', '{HSupplierID}', '{item["鏁伴噺"]}', '{item["鍗曚环"]}', '{item["閲戦"]}','{HMaterID}', '{item["鎹熻�楅噺"]}', '{DateTime.Now}', '{"0"}', '{"0"}', '{"0"}');");
-                        HEntryID++;
+                        // 鎻掑叆瀛愯〃锛堢粍鍐呮瘡涓墿鏂欐彃鍏ヤ竴娆★級
+                        int HEntryID = 1;
+                        foreach (var item in group)
+                        {
+                            int HMaterID = materialDict[$"{item["鐗╂枡缂栫爜"]}_{item["鐗╂枡鍚嶇О"]}"];
+                            int HCostID = CostDict[item["鎴愭湰椤圭洰"]];
+
+                            insertSql_sub.AppendLine($"INSERT INTO [CB_ItemMoneyBillSub] ([HInterID], [HEntryID], [HCloseMan], [HEntryCloseDate], [HCloseType], [HRemark],[HSourceInterID], [HSourceEntryID], [HSourceBillNo], [HSourceBillType],[HRelationQty], [HRelationMoney], [HBillNo_bak], [HCostItemID],[HDeptID], [HQty], [HPrice], [HMoney],[HMaterID], [HWaster], [HDate], [HBeginBalance], [HEndBalance], [HTimes]) VALUES (");
+                            insertSql_sub.AppendLine($"'{HInterID}', '{HEntryID}', '{""}', '{DateTime.Now}', '{"0"}', '{"excel瀵煎叆"}','{"0"}', '{"0"}',  '{"0"}', '{"0"}','{"0"}', '{"0"}', '{""}', '{HCostID}', '{HSupplierID}', '{item["鏁伴噺"]}', '{item["鍗曚环"]}', '{item["閲戦"]}','{HMaterID}', '{item["鎹熻�楅噺"] ?? "0"}', '{DateTime.Now}', '{"0"}', '{"0"}', '{"0"}');");
+                            HEntryID++;
+                        }
                     }
-                    else
-                    {
-                        // 鏇存柊璇彞
-                        updateSql.AppendLine($"UPDATE CB_ItemMoneyBillMain SET HDate = '{currentDate}', HRemark = '{item["澶囨敞"]}', HEmpID = '{EmpID}', HMaterID = '{HMaterID}' , HDeptID = '{HSupplierID}' WHERE HBillNo = '{item["鍗曟嵁鍙�"]}'");
-                        updateSql_sub.AppendLine($"UPDATE CB_ItemMoneyBillSub SET HDate = '{currentDate}', HQty = '{item["鏁伴噺"]}', HDeptID = '{HSupplierID}', HPrice = '{item["鍗曚环"]}', HRelationQty = '{item["鍏宠仈鏁伴噺"]}', HRelationMoney = '{item["鍏宠仈閲戦"]}', HMoney = '{item["閲戦"]}', HWaster = '{item["鎹熻�楅噺"]}', HMaterID = '{HMaterID}' WHERE HInterID = {HInterID};");
-                    }
+                   
                 }
-
-                
 
                 // 鎵ц鎵归噺鎿嶄綔
                 if (insertSql.Length > 0)
                 {
                     oCN.RunProc(insertSql.ToString());
+                }
+
+                if (insertSql_sub.Length > 0)
+                {
                     oCN.RunProc(insertSql_sub.ToString());
                 }
-                 
-                if (updateSql.Length > 0)
-                {
-                    oCN.RunProc(updateSql.ToString());
-                    oCN.RunProc(updateSql_sub.ToString());
-                }
-                   
 
                 oCN.Commit();
 
                 res.code = "1";
-                res.count = 1;
-                res.Message = "瀵煎叆鎴愬姛!";
+                res.count = groupedData.Count;
+                res.Message = $"瀵煎叆鎴愬姛! 鍏卞鐞唟groupedData.Count}缁勬暟鎹�";
                 res.data = null;
                 return res;
             }

--
Gitblit v1.9.1