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

709 lines
38 KiB

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