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

1066 lines
64 KiB

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