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

764 lines
41 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  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. /// <summary>
  24. /// 获取成品入库单
  25. /// </summary>
  26. /// <param name="infos"></param>
  27. /// <returns></returns>
  28. public string Get(List<ICSManufactureReceiveDoc> infos)
  29. {
  30. List<ICSManufactureReceiveDoc> szJson = new List<ICSManufactureReceiveDoc>();
  31. DataTable dt = null;
  32. DataTable dtNew = null;
  33. string connS = "";
  34. string json = "";
  35. if (infos.Count <= 0)
  36. {
  37. throw new Exception("传送数据为空!");
  38. }
  39. string res = string.Empty;
  40. SqlConnection conn = new SqlConnection();
  41. SqlCommand cmd = new SqlCommand();
  42. string sql = string.Empty;
  43. foreach (ICSManufactureReceiveDoc info in infos)
  44. {
  45. try
  46. {
  47. connS = string.Format(connString, info.WorkPoint);
  48. conn = new System.Data.SqlClient.SqlConnection(connS);
  49. conn.Open();
  50. SqlTransaction sqlTran = conn.BeginTransaction();
  51. cmd = new SqlCommand();
  52. cmd.Transaction = sqlTran;
  53. cmd.Connection = conn;
  54. if (info.MTime < new DateTime(2000, 01, 01))
  55. throw new Exception("请输入正确的操作时间:" + info.MTime);
  56. sql = @" select a.ID,a.cCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,a.cMPoCode ,a.cMaker ,a.dnmaketime ,a.cHandler ,a.dnverifytime ,
  57. b.AutoID,b.iRSRowNO ,b.cInvCode ,b.iQuantity ,b.iNum ,b.iMPoIds from rdrecord10 a
  58. left join rdrecords10 b on a.ID=b.ID
  59. left join Department c on a.cDepCode=c.cDepCode
  60. left join Warehouse d on a.cWhCode=d.cWhCode WHERE 1=1";
  61. if (!string.IsNullOrWhiteSpace(info.MRCVCode))
  62. {
  63. sql += " and a.cCode='{0}'";
  64. }
  65. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  66. {
  67. sql += " and ISNULL(a.dnmodifytime,ISNULL(a.dnverifytime, ISNULL(a.dnmodifytime, a.dnmaketime)))>='{1}'";
  68. }
  69. if (!string.IsNullOrWhiteSpace(info.User))
  70. {
  71. sql += "and a.CMAKER='{2}'";
  72. }
  73. sql = string.Format(sql, info.MRCVCode, info.MTime, info.User);
  74. dt = DBHelper.SQlReturnData(sql, cmd);
  75. if (dt.Rows.Count <= 0 || dt == null)
  76. throw new Exception("成品入库单:" + info.MRCVCode + ",无信息!");
  77. if (dtNew == null)
  78. dtNew = dt;
  79. else
  80. dtNew.Merge(dt);
  81. cmd.Transaction.Commit();
  82. }
  83. catch (Exception ex)
  84. {
  85. cmd.Transaction.Rollback();
  86. log.Error(ex.Message);
  87. throw new Exception(ex.Message);
  88. }
  89. finally
  90. {
  91. if (conn.State == ConnectionState.Open)
  92. {
  93. conn.Close();
  94. }
  95. conn.Dispose();
  96. }
  97. }
  98. json = JsonConvert.SerializeObject(dtNew);
  99. return json;
  100. }
  101. /// <summary>
  102. /// 创建成品入库单
  103. /// </summary>
  104. /// <param name="Bills"></param>
  105. /// <returns></returns>
  106. public string CreateManufactureReceiveDoc(List<ICSManufactureReceiveDoc> Bills)
  107. {
  108. string msg = "";
  109. DataTable dtNew1 = null;
  110. DataTable dtNew2 = null;
  111. string connS = "";
  112. int num = 0;
  113. string sql = string.Empty;
  114. string mocode = "";
  115. string seq = "";
  116. //取得out库单的默认显示模版
  117. //取得采购入库单单据 表头ID,表体DID
  118. VouchKey key = new VouchKey();
  119. SqlConnection conn = new SqlConnection();
  120. SqlCommand cmd = new SqlCommand();
  121. if (Bills.Count <= 0)
  122. {
  123. throw new Exception("传送数据为空!");
  124. }
  125. LogInfo(Bills);
  126. //MergeObject(Bills, cmd);
  127. //string ERPName = U8Helper.GetERPName();
  128. foreach (ICSManufactureReceiveDoc head in Bills)
  129. {
  130. try
  131. {
  132. string iFatherIdTwo = "";
  133. connS = string.Format(connString, head.WorkPoint);
  134. conn = new System.Data.SqlClient.SqlConnection(connS);
  135. conn.Open();
  136. SqlTransaction sqlTran = conn.BeginTransaction();
  137. cmd = new SqlCommand();
  138. cmd.Transaction = sqlTran;
  139. cmd.Connection = conn;
  140. if (DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  141. {
  142. }
  143. else
  144. {
  145. throw new Exception("U8正在整理现存量,请稍后再试");
  146. }
  147. string[] ss = head.WorkPoint.Split('_');
  148. ERPDB = ss[1];
  149. string[] dd = CRNALL.Split('~');
  150. string crdname = dd[0];
  151. string carname = dd[1];
  152. string surface = dd[2];
  153. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  154. num = head.details.Count();
  155. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  156. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  157. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  158. DateTime date = DateTime.Now;
  159. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  160. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  161. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", head.WorkPoint);
  162. //Person personEntity = new Person();
  163. //personEntity = U8Helper.GetDepCodeByUserCode(head.UserCode, cmd);
  164. DateTime dvDate = DateTime.Now.Date;
  165. #region 产成品入库单表头
  166. sql = @"INSERT INTO dbo.rdrecord10
  167. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,
  168. cCode ,cRdCode ,cDepCode ,cHandler ,bTransFlag ,cMaker ,
  169. cDefine1 ,cDefine2 ,cDefine3 ,cDefine4 ,cDefine5 ,cDefine6 ,
  170. cDefine7 ,cDefine8 ,cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,
  171. biafirst ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,cDefine13 ,
  172. cDefine14 ,cDefine15 ,cDefine16 ,cMPoCode ,iproorderid ,
  173. bFromPreYear ,bIsComplement ,iDiscountTaxType ,ireturncount ,
  174. iverifystate ,iswfcontrolled ,cModifyPerson ,dnmaketime ,
  175. dnverifytime ,bredvouch ,iPrintCount )
  176. SELECT @ID,1,'10','','',@cWhCode,convert(datetime,convert(varchar(10),getdate(),120)),
  177. @cCode,@RdCode,@DepCode,@cHandler,0,@cMaker,
  178. a.Define1,a.Define2,a.Define3,a.Define4,a.Define5,a.Define6,
  179. a.Define7,a.Define8,a.Define9,a.Define10,@dDate,0,
  180. 0,@VT_ID,0,a.Define11,a.Define12,a.Define13,
  181. a.Define14,a.Define15,a.Define16,a.MoCode,a.MoId,
  182. 0,0,0,0,
  183. 0,0,'',@dDate,
  184. @dDate,0,0
  185. FROM dbo.mom_order a
  186. WHERE a.MoCode='" + head.MOCode + "'";
  187. cmd.Parameters.Clear();
  188. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  189. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  190. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  191. cmd.Parameters.Add(new SqlParameter("@cDepCode", head.DepCode));
  192. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  193. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  194. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  195. cmd.Parameters.Add(new SqlParameter("@DepCode", head.DepCode));
  196. cmd.Parameters.Add(new SqlParameter("@dDate", dvDate));
  197. cmd.Parameters.Add(new SqlParameter("@RdCode", cRDcode));
  198. cmd.CommandText = sql;
  199. try
  200. {
  201. int count = cmd.ExecuteNonQuery();
  202. if (count <= 0)
  203. {
  204. log.Error("生成成品入库单表头失败,受影响行数<=0;");
  205. throw new Exception("生成成品入库单表头失败,受影响行数<=0;");
  206. }
  207. }
  208. catch (Exception ex)
  209. {
  210. log.Error("生成成品入库单表头失败!SQL:\r\n" + sql, ex);
  211. throw new Exception("生成成品入库单表头失败!SQL:\r\n" + sql, ex);
  212. }
  213. #endregion
  214. #region 产成品入库单表体
  215. foreach (ICSManufactureReceiveDocs body in head.details)
  216. {
  217. #region 判断工单行是否关闭,关闭不能入库
  218. sql = @"SELECT ISNULL(a.CloseUser,'') as CloseUser,b.MoCode,a.SortSeq FROM dbo.mom_orderdetail a INNER JOIN
  219. dbo.mom_order b ON b.MoId = a.MoId WHERE a.MoDId = '" + body.MODetailID + "'";
  220. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  221. if (dtCheck != null && dtCheck.Rows.Count > 0)
  222. {
  223. mocode = dtCheck.Rows[0]["MoCode"].ToString();
  224. seq = dtCheck.Rows[0]["SortSeq"].ToString();
  225. if (!string.IsNullOrEmpty(dtCheck.Rows[0]["CloseUser"].ToString()))
  226. {
  227. throw new Exception("工单号:" + dtCheck.Rows[0]["MoCode"].ToString() + ",行号:" + dtCheck.Rows[0]["SortSeq"].ToString() + ",已关闭,无法入库!");
  228. }
  229. }
  230. #endregion
  231. #region 生成产成品入库单表体
  232. //if (head.IsReturn == "1")
  233. //{
  234. // body.iQuantity = -body.iQuantity;
  235. //}
  236. sql = @"INSERT INTO dbo.rdrecords10
  237. ( AutoID ,ID ,cInvCode ,iQuantity ,cBatch ,iFlag ,
  238. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  239. cItemCode ,cName ,iNQuantity ,cDefine28 ,
  240. cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,cDefine33 ,cDefine34 ,
  241. cDefine35 ,cDefine36 ,cDefine37 ,iMPoIds ,bRelated ,bLPUseFree ,
  242. iRSRowNO ,iOriTrackID ,bCosting ,cmocode ,imoseq ,iExpiratDateCalcu ,
  243. iorderdid ,iordertype,iordercode,iorderseq,isodid,isotype,csocode,isoseq,irowno ,iposflag ,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  244. SELECT @AutoID,@ID,@cInvCode,@iQuantity,@cBatch,0,
  245. a.Define22,a.Define23,a.Define24,a.Define25,a.Define26,a.Define27,
  246. null,null,a.Qty,a.Define28,
  247. a.Define29,a.Define30,a.Define31,a.Define32,a.Define33,a.Define34,
  248. a.Define35,a.Define36,a.Define37,a.MoDId,0,0,
  249. 0,0,1,b.MoCode,a.SortSeq,0,
  250. 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
  251. FROM dbo.mom_orderdetail a
  252. INNER JOIN dbo.mom_order b ON a.MoId=b.MoId WHERE a.MoDId='" + body.MODetailID + "'";
  253. cmd.Parameters.Clear();
  254. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  255. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  256. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  257. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  258. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  259. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  260. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  261. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  262. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  263. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  264. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  265. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  266. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  267. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  268. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  269. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  270. cmd.CommandText = sql;
  271. try
  272. {
  273. int count = cmd.ExecuteNonQuery();
  274. if (count <= 0)
  275. {
  276. log.Error("生成材料出库单表头失败,受影响行数<=0;");
  277. throw new Exception("生成材料出库单表头失败,受影响行数<=0;");
  278. }
  279. }
  280. catch (Exception ex)
  281. {
  282. log.Error("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  283. throw new Exception("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  284. }
  285. #endregion
  286. #region 齐套检验
  287. sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' ";
  288. DataTable qt = DBHelper.SQlReturnData(sql, cmd);
  289. if (qt != null && qt.Rows.Count > 0)
  290. {
  291. if (qt.Rows[0]["cValue"].ToString() == "1")
  292. {
  293. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  294. (
  295. select
  296. a.MoCode,b.SortSeq,
  297. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  298. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  299. from DBO.mom_order a
  300. left join DBO.mom_orderdetail b on a.moid = b.moid
  301. left join DBO.mom_moallocate c on b.modid = c.modid
  302. left join
  303. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  304. DBO.mom_moallocatesub d
  305. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  306. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  307. where a.MoCode='{0}' and b.SortSeq='{1}'
  308. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  309. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  310. ) tt
  311. group by MoCode,SortSeq";
  312. sql = string.Format(sql, mocode, seq);
  313. qt = DBHelper.SQlReturnData(sql, cmd);
  314. if (qt == null || qt.Rows.Count == 0)
  315. {
  316. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  317. }
  318. else
  319. {
  320. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  321. {
  322. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料<=0!");
  323. }
  324. }
  325. }
  326. else if (qt.Rows[0]["cValue"].ToString() == "2")
  327. {
  328. sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'";
  329. qt = DBHelper.SQlReturnData(sql, cmd);
  330. if (qt != null && qt.Rows.Count > 0)
  331. {
  332. if (qt.Rows[0]["cValue"].ToString() == "true")
  333. {
  334. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  335. (
  336. select
  337. a.MoCode,b.SortSeq,
  338. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  339. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  340. from DBO.mom_order a
  341. left join DBO.mom_orderdetail b on a.moid = b.moid
  342. left join DBO.mom_moallocate c on b.modid = c.modid
  343. left join
  344. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  345. DBO.mom_moallocatesub d
  346. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  347. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  348. where a.MoCode='{0}' and b.SortSeq='{1}'
  349. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1 and f.bInvKeyPart=1
  350. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  351. ) tt
  352. group by MoCode,SortSeq";
  353. sql = string.Format(sql, mocode, seq);
  354. qt = DBHelper.SQlReturnData(sql, cmd);
  355. if (qt == null || qt.Rows.Count == 0)
  356. {
  357. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  358. }
  359. else
  360. {
  361. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  362. {
  363. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  364. }
  365. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity <= 0)
  366. {
  367. throw new Exception("该物料:" + body.InvCode + @"入库数量大于关键物料领料套数!");
  368. }
  369. }
  370. }
  371. else
  372. {
  373. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  374. (
  375. select
  376. a.MoCode,b.SortSeq,
  377. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  378. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  379. from DBO.mom_order a
  380. left join DBO.mom_orderdetail b on a.moid = b.moid
  381. left join DBO.mom_moallocate c on b.modid = c.modid
  382. left join
  383. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  384. DBO.mom_moallocatesub d
  385. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  386. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  387. where a.MoCode='{0}' and b.SortSeq='{1}'
  388. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  389. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  390. ) tt
  391. group by MoCode,SortSeq";
  392. sql = string.Format(sql, mocode, seq);
  393. qt = DBHelper.SQlReturnData(sql, cmd);
  394. if (qt == null || qt.Rows.Count == 0)
  395. {
  396. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  397. }
  398. else
  399. {
  400. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  401. {
  402. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  403. }
  404. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity <= 0)
  405. {
  406. throw new Exception("该物料:" + body.InvCode + @"入库数量大于领料套数!");
  407. }
  408. }
  409. }
  410. }
  411. }
  412. }
  413. #endregion
  414. if (head.UpdateStock)
  415. {
  416. #region 更新现存量
  417. key.cBustypeUN = "成品入库";
  418. key.cVouchTypeUN = "10";
  419. key.TableName = "IA_ST_UnAccountVouch10";
  420. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  421. 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);
  422. #endregion
  423. }
  424. #region 回写生产订单入库数量
  425. sql = @"UPDATE dbo.mom_orderdetail SET QualifiedInQty=ISNULL(QualifiedInQty,0)+" + body.Quantity + " where MoDId='" + body.MODetailID + "'";
  426. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写生产订单入库数量失败!");
  427. #endregion
  428. #region 判断工单行是否全部入库
  429. sql = "SELECT (QualifiedInQty - Qty) as qty,QualifiedInQty FROM dbo.mom_orderdetail WHERE MoDId='" + body.MODetailID + "'";
  430. DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  431. foreach (DataRow dr in dtClose.Rows)
  432. {
  433. if (Convert.ToDecimal(dr["qty"]) >= 0)
  434. {
  435. sql = "update mom_orderdetail set CloseTime = GETDATE(),CloseDate= CONVERT(NVARCHAR(15),GETDATE(),23), CloseUser ='" + head.User + "',Status=4 where MODId='" + body.MODetailID + "'";
  436. DBHelper.CmdExecuteNonQuery(sql, cmd, "生产订单行关闭失败!");
  437. }
  438. }
  439. #endregion
  440. iFatherIdTwo += "'" + iFatherId + "',";
  441. iChildId--;
  442. }
  443. #endregion
  444. #region 查询
  445. sql = @" select a.ID as ID,a.ID as IDs,a.cCode as MRCVCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName
  446. ,a.cMPoCode as MOCode ,a.cMaker as CreateUser ,a.dnmaketime as CreateDateTime
  447. ,a.cHandler as Checker ,a.dnverifytime as CheckDateTime
  448. from rdrecord10 a
  449. left join Department c on a.cDepCode=c.cDepCode
  450. left join Warehouse d on a.cWhCode=d.cWhCode WHERE A.cHandler IS NOT NULL AND A.ID in({0})
  451. select a.ID as IDs, b.AutoID as DetailID,b.irowno as Sequence ,b.cInvCode as InvCode ,
  452. b.iQuantity as Quantity ,b.iNum as Amount,b.iMPoIds as MODetailID
  453. from rdrecord10 a
  454. left join rdrecords10 b on a.ID=b.ID
  455. left join Department c on a.cDepCode=c.cDepCode
  456. left join Warehouse d on a.cWhCode=d.cWhCode WHERE A.cHandler IS NOT NULL AND A.ID in({0}) ";
  457. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  458. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  459. if (dtNew1 == null)
  460. dtNew1 = ds.Tables[0];
  461. else
  462. dtNew1.Merge(ds.Tables[0]);
  463. if (dtNew2 == null)
  464. dtNew2 = ds.Tables[1];
  465. else
  466. dtNew2.Merge(ds.Tables[1]);
  467. #endregion
  468. cmd.Transaction.Commit();
  469. }
  470. catch (Exception ex)
  471. {
  472. cmd.Transaction.Rollback();
  473. log.Error(ex.Message);
  474. throw new Exception(ex.Message);
  475. }
  476. finally
  477. {
  478. if (conn.State == ConnectionState.Open)
  479. {
  480. conn.Close();
  481. }
  482. conn.Dispose();
  483. }
  484. }
  485. string RelationName = "details";
  486. DataSet dz = new DataSet();
  487. dtNew1.TableName = "Table0";
  488. dtNew2.TableName = "Table1";
  489. dz.Tables.Add(dtNew1.Copy());
  490. dz.Tables.Add(dtNew2.Copy());
  491. DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["IDs"], dz.Tables[1].Columns["IDs"]);
  492. dz.Relations.Add(dh);
  493. msg = DBHelper.DataSetToJson(dz, RelationName);
  494. return msg;
  495. }
  496. /// <summary>
  497. /// 审核成品入库单
  498. /// </summary>
  499. /// <param name="infos"></param>
  500. /// <returns></returns>
  501. public bool Approve(List<ICSManufactureReceiveDoc> infos)
  502. {
  503. bool ResultFlag = false;
  504. List<ICSManufactureReceiveDoc> szJson = new List<ICSManufactureReceiveDoc>();
  505. string connS = "";
  506. if (infos.Count <= 0)
  507. {
  508. throw new Exception("传送数据为空!");
  509. }
  510. LogInfo(infos);
  511. string res = string.Empty;
  512. SqlConnection conn = new SqlConnection();
  513. SqlCommand cmd = new SqlCommand();
  514. string sql = string.Empty;
  515. foreach (ICSManufactureReceiveDoc head in infos)
  516. {
  517. try
  518. {
  519. connS = string.Format(connString, head.WorkPoint);
  520. conn = new System.Data.SqlClient.SqlConnection(connS);
  521. conn.Open();
  522. SqlTransaction sqlTran = conn.BeginTransaction();
  523. cmd = new SqlCommand();
  524. cmd.Transaction = sqlTran;
  525. cmd.Connection = conn;
  526. if (DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  527. {
  528. }
  529. else
  530. {
  531. throw new Exception("U8正在整理现存量,请稍后再试");
  532. }
  533. ICSUserInfo userInfo = new ICSUserInfo();
  534. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  535. #region 检验单号是否存在
  536. sql = "select * from rdrecord10 where ID='" + head.ID + "'";
  537. DataTable dt = DBHelper.SQlReturnData(sql, cmd);
  538. if (dt != null && dt.Rows.Count > 0)
  539. {
  540. string cHandler = dt.Rows[0]["cHandler"].ToString();
  541. if (!string.IsNullOrEmpty(cHandler))
  542. {
  543. throw new Exception("单据ID:" + head.ID + "不是开立状态!");
  544. }
  545. }
  546. else
  547. {
  548. throw new Exception("单据ID:" + head.ID + "在U8中不存在!");
  549. }
  550. #endregion
  551. #region 审核其他出库单
  552. sql = @"UPDATE dbo.rdrecord10 SET cHandler='" + userInfo.UserName + @"' ,
  553. dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE() WHERE ID='" + head.ID + "'";
  554. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核其他出库单失败!");
  555. #endregion
  556. sql = "select * from dbo.rdrecords10 a inner join rdrecord10 b on a.ID=b.ID where b.ID='" + head.ID + "';";
  557. DataTable dtChecks = DBHelper.SQlReturnData(sql, cmd);
  558. if (head.UpdateStock)
  559. {
  560. #region 更新现存量
  561. for (int i = 0; i < dtChecks.Rows.Count; i++)
  562. {
  563. //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
  564. 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() + "'";
  565. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  566. if (dtItem != null && dtItem.Rows.Count > 0)
  567. {
  568. //if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cBatch))
  569. //{
  570. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.cInvCode);
  571. //}
  572. if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString()))
  573. {
  574. throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + dtChecks.Rows[i]["cInvCode"].ToString());
  575. }
  576. }
  577. else
  578. {
  579. throw new Exception("物料:" + dtChecks.Rows[i]["cInvCode"].ToString() + "在现存量表中不存在!");
  580. }
  581. VouchKey key = new VouchKey();
  582. key.cBustypeUN = "成品入库";
  583. key.cVouchTypeUN = "10";
  584. key.TableName = "IA_ST_UnAccountVouch10";
  585. //DBHelper.UpdateCurrentStock(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), dtChecks.Rows[i]["cWhCode"].ToString(), "", Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString()), key);
  586. 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);
  587. //回写fOutQuantityy
  588. // sql = @"Update CurrentStock set fOutQuantity=isnull(fOutQuantity,0)-" + Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString()) + @"
  589. // where cInvCode='" + dtChecks.Rows[i]["cInvCode"].ToString() + "' and cWhCode='" + dtChecks.Rows[i]["cWhCode"].ToString() + "' ";
  590. //if (body.cBatch != null)
  591. //{
  592. // sql += "and cBatch='" + body.cBatch + "'";
  593. //}
  594. //else
  595. //{
  596. // sql += "and cBatch=''";
  597. //}
  598. //DBHelper.CmdExecuteNonQuery(sql, cmd, "回写fOutQuantity失败!");
  599. //if (head.UpdateTodoQuantity == true)
  600. //{
  601. }
  602. #endregion
  603. }
  604. cmd.Transaction.Commit();
  605. }
  606. catch (Exception ex)
  607. {
  608. cmd.Transaction.Rollback();
  609. log.Error(ex.Message);
  610. throw new Exception(ex.Message);
  611. }
  612. finally
  613. {
  614. if (conn.State == ConnectionState.Open)
  615. {
  616. conn.Close();
  617. }
  618. conn.Dispose();
  619. }
  620. ResultFlag = true;
  621. }
  622. return ResultFlag;
  623. }
  624. /// <summary>
  625. /// 删除成品入库单
  626. /// </summary>
  627. /// <param name="infos"></param>
  628. /// <returns></returns>
  629. public string Delete(List<ICSManufactureReceiveDoc> infos)
  630. {
  631. List<ICSManufactureReceiveDoc> szJson = new List<ICSManufactureReceiveDoc>();
  632. if (infos.Count <= 0)
  633. {
  634. throw new Exception("传送数据为空!");
  635. }
  636. string connS = "";
  637. string res = string.Empty;
  638. SqlConnection conn = new SqlConnection();
  639. SqlCommand cmd = new SqlCommand();
  640. string sql = string.Empty;
  641. foreach (ICSManufactureReceiveDoc info in infos)
  642. {
  643. try
  644. {
  645. connS = string.Format(connString, info.WorkPoint);
  646. conn = new System.Data.SqlClient.SqlConnection(connS);
  647. conn.Open();
  648. SqlTransaction sqlTran = conn.BeginTransaction();
  649. cmd = new SqlCommand();
  650. cmd.Transaction = sqlTran;
  651. cmd.Connection = conn;
  652. if (info.MTime < new DateTime(2000, 01, 01))
  653. throw new Exception("请输入正确的操作时间:" + info.MTime);
  654. sql = @" DELETE rdrecord10 WHERE ID={0}";
  655. sql = string.Format(sql, info.ID);
  656. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  657. cmd.Transaction.Commit();
  658. }
  659. catch (Exception ex)
  660. {
  661. cmd.Transaction.Rollback();
  662. log.Error(ex.Message);
  663. throw new Exception(ex.Message);
  664. }
  665. finally
  666. {
  667. if (conn.State == ConnectionState.Open)
  668. {
  669. conn.Close();
  670. }
  671. conn.Dispose();
  672. }
  673. }
  674. return res;
  675. }
  676. /// <summary>
  677. ///
  678. /// 记录日志
  679. /// </summary>
  680. /// <param name="Bills"></param>
  681. private void LogInfo(List<ICSManufactureReceiveDoc> Bills)
  682. {
  683. string HeadList = string.Empty;
  684. string BodyList = string.Empty;
  685. foreach (ICSManufactureReceiveDoc head in Bills)
  686. {
  687. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  688. foreach (ICSManufactureReceiveDocs body in head.details)
  689. {
  690. BodyList += "\r\n 表体主键ID: " + body.MODetailID + ",数量:" + body.Quantity;
  691. }
  692. }
  693. log.Info(HeadList);
  694. log.Info(BodyList);
  695. }
  696. }
  697. }