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

1827 lines
106 KiB

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