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

707 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
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
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)
  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. foreach (ICSOutsourcingIssueDocs body in head.details)
  353. {
  354. body.Quantity = -body.Quantity;
  355. iNQuantity = "-a.iQuantity";
  356. if (head.SourceType != "委外领料申请单")
  357. {
  358. sql = @"INSERT INTO dbo.rdrecords11
  359. ( AutoID,ID ,cInvCode,iQuantity ,cBatch ,
  360. iFlag ,cItemCode ,cName ,iNQuantity ,iMPoIds ,
  361. bLPUseFree ,iOriTrackID ,bCosting ,bVMIUsed ,cmocode ,
  362. invcode ,imoseq ,iopseq ,iExpiratDateCalcu ,iorderdid ,
  363. iordertype ,isotype ,ipesodid ,ipesotype ,cpesocode ,
  364. ipesoseq ,irowno ,bcanreplace ,iposflag,iOMoDID,
  365. iOMoMID,comcode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  366. SELECT distinct
  367. @AutoID,@ID,a.cInvCode,@iQuantity,@cBatch,
  368. 0,null,null,@iQuantity,NULL,
  369. 0,0,1,0,NULL,
  370. b.cInvCode,NULL,NULL,0,0,
  371. 0,0,a.MOMaterialsID,'6',c.cCode,
  372. NULL, @irowno,0,NULL,b.MODetailsID,
  373. a.MOMaterialsID,c.cCode,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  374. FROM dbo.OM_MOMaterials a
  375. INNER JOIN dbo.OM_MODetails b ON a.MoDetailsID=b.MODetailsID
  376. INNER JOIN dbo.OM_MOMain c ON b.MOID=c.MOID
  377. WHERE a.MOMaterialsID='" + body.SourceDetailID + "'";
  378. cmd.Parameters.Clear();
  379. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  380. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  381. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  382. //cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  383. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  384. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  385. cmd.Parameters.Add(new SqlParameter("@iNQuantity", iNQuantity));
  386. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  387. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  388. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  389. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  390. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  391. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  392. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  393. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  394. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  395. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  396. cmd.CommandText = sql;
  397. }
  398. else
  399. {
  400. sql = @"INSERT INTO dbo.rdrecords11
  401. ( AutoID ,ID ,cInvCode ,iQuantity ,iFlag ,cDefine22 ,iOMoDID,iOMoMID,
  402. cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  403. cItem_class ,cItemCode ,cName ,cItemCName ,iNQuantity ,
  404. cDefine28 ,cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,
  405. cDefine33 ,cDefine34 ,cDefine35 ,cDefine36 ,cDefine37 ,
  406. bLPUseFree ,iOriTrackID ,bCosting ,bVMIUsed ,iMaIDs ,
  407. iExpiratDateCalcu ,iordertype ,isotype ,ipesotype ,irowno,
  408. cBatch,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  409. SELECT @AutoID,@ID,a.cInvCode,@iQuantity,0,a.cDefine22,a.iOMoDID,a.iOMoMID,
  410. a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  411. a.cItem_class,a.cItemCode,a.cName,a.cItemCName,@iQuantity,
  412. a.cDefine28,a.cDefine29,a.cDefine30,a.cDefine31,a.cDefine32,
  413. a.cDefine33,a.cDefine34,a.cDefine35,a.cDefine36,a.cDefine37,
  414. 0,0,1,0,a.AutoID,
  415. 0,0,0,0,@irowno,
  416. @cBatch,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  417. FROM dbo.MaterialAppVouchs a WHERE a.AutoID='" + body.SourceDetailID + "'";
  418. cmd.Parameters.Clear();
  419. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  420. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  421. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  422. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  423. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  424. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  425. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  426. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  427. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  428. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  429. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  430. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  431. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  432. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  433. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  434. //DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表头失败!");
  435. cmd.CommandText = sql;
  436. }
  437. try
  438. {
  439. int count = cmd.ExecuteNonQuery();
  440. if (count <= 0)
  441. {
  442. log.Error("生产退料单表体失败,受影响行数<=0;");
  443. throw new Exception("生产退料单表体失败,受影响行数<=0;");
  444. }
  445. }
  446. catch (Exception ex)
  447. {
  448. log.Error("生产退料单表体失败" + sql, ex);
  449. throw new Exception("生产退料单表体失败 " + sql, ex);
  450. }
  451. if (head.UpdateStock)
  452. {
  453. //更新现存量
  454. key.cBustypeUN = "委外发料";
  455. key.cVouchTypeUN = "11";
  456. key.TableName = "IA_ST_UnAccountVouch11";
  457. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key);
  458. 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);
  459. }
  460. if (head.SourceType != "委外领料申请单")
  461. {
  462. #region 回写工单子件已领数量
  463. sql = "update OM_MOMaterials set iSendQTY=isnull(iSendQTY,0)+" + body.Quantity + ",fsendapplyqty=isnull(fsendapplyqty,0)+" + body.Quantity + @" where MOMaterialsID='" + body.SourceDetailID + "' ";
  464. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写委外订单子件已领数量失败!");
  465. #endregion
  466. }
  467. else
  468. {
  469. #region 回写工单子件已领数量
  470. 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 + @"')
  471. update MaterialAppVouchs set fOutQuantity=isnull(fOutQuantity,0)+" + body.Quantity + @" where AutoID='" + body.SourceDetailID + @"'
  472. ";
  473. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写委外订单子件已领数量失败!");
  474. #endregion
  475. }
  476. iFatherIdTwo += "'" + iFatherId + "',";
  477. iChildId--;
  478. }
  479. #endregion
  480. #region 查询
  481. sql = @"select a.ID as ID,a.ID as IDs,a.cCode as IssueNEGCode,a.cDepCode,c.cDepName,a.cWhCode,
  482. d.cWhName,a.cSource,cMPoCode,a.cMaker,dnmaketime,cHandler,dnverifytime
  483. from rdrecord11 a
  484. left join Department c on a.cDepCode=c.cDepCode
  485. left join Warehouse d on a.cWhCode=d.cWhCode
  486. where a.cBusType='' and a.ID in ({0})
  487. 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
  488. from rdrecord11 a
  489. left join rdrecords11 b on a.ID=b.ID
  490. left join Department c on a.cDepCode=c.cDepCode
  491. left join Warehouse d on a.cWhCode=d.cWhCode
  492. left join OM_MOMaterials h on b.iOMoMID=h.MOMaterialsID
  493. left join MaterialAppVouchs f on b.iMaIDs=f.autoid
  494. where a.cBusType='' and a.ID in ({0})";
  495. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  496. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  497. if (dtNew1 == null)
  498. dtNew1 = ds.Tables[0];
  499. else
  500. dtNew1.Merge(ds.Tables[0]);
  501. if (dtNew2 == null)
  502. dtNew2 = ds.Tables[1];
  503. else
  504. dtNew2.Merge(ds.Tables[1]);
  505. // sql = @" select a.ID as ID,iMPoIds,iordercode,a.cCode,a.cDepCode,c.cDepName,a.cWhCode,
  506. // d.cWhName,a.cSource,a.cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,
  507. // b.AutoID ,b.irowno,b.cInvCode,b.iQuantity,e.MoDetailsID,b.iNum,e.csocode
  508. // from rdrecord11 a
  509. // left join rdrecords11 b on a.ID=b.ID
  510. // left join Department c on a.cDepCode=c.cDepCode
  511. // left join Warehouse d on a.cWhCode=d.cWhCode
  512. // left join OM_MODetails e on b.iOMoDID=e.MoDetailsID
  513. // where a.cBusType='委外发料'and b.iQuantity<0 and a.ID in ({0})";
  514. // sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  515. // dt = DBHelper.SQlReturnData(sql, cmd);
  516. #endregion
  517. cmd.Transaction.Commit();
  518. }
  519. catch (Exception ex)
  520. {
  521. cmd.Transaction.Rollback();
  522. log.Error(ex.Message);
  523. throw new Exception(ex.Message);
  524. }
  525. finally
  526. {
  527. if (conn.State == ConnectionState.Open)
  528. {
  529. conn.Close();
  530. }
  531. conn.Dispose();
  532. }
  533. }
  534. string RelationName = "details";
  535. DataSet dz = new DataSet();
  536. dtNew1.TableName = "Table0";
  537. dtNew2.TableName = "Table1";
  538. dz.Tables.Add(dtNew1.Copy());
  539. dz.Tables.Add(dtNew2.Copy());
  540. DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["IDs"], dz.Tables[1].Columns["IDs"]);
  541. dz.Relations.Add(dh);
  542. msg = DBHelper.DataSetToJson(dz, RelationName);
  543. return msg;
  544. }
  545. public string Approve(List<ICSOutsourcingIssueDoc> infos)
  546. {
  547. List<ICSOutsourcingIssueDoc> szJson = new List<ICSOutsourcingIssueDoc>();
  548. string connS = "";
  549. string json = "";
  550. if (infos.Count <= 0)
  551. {
  552. throw new Exception("传送数据为空!");
  553. }
  554. string res = string.Empty;
  555. SqlConnection conn = new SqlConnection();
  556. SqlCommand cmd = new SqlCommand();
  557. string sql = string.Empty;
  558. foreach (ICSOutsourcingIssueDoc info in infos)
  559. {
  560. try
  561. {
  562. connS = string.Format(connString, info.WorkPoint);
  563. conn = new System.Data.SqlClient.SqlConnection(connS);
  564. conn.Open();
  565. SqlTransaction sqlTran = conn.BeginTransaction();
  566. cmd = new SqlCommand();
  567. cmd.Transaction = sqlTran;
  568. cmd.Connection = conn;
  569. if (info.MTime < new DateTime(2000, 01, 01))
  570. throw new Exception("请输入正确的操作时间:" + info.MTime);
  571. sql = @"UPDATE dbo.rdrecord11 SET cHandler ='" + info.User + @"' ,
  572. dnmodifytime =CONVERT(VARCHAR(50),GETDATE(),112),dModifyDate =GETDATE() WHERE ID='{0}'";
  573. sql = string.Format(sql, info.ID);
  574. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  575. cmd.Transaction.Commit();
  576. }
  577. catch (Exception ex)
  578. {
  579. cmd.Transaction.Rollback();
  580. log.Error(ex.Message);
  581. throw new Exception(ex.Message);
  582. }
  583. finally
  584. {
  585. if (conn.State == ConnectionState.Open)
  586. {
  587. conn.Close();
  588. }
  589. conn.Dispose();
  590. }
  591. }
  592. return json;
  593. }
  594. /// <summary>
  595. /// 删除请购单
  596. /// </summary>
  597. /// <param name="infos"></param>
  598. /// <returns></returns>
  599. public string Delete(List<ICSOutsourcingIssueDoc> infos)
  600. {
  601. List<ICSOutsourcingIssueDoc> szJson = new List<ICSOutsourcingIssueDoc>();
  602. if (infos.Count <= 0)
  603. {
  604. throw new Exception("传送数据为空!");
  605. }
  606. DataTable dtNew = null;
  607. string connS = "";
  608. string res = string.Empty;
  609. SqlConnection conn = new SqlConnection();
  610. SqlCommand cmd = new SqlCommand();
  611. string sql = string.Empty;
  612. foreach (ICSOutsourcingIssueDoc info in infos)
  613. {
  614. try
  615. {
  616. connS = string.Format(connString, info.WorkPoint);
  617. conn = new System.Data.SqlClient.SqlConnection(connS);
  618. conn.Open();
  619. SqlTransaction sqlTran = conn.BeginTransaction();
  620. cmd = new SqlCommand();
  621. cmd.Transaction = sqlTran;
  622. cmd.Connection = conn;
  623. if (info.MTime < new DateTime(2000, 01, 01))
  624. throw new Exception("请输入正确的操作时间:" + info.MTime);
  625. sql = @" DELETE dbo.rdrecord11 WHERE ID='{0}'";
  626. sql = string.Format(sql, info.ID);
  627. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  628. cmd.Transaction.Commit();
  629. }
  630. catch (Exception ex)
  631. {
  632. cmd.Transaction.Rollback();
  633. log.Error(ex.Message);
  634. throw new Exception(ex.Message);
  635. }
  636. finally
  637. {
  638. if (conn.State == ConnectionState.Open)
  639. {
  640. conn.Close();
  641. }
  642. conn.Dispose();
  643. }
  644. }
  645. return res;
  646. }
  647. /// <summary>
  648. ///
  649. /// 记录日志
  650. /// </summary>
  651. /// <param name="Bills"></param>
  652. private void LogInfo(List<ICSOutsourcingIssueDoc> Bills)
  653. {
  654. string HeadList = string.Empty;
  655. string BodyList = string.Empty;
  656. foreach (ICSOutsourcingIssueDoc head in Bills)
  657. {
  658. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  659. foreach (ICSOutsourcingIssueDocs body in head.details)
  660. {
  661. BodyList += "\r\n 表体主键ID: " + body.SourceDetailID + ",数量:" + body.Quantity;
  662. }
  663. }
  664. log.Info(HeadList);
  665. log.Info(BodyList);
  666. }
  667. }
  668. }