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

854 lines
47 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. using ICSSoft.Common;
  2. using ICSSoft.Entity;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ICSSoft.DataProject
  12. {
  13. /// <summary>
  14. /// 委外退料
  15. /// </summary>
  16. public class OutsourcingIssueDoNegative
  17. {
  18. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  19. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  20. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  21. private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"];
  22. private static string CRNALL = System.Configuration.ConfigurationManager.AppSettings["OutIssCRDNAME"];
  23. public string Get(List<ICSOutsourcingIssueDoc> infos)
  24. {
  25. List<ICSOutsourcingIssueDoc> szJson = new List<ICSOutsourcingIssueDoc>();
  26. DataTable dt = null;
  27. DataTable dtNew = null;
  28. string connS = "";
  29. string json = "";
  30. if (infos.Count <= 0)
  31. {
  32. throw new Exception("传送数据为空!");
  33. }
  34. string res = string.Empty;
  35. SqlConnection conn = new SqlConnection();
  36. SqlCommand cmd = new SqlCommand();
  37. string sql = string.Empty;
  38. foreach (ICSOutsourcingIssueDoc info in infos)
  39. {
  40. try
  41. {
  42. connS = string.Format(connString, info.WorkPoint);
  43. conn = new System.Data.SqlClient.SqlConnection(connS);
  44. conn.Open();
  45. SqlTransaction sqlTran = conn.BeginTransaction();
  46. cmd = new SqlCommand();
  47. cmd.Transaction = sqlTran;
  48. cmd.Connection = conn;
  49. if (info.MTime < new DateTime(2000, 01, 01))
  50. throw new Exception("请输入正确的操作时间:" + info.MTime);
  51. sql = @" select a.ID,iMPoIds,iordercode,a.cCode,a.cDepCode,c.cDepName,a.cWhCode,
  52. d.cWhName,a.cSource,a.cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,
  53. b.AutoID ,b.irowno,b.cInvCode,b.iQuantity,e.MoDetailsID,b.iNum,e.csocode
  54. from rdrecord11 a
  55. left join rdrecords11 b on a.ID=b.ID
  56. left join Department c on a.cDepCode=c.cDepCode
  57. left join Warehouse d on a.cWhCode=d.cWhCode
  58. left join OM_MODetails e on b.iOMoDID=e.MoDetailsID
  59. where a.cBusType=''and b.iQuantity<0";
  60. if (!string.IsNullOrWhiteSpace(info.IssueCode))
  61. {
  62. sql += " and a.cCode='{0}'";
  63. }
  64. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  65. {
  66. sql += " and ISNULL(a.dnmodifytime,ISNULL(a.dnverifytime, ISNULL(a.dnmodifytime, a.dnmaketime)))>='{1}'";
  67. }
  68. if (!string.IsNullOrWhiteSpace(info.User))
  69. {
  70. sql += "and a.CMAKER='{2}'";
  71. }
  72. sql = string.Format(sql, info.IssueCode, info.MTime, info.User);
  73. dt = DBHelper.SQlReturnData(sql, cmd);
  74. if (dt.Rows.Count <= 0 || dt == null)
  75. throw new Exception("委外发料单:" + info.IssueCode + ",无信息!");
  76. if (dtNew == null)
  77. dtNew = dt;
  78. else
  79. dtNew.Merge(dt);
  80. cmd.Transaction.Commit();
  81. }
  82. catch (Exception ex)
  83. {
  84. cmd.Transaction.Rollback();
  85. log.Error(ex.Message);
  86. throw new Exception(ex.Message);
  87. }
  88. finally
  89. {
  90. if (conn.State == ConnectionState.Open)
  91. {
  92. conn.Close();
  93. }
  94. conn.Dispose();
  95. }
  96. }
  97. json = JsonConvert.SerializeObject(dtNew);
  98. return json;
  99. }
  100. public string CreateOutsourcingIssueDoNegative(List<ICSOutsourcingIssueDoc> Bills)
  101. {
  102. string msg = "";
  103. string sql = string.Empty;
  104. DataTable dt = null;
  105. DataTable dtNew1 = null;
  106. DataTable dtNew2 = null;
  107. string connS = "";
  108. int num = 0;
  109. //取得out库单的默认显示模版
  110. //取得采购入库单单据 表头ID,表体DID
  111. VouchKey key = new VouchKey();
  112. SqlConnection conn = new SqlConnection();
  113. SqlCommand cmd = new SqlCommand();
  114. if (Bills.Count <= 0)
  115. {
  116. throw new Exception("传送数据为空!");
  117. }
  118. LogInfo(Bills);
  119. foreach (ICSOutsourcingIssueDoc head in Bills)
  120. {
  121. try
  122. {
  123. string mocode = "";
  124. string seq = "";
  125. string iFatherIdTwo = "";
  126. connS = string.Format(connString, head.WorkPoint);
  127. conn = new System.Data.SqlClient.SqlConnection(connS);
  128. conn.Open();
  129. SqlTransaction sqlTran = conn.BeginTransaction();
  130. cmd = new SqlCommand();
  131. cmd.Transaction = sqlTran;
  132. cmd.Connection = conn;
  133. string[] ss = head.WorkPoint.Split('_');
  134. ERPDB = ss[1];
  135. if (DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  136. {
  137. }
  138. else
  139. {
  140. throw new Exception("U8正在整理现存量,请稍后再试");
  141. }
  142. if (head.details.Count <= 0)
  143. {
  144. throw new Exception("表体信息不存在!");
  145. }
  146. if (head.SourceType=="委外领料申请单")
  147. {
  148. #region 判断物料是否超额领取
  149. foreach (ICSOutsourcingIssueDocs body in head.details)
  150. {
  151. sql = @"SELECT isnull(fOutQuantity,0) as fOutQuantity,iQuantity FROM dbo.MaterialAppVouchs WHERE AutoID='" + body.SourceDetailID + "'";
  152. DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  153. if (dtQty != null && dtQty.Rows.Count > 0)
  154. {
  155. //已领数量
  156. decimal IssQty = Convert.ToDecimal(dtQty.Rows[0]["fOutQuantity"]);
  157. //应领数量
  158. decimal Qty = Convert.ToDecimal(dtQty.Rows[0]["iQuantity"]);
  159. if (IssQty - body.Quantity < 0)
  160. {
  161. throw new Exception("物料领取小于当前可领用最大数,单据号:" + head.SourceCode+",行号:"+body.Sequence+"");
  162. }
  163. }
  164. else
  165. {
  166. throw new Exception("领料申请单不存在!单据号:" + head.SourceCode);
  167. }
  168. }
  169. #endregion
  170. #region 现存量是否够领
  171. // //if (head.IsReturn != "1")
  172. // //{
  173. // foreach (rdrecords11 body in head.rdcords)
  174. // {
  175. // sql = string.Format(@"SELECT iQuantity FROM dbo.CurrentStock
  176. // WHERE cInvCode='{0}' AND cWhCode='{1}'", body.cInvCode, head.cWhCode);
  177. // if (!string.IsNullOrWhiteSpace(body.cBatch))
  178. // sql += " and cBatch='" + body.cBatch + "'";
  179. // DataTable dt = DBHelper.SQlReturnData(sql, cmd);
  180. // decimal qtyCurrent = 0;
  181. // if (dt != null && dt.Rows.Count > 0)
  182. // {
  183. // qtyCurrent = Convert.ToDecimal(dt.Rows[0][0]);
  184. // if (qtyCurrent < body.Quantity)
  185. // {
  186. // throw new Exception("物料编号:" + body.cInvCode + ",现存量不足!");
  187. // }
  188. // }
  189. // else
  190. // {
  191. // throw new Exception("该物料:" + body.cInvCode + "在U8中仓库:" + head.cWhCode + "中不存在!");
  192. // }
  193. // }
  194. // //}
  195. #endregion
  196. }
  197. else
  198. {
  199. #region 判断委外订单是否已关闭
  200. sql = @"SELECT distinct isnull(b.cbCloser,'') as CloseUser
  201. from dbo.OM_MOMaterials a
  202. INNER JOIN OM_MODetails b ON a.MoDetailsID = b.MoDetailsID
  203. where a.MOMaterialsID='" + head.details[0].SourceDetailID + "'";
  204. DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  205. if (dtClose != null && dtClose.Rows.Count > 0)
  206. {
  207. if (!string.IsNullOrEmpty(dtClose.Rows[0][0].ToString()))
  208. {
  209. throw new Exception("该委外订单行已关闭,无法领料!");
  210. }
  211. }
  212. #endregion
  213. #region 判断物料是否超额领取
  214. foreach (ICSOutsourcingIssueDocs body in head.details)
  215. {
  216. sql = @"SELECT isnull(b.fOutExcess,0) as fOutExcess,isnull(a.iSendQTY,0) as IssQty,a.iQuantity
  217. FROM dbo.OM_MOMaterials a
  218. LEFT JOIN dbo.Inventory b ON a.cInvCode=b.cInvCode
  219. WHERE a.MOMaterialsID='" + body.SourceDetailID + "'";
  220. DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  221. if (dtQty != null && dtQty.Rows.Count > 0)
  222. {
  223. //可超领比率
  224. decimal fOutExcess = Convert.ToDecimal(dtQty.Rows[0]["fOutExcess"]);
  225. //已领数量
  226. decimal IssQty = Convert.ToDecimal(dtQty.Rows[0]["IssQty"]);
  227. //应领数量
  228. decimal Qty = Convert.ToDecimal(dtQty.Rows[0]["iQuantity"]);
  229. //当前可总领数量
  230. decimal TotalQty = Qty + fOutExcess * Qty;
  231. //if (head.IsReturn != "1")
  232. //{
  233. if (IssQty - body.Quantity <0)
  234. {
  235. throw new Exception("物料退料数量超过当前已领用数量,AllocateId:" + body.SourceDetailID);
  236. }
  237. //}
  238. //else
  239. //{
  240. //if (IssQty - body.Quantity < 0)
  241. //{
  242. // throw new Exception("物料退料超过当前已领用最大数,AllocateId:" + body.SourceDetailID);
  243. //}
  244. //}
  245. }
  246. else
  247. {
  248. throw new Exception("委外订单行子件不存在!AllocateId:" + body.SourceDetailID);
  249. }
  250. }
  251. #endregion
  252. }
  253. #region 委外退料表头
  254. num = head.details.Count();
  255. string[] dd = CRNALL.Split('~');
  256. string crdname = dd[0];
  257. string carname = dd[1];
  258. string surface = dd[2];
  259. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  260. //取得out库单的默认显示模版
  261. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  262. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  263. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  264. DateTime date = DateTime.Now;
  265. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", head.WorkPoint);
  266. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  267. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  268. if (head.SourceType != "委外领料申请单")
  269. {
  270. sql = @"
  271. INSERT INTO dbo.rdrecord11
  272. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,
  273. cWhCode ,dDate ,cCode ,cRdCode ,cDepCode,
  274. cPersonCode,cHandler ,bTransFlag ,cMaker ,dVeriDate ,
  275. bpufirst ,biafirst ,VT_ID ,bIsSTQc ,cPsPcode ,
  276. cMPoCode ,iproorderid ,bFromPreYear ,bIsLsQuery ,bIsComplement ,
  277. iDiscountTaxType ,ireturncount ,iverifystate ,iswfcontrolled ,dnmaketime ,
  278. dnverifytime ,bredvouch ,iPrintCount,cVenCode,iMQuantity)
  279. SELECT distinct @ID,0,'11','','',
  280. @cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,c.cDepCode,
  281. c.cPersonCode,@cHandler,0,@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),
  282. 0,0,@VT_ID,0,null,
  283. c.cCode,a.MoId,0,0,0,
  284. 0,0,0,0,GETDATE(),
  285. GETDATE(),@bredvouch,0,c.cVenCode,null
  286. from dbo.OM_MOMaterials a
  287. INNER JOIN dbo.OM_MODetails b ON a.MoDetailsID = b.MoDetailsID
  288. LEFT JOIN dbo.OM_MOMain c ON b.MOID = c.MOID
  289. WHERE c.cCode='" + head.SourceCode + "'";
  290. cmd.Parameters.Clear();
  291. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  292. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  293. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  294. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  295. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  296. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  297. cmd.Parameters.Add(new SqlParameter("@bredvouch", "0"));
  298. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  299. cmd.CommandText = sql;
  300. }
  301. else
  302. {
  303. sql = @"INSERT INTO dbo.rdrecord11
  304. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cBusCode ,cWhCode ,cMPoCode,
  305. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cHandler ,cMemo ,
  306. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,cDefine4 ,
  307. cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,cDefine9 ,cDefine10 ,
  308. dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc ,cDefine11 ,
  309. cDefine12 ,cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  310. bOMFirst ,bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  311. ireturncount ,iverifystate ,iswfcontrolled ,cModifyPerson ,
  312. dnmaketime ,dnverifytime ,bredvouch ,bmotran ,bHYVouch ,iPrintCount
  313. )
  314. SELECT distinct @ID,0,'11','','',a.cCode,@cWhCode,comcode,
  315. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,a.cDepCode,a.cPersonCode,@cHandler,a.cMemo,
  316. 0,@cMaker,a.cDefine1,cdefine2,a.cDefine3,a.cDefine4,
  317. a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,a.cDefine9,a.cDefine10,
  318. CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0,a.cDefine11,
  319. a.cDefine12,a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  320. 0,0,0,0,
  321. 0,0,0,'',
  322. GETDATE(),GETDATE(),0,0,0,0
  323. FROM dbo.MaterialAppVouch a
  324. LEFT JOIN dbo.MaterialAppVouchs B ON A.ID=B.ID
  325. WHERE a.cCode='" + head.SourceCode + "'";
  326. cmd.Parameters.Clear();
  327. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  328. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  329. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  330. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  331. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  332. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  333. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  334. //DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表头失败!");
  335. cmd.CommandText = sql;
  336. }
  337. try
  338. {
  339. int count = cmd.ExecuteNonQuery();
  340. if (count <= 0)
  341. {
  342. log.Error("生成材料出库单表头失败,受影响行数<=0;");
  343. throw new Exception("生成材料出库单表头失败,受影响行数<=0;");
  344. }
  345. }
  346. catch (Exception ex)
  347. {
  348. log.Error("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  349. throw new Exception("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  350. }
  351. #endregion
  352. #region 委外退料表体
  353. string iNQuantity = "";
  354. int irowno = 0;
  355. foreach (ICSOutsourcingIssueDocs body in head.details)
  356. {
  357. sql = @"SELECT b.cCode,a.iVouchRowNo FROM dbo.OM_MODetails a INNER JOIN
  358. dbo.OM_MOMain b ON b.MoId = a.MoId WHERE a.MODetailsID =(select a.MODetailsID from OM_MODetails a left join
  359. OM_MOMaterials b on a.MoDetailsID=b.MODetailsID where b.MOMaterialsID='" + body.SourceDetailID + @"'
  360. )";
  361. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  362. if (dtCheck != null && dtCheck.Rows.Count > 0)
  363. {
  364. mocode = dtCheck.Rows[0]["cCode"].ToString();
  365. seq = dtCheck.Rows[0]["iVouchRowNo"].ToString();
  366. }
  367. irowno++;
  368. body.Quantity = -body.Quantity;
  369. iNQuantity = "-a.iQuantity";
  370. if (head.SourceType != "委外领料申请单")
  371. {
  372. sql = @"INSERT INTO dbo.rdrecords11
  373. ( AutoID,ID ,cInvCode,iQuantity ,cBatch ,iNum,
  374. iFlag ,cItemCode ,cName ,iNQuantity ,iMPoIds ,
  375. bLPUseFree ,iOriTrackID ,bCosting ,bVMIUsed ,cmocode ,
  376. invcode ,imoseq ,iopseq ,iExpiratDateCalcu ,iorderdid ,
  377. iordertype ,isotype ,ipesodid ,ipesotype ,cpesocode ,
  378. ipesoseq ,irowno ,bcanreplace ,iposflag,iOMoDID,
  379. iOMoMID,comcode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  380. SELECT distinct
  381. @AutoID,@ID,a.cInvCode,@iQuantity,@cBatch,@iNum,
  382. 0,null,null,@iQuantity,NULL,
  383. 0,0,1,0,NULL,
  384. b.cInvCode,NULL,NULL,0,0,
  385. 0,0,a.MOMaterialsID,'6',c.cCode,
  386. NULL, @irowno,0,NULL,b.MODetailsID,
  387. a.MOMaterialsID,c.cCode,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  388. FROM dbo.OM_MOMaterials a
  389. INNER JOIN dbo.OM_MODetails b ON a.MoDetailsID=b.MODetailsID
  390. INNER JOIN dbo.OM_MOMain c ON b.MOID=c.MOID
  391. WHERE a.MOMaterialsID='" + body.SourceDetailID + "'";
  392. cmd.Parameters.Clear();
  393. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  394. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  395. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  396. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount));
  397. //cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  398. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  399. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  400. cmd.Parameters.Add(new SqlParameter("@iNQuantity", iNQuantity));
  401. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  402. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  403. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  404. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  405. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  406. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  407. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  408. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  409. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  410. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  411. cmd.CommandText = sql;
  412. }
  413. else
  414. {
  415. sql = @"INSERT INTO dbo.rdrecords11
  416. ( AutoID ,ID ,cInvCode ,iQuantity ,iFlag ,cDefine22 ,iOMoDID,iOMoMID,iNum,
  417. cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  418. cItem_class ,cItemCode ,cName ,cItemCName ,iNQuantity ,
  419. cDefine28 ,cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,
  420. cDefine33 ,cDefine34 ,cDefine35 ,cDefine36 ,cDefine37 ,
  421. bLPUseFree ,iOriTrackID ,bCosting ,bVMIUsed ,iMaIDs ,
  422. iExpiratDateCalcu ,iordertype ,isotype ,ipesotype ,irowno,
  423. cBatch,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  424. SELECT @AutoID,@ID,a.cInvCode,@iQuantity,0,a.cDefine22,a.iOMoDID,a.iOMoMID,@iNum,
  425. a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  426. a.cItem_class,a.cItemCode,a.cName,a.cItemCName,@iQuantity,
  427. a.cDefine28,a.cDefine29,a.cDefine30,a.cDefine31,a.cDefine32,
  428. a.cDefine33,a.cDefine34,a.cDefine35,a.cDefine36,a.cDefine37,
  429. 0,0,1,0,a.AutoID,
  430. 0,0,0,0,@irowno,
  431. @cBatch,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  432. FROM dbo.MaterialAppVouchs a WHERE a.AutoID='" + body.SourceDetailID + "'";
  433. cmd.Parameters.Clear();
  434. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  435. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  436. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  437. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount));
  438. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  439. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  440. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  441. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  442. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  443. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  444. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  445. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  446. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  447. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  448. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  449. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  450. //DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表头失败!");
  451. cmd.CommandText = sql;
  452. }
  453. try
  454. {
  455. int count = cmd.ExecuteNonQuery();
  456. if (count <= 0)
  457. {
  458. log.Error("生产退料单表体失败,受影响行数<=0;");
  459. throw new Exception("生产退料单表体失败,受影响行数<=0;");
  460. }
  461. }
  462. catch (Exception ex)
  463. {
  464. log.Error("生产退料单表体失败" + sql, ex);
  465. throw new Exception("生产退料单表体失败 " + sql, ex);
  466. }
  467. if (head.CompleteVerification)
  468. {
  469. #region 齐套检验
  470. sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' ";
  471. DataTable qt = DBHelper.SQlReturnData(sql, cmd);
  472. if (qt != null && qt.Rows.Count > 0)
  473. {
  474. if (qt.Rows[0]["cValue"].ToString() == "1")
  475. {
  476. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  477. (
  478. select
  479. a.cCode,b.iVouchRowNo,
  480. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  481. from DBO.OM_MOMain a
  482. left join DBO.OM_MODetails b on a.moid = b.moid
  483. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  484. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  485. where a.cCode='{0}' and b.iVouchRowNo='{1}'
  486. and c.iQuantity<>0
  487. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  488. ) tt
  489. group by cCode,iVouchRowNo";
  490. sql = string.Format(sql, mocode, seq);
  491. qt = DBHelper.SQlReturnData(sql, cmd);
  492. if (qt == null || qt.Rows.Count == 0)
  493. {
  494. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  495. }
  496. else
  497. {
  498. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  499. {
  500. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料<=0!");
  501. }
  502. }
  503. }
  504. else if (qt.Rows[0]["cValue"].ToString() == "2")
  505. {
  506. sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'";
  507. qt = DBHelper.SQlReturnData(sql, cmd);
  508. if (qt != null && qt.Rows.Count > 0)
  509. {
  510. if (qt.Rows[0]["cValue"].ToString() == "true")
  511. {
  512. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  513. (
  514. select
  515. a.cCode,b.iVouchRowNo,
  516. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  517. from DBO.OM_MOMain a
  518. left join DBO.OM_MODetails b on a.moid = b.moid
  519. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  520. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  521. where a.cCode='{0}' and b.iVouchRowNo='{1}'
  522. and c.iQuantity<>0
  523. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  524. ) tt
  525. group by cCode,iVouchRowNo";
  526. sql = string.Format(sql, mocode, seq);
  527. qt = DBHelper.SQlReturnData(sql, cmd);
  528. if (qt == null || qt.Rows.Count == 0)
  529. {
  530. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  531. }
  532. else
  533. {
  534. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  535. {
  536. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  537. }
  538. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity <= 0)
  539. {
  540. throw new Exception("该物料:" + body.InvCode + @"入库数量大于关键物料领料套数!");
  541. }
  542. }
  543. }
  544. else
  545. {
  546. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  547. (
  548. select
  549. a.cCode,b.iVouchRowNo,
  550. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  551. from DBO.OM_MOMain a
  552. left join DBO.OM_MODetails b on a.moid = b.moid
  553. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  554. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  555. where a.cCode='{0}' and b.iVouchRowNo='{1}'
  556. and c.iQuantity<>0
  557. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  558. ) tt
  559. group by cCode,iVouchRowNo";
  560. sql = string.Format(sql, mocode, seq);
  561. qt = DBHelper.SQlReturnData(sql, cmd);
  562. if (qt == null || qt.Rows.Count == 0)
  563. {
  564. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  565. }
  566. else
  567. {
  568. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  569. {
  570. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  571. }
  572. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity <= 0)
  573. {
  574. throw new Exception("该物料:" + body.InvCode + @"入库数量大于领料套数!");
  575. }
  576. }
  577. }
  578. }
  579. }
  580. }
  581. #endregion
  582. }
  583. if (head.UpdateStock)
  584. {
  585. //更新现存量
  586. key.cBustypeUN = "委外发料";
  587. key.cVouchTypeUN = "11";
  588. key.TableName = "IA_ST_UnAccountVouch11";
  589. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key);
  590. 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);
  591. }
  592. if (head.SourceType != "委外领料申请单")
  593. {
  594. #region 回写工单子件已领数量
  595. sql = "update OM_MOMaterials set iSendQTY=isnull(iSendQTY,0)+" + body.Quantity + ",fsendapplyqty=isnull(fsendapplyqty,0)+" + body.Quantity + @" where MOMaterialsID='" + body.SourceDetailID + "' ";
  596. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写委外订单子件已领数量失败!");
  597. #endregion
  598. }
  599. else
  600. {
  601. #region 回写工单子件已领数量
  602. sql = "update OM_MOMaterials set iSendQTY=isnull(iSendQTY,0)+" + body.Quantity + @",fsendapplyqty=isnull(fsendapplyqty,0)+" + body.Quantity + @" where MOMaterialsID=(select iOMoMID from MaterialAppVouchs where AutoID='" + body.SourceDetailID + @"')
  603. update MaterialAppVouchs set fOutQuantity=isnull(fOutQuantity,0)+" + body.Quantity + @" where AutoID='" + body.SourceDetailID + @"'
  604. ";
  605. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写委外订单子件已领数量失败!");
  606. #endregion
  607. }
  608. iFatherIdTwo += "'" + iFatherId + "',";
  609. iChildId--;
  610. }
  611. #endregion
  612. #region 查询
  613. sql = @"select a.ID as ID,a.ID as IDs,a.cCode as IssueNEGCode,a.cDepCode,c.cDepName,a.cWhCode,
  614. d.cWhName,a.cSource,cMPoCode,a.cMaker,dnmaketime,cHandler,dnverifytime
  615. from rdrecord11 a
  616. left join Department c on a.cDepCode=c.cDepCode
  617. left join Warehouse d on a.cWhCode=d.cWhCode
  618. where a.cBusType='' and a.ID in ({0})
  619. select a.ID as IDs,(case when isnull(f.AutoID,'')='' then h.MOMaterialsID else f.AutoID end) as SourceDetailID,b.irowno as Sequence ,b.iordercode,b.cInvCode,b.iQuantity,b.iNum,b.AutoID as DetailID
  620. from rdrecord11 a
  621. left join rdrecords11 b on a.ID=b.ID
  622. left join Department c on a.cDepCode=c.cDepCode
  623. left join Warehouse d on a.cWhCode=d.cWhCode
  624. left join OM_MOMaterials h on b.iOMoMID=h.MOMaterialsID
  625. left join MaterialAppVouchs f on b.iMaIDs=f.autoid
  626. where a.cBusType='' and a.ID in ({0})";
  627. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  628. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  629. if (dtNew1 == null)
  630. dtNew1 = ds.Tables[0];
  631. else
  632. dtNew1.Merge(ds.Tables[0]);
  633. if (dtNew2 == null)
  634. dtNew2 = ds.Tables[1];
  635. else
  636. dtNew2.Merge(ds.Tables[1]);
  637. // sql = @" select a.ID as ID,iMPoIds,iordercode,a.cCode,a.cDepCode,c.cDepName,a.cWhCode,
  638. // d.cWhName,a.cSource,a.cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,
  639. // b.AutoID ,b.irowno,b.cInvCode,b.iQuantity,e.MoDetailsID,b.iNum,e.csocode
  640. // from rdrecord11 a
  641. // left join rdrecords11 b on a.ID=b.ID
  642. // left join Department c on a.cDepCode=c.cDepCode
  643. // left join Warehouse d on a.cWhCode=d.cWhCode
  644. // left join OM_MODetails e on b.iOMoDID=e.MoDetailsID
  645. // where a.cBusType='委外发料'and b.iQuantity<0 and a.ID in ({0})";
  646. // sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  647. // dt = DBHelper.SQlReturnData(sql, cmd);
  648. #endregion
  649. cmd.Transaction.Commit();
  650. }
  651. catch (Exception ex)
  652. {
  653. cmd.Transaction.Rollback();
  654. log.Error(ex.Message);
  655. throw new Exception(ex.Message);
  656. }
  657. finally
  658. {
  659. if (conn.State == ConnectionState.Open)
  660. {
  661. conn.Close();
  662. }
  663. conn.Dispose();
  664. }
  665. }
  666. string RelationName = "details";
  667. DataSet dz = new DataSet();
  668. dtNew1.TableName = "Table0";
  669. dtNew2.TableName = "Table1";
  670. dz.Tables.Add(dtNew1.Copy());
  671. dz.Tables.Add(dtNew2.Copy());
  672. DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["IDs"], dz.Tables[1].Columns["IDs"]);
  673. dz.Relations.Add(dh);
  674. msg = DBHelper.DataSetToJson(dz, RelationName);
  675. return msg;
  676. }
  677. public string Approve(List<ICSOutsourcingIssueDoc> infos)
  678. {
  679. List<ICSOutsourcingIssueDoc> szJson = new List<ICSOutsourcingIssueDoc>();
  680. string connS = "";
  681. string json = "";
  682. if (infos.Count <= 0)
  683. {
  684. throw new Exception("传送数据为空!");
  685. }
  686. string res = string.Empty;
  687. SqlConnection conn = new SqlConnection();
  688. SqlCommand cmd = new SqlCommand();
  689. string sql = string.Empty;
  690. foreach (ICSOutsourcingIssueDoc info in infos)
  691. {
  692. try
  693. {
  694. connS = string.Format(connString, info.WorkPoint);
  695. conn = new System.Data.SqlClient.SqlConnection(connS);
  696. conn.Open();
  697. SqlTransaction sqlTran = conn.BeginTransaction();
  698. cmd = new SqlCommand();
  699. cmd.Transaction = sqlTran;
  700. cmd.Connection = conn;
  701. if (info.MTime < new DateTime(2000, 01, 01))
  702. throw new Exception("请输入正确的操作时间:" + info.MTime);
  703. sql = @"UPDATE dbo.rdrecord11 SET cHandler ='" + info.User + @"' ,
  704. dnmodifytime =CONVERT(VARCHAR(50),GETDATE(),112),dModifyDate =GETDATE() WHERE ID='{0}'";
  705. sql = string.Format(sql, info.ID);
  706. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  707. cmd.Transaction.Commit();
  708. }
  709. catch (Exception ex)
  710. {
  711. cmd.Transaction.Rollback();
  712. log.Error(ex.Message);
  713. throw new Exception(ex.Message);
  714. }
  715. finally
  716. {
  717. if (conn.State == ConnectionState.Open)
  718. {
  719. conn.Close();
  720. }
  721. conn.Dispose();
  722. }
  723. }
  724. return json;
  725. }
  726. /// <summary>
  727. /// 删除请购单
  728. /// </summary>
  729. /// <param name="infos"></param>
  730. /// <returns></returns>
  731. public string Delete(List<ICSOutsourcingIssueDoc> infos)
  732. {
  733. List<ICSOutsourcingIssueDoc> szJson = new List<ICSOutsourcingIssueDoc>();
  734. if (infos.Count <= 0)
  735. {
  736. throw new Exception("传送数据为空!");
  737. }
  738. DataTable dtNew = null;
  739. string connS = "";
  740. string res = string.Empty;
  741. SqlConnection conn = new SqlConnection();
  742. SqlCommand cmd = new SqlCommand();
  743. string sql = string.Empty;
  744. foreach (ICSOutsourcingIssueDoc info in infos)
  745. {
  746. try
  747. {
  748. connS = string.Format(connString, info.WorkPoint);
  749. conn = new System.Data.SqlClient.SqlConnection(connS);
  750. conn.Open();
  751. SqlTransaction sqlTran = conn.BeginTransaction();
  752. cmd = new SqlCommand();
  753. cmd.Transaction = sqlTran;
  754. cmd.Connection = conn;
  755. if (info.MTime < new DateTime(2000, 01, 01))
  756. throw new Exception("请输入正确的操作时间:" + info.MTime);
  757. sql = @" DELETE dbo.rdrecord11 WHERE ID='{0}'";
  758. sql = string.Format(sql, info.ID);
  759. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  760. cmd.Transaction.Commit();
  761. }
  762. catch (Exception ex)
  763. {
  764. cmd.Transaction.Rollback();
  765. log.Error(ex.Message);
  766. throw new Exception(ex.Message);
  767. }
  768. finally
  769. {
  770. if (conn.State == ConnectionState.Open)
  771. {
  772. conn.Close();
  773. }
  774. conn.Dispose();
  775. }
  776. }
  777. return res;
  778. }
  779. /// <summary>
  780. ///
  781. /// 记录日志
  782. /// </summary>
  783. /// <param name="Bills"></param>
  784. private void LogInfo(List<ICSOutsourcingIssueDoc> Bills)
  785. {
  786. string HeadList = string.Empty;
  787. string BodyList = string.Empty;
  788. foreach (ICSOutsourcingIssueDoc head in Bills)
  789. {
  790. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  791. foreach (ICSOutsourcingIssueDocs body in head.details)
  792. {
  793. BodyList += "\r\n 表体主键ID: " + body.SourceDetailID + ",数量:" + body.Quantity;
  794. }
  795. }
  796. log.Info(HeadList);
  797. log.Info(BodyList);
  798. }
  799. }
  800. }