爱思开
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.

1072 lines
65 KiB

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