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

789 lines
40 KiB

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