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

492 lines
27 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using ICSSoft.Common;
  9. using ICSSoft.Entity;
  10. namespace ICSSoft.DataProject
  11. {
  12. public class ISComplete
  13. {
  14. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  15. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  16. public string ConfirmRd08(List<Complete> Bills)
  17. {
  18. int isif = 0;
  19. DataSet ds = null;
  20. string mag = "";
  21. string sql = "";
  22. DataTable dtCheck = null;
  23. string connS = "";
  24. string newsql="";
  25. VouchKey key = new VouchKey();
  26. SqlConnection conn = new SqlConnection();
  27. SqlCommand cmd = new SqlCommand();
  28. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  29. foreach (string WorkPoint in result)
  30. {
  31. try
  32. {
  33. connS = string.Format(connString, WorkPoint);
  34. conn = new System.Data.SqlClient.SqlConnection(connS);
  35. conn.Open();
  36. SqlTransaction sqlTran = conn.BeginTransaction();
  37. cmd = new SqlCommand();
  38. cmd.Transaction = sqlTran;
  39. cmd.Connection = conn;
  40. foreach (Complete head in Bills)
  41. {
  42. if (WorkPoint != head.WorkPoint)
  43. {
  44. continue;
  45. }
  46. if (head.Type == "生产订单")
  47. {
  48. //case when SUM(c.IssQty) is not null and SUM(c.IssQty)<>0 then SUM(c.IssQty)/(SUM(c.Qty)+0.0/b.Qty) else sum(c.Qty) end as lotqty
  49. //SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  50. //case when sum(c.Qty) is not null and sum(c.Qty)<>0
  51. // then
  52. // case when sum(c.Qty) =0
  53. // then 0 else SUM(c.IssQty)/(SUM(c.Qty)+0.0/b.Qty)
  54. // end
  55. // else b.Qty
  56. // end as lotqty
  57. sql = @" select
  58. count(c.AllocateID) num,d.number
  59. from DBO.mom_order a
  60. left join DBO.mom_orderdetail b on a.moid = b.moid
  61. left join DBO.mom_moallocate c on b.modid = c.modid
  62. left join(select modid,COUNT(AllocateID) number from DBO.mom_moallocate where WIPType=1 GROUP BY modid
  63. ) d on b.modid = d.modid
  64. where b.MoDId='{0}'
  65. GROUP BY d.number ";
  66. sql = string.Format(sql, head.DetailID);
  67. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  68. if (dtCheck != null && dtCheck.Rows.Count > 0)
  69. {
  70. if (dtCheck.Rows[0]["num"].ToString() != dtCheck.Rows[0]["number"].ToString())
  71. {
  72. isif = 1;
  73. }
  74. else
  75. {
  76. isif = 0;
  77. }
  78. }
  79. newsql = "where b.MoDId='{0}'";
  80. newsql = string.Format(newsql, head.DetailID);
  81. #region 齐套检验
  82. sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' ";
  83. DataTable qt = DBHelper.SQlReturnData(sql, cmd);
  84. if (qt != null && qt.Rows.Count > 0)
  85. {
  86. if (qt.Rows[0]["cValue"].ToString() == "1")
  87. {
  88. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  89. (
  90. select
  91. a.MoCode,b.SortSeq,
  92. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  93. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,
  94. (case when min (c.IssQty) is null then b.Qty else
  95. (case when min(c.IssQty) =sum(c.Qty) then b.Qty else min(IssQty/BaseQtyN) end)
  96. end) lotqty
  97. from DBO.mom_order a
  98. left join DBO.mom_orderdetail b on a.moid = b.moid
  99. left join DBO.mom_moallocate c on b.modid = c.modid
  100. left join
  101. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  102. DBO.mom_moallocatesub d
  103. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  104. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  105. where (b.MoDId='{0}' and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1) or (b.MoDId='{0}' and c.AllocateId is null)
  106. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  107. ) tt
  108. group by MoCode,SortSeq";
  109. sql = string.Format(sql, head.DetailID);
  110. }
  111. else if (qt.Rows[0]["cValue"].ToString() == "2")
  112. {
  113. sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'";
  114. qt = DBHelper.SQlReturnData(sql, cmd);
  115. if (qt != null && qt.Rows.Count > 0)
  116. {
  117. if (qt.Rows[0]["cValue"].ToString() == "true")
  118. {
  119. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  120. (
  121. select
  122. a.MoCode,b.SortSeq,
  123. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  124. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,
  125. (case when min (c.IssQty) is null then b.Qty else
  126. (case when min(c.IssQty) =sum(c.Qty) then b.Qty else min(IssQty/BaseQtyN) end)
  127. end) lotqty
  128. from DBO.mom_order a
  129. left join DBO.mom_orderdetail b on a.moid = b.moid
  130. left join DBO.mom_moallocate c on b.modid = c.modid
  131. left join
  132. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  133. DBO.mom_moallocatesub d
  134. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  135. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  136. where (b.MoDId='{0}' and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1 and f.bInvKeyPart=1) or (b.MoDId='{0}' and c.AllocateId is null)
  137. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  138. ) tt
  139. group by MoCode,SortSeq";
  140. sql = string.Format(sql, head.DetailID);
  141. }
  142. else
  143. {
  144. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  145. (
  146. select
  147. a.MoCode,b.SortSeq,
  148. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  149. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,
  150. (case when min (c.IssQty) is null then b.Qty else
  151. (case when min(c.IssQty) =sum(c.Qty) then b.Qty else min(IssQty/BaseQtyN) end)
  152. end) lotqty
  153. from DBO.mom_order a
  154. left join DBO.mom_orderdetail b on a.moid = b.moid
  155. left join DBO.mom_moallocate c on b.modid = c.modid
  156. left join
  157. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  158. DBO.mom_moallocatesub d
  159. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  160. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  161. where (b.MoDId='{0}' and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1 ) or (b.MoDId='{0}' and c.AllocateId is null)
  162. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  163. ) tt
  164. group by MoCode,SortSeq";
  165. sql = string.Format(sql, head.DetailID);
  166. }
  167. }
  168. }
  169. else
  170. {
  171. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  172. (
  173. select
  174. a.MoCode,b.SortSeq,
  175. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  176. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,
  177. (case when min (c.IssQty) is null then b.Qty else
  178. (case when min(c.IssQty) =sum(c.Qty) then b.Qty else min(IssQty/BaseQtyN) end)
  179. end) lotqty
  180. from DBO.mom_order a
  181. left join DBO.mom_orderdetail b on a.moid = b.moid
  182. left join DBO.mom_moallocate c on b.modid = c.modid
  183. left join
  184. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  185. DBO.mom_moallocatesub d
  186. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  187. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  188. where (b.MoDId='{0}' and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1 ) or (b.MoDId='{0}' and c.AllocateId is null)
  189. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  190. ) tt
  191. group by MoCode,SortSeq";
  192. sql = string.Format(sql, head.DetailID);
  193. }
  194. }
  195. #endregion
  196. }
  197. else
  198. {
  199. sql = @" select
  200. count(c.MOMaterialsID) num,d.number
  201. from DBO.OM_MOMain a
  202. left join DBO.OM_MODetails b on a.moid = b.moid
  203. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  204. left join(select MODetailsID,COUNT(MOMaterialsID) number from DBO.OM_MOMaterials where iWIPtype=1 GROUP BY MODetailsID
  205. ) d on b.MODetailsID = d.MODetailsID
  206. where b.MODetailsID='{0}'
  207. GROUP BY d.number ";
  208. sql = string.Format(sql, head.DetailID);
  209. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  210. if (dtCheck != null && dtCheck.Rows.Count > 0)
  211. {
  212. if (dtCheck.Rows[0]["num"].ToString() != dtCheck.Rows[0]["number"].ToString())
  213. {
  214. isif = 1;
  215. }
  216. else
  217. {
  218. isif = 0;
  219. }
  220. }
  221. //case when SUM(c.iSendQTY) is not null and SUM(c.iSendQTY)<>0 then (SUM(c.iQuantity)+0.0/b.iQuantity) else sum(c.iQuantity) end as lotqty
  222. // SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  223. //case when sum(c.iQuantity) is not null and sum(c.iQuantity)<>0
  224. // then
  225. // case when sum(c.iQuantity) =0
  226. // then 0 else (SUM(c.iQuantity)+0.0/b.iQuantity)
  227. // end
  228. // else b.iQuantity
  229. // end as lotqty
  230. newsql = " where b.MODetailsID='{0}' ";
  231. newsql = string.Format(newsql, head.DetailID);
  232. #region 齐套检验
  233. sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' ";
  234. DataTable qt = DBHelper.SQlReturnData(sql, cmd);
  235. if (qt != null && qt.Rows.Count > 0)
  236. {
  237. if (qt.Rows[0]["cValue"].ToString() == "1")
  238. {
  239. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  240. (
  241. select
  242. a.cCode,b.iVouchRowNo,
  243. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,
  244. (case when min (c.iSendQTY) is null then b.iQuantity else
  245. (case when min(c.iSendQTY) =sum(c.iQuantity) then b.Qty else min(c.iSendQTY /c.fBaseQtyN) end)
  246. end) lotqty
  247. from DBO.OM_MOMain a
  248. left join DBO.OM_MODetails b on a.moid = b.moid
  249. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  250. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  251. where (b.MODetailsID='{0}' and c.iQuantity<>0 and c.iWIPtype<>1) or (b.MODetailsID='{0}' and c.MOMaterialsID is null)
  252. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  253. ) tt
  254. group by cCode,iVouchRowNo";
  255. sql = string.Format(sql, head.DetailID);
  256. }
  257. else if (qt.Rows[0]["cValue"].ToString() == "2")
  258. {
  259. sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'";
  260. qt = DBHelper.SQlReturnData(sql, cmd);
  261. if (qt != null && qt.Rows.Count > 0)
  262. {
  263. if (qt.Rows[0]["cValue"].ToString() == "true")
  264. {
  265. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  266. (
  267. select
  268. a.cCode,b.iVouchRowNo,
  269. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,
  270. (case when min (c.iSendQTY) is null then b.iQuantity else
  271. (case when min(c.iSendQTY) =sum(c.iQuantity) then b.Qty else min(c.iSendQTY /c.fBaseQtyN) end)
  272. end) lotqty
  273. from DBO.OM_MOMain a
  274. left join DBO.OM_MODetails b on a.moid = b.moid
  275. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  276. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  277. where (b.MODetailsID='{0}' and c.iQuantity<>0 and c.iWIPtype<>1 and f.bInvKeyPart=1) or (b.MODetailsID='{0}' and c.MOMaterialsID is null)
  278. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  279. ) tt
  280. group by cCode,iVouchRowNo";
  281. sql = string.Format(sql, head.DetailID);
  282. qt = DBHelper.SQlReturnData(sql, cmd);
  283. }
  284. else
  285. {
  286. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  287. (
  288. select
  289. a.cCode,b.iVouchRowNo,
  290. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,
  291. (case when min (c.iSendQTY) is null then b.iQuantity else
  292. (case when min(c.iSendQTY) =sum(c.iQuantity) then b.Qty else min(c.iSendQTY /c.fBaseQtyN) end)
  293. end) lotqty
  294. from DBO.OM_MOMain a
  295. left join DBO.OM_MODetails b on a.moid = b.moid
  296. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  297. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  298. where (b.MODetailsID='{0}' and c.iQuantity<>0 and c.iWIPtype<>1 ) or (b.MODetailsID='{0}' and c.MOMaterialsID is null)
  299. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  300. ) tt
  301. group by cCode,iVouchRowNo";
  302. sql = string.Format(sql, head.DetailID);
  303. qt = DBHelper.SQlReturnData(sql, cmd);
  304. }
  305. }
  306. }
  307. else
  308. {
  309. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  310. (
  311. select
  312. a.cCode,b.iVouchRowNo,
  313. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,
  314. (case when min (c.iSendQTY) is null then b.iQuantity else
  315. (case when min(c.iSendQTY) =sum(c.iQuantity) then b.Qty else min(c.iSendQTY /c.fBaseQtyN) end)
  316. end) lotqty
  317. from DBO.OM_MOMain a
  318. left join DBO.OM_MODetails b on a.moid = b.moid
  319. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  320. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  321. where (b.MODetailsID='{0}' and c.iQuantity<>0 ) or (b.MODetailsID='{0}' and c.MOMaterialsID is null)
  322. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  323. ) tt
  324. group by cCode,iVouchRowNo";
  325. sql = string.Format(sql, head.DetailID);
  326. }
  327. }
  328. #endregion
  329. }
  330. if (head.Type == "生产订单")
  331. {
  332. if (isif == 0)
  333. {
  334. sql = @"select DISTINCT a.MoCode,b.SortSeq,C.SortSeq SubSortSeq,(case isnull(d.min_lotqty,0) when 0 then b.Qty else d.min_lotqty end) min_lotqty,
  335. b.Qty,b.QualifiedInQty,b.InvCode,c.InvCode subInvCode,c.Qty AxuQTY,c.IssQty
  336. into #info
  337. from mom_order a
  338. LEFT JOIN mom_orderdetail b on a.moid = b.moid
  339. LEFT JOIN mom_moallocate c on b.modid = c.modid
  340. left join (" + sql + @") d on a.MoCode=d.MoCode and b.SortSeq=d.SortSeq
  341. 1=1
  342. select DISTINCT MoCode,SortSeq,MoCode+'|'+CONVERT(nvarchar(20),SortSeq) GL,min_lotqty,Qty,QualifiedInQty,InvCode from #info
  343. select DISTINCT MoCode,SortSeq,MoCode+'|'+CONVERT(nvarchar(20),SortSeq) GL,SubSortSeq,subInvCode,AxuQTY,IssQty from #info
  344. drop table #info
  345. ";
  346. }
  347. else
  348. {
  349. sql = @"select DISTINCT a.MoCode,b.SortSeq,C.SortSeq SubSortSeq,isnull(d.min_lotqty,0) min_lotqty,b.Qty,b.QualifiedInQty,b.InvCode,c.InvCode subInvCode,c.Qty AxuQTY,c.IssQty
  350. into #info
  351. from mom_order a
  352. LEFT JOIN mom_orderdetail b on a.moid = b.moid
  353. LEFT JOIN mom_moallocate c on b.modid = c.modid
  354. left join (" + sql + @") d on a.MoCode=d.MoCode and b.SortSeq=d.SortSeq
  355. 1=1
  356. select DISTINCT MoCode,SortSeq,MoCode+'|'+CONVERT(nvarchar(20),SortSeq) GL,min_lotqty,Qty,QualifiedInQty,InvCode from #info
  357. select DISTINCT MoCode,SortSeq,MoCode+'|'+CONVERT(nvarchar(20),SortSeq) GL,SubSortSeq,subInvCode,AxuQTY,IssQty from #info
  358. drop table #info
  359. ";
  360. }
  361. sql = sql.Replace("1=1", newsql);
  362. }
  363. else
  364. {
  365. if (isif == 0)
  366. {
  367. sql = @"select DISTINCT a.cCode MoCode,b.iVouchRowNo SortSeq,C.irowno SubSortSeq,(case isnull(d.min_lotqty,0) when 0 then b.iQuantity else d.min_lotqty end) min_lotqty,
  368. b.iQuantity Qty,b.iReceivedQTY QualifiedInQty,b.cInvCode InvCode,c.cInvCode subInvCode,c.iQuantity AxuQTY,c.iSendQTY IssQty
  369. into #info
  370. from OM_MOMain a
  371. left join DBO.OM_MODetails b on a.moid = b.moid
  372. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  373. left join (" + sql + @") d on a.cCode=d.cCode and b.iVouchRowNo=d.iVouchRowNo
  374. 1=1
  375. select DISTINCT MoCode,SortSeq,MoCode+'|'+CONVERT(nvarchar(20),SortSeq) GL,min_lotqty,Qty,QualifiedInQty,InvCode from #info
  376. select DISTINCT MoCode,SortSeq,MoCode+'|'+CONVERT(nvarchar(20),SortSeq) GL,SubSortSeq,subInvCode,AxuQTY,IssQty from #info
  377. drop table #info
  378. ";
  379. }
  380. else
  381. {
  382. sql = @"select DISTINCT a.cCode MoCode,b.iVouchRowNo SortSeq,C.irowno SubSortSeq,isnull(d.min_lotqty,0) min_lotqty,b.iQuantity Qty,b.iReceivedQTY QualifiedInQty,b.cInvCode InvCode,c.cInvCode subInvCode,c.iQuantity AxuQTY,c.iSendQTY IssQty
  383. into #info
  384. from OM_MOMain a
  385. left join DBO.OM_MODetails b on a.moid = b.moid
  386. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  387. left join (" + sql + @") d on a.cCode=d.cCode and b.iVouchRowNo=d.iVouchRowNo
  388. 1=1
  389. select DISTINCT MoCode,SortSeq,MoCode+'|'+CONVERT(nvarchar(20),SortSeq) GL,min_lotqty,Qty,QualifiedInQty,InvCode from #info
  390. select DISTINCT MoCode,SortSeq,MoCode+'|'+CONVERT(nvarchar(20),SortSeq) GL,SubSortSeq,subInvCode,AxuQTY,IssQty from #info
  391. drop table #info
  392. ";
  393. }
  394. sql = sql.Replace("1=1", newsql);
  395. }
  396. if (ds != null)
  397. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  398. else
  399. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  400. cmd.CommandType = CommandType.Text;
  401. cmd.Parameters.Clear();
  402. }
  403. }
  404. catch (Exception ex)
  405. {
  406. if (cmd.Transaction != null)
  407. cmd.Transaction.Rollback();
  408. log.Error(ex.Message);
  409. throw new Exception(ex.Message);
  410. }
  411. finally
  412. {
  413. if (conn.State == ConnectionState.Open)
  414. {
  415. conn.Close();
  416. }
  417. conn.Dispose();
  418. }
  419. }
  420. mag = JSON.DataSetToJson(ds, "details", "GL");
  421. return mag;
  422. }
  423. }
  424. }