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

1260 lines
82 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
  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. using ICSSoft.DataProject;
  11. using Newtonsoft.Json;
  12. using ICSSoft.Entity.ASK;
  13. namespace ICSSoft.DataProject.ASK
  14. {
  15. public class ManufactureReceiveDocASK
  16. {
  17. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  18. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  19. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  20. private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"];
  21. private static string CRNALL = System.Configuration.ConfigurationManager.AppSettings["ManuFaCRDNAME"];
  22. private static string CRNA = System.Configuration.ConfigurationManager.AppSettings["MoICRDNAME"];
  23. public string CreateManufactureReceiveDoc(List<ICSManufactureReceiveDocASK> Bills)
  24. {
  25. string msg = "";
  26. DataSet ds = null;
  27. string iFatherIdTwo = "";
  28. string iFatherIdTree = "";
  29. List<WH> LWH = new List<WH>();
  30. int isif = 0;
  31. string connS = "";
  32. string MoClass = "";
  33. int num = 0;
  34. int news = 0;
  35. int iseq = 0;
  36. int out11 = 0;
  37. int irownnew = 0;
  38. string sql = string.Empty;
  39. string mocode = "";
  40. string seq = "";
  41. //取得out库单的默认显示模版
  42. //取得采购入库单单据 表头ID,表体DID
  43. VouchKey key = new VouchKey();
  44. SqlConnection conn = new SqlConnection();
  45. SqlCommand cmd = new SqlCommand();
  46. if (Bills.Count <= 0)
  47. {
  48. throw new Exception("传送数据为空!");
  49. }
  50. LogInfo(Bills);
  51. foreach (ICSManufactureReceiveDocASK head in Bills)
  52. {
  53. foreach (ICSManufactureReceiveDocASKS load in head.details)
  54. {
  55. foreach (ICSManufactureReceiveDocASKSS loads in load.detailss)
  56. {
  57. news++;
  58. }
  59. }
  60. }
  61. if (news != 0)
  62. {
  63. foreach (ICSManufactureReceiveDocASK head in Bills)
  64. {
  65. foreach (ICSManufactureReceiveDocASKS load in head.details)
  66. {
  67. foreach (ICSManufactureReceiveDocASKSS loads in load.detailss)
  68. {
  69. WH wh = new WH();
  70. wh.WHCODE = loads.WHCode;
  71. wh.seq = 0;
  72. if (LWH.Count != 0)
  73. {
  74. int whnumber = 0;
  75. foreach (WH item in LWH)
  76. {
  77. if (item.WHCODE == wh.WHCODE)
  78. whnumber++;
  79. }
  80. if (whnumber == 0)
  81. LWH.Add(wh);
  82. }
  83. else
  84. LWH.Add(wh);
  85. }
  86. }
  87. }
  88. }
  89. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  90. foreach (string WorkPoint in result)
  91. {
  92. try
  93. {
  94. connS = string.Format(connString, WorkPoint);
  95. conn = new System.Data.SqlClient.SqlConnection(connS);
  96. conn.Open();
  97. SqlTransaction sqlTran = conn.BeginTransaction();
  98. cmd = new SqlCommand();
  99. cmd.Transaction = sqlTran;
  100. cmd.Connection = conn;
  101. #region 连副产品
  102. foreach (ICSManufactureReceiveDocASK head in Bills)
  103. {
  104. iFatherIdTwo = "";
  105. if (WorkPoint != head.WorkPoint)
  106. continue;
  107. if (head.TYPE != "连副产品")
  108. continue;
  109. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  110. throw new Exception("U8正在整理现存量,请稍后再试");
  111. string[] ss = head.WorkPoint.Split('_');
  112. ERPDB = ss[1];
  113. ICSUserInfo userInfo = new ICSUserInfo();
  114. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  115. string[] dd = CRNALL.Split('~');
  116. string crdname = dd[0];
  117. string carname = dd[1];
  118. string surface = dd[2];
  119. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  120. num = head.details.Count();
  121. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint,cmd);
  122. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  123. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  124. DateTime date = DateTime.Now;
  125. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  126. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  127. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode, head.WorkPoint,cmd);
  128. DateTime dvDate = DateTime.Now.Date;
  129. #region 产成品入库单表头
  130. sql = @"INSERT INTO dbo.rdrecord10
  131. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,
  132. cCode ,cRdCode ,cDepCode ,cHandler ,bTransFlag ,cMaker ,
  133. cDefine1 ,cDefine2 ,cDefine3 ,cDefine4 ,cDefine5 ,cDefine6 ,
  134. cDefine7 ,cDefine8 ,cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,
  135. biafirst ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,cDefine13 ,
  136. cDefine14 ,cDefine15 ,cDefine16 ,cMPoCode ,iproorderid ,
  137. bFromPreYear ,bIsComplement ,iDiscountTaxType ,ireturncount ,
  138. iverifystate ,iswfcontrolled ,cModifyPerson ,dnmaketime ,
  139. dnverifytime ,bredvouch ,iPrintCount )
  140. SELECT @ID,1,'10','','',@cWhCode,convert(datetime,convert(varchar(10),getdate(),120)),
  141. @cCode,@RdCode,@DepCode,@cHandler,0,@cMaker,
  142. a.Define1,a.Define2,a.Define3,a.Define4,a.Define5,a.Define6,
  143. a.Define7,a.Define8,a.Define9,a.Define10,@dDate,0,
  144. 0,@VT_ID,0,a.Define11,a.Define12,a.Define13,
  145. a.Define14,a.Define15,a.Define16,a.MoCode,a.MoId,
  146. 0,0,0,0,
  147. 0,0,'',@dDate,
  148. @dDate,0,0
  149. FROM dbo.mom_order a
  150. WHERE a.MoCode='" + head.MOCode + "'";
  151. cmd.Parameters.Clear();
  152. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  153. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  154. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  155. cmd.Parameters.Add(new SqlParameter("@cDepCode", head.DepCode));
  156. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  157. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  158. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  159. cmd.Parameters.Add(new SqlParameter("@DepCode", head.DepCode));
  160. cmd.Parameters.Add(new SqlParameter("@dDate", dvDate));
  161. cmd.Parameters.Add(new SqlParameter("@RdCode", cRDcode));
  162. cmd.CommandText = sql;
  163. try
  164. {
  165. int count = cmd.ExecuteNonQuery();
  166. if (count <= 0)
  167. {
  168. log.Error("生成连副产品入库单表头失败,受影响行数<=0;");
  169. throw new Exception("生成连副产品入库单表头失败,受影响行数<=0;");
  170. }
  171. }
  172. catch (Exception ex)
  173. {
  174. log.Error("生成连副产品入库单表头失败!SQL:\r\n" + sql, ex);
  175. throw new Exception("生成连副产品入库单表头失败!SQL:\r\n" + sql, ex);
  176. }
  177. #endregion
  178. #region 产成品入库单表体
  179. int irowno = 0;
  180. foreach (ICSManufactureReceiveDocASKS body in head.details)
  181. {
  182. //if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  183. //{
  184. // if (body.BatchCode == "")
  185. // {
  186. // log.Error("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  187. // throw new Exception("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  188. // }
  189. //}
  190. //else
  191. //{
  192. // if (body.BatchCode != "")
  193. // {
  194. // log.Error("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  195. // throw new Exception("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  196. // }
  197. //}
  198. irowno++;
  199. #region 判断工单行是否关闭,关闭不能入库
  200. sql = @"SELECT ISNULL(a.CloseUser,'') as CloseUser,b.MoCode,a.SortSeq FROM dbo.mom_orderdetail a INNER JOIN
  201. dbo.mom_order b ON b.MoId = a.MoId WHERE a.MoDId =(select MoDId from dbo.mom_moallocate where AllocateId='" + body.MODetailID + "')";
  202. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  203. if (dtCheck != null && dtCheck.Rows.Count > 0)
  204. {
  205. mocode = dtCheck.Rows[0]["MoCode"].ToString();
  206. seq = dtCheck.Rows[0]["SortSeq"].ToString();
  207. if (dtCheck.Rows[0]["CloseUser"].ToString()!="")
  208. throw new Exception("工单号:" + dtCheck.Rows[0]["MoCode"].ToString() + ",行号:" + dtCheck.Rows[0]["SortSeq"].ToString() + ",已关闭,无法入库!");
  209. sql = @" select
  210. count(c.AllocateID) num,d.number
  211. from DBO.mom_order a
  212. left join DBO.mom_orderdetail b on a.moid = b.moid
  213. left join DBO.mom_moallocate c on b.modid = c.modid
  214. left join(select modid,COUNT(AllocateID) number from DBO.mom_moallocate where WIPType=1 GROUP BY modid
  215. ) d on b.modid = d.modid
  216. where a.MoCode='{0}' and b.SortSeq='{1}'
  217. GROUP BY d.number ";
  218. sql = string.Format(sql, mocode, seq);
  219. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  220. if (dtCheck != null && dtCheck.Rows.Count > 0)
  221. {
  222. if (dtCheck.Rows[0]["num"].ToString() != dtCheck.Rows[0]["number"].ToString())
  223. isif = 1;
  224. else
  225. isif = 0;
  226. }
  227. }
  228. #endregion
  229. #region 判断是否都是联.副产品
  230. sql = @"select ProductType from dbo.mom_moallocate where AllocateId='" + body.MODetailID + "'";
  231. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  232. if (dtCheck != null && dtCheck.Rows.Count > 0)
  233. {
  234. string ProductType = dtCheck.Rows[0]["ProductType"].ToString();
  235. if (Convert.ToInt32(ProductType) == 1)
  236. throw new Exception("该返工工单存在非联、副产品子件,无法入库!");
  237. }
  238. #endregion
  239. #region 生成产成品入库单表体
  240. sql = @"INSERT INTO dbo.rdrecords10
  241. ( AutoID ,ID ,cInvCode ,iQuantity ,cBatch ,iFlag ,iNum,
  242. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  243. cItemCode ,cName ,iNQuantity ,cDefine28 ,
  244. cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,cDefine33 ,cDefine34 ,
  245. cDefine35 ,cDefine36 ,cDefine37 ,iMPoIds ,bRelated ,bLPUseFree ,
  246. iRSRowNO ,iOriTrackID ,bCosting ,cmocode ,imoseq ,iExpiratDateCalcu ,
  247. iorderdid ,iordertype,iordercode,iorderseq,isodid,isotype,csocode,isoseq,irowno ,iposflag ,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  248. SELECT @AutoID,@ID,@cInvCode,@iQuantity,@cBatch,0,@iNum,
  249. a.Define22,a.Define23,a.Define24,a.Define25,a.Define26,a.Define27,
  250. @cItemCode,null,c.Qty-isnull(c.QualifiedInQty,0),c.Define28,
  251. c.Define29,c.Define30,c.Define31,c.Define32,c.Define33,c.Define34,
  252. c.Define35,c.Define36,c.Define37,c.MoDId,0,0,
  253. 0,0,1,b.MoCode,a.SortSeq,0,
  254. a.AllocateId,c.OrderType,c.OrderCode,c.OrderSeq,c.SoDId,c.SoType,c.SoCode,c.SoSeq,@irowno,0,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  255. FROM dbo.mom_moallocate a
  256. INNER join dbo.mom_orderdetail c on a.MoDId=c.MoDId
  257. INNER JOIN dbo.mom_order b ON c.MoId=b.MoId
  258. WHERE a.AllocateId='" + body.MODetailID + "' ";
  259. cmd.Parameters.Clear();
  260. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  261. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  262. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  263. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  264. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount.ToString("0.00")));
  265. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  266. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  267. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  268. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  269. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  270. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  271. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  272. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  273. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  274. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  275. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  276. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  277. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  278. cmd.CommandText = sql;
  279. try
  280. {
  281. int count = cmd.ExecuteNonQuery();
  282. if (count <= 0)
  283. {
  284. log.Error("生成材料出库单表头失败,受影响行数<=0;");
  285. throw new Exception("生成材料出库单表头失败,受影响行数<=0;");
  286. }
  287. }
  288. catch (Exception ex)
  289. {
  290. log.Error("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  291. throw new Exception("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  292. }
  293. #endregion
  294. if (isif == 1)
  295. {
  296. if (head.CompleteVerification)
  297. {
  298. #region 齐套检验
  299. sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' ";
  300. DataTable qt = DBHelper.SQlReturnData(sql, cmd);
  301. if (qt != null && qt.Rows.Count > 0)
  302. {
  303. if (qt.Rows[0]["cValue"].ToString() == "1")
  304. {
  305. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  306. (
  307. select
  308. a.MoCode,b.SortSeq,
  309. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  310. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  311. from DBO.mom_order a
  312. left join DBO.mom_orderdetail b on a.moid = b.moid
  313. left join DBO.mom_moallocate c on b.modid = c.modid
  314. left join
  315. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  316. DBO.mom_moallocatesub d
  317. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  318. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  319. where a.MoCode='{0}' and b.SortSeq='{1}'
  320. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  321. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  322. ) tt
  323. group by MoCode,SortSeq";
  324. sql = string.Format(sql, mocode, seq);
  325. qt = DBHelper.SQlReturnData(sql, cmd);
  326. if (qt == null || qt.Rows.Count == 0)
  327. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  328. else
  329. {
  330. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  331. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料<=0!");
  332. }
  333. }
  334. else if (qt.Rows[0]["cValue"].ToString() == "2")
  335. {
  336. sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'";
  337. qt = DBHelper.SQlReturnData(sql, cmd);
  338. if (qt != null && qt.Rows.Count > 0)
  339. {
  340. if (qt.Rows[0]["cValue"].ToString() == "true")
  341. {
  342. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  343. (
  344. select
  345. a.MoCode,b.SortSeq,
  346. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  347. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  348. from DBO.mom_order a
  349. left join DBO.mom_orderdetail b on a.moid = b.moid
  350. left join DBO.mom_moallocate c on b.modid = c.modid
  351. left join
  352. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  353. DBO.mom_moallocatesub d
  354. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  355. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  356. where a.MoCode='{0}' and b.SortSeq='{1}'
  357. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1 and f.bInvKeyPart=1
  358. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  359. ) tt
  360. group by MoCode,SortSeq";
  361. sql = string.Format(sql, mocode, seq);
  362. qt = DBHelper.SQlReturnData(sql, cmd);
  363. if (qt == null || qt.Rows.Count == 0)
  364. {
  365. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  366. }
  367. else
  368. {
  369. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  370. {
  371. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  372. }
  373. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity <= 0)
  374. {
  375. throw new Exception("该物料:" + body.InvCode + @"入库数量大于关键物料领料套数!");
  376. }
  377. }
  378. }
  379. else
  380. {
  381. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  382. (
  383. select
  384. a.MoCode,b.SortSeq,
  385. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  386. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  387. from DBO.mom_order a
  388. left join DBO.mom_orderdetail b on a.moid = b.moid
  389. left join DBO.mom_moallocate c on b.modid = c.modid
  390. left join
  391. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  392. DBO.mom_moallocatesub d
  393. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  394. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  395. where a.MoCode='{0}' and b.SortSeq='{1}'
  396. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  397. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  398. ) tt
  399. group by MoCode,SortSeq";
  400. sql = string.Format(sql, mocode, seq);
  401. qt = DBHelper.SQlReturnData(sql, cmd);
  402. if (qt == null || qt.Rows.Count == 0)
  403. {
  404. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  405. }
  406. else
  407. {
  408. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  409. {
  410. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  411. }
  412. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity <= 0)
  413. {
  414. throw new Exception("该物料:" + body.InvCode + @"入库数量大于领料套数!");
  415. }
  416. }
  417. }
  418. }
  419. }
  420. }
  421. #endregion
  422. }
  423. }
  424. #region 回写生产子件已领数量
  425. sql = @"
  426. UPDATE a SET a.IssQty=ISNULL(a.IssQty,0)+" + body.Quantity + @" from dbo.mom_moallocate a
  427. inner join dbo.mom_orderdetail b on a.MoDId=b.MoDId
  428. where a.AllocateId='" + body.MODetailID + "' and a.SortSeq='" + body.Sequence + @"'
  429. ";
  430. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写生产订单入库数量失败!");
  431. #endregion
  432. sql = @"select isnull(IssQty,0)fOutQuantity,isnull(Qty,0)iQuantity,ISNULL(AuxQty, 0)iNum,ISNULL(NiNum, 0)NiNum,ISNULL(AuxQty, 0)-ISNULL(NiNum, 0) SiNum from mom_moallocate a
  433. LEFT JOIN (select sum(iNum) NiNum,iorderdid from rdrecords10 GROUP BY iorderdid) b on a.AllocateId=b.iorderdid
  434. where a.AllocateId='" + body.MODetailID + "'";
  435. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  436. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  437. {
  438. foreach (DataRow item in dtDisCheck.Rows)
  439. {
  440. if (Convert.ToDecimal(item["fOutQuantity"].ToString()) == Convert.ToDecimal(item["iQuantity"].ToString()))
  441. {
  442. if (Convert.ToDecimal(item["iNum"].ToString()) != 0)
  443. {
  444. if (Convert.ToDecimal(item["SiNum"].ToString()) != 0)
  445. {
  446. sql = @"Update rdrecords10 set iNum=isnull(iNum,0)+(" + item["SiNum"].ToString() + ") where autoid='" + iChildId + "' ";
  447. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item["SiNum"].ToString()));
  448. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  449. }
  450. }
  451. }
  452. }
  453. }
  454. #region 更新现存量
  455. key.cBustypeUN = "成品入库";
  456. key.cVouchTypeUN = "10";
  457. key.TableName = "IA_ST_UnAccountVouch10";
  458. //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);
  459. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, "", body.Quantity, body.Amount, body.cFree1,
  460. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  461. "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  462. #endregion
  463. #region 判断工单行是否全部入库
  464. sql = "select Qty from mom_moallocate WHERE MoDId=(select MoDId from dbo.mom_moallocate where AllocateId='" + body.MODetailID + @"') and IssQty-qty<0
  465. union all
  466. select Qty from mom_orderdetail where MoDId=(select MoDId from dbo.mom_moallocate where AllocateId='" + body.MODetailID + @"') and QualifiedInQty-Qty<0";
  467. DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  468. if (dtClose.Rows.Count == 0)
  469. {
  470. sql = @"
  471. update mom_orderdetail set CloseTime = GETDATE(),CloseDate= CONVERT(NVARCHAR(15),GETDATE(),23), CloseUser ='" + head.User + "',Status=4 where MODId=(select MoDId from dbo.mom_moallocate where AllocateId='" + body.MODetailID + @"')";
  472. DBHelper.CmdExecuteNonQuery(sql, cmd, "生产订单行关闭失败!");
  473. }
  474. #endregion
  475. iChildId--;
  476. }
  477. #endregion
  478. }
  479. #endregion
  480. #region 入库倒冲
  481. foreach (ICSManufactureReceiveDocASK head in Bills)
  482. {
  483. if (WorkPoint != head.WorkPoint)
  484. continue;
  485. if (head.TYPE != "入库")
  486. continue;
  487. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  488. throw new Exception("U8正在整理现存量,请稍后再试");
  489. string[] ss = head.WorkPoint.Split('_');
  490. ERPDB = ss[1];
  491. ICSUserInfo userInfo = new ICSUserInfo();
  492. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  493. string[] dd = CRNALL.Split('~');
  494. string crdname = dd[0];
  495. string carname = dd[1];
  496. string surface = dd[2];
  497. string[] d = CRNA.Split('~');
  498. string crdname1 = d[0];
  499. string carname1 = d[1];
  500. string surface1 = d[2];
  501. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  502. string cardnewcode1 = DBHelper.GetCardNumber(carname1, cmd);
  503. num = head.details.Count();
  504. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint,cmd);
  505. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  506. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  507. DateTime date = DateTime.Now;
  508. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  509. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  510. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode, head.WorkPoint, cmd);
  511. string cRDcode1 = DBHelper.GetRDCode(crdname1, cmd);
  512. string DEF_ID1 = DBHelper.GetDefaultTemplate(cardnewcode1, cmd);
  513. DateTime dvDate = DateTime.Now.Date;
  514. #region 产成品入库单表头
  515. sql = @"INSERT INTO dbo.rdrecord10
  516. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,
  517. cCode ,cRdCode ,cDepCode ,cHandler ,bTransFlag ,cMaker ,
  518. cDefine1 ,cDefine2 ,cDefine3 ,cDefine4 ,cDefine5 ,cDefine6 ,
  519. cDefine7 ,cDefine8 ,cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,
  520. biafirst ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,cDefine13 ,
  521. cDefine14 ,cDefine15 ,cDefine16 ,cMPoCode ,iproorderid ,
  522. bFromPreYear ,bIsComplement ,iDiscountTaxType ,ireturncount ,
  523. iverifystate ,iswfcontrolled ,cModifyPerson ,dnmaketime ,
  524. dnverifytime ,bredvouch ,iPrintCount )
  525. SELECT @ID,1,'10','','',@cWhCode,convert(datetime,convert(varchar(10),getdate(),120)),
  526. @cCode,@RdCode,@DepCode,@cHandler,0,@cMaker,
  527. a.Define1,a.Define2,a.Define3,a.Define4,a.Define5,a.Define6,
  528. a.Define7,a.Define8,a.Define9,a.Define10,@dDate,0,
  529. 0,@VT_ID,0,a.Define11,a.Define12,a.Define13,
  530. a.Define14,a.Define15,a.Define16,a.MoCode,a.MoId,
  531. 0,0,0,0,
  532. 0,0,'',@dDate,
  533. @dDate,0,0
  534. FROM dbo.mom_order a
  535. WHERE a.MoCode='" + head.MOCode + "'";
  536. cmd.Parameters.Clear();
  537. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  538. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  539. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  540. cmd.Parameters.Add(new SqlParameter("@cDepCode", head.DepCode));
  541. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  542. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  543. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  544. cmd.Parameters.Add(new SqlParameter("@DepCode", head.DepCode));
  545. cmd.Parameters.Add(new SqlParameter("@dDate", dvDate));
  546. cmd.Parameters.Add(new SqlParameter("@RdCode", cRDcode));
  547. cmd.CommandText = sql;
  548. try
  549. {
  550. int count = cmd.ExecuteNonQuery();
  551. if (count <= 0)
  552. {
  553. log.Error("生成成品入库单表头失败,受影响行数<=0;");
  554. throw new Exception("生成成品入库单表头失败,受影响行数<=0;");
  555. }
  556. }
  557. catch (Exception ex)
  558. {
  559. log.Error("生成成品入库单表头失败!SQL:\r\n" + sql, ex);
  560. throw new Exception("生成成品入库单表头失败!SQL:\r\n" + sql, ex);
  561. }
  562. #endregion
  563. if (news != 0)
  564. {
  565. foreach (WH item in LWH)
  566. {
  567. Dictionary<string, int> dic1 = DBHelper.GetAllCode("" + ERPDB + "", surface1, "1", head.WorkPoint,cmd);
  568. int iFatherId1 = Convert.ToInt32(dic1["iFatherId"].ToString());
  569. int iChildId1 = Convert.ToInt32(dic1["iChildId"].ToString());
  570. DateTime date1 = DateTime.Now;
  571. string iBaseCodeLen1 = DBHelper.GetAllRDCode(cardnewcode1, "" + date1 + "", "admin", cRDcode1, head.WorkPoint, cmd);
  572. #region 材料出库单表头
  573. sql = @"INSERT INTO dbo.rdrecord11
  574. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,cBusCode,
  575. cCode ,cRdCode ,cDepCode,cHandler ,bTransFlag ,cMaker ,
  576. dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc
  577. ,cPsPcode ,cMPoCode ,iproorderid ,bFromPreYear ,
  578. bIsLsQuery ,bIsComplement ,iDiscountTaxType ,ireturncount ,iverifystate ,
  579. iswfcontrolled ,dnmaketime ,dnverifytime ,bredvouch ,iPrintCount,
  580. iMQuantity)
  581. SELECT TOP 1 @ID,0,'11','','',@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cBusCode,
  582. @cCode,@cRdCode,a.MDeptCode,@cHandler,0,@cMaker,
  583. CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0,
  584. null,b.MoCode,a.MoId,0,
  585. 0,0,0,0,0,
  586. 0,GETDATE(),GETDATE(),1,1,
  587. null
  588. FROM dbo.mom_orderdetail a
  589. left JOIN dbo.mom_order b ON a.MoId=b.MoId
  590. left join mom_morder c on a.modid=c.modid
  591. WHERE b.MoCode='" + head.MOCode + "'";
  592. cmd.Parameters.Clear();
  593. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId1));
  594. cmd.Parameters.Add(new SqlParameter("@cBusCode", iBaseCodeLen));
  595. cmd.Parameters.Add(new SqlParameter("@cWhCode", item.WHCODE));
  596. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen1));
  597. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  598. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  599. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID1));
  600. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode1));
  601. cmd.CommandText = sql;
  602. try
  603. {
  604. int count = cmd.ExecuteNonQuery();
  605. if (count <= 0)
  606. {
  607. log.Error("生成成品入库单表头失败,受影响行数<=0;");
  608. throw new Exception("生成成品入库单表头失败,受影响行数<=0;");
  609. }
  610. }
  611. catch (Exception ex)
  612. {
  613. log.Error("生成成品入库单表头失败!SQL:\r\n" + sql, ex);
  614. throw new Exception("生成成品入库单表头失败!SQL:\r\n" + sql, ex);
  615. }
  616. item.ID = iFatherId1;
  617. #endregion
  618. }
  619. }
  620. #region 产成品入库单表体
  621. int irow = 0;
  622. foreach (ICSManufactureReceiveDocASKS body in head.details)
  623. {
  624. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  625. {
  626. if (body.BatchCode == "")
  627. {
  628. log.Error("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  629. throw new Exception("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  630. }
  631. }
  632. else
  633. {
  634. if (body.BatchCode != "")
  635. {
  636. log.Error("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  637. throw new Exception("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  638. }
  639. }
  640. irow++;
  641. #region 判断工单行是否关闭,关闭不能入库
  642. sql = @"SELECT ISNULL(a.CloseUser,'') as CloseUser,b.MoCode,a.SortSeq FROM dbo.mom_orderdetail a INNER JOIN
  643. dbo.mom_order b ON b.MoId = a.MoId WHERE a.MoDId = '" + body.MODetailID + "'";
  644. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  645. if (dtCheck != null && dtCheck.Rows.Count > 0)
  646. {
  647. mocode = dtCheck.Rows[0]["MoCode"].ToString();
  648. seq = dtCheck.Rows[0]["SortSeq"].ToString();
  649. if (dtCheck.Rows[0]["CloseUser"].ToString()!="")
  650. {
  651. throw new Exception("工单号:" + dtCheck.Rows[0]["MoCode"].ToString() + ",行号:" + dtCheck.Rows[0]["SortSeq"].ToString() + ",已关闭,无法入库!");
  652. }
  653. sql = @" select
  654. count(c.AllocateID) num,d.number
  655. from DBO.mom_order a
  656. left join DBO.mom_orderdetail b on a.moid = b.moid
  657. left join DBO.mom_moallocate c on b.modid = c.modid
  658. left join(select modid,COUNT(AllocateID) number from DBO.mom_moallocate where WIPType=1 GROUP BY modid
  659. ) d on b.modid = d.modid
  660. where a.MoCode='{0}' and b.SortSeq='{1}'
  661. GROUP BY d.number ";
  662. sql = string.Format(sql, mocode, seq);
  663. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  664. if (dtCheck != null && dtCheck.Rows.Count > 0)
  665. {
  666. if (dtCheck.Rows[0]["num"].ToString() != dtCheck.Rows[0]["number"].ToString())
  667. {
  668. isif = 1;
  669. }
  670. else
  671. {
  672. isif = 0;
  673. }
  674. }
  675. }
  676. #endregion
  677. #region 判断是否为非标
  678. sql = @"select MoClass from dbo.mom_orderdetail where MoDId='" + body.MODetailID + "'";
  679. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  680. if (dtCheck != null && dtCheck.Rows.Count > 0)
  681. {
  682. MoClass = dtCheck.Rows[0]["MoClass"].ToString();
  683. }
  684. #endregion
  685. #region 生成产成品入库单表体
  686. sql = @"INSERT INTO dbo.rdrecords10
  687. ( AutoID ,ID ,cInvCode ,iQuantity ,cBatch ,iFlag ,iNum,
  688. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  689. cItemCode ,cName ,iNQuantity ,cDefine28 ,
  690. cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,cDefine33 ,cDefine34 ,
  691. cDefine35 ,cDefine36 ,cDefine37 ,iMPoIds ,bRelated ,bLPUseFree ,
  692. iRSRowNO ,iOriTrackID ,bCosting ,cmocode ,imoseq ,iExpiratDateCalcu ,
  693. iorderdid ,iordertype,iordercode,iorderseq,isodid,isotype,csocode,isoseq,irowno ,iposflag ,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  694. SELECT @AutoID,@ID,@cInvCode,@iQuantity,@cBatch,0,@iNum,
  695. a.Define22,a.Define23,a.Define24,a.Define25,a.Define26,a.Define27,
  696. @cItemCode,null,a.Qty-isnull(a.QualifiedInQty,0),a.Define28,
  697. a.Define29,a.Define30,a.Define31,a.Define32,a.Define33,a.Define34,
  698. a.Define35,a.Define36,a.Define37,a.MoDId,0,0,
  699. 0,0,1,b.MoCode,a.SortSeq,0,
  700. 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
  701. FROM dbo.mom_orderdetail a
  702. INNER JOIN dbo.mom_order b ON a.MoId=b.MoId WHERE a.MoDId='" + body.MODetailID + "'";
  703. cmd.Parameters.Clear();
  704. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  705. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  706. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  707. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount.ToString("0.00")));
  708. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  709. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  710. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  711. cmd.Parameters.Add(new SqlParameter("@irowno", irow));
  712. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  713. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  714. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  715. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  716. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  717. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  718. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  719. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  720. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  721. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  722. cmd.CommandText = sql;
  723. try
  724. {
  725. int count = cmd.ExecuteNonQuery();
  726. if (count <= 0)
  727. {
  728. log.Error("生成材料出库单表头失败,受影响行数<=0;");
  729. throw new Exception("生成材料出库单表头失败,受影响行数<=0;");
  730. }
  731. }
  732. catch (Exception ex)
  733. {
  734. log.Error("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  735. throw new Exception("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  736. }
  737. #endregion
  738. if (isif == 1)
  739. {
  740. if (head.CompleteVerification)
  741. {
  742. #region 齐套检验
  743. sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' ";
  744. DataTable qt = DBHelper.SQlReturnData(sql, cmd);
  745. if (qt != null && qt.Rows.Count > 0)
  746. {
  747. if (qt.Rows[0]["cValue"].ToString() == "1")
  748. {
  749. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  750. (
  751. select
  752. a.MoCode,b.SortSeq,
  753. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  754. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  755. from DBO.mom_order a
  756. left join DBO.mom_orderdetail b on a.moid = b.moid
  757. left join DBO.mom_moallocate c on b.modid = c.modid
  758. left join
  759. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  760. DBO.mom_moallocatesub d
  761. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  762. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  763. where a.MoCode='{0}' and b.SortSeq='{1}'
  764. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  765. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  766. ) tt
  767. group by MoCode,SortSeq";
  768. sql = string.Format(sql, mocode, seq);
  769. qt = DBHelper.SQlReturnData(sql, cmd);
  770. if (qt == null || qt.Rows.Count == 0)
  771. {
  772. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  773. }
  774. else
  775. {
  776. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  777. {
  778. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料<=0!");
  779. }
  780. }
  781. }
  782. else if (qt.Rows[0]["cValue"].ToString() == "2")
  783. {
  784. sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'";
  785. qt = DBHelper.SQlReturnData(sql, cmd);
  786. if (qt != null && qt.Rows.Count > 0)
  787. {
  788. if (qt.Rows[0]["cValue"].ToString() == "true")
  789. {
  790. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  791. (
  792. select
  793. a.MoCode,b.SortSeq,
  794. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  795. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  796. from DBO.mom_order a
  797. left join DBO.mom_orderdetail b on a.moid = b.moid
  798. left join DBO.mom_moallocate c on b.modid = c.modid
  799. left join
  800. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  801. DBO.mom_moallocatesub d
  802. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  803. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  804. where a.MoCode='{0}' and b.SortSeq='{1}'
  805. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1 and f.bInvKeyPart=1
  806. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  807. ) tt
  808. group by MoCode,SortSeq";
  809. sql = string.Format(sql, mocode, seq);
  810. qt = DBHelper.SQlReturnData(sql, cmd);
  811. if (qt == null || qt.Rows.Count == 0)
  812. {
  813. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  814. }
  815. else
  816. {
  817. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  818. {
  819. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  820. }
  821. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity <= 0)
  822. {
  823. throw new Exception("该物料:" + body.InvCode + @"入库数量大于关键物料领料套数!");
  824. }
  825. }
  826. }
  827. else
  828. {
  829. sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from
  830. (
  831. select
  832. a.MoCode,b.SortSeq,
  833. case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId
  834. end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty
  835. from DBO.mom_order a
  836. left join DBO.mom_orderdetail b on a.moid = b.moid
  837. left join DBO.mom_moallocate c on b.modid = c.modid
  838. left join
  839. (select d.AllocateId,e.InvCode,d.MoallocateSubId from
  840. DBO.mom_moallocatesub d
  841. left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId
  842. left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode
  843. where a.MoCode='{0}' and b.SortSeq='{1}'
  844. and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1
  845. group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty
  846. ) tt
  847. group by MoCode,SortSeq";
  848. sql = string.Format(sql, mocode, seq);
  849. qt = DBHelper.SQlReturnData(sql, cmd);
  850. if (qt == null || qt.Rows.Count == 0)
  851. {
  852. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  853. }
  854. else
  855. {
  856. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  857. {
  858. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  859. }
  860. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity <= 0)
  861. {
  862. throw new Exception("该物料:" + body.InvCode + @"入库数量大于领料套数!");
  863. }
  864. }
  865. }
  866. }
  867. }
  868. }
  869. #endregion
  870. }
  871. }
  872. #region 回写生产订单入库数量
  873. sql = @"UPDATE dbo.mom_orderdetail SET QualifiedInQty=ISNULL(QualifiedInQty,0)+" + body.Quantity + " where MoDId='" + body.MODetailID + "'";
  874. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写生产订单入库数量失败!");
  875. #endregion
  876. if (Convert.ToInt32(MoClass) == 1)
  877. {
  878. #region 判断工单行是否全部入库
  879. sql = "select Qty from mom_moallocate WHERE MoDId='" + body.MODetailID + @"' and IssQty-qty<0 and ProductType=1
  880. union all
  881. select Qty from mom_orderdetail where MoDId='" + body.MODetailID + @"' and QualifiedInQty-Qty<0";
  882. DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  883. if (dtClose.Rows.Count == 0)
  884. {
  885. sql = @"
  886. update mom_orderdetail set CloseTime = GETDATE(),CloseDate= CONVERT(NVARCHAR(15),GETDATE(),23), CloseUser ='" + head.User + "',Status=4 where MODId='" + body.MODetailID + "'";
  887. DBHelper.CmdExecuteNonQuery(sql, cmd, "生产订单行关闭失败!");
  888. }
  889. #endregion
  890. }
  891. else
  892. {
  893. #region 判断工单行是否全部入库
  894. sql = "select Qty from mom_moallocate WHERE MoDId='" + body.MODetailID + @"' and IssQty-qty<0
  895. union all
  896. select Qty from mom_orderdetail where MoDId='" + body.MODetailID + @"' and QualifiedInQty-Qty<0
  897. ";
  898. DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  899. if (dtClose.Rows.Count == 0)
  900. {
  901. sql = @"
  902. update mom_orderdetail set CloseTime = GETDATE(),CloseDate= CONVERT(NVARCHAR(15),GETDATE(),23), CloseUser ='" + head.User + "',Status=4 where MODId='" + body.MODetailID + "'";
  903. DBHelper.CmdExecuteNonQuery(sql, cmd, "生产订单行关闭失败!");
  904. }
  905. #endregion
  906. }
  907. sql = @"select isnull(QualifiedInQty,0)fOutQuantity,isnull(Qty,0)iQuantity,ISNULL(AuxQty, 0)iNum,ISNULL(NiNum, 0)NiNum,ISNULL(AuxQty, 0)-ISNULL(NiNum, 0) SiNum from mom_orderdetail a
  908. LEFT JOIN (select sum(iNum) NiNum,iMPoIds from rdrecords10 GROUP BY iMPoIds) b on a.MoDId=b.iMPoIds
  909. where a.MoDId='" + body.MODetailID + "'";
  910. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  911. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  912. {
  913. foreach (DataRow item in dtDisCheck.Rows)
  914. {
  915. if (Convert.ToDecimal(item["fOutQuantity"].ToString()) == Convert.ToDecimal(item["iQuantity"].ToString()))
  916. {
  917. if (Convert.ToDecimal(item["iNum"].ToString()) != 0)
  918. {
  919. if (Convert.ToDecimal(item["SiNum"].ToString()) != 0)
  920. {
  921. sql = @"Update rdrecords10 set iNum=isnull(iNum,0)+(" + item["SiNum"].ToString() + ") where autoid='" + iChildId + "' ";
  922. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item["SiNum"].ToString()));
  923. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  924. }
  925. }
  926. }
  927. }
  928. }
  929. #region 更新现存量
  930. key.cBustypeUN = "成品入库";
  931. key.cVouchTypeUN = "10";
  932. key.TableName = "IA_ST_UnAccountVouch10";
  933. //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);
  934. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, body.Quantity, body.Amount, body.cFree1,
  935. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  936. "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0,iFatherId, iChildId);
  937. #endregion
  938. num = body.detailss.Count();
  939. Dictionary<string, int> dic2 = DBHelper.GetAllCode("" + ERPDB + "", surface1, "" + num + "", head.WorkPoint,cmd);
  940. int iFatherId2 = Convert.ToInt32(dic2["iFatherId"].ToString());
  941. int iChildId2 = Convert.ToInt32(dic2["iChildId"].ToString());
  942. iFatherIdTree = "";
  943. irownnew = 0;
  944. foreach (ICSManufactureReceiveDocASKSS item in body.detailss)
  945. {
  946. if (item.PickID != null && item.PickID != "")
  947. {
  948. foreach (WH wh in LWH)
  949. {
  950. if (wh.WHCODE == item.WHCode)
  951. {
  952. out11 = wh.ID;
  953. iseq = wh.seq + 1;
  954. wh.seq += 1;
  955. }
  956. }
  957. irownnew++;
  958. sql = @"INSERT INTO dbo.rdrecords11
  959. ( AutoID ,ID ,cInvCode ,iQuantity ,cBatch ,iFlag ,iNum,
  960. cItemCode ,cName ,iNQuantity ,iMPoIds ,bLPUseFree ,
  961. iOriTrackID ,bCosting ,bVMIUsed ,cmocode ,invcode ,imoseq ,
  962. iopseq ,iExpiratDateCalcu ,iorderdid ,iordertype ,isotype ,
  963. ipesodid ,ipesotype ,cpesocode ,ipesoseq ,irowno ,
  964. bcanreplace ,iposflag,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  965. SELECT @AutoID,@ID,a.InvCode,@iQuantity,@cBatch,0,@iNum,
  966. @cItemCode,null,a.Qty,a.AllocateId,0,
  967. 0,1,0,c.MoCode,b.InvCode,b.SortSeq,
  968. '0000',0,0,0,0,
  969. a.AllocateId,'7',c.MoCode,b.SortSeq,@irowno,
  970. 0,null,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  971. FROM dbo.mom_moallocate a
  972. INNER JOIN dbo.mom_orderdetail b ON a.MoDId=b.MoDId
  973. INNER JOIN dbo.mom_order c ON b.MoId=c.MoId
  974. WHERE a.AllocateId='" + item.PickID + "'";
  975. cmd.Parameters.Clear();
  976. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId2));
  977. cmd.Parameters.Add(new SqlParameter("@ID", out11));
  978. cmd.Parameters.Add(new SqlParameter("@iQuantity", item.Quantity));
  979. cmd.Parameters.Add(new SqlParameter("@iNum", item.Amount.ToString("0.00")));
  980. cmd.Parameters.Add(new SqlParameter("@cBatch", item.BatchCode));
  981. cmd.Parameters.Add(new SqlParameter("@cItemCode", item.ProjectCode));
  982. cmd.Parameters.Add(new SqlParameter("@irowno", iseq));
  983. cmd.Parameters.Add(new SqlParameter("@cFree1", item.cFree1));
  984. cmd.Parameters.Add(new SqlParameter("@cFree2", item.cFree2));
  985. cmd.Parameters.Add(new SqlParameter("@cFree3", item.cFree3));
  986. cmd.Parameters.Add(new SqlParameter("@cFree4", item.cFree4));
  987. cmd.Parameters.Add(new SqlParameter("@cFree5", item.cFree5));
  988. cmd.Parameters.Add(new SqlParameter("@cFree6", item.cFree6));
  989. cmd.Parameters.Add(new SqlParameter("@cFree7", item.cFree7));
  990. cmd.Parameters.Add(new SqlParameter("@cFree8", item.cFree8));
  991. cmd.Parameters.Add(new SqlParameter("@cFree9", item.cFree9));
  992. cmd.Parameters.Add(new SqlParameter("@cFree10", item.cFree10));
  993. cmd.CommandText = sql;
  994. try
  995. {
  996. int count = cmd.ExecuteNonQuery();
  997. if (count <= 0)
  998. {
  999. log.Error("生成材料出库单表体失败,受影响行数<=0;");
  1000. throw new Exception("生成材料出库单表体失败,受影响行数<=0;");
  1001. }
  1002. }
  1003. catch (Exception ex)
  1004. {
  1005. log.Error("生成材料出库单表体失败" + sql, ex);
  1006. throw new Exception("生成材料出库单表体失败 " + sql, ex);
  1007. }
  1008. #region 回写工单子件已领数量
  1009. sql = "update mom_moallocate set IssQty=isnull(IssQty,0)+" + item.Quantity + ",RequisitionIssQty=isnull(RequisitionIssQty,0)+(" + item.Quantity + ") where AllocateId='" + item.PickID + "' ";
  1010. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写工单子件已领数量失败!");
  1011. #endregion
  1012. sql = @"select isnull(Issqty,0)fOutQuantity,isnull(qty,0)iQuantity,ISNULL(AuxQty, 0)iNum,ISNULL(NiNum, 0)NiNum,ISNULL(AuxQty, 0)-ISNULL(NiNum, 0) SiNum from mom_moallocate a
  1013. LEFT JOIN (select sum(iNum) NiNum,iMPoIds from rdrecords11 GROUP BY iMPoIds) b on a.AllocateId=b.iMPoIds
  1014. where a.AllocateId='" + item.PickID + "'";
  1015. dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  1016. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  1017. {
  1018. foreach (DataRow item1 in dtDisCheck.Rows)
  1019. {
  1020. if (Convert.ToDecimal(item1["fOutQuantity"].ToString()) == Convert.ToDecimal(item1["iQuantity"].ToString()))
  1021. {
  1022. if (Convert.ToDecimal(item1["iNum"].ToString()) != 0)
  1023. {
  1024. if (Convert.ToDecimal(item1["SiNum"].ToString()) != 0)
  1025. {
  1026. sql = @"Update rdrecords11 set iNum=isnull(iNum,0)+(" + item1["SiNum"].ToString() + ") where autoid='" + iChildId2 + "' ";
  1027. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item1["SiNum"].ToString()));
  1028. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  1029. }
  1030. }
  1031. }
  1032. }
  1033. }
  1034. //更新现存量
  1035. key.cBustypeUN = "领料";
  1036. key.cVouchTypeUN = "11";
  1037. key.TableName = "IA_ST_UnAccountVouch11";
  1038. //DBHelper.UpdateCurrentStockNEW(cmd, item.InvCode, item.WHCode, item.BatchCode, -item.Quantity, item.cFree1, item.cFree2, item.cFree3, item.cFree4, item.cFree5, item.cFree6, item.cFree7, item.cFree8, item.cFree9, item.cFree10, key);
  1039. DBHelper.UpdateCurrentStockCCGC(cmd, item.InvCode, item.WHCode, item.BatchCode, -item.Quantity, -item.Amount, item.cFree1,
  1040. item.cFree2, item.cFree3, item.cFree4, item.cFree5, item.cFree6, item.cFree7, item.cFree8, item.cFree9, item.cFree10,
  1041. "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, out11, iChildId2);
  1042. iFatherIdTree += "'" + out11 + "',";
  1043. iChildId2--;
  1044. }
  1045. }
  1046. iFatherIdTwo = "'" + iFatherId + "',";
  1047. iChildId--;
  1048. }
  1049. #endregion
  1050. #region 查询
  1051. if (news == 0)
  1052. {
  1053. sql = @" select top 1 a.ID as ID,a.ID as IDs,a.cCode as MRCVCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName
  1054. ,a.cMPoCode as MOCode ,a.cMaker as CreateUser ,a.dnmaketime as CreateDateTime
  1055. ,a.cHandler as Checker ,a.dnverifytime as CheckDateTime
  1056. from rdrecord10 a
  1057. left join Department c on a.cDepCode=c.cDepCode
  1058. left join Warehouse d on a.cWhCode=d.cWhCode WHERE A.cHandler IS NOT NULL AND A.ID in({0})
  1059. select DISTINCT a.ID as IDs, b.AutoID as DetailID,CAST(b.iMPoIds as char) as IDSS ,b.irowno as Sequence ,b.cInvCode as InvCode ,
  1060. b.iQuantity as Quantity ,b.iNum as Amount,b.iMPoIds as MODetailID,
  1061. isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  1062. isnull(b.cFree1,'') as cFree1,
  1063. isnull(b.cFree2,'') as cFree2,
  1064. isnull(b.cFree3,'') as cFree3,
  1065. isnull(b.cFree4,'') as cFree4,
  1066. isnull(b.cFree5,'') as cFree5,
  1067. isnull(b.cFree6,'') as cFree6,
  1068. isnull(b.cFree7,'') as cFree7,
  1069. isnull(b.cFree8,'') as cFree8,
  1070. isnull(b.cFree9,'') as cFree9,
  1071. isnull(b.cFree10,'') as cFree10
  1072. from rdrecord10 a
  1073. left join rdrecords10 b on a.ID=b.ID
  1074. left join Department c on a.cDepCode=c.cDepCode
  1075. left join Warehouse d on a.cWhCode=d.cWhCode WHERE A.cHandler IS NOT NULL AND A.ID in({0})
  1076. ";
  1077. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  1078. }
  1079. else
  1080. {
  1081. sql = @" select top 1 a.ID as ID,a.ID as IDs,a.cCode as MRCVCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName
  1082. ,a.cMPoCode as MOCode ,a.cMaker as CreateUser ,a.dnmaketime as CreateDateTime
  1083. ,a.cHandler as Checker ,a.dnverifytime as CheckDateTime
  1084. from rdrecord10 a
  1085. left join Department c on a.cDepCode=c.cDepCode
  1086. left join Warehouse d on a.cWhCode=d.cWhCode WHERE A.cHandler IS NOT NULL AND A.ID in({0})
  1087. select DISTINCT a.ID as IDs, b.AutoID as DetailID,CAST(b.iMPoIds as char) as IDSS ,b.irowno as Sequence ,b.cInvCode as InvCode ,
  1088. b.iQuantity as Quantity ,b.iNum as Amount,b.iMPoIds as MODetailID ,
  1089. isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  1090. isnull(b.cFree1,'') as cFree1,
  1091. isnull(b.cFree2,'') as cFree2,
  1092. isnull(b.cFree3,'') as cFree3,
  1093. isnull(b.cFree4,'') as cFree4,
  1094. isnull(b.cFree5,'') as cFree5,
  1095. isnull(b.cFree6,'') as cFree6,
  1096. isnull(b.cFree7,'') as cFree7,
  1097. isnull(b.cFree8,'') as cFree8,
  1098. isnull(b.cFree9,'') as cFree9,
  1099. isnull(b.cFree10,'') as cFree10
  1100. from rdrecord10 a
  1101. left join rdrecords10 b on a.ID=b.ID
  1102. left join Department c on a.cDepCode=c.cDepCode
  1103. left join Warehouse d on a.cWhCode=d.cWhCode WHERE A.cHandler IS NOT NULL AND A.ID in({0})
  1104. ";
  1105. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  1106. }
  1107. if (ds != null)
  1108. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  1109. else
  1110. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  1111. #endregion
  1112. }
  1113. #endregion
  1114. cmd.Transaction.Commit();
  1115. }
  1116. catch (Exception ex)
  1117. {
  1118. if (cmd.Transaction != null)
  1119. cmd.Transaction.Rollback();
  1120. log.Error(ex.Message);
  1121. throw new Exception(ex.Message);
  1122. }
  1123. finally
  1124. {
  1125. if (conn.State == ConnectionState.Open)
  1126. {
  1127. conn.Close();
  1128. }
  1129. conn.Dispose();
  1130. }
  1131. }
  1132. msg = JSON.DataSetToJson(ds, "details", "IDs");
  1133. return msg;
  1134. }
  1135. /// <summary>
  1136. ///
  1137. /// 记录日志
  1138. /// </summary>
  1139. /// <param name="Bills"></param>
  1140. private void LogInfo(List<ICSManufactureReceiveDocASK> Bills)
  1141. {
  1142. string HeadList = string.Empty;
  1143. string BodyList = string.Empty;
  1144. foreach (ICSManufactureReceiveDocASK head in Bills)
  1145. {
  1146. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  1147. foreach (ICSManufactureReceiveDocASKS body in head.details)
  1148. {
  1149. BodyList += "\r\n 表体主键ID: " + body.MODetailID + ",数量:" + body.Quantity;
  1150. }
  1151. }
  1152. log.Info(HeadList);
  1153. log.Info(BodyList);
  1154. }
  1155. }
  1156. }
  1157. //select DISTINCT a.ID as IDs,b.AutoID as DetailID,CAST(f.MoDId as char) as IDSS,a.cCode as MRCVCode,irowno as Sequence,b.cInvCode as InvCode
  1158. // ,iQuantity as Quantity,e.AllocateId as SourceDetailID,iNum as Amount,
  1159. // isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  1160. // isnull(b.cFree1,'') as cFree1,
  1161. // isnull(b.cFree2,'') as cFree2,
  1162. // isnull(b.cFree3,'') as cFree3,
  1163. // isnull(b.cFree4,'') as cFree4,
  1164. // isnull(b.cFree5,'') as cFree5,
  1165. // isnull(b.cFree6,'') as cFree6,
  1166. // isnull(b.cFree7,'') as cFree7,
  1167. // isnull(b.cFree8,'') as cFree8,
  1168. // isnull(b.cFree9,'') as cFree9,
  1169. // isnull(b.cFree10,'') as cFree10
  1170. // from rdrecord11 a
  1171. // left join rdrecords11 b on a.ID=b.ID
  1172. // left join Department c on a.cDepCode=c.cDepCode
  1173. // left join Warehouse d on a.cWhCode=d.cWhCode
  1174. // left join mom_moallocate e on b.iMPoIds=e.AllocateId
  1175. // INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId
  1176. // INNER JOIN dbo.mom_order g ON f.MoId=g.MoId WHERE a.ID in ({1})