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

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