纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1053 lines
58 KiB

3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. using ICSSoft.Common;
  2. using ICSSoft.Entity;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ICSSoft.DataProject
  12. {
  13. /// <summary>
  14. /// 材料出库 备料表,生产订单
  15. /// </summary>
  16. public class MOIssueDoc
  17. {
  18. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  19. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  20. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  21. private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"];
  22. private static string CRNALL = System.Configuration.ConfigurationManager.AppSettings["MoICRDNAME"];
  23. public string Get(List<ICSRdrecord11> infos)
  24. {
  25. List<ICSRdrecord11> szJson = new List<ICSRdrecord11>();
  26. DataTable dt = null;
  27. DataTable dtNew = null;
  28. string connS = "";
  29. string json = "";
  30. if (infos.Count <= 0)
  31. {
  32. throw new Exception("传送数据为空!");
  33. }
  34. string res = string.Empty;
  35. SqlConnection conn = new SqlConnection();
  36. SqlCommand cmd = new SqlCommand();
  37. string sql = string.Empty;
  38. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  39. foreach (string WorkPoint in result)
  40. {
  41. try
  42. {
  43. connS = string.Format(connString, WorkPoint);
  44. conn = new System.Data.SqlClient.SqlConnection(connS);
  45. conn.Open();
  46. SqlTransaction sqlTran = conn.BeginTransaction();
  47. cmd = new SqlCommand();
  48. cmd.Transaction = sqlTran;
  49. cmd.Connection = conn;
  50. foreach (ICSRdrecord11 info in infos)
  51. {
  52. if (WorkPoint != info.WorkPoint)
  53. continue;
  54. ICSUserInfo userInfo = new ICSUserInfo();
  55. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  56. if (info.MTime < new DateTime(2000, 01, 01))
  57. throw new Exception("请输入正确的操作时间:" + info.MTime);
  58. sql = @" select a.ID,iMPoIds,iordercode,cCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,cSource,cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,b.AutoID ,irowno,b.cInvCode,iQuantity,f.MoDId,iNum,g.MoCode from rdrecord11 a
  59. left join rdrecords11 b on a.ID=b.ID
  60. left join Department c on a.cDepCode=c.cDepCode
  61. left join Warehouse d on a.cWhCode=d.cWhCode
  62. left join mom_moallocate e on b.iMPoIds=e.AllocateId
  63. INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId
  64. INNER JOIN dbo.mom_order g ON f.MoId=g.MoId WHERE 1=1";
  65. if (!string.IsNullOrWhiteSpace(info.IssueCode))
  66. {
  67. sql += " and a.cCode='{0}'";
  68. }
  69. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  70. {
  71. sql += " and ISNULL(a.dnmodifytime ,ISNULL(a.dnverifytime , ISNULL(a.dnmodifytime , a.dnmaketime)))>='{1}'";
  72. }
  73. if (!string.IsNullOrWhiteSpace(info.User))
  74. {
  75. sql += " and a.CMAKER='{2}'";
  76. }
  77. sql = string.Format(sql, info.IssueCode, info.MTime, userInfo.UserName);
  78. dt = DBHelper.SQlReturnData(sql, cmd);
  79. if (dtNew == null)
  80. dtNew = dt;
  81. else
  82. dtNew.Merge(dt);
  83. }
  84. cmd.Transaction.Commit();
  85. }
  86. catch (Exception ex)
  87. {
  88. cmd.Transaction.Rollback();
  89. log.Error(ex.Message);
  90. throw new Exception(ex.Message);
  91. }
  92. finally
  93. {
  94. if (conn.State == ConnectionState.Open)
  95. {
  96. conn.Close();
  97. }
  98. conn.Dispose();
  99. }
  100. }
  101. json = JsonConvert.SerializeObject(dtNew);
  102. return json;
  103. }
  104. public string CreateRd11(List<ICSRdrecord11> Bills)
  105. {
  106. string msg = "";
  107. DataTable dt = null;
  108. string iFatherIdTwo = string.Empty;
  109. DataSet ds = null;
  110. string connS = "";
  111. VouchKey key = new VouchKey();
  112. int num = 0;
  113. int iFatherId = 0;
  114. int iChildId = 0;
  115. string sql = string.Empty;
  116. SqlConnection conn = new SqlConnection();
  117. SqlCommand cmd = new SqlCommand();
  118. if (Bills.Count <= 0)
  119. {
  120. throw new Exception("传送数据为空!");
  121. }
  122. LogInfo(Bills);
  123. //MergeObject(Bills, cmd);
  124. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  125. foreach (string WorkPoint in result)
  126. {
  127. try
  128. {
  129. connS = string.Format(connString, WorkPoint);
  130. conn = new System.Data.SqlClient.SqlConnection(connS);
  131. conn.Open();
  132. SqlTransaction sqlTran = conn.BeginTransaction();
  133. cmd = new SqlCommand();
  134. cmd.Transaction = sqlTran;
  135. cmd.Connection = conn;
  136. foreach (ICSRdrecord11 head in Bills)
  137. {
  138. iFatherIdTwo = "";
  139. if (WorkPoint != head.WorkPoint)
  140. continue;
  141. ICSUserInfo userInfo = new ICSUserInfo();
  142. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  143. num = head.details.Count();
  144. string[] ss = head.WorkPoint.Split('_');
  145. ERPDB = ss[1];
  146. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  147. throw new Exception("U8正在整理现存量,请稍后再试");
  148. string[] dd = CRNALL.Split('~');
  149. string crdname = dd[0];
  150. string carname = dd[1];
  151. string surface = dd[2];
  152. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  153. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  154. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  155. iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  156. iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  157. DateTime date = DateTime.Now;
  158. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode, head.WorkPoint);
  159. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  160. #region 判断工单是否已关闭
  161. sql = @"SELECT isnull(CloseUser,'') as CloseUser from mom_orderdetail a
  162. inner join mom_order b on a.MoId=b.MoId where b.MoCode='" + head.SourceCode + "' ";
  163. DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  164. if (dtClose != null && dtClose.Rows.Count > 0)
  165. {
  166. if (!string.IsNullOrEmpty(dtClose.Rows[0][0].ToString()))
  167. {
  168. throw new Exception("该工单行已关闭,无法领料!");
  169. }
  170. }
  171. #endregion
  172. #region 判断物料是否超额领取
  173. // foreach (rdrecords11 body in head.details)
  174. // {
  175. // sql = @"SELECT isnull(b.fOutExcess,0) as fOutExcess,isnull(a.IssQty,0) as IssQty,a.Qty,a.ReplenishQty FROM dbo.mom_moallocate a
  176. // LEFT JOIN dbo.Inventory b ON a.InvCode=b.cInvCode
  177. // WHERE a.AllocateId='" + body.SourceDetailID + "'";
  178. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  179. // if (dtQty != null && dtQty.Rows.Count > 0)
  180. // {
  181. // //可超领比率
  182. // decimal fOutExcess = Convert.ToDecimal(dtQty.Rows[0]["fOutExcess"]);
  183. // //已领数量
  184. // decimal IssQty = Convert.ToDecimal(dtQty.Rows[0]["IssQty"]);
  185. // //应领数量
  186. // decimal Qty = Convert.ToDecimal(dtQty.Rows[0]["Qty"]);
  187. // //当前可总领数量
  188. // decimal TotalQty = Qty;// + fOutExcess * Qty;
  189. // //补料数量
  190. // decimal ReplenishQty = Convert.ToDecimal(dtQty.Rows[0]["ReplenishQty"]);
  191. // string cql = @"SELECT * from Inventory where cInvCode='" + body.InvCode + "'AND bKCCutMantissa =1 AND bCutMantissa=1";
  192. // DataTable dtInv = DBHelper.SQlReturnData(cql, cmd);
  193. // if (dtInv != null && dtInv.Rows.Count > 0)
  194. // {
  195. // }
  196. // else
  197. // {
  198. // if (IssQty - ReplenishQty + body.Quantity > TotalQty)
  199. // {
  200. // throw new Exception("物料领取超过当前可领用最大数,AllocateId:" + body.SourceDetailID);
  201. // }
  202. // }
  203. // //if (IssQty + body.iQuantity > TotalQty)
  204. // //{
  205. // // throw new Exception("物料领取超过当前可领用最大数,AllocateId:" + body.AllocateId);
  206. // //}
  207. // }
  208. // else
  209. // {
  210. // throw new Exception("工单行子件不存在!AllocateId:" + body.SourceDetailID);
  211. // }
  212. // }
  213. #endregion
  214. #region 材料出库单表头
  215. sql = @"INSERT INTO dbo.rdrecord11
  216. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,
  217. cCode ,cRdCode ,cDepCode,cHandler ,bTransFlag ,cMaker ,
  218. dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc
  219. ,cPsPcode ,cMPoCode ,iproorderid ,bFromPreYear ,
  220. bIsLsQuery ,bIsComplement ,iDiscountTaxType ,ireturncount ,iverifystate ,
  221. iswfcontrolled ,dnmaketime ,dnverifytime ,bredvouch ,iPrintCount,
  222. iMQuantity)
  223. SELECT top 1 @ID,0,'11','','',@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),
  224. @cCode,@cRDcode,a.MDeptCode,@cHandler,0,@cMaker,
  225. CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0,
  226. NULL,b.MoCode,a.MoId,0,
  227. 0,0,0,0,0,
  228. 0,GETDATE(),GETDATE(),0,0,
  229. NULL
  230. FROM dbo.mom_orderdetail a
  231. left JOIN dbo.mom_order b ON a.MoId=b.MoId
  232. left join mom_morder c on a.modid=c.modid
  233. WHERE b.MoCode='" + head.SourceCode + "'";
  234. cmd.Parameters.Clear();
  235. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  236. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  237. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  238. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  239. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  240. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  241. cmd.Parameters.Add(new SqlParameter("@cRDcode", cRDcode));
  242. cmd.CommandText = sql;
  243. try
  244. {
  245. int count = cmd.ExecuteNonQuery();
  246. if (count <= 0)
  247. {
  248. log.Error("生成材料出库单表头失败,受影响行数<=0;");
  249. throw new Exception("生成材料出库单表头失败,受影响行数<=0;");
  250. }
  251. }
  252. catch (Exception ex)
  253. {
  254. log.Error("生成材料出库单表头失败" + sql, ex);
  255. throw new Exception("生成材料出库单表头失败" + sql, ex);
  256. }
  257. #endregion
  258. #region 材料出库单表体
  259. int irowno = 0;
  260. foreach (rdrecords11 body in head.details)
  261. {
  262. irowno++;
  263. //if (irowno == 0)
  264. //{
  265. // iChildId = iChildId;
  266. //}
  267. //else
  268. //{
  269. // iChildId -= 1;
  270. //}
  271. //irowno += 1;
  272. //if (head.IsReturn == "1")
  273. //{
  274. // body.iQuantity = -body.iQuantity;
  275. //}
  276. //if (head.IsReturn == "0")
  277. //{
  278. // //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
  279. // sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.cInvCode + "'AND cBatch='" + body.cBatch + "'and cWhCode='" + head.cWhCode + "'";
  280. // DataTable dtItem = U8Helper.SQlReturnData(sql, cmd);
  281. // if (dtItem != null && dtItem.Rows.Count > 0)
  282. // {
  283. // if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cBatch))
  284. // {
  285. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.cInvCode);
  286. // }
  287. // if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < body.iQuantity)
  288. // {
  289. // throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.cInvCode);
  290. // }
  291. // }
  292. // else
  293. // {
  294. // throw new Exception("物料:" + body.cInvCode + "在现存量表中不存在!");
  295. // }
  296. //}
  297. sql = @"INSERT INTO dbo.rdrecords11
  298. ( AutoID ,ID ,cInvCode ,iQuantity ,cBatch ,iFlag ,iNum,
  299. cItemCode ,cName ,iNQuantity ,iMPoIds ,bLPUseFree ,
  300. iOriTrackID ,bCosting ,bVMIUsed ,cmocode ,invcode ,imoseq ,
  301. iopseq ,iExpiratDateCalcu ,iorderdid ,iordertype ,isotype ,
  302. ipesodid ,ipesotype ,cpesocode ,ipesoseq ,irowno ,
  303. bcanreplace ,iposflag,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  304. SELECT @AutoID,@ID,a.InvCode,@iQuantity,@cBatch,0,@iNum,
  305. @cItemCode,null,a.Qty,a.AllocateId,0,
  306. 0,1,0,c.MoCode,b.InvCode,b.SortSeq,
  307. '0000',0,0,0,0,
  308. a.AllocateId,'7',c.MoCode,b.SortSeq,@irowno,
  309. 0,null,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  310. FROM dbo.mom_moallocate a
  311. INNER JOIN dbo.mom_orderdetail b ON a.MoDId=b.MoDId
  312. INNER JOIN dbo.mom_order c ON b.MoId=c.MoId
  313. WHERE a.AllocateId='" + body.SourceDetailID + "'";
  314. cmd.Parameters.Clear();
  315. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  316. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  317. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount.ToString("0.00")));
  318. //cmd.Parameters.Add(new SqlParameter("@cBatch", "''"));
  319. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  320. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  321. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  322. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  323. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  324. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  325. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  326. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  327. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  328. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  329. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  330. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  331. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  332. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  333. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  334. cmd.CommandText = sql;
  335. try
  336. {
  337. int count = cmd.ExecuteNonQuery();
  338. if (count <= 0)
  339. {
  340. log.Error("生成材料出库单表体失败,受影响行数<=0;");
  341. throw new Exception("生成材料出库单表体失败,受影响行数<=0;");
  342. }
  343. }
  344. catch (Exception ex)
  345. {
  346. log.Error("生成材料出库单表体失败" + sql, ex);
  347. throw new Exception("生成材料出库单表体失败 " + sql, ex);
  348. }
  349. #region 回写工单子件已领数量
  350. sql = "update mom_moallocate set IssQty=isnull(IssQty,0)+" + body.Quantity + ",RequisitionIssQty=isnull(RequisitionIssQty,0)+(" + body.Quantity + ") where AllocateId='" + body.SourceDetailID + "' ";
  351. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写工单子件已领数量失败!");
  352. #endregion
  353. sql = @"select isnull(Issqty,0)fOutQuantity,isnull(qty,0)iQuantity,ISNULL(AuxQty, 0)iNum,ISNULL(NiNum, 0)NiNum,ISNULL(AuxQty, 0)-ISNULL(NiNum, 0) SiNum from mom_moallocate a
  354. LEFT JOIN (select sum(iNum) NiNum,ipesodid from rdrecords11 GROUP BY ipesodid) b on a.AllocateId=b.ipesodid
  355. where a.AllocateId='" + body.SourceDetailID + "'";
  356. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  357. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  358. {
  359. foreach (DataRow item1 in dtDisCheck.Rows)
  360. {
  361. if (Convert.ToDecimal(item1["fOutQuantity"].ToString()) == Convert.ToDecimal(item1["iQuantity"].ToString()))
  362. {
  363. if (Convert.ToDecimal(item1["iNum"].ToString()) != 0)
  364. {
  365. if (Convert.ToDecimal(item1["SiNum"].ToString()) != 0)
  366. {
  367. sql = @"Update rdrecords11 set iNum=isnull(iNum,0)+(" + item1["SiNum"].ToString() + ") where autoid='" + iChildId + "' ";
  368. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item1["SiNum"].ToString()));
  369. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  370. }
  371. }
  372. }
  373. }
  374. }
  375. //更新现存量
  376. key.cBustypeUN = "领料";
  377. key.cVouchTypeUN = "11";
  378. key.TableName = "IA_ST_UnAccountVouch11";
  379. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  380. //DBHelper.UpdateCurrentStockNEW(cmd, body.InvCode, head.WHCode, body.BatchCode, body.Quantity, body.cFree1, body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10, key);
  381. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, body.Quantity, body.Amount, body.cFree1,
  382. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  383. "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  384. iFatherIdTwo = "'" + iFatherId + "',";
  385. iChildId--;
  386. }
  387. #endregion
  388. #region 查询
  389. sql = @"
  390. select DISTINCT a.ID as ID,a.ID as IDs,cMPoCode as SourceCode ,
  391. cCode as IssueCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,
  392. cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime,
  393. cHandler as Checker,dnverifytime as CheckDateTime
  394. from rdrecord11 a
  395. left join Department c on a.cDepCode=c.cDepCode
  396. left join Warehouse d on a.cWhCode=d.cWhCode
  397. WHERE a.ID in ({0})
  398. select DISTINCT a.ID as IDs,b.AutoID as DetailID,irowno as Sequence,b.cInvCode as InvCode
  399. ,iQuantity as Quantity,e.AllocateId as SourceDetailID,iNum as Amount,
  400. isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  401. isnull(b.cFree1,'') as cFree1,
  402. isnull(b.cFree2,'') as cFree2,
  403. isnull(b.cFree3,'') as cFree3,
  404. isnull(b.cFree4,'') as cFree4,
  405. isnull(b.cFree5,'') as cFree5,
  406. isnull(b.cFree6,'') as cFree6,
  407. isnull(b.cFree7,'') as cFree7,
  408. isnull(b.cFree8,'') as cFree8,
  409. isnull(b.cFree9,'') as cFree9,
  410. isnull(b.cFree10,'') as cFree10
  411. from rdrecord11 a
  412. left join rdrecords11 b on a.ID=b.ID
  413. left join Department c on a.cDepCode=c.cDepCode
  414. left join Warehouse d on a.cWhCode=d.cWhCode
  415. left join mom_moallocate e on b.iMPoIds=e.AllocateId
  416. INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId
  417. INNER JOIN dbo.mom_order g ON f.MoId=g.MoId WHERE a.ID in ({0})";
  418. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  419. if (ds != null)
  420. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  421. else
  422. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  423. #endregion
  424. }
  425. cmd.Transaction.Commit();
  426. }
  427. catch (Exception ex)
  428. {
  429. cmd.Transaction.Rollback();
  430. log.Error(ex.Message);
  431. throw new Exception(ex.Message);
  432. }
  433. finally
  434. {
  435. if (conn.State == ConnectionState.Open)
  436. {
  437. conn.Close();
  438. }
  439. conn.Dispose();
  440. }
  441. }
  442. msg = JSON.DataSetToJson(ds, "details", "IDs");
  443. return msg;
  444. }
  445. public string MaterialAppVouchs(List<ICSRdrecord11> Bills)
  446. {
  447. string mag = "";
  448. string sql="";
  449. DataSet ds = null;
  450. string connS = "";
  451. string iFatherIdTwo = string.Empty;
  452. SqlConnection conn = new SqlConnection();
  453. SqlCommand cmd = new SqlCommand();
  454. int num = 0;
  455. if (Bills.Count <= 0)
  456. {
  457. throw new Exception("传送数据为空!");
  458. }
  459. LogInfo(Bills);
  460. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  461. foreach (string WorkPoint in result)
  462. {
  463. try
  464. {
  465. connS = string.Format(connString, WorkPoint);
  466. conn = new System.Data.SqlClient.SqlConnection(connS);
  467. conn.Open();
  468. SqlTransaction sqlTran = conn.BeginTransaction();
  469. cmd = new SqlCommand();
  470. cmd.Transaction = sqlTran;
  471. cmd.Connection = conn;
  472. foreach (ICSRdrecord11 head in Bills)
  473. {
  474. iFatherIdTwo = "";
  475. num = head.details.Count();
  476. if (WorkPoint != head.WorkPoint)
  477. continue;
  478. string[] ss = head.WorkPoint.Split('_');
  479. ERPDB = ss[1];
  480. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  481. throw new Exception("U8正在整理现存量,请稍后再试");
  482. #region 判断物料是否超额领取
  483. foreach (rdrecords11 body in head.details)
  484. {
  485. sql = @"SELECT isnull(fOutQuantity,0) as fOutQuantity,iQuantity FROM dbo.MaterialAppVouchs WHERE AutoID='" + body.SourceDetailID + "'";
  486. DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  487. if (dtQty != null && dtQty.Rows.Count > 0)
  488. {
  489. //已领数量
  490. decimal IssQty = Convert.ToDecimal(dtQty.Rows[0]["fOutQuantity"]);
  491. //应领数量
  492. decimal Qty = Convert.ToDecimal(dtQty.Rows[0]["iQuantity"]);
  493. if (IssQty + body.Quantity > Qty)
  494. {
  495. throw new Exception("物料领取超过当前可领用最大数,订单号:" + head.SourceCode + ",行号:" + body.Sequence + " ");
  496. }
  497. }
  498. else
  499. {
  500. throw new Exception("领料申请单不存在!订单号:" + head.SourceCode);
  501. }
  502. }
  503. #endregion
  504. #region 现存量是否够领
  505. // //if (head.IsReturn != "1")
  506. // //{
  507. // foreach (rdrecords11 body in head.rdcords)
  508. // {
  509. // sql = string.Format(@"SELECT iQuantity FROM dbo.CurrentStock
  510. // WHERE cInvCode='{0}' AND cWhCode='{1}'", body.cInvCode, head.cWhCode);
  511. // if (!string.IsNullOrWhiteSpace(body.cBatch))
  512. // sql += " and cBatch='" + body.cBatch + "'";
  513. // DataTable dt = DBHelper.SQlReturnData(sql, cmd);
  514. // decimal qtyCurrent = 0;
  515. // if (dt != null && dt.Rows.Count > 0)
  516. // {
  517. // qtyCurrent = Convert.ToDecimal(dt.Rows[0][0]);
  518. // if (qtyCurrent < body.Quantity)
  519. // {
  520. // throw new Exception("物料编号:" + body.cInvCode + ",现存量不足!");
  521. // }
  522. // }
  523. // else
  524. // {
  525. // throw new Exception("该物料:" + body.cInvCode + "在U8中仓库:" + head.cWhCode + "中不存在!");
  526. // }
  527. // }
  528. // //}
  529. #endregion
  530. ICSUserInfo userInfo = new ICSUserInfo();
  531. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  532. #region 材料出库单表头
  533. //取得out库单的默认显示模版
  534. string[] dd = CRNALL.Split('~');
  535. string crdname = dd[0];
  536. string carname = dd[1];
  537. string surface = dd[2];
  538. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  539. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  540. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  541. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  542. DateTime date = DateTime.Now;
  543. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  544. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode, head.WorkPoint);
  545. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  546. //取得采购入库单单据 表头ID,表体DID
  547. VouchKey key = new VouchKey();
  548. sql = @"INSERT INTO dbo.rdrecord11
  549. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cBusCode ,cWhCode ,
  550. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cHandler ,cMemo ,
  551. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,cDefine4 ,
  552. cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,cDefine9 ,cDefine10 ,
  553. dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc ,cDefine11 ,
  554. cDefine12 ,cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  555. bOMFirst ,bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  556. ireturncount ,iverifystate ,iswfcontrolled ,cModifyPerson ,
  557. dnmaketime ,dnverifytime ,bredvouch ,bmotran ,bHYVouch ,iPrintCount
  558. )
  559. SELECT @ID,0,'11','','',a.cCode,@cWhCode,
  560. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,a.cDepCode,a.cPersonCode,@cHandler,a.cMemo,
  561. 0,@cMaker,a.cDefine1,cdefine2,a.cDefine3,a.cDefine4,
  562. a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,a.cDefine9,a.cDefine10,
  563. CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0,a.cDefine11,
  564. a.cDefine12,a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  565. 0,0,0,0,
  566. 0,0,0,'',
  567. GETDATE(),GETDATE(),0,0,0,0
  568. FROM dbo.MaterialAppVouch a WHERE a.cCode='" + head.SourceCode + "'";
  569. cmd.Parameters.Clear();
  570. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  571. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  572. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  573. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  574. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  575. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  576. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  577. //DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表头失败!");
  578. cmd.CommandText = sql;
  579. try
  580. {
  581. int count = cmd.ExecuteNonQuery();
  582. if (count <= 0)
  583. {
  584. log.Error("生成材料出库单表头失败,受影响行数<=0;");
  585. throw new Exception("生成材料出库单表头失败,受影响行数<=0;");
  586. }
  587. }
  588. catch (Exception ex)
  589. {
  590. log.Error("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  591. throw new Exception("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  592. }
  593. #endregion
  594. #region 材料出库单表体
  595. int irowno = 0;
  596. foreach (rdrecords11 body in head.details)
  597. {
  598. irowno++;
  599. //if (IsReturn == "1")
  600. //{
  601. //}
  602. //判断物料批号与现存量表批号是否一致and数量不能超过现存量物料数量
  603. //sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.InvCode + "'AND cBatch=''and cWhCode='" + head.WHCode + "' ";
  604. sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.InvCode + "'AND cBatch='" + body.BatchCode + @"'AND cFree1='" + body.cFree1 + @"' AND cFree2='" + body.cFree2 + @"' AND cFree3='" + body.cFree3 + @"' AND cFree4='" + body.cFree4 + @"' AND cFree5='" + body.cFree5 + @"' AND cFree6='" + body.cFree6 + @"' AND cFree7='" + body.cFree7 + @"' AND cFree8='" + body.cFree8 + @"' AND cFree9='" + body.cFree9 + @"' AND cFree10='" + body.cFree10 + @"' and cWhCode='" + head.WHCode + "'";
  605. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  606. if (dtItem != null && dtItem.Rows.Count > 0)
  607. {
  608. //if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cBatch))
  609. //{
  610. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.InvCode);
  611. //}
  612. if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < body.Quantity)
  613. {
  614. throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.InvCode);
  615. }
  616. sql = @"INSERT INTO dbo.rdrecords11
  617. ( AutoID ,ID ,cInvCode ,iQuantity ,iFlag ,cDefine22 ,iNum,iMPoIds,cmocode,imoseq,iOMoDID,iOMoMID,
  618. cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  619. cItem_class ,cItemCode ,cName ,cItemCName ,iNQuantity ,
  620. cDefine28 ,cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,
  621. cDefine33 ,cDefine34 ,cDefine35 ,cDefine36 ,cDefine37 ,
  622. bLPUseFree ,iOriTrackID ,bCosting ,bVMIUsed ,iMaIDs ,
  623. iExpiratDateCalcu ,iordertype ,isotype ,ipesotype ,irowno,
  624. cBatch,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  625. SELECT @AutoID,@ID,a.cInvCode,@iQuantity,0,a.cDefine22,@iNum,iMPoIds,cmocode,imoseq,iOMoDID,iOMoMID,
  626. a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  627. a.cItem_class,@cItemCode,a.cName,a.cItemCName,a.iQuantity,
  628. a.cDefine28,a.cDefine29,a.cDefine30,a.cDefine31,a.cDefine32,
  629. a.cDefine33,a.cDefine34,a.cDefine35,a.cDefine36,a.cDefine37,
  630. 0,0,1,0,a.AutoID,
  631. 0,0,0,0,@irowno,
  632. @cBatch,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  633. FROM dbo.MaterialAppVouchs a WHERE a.AutoID='" + body.SourceDetailID + "'";
  634. cmd.Parameters.Clear();
  635. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  636. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  637. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  638. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount.ToString("0.00")));
  639. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  640. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  641. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  642. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  643. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  644. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  645. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  646. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  647. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  648. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  649. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  650. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  651. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  652. //DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表头失败!");
  653. cmd.CommandText = sql;
  654. // SqlParameter[] paramBody = {
  655. // new SqlParameter("@AutoID",DbType.String),
  656. // new SqlParameter("@ID",DbType.String),
  657. // new SqlParameter("@iQuantity",DbType.Decimal),
  658. // new SqlParameter("@irowno",DbType.String),
  659. // new SqlParameter("@cBatch",DbType.String)
  660. //};
  661. //paramBody[0].Value = key.DID;
  662. //paramBody[1].Value = key.ID;
  663. //paramBody[2].Value = body.Quantity;
  664. //paramBody[3].Value = irowno;
  665. // paramBody[4].Value = body.cBatch;
  666. try
  667. {
  668. int count = cmd.ExecuteNonQuery();
  669. if (count <= 0)
  670. {
  671. log.Error("生成材料出库单表体失败,受影响行数<=0;");
  672. throw new Exception("生成材料出库单表体失败,受影响行数<=0;");
  673. }
  674. }
  675. catch (Exception ex)
  676. {
  677. log.Error("生成材料出库单表体失败!SQL:\r\n" + sql, ex);
  678. throw new Exception("生成材料出库单表体失败!SQL:\r\n" + sql, ex);
  679. }
  680. if (head.SourceType == "委外领料申请单")
  681. {
  682. #region 回写领料申请单已领数量
  683. sql = @"
  684. update OM_MOMaterials set iSendQTY=isnull(iSendQTY,0)+(" + body.Quantity + @"),fsendapplyqty=isnull(fsendapplyqty,0)+(" + body.Quantity + @") where MOMaterialsID=(select iOMoMID from MaterialAppVouchs where AutoID='" + body.SourceDetailID + @"')
  685. UPDATE dbo.MaterialAppVouchs SET fOutQuantity=ISNULL(fOutQuantity,0)+(" + body.Quantity + ") WHERE AutoID='" + body.SourceDetailID + "'";
  686. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写领料申请单已领数量失败!");
  687. #endregion
  688. }
  689. else
  690. {
  691. #region 回写领料申请单已领数量
  692. sql = @"
  693. update mom_moallocate set IssQty=isnull(IssQty,0)+(" + body.Quantity + "),RequisitionIssQty=isnull(RequisitionIssQty,0)+(" + body.Quantity + ") where AllocateId=(select iMPoIds from MaterialAppVouchs where AutoID='" + body.SourceDetailID + @"')
  694. UPDATE dbo.MaterialAppVouchs SET fOutQuantity=ISNULL(fOutQuantity,0)+(" + body.Quantity + ") WHERE AutoID='" + body.SourceDetailID + "'";
  695. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写领料申请单已领数量失败!");
  696. #endregion
  697. }
  698. sql = @"select isnull(fOutQuantity,0)fOutQuantity,isnull(iQuantity,0)iQuantity,ISNULL(iNum, 0)iNum,ISNULL(NiNum, 0)NiNum,ISNULL(iNum, 0)-ISNULL(NiNum, 0) SiNum from MaterialAppVouchs a
  699. LEFT JOIN (select sum(iNum) NiNum,iMaIDs from rdrecords11 GROUP BY iMaIDs) b on a.AutoID=b.iMaIDs
  700. where a.AutoID='" + body.SourceDetailID + "'";
  701. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  702. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  703. {
  704. foreach (DataRow item in dtDisCheck.Rows)
  705. {
  706. if (Convert.ToDecimal(item["fOutQuantity"].ToString()) == Convert.ToDecimal(item["iQuantity"].ToString()))
  707. {
  708. if (Convert.ToDecimal(item["iNum"].ToString()) != 0)
  709. {
  710. if (Convert.ToDecimal(item["SiNum"].ToString()) != 0)
  711. {
  712. sql = @"Update rdrecords11 set iNum=isnull(iNum,0)+(" + item["SiNum"].ToString() + ") where autoid='" + iChildId + "' ";
  713. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item["SiNum"].ToString()));
  714. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  715. }
  716. }
  717. }
  718. }
  719. }
  720. //更新现存量
  721. key.cBustypeUN = "领料";
  722. key.cVouchTypeUN = "11";
  723. key.TableName = "IA_ST_UnAccountVouch11";
  724. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key);
  725. //DBHelper.UpdateCurrentStockNEW(cmd, body.InvCode, head.WHCode, body.BatchCode, -body.Quantity, body.cFree1, body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10, key);
  726. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, -body.Quantity, -body.Amount, body.cFree1,
  727. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  728. "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  729. }
  730. else
  731. {
  732. throw new Exception("物料:" + body.InvCode + "在现存量表中不存在!");
  733. }
  734. iFatherIdTwo = "'" + iFatherId + "',";
  735. iChildId--;
  736. }
  737. #endregion
  738. //DBHelper.UpdateIDandDID("rd", ErpName, head.list.Count(), cmd);
  739. #region 查询
  740. sql = @"
  741. select DISTINCT a.ID as ID,a.ID as IDs,cMPoCode as SourceCode ,
  742. cCode as IssueCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,
  743. cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime,
  744. cHandler as Checker,dnverifytime as CheckDateTime
  745. from rdrecord11 a
  746. left join Department c on a.cDepCode=c.cDepCode
  747. left join Warehouse d on a.cWhCode=d.cWhCode
  748. WHERE a.ID in ({0})
  749. select DISTINCT a.ID as IDs,b.AutoID as DetailID,irowno as Sequence,b.cInvCode as InvCode
  750. ,iQuantity as Quantity,b.iMaIDs as SourceDetailID,iNum as Amount,
  751. isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  752. isnull(b.cFree1,'') as cFree1,
  753. isnull(b.cFree2,'') as cFree2,
  754. isnull(b.cFree3,'') as cFree3,
  755. isnull(b.cFree4,'') as cFree4,
  756. isnull(b.cFree5,'') as cFree5,
  757. isnull(b.cFree6,'') as cFree6,
  758. isnull(b.cFree7,'') as cFree7,
  759. isnull(b.cFree8,'') as cFree8,
  760. isnull(b.cFree9,'') as cFree9,
  761. isnull(b.cFree10,'') as cFree10
  762. from rdrecord11 a
  763. left join rdrecords11 b on a.ID=b.ID
  764. left join Department c on a.cDepCode=c.cDepCode
  765. left join Warehouse d on a.cWhCode=d.cWhCode
  766. WHERE a.ID in ({0})";
  767. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  768. if (ds != null)
  769. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  770. else
  771. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  772. //return msg;
  773. #endregion
  774. }
  775. cmd.Transaction.Commit();
  776. }
  777. catch (Exception ex)
  778. {
  779. cmd.Transaction.Rollback();
  780. log.Error(ex.Message);
  781. throw new Exception(ex.Message);
  782. }
  783. finally
  784. {
  785. if (conn.State == ConnectionState.Open)
  786. {
  787. conn.Close();
  788. }
  789. conn.Dispose();
  790. }
  791. }
  792. mag = JSON.DataSetToJson(ds, "details", "IDs");
  793. return mag;
  794. }
  795. /// <summary>
  796. /// 审核材料出库单据
  797. /// </summary>
  798. /// <param name="infos"></param>
  799. /// <returns></returns>
  800. public string Approve(List<ICSRdrecord11> infos)
  801. {
  802. List<ICSRdrecord11> szJson = new List<ICSRdrecord11>();
  803. DataTable dt = null;
  804. DataTable dtNew = null;
  805. string connS = "";
  806. string json = "";
  807. if (infos.Count <= 0)
  808. {
  809. throw new Exception("传送数据为空!");
  810. }
  811. string res = string.Empty;
  812. SqlConnection conn = new SqlConnection();
  813. SqlCommand cmd = new SqlCommand();
  814. string sql = string.Empty;
  815. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  816. foreach (string WorkPoint in result)
  817. {
  818. try
  819. {
  820. connS = string.Format(connString, WorkPoint);
  821. conn = new System.Data.SqlClient.SqlConnection(connS);
  822. conn.Open();
  823. SqlTransaction sqlTran = conn.BeginTransaction();
  824. cmd = new SqlCommand();
  825. cmd.Transaction = sqlTran;
  826. cmd.Connection = conn;
  827. foreach (ICSRdrecord11 info in infos)
  828. {
  829. if (WorkPoint != info.WorkPoint)
  830. continue;
  831. if (!DBHelper.IsInventoryConsolidation(cmd, info.WorkPoint))
  832. throw new Exception("U8正在整理现存量,请稍后再试");
  833. ICSUserInfo userInfo = new ICSUserInfo();
  834. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  835. if (info.MTime < new DateTime(2000, 01, 01))
  836. throw new Exception("请输入正确的操作时间:" + info.MTime);
  837. sql = @"UPDATE dbo.rdrecord11 SET cHandler='" + userInfo.UserName + @"' ,
  838. dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  839. WHERE ID='" + info.ID + "'";
  840. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核材料出库单失败!");
  841. }
  842. cmd.Transaction.Commit();
  843. }
  844. catch (Exception ex)
  845. {
  846. cmd.Transaction.Rollback();
  847. log.Error(ex.Message);
  848. throw new Exception(ex.Message);
  849. }
  850. finally
  851. {
  852. if (conn.State == ConnectionState.Open)
  853. {
  854. conn.Close();
  855. }
  856. conn.Dispose();
  857. }
  858. }
  859. return json;
  860. }
  861. /// <summary>
  862. /// 删除材料出库单据
  863. /// </summary>
  864. /// <param name="infos"></param>
  865. /// <returns></returns>
  866. public string Delete(List<ICSRdrecord11> infos)
  867. {
  868. List<ICSRdrecord11> szJson = new List<ICSRdrecord11>();
  869. DataTable dt = null;
  870. DataTable dtNew = null;
  871. string connS = "";
  872. string json = "";
  873. if (infos.Count <= 0)
  874. {
  875. throw new Exception("传送数据为空!");
  876. }
  877. string res = string.Empty;
  878. SqlConnection conn = new SqlConnection();
  879. SqlCommand cmd = new SqlCommand();
  880. string sql = string.Empty;
  881. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  882. foreach (string WorkPoint in result)
  883. {
  884. try
  885. {
  886. connS = string.Format(connString, WorkPoint);
  887. conn = new System.Data.SqlClient.SqlConnection(connS);
  888. conn.Open();
  889. SqlTransaction sqlTran = conn.BeginTransaction();
  890. cmd = new SqlCommand();
  891. cmd.Transaction = sqlTran;
  892. cmd.Connection = conn;
  893. foreach (ICSRdrecord11 info in infos)
  894. {
  895. if (WorkPoint != info.WorkPoint)
  896. continue;
  897. if (info.MTime < new DateTime(2000, 01, 01))
  898. throw new Exception("请输入正确的操作时间:" + info.MTime);
  899. sql = @"delete rdrecord11 where rdrecord11.ID='" + info.ID + "'";
  900. sql += @"delete rdrecords11 where rdrecords11.ID='" + info.ID + "'";
  901. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  902. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  903. // WHERE a.ID='" + info.ID + "'";
  904. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除材料出库单失败!");
  905. }
  906. cmd.Transaction.Commit();
  907. }
  908. catch (Exception ex)
  909. {
  910. cmd.Transaction.Rollback();
  911. log.Error(ex.Message);
  912. throw new Exception(ex.Message);
  913. }
  914. finally
  915. {
  916. if (conn.State == ConnectionState.Open)
  917. {
  918. conn.Close();
  919. }
  920. conn.Dispose();
  921. }
  922. }
  923. return json;
  924. }
  925. /// <summary>
  926. /// 记录日志
  927. /// </summary>
  928. /// <param name="Bills"></param>
  929. private void LogInfo(List<ICSRdrecord11> Bills)
  930. {
  931. string HeadList = string.Empty;
  932. string BodyList = string.Empty;
  933. foreach (ICSRdrecord11 head in Bills)
  934. {
  935. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  936. foreach (rdrecords11 body in head.details)
  937. {
  938. BodyList += "\r\n 表体主键ID: " + body.SourceDetailID + ",数量:" + body.Quantity;
  939. }
  940. }
  941. log.Info(HeadList);
  942. log.Info(BodyList);
  943. }
  944. }
  945. }