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

1867 lines
109 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 ManufactureReceiveDoc
  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["ManuFaCRDNAME"];
  23. private static string CRNA = System.Configuration.ConfigurationManager.AppSettings["MoICRDNAME"];
  24. /// <summary>
  25. /// 获取成品入库单
  26. /// </summary>
  27. /// <param name="infos"></param>
  28. /// <returns></returns>
  29. public string Get(List<ICSManufactureReceiveDoc> infos)
  30. {
  31. List<ICSManufactureReceiveDoc> szJson = new List<ICSManufactureReceiveDoc>();
  32. DataTable dt = null;
  33. DataTable dtNew = null;
  34. string connS = "";
  35. string json = "";
  36. if (infos.Count <= 0)
  37. {
  38. throw new Exception("传送数据为空!");
  39. }
  40. string res = string.Empty;
  41. SqlConnection conn = new SqlConnection();
  42. SqlCommand cmd = new SqlCommand();
  43. string sql = string.Empty;
  44. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  45. foreach (string WorkPoint in result)
  46. {
  47. try
  48. {
  49. connS = string.Format(connString, WorkPoint);
  50. conn = new System.Data.SqlClient.SqlConnection(connS);
  51. conn.Open();
  52. SqlTransaction sqlTran = conn.BeginTransaction();
  53. cmd = new SqlCommand();
  54. cmd.Transaction = sqlTran;
  55. cmd.Connection = conn;
  56. foreach (ICSManufactureReceiveDoc info in infos)
  57. {
  58. if (WorkPoint != info.WorkPoint)
  59. continue;
  60. ICSUserInfo userInfo = new ICSUserInfo();
  61. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  62. if (info.MTime < new DateTime(2000, 01, 01))
  63. throw new Exception("请输入正确的操作时间:" + info.MTime);
  64. sql = @" select a.ID,a.cCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,a.cMPoCode ,a.cMaker ,a.dnmaketime ,a.cHandler ,a.dnverifytime ,
  65. b.AutoID,b.iRSRowNO ,b.cInvCode ,b.iQuantity ,b.iNum ,b.iMPoIds from rdrecord10 a
  66. left join rdrecords10 b on a.ID=b.ID
  67. left join Department c on a.cDepCode=c.cDepCode
  68. left join Warehouse d on a.cWhCode=d.cWhCode WHERE 1=1";
  69. if (!string.IsNullOrWhiteSpace(info.MRCVCode))
  70. {
  71. sql += " and a.cCode='{0}'";
  72. }
  73. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  74. {
  75. sql += " and ISNULL(a.dnmodifytime,ISNULL(a.dnverifytime, ISNULL(a.dnmodifytime, a.dnmaketime)))>='{1}'";
  76. }
  77. if (!string.IsNullOrWhiteSpace(info.User))
  78. {
  79. sql += "and a.CMAKER='{2}'";
  80. }
  81. sql = string.Format(sql, info.MRCVCode, info.MTime, userInfo.UserName);
  82. dt = DBHelper.SQlReturnData(sql, cmd);
  83. if (dt.Rows.Count <= 0 || dt == null)
  84. throw new Exception("成品入库单:" + info.MRCVCode + ",无信息!");
  85. if (dtNew == null)
  86. dtNew = dt;
  87. else
  88. dtNew.Merge(dt);
  89. cmd.CommandType = CommandType.Text;
  90. cmd.Parameters.Clear();
  91. }
  92. cmd.Transaction.Commit();
  93. }
  94. catch (Exception ex)
  95. {
  96. if (cmd.Transaction != null)
  97. cmd.Transaction.Rollback();
  98. log.Error(ex.Message);
  99. throw new Exception(ex.Message);
  100. }
  101. finally
  102. {
  103. if (conn.State == ConnectionState.Open)
  104. {
  105. conn.Close();
  106. }
  107. conn.Dispose();
  108. }
  109. }
  110. json = JsonConvert.SerializeObject(dtNew);
  111. return json;
  112. }
  113. /// <summary>
  114. /// 创建成品入库单
  115. /// </summary>
  116. /// <param name="Bills"></param>
  117. /// <returns></returns>
  118. public string CreateManufactureReceiveDoc(List<ICSManufactureReceiveDoc> Bills)
  119. {
  120. int moidinbatch = 0;
  121. string msg = "";
  122. DataSet ds=null;
  123. string iFatherIdTwo = "";
  124. string iFatherIdTree = "";
  125. int isif = 0;
  126. string connS = "";
  127. string MoClass = "";
  128. int num = 0;
  129. int news = 0;
  130. int out11 = 0;
  131. int iseq = 0;
  132. int irownnew = 0;
  133. string sql = string.Empty;
  134. string mocode = "";
  135. string seq = "";
  136. //取得out库单的默认显示模版
  137. //取得采购入库单单据 表头ID,表体DID
  138. VouchKey key = new VouchKey();
  139. SqlConnection conn = new SqlConnection();
  140. SqlCommand cmd = new SqlCommand();
  141. if (Bills.Count <= 0)
  142. {
  143. throw new Exception("传送数据为空!");
  144. }
  145. LogInfo(Bills);
  146. foreach (ICSManufactureReceiveDoc head in Bills)
  147. {
  148. foreach (ICSManufactureReceiveDocs load in head.details)
  149. {
  150. foreach (ICSManufactureReceiveDocss loads in load.detailss)
  151. {
  152. news++;
  153. }
  154. }
  155. }
  156. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  157. foreach (string WorkPoint in result)
  158. {
  159. try
  160. {
  161. connS = string.Format(connString, WorkPoint);
  162. conn = new System.Data.SqlClient.SqlConnection(connS);
  163. conn.Open();
  164. SqlTransaction sqlTran = conn.BeginTransaction();
  165. cmd = new SqlCommand();
  166. cmd.Transaction = sqlTran;
  167. cmd.Connection = conn;
  168. foreach (ICSManufactureReceiveDoc head in Bills)
  169. {
  170. moidinbatch=0;
  171. iFatherIdTwo = "";
  172. iFatherIdTree = "";
  173. if (WorkPoint != head.WorkPoint)
  174. continue;
  175. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  176. throw new Exception("U8正在整理现存量,请稍后再试");
  177. ICSUserInfo userInfo = new ICSUserInfo();
  178. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  179. string[] ss = head.WorkPoint.Split('_');
  180. ERPDB = ss[1];
  181. string[] dd = CRNALL.Split('~');
  182. string crdname = dd[0];
  183. string carname = dd[1];
  184. string surface = dd[2];
  185. string[] d = CRNA.Split('~');
  186. string crdname1 = d[0];
  187. string carname1 = d[1];
  188. string surface1 = d[2];
  189. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  190. string cardnewcode1 = DBHelper.GetCardNumber(carname1, cmd);
  191. num = head.details.Count();
  192. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint, cmd);
  193. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  194. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  195. DateTime date = DateTime.Now;
  196. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  197. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  198. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode, head.WorkPoint, cmd);
  199. string cRDcode1 = DBHelper.GetRDCode(crdname1, cmd);
  200. string DEF_ID1 = DBHelper.GetDefaultTemplate(cardnewcode1, cmd);
  201. //Person personEntity = new Person();
  202. //personEntity = U8Helper.GetDepCodeByUserCode(head.UserCode, cmd);
  203. DateTime dvDate = DateTime.Now;
  204. #region 产成品入库单表头
  205. sql = @"INSERT INTO dbo.rdrecord10
  206. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,
  207. cCode ,cRdCode ,cDepCode ,cHandler ,bTransFlag ,cMaker ,
  208. cDefine1 ,cDefine2 ,cDefine3 ,cDefine4 ,cDefine5 ,cDefine6 ,
  209. cDefine7 ,cDefine8 ,cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,
  210. biafirst ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,cDefine13 ,
  211. cDefine14 ,cDefine15 ,cDefine16 ,cMPoCode ,iproorderid ,
  212. bFromPreYear ,bIsComplement ,iDiscountTaxType ,ireturncount ,
  213. iverifystate ,iswfcontrolled ,cModifyPerson ,dnmaketime ,
  214. dnverifytime ,bredvouch ,iPrintCount )
  215. SELECT @ID,1,'10','','',@cWhCode,convert(datetime,convert(varchar(10),getdate(),120)),
  216. @cCode,@RdCode,@DepCode,@cHandler,0,@cMaker,
  217. a.Define1,a.Define2,a.Define3,a.Define4,a.Define5,a.Define6,
  218. a.Define7,a.Define8,a.Define9,a.Define10,@dDate,0,
  219. 0,@VT_ID,0,a.Define11,a.Define12,a.Define13,
  220. a.Define14,a.Define15,a.Define16,a.MoCode,a.MoId,
  221. 0,0,0,0,
  222. 0,0,'',@dDate,
  223. @dDate,0,0
  224. FROM dbo.mom_order a
  225. WHERE a.MoCode='" + head.MOCode + "'";
  226. cmd.Parameters.Clear();
  227. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  228. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  229. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  230. cmd.Parameters.Add(new SqlParameter("@cDepCode", head.DepCode));
  231. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  232. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  233. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  234. cmd.Parameters.Add(new SqlParameter("@DepCode", head.DepCode));
  235. cmd.Parameters.Add(new SqlParameter("@dDate", dvDate));
  236. cmd.Parameters.Add(new SqlParameter("@RdCode", cRDcode));
  237. cmd.CommandText = sql;
  238. try
  239. {
  240. int count = cmd.ExecuteNonQuery();
  241. if (count <= 0)
  242. {
  243. log.Error("生成成品入库单表头失败,受影响行数<=0;");
  244. throw new Exception("生成成品入库单表头失败,受影响行数<=0;");
  245. }
  246. }
  247. catch (Exception ex)
  248. {
  249. log.Error("生成成品入库单表头失败!SQL:\r\n" + sql, ex);
  250. throw new Exception("生成成品入库单表头失败!SQL:\r\n" + sql, ex);
  251. }
  252. List<WH> LWH = new List<WH>();
  253. #endregion
  254. if (news != 0)
  255. {
  256. foreach (ICSManufactureReceiveDocs load in head.details)
  257. {
  258. foreach (ICSManufactureReceiveDocss loads in load.detailss)
  259. {
  260. WH wh = new WH();
  261. wh.WHCODE = loads.WHCode;
  262. wh.seq = 0;
  263. if (LWH.Count != 0)
  264. {
  265. int whnumber = 0;
  266. foreach (WH item in LWH)
  267. {
  268. if (item.WHCODE == wh.WHCODE)
  269. whnumber++;
  270. }
  271. if (whnumber == 0)
  272. LWH.Add(wh);
  273. }
  274. else
  275. LWH.Add(wh);
  276. }
  277. }
  278. foreach (WH item in LWH)
  279. {
  280. Dictionary<string, int> dic1 = DBHelper.GetAllCode("" + ERPDB + "", surface1, "1", head.WorkPoint, cmd);
  281. int iFatherId1 = Convert.ToInt32(dic1["iFatherId"].ToString());
  282. int iChildId1 = Convert.ToInt32(dic1["iChildId"].ToString());
  283. DateTime date1 = DateTime.Now;
  284. string iBaseCodeLen1 = DBHelper.GetAllRDCode(cardnewcode1, "" + date1 + "", "admin", cRDcode1, head.WorkPoint, cmd);
  285. #region 材料出库单表头
  286. sql = @"INSERT INTO dbo.rdrecord11
  287. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,cBusCode,
  288. cCode ,cRdCode ,cDepCode,cHandler ,bTransFlag ,cMaker ,
  289. dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc
  290. ,cPsPcode ,cMPoCode ,iproorderid ,bFromPreYear ,
  291. bIsLsQuery ,bIsComplement ,iDiscountTaxType ,ireturncount ,iverifystate ,
  292. iswfcontrolled ,dnmaketime ,dnverifytime ,bredvouch ,iPrintCount,
  293. iMQuantity)
  294. SELECT TOP 1 @ID,0,'11','','',@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cBusCode,
  295. @cCode,@cRdCode,a.MDeptCode,@cHandler,0,@cMaker,
  296. CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0,
  297. null,b.MoCode,a.MoId,0,
  298. 0,0,0,0,0,
  299. 0,GETDATE(),GETDATE(),1,1,
  300. null
  301. FROM dbo.mom_orderdetail a
  302. left JOIN dbo.mom_order b ON a.MoId=b.MoId
  303. left join mom_morder c on a.modid=c.modid
  304. WHERE b.MoCode='" + head.MOCode + "'";
  305. cmd.Parameters.Clear();
  306. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId1));
  307. cmd.Parameters.Add(new SqlParameter("@cBusCode", iBaseCodeLen));
  308. cmd.Parameters.Add(new SqlParameter("@cWhCode", item.WHCODE));
  309. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen1));
  310. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  311. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  312. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID1));
  313. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode1));
  314. //cmd.Parameters.Add(new SqlParameter("@bredvouch",0));
  315. cmd.CommandText = sql;
  316. try
  317. {
  318. int count = cmd.ExecuteNonQuery();
  319. if (count <= 0)
  320. {
  321. log.Error("生成成品入库单表头失败,受影响行数<=0;");
  322. throw new Exception("生成成品入库单表头失败,受影响行数<=0;");
  323. }
  324. }
  325. catch (Exception ex)
  326. {
  327. log.Error("生成成品入库单表头失败!SQL:\r\n" + sql, ex);
  328. throw new Exception("生成成品入库单表头失败!SQL:\r\n" + sql, ex);
  329. }
  330. item.ID = iFatherId1;
  331. #endregion
  332. }
  333. }
  334. #region 产成品入库单表体
  335. int irow = 0;
  336. foreach (ICSManufactureReceiveDocs body in head.details)
  337. {
  338. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  339. {
  340. if (body.BatchCode == "")
  341. {
  342. log.Error("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  343. throw new Exception("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  344. }
  345. }
  346. else
  347. {
  348. if (body.BatchCode != "")
  349. {
  350. log.Error("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  351. throw new Exception("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  352. }
  353. }
  354. irow++;
  355. #region 判断工单行是否关闭,关闭不能入库
  356. sql = @"SELECT ISNULL(a.CloseUser,'') as CloseUser,b.MoCode,a.SortSeq FROM dbo.mom_orderdetail a INNER JOIN
  357. dbo.mom_order b ON b.MoId = a.MoId WHERE a.MoDId = '" + body.MODetailID + "'";
  358. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  359. if (dtCheck != null && dtCheck.Rows.Count > 0)
  360. {
  361. mocode = dtCheck.Rows[0]["MoCode"].ToString();
  362. seq = dtCheck.Rows[0]["SortSeq"].ToString();
  363. if (!string.IsNullOrEmpty(dtCheck.Rows[0]["CloseUser"].ToString()))
  364. {
  365. throw new Exception("工单号:" + dtCheck.Rows[0]["MoCode"].ToString() + ",行号:" + dtCheck.Rows[0]["SortSeq"].ToString() + ",已关闭,无法入库!");
  366. }
  367. sql = @" select
  368. count(c.AllocateID) num,d.number
  369. from DBO.mom_order a
  370. left join DBO.mom_orderdetail b on a.moid = b.moid
  371. left join DBO.mom_moallocate c on b.modid = c.modid
  372. left join(select modid,COUNT(AllocateID) number from DBO.mom_moallocate where WIPType=1 GROUP BY modid
  373. ) d on b.modid = d.modid
  374. where a.MoCode='{0}' and b.SortSeq='{1}'
  375. GROUP BY d.number ";
  376. sql = string.Format(sql, mocode, seq);
  377. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  378. if (dtCheck != null && dtCheck.Rows.Count > 0)
  379. {
  380. if (dtCheck.Rows[0]["num"].ToString() != dtCheck.Rows[0]["number"].ToString())
  381. {
  382. isif = 1;
  383. }
  384. else
  385. {
  386. isif = 0;
  387. }
  388. }
  389. }
  390. #endregion
  391. #region 判断是否为非标
  392. sql = @"select MoClass from dbo.mom_orderdetail where MoDId='" + body.MODetailID + "'";
  393. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  394. if (dtCheck != null && dtCheck.Rows.Count > 0)
  395. {
  396. MoClass = dtCheck.Rows[0]["MoClass"].ToString();
  397. }
  398. #endregion
  399. #region 生成产成品入库单表体
  400. //if (head.IsReturn == "1")
  401. //{
  402. // body.iQuantity = -body.iQuantity;
  403. //}
  404. moidinbatch = iChildId;
  405. sql = @"INSERT INTO dbo.rdrecords10
  406. ( AutoID ,ID ,cInvCode ,iQuantity ,cBatch ,iFlag ,iNum,cbMemo,
  407. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  408. cItemCode ,cName ,iNQuantity ,cDefine28 ,
  409. cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,cDefine33 ,cDefine34 ,
  410. cDefine35 ,cDefine36 ,cDefine37 ,iMPoIds ,bRelated ,bLPUseFree ,
  411. iRSRowNO ,iOriTrackID ,bCosting ,cmocode ,imoseq ,iExpiratDateCalcu ,
  412. iorderdid ,iordertype,iordercode,iorderseq,isodid,isotype,csocode,isoseq,irowno ,iposflag ,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  413. SELECT @AutoID,@ID,@cInvCode,@iQuantity,@cBatch,0,@iNum,a.Remark,
  414. a.Define22,a.Define23,a.Define24,a.Define25,a.Define26,a.Define27,
  415. @cItemCode,null,a.Qty-isnull(a.QualifiedInQty,0),a.Define28,
  416. a.Define29,a.Define30,a.Define31,a.Define32,a.Define33,a.Define34,
  417. a.Define35,a.Define36,a.Define37,a.MoDId,0,0,
  418. 0,0,1,b.MoCode,a.SortSeq,0,
  419. a.OrderDid,a.OrderType,a.OrderCode,a.OrderSeq,a.SoDId,a.SoType,a.SoCode,a.SoSeq,@irowno,0,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  420. FROM dbo.mom_orderdetail a
  421. INNER JOIN dbo.mom_order b ON a.MoId=b.MoId WHERE a.MoDId='" + body.MODetailID + "'";
  422. cmd.Parameters.Clear();
  423. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  424. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  425. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  426. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount.ToString("0.00")));
  427. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  428. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  429. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  430. cmd.Parameters.Add(new SqlParameter("@irowno", irow));
  431. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  432. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  433. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  434. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  435. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  436. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  437. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  438. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  439. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  440. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  441. cmd.CommandText = sql;
  442. try
  443. {
  444. int count = cmd.ExecuteNonQuery();
  445. if (count <= 0)
  446. {
  447. log.Error("生成材料出库单表头失败,受影响行数<=0;");
  448. throw new Exception("生成材料出库单表头失败,受影响行数<=0;");
  449. }
  450. }
  451. catch (Exception ex)
  452. {
  453. log.Error("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  454. throw new Exception("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  455. }
  456. #endregion
  457. if (MoClass!="2")
  458. {
  459. if (isif == 1)
  460. {
  461. //SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  462. if (head.CompleteVerification)
  463. {
  464. #region 齐套检验
  465. sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' ";
  466. DataTable qt = DBHelper.SQlReturnData(sql, cmd);
  467. if (qt != null && qt.Rows.Count > 0)
  468. {
  469. if (qt.Rows[0]["cValue"].ToString() == "1")
  470. {
  471. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  472. (
  473. select
  474. a.MoCode,b.SortSeq,
  475. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  476. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,
  477. (case when min (c.IssQty) is null then b.Qty else
  478. (case when min(c.IssQty) =sum(c.Qty) then b.Qty else min(IssQty/BaseQtyN) end)
  479. end) lotqty
  480. from DBO.mom_order a
  481. left join DBO.mom_orderdetail b on a.moid = b.moid
  482. left join DBO.mom_moallocate c on b.modid = c.modid
  483. left join
  484. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  485. DBO.mom_moallocatesub d
  486. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  487. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  488. where a.MoCode='{0}' and b.SortSeq='{1}'
  489. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  490. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  491. ) tt
  492. group by MoCode,SortSeq";
  493. sql = string.Format(sql, mocode, seq);
  494. qt = DBHelper.SQlReturnData(sql, cmd);
  495. if (qt == null || qt.Rows.Count == 0)
  496. {
  497. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  498. }
  499. else
  500. {
  501. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  502. {
  503. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料<=0!");
  504. }
  505. }
  506. }
  507. else if (qt.Rows[0]["cValue"].ToString() == "2")
  508. {
  509. sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'";
  510. qt = DBHelper.SQlReturnData(sql, cmd);
  511. if (qt != null && qt.Rows.Count > 0)
  512. {
  513. if (qt.Rows[0]["cValue"].ToString() == "true")
  514. {
  515. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  516. (
  517. select
  518. a.MoCode,b.SortSeq,
  519. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  520. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,
  521. (case when min (c.IssQty) is null then b.Qty else
  522. (case when min(c.IssQty) =sum(c.Qty) then b.Qty else min(IssQty/BaseQtyN) end)
  523. end) lotqty
  524. from DBO.mom_order a
  525. left join DBO.mom_orderdetail b on a.moid = b.moid
  526. left join DBO.mom_moallocate c on b.modid = c.modid
  527. left join
  528. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  529. DBO.mom_moallocatesub d
  530. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  531. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  532. where a.MoCode='{0}' and b.SortSeq='{1}'
  533. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1 and f.bInvKeyPart=1
  534. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  535. ) tt
  536. group by MoCode,SortSeq";
  537. sql = string.Format(sql, mocode, seq);
  538. qt = DBHelper.SQlReturnData(sql, cmd);
  539. if (qt == null || qt.Rows.Count == 0)
  540. {
  541. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  542. }
  543. else
  544. {
  545. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  546. {
  547. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  548. }
  549. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity < 0)
  550. {
  551. throw new Exception("该物料:" + body.InvCode + @"入库数量大于关键物料领料套数!");
  552. }
  553. }
  554. }
  555. else
  556. {
  557. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  558. (
  559. select
  560. a.MoCode,b.SortSeq,
  561. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  562. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,
  563. (case when min (c.IssQty) is null then b.Qty else
  564. (case when min(c.IssQty) =sum(c.Qty) then b.Qty else min(IssQty/BaseQtyN) end)
  565. end) lotqty
  566. from DBO.mom_order a
  567. left join DBO.mom_orderdetail b on a.moid = b.moid
  568. left join DBO.mom_moallocate c on b.modid = c.modid
  569. left join
  570. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  571. DBO.mom_moallocatesub d
  572. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  573. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  574. where a.MoCode='{0}' and b.SortSeq='{1}'
  575. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  576. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  577. ) tt
  578. group by MoCode,SortSeq";
  579. sql = string.Format(sql, mocode, seq);
  580. qt = DBHelper.SQlReturnData(sql, cmd);
  581. if (qt == null || qt.Rows.Count == 0)
  582. {
  583. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  584. }
  585. else
  586. {
  587. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  588. {
  589. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  590. }
  591. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity < 0)
  592. {
  593. throw new Exception("该物料:" + body.InvCode + @"入库数量大于领料套数!");
  594. }
  595. }
  596. }
  597. }
  598. }
  599. }
  600. #endregion
  601. }
  602. }
  603. }
  604. sql = @"select isnull(QualifiedInQty,0)fOutQuantity,isnull(Qty,0)iQuantity,ISNULL(AuxQty, 0)iNum,ISNULL(NiNum, 0)NiNum,ISNULL(AuxQty, 0)-ISNULL(NiNum, 0) SiNum from mom_orderdetail a
  605. LEFT JOIN (select sum(iNum) NiNum,iMPoIds from rdrecords10 GROUP BY iMPoIds) b on a.MoDId=b.iMPoIds
  606. where a.MoDId='" + body.MODetailID + "'";
  607. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  608. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  609. {
  610. foreach (DataRow item in dtDisCheck.Rows)
  611. {
  612. if (Convert.ToDecimal(item["fOutQuantity"].ToString()) == Convert.ToDecimal(item["iQuantity"].ToString()))
  613. {
  614. if (Convert.ToDecimal(item["iNum"].ToString()) != 0)
  615. {
  616. if (Convert.ToDecimal(item["SiNum"].ToString()) != 0)
  617. {
  618. sql = @"Update rdrecords10 set iNum=isnull(iNum,0)+(" + item["SiNum"].ToString() + ") where autoid='" + iChildId + "' ";
  619. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item["SiNum"].ToString()));
  620. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  621. }
  622. }
  623. }
  624. }
  625. }
  626. #region 回写生产订单入库数量
  627. sql = @"UPDATE dbo.mom_orderdetail SET QualifiedInQty=ISNULL(QualifiedInQty,0)+" + body.Quantity + " where MoDId='" + body.MODetailID + "'";
  628. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写生产订单入库数量失败!");
  629. #endregion
  630. #region 更新现存量
  631. key.cBustypeUN = "成品入库";
  632. key.cVouchTypeUN = "10";
  633. key.TableName = "IA_ST_UnAccountVouch10";
  634. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  635. //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);
  636. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, body.Quantity, body.Amount, body.cFree1,
  637. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  638. "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  639. #endregion
  640. if (Convert.ToInt32(MoClass) == 1)
  641. {
  642. #region 判断工单行是否全部入库
  643. sql = "select Qty from mom_moallocate WHERE MoDId='" + body.MODetailID + @"' and IssQty-qty<=0 and ProductType=1
  644. union all
  645. select Qty from mom_orderdetail where MoDId='" + body.MODetailID + @"' and QualifiedInQty-Qty<=0";
  646. DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  647. if (dtClose.Rows.Count == 0)
  648. {
  649. sql = @"
  650. update mom_orderdetail set CloseTime = GETDATE(),CloseDate= CONVERT(NVARCHAR(15),GETDATE(),23), CloseUser ='" + head.User + "',Status=4 where MODId='" + body.MODetailID + "'";
  651. DBHelper.CmdExecuteNonQuery(sql, cmd, "生产订单行关闭失败!");
  652. }
  653. //foreach (DataRow dr in dtClose.Rows)
  654. //{
  655. // if (Convert.ToDecimal(dr["qty"]) >= 0)
  656. // {
  657. // sql = "update mom_orderdetail set CloseTime = GETDATE(),CloseDate= CONVERT(NVARCHAR(15),GETDATE(),23), CloseUser ='" + head.User + "',Status=4 where MODId='" + body.MODetailID + "'";
  658. // DBHelper.CmdExecuteNonQuery(sql, cmd, "生产订单行关闭失败!");
  659. // }
  660. //}
  661. #endregion
  662. }
  663. else
  664. {
  665. #region 判断工单行是否全部入库
  666. sql = "select Qty from mom_moallocate WHERE MoDId='" + body.MODetailID + @"' and IssQty-qty<=0
  667. union all
  668. select Qty from mom_orderdetail where MoDId='" + body.MODetailID + @"' and QualifiedInQty-Qty<=0
  669. ";
  670. DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  671. if (dtClose.Rows.Count == 0)
  672. {
  673. sql = @"
  674. update mom_orderdetail set CloseTime = GETDATE(),CloseDate= CONVERT(NVARCHAR(15),GETDATE(),23), CloseUser ='" + head.User + "',Status=4 where MODId='" + body.MODetailID + "'";
  675. DBHelper.CmdExecuteNonQuery(sql, cmd, "生产订单行关闭失败!");
  676. }
  677. //foreach (DataRow dr in dtClose.Rows)
  678. //{
  679. // if (Convert.ToDecimal(dr["qty"]) >= 0)
  680. // {
  681. // sql = "update mom_orderdetail set CloseTime = GETDATE(),CloseDate= CONVERT(NVARCHAR(15),GETDATE(),23), CloseUser ='" + head.User + "',Status=4 where MODId='" + body.MODetailID + "'";
  682. // DBHelper.CmdExecuteNonQuery(sql, cmd, "生产订单行关闭失败!");
  683. // }
  684. //}
  685. #endregion
  686. }
  687. num = body.detailss.Count();
  688. Dictionary<string, int> dic2 = DBHelper.GetAllCode("" + ERPDB + "", surface1, "" + num + "", head.WorkPoint, cmd);
  689. int iFatherId2 = Convert.ToInt32(dic2["iFatherId"].ToString());
  690. int iChildId2 = Convert.ToInt32(dic2["iChildId"].ToString());
  691. foreach (ICSManufactureReceiveDocss item in body.detailss)
  692. {
  693. if (item.PickID != null && item.PickID != "")
  694. {
  695. foreach (WH wh in LWH)
  696. {
  697. if (wh.WHCODE == item.WHCode)
  698. {
  699. out11 = wh.ID;
  700. iseq = wh.seq + 1;
  701. wh.seq += 1;
  702. }
  703. }
  704. sql = @"INSERT INTO dbo.rdrecords11
  705. ( AutoID ,ID ,cInvCode ,iQuantity ,cBatch ,iFlag ,iNum,
  706. cItemCode ,cName ,iNQuantity ,iMPoIds ,bLPUseFree ,
  707. iOriTrackID ,bCosting ,bVMIUsed ,cmocode ,invcode ,imoseq ,
  708. iopseq ,iExpiratDateCalcu ,iorderdid ,iordertype ,isotype ,
  709. ipesodid ,ipesotype ,cpesocode ,ipesoseq ,irowno ,
  710. bcanreplace ,iposflag,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  711. SELECT @AutoID,@ID,a.InvCode,@iQuantity,@cBatch,0,@iNum,
  712. @cItemCode,null,a.Qty,a.AllocateId,0,
  713. 0,1,0,c.MoCode,b.InvCode,b.SortSeq,
  714. '0000',0,0,0,0,
  715. a.AllocateId,'7',c.MoCode,b.SortSeq,@irowno,
  716. 0,null,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  717. FROM dbo.mom_moallocate a
  718. INNER JOIN dbo.mom_orderdetail b ON a.MoDId=b.MoDId
  719. INNER JOIN dbo.mom_order c ON b.MoId=c.MoId
  720. WHERE a.AllocateId='" + item.PickID + "'";
  721. cmd.Parameters.Clear();
  722. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId2));
  723. cmd.Parameters.Add(new SqlParameter("@ID", out11));
  724. cmd.Parameters.Add(new SqlParameter("@iQuantity", item.Quantity));
  725. cmd.Parameters.Add(new SqlParameter("@iNum", item.Amount));
  726. cmd.Parameters.Add(new SqlParameter("@cBatch", item.BatchCode));
  727. cmd.Parameters.Add(new SqlParameter("@cItemCode", item.ProjectCode));
  728. cmd.Parameters.Add(new SqlParameter("@irowno", iseq));
  729. cmd.Parameters.Add(new SqlParameter("@cFree1", item.cFree1));
  730. cmd.Parameters.Add(new SqlParameter("@cFree2", item.cFree2));
  731. cmd.Parameters.Add(new SqlParameter("@cFree3", item.cFree3));
  732. cmd.Parameters.Add(new SqlParameter("@cFree4", item.cFree4));
  733. cmd.Parameters.Add(new SqlParameter("@cFree5", item.cFree5));
  734. cmd.Parameters.Add(new SqlParameter("@cFree6", item.cFree6));
  735. cmd.Parameters.Add(new SqlParameter("@cFree7", item.cFree7));
  736. cmd.Parameters.Add(new SqlParameter("@cFree8", item.cFree8));
  737. cmd.Parameters.Add(new SqlParameter("@cFree9", item.cFree9));
  738. cmd.Parameters.Add(new SqlParameter("@cFree10", item.cFree10));
  739. cmd.CommandText = sql;
  740. try
  741. {
  742. int count = cmd.ExecuteNonQuery();
  743. if (count <= 0)
  744. {
  745. log.Error("生成材料出库单表体失败,受影响行数<=0;");
  746. throw new Exception("生成材料出库单表体失败,受影响行数<=0;");
  747. }
  748. }
  749. catch (Exception ex)
  750. {
  751. log.Error("生成材料出库单表体失败" + sql, ex);
  752. throw new Exception("生成材料出库单表体失败 " + sql, ex);
  753. }
  754. #region 回写工单子件已领数量
  755. sql = "update mom_moallocate set IssQty=isnull(IssQty,0)+" + item.Quantity + ",RequisitionIssQty=isnull(RequisitionIssQty,0)+(" + item.Quantity + ") where AllocateId='" + item.PickID + "' ";
  756. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写工单子件已领数量失败!");
  757. #endregion
  758. sql = @"select isnull(Issqty,0)fOutQuantity,isnull(qty,0)iQuantity,ISNULL(AuxQty, 0)iNum,ISNULL(NiNum, 0)NiNum,ISNULL(AuxQty, 0)-ISNULL(NiNum, 0) SiNum from mom_moallocate a
  759. LEFT JOIN (select sum(iNum) NiNum,ipesodid from rdrecords11 GROUP BY ipesodid) b on a.AllocateId=b.ipesodid
  760. where a.AllocateId='" + item.PickID + "'";
  761. dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  762. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  763. {
  764. foreach (DataRow item1 in dtDisCheck.Rows)
  765. {
  766. if (Convert.ToDecimal(item1["fOutQuantity"].ToString()) == Convert.ToDecimal(item1["iQuantity"].ToString()))
  767. {
  768. if (Convert.ToDecimal(item1["iNum"].ToString()) != 0)
  769. {
  770. if (Convert.ToDecimal(item1["SiNum"].ToString()) != 0)
  771. {
  772. sql = @"Update rdrecords11 set iNum=isnull(iNum,0)+(" + item1["SiNum"].ToString() + ") where autoid='" + iChildId2 + "' ";
  773. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item1["SiNum"].ToString()));
  774. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  775. }
  776. }
  777. }
  778. }
  779. }
  780. //更新现存量
  781. key.cBustypeUN = "领料";
  782. key.cVouchTypeUN = "11";
  783. key.TableName = "IA_ST_UnAccountVouch11";
  784. //DBHelper.UpdateCurrentStockNEW(cmd, item.InvCode, head.WHCode, item.BatchCode, -item.Quantity, item.cFree1, item.cFree2, item.cFree3, item.cFree4, item.cFree5, item.cFree6, item.cFree7, item.cFree8, item.cFree9, item.cFree10, key);
  785. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, -body.Quantity, -body.Amount, body.cFree1,
  786. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  787. "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, out11, iChildId2);
  788. iFatherIdTree += "'" + out11 + "',";
  789. iChildId2--;
  790. }
  791. }
  792. iFatherIdTwo = "'" + iFatherId + "',";
  793. iChildId--;
  794. }
  795. #endregion
  796. #region 查询
  797. if (news == 0)
  798. {
  799. sql = @" select DISTINCT a.ID as ID,a.ID as IDs,a.cCode as MRCVCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName
  800. ,a.cMPoCode as MOCode ,a.cMaker as CreateUser ,a.dnmaketime as CreateDateTime
  801. ,a.cHandler as Checker ,a.dnverifytime as CheckDateTime
  802. from rdrecord10 a
  803. left join Department c on a.cDepCode=c.cDepCode
  804. left join Warehouse d on a.cWhCode=d.cWhCode WHERE A.cHandler IS NOT NULL AND A.ID in({0})
  805. select DISTINCT a.ID as IDs, b.AutoID as DetailID,CAST(b.AutoID as char) as IDSS ,b.irowno as Sequence ,b.cInvCode as InvCode ,
  806. b.iQuantity as Quantity ,b.iNum as Amount,b.iMPoIds as MODetailID,
  807. isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  808. isnull(b.cFree1,'') as cFree1,
  809. isnull(b.cFree2,'') as cFree2,
  810. isnull(b.cFree3,'') as cFree3,
  811. isnull(b.cFree4,'') as cFree4,
  812. isnull(b.cFree5,'') as cFree5,
  813. isnull(b.cFree6,'') as cFree6,
  814. isnull(b.cFree7,'') as cFree7,
  815. isnull(b.cFree8,'') as cFree8,
  816. isnull(b.cFree9,'') as cFree9,
  817. isnull(b.cFree10,'') as cFree10
  818. from rdrecord10 a
  819. left join rdrecords10 b on a.ID=b.ID
  820. left join Department c on a.cDepCode=c.cDepCode
  821. left join Warehouse d on a.cWhCode=d.cWhCode WHERE A.cHandler IS NOT NULL AND A.ID in({0})
  822. select DISTINCT a.ID as IDs,b.AutoID as DetailID,CAST('{2}' as char) as IDSS,a.cCode as MRCVCode,irowno as Sequence,b.cInvCode as InvCode
  823. ,iQuantity as Quantity,e.AllocateId as SourceDetailID,iNum as Amount,
  824. isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  825. isnull(b.cFree1,'') as cFree1,
  826. isnull(b.cFree2,'') as cFree2,
  827. isnull(b.cFree3,'') as cFree3,
  828. isnull(b.cFree4,'') as cFree4,
  829. isnull(b.cFree5,'') as cFree5,
  830. isnull(b.cFree6,'') as cFree6,
  831. isnull(b.cFree7,'') as cFree7,
  832. isnull(b.cFree8,'') as cFree8,
  833. isnull(b.cFree9,'') as cFree9,
  834. isnull(b.cFree10,'') as cFree10
  835. from rdrecord11 a
  836. left join rdrecords11 b on a.ID=b.ID
  837. left join Department c on a.cDepCode=c.cDepCode
  838. left join Warehouse d on a.cWhCode=d.cWhCode
  839. left join mom_moallocate e on b.iMPoIds=e.AllocateId
  840. INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId
  841. INNER JOIN dbo.mom_order g ON f.MoId=g.MoId WHERE a.ID in ({1})
  842. ";
  843. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','), "''", moidinbatch);
  844. }
  845. else
  846. {
  847. sql = @" select DISTINCT a.ID as ID,a.ID as IDs,a.cCode as MRCVCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName
  848. ,a.cMPoCode as MOCode ,a.cMaker as CreateUser ,a.dnmaketime as CreateDateTime
  849. ,a.cHandler as Checker ,a.dnverifytime as CheckDateTime
  850. from rdrecord10 a
  851. left join Department c on a.cDepCode=c.cDepCode
  852. left join Warehouse d on a.cWhCode=d.cWhCode WHERE A.cHandler IS NOT NULL AND A.ID in({0})
  853. select DISTINCT a.ID as IDs, b.AutoID as DetailID,CAST(b.AutoID as char) as IDSS ,b.irowno as Sequence ,b.cInvCode as InvCode ,
  854. b.iQuantity as Quantity ,b.iNum as Amount,b.iMPoIds as MODetailID ,
  855. isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  856. isnull(b.cFree1,'') as cFree1,
  857. isnull(b.cFree2,'') as cFree2,
  858. isnull(b.cFree3,'') as cFree3,
  859. isnull(b.cFree4,'') as cFree4,
  860. isnull(b.cFree5,'') as cFree5,
  861. isnull(b.cFree6,'') as cFree6,
  862. isnull(b.cFree7,'') as cFree7,
  863. isnull(b.cFree8,'') as cFree8,
  864. isnull(b.cFree9,'') as cFree9,
  865. isnull(b.cFree10,'') as cFree10
  866. from rdrecord10 a
  867. left join rdrecords10 b on a.ID=b.ID
  868. left join Department c on a.cDepCode=c.cDepCode
  869. left join Warehouse d on a.cWhCode=d.cWhCode WHERE A.cHandler IS NOT NULL AND A.ID in({0})
  870. select DISTINCT a.ID as IDs,b.AutoID as DetailID,CAST('{2}' as char) as IDSS,a.cCode as MRCVCode,irowno as Sequence,b.cInvCode as InvCode
  871. ,iQuantity as Quantity,e.AllocateId as SourceDetailID,iNum as Amount,a.ID ERPID,b.AutoID as ERPDetailID,
  872. isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  873. isnull(b.cFree1,'') as cFree1,
  874. isnull(b.cFree2,'') as cFree2,
  875. isnull(b.cFree3,'') as cFree3,
  876. isnull(b.cFree4,'') as cFree4,
  877. isnull(b.cFree5,'') as cFree5,
  878. isnull(b.cFree6,'') as cFree6,
  879. isnull(b.cFree7,'') as cFree7,
  880. isnull(b.cFree8,'') as cFree8,
  881. isnull(b.cFree9,'') as cFree9,
  882. isnull(b.cFree10,'') as cFree10
  883. from rdrecord11 a
  884. left join rdrecords11 b on a.ID=b.ID
  885. left join Department c on a.cDepCode=c.cDepCode
  886. left join Warehouse d on a.cWhCode=d.cWhCode
  887. left join mom_moallocate e on b.iMPoIds=e.AllocateId
  888. INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId
  889. INNER JOIN dbo.mom_order g ON f.MoId=g.MoId WHERE a.ID in ({1})
  890. ";
  891. if (iFatherIdTree.TrimEnd(',')=="")
  892. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','), "''", moidinbatch);
  893. else
  894. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','), iFatherIdTree.TrimEnd(','), moidinbatch);
  895. }
  896. if (ds != null)
  897. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  898. else
  899. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  900. #endregion
  901. cmd.CommandType = CommandType.Text;
  902. cmd.Parameters.Clear();
  903. }
  904. cmd.Transaction.Commit();
  905. }
  906. catch (Exception ex)
  907. {
  908. if (cmd.Transaction != null)
  909. cmd.Transaction.Rollback();
  910. log.Error(ex.Message);
  911. throw new Exception(ex.Message);
  912. }
  913. finally
  914. {
  915. if (conn.State == ConnectionState.Open)
  916. {
  917. conn.Close();
  918. }
  919. conn.Dispose();
  920. }
  921. }
  922. if (news != 0)
  923. {
  924. msg = JSON.DataSetToJson(ds, "details", "IDs", "detailss", "IDSS");
  925. }
  926. else
  927. {
  928. msg = JSON.DataSetToJson(ds, "details", "IDs");
  929. }
  930. return msg;
  931. }
  932. /// <summary>
  933. /// 创建联副产品
  934. /// </summary>
  935. /// <param name="Bills"></param>
  936. /// <returns></returns>
  937. public string CreateManufactureReceiveDoc1(List<ICSManufactureReceiveDoc> Bills)
  938. {
  939. string msg = "";
  940. int isif = 0;
  941. DataSet ds = null;
  942. string connS = "";
  943. int num = 0;
  944. string sql = string.Empty;
  945. string mocode = "";
  946. string seq = "";
  947. //取得out库单的默认显示模版
  948. string iFatherIdTwo = "";
  949. //取得采购入库单单据 表头ID,表体DID
  950. VouchKey key = new VouchKey();
  951. SqlConnection conn = new SqlConnection();
  952. SqlCommand cmd = new SqlCommand();
  953. if (Bills.Count <= 0)
  954. {
  955. throw new Exception("传送数据为空!");
  956. }
  957. LogInfo(Bills);
  958. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  959. foreach (string WorkPoint in result)
  960. {
  961. try
  962. {
  963. connS = string.Format(connString, WorkPoint);
  964. conn = new System.Data.SqlClient.SqlConnection(connS);
  965. conn.Open();
  966. SqlTransaction sqlTran = conn.BeginTransaction();
  967. cmd = new SqlCommand();
  968. cmd.Transaction = sqlTran;
  969. cmd.Connection = conn;
  970. foreach (ICSManufactureReceiveDoc head in Bills)
  971. {
  972. iFatherIdTwo = "";
  973. if (WorkPoint != head.WorkPoint)
  974. continue;
  975. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  976. throw new Exception("U8正在整理现存量,请稍后再试");
  977. string[] ss = head.WorkPoint.Split('_');
  978. ERPDB = ss[1];
  979. string[] dd = CRNALL.Split('~');
  980. string crdname = dd[0];
  981. string carname = dd[1];
  982. string surface = dd[2];
  983. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  984. ICSUserInfo userInfo = new ICSUserInfo();
  985. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  986. num = head.details.Count();
  987. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint, cmd);
  988. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  989. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  990. DateTime date = DateTime.Now;
  991. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  992. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  993. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode, head.WorkPoint, cmd);
  994. //Person personEntity = new Person();
  995. //personEntity = U8Helper.GetDepCodeByUserCode(head.UserCode, cmd);
  996. DateTime dvDate = DateTime.Now;
  997. #region 产成品入库单表头
  998. sql = @"INSERT INTO dbo.rdrecord10
  999. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,
  1000. cCode ,cRdCode ,cDepCode ,cHandler ,bTransFlag ,cMaker ,
  1001. cDefine1 ,cDefine2 ,cDefine3 ,cDefine4 ,cDefine5 ,cDefine6 ,
  1002. cDefine7 ,cDefine8 ,cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,
  1003. biafirst ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,cDefine13 ,
  1004. cDefine14 ,cDefine15 ,cDefine16 ,cMPoCode ,iproorderid ,
  1005. bFromPreYear ,bIsComplement ,iDiscountTaxType ,ireturncount ,
  1006. iverifystate ,iswfcontrolled ,cModifyPerson ,dnmaketime ,
  1007. dnverifytime ,bredvouch ,iPrintCount )
  1008. SELECT @ID,1,'10','','',@cWhCode,convert(datetime,convert(varchar(10),getdate(),120)),
  1009. @cCode,@RdCode,@DepCode,@cHandler,0,@cMaker,
  1010. a.Define1,a.Define2,a.Define3,a.Define4,a.Define5,a.Define6,
  1011. a.Define7,a.Define8,a.Define9,a.Define10,@dDate,0,
  1012. 0,@VT_ID,0,a.Define11,a.Define12,a.Define13,
  1013. a.Define14,a.Define15,a.Define16,a.MoCode,a.MoId,
  1014. 0,0,0,0,
  1015. 0,0,'',@dDate,
  1016. @dDate,0,0
  1017. FROM dbo.mom_order a
  1018. WHERE a.MoCode='" + head.MOCode + "'";
  1019. cmd.Parameters.Clear();
  1020. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  1021. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  1022. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  1023. cmd.Parameters.Add(new SqlParameter("@cDepCode", head.DepCode));
  1024. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  1025. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  1026. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  1027. cmd.Parameters.Add(new SqlParameter("@DepCode", head.DepCode));
  1028. cmd.Parameters.Add(new SqlParameter("@dDate", dvDate));
  1029. cmd.Parameters.Add(new SqlParameter("@RdCode", cRDcode));
  1030. cmd.CommandText = sql;
  1031. try
  1032. {
  1033. int count = cmd.ExecuteNonQuery();
  1034. if (count <= 0)
  1035. {
  1036. log.Error("生成成品入库单表头失败,受影响行数<=0;");
  1037. throw new Exception("生成成品入库单表头失败,受影响行数<=0;");
  1038. }
  1039. }
  1040. catch (Exception ex)
  1041. {
  1042. log.Error("生成成品入库单表头失败!SQL:\r\n" + sql, ex);
  1043. throw new Exception("生成成品入库单表头失败!SQL:\r\n" + sql, ex);
  1044. }
  1045. #endregion
  1046. #region 产成品入库单表体
  1047. int irowno = 0;
  1048. foreach (ICSManufactureReceiveDocs body in head.details)
  1049. {
  1050. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  1051. {
  1052. if (body.BatchCode == "")
  1053. {
  1054. log.Error("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  1055. throw new Exception("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  1056. }
  1057. }
  1058. else
  1059. {
  1060. if (body.BatchCode != "")
  1061. {
  1062. log.Error("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  1063. throw new Exception("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  1064. }
  1065. }
  1066. irowno++;
  1067. #region 判断工单行是否关闭,关闭不能入库
  1068. sql = @"SELECT ISNULL(a.CloseUser,'') as CloseUser,b.MoCode,a.SortSeq FROM dbo.mom_orderdetail a INNER JOIN
  1069. dbo.mom_order b ON b.MoId = a.MoId WHERE a.MoDId =(select MoDId from dbo.mom_moallocate where AllocateId='" + body.MODetailID + "')";
  1070. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  1071. if (dtCheck != null && dtCheck.Rows.Count > 0)
  1072. {
  1073. mocode = dtCheck.Rows[0]["MoCode"].ToString();
  1074. seq = dtCheck.Rows[0]["SortSeq"].ToString();
  1075. if (!string.IsNullOrEmpty(dtCheck.Rows[0]["CloseUser"].ToString()))
  1076. {
  1077. throw new Exception("工单号:" + dtCheck.Rows[0]["MoCode"].ToString() + ",行号:" + dtCheck.Rows[0]["SortSeq"].ToString() + ",已关闭,无法入库!");
  1078. }
  1079. sql = @" select
  1080. count(c.AllocateID) num,d.number
  1081. from DBO.mom_order a
  1082. left join DBO.mom_orderdetail b on a.moid = b.moid
  1083. left join DBO.mom_moallocate c on b.modid = c.modid
  1084. left join(select modid,COUNT(AllocateID) number from DBO.mom_moallocate where WIPType=1 GROUP BY modid
  1085. ) d on b.modid = d.modid
  1086. where a.MoCode='{0}' and b.SortSeq='{1}'
  1087. GROUP BY d.number ";
  1088. sql = string.Format(sql, mocode, seq);
  1089. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  1090. if (dtCheck != null && dtCheck.Rows.Count > 0)
  1091. {
  1092. if (dtCheck.Rows[0]["num"].ToString() != dtCheck.Rows[0]["number"].ToString())
  1093. {
  1094. isif = 1;
  1095. }
  1096. else
  1097. {
  1098. isif = 0;
  1099. }
  1100. }
  1101. }
  1102. #endregion
  1103. #region 判断是否都是联.副产品
  1104. sql = @"select ProductType from dbo.mom_moallocate where AllocateId='" + body.MODetailID + "'";
  1105. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  1106. if (dtCheck != null && dtCheck.Rows.Count > 0)
  1107. {
  1108. string ProductType = dtCheck.Rows[0]["ProductType"].ToString();
  1109. if (Convert.ToInt32(ProductType) == 1)
  1110. {
  1111. throw new Exception("该返工工单存在非联、副产品子件,无法入库!");
  1112. }
  1113. }
  1114. #endregion
  1115. #region 生成产成品入库单表体
  1116. //if (head.IsReturn == "1")
  1117. //{
  1118. // body.iQuantity = -body.iQuantity;
  1119. //}
  1120. sql = @"INSERT INTO dbo.rdrecords10
  1121. ( AutoID ,ID ,cInvCode ,iQuantity ,cBatch ,iFlag ,iNum,cbMemo,
  1122. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  1123. cItemCode ,cName ,iNQuantity ,cDefine28 ,
  1124. cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,cDefine33 ,cDefine34 ,
  1125. cDefine35 ,cDefine36 ,cDefine37 ,iMPoIds ,bRelated ,bLPUseFree ,
  1126. iRSRowNO ,iOriTrackID ,bCosting ,cmocode ,imoseq ,iExpiratDateCalcu ,
  1127. iorderdid ,iordertype,iordercode,iorderseq,isodid,isotype,csocode,isoseq,irowno ,iposflag ,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  1128. SELECT @AutoID,@ID,@cInvCode,@iQuantity,@cBatch,0,@iNum,a.Remark,
  1129. a.Define22,a.Define23,a.Define24,a.Define25,a.Define26,a.Define27,
  1130. @cItemCode,null,c.Qty-isnull(c.QualifiedInQty,0),c.Define28,
  1131. c.Define29,c.Define30,c.Define31,c.Define32,c.Define33,c.Define34,
  1132. c.Define35,c.Define36,c.Define37,c.MoDId,0,0,
  1133. 0,0,1,b.MoCode,a.SortSeq,0,
  1134. a.AllocateId,c.OrderType,c.OrderCode,c.OrderSeq,c.SoDId,c.SoType,c.SoCode,c.SoSeq,@irowno,0,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  1135. FROM dbo.mom_moallocate a
  1136. INNER join dbo.mom_orderdetail c on a.MoDId=c.MoDId
  1137. INNER JOIN dbo.mom_order b ON c.MoId=b.MoId
  1138. WHERE a.AllocateId='" + body.MODetailID + "' and a.SortSeq='" + body.Sequence + "' ";
  1139. cmd.Parameters.Clear();
  1140. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  1141. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  1142. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  1143. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  1144. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount.ToString("0.00")));
  1145. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  1146. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  1147. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  1148. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  1149. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  1150. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  1151. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  1152. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  1153. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  1154. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  1155. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  1156. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  1157. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  1158. cmd.CommandText = sql;
  1159. try
  1160. {
  1161. int count = cmd.ExecuteNonQuery();
  1162. if (count <= 0)
  1163. {
  1164. log.Error("生成材料出库单表头失败,受影响行数<=0;");
  1165. throw new Exception("生成材料出库单表头失败,受影响行数<=0;");
  1166. }
  1167. }
  1168. catch (Exception ex)
  1169. {
  1170. log.Error("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  1171. throw new Exception("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  1172. }
  1173. #endregion
  1174. if (isif == 1)
  1175. {
  1176. if (head.CompleteVerification)
  1177. {
  1178. #region 齐套检验
  1179. sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' ";
  1180. DataTable qt = DBHelper.SQlReturnData(sql, cmd);
  1181. if (qt != null && qt.Rows.Count > 0)
  1182. {
  1183. if (qt.Rows[0]["cValue"].ToString() == "1")
  1184. {
  1185. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  1186. (
  1187. select
  1188. a.MoCode,b.SortSeq,
  1189. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  1190. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,
  1191. (case when min (c.IssQty) is null then b.Qty else
  1192. (case when min(c.IssQty) =sum(c.Qty) then b.Qty else min(IssQty/BaseQtyN) end)
  1193. end) lotqty
  1194. from DBO.mom_order a
  1195. left join DBO.mom_orderdetail b on a.moid = b.moid
  1196. left join DBO.mom_moallocate c on b.modid = c.modid
  1197. left join
  1198. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  1199. DBO.mom_moallocatesub d
  1200. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  1201. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  1202. where a.MoCode='{0}' and b.SortSeq='{1}'
  1203. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  1204. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  1205. ) tt
  1206. group by MoCode,SortSeq";
  1207. sql = string.Format(sql, mocode, seq);
  1208. qt = DBHelper.SQlReturnData(sql, cmd);
  1209. if (qt == null || qt.Rows.Count == 0)
  1210. {
  1211. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  1212. }
  1213. else
  1214. {
  1215. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  1216. {
  1217. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料<=0!");
  1218. }
  1219. }
  1220. }
  1221. else if (qt.Rows[0]["cValue"].ToString() == "2")
  1222. {
  1223. sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'";
  1224. qt = DBHelper.SQlReturnData(sql, cmd);
  1225. if (qt != null && qt.Rows.Count > 0)
  1226. {
  1227. if (qt.Rows[0]["cValue"].ToString() == "true")
  1228. {
  1229. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  1230. (
  1231. select
  1232. a.MoCode,b.SortSeq,
  1233. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  1234. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,
  1235. (case when min (c.IssQty) is null then b.Qty else
  1236. (case when min(c.IssQty) =sum(c.Qty) then b.Qty else min(IssQty/BaseQtyN) end)
  1237. end) lotqty
  1238. from DBO.mom_order a
  1239. left join DBO.mom_orderdetail b on a.moid = b.moid
  1240. left join DBO.mom_moallocate c on b.modid = c.modid
  1241. left join
  1242. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  1243. DBO.mom_moallocatesub d
  1244. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  1245. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  1246. where a.MoCode='{0}' and b.SortSeq='{1}'
  1247. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1 and f.bInvKeyPart=1
  1248. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  1249. ) tt
  1250. group by MoCode,SortSeq";
  1251. sql = string.Format(sql, mocode, seq);
  1252. qt = DBHelper.SQlReturnData(sql, cmd);
  1253. if (qt == null || qt.Rows.Count == 0)
  1254. {
  1255. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  1256. }
  1257. else
  1258. {
  1259. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  1260. {
  1261. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  1262. }
  1263. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity < 0)
  1264. {
  1265. throw new Exception("该物料:" + body.InvCode + @"入库数量大于关键物料领料套数!");
  1266. }
  1267. }
  1268. }
  1269. else
  1270. {
  1271. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  1272. (
  1273. select
  1274. a.MoCode,b.SortSeq,
  1275. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  1276. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,
  1277. (case when min (c.IssQty) is null then b.Qty else
  1278. (case when min(c.IssQty) =sum(c.Qty) then b.Qty else min(IssQty/BaseQtyN) end)
  1279. end) lotqty
  1280. from DBO.mom_order a
  1281. left join DBO.mom_orderdetail b on a.moid = b.moid
  1282. left join DBO.mom_moallocate c on b.modid = c.modid
  1283. left join
  1284. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  1285. DBO.mom_moallocatesub d
  1286. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  1287. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  1288. where a.MoCode='{0}' and b.SortSeq='{1}'
  1289. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  1290. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  1291. ) tt
  1292. group by MoCode,SortSeq";
  1293. sql = string.Format(sql, mocode, seq);
  1294. qt = DBHelper.SQlReturnData(sql, cmd);
  1295. if (qt == null || qt.Rows.Count == 0)
  1296. {
  1297. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  1298. }
  1299. else
  1300. {
  1301. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  1302. {
  1303. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  1304. }
  1305. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity < 0)
  1306. {
  1307. throw new Exception("该物料:" + body.InvCode + @"入库数量大于领料套数!");
  1308. }
  1309. }
  1310. }
  1311. }
  1312. }
  1313. }
  1314. #endregion
  1315. }
  1316. }
  1317. #region 回写生产子件已领数量
  1318. sql = @"
  1319. UPDATE a SET a.IssQty=ISNULL(a.IssQty,0)+" + body.Quantity + @" from dbo.mom_moallocate a
  1320. inner join dbo.mom_orderdetail b on a.MoDId=b.MoDId
  1321. where a.AllocateId='" + body.MODetailID + "' and a.SortSeq='" + body.Sequence + @"'
  1322. ";
  1323. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写生产订单入库数量失败!");
  1324. #endregion
  1325. sql = @"select isnull(IssQty,0)fOutQuantity,isnull(Qty,0)iQuantity,ISNULL(AuxQty, 0)iNum,ISNULL(NiNum, 0)NiNum,ISNULL(AuxQty, 0)-ISNULL(NiNum, 0) SiNum from mom_moallocate a
  1326. LEFT JOIN (select sum(iNum) NiNum,iorderdid from rdrecords10 GROUP BY iorderdid) b on a.AllocateId=b.iorderdid
  1327. where a.AllocateId='" + body.MODetailID + "'";
  1328. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  1329. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  1330. {
  1331. foreach (DataRow item in dtDisCheck.Rows)
  1332. {
  1333. if (Convert.ToDecimal(item["fOutQuantity"].ToString()) == Convert.ToDecimal(item["iQuantity"].ToString()))
  1334. {
  1335. if (Convert.ToDecimal(item["iNum"].ToString()) != 0)
  1336. {
  1337. if (Convert.ToDecimal(item["SiNum"].ToString()) != 0)
  1338. {
  1339. sql = @"Update rdrecords10 set iNum=isnull(iNum,0)+(" + item["SiNum"].ToString() + ") where autoid='" + iChildId + "' ";
  1340. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item["SiNum"].ToString()));
  1341. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  1342. }
  1343. }
  1344. }
  1345. }
  1346. }
  1347. #region 更新现存量
  1348. key.cBustypeUN = "成品入库";
  1349. key.cVouchTypeUN = "10";
  1350. key.TableName = "IA_ST_UnAccountVouch10";
  1351. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  1352. //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);
  1353. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, body.Quantity, body.Amount, body.cFree1,
  1354. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  1355. "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  1356. #endregion
  1357. #region 判断工单行是否全部入库
  1358. sql = "select Qty from mom_moallocate WHERE MoDId=(select MoDId from dbo.mom_moallocate where AllocateId='" + body.MODetailID + @"') and IssQty-qty<0
  1359. union all
  1360. select Qty from mom_orderdetail where MoDId=(select MoDId from dbo.mom_moallocate where AllocateId='" + body.MODetailID + @"') and QualifiedInQty-Qty<0
  1361. ";
  1362. DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  1363. if (dtClose.Rows.Count == 0)
  1364. {
  1365. sql = @"
  1366. update mom_orderdetail set CloseTime = GETDATE(),CloseDate= CONVERT(NVARCHAR(15),GETDATE(),23), CloseUser ='" + head.User + "',Status=4 where MODId=(select MoDId from dbo.mom_moallocate where AllocateId='" + body.MODetailID + @"')";
  1367. DBHelper.CmdExecuteNonQuery(sql, cmd, "生产订单行关闭失败!");
  1368. }
  1369. //foreach (DataRow dr in dtClose.Rows)
  1370. //{
  1371. // if (Convert.ToDecimal(dr["qty"]) >= 0)
  1372. // {
  1373. // sql = "update mom_orderdetail set CloseTime = GETDATE(),CloseDate= CONVERT(NVARCHAR(15),GETDATE(),23), CloseUser ='" + head.User + "',Status=4 where MODId='" + body.MODetailID + "'";
  1374. // DBHelper.CmdExecuteNonQuery(sql, cmd, "生产订单行关闭失败!");
  1375. // }
  1376. //}
  1377. #endregion
  1378. iFatherIdTwo = "'" + iFatherId + "',";
  1379. iChildId--;
  1380. }
  1381. #endregion
  1382. #region 查询
  1383. sql = @" select DISTINCT a.ID as ID,a.ID as IDs,a.cCode as MRCVCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName
  1384. ,a.cMPoCode as MOCode ,a.cMaker as CreateUser ,a.dnmaketime as CreateDateTime
  1385. ,a.cHandler as Checker ,a.dnverifytime as CheckDateTime
  1386. from rdrecord10 a
  1387. left join Department c on a.cDepCode=c.cDepCode
  1388. left join Warehouse d on a.cWhCode=d.cWhCode WHERE A.cHandler IS NOT NULL AND A.ID in({0})
  1389. select DISTINCT a.ID as IDs, b.iorderdid as DetailID,b.irowno as Sequence ,b.cInvCode as InvCode ,
  1390. b.iQuantity as Quantity ,b.iNum as Amount,b.iMPoIds as MODetailID ,
  1391. isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  1392. isnull(b.cFree1,'') as cFree1,
  1393. isnull(b.cFree2,'') as cFree2,
  1394. isnull(b.cFree3,'') as cFree3,
  1395. isnull(b.cFree4,'') as cFree4,
  1396. isnull(b.cFree5,'') as cFree5,
  1397. isnull(b.cFree6,'') as cFree6,
  1398. isnull(b.cFree7,'') as cFree7,
  1399. isnull(b.cFree8,'') as cFree8,
  1400. isnull(b.cFree9,'') as cFree9,
  1401. isnull(b.cFree10,'') as cFree10
  1402. from rdrecord10 a
  1403. left join rdrecords10 b on a.ID=b.ID
  1404. left join Department c on a.cDepCode=c.cDepCode
  1405. left join Warehouse d on a.cWhCode=d.cWhCode WHERE A.cHandler IS NOT NULL AND A.ID in({0}) ";
  1406. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  1407. if (ds != null)
  1408. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  1409. else
  1410. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  1411. #endregion
  1412. cmd.CommandType = CommandType.Text;
  1413. cmd.Parameters.Clear();
  1414. }
  1415. cmd.Transaction.Commit();
  1416. }
  1417. catch (Exception ex)
  1418. {
  1419. if (cmd.Transaction != null)
  1420. cmd.Transaction.Rollback();
  1421. log.Error(ex.Message);
  1422. throw new Exception(ex.Message);
  1423. }
  1424. finally
  1425. {
  1426. if (conn.State == ConnectionState.Open)
  1427. {
  1428. conn.Close();
  1429. }
  1430. conn.Dispose();
  1431. }
  1432. }
  1433. //string RelationName = "details";
  1434. //DataSet dz = new DataSet();
  1435. //dtNew1.TableName = "Table0";
  1436. //dtNew2.TableName = "Table1";
  1437. //dz.Tables.Add(dtNew1.Copy());
  1438. //dz.Tables.Add(dtNew2.Copy());
  1439. //DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["IDs"], dz.Tables[1].Columns["IDs"]);
  1440. //dz.Relations.Add(dh);
  1441. msg = JSON.DataSetToJson(ds, "details", "IDs");
  1442. return msg;
  1443. }
  1444. /// <summary>
  1445. /// 审核成品入库单
  1446. /// </summary>
  1447. /// <param name="infos"></param>
  1448. /// <returns></returns>
  1449. public bool Approve(List<ICSManufactureReceiveDoc> infos)
  1450. {
  1451. bool ResultFlag = false;
  1452. List<ICSManufactureReceiveDoc> szJson = new List<ICSManufactureReceiveDoc>();
  1453. string connS = "";
  1454. if (infos.Count <= 0)
  1455. {
  1456. throw new Exception("传送数据为空!");
  1457. }
  1458. LogInfo(infos);
  1459. string res = string.Empty;
  1460. SqlConnection conn = new SqlConnection();
  1461. SqlCommand cmd = new SqlCommand();
  1462. string sql = string.Empty;
  1463. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  1464. foreach (string WorkPoint in result)
  1465. {
  1466. try
  1467. {
  1468. connS = string.Format(connString, WorkPoint);
  1469. conn = new System.Data.SqlClient.SqlConnection(connS);
  1470. conn.Open();
  1471. SqlTransaction sqlTran = conn.BeginTransaction();
  1472. cmd = new SqlCommand();
  1473. cmd.Transaction = sqlTran;
  1474. cmd.Connection = conn;
  1475. foreach (ICSManufactureReceiveDoc head in infos)
  1476. {
  1477. if (WorkPoint != head.WorkPoint)
  1478. continue;
  1479. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  1480. throw new Exception("U8正在整理现存量,请稍后再试");
  1481. ICSUserInfo userInfo = new ICSUserInfo();
  1482. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  1483. #region 检验单号是否存在
  1484. sql = "select * from rdrecord10 where ID='" + head.ID + "'";
  1485. DataTable dt = DBHelper.SQlReturnData(sql, cmd);
  1486. if (dt != null && dt.Rows.Count > 0)
  1487. {
  1488. string cHandler = dt.Rows[0]["cHandler"].ToString();
  1489. if (!string.IsNullOrEmpty(cHandler))
  1490. {
  1491. throw new Exception("单据ID:" + head.ID + "不是开立状态!");
  1492. }
  1493. }
  1494. else
  1495. {
  1496. throw new Exception("单据ID:" + head.ID + "在U8中不存在!");
  1497. }
  1498. #endregion
  1499. #region 审核其他出库单
  1500. sql = @"UPDATE dbo.rdrecord10 SET cHandler='" + userInfo.UserName + @"' ,
  1501. dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE() WHERE ID='" + head.ID + "'";
  1502. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核其他出库单失败!");
  1503. #endregion
  1504. sql = "select * from dbo.rdrecords10 a inner join rdrecord10 b on a.ID=b.ID where b.ID='" + head.ID + "';";
  1505. DataTable dtChecks = DBHelper.SQlReturnData(sql, cmd);
  1506. #region 更新现存量
  1507. for (int i = 0; i < dtChecks.Rows.Count; i++)
  1508. {
  1509. //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
  1510. 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() + "'";
  1511. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  1512. if (dtItem != null && dtItem.Rows.Count > 0)
  1513. {
  1514. //if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cBatch))
  1515. //{
  1516. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.cInvCode);
  1517. //}
  1518. if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString()))
  1519. {
  1520. throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + dtChecks.Rows[i]["cInvCode"].ToString());
  1521. }
  1522. }
  1523. else
  1524. {
  1525. throw new Exception("物料:" + dtChecks.Rows[i]["cInvCode"].ToString() + "在现存量表中不存在!");
  1526. }
  1527. VouchKey key = new VouchKey();
  1528. key.cBustypeUN = "成品入库";
  1529. key.cVouchTypeUN = "10";
  1530. key.TableName = "IA_ST_UnAccountVouch10";
  1531. //DBHelper.UpdateCurrentStock(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), dtChecks.Rows[i]["cWhCode"].ToString(), "", Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString()), key);
  1532. //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);
  1533. string inum = dtChecks.Rows[i]["iAVNum"].ToString();
  1534. if (inum == "")
  1535. {
  1536. inum = "0";
  1537. }
  1538. DBHelper.UpdateCurrentStockCCGC(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), dtChecks.Rows[i]["cWhCode"].ToString(),
  1539. dtChecks.Rows[i]["cBatch"].ToString(), Convert.ToDecimal(dtChecks.Rows[i]["iAVQuantity"].ToString()),
  1540. Convert.ToDecimal(inum), dtChecks.Rows[i]["cFree1"].ToString(),
  1541. 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(),
  1542. "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, Convert.ToInt32(dtChecks.Rows[i]["ID"].ToString()), Convert.ToInt32(dtChecks.Rows[i]["AutoID"].ToString()));
  1543. //回写fOutQuantityy
  1544. // sql = @"Update CurrentStock set fOutQuantity=isnull(fOutQuantity,0)-" + Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString()) + @"
  1545. // where cInvCode='" + dtChecks.Rows[i]["cInvCode"].ToString() + "' and cWhCode='" + dtChecks.Rows[i]["cWhCode"].ToString() + "' ";
  1546. //if (body.cBatch != null)
  1547. //{
  1548. // sql += "and cBatch='" + body.cBatch + "'";
  1549. //}
  1550. //else
  1551. //{
  1552. // sql += "and cBatch=''";
  1553. //}
  1554. //DBHelper.CmdExecuteNonQuery(sql, cmd, "回写fOutQuantity失败!");
  1555. //if (head.UpdateTodoQuantity == true)
  1556. //{
  1557. }
  1558. #endregion
  1559. cmd.CommandType = CommandType.Text;
  1560. cmd.Parameters.Clear();
  1561. }
  1562. cmd.Transaction.Commit();
  1563. }
  1564. catch (Exception ex)
  1565. {
  1566. if (cmd.Transaction != null)
  1567. cmd.Transaction.Rollback();
  1568. log.Error(ex.Message);
  1569. throw new Exception(ex.Message);
  1570. }
  1571. finally
  1572. {
  1573. if (conn.State == ConnectionState.Open)
  1574. {
  1575. conn.Close();
  1576. }
  1577. conn.Dispose();
  1578. }
  1579. ResultFlag = true;
  1580. }
  1581. return ResultFlag;
  1582. }
  1583. /// <summary>
  1584. /// 删除成品入库单
  1585. /// </summary>
  1586. /// <param name="infos"></param>
  1587. /// <returns></returns>
  1588. public string Delete(List<ICSManufactureReceiveDoc> infos)
  1589. {
  1590. List<ICSManufactureReceiveDoc> szJson = new List<ICSManufactureReceiveDoc>();
  1591. if (infos.Count <= 0)
  1592. {
  1593. throw new Exception("传送数据为空!");
  1594. }
  1595. string connS = "";
  1596. string res = string.Empty;
  1597. SqlConnection conn = new SqlConnection();
  1598. SqlCommand cmd = new SqlCommand();
  1599. string sql = string.Empty;
  1600. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  1601. foreach (string WorkPoint in result)
  1602. {
  1603. try
  1604. {
  1605. connS = string.Format(connString, WorkPoint);
  1606. conn = new System.Data.SqlClient.SqlConnection(connS);
  1607. conn.Open();
  1608. SqlTransaction sqlTran = conn.BeginTransaction();
  1609. cmd = new SqlCommand();
  1610. cmd.Transaction = sqlTran;
  1611. cmd.Connection = conn;
  1612. foreach (ICSManufactureReceiveDoc info in infos)
  1613. {
  1614. if (WorkPoint != info.WorkPoint)
  1615. continue;
  1616. if (info.MTime < new DateTime(2000, 01, 01))
  1617. throw new Exception("请输入正确的操作时间:" + info.MTime);
  1618. sql = @" DELETE rdrecord10 WHERE ID={0}";
  1619. sql = string.Format(sql, info.ID);
  1620. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  1621. }
  1622. cmd.Transaction.Commit();
  1623. }
  1624. catch (Exception ex)
  1625. {
  1626. if (cmd.Transaction != null)
  1627. cmd.Transaction.Rollback();
  1628. log.Error(ex.Message);
  1629. throw new Exception(ex.Message);
  1630. }
  1631. finally
  1632. {
  1633. if (conn.State == ConnectionState.Open)
  1634. {
  1635. conn.Close();
  1636. }
  1637. conn.Dispose();
  1638. }
  1639. }
  1640. return res;
  1641. }
  1642. /// <summary>
  1643. ///
  1644. /// 记录日志
  1645. /// </summary>
  1646. /// <param name="Bills"></param>
  1647. private void LogInfo(List<ICSManufactureReceiveDoc> Bills)
  1648. {
  1649. string HeadList = string.Empty;
  1650. string BodyList = string.Empty;
  1651. foreach (ICSManufactureReceiveDoc head in Bills)
  1652. {
  1653. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  1654. foreach (ICSManufactureReceiveDocs body in head.details)
  1655. {
  1656. BodyList += "\r\n 表体主键ID: " + body.MODetailID + ",数量:" + body.Quantity;
  1657. }
  1658. }
  1659. log.Info(HeadList);
  1660. log.Info(BodyList);
  1661. }
  1662. }
  1663. }