纽威
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.

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