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

352 lines
20 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
  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. DataTable dtNew1 = null;
  19. DataTable dtNew2 = null;
  20. string mag = "";
  21. string sql = "";
  22. string connS = "";
  23. string newsql="";
  24. VouchKey key = new VouchKey();
  25. SqlConnection conn = new SqlConnection();
  26. SqlCommand cmd = new SqlCommand();
  27. try
  28. {
  29. foreach (Complete head in Bills)
  30. {
  31. connS = string.Format(connString, head.WorkPoint);
  32. conn = new System.Data.SqlClient.SqlConnection(connS);
  33. conn.Open();
  34. SqlTransaction sqlTran = conn.BeginTransaction();
  35. cmd = new SqlCommand();
  36. cmd.Transaction = sqlTran;
  37. cmd.Connection = conn;
  38. foreach (Completes body in head.details)
  39. {
  40. if (head.SourceType == "生产订单")
  41. {
  42. newsql = "where a.MoCode='{0}' and b.SortSeq='{1}'";
  43. newsql = string.Format(newsql, head.SourceCode, body.Sequence);
  44. #region 齐套检验
  45. sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' ";
  46. DataTable qt = DBHelper.SQlReturnData(sql, cmd);
  47. if (qt != null && qt.Rows.Count > 0)
  48. {
  49. if (qt.Rows[0]["cValue"].ToString() == "1")
  50. {
  51. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  52. (
  53. select
  54. a.MoCode,b.SortSeq,
  55. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  56. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  57. from DBO.mom_order a
  58. left join DBO.mom_orderdetail b on a.moid = b.moid
  59. left join DBO.mom_moallocate c on b.modid = c.modid
  60. left join
  61. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  62. DBO.mom_moallocatesub d
  63. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  64. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  65. where a.MoCode='{0}' and b.SortSeq='{1}'
  66. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  67. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  68. ) tt
  69. group by MoCode,SortSeq";
  70. sql = string.Format(sql, head.SourceCode, body.Sequence);
  71. }
  72. else if (qt.Rows[0]["cValue"].ToString() == "2")
  73. {
  74. sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'";
  75. qt = DBHelper.SQlReturnData(sql, cmd);
  76. if (qt != null && qt.Rows.Count > 0)
  77. {
  78. if (qt.Rows[0]["cValue"].ToString() == "true")
  79. {
  80. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  81. (
  82. select
  83. a.MoCode,b.SortSeq,
  84. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  85. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  86. from DBO.mom_order a
  87. left join DBO.mom_orderdetail b on a.moid = b.moid
  88. left join DBO.mom_moallocate c on b.modid = c.modid
  89. left join
  90. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  91. DBO.mom_moallocatesub d
  92. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  93. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  94. where a.MoCode='{0}' and b.SortSeq='{1}'
  95. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1 and f.bInvKeyPart=1
  96. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  97. ) tt
  98. group by MoCode,SortSeq";
  99. sql = string.Format(sql, head.SourceCode, body.Sequence);
  100. }
  101. else
  102. {
  103. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  104. (
  105. select
  106. a.MoCode,b.SortSeq,
  107. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  108. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  109. from DBO.mom_order a
  110. left join DBO.mom_orderdetail b on a.moid = b.moid
  111. left join DBO.mom_moallocate c on b.modid = c.modid
  112. left join
  113. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  114. DBO.mom_moallocatesub d
  115. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  116. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  117. where a.MoCode='{0}' and b.SortSeq='{1}'
  118. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  119. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  120. ) tt
  121. group by MoCode,SortSeq";
  122. sql = string.Format(sql, head.SourceCode, body.Sequence);
  123. }
  124. }
  125. }
  126. else
  127. {
  128. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  129. (
  130. select
  131. a.MoCode,b.SortSeq,
  132. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  133. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  134. from DBO.mom_order a
  135. left join DBO.mom_orderdetail b on a.moid = b.moid
  136. left join DBO.mom_moallocate c on b.modid = c.modid
  137. left join
  138. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  139. DBO.mom_moallocatesub d
  140. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  141. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  142. where a.MoCode='{0}' and b.SortSeq='{1}'
  143. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  144. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  145. ) tt
  146. group by MoCode,SortSeq";
  147. sql = string.Format(sql, head.SourceCode, body.Sequence);
  148. }
  149. }
  150. #endregion
  151. }
  152. else
  153. {
  154. newsql = " where a.cCode='{0}' and b.iVouchRowNo='{1}'";
  155. newsql = string.Format(newsql, head.SourceCode, body.Sequence);
  156. #region 齐套检验
  157. sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' ";
  158. DataTable qt = DBHelper.SQlReturnData(sql, cmd);
  159. if (qt != null && qt.Rows.Count > 0)
  160. {
  161. if (qt.Rows[0]["cValue"].ToString() == "1")
  162. {
  163. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  164. (
  165. select
  166. a.cCode,b.iVouchRowNo,
  167. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  168. from DBO.OM_MOMain a
  169. left join DBO.OM_MODetails b on a.moid = b.moid
  170. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  171. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  172. where a.cCode='{0}' and b.iVouchRowNo='{1}'
  173. and c.iQuantity<>0
  174. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  175. ) tt
  176. group by cCode,iVouchRowNo";
  177. sql = string.Format(sql, head.SourceCode, body.Sequence);
  178. }
  179. else if (qt.Rows[0]["cValue"].ToString() == "2")
  180. {
  181. sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'";
  182. qt = DBHelper.SQlReturnData(sql, cmd);
  183. if (qt != null && qt.Rows.Count > 0)
  184. {
  185. if (qt.Rows[0]["cValue"].ToString() == "true")
  186. {
  187. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  188. (
  189. select
  190. a.cCode,b.iVouchRowNo,
  191. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  192. from DBO.OM_MOMain a
  193. left join DBO.OM_MODetails b on a.moid = b.moid
  194. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  195. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  196. where a.cCode='{0}' and b.iVouchRowNo='{1}'
  197. and c.iQuantity<>0
  198. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  199. ) tt
  200. group by cCode,iVouchRowNo";
  201. sql = string.Format(sql, head.SourceCode, body.Sequence);
  202. qt = DBHelper.SQlReturnData(sql, cmd);
  203. }
  204. else
  205. {
  206. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  207. (
  208. select
  209. a.cCode,b.iVouchRowNo,
  210. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  211. from DBO.OM_MOMain a
  212. left join DBO.OM_MODetails b on a.moid = b.moid
  213. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  214. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  215. where a.cCode='{0}' and b.iVouchRowNo='{1}'
  216. and c.iQuantity<>0
  217. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  218. ) tt
  219. group by cCode,iVouchRowNo";
  220. sql = string.Format(sql, head.SourceCode, body.Sequence);
  221. qt = DBHelper.SQlReturnData(sql, cmd);
  222. }
  223. }
  224. }
  225. else
  226. {
  227. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  228. (
  229. select
  230. a.cCode,b.iVouchRowNo,
  231. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  232. from DBO.OM_MOMain a
  233. left join DBO.OM_MODetails b on a.moid = b.moid
  234. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  235. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  236. where a.cCode='{0}' and b.iVouchRowNo='{1}'
  237. and c.iQuantity<>0
  238. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  239. ) tt
  240. group by cCode,iVouchRowNo";
  241. sql = string.Format(sql, head.SourceCode, body.Sequence);
  242. }
  243. }
  244. #endregion
  245. }
  246. }
  247. if (head.SourceType == "生产订单")
  248. {
  249. sql = @"select a.MoCode,b.SortSeq,C.SortSeq SubSortSeq,d.min_lotqty,b.Qty,b.QualifiedInQty,b.InvCode,c.InvCode subInvCode,c.Qty AxuQTY,c.IssQty
  250. into #info
  251. from mom_order a
  252. LEFT JOIN mom_orderdetail b on a.moid = b.moid
  253. LEFT JOIN mom_moallocate c on b.modid = c.modid
  254. left join (" + sql + @") d on a.MoCode=d.MoCode and b.SortSeq=d.SortSeq
  255. 1=1
  256. select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,min_lotqty,Qty,QualifiedInQty,InvCode from #info
  257. select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,SubSortSeq,subInvCode,AxuQTY,IssQty from #info
  258. ";
  259. sql = sql.Replace("1=1", newsql);
  260. }
  261. else
  262. {
  263. sql = @"select a.cCode MoCode,b.iVouchRowNo SortSeq,C.irowno SubSortSeq,d.min_lotqty,b.iQuantity Qty,b.iReceivedQTY QualifiedInQty,b.cInvCode InvCode,c.cInvCode subInvCode,c.iQuantity AxuQTY,c.iSendQTY IssQty
  264. into #info
  265. from OM_MOMain a
  266. left join DBO.OM_MODetails b on a.moid = b.moid
  267. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  268. left join (" + sql + @") d on a.cCode=d.cCode and b.iVouchRowNo=d.iVouchRowNo
  269. 1=1
  270. select DISTINCT MoCode,SortSeq,min_lotqty,Qty,QualifiedInQty,InvCode from #info
  271. select DISTINCT MoCode,SortSeq,SubSortSeq,subInvCode,AxuQTY,IssQty from #info
  272. ";
  273. sql = sql.Replace("1=1", newsql);
  274. }
  275. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  276. if (dtNew1 == null)
  277. dtNew1 = ds.Tables[0];
  278. else
  279. dtNew1 = ds.Tables[0];
  280. if (dtNew2 == null)
  281. dtNew2 = ds.Tables[1];
  282. else
  283. dtNew2 = ds.Tables[1];
  284. }
  285. }
  286. catch (Exception ex)
  287. {
  288. cmd.Transaction.Rollback();
  289. log.Error(ex.Message);
  290. throw new Exception(ex.Message);
  291. }
  292. finally
  293. {
  294. if (conn.State == ConnectionState.Open)
  295. {
  296. conn.Close();
  297. }
  298. conn.Dispose();
  299. }
  300. string RelationName = "details";
  301. DataSet dz = new DataSet();
  302. dtNew1.TableName = "Table0";
  303. dtNew2.TableName = "Table1";
  304. dz.Tables.Add(dtNew1.Copy());
  305. dz.Tables.Add(dtNew2.Copy());
  306. DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["GL"], dz.Tables[1].Columns["GL"]);
  307. dz.Relations.Add(dh);
  308. mag = DBHelper.DataSetToJson(dz, RelationName);
  309. return mag;
  310. }
  311. }
  312. }