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

737 lines
37 KiB

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. public string Get(List<ICSRdrecord11> infos)
  23. {
  24. List<ICSRdrecord11> szJson = new List<ICSRdrecord11>();
  25. DataTable dt = null;
  26. string json = "";
  27. if (infos.Count <= 0)
  28. {
  29. throw new Exception("传送数据为空!");
  30. }
  31. string res = string.Empty;
  32. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  33. conn.Open();
  34. SqlTransaction sqlTran = conn.BeginTransaction();
  35. SqlCommand cmd = new SqlCommand();
  36. cmd.Transaction = sqlTran;
  37. cmd.Connection = conn;
  38. try
  39. {
  40. string sql = string.Empty;
  41. foreach (ICSRdrecord11 info in infos)
  42. {
  43. if (info.MTime < new DateTime(2000, 01, 01))
  44. throw new Exception("请输入正确的操作时间:" + info.MTime);
  45. 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
  46. left join rdrecords11 b on a.ID=b.ID
  47. left join Department c on a.cDepCode=c.cDepCode
  48. left join Warehouse d on a.cWhCode=d.cWhCode
  49. left join mom_moallocate e on b.iMPoIds=e.AllocateId
  50. INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId
  51. INNER JOIN dbo.mom_order g ON f.MoId=g.MoId WHERE 1=1";
  52. if (!string.IsNullOrWhiteSpace(info.IssueCode))
  53. {
  54. sql += " and a.cCode='{0}'";
  55. }
  56. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  57. {
  58. sql += " and ISNULL(a.dnmodifytime ,ISNULL(a.dnverifytime , ISNULL(a.dnmodifytime , a.dnmaketime)))>='{1}'";
  59. }
  60. if (!string.IsNullOrWhiteSpace(info.User))
  61. {
  62. sql += " and a.CMAKER='{2}'";
  63. }
  64. sql = string.Format(sql, info.IssueCode, info.MTime, info.User);
  65. dt = DBHelper.SQlReturnData(sql, cmd);
  66. json = JsonConvert.SerializeObject(dt);
  67. }
  68. cmd.Transaction.Commit();
  69. return json;
  70. }
  71. catch (Exception ex)
  72. {
  73. cmd.Transaction.Rollback();
  74. log.Error(ex.Message);
  75. throw new Exception(ex.Message);
  76. }
  77. finally
  78. {
  79. if (conn.State == ConnectionState.Open)
  80. {
  81. conn.Close();
  82. }
  83. conn.Dispose();
  84. }
  85. }
  86. public string CreateRd11(List<ICSRdrecord11> Bills)
  87. {
  88. string msg = "";
  89. DataTable dt = null;
  90. bool ResultFlag = false;
  91. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  92. conn.Open();
  93. VouchKey key = new VouchKey();
  94. int num = 0;
  95. int iFatherId = 0;
  96. string iFatherIdTwo = string.Empty;
  97. int iChildId = 0;
  98. string sql = string.Empty;
  99. SqlTransaction sqlTran = conn.BeginTransaction();
  100. SqlCommand cmd = new SqlCommand();
  101. cmd.Transaction = sqlTran;
  102. cmd.Connection = conn;
  103. cmd.CommandTimeout = 300;
  104. try
  105. {
  106. if (Bills.Count <= 0)
  107. {
  108. throw new Exception("传送数据为空!");
  109. }
  110. LogInfo(Bills);
  111. //MergeObject(Bills, cmd);
  112. LogInfo(Bills);
  113. foreach (ICSRdrecord11 head in Bills)
  114. {
  115. num = head.details.Count();
  116. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + num + "");
  117. iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  118. iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  119. DateTime date = DateTime.Now;
  120. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  121. #region 判断工单是否已关闭
  122. sql = @"SELECT isnull(CloseUser,'') as CloseUser from mom_orderdetail a
  123. inner join mom_order b on a.MoId=b.MoId where b.MoCode='" + head.SourceCode + "' ";
  124. DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  125. if (dtClose != null && dtClose.Rows.Count > 0)
  126. {
  127. if (!string.IsNullOrEmpty(dtClose.Rows[0][0].ToString()))
  128. {
  129. throw new Exception("该工单行已关闭,无法领料!");
  130. }
  131. }
  132. #endregion
  133. #region 判断物料是否超额领取
  134. // foreach (rdrecords11 body in head.details)
  135. // {
  136. // sql = @"SELECT isnull(b.fOutExcess,0) as fOutExcess,isnull(a.IssQty,0) as IssQty,a.Qty,a.ReplenishQty FROM dbo.mom_moallocate a
  137. // LEFT JOIN dbo.Inventory b ON a.InvCode=b.cInvCode
  138. // WHERE a.AllocateId='" + body.SourceDetailID + "'";
  139. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  140. // if (dtQty != null && dtQty.Rows.Count > 0)
  141. // {
  142. // //可超领比率
  143. // decimal fOutExcess = Convert.ToDecimal(dtQty.Rows[0]["fOutExcess"]);
  144. // //已领数量
  145. // decimal IssQty = Convert.ToDecimal(dtQty.Rows[0]["IssQty"]);
  146. // //应领数量
  147. // decimal Qty = Convert.ToDecimal(dtQty.Rows[0]["Qty"]);
  148. // //当前可总领数量
  149. // decimal TotalQty = Qty;// + fOutExcess * Qty;
  150. // //补料数量
  151. // decimal ReplenishQty = Convert.ToDecimal(dtQty.Rows[0]["ReplenishQty"]);
  152. // string cql = @"SELECT * from Inventory where cInvCode='" + body.InvCode + "'AND bKCCutMantissa =1 AND bCutMantissa=1";
  153. // DataTable dtInv = DBHelper.SQlReturnData(cql, cmd);
  154. // if (dtInv != null && dtInv.Rows.Count > 0)
  155. // {
  156. // }
  157. // else
  158. // {
  159. // if (IssQty - ReplenishQty + body.Quantity > TotalQty)
  160. // {
  161. // throw new Exception("物料领取超过当前可领用最大数,AllocateId:" + body.SourceDetailID);
  162. // }
  163. // }
  164. // //if (IssQty + body.iQuantity > TotalQty)
  165. // //{
  166. // // throw new Exception("物料领取超过当前可领用最大数,AllocateId:" + body.AllocateId);
  167. // //}
  168. // }
  169. // else
  170. // {
  171. // throw new Exception("工单行子件不存在!AllocateId:" + body.SourceDetailID);
  172. // }
  173. // }
  174. #endregion
  175. #region 材料出库单表头
  176. sql = @"INSERT INTO dbo.rdrecord11
  177. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,
  178. cCode ,cRdCode ,cDepCode,cHandler ,bTransFlag ,cMaker ,
  179. dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc
  180. ,cPsPcode ,cMPoCode ,iproorderid ,bFromPreYear ,
  181. bIsLsQuery ,bIsComplement ,iDiscountTaxType ,ireturncount ,iverifystate ,
  182. iswfcontrolled ,dnmaketime ,dnverifytime ,bredvouch ,iPrintCount,
  183. iMQuantity)
  184. SELECT @ID,0,'11','','',@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),
  185. @cCode,'23',a.MDeptCode,@cHandler,0,@cMaker,
  186. CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0,
  187. a.InvCode,b.MoCode,a.MoId,0,
  188. 0,0,0,0,0,
  189. 0,GETDATE(),GETDATE(),0,0,
  190. a.Qty
  191. FROM dbo.mom_orderdetail a
  192. left JOIN dbo.mom_order b ON a.MoId=b.MoId
  193. left join mom_morder c on a.modid=c.modid
  194. WHERE b.MoCode='" + head.SourceType + "'";
  195. cmd.Parameters.Clear();
  196. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  197. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  198. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  199. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  200. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  201. cmd.Parameters.Add(new SqlParameter("@VT_ID", iFatherId));
  202. cmd.CommandText = sql;
  203. try
  204. {
  205. int count = cmd.ExecuteNonQuery();
  206. if (count <= 0)
  207. {
  208. log.Error("生成材料出库单表体失败,受影响行数<=0;");
  209. throw new Exception("生成材料出库单表体失败,受影响行数<=0;");
  210. }
  211. }
  212. catch (Exception ex)
  213. {
  214. log.Error("生成材料出库单表体失败" + sql, ex);
  215. throw new Exception("生成材料出库单表体失败" + sql, ex);
  216. }
  217. #endregion
  218. #region 材料出库单表体
  219. int irowno = 0;
  220. foreach (rdrecords11 body in head.details)
  221. {
  222. //if (irowno == 0)
  223. //{
  224. // iChildId = iChildId;
  225. //}
  226. //else
  227. //{
  228. // iChildId -= 1;
  229. //}
  230. //irowno += 1;
  231. //if (head.IsReturn == "1")
  232. //{
  233. // body.iQuantity = -body.iQuantity;
  234. //}
  235. //if (head.IsReturn == "0")
  236. //{
  237. // //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
  238. // sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.cInvCode + "'AND cBatch='" + body.cBatch + "'and cWhCode='" + head.cWhCode + "'";
  239. // DataTable dtItem = U8Helper.SQlReturnData(sql, cmd);
  240. // if (dtItem != null && dtItem.Rows.Count > 0)
  241. // {
  242. // if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cBatch))
  243. // {
  244. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.cInvCode);
  245. // }
  246. // if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < body.iQuantity)
  247. // {
  248. // throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.cInvCode);
  249. // }
  250. // }
  251. // else
  252. // {
  253. // throw new Exception("物料:" + body.cInvCode + "在现存量表中不存在!");
  254. // }
  255. //}
  256. sql = @"INSERT INTO dbo.rdrecords11
  257. ( AutoID ,ID ,cInvCode ,iQuantity ,cBatch ,iFlag ,
  258. cItemCode ,cName ,iNQuantity ,iMPoIds ,bLPUseFree ,
  259. iOriTrackID ,bCosting ,bVMIUsed ,cmocode ,invcode ,imoseq ,
  260. iopseq ,iExpiratDateCalcu ,iorderdid ,iordertype ,isotype ,
  261. ipesodid ,ipesotype ,cpesocode ,ipesoseq ,irowno ,
  262. bcanreplace ,iposflag)
  263. SELECT @AutoID,@ID,a.InvCode,@iQuantity,@cBatch,0,
  264. null,null,a.Qty,a.AllocateId,0,
  265. 0,1,0,c.MoCode,b.InvCode,b.SortSeq,
  266. '0000',0,0,0,0,
  267. a.AllocateId,'7',c.MoCode,b.SortSeq,@irowno,
  268. 0,null
  269. FROM dbo.mom_moallocate a
  270. INNER JOIN dbo.mom_orderdetail b ON a.MoDId=b.MoDId
  271. INNER JOIN dbo.mom_order c ON b.MoId=c.MoId
  272. WHERE a.AllocateId='" + body.SourceDetailID + "'";
  273. cmd.Parameters.Clear();
  274. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  275. cmd.Parameters.Add(new SqlParameter("@iQuantity",body.Quantity));
  276. //cmd.Parameters.Add(new SqlParameter("@cBatch", "''"));
  277. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  278. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  279. cmd.Parameters.Add(new SqlParameter("@cBatch",""));
  280. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  281. cmd.CommandText = sql;
  282. try
  283. {
  284. int count = cmd.ExecuteNonQuery();
  285. if (count <= 0)
  286. {
  287. log.Error("生成材料出库单表体失败,受影响行数<=0;");
  288. throw new Exception("生成材料出库单表体失败,受影响行数<=0;");
  289. }
  290. }
  291. catch (Exception ex)
  292. {
  293. log.Error("生成材料出库单表体失败"+ sql, ex);
  294. throw new Exception("生成材料出库单表体失败 "+ sql, ex);
  295. }
  296. //更新现存量
  297. key.cBustypeUN = "领料";
  298. key.cVouchTypeUN = "11";
  299. key.TableName = "IA_ST_UnAccountVouch11";
  300. DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  301. #region 回写工单子件已领数量
  302. sql = "update mom_moallocate set IssQty=isnull(IssQty,0)+" + body.Quantity + " where AllocateId='" + body.SourceDetailID + "' ";
  303. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写工单子件已领数量失败!");
  304. #endregion
  305. iChildId--;
  306. }
  307. #endregion
  308. iFatherIdTwo += "'" + iFatherId + "',";
  309. }
  310. cmd.Transaction.Commit();
  311. sql = @"
  312. 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
  313. left join rdrecords11 b on a.ID=b.ID
  314. left join Department c on a.cDepCode=c.cDepCode
  315. left join Warehouse d on a.cWhCode=d.cWhCode
  316. left join mom_moallocate e on b.iMPoIds=e.AllocateId
  317. INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId
  318. INNER JOIN dbo.mom_order g ON f.MoId=g.MoId WHERE a.ID in ({0}) ";
  319. sql = string.Format(sql, iFatherIdTwo);
  320. dt = DBHelper.SQlReturnData(sql, cmd);
  321. msg = JsonConvert.SerializeObject(dt);
  322. return msg;
  323. }
  324. catch (Exception ex)
  325. {
  326. cmd.Transaction.Rollback();
  327. log.Error(ex.Message);
  328. throw new Exception(ex.Message);
  329. }
  330. finally
  331. {
  332. if (conn.State == ConnectionState.Open)
  333. {
  334. conn.Close();
  335. }
  336. conn.Dispose();
  337. }
  338. }
  339. public string MaterialAppVouchs(List<ICSRdrecord11> Bills)
  340. {
  341. string mag = "";
  342. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  343. conn.Open();
  344. SqlTransaction sqlTran = conn.BeginTransaction();
  345. SqlCommand cmd = new SqlCommand();
  346. cmd.Transaction = sqlTran;
  347. cmd.Connection = conn;
  348. int num = 0;
  349. foreach (ICSRdrecord11 head in Bills)
  350. {
  351. num = head.details.Count();
  352. }
  353. try
  354. {
  355. if (Bills.Count <= 0)
  356. {
  357. throw new Exception("传送数据为空!");
  358. }
  359. LogInfo(Bills);
  360. //MergeObject(Bills, cmd);
  361. foreach (ICSRdrecord11 head in Bills)
  362. {
  363. string sql = string.Empty;
  364. #region 判断物料是否超额领取
  365. foreach (rdrecords11 body in head.details)
  366. {
  367. sql = @"SELECT isnull(fOutQuantity,0) as fOutQuantity,iQuantity FROM dbo.MaterialAppVouchs WHERE AutoID='" + body.SourceDetailID + "'";
  368. DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  369. if (dtQty != null && dtQty.Rows.Count > 0)
  370. {
  371. //已领数量
  372. decimal IssQty = Convert.ToDecimal(dtQty.Rows[0]["fOutQuantity"]);
  373. //应领数量
  374. decimal Qty = Convert.ToDecimal(dtQty.Rows[0]["iQuantity"]);
  375. if (IssQty + body.Quantity > Qty)
  376. {
  377. throw new Exception("物料领取超过当前可领用最大数,AutoID:" + body.SourceDetailID);
  378. }
  379. }
  380. else
  381. {
  382. throw new Exception("领料申请单不存在!AutoID:" + body.SourceDetailID);
  383. }
  384. }
  385. #endregion
  386. #region 现存量是否够领
  387. // //if (head.IsReturn != "1")
  388. // //{
  389. // foreach (rdrecords11 body in head.rdcords)
  390. // {
  391. // sql = string.Format(@"SELECT iQuantity FROM dbo.CurrentStock
  392. // WHERE cInvCode='{0}' AND cWhCode='{1}'", body.cInvCode, head.cWhCode);
  393. // if (!string.IsNullOrWhiteSpace(body.cBatch))
  394. // sql += " and cBatch='" + body.cBatch + "'";
  395. // DataTable dt = DBHelper.SQlReturnData(sql, cmd);
  396. // decimal qtyCurrent = 0;
  397. // if (dt != null && dt.Rows.Count > 0)
  398. // {
  399. // qtyCurrent = Convert.ToDecimal(dt.Rows[0][0]);
  400. // if (qtyCurrent < body.Quantity)
  401. // {
  402. // throw new Exception("物料编号:" + body.cInvCode + ",现存量不足!");
  403. // }
  404. // }
  405. // else
  406. // {
  407. // throw new Exception("该物料:" + body.cInvCode + "在U8中仓库:" + head.cWhCode + "中不存在!");
  408. // }
  409. // }
  410. // //}
  411. #endregion
  412. #region 材料出库单表头
  413. //取得out库单的默认显示模版
  414. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "PuArrival", "" + num + "");
  415. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  416. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  417. DateTime date = DateTime.Now;
  418. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  419. //取得采购入库单单据 表头ID,表体DID
  420. VouchKey key = new VouchKey();
  421. sql = @"INSERT INTO dbo.rdrecord11
  422. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cBusCode ,cWhCode ,
  423. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cHandler ,cMemo ,
  424. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,cDefine4 ,
  425. cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,cDefine9 ,cDefine10 ,
  426. dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc ,cDefine11 ,
  427. cDefine12 ,cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  428. bOMFirst ,bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  429. ireturncount ,iverifystate ,iswfcontrolled ,cModifyPerson ,
  430. dnmaketime ,dnverifytime ,bredvouch ,bmotran ,bHYVouch ,iPrintCount
  431. )
  432. SELECT @ID,0,'11','','',a.cCode,@cWhCode,
  433. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,a.cRdCode,a.cDepCode,a.cPersonCode,@cHandler,a.cMemo,
  434. 0,@cMaker,a.cDefine1,cdefine2,a.cDefine3,a.cDefine4,
  435. a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,a.cDefine9,a.cDefine10,
  436. CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0,a.cDefine11,
  437. a.cDefine12,a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  438. 0,0,0,0,
  439. 0,0,0,'',
  440. GETDATE(),GETDATE(),0,0,0,0
  441. FROM dbo.MaterialAppVouch a WHERE a.ID='" + head.ID + "'";
  442. cmd.Parameters.Clear();
  443. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  444. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  445. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  446. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  447. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  448. cmd.Parameters.Add(new SqlParameter("@VT_ID", iChildId));
  449. DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表头失败!");
  450. cmd.CommandText = sql;
  451. try
  452. {
  453. int count = cmd.ExecuteNonQuery();
  454. if (count <= 0)
  455. {
  456. log.Error("生成材料出库单表头失败,受影响行数<=0;");
  457. throw new Exception("生成材料出库单表头失败,受影响行数<=0;");
  458. }
  459. }
  460. catch (Exception ex)
  461. {
  462. log.Error("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  463. throw new Exception("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  464. }
  465. #endregion
  466. #region 材料出库单表体
  467. int irowno = 0;
  468. foreach (rdrecords11 body in head.details)
  469. {
  470. irowno += 1;
  471. iChildId -= 1;
  472. //if (head.IsReturn == "1")
  473. //{
  474. body.Quantity = -body.Quantity;
  475. //}
  476. //判断物料批号与现存量表批号是否一致and数量不能超过现存量物料数量
  477. // sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.InvCode + "'AND cBatch='" + body.cBatch + "'and cWhCode='" + head.WHCode + "' ";
  478. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  479. if (dtItem != null && dtItem.Rows.Count > 0)
  480. {
  481. //if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cBatch))
  482. //{
  483. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.InvCode);
  484. //}
  485. if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < body.Quantity)
  486. {
  487. throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.InvCode);
  488. }
  489. sql = @"INSERT INTO dbo.rdrecords11
  490. ( AutoID ,ID ,cInvCode ,iQuantity ,iFlag ,cDefine22 ,
  491. cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  492. cItem_class ,cItemCode ,cName ,cItemCName ,iNQuantity ,
  493. cDefine28 ,cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,
  494. cDefine33 ,cDefine34 ,cDefine35 ,cDefine36 ,cDefine37 ,
  495. bLPUseFree ,iOriTrackID ,bCosting ,bVMIUsed ,iMaIDs ,
  496. iExpiratDateCalcu ,iordertype ,isotype ,ipesotype ,irowno,
  497. cBatch)
  498. SELECT @AutoID,@ID,a.cInvCode,@iQuantity,0,a.cDefine22,
  499. a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  500. a.cItem_class,a.cItemCode,a.cName,a.cItemCName,a.iQuantity,
  501. a.cDefine28,a.cDefine29,a.cDefine30,a.cDefine31,a.cDefine32,
  502. a.cDefine33,a.cDefine34,a.cDefine35,a.cDefine36,a.cDefine37,
  503. 0,0,1,0,a.AutoID,
  504. 0,0,0,0,@irowno,
  505. @cBatch
  506. FROM dbo.MaterialAppVouchs a WHERE a.AutoID='" + body.SourceDetailID + "'";
  507. cmd.Parameters.Clear();
  508. SqlParameter[] paramBody = {
  509. new SqlParameter("@AutoID",DbType.String),
  510. new SqlParameter("@ID",DbType.String),
  511. new SqlParameter("@iQuantity",DbType.Decimal),
  512. new SqlParameter("@irowno",DbType.String),
  513. new SqlParameter("@cBatch",DbType.String)
  514. };
  515. paramBody[0].Value = key.DID;
  516. paramBody[1].Value = key.ID;
  517. paramBody[2].Value = body.Quantity;
  518. paramBody[3].Value = irowno;
  519. // paramBody[4].Value = body.cBatch;
  520. foreach (SqlParameter parameter in paramBody)
  521. {
  522. if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
  523. (parameter.Value == null))
  524. {
  525. parameter.Value = DBNull.Value;
  526. }
  527. cmd.Parameters.Add(parameter);
  528. }
  529. DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表体失败!");
  530. //更新现存量
  531. key.cBustypeUN = "领料";
  532. key.cVouchTypeUN = "11";
  533. key.TableName = "IA_ST_UnAccountVouch11";
  534. DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode,"", -body.Quantity, key);
  535. #region 回写领料申请单已领数量
  536. sql = "UPDATE dbo.MaterialAppVouchs SET fOutQuantity=ISNULL(fOutQuantity,0)+" + body.Quantity + " WHERE AutoID='" + body.SourceDetailID + "'";
  537. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写领料申请单已领数量失败!");
  538. #endregion
  539. }
  540. else
  541. {
  542. throw new Exception("物料:" + body.InvCode + "在现存量表中不存在!");
  543. }
  544. }
  545. #endregion
  546. //DBHelper.UpdateIDandDID("rd", ErpName, head.list.Count(), cmd);
  547. }
  548. cmd.Transaction.Commit();
  549. }
  550. catch (Exception ex)
  551. {
  552. cmd.Transaction.Rollback();
  553. log.Error(ex.Message);
  554. throw new Exception(ex.Message);
  555. }
  556. finally
  557. {
  558. if (conn.State == ConnectionState.Open)
  559. {
  560. conn.Close();
  561. }
  562. conn.Dispose();
  563. }
  564. return mag;
  565. }
  566. /// <summary>
  567. /// 审核材料出库单据
  568. /// </summary>
  569. /// <param name="infos"></param>
  570. /// <returns></returns>
  571. public string Approve(List<ICSRdrecord11> infos)
  572. {
  573. List<ICSRdrecord11> szJson = new List<ICSRdrecord11>();
  574. DataTable dt = null;
  575. string json = "";
  576. if (infos.Count <= 0)
  577. {
  578. throw new Exception("传送数据为空!");
  579. }
  580. string res = string.Empty;
  581. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  582. conn.Open();
  583. SqlTransaction sqlTran = conn.BeginTransaction();
  584. SqlCommand cmd = new SqlCommand();
  585. cmd.Transaction = sqlTran;
  586. cmd.Connection = conn;
  587. try
  588. {
  589. string sql = string.Empty;
  590. foreach (ICSRdrecord11 info in infos)
  591. {
  592. if (info.MTime < new DateTime(2000, 01, 01))
  593. throw new Exception("请输入正确的操作时间:" + info.MTime);
  594. sql = @"UPDATE dbo.rdrecord11 SET cHandler='" + info.User + @"' ,
  595. dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  596. WHERE ID='" + info.ID + "'";
  597. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核材料出库单失败!");
  598. }
  599. cmd.Transaction.Commit();
  600. return json;
  601. }
  602. catch (Exception ex)
  603. {
  604. cmd.Transaction.Rollback();
  605. log.Error(ex.Message);
  606. throw new Exception(ex.Message);
  607. }
  608. finally
  609. {
  610. if (conn.State == ConnectionState.Open)
  611. {
  612. conn.Close();
  613. }
  614. conn.Dispose();
  615. }
  616. }
  617. /// <summary>
  618. /// 审核材料出库单据
  619. /// </summary>
  620. /// <param name="infos"></param>
  621. /// <returns></returns>
  622. public string Delete(List<ICSRdrecord11> infos)
  623. {
  624. List<ICSRdrecord11> szJson = new List<ICSRdrecord11>();
  625. DataTable dt = null;
  626. string json = "";
  627. if (infos.Count <= 0)
  628. {
  629. throw new Exception("传送数据为空!");
  630. }
  631. string res = string.Empty;
  632. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  633. conn.Open();
  634. SqlTransaction sqlTran = conn.BeginTransaction();
  635. SqlCommand cmd = new SqlCommand();
  636. cmd.Transaction = sqlTran;
  637. cmd.Connection = conn;
  638. try
  639. {
  640. string sql = string.Empty;
  641. foreach (ICSRdrecord11 info in infos)
  642. {
  643. if (info.MTime < new DateTime(2000, 01, 01))
  644. throw new Exception("请输入正确的操作时间:" + info.MTime);
  645. sql = @"delete rdrecord11 where rdrecord11.ID='" + info.ID + "'";
  646. sql += @"delete rdrecords11 where rdrecords11.ID='" + info.ID + "'";
  647. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  648. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  649. // WHERE a.ID='" + info.ID + "'";
  650. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除材料出库单失败!");
  651. }
  652. cmd.Transaction.Commit();
  653. return json;
  654. }
  655. catch (Exception ex)
  656. {
  657. cmd.Transaction.Rollback();
  658. log.Error(ex.Message);
  659. throw new Exception(ex.Message);
  660. }
  661. finally
  662. {
  663. if (conn.State == ConnectionState.Open)
  664. {
  665. conn.Close();
  666. }
  667. conn.Dispose();
  668. }
  669. }
  670. /// <summary>
  671. /// 记录日志
  672. /// </summary>
  673. /// <param name="Bills"></param>
  674. private void LogInfo(List<ICSRdrecord11> Bills)
  675. {
  676. string HeadList = string.Empty;
  677. string BodyList = string.Empty;
  678. foreach (ICSRdrecord11 head in Bills)
  679. {
  680. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User;
  681. foreach (rdrecords11 body in head.details)
  682. {
  683. BodyList += "\r\n 表体主键ID: " + body.SourceDetailID + ",数量:" + body.Quantity;
  684. }
  685. }
  686. log.Info(HeadList);
  687. log.Info(BodyList);
  688. }
  689. /// <summary>
  690. /// 表体合并
  691. /// </summary>
  692. /// <param name="Bills"></param>
  693. //private void MergeObject(List<ICSRdrecord11> Bills, SqlCommand cmd)
  694. //{
  695. // foreach (ICSRdrecord11 head in Bills)
  696. // {
  697. // DataTable dt = DBHelper.ToDataTable(head.details.ToList());
  698. // DataTable dtNew = dt.Clone();
  699. // dtNew = DBHelper.MergeDataTable(dt, "AllocateId", "cBatch", "iQuantity", "cInvCode", cmd);
  700. // List<rdrecords11> list = DBHelper.ConvertTo<rdrecords11>(dtNew).ToList();
  701. // head.details = list;
  702. // }
  703. //}
  704. }
  705. }