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

441 lines
24 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
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. sql = @" select
  49. count(c.AllocateID) num,d.number
  50. from DBO.mom_order a
  51. left join DBO.mom_orderdetail b on a.moid = b.moid
  52. left join DBO.mom_moallocate c on b.modid = c.modid
  53. left join(select modid,COUNT(AllocateID) number from DBO.mom_moallocate where WIPType=1 GROUP BY modid
  54. ) d on b.modid = d.modid
  55. where b.MoDId='{0}'
  56. GROUP BY d.number ";
  57. sql = string.Format(sql, head.DetailID);
  58. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  59. if (dtCheck != null && dtCheck.Rows.Count > 0)
  60. {
  61. if (dtCheck.Rows[0]["num"].ToString() != dtCheck.Rows[0]["number"].ToString())
  62. {
  63. isif = 1;
  64. }
  65. else
  66. {
  67. isif = 0;
  68. }
  69. }
  70. newsql = "where b.MoDId='{0}'";
  71. newsql = string.Format(newsql, head.DetailID);
  72. #region 齐套检验
  73. sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' ";
  74. DataTable qt = DBHelper.SQlReturnData(sql, cmd);
  75. if (qt != null && qt.Rows.Count > 0)
  76. {
  77. if (qt.Rows[0]["cValue"].ToString() == "1")
  78. {
  79. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  80. (
  81. select
  82. a.MoCode,b.SortSeq,
  83. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  84. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  85. from DBO.mom_order a
  86. left join DBO.mom_orderdetail b on a.moid = b.moid
  87. left join DBO.mom_moallocate c on b.modid = c.modid
  88. left join
  89. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  90. DBO.mom_moallocatesub d
  91. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  92. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  93. where b.MoDId='{0}'
  94. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  95. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  96. ) tt
  97. group by MoCode,SortSeq";
  98. sql = string.Format(sql, head.DetailID);
  99. }
  100. else if (qt.Rows[0]["cValue"].ToString() == "2")
  101. {
  102. sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'";
  103. qt = DBHelper.SQlReturnData(sql, cmd);
  104. if (qt != null && qt.Rows.Count > 0)
  105. {
  106. if (qt.Rows[0]["cValue"].ToString() == "true")
  107. {
  108. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  109. (
  110. select
  111. a.MoCode,b.SortSeq,
  112. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  113. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  114. from DBO.mom_order a
  115. left join DBO.mom_orderdetail b on a.moid = b.moid
  116. left join DBO.mom_moallocate c on b.modid = c.modid
  117. left join
  118. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  119. DBO.mom_moallocatesub d
  120. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  121. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  122. where b.MoDId='{0}'
  123. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1 and f.bInvKeyPart=1
  124. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  125. ) tt
  126. group by MoCode,SortSeq";
  127. sql = string.Format(sql, head.DetailID);
  128. }
  129. else
  130. {
  131. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  132. (
  133. select
  134. a.MoCode,b.SortSeq,
  135. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  136. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  137. from DBO.mom_order a
  138. left join DBO.mom_orderdetail b on a.moid = b.moid
  139. left join DBO.mom_moallocate c on b.modid = c.modid
  140. left join
  141. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  142. DBO.mom_moallocatesub d
  143. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  144. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  145. where b.MoDId='{0}'
  146. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  147. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  148. ) tt
  149. group by MoCode,SortSeq";
  150. sql = string.Format(sql, head.DetailID);
  151. }
  152. }
  153. }
  154. else
  155. {
  156. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  157. (
  158. select
  159. a.MoCode,b.SortSeq,
  160. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  161. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  162. from DBO.mom_order a
  163. left join DBO.mom_orderdetail b on a.moid = b.moid
  164. left join DBO.mom_moallocate c on b.modid = c.modid
  165. left join
  166. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  167. DBO.mom_moallocatesub d
  168. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  169. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  170. where b.MoDId='{0}'
  171. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  172. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  173. ) tt
  174. group by MoCode,SortSeq";
  175. sql = string.Format(sql, head.DetailID);
  176. }
  177. }
  178. #endregion
  179. }
  180. else
  181. {
  182. sql = @" select
  183. count(c.MOMaterialsID) num,d.number
  184. from DBO.OM_MOMain a
  185. left join DBO.OM_MODetails b on a.moid = b.moid
  186. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  187. left join(select MODetailsID,COUNT(MOMaterialsID) number from DBO.OM_MOMaterials where iWIPtype=1 GROUP BY MODetailsID
  188. ) d on b.MODetailsID = d.MODetailsID
  189. where b.MODetailsID='{0}'
  190. GROUP BY d.number ";
  191. sql = string.Format(sql, head.DetailID);
  192. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  193. if (dtCheck != null && dtCheck.Rows.Count > 0)
  194. {
  195. if (dtCheck.Rows[0]["num"].ToString() != dtCheck.Rows[0]["number"].ToString())
  196. {
  197. isif = 1;
  198. }
  199. else
  200. {
  201. isif = 0;
  202. }
  203. }
  204. newsql = " where b.MODetailsID='{0}' ";
  205. newsql = string.Format(newsql, head.DetailID);
  206. #region 齐套检验
  207. sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' ";
  208. DataTable qt = DBHelper.SQlReturnData(sql, cmd);
  209. if (qt != null && qt.Rows.Count > 0)
  210. {
  211. if (qt.Rows[0]["cValue"].ToString() == "1")
  212. {
  213. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  214. (
  215. select
  216. a.cCode,b.iVouchRowNo,
  217. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  218. from DBO.OM_MOMain a
  219. left join DBO.OM_MODetails b on a.moid = b.moid
  220. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  221. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  222. where b.MODetailsID='{0}'
  223. and c.iQuantity<>0 and c.iWIPtype<>1
  224. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  225. ) tt
  226. group by cCode,iVouchRowNo";
  227. sql = string.Format(sql, head.DetailID);
  228. }
  229. else if (qt.Rows[0]["cValue"].ToString() == "2")
  230. {
  231. sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'";
  232. qt = DBHelper.SQlReturnData(sql, cmd);
  233. if (qt != null && qt.Rows.Count > 0)
  234. {
  235. if (qt.Rows[0]["cValue"].ToString() == "true")
  236. {
  237. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  238. (
  239. select
  240. a.cCode,b.iVouchRowNo,
  241. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  242. from DBO.OM_MOMain a
  243. left join DBO.OM_MODetails b on a.moid = b.moid
  244. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  245. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  246. where b.MODetailsID='{0}'
  247. and c.iQuantity<>0 and c.iWIPtype<>1 and f.bInvKeyPart=1
  248. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  249. ) tt
  250. group by cCode,iVouchRowNo";
  251. sql = string.Format(sql, head.DetailID);
  252. qt = DBHelper.SQlReturnData(sql, cmd);
  253. }
  254. else
  255. {
  256. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  257. (
  258. select
  259. a.cCode,b.iVouchRowNo,
  260. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  261. from DBO.OM_MOMain a
  262. left join DBO.OM_MODetails b on a.moid = b.moid
  263. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  264. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  265. where b.MODetailsID='{0}'
  266. and c.iQuantity<>0 and c.iWIPtype<>1
  267. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  268. ) tt
  269. group by cCode,iVouchRowNo";
  270. sql = string.Format(sql, head.DetailID);
  271. qt = DBHelper.SQlReturnData(sql, cmd);
  272. }
  273. }
  274. }
  275. else
  276. {
  277. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  278. (
  279. select
  280. a.cCode,b.iVouchRowNo,
  281. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  282. from DBO.OM_MOMain a
  283. left join DBO.OM_MODetails b on a.moid = b.moid
  284. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  285. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  286. where b.MODetailsID='{0}'
  287. and c.iQuantity<>0
  288. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  289. ) tt
  290. group by cCode,iVouchRowNo";
  291. sql = string.Format(sql, head.DetailID);
  292. }
  293. }
  294. #endregion
  295. }
  296. if (head.Type == "生产订单")
  297. {
  298. if (isif == 0)
  299. {
  300. 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,
  301. b.Qty,b.QualifiedInQty,b.InvCode,c.InvCode subInvCode,c.Qty AxuQTY,c.IssQty
  302. into #info
  303. from mom_order a
  304. LEFT JOIN mom_orderdetail b on a.moid = b.moid
  305. LEFT JOIN mom_moallocate c on b.modid = c.modid
  306. left join (" + sql + @") d on a.MoCode=d.MoCode and b.SortSeq=d.SortSeq
  307. 1=1
  308. select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,min_lotqty,Qty,QualifiedInQty,InvCode from #info
  309. select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,SubSortSeq,subInvCode,AxuQTY,IssQty from #info
  310. drop table #info
  311. ";
  312. }
  313. else
  314. {
  315. 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
  316. into #info
  317. from mom_order a
  318. LEFT JOIN mom_orderdetail b on a.moid = b.moid
  319. LEFT JOIN mom_moallocate c on b.modid = c.modid
  320. left join (" + sql + @") d on a.MoCode=d.MoCode and b.SortSeq=d.SortSeq
  321. 1=1
  322. select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,min_lotqty,Qty,QualifiedInQty,InvCode from #info
  323. select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,SubSortSeq,subInvCode,AxuQTY,IssQty from #info
  324. drop table #info
  325. ";
  326. }
  327. sql = sql.Replace("1=1", newsql);
  328. }
  329. else
  330. {
  331. if (isif == 0)
  332. {
  333. 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,
  334. b.iQuantity Qty,b.iReceivedQTY QualifiedInQty,b.cInvCode InvCode,c.cInvCode subInvCode,c.iQuantity AxuQTY,c.iSendQTY IssQty
  335. into #info
  336. from OM_MOMain a
  337. left join DBO.OM_MODetails b on a.moid = b.moid
  338. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  339. left join (" + sql + @") d on a.cCode=d.cCode and b.iVouchRowNo=d.iVouchRowNo
  340. 1=1
  341. select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,min_lotqty,Qty,QualifiedInQty,InvCode from #info
  342. select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,SubSortSeq,subInvCode,AxuQTY,IssQty from #info
  343. drop table #info
  344. ";
  345. }
  346. else
  347. {
  348. 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
  349. into #info
  350. from OM_MOMain a
  351. left join DBO.OM_MODetails b on a.moid = b.moid
  352. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  353. left join (" + sql + @") d on a.cCode=d.cCode and b.iVouchRowNo=d.iVouchRowNo
  354. 1=1
  355. select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,min_lotqty,Qty,QualifiedInQty,InvCode from #info
  356. select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,SubSortSeq,subInvCode,AxuQTY,IssQty from #info
  357. drop table #info
  358. ";
  359. }
  360. sql = sql.Replace("1=1", newsql);
  361. }
  362. if (ds != null)
  363. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  364. else
  365. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  366. }
  367. }
  368. catch (Exception ex)
  369. {
  370. if (cmd.Transaction != null)
  371. cmd.Transaction.Rollback();
  372. log.Error(ex.Message);
  373. throw new Exception(ex.Message);
  374. }
  375. finally
  376. {
  377. if (conn.State == ConnectionState.Open)
  378. {
  379. conn.Close();
  380. }
  381. conn.Dispose();
  382. }
  383. }
  384. mag = JSON.DataSetToJson(ds, "details", "GL");
  385. return mag;
  386. }
  387. }
  388. }