爱思开
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.

1056 lines
59 KiB

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