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

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