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

2142 lines
119 KiB

  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 OutsourcingReceiveDoc
  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["OutIssRCRDNAME"];
  23. private static string CRNA = System.Configuration.ConfigurationManager.AppSettings["MoICRDNAME"];
  24. /// <summary>
  25. /// 获取委外采购入库单
  26. /// </summary>
  27. /// <param name="infos"></param>
  28. /// <returns></returns>
  29. public string Get(List<ICSOutsourcingReceiveDoc> infos)
  30. {
  31. List<ICSOutsourcingReceiveDoc> szJson = new List<ICSOutsourcingReceiveDoc>();
  32. DataTable dt = null;
  33. DataTable dtNew = null;
  34. string connS = "";
  35. string json = "";
  36. if (infos.Count <= 0)
  37. {
  38. throw new Exception("传送数据为空!");
  39. }
  40. string res = string.Empty;
  41. SqlConnection conn = new SqlConnection();
  42. SqlCommand cmd = new SqlCommand();
  43. string sql = string.Empty;
  44. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  45. foreach (string WorkPoint in result)
  46. {
  47. try
  48. {
  49. connS = string.Format(connString, WorkPoint);
  50. conn = new System.Data.SqlClient.SqlConnection(connS);
  51. conn.Open();
  52. SqlTransaction sqlTran = conn.BeginTransaction();
  53. cmd = new SqlCommand();
  54. cmd.Transaction = sqlTran;
  55. cmd.Connection = conn;
  56. foreach (ICSOutsourcingReceiveDoc info in infos)
  57. {
  58. if (WorkPoint != info.WorkPoint)
  59. continue;
  60. ICSUserInfo userInfo = new ICSUserInfo();
  61. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  62. if (info.MTime < new DateTime(2000, 01, 01))
  63. throw new Exception("请输入正确的操作时间:" + info.MTime);
  64. sql = @" select a.ID,iMPoIds,iordercode,a.cCode,a.cDepCode,c.cDepName,a.cWhCode,
  65. d.cWhName,a.cSource,a.cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,
  66. b.AutoID ,b.irowno,b.cInvCode,b.iQuantity,e.MoDetailsID,b.iNum,e.csocode
  67. from rdrecord11 a
  68. left join rdrecords11 b on a.ID=b.ID
  69. left join Department c on a.cDepCode=c.cDepCode
  70. left join Warehouse d on a.cWhCode=d.cWhCode
  71. left join OM_MODetails e on b.iOMoDID=e.MoDetailsID
  72. where a.cBusType=''";
  73. if (!string.IsNullOrWhiteSpace(info.ORCVTCode))
  74. {
  75. sql += " and a.cCode='{0}'";
  76. }
  77. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  78. {
  79. sql += " and ISNULL(a.dnmodifytime,ISNULL(a.dnverifytime, ISNULL(a.dnmodifytime, a.dnmaketime)))>='{1}'";
  80. }
  81. if (!string.IsNullOrWhiteSpace(info.User))
  82. {
  83. sql += "and a.CMAKER='{2}'";
  84. }
  85. sql = string.Format(sql, info.ORCVTCode, info.MTime, userInfo.UserName);
  86. dt = DBHelper.SQlReturnData(sql, cmd);
  87. if (dt.Rows.Count <= 0 || dt == null)
  88. throw new Exception("委外发料单:" + info.ORCVTCode + ",无信息!");
  89. if (dtNew == null)
  90. dtNew = dt;
  91. else
  92. dtNew.Merge(dt);
  93. }
  94. cmd.Transaction.Commit();
  95. }
  96. catch (Exception ex)
  97. {
  98. cmd.Transaction.Rollback();
  99. log.Error(ex.Message);
  100. throw new Exception(ex.Message);
  101. }
  102. finally
  103. {
  104. if (conn.State == ConnectionState.Open)
  105. {
  106. conn.Close();
  107. }
  108. conn.Dispose();
  109. }
  110. }
  111. json = JsonConvert.SerializeObject(dtNew);
  112. return json;
  113. }
  114. /// <summary>
  115. /// 创建委外采购入(到货单)
  116. /// </summary>
  117. /// <param name="Bills"></param>
  118. /// <returns></returns>
  119. public string CreateOutsourcingReceiveDocByPOArrive(List<ICSOutsourcingReceiveDoc> Bills)
  120. {
  121. string msg = "";
  122. DataSet ds = null;
  123. string connS = "";
  124. string sql = "";
  125. string iFatherIdTwo = "";
  126. int num = 0;
  127. int isEnable = 0;
  128. string gys = "";
  129. //取得out库单的默认显示模版
  130. //取得采购入库单单据 表头ID,表体DID
  131. VouchKey key = new VouchKey();
  132. SqlConnection conn = new SqlConnection();
  133. SqlCommand cmd = new SqlCommand();
  134. if (Bills.Count <= 0)
  135. {
  136. throw new Exception("传送数据为空!");
  137. }
  138. LogInfo(Bills);
  139. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  140. foreach (string WorkPoint in result)
  141. {
  142. try
  143. {
  144. connS = string.Format(connString, WorkPoint);
  145. conn = new System.Data.SqlClient.SqlConnection(connS);
  146. conn.Open();
  147. SqlTransaction sqlTran = conn.BeginTransaction();
  148. cmd = new SqlCommand();
  149. cmd.Transaction = sqlTran;
  150. cmd.Connection = conn;
  151. foreach (ICSOutsourcingReceiveDoc head in Bills)
  152. {
  153. iFatherIdTwo = "";
  154. num = head.details.Count();
  155. if (WorkPoint != head.WorkPoint)
  156. continue;
  157. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  158. throw new Exception("U8正在整理现存量,请稍后再试");
  159. string[] ss = head.WorkPoint.Split('_');
  160. ERPDB = ss[1];
  161. ICSUserInfo userInfo = new ICSUserInfo();
  162. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  163. string[] dd = CRNALL.Split('~');
  164. string crdname = dd[0];
  165. string carname = dd[1];
  166. string surface = dd[2];
  167. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  168. //取得out库单的默认显示模版
  169. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  170. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  171. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  172. DateTime date = DateTime.Now;
  173. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  174. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode, head.WorkPoint);
  175. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  176. #region 验证数量不能大于到货单数量
  177. //foreach (ICSOutsourcingReceiveDocs body in head.details)
  178. // {
  179. // sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNDetailID + "'";
  180. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  181. // if (dtQty != null && dtQty.Rows.Count > 0)
  182. // {
  183. // //到货单可入数量
  184. // decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  185. // sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNDetailID + "'";
  186. // DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  187. // decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  188. // if (ReceQty + body.Quantity > PUQty)
  189. // {
  190. // throw new Exception("入库数量大于当前到货单行可入库数量!");
  191. // }
  192. // }
  193. // else
  194. // {
  195. // throw new Exception("U8不存在该到货单行!");
  196. // }
  197. // }
  198. #endregion
  199. if (head.Enable == true)
  200. {
  201. sql = @"SELECT a.POID,a.cVenCode FROM PO_Pomain a
  202. LEFT JOIN PU_ArrivalVouch b ON a.cPOID=b.cpocode WHERE a.cBusType='' and b.cCode ='" + head.ODNCode + "' ";
  203. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  204. if (dtCheck != null && dtCheck.Rows.Count > 0)
  205. {
  206. gys = dtCheck.Rows[0][1].ToString();
  207. isEnable = 0;
  208. sql = @"SELECT cWhCode FROM Warehouse WHERE bProxyWh ='1' ";
  209. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  210. if (dtCheck != null && dtCheck.Rows.Count > 0)
  211. {
  212. foreach (DataRow item in dtCheck.Rows)
  213. {
  214. if (head.WHCode == item["cWhCode"].ToString())
  215. {
  216. isEnable++;
  217. }
  218. }
  219. }
  220. if (isEnable == 0)
  221. {
  222. throw new Exception("该仓库:" + head.WHCode + @"不是代管仓,请重新输入!");
  223. }
  224. }
  225. sql = @"SELECT POID FROM PO_Pomain a
  226. LEFT JOIN PU_ArrivalVouch b ON a.cPOID=b.cpocode WHERE a.cBusType='' and b.cCode ='" + head.ODNCode + "' ";
  227. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  228. if (dtCheck != null && dtCheck.Rows.Count > 0)
  229. {
  230. int isZL = 0;
  231. sql = @"SELECT cWhCode FROM Warehouse WHERE bWhAsset ='1' ";
  232. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  233. if (dtCheck != null && dtCheck.Rows.Count > 0)
  234. {
  235. foreach (DataRow item in dtCheck.Rows)
  236. {
  237. if (head.WHCode == item["cWhCode"].ToString())
  238. {
  239. isZL++;
  240. }
  241. }
  242. }
  243. if (isZL == 0)
  244. {
  245. throw new Exception("该仓库:" + head.WHCode + @"不是资产仓,请重新输入!");
  246. }
  247. }
  248. }
  249. string cfcode = string.Empty;
  250. string zq = string.Empty;
  251. string lz = string.Empty;
  252. string dz = string.Empty;
  253. sql = @"select a.cCode,a.iLZYJ,a.iLZFS,iPLNum,dblZQNum,c.dDate,
  254. (case when ilzfs=1 then dateadd(day,-day(getdate()),dateadd(month,1,getdate())) else DATEADD(day,isnull(iPLNum,0),c.dDate) end)
  255. lz,DATEADD(day,isnull(dblZQNum,0),DATEADD(day,isnull(iPLNum,0),c.dDate)) dz
  256. from
  257. (select b.cVenPUOMProtocol,a.cvencode,a.dDate,a.cCode from PU_ArrivalVouch a
  258. left join Vendor b on a.cvencode=b.cvencode
  259. where a.cCode='" + head.ODNCode + @"'
  260. ) c
  261. left join AA_Agreement a on a.cCode=c.cVenPUOMProtocol
  262. ";
  263. DataTable dtDisCheck1 = DBHelper.SQlReturnData(sql, cmd);
  264. if (dtDisCheck1 != null && dtDisCheck1.Rows.Count > 0)
  265. {
  266. foreach (DataRow item in dtDisCheck1.Rows)
  267. {
  268. if (item["iLZYJ"].ToString() == "10" & (item["iLZFS"].ToString() == "0" | item["iLZFS"].ToString() == "1"))
  269. {
  270. cfcode = item["cCode"].ToString();
  271. zq = item["dblZQNum"].ToString();
  272. lz = item["lz"].ToString();
  273. dz = item["dz"].ToString();
  274. }
  275. else
  276. {
  277. if (item["cCode"].ToString()=="")
  278. cfcode = item["cCode"].ToString();
  279. }
  280. }
  281. }
  282. #region 委外采购入库单 表头
  283. sql = @"INSERT INTO dbo.RdRecord01
  284. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  285. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  286. cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  287. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  288. cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  289. cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  290. dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  291. cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  292. ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  293. bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  294. ireturncount ,iverifystate ,iswfcontrolled ,
  295. cModifyPerson ,dnmaketime ,dnverifytime ,
  296. bredvouch ,bCredit ,iPrintCount,cVenPUOMProtocol,iCreditPeriod,dCreditStart,dGatheringDate)
  297. SELECT @ID,1,'01','','',@cWhCode,
  298. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,@cDepCode,a.cPersonCode,'01',
  299. a.cVenCode,b.cCode,a.cCode,@cHandler,a.cMemo,
  300. 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  301. a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  302. a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  303. a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  304. a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  305. a.ID,a.iTaxRate,a.iExchRate,a.cexch_name,0,
  306. 0,0,0,
  307. 0,0,0,
  308. '',GETDATE(),GETDATE(),
  309. 0,0,0,@cVenPUOMProtocol,@iCreditPeriod,@dCreditStart,@dGatheringDate
  310. FROM dbo.PU_ArrivalVouch a LEFT JOIN dbo.OM_MOMain b ON a.cpocode=b.cCode WHERE a.cCode='" + head.ODNCode + "'";
  311. cmd.Parameters.Clear();
  312. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  313. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  314. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  315. cmd.Parameters.Add(new SqlParameter("@cDepCode", ""));
  316. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  317. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  318. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  319. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  320. cmd.Parameters.Add(new SqlParameter("@cVenPUOMProtocol", cfcode));
  321. cmd.Parameters.Add(new SqlParameter("@iCreditPeriod", zq));
  322. cmd.Parameters.Add(new SqlParameter("@dCreditStart", lz));
  323. cmd.Parameters.Add(new SqlParameter("@dGatheringDate", dz));
  324. cmd.CommandText = sql;
  325. try
  326. {
  327. int count = cmd.ExecuteNonQuery();
  328. if (count <= 0)
  329. {
  330. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  331. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  332. }
  333. }
  334. catch (Exception ex)
  335. {
  336. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  337. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  338. }
  339. #endregion
  340. #region 采购入库单 表体
  341. foreach (ICSOutsourcingReceiveDocs body in head.details)
  342. {
  343. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  344. {
  345. if (body.BatchCode == "")
  346. {
  347. log.Error("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  348. throw new Exception("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  349. }
  350. }
  351. else
  352. {
  353. if (body.BatchCode != "")
  354. {
  355. log.Error("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  356. throw new Exception("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  357. }
  358. }
  359. sql = @"INSERT INTO dbo.rdrecords01
  360. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,iNum,
  361. iFlag ,iSQuantity ,iSNum ,iMoney ,
  362. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  363. cItem_class ,cItemCode ,iPOsID ,fACost ,cName ,iNQuantity ,
  364. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  365. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  366. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode ,
  367. dbarvdate ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 )
  368. SELECT @AutoID,@ID1,@cInvCode,@iQuantity,0,0,0,@iNum,
  369. 0,0,0,0,
  370. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  371. a.cItem_class,@cItemCode,a.iPOsID,0,a.cItemName,a.iQuantity,
  372. c.cVenCode,a.Autoid,0,0,0,0,
  373. 0,a.iTaxRate,0,0,1,d.cCode,0,
  374. 0,0,0,1,c.cCode,
  375. c.dDate,0,0,0,@irowno,0,@cBatch,a.iCost*convert(decimal, @iQuantity),a.iCost,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  376. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  377. INNER JOIN dbo.OM_MOMain d ON b.MOID=d.MOID
  378. INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where a.Autoid='" + body.ODNDetailID + @"'
  379. EXEC PROC_Update_OM_RdRecord01 @ID= @ID1
  380. ";
  381. cmd.Parameters.Clear();
  382. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  383. cmd.Parameters.Add(new SqlParameter("@ID1", iFatherId));
  384. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  385. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  386. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount.ToString("0.00")));
  387. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  388. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  389. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  390. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  391. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  392. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  393. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  394. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  395. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  396. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  397. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  398. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  399. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  400. cmd.CommandText = sql;
  401. try
  402. {
  403. int count = cmd.ExecuteNonQuery();
  404. if (count <= 0)
  405. {
  406. log.Error("委外采购入库单表体失败,受影响行数<=0;");
  407. throw new Exception("委外采购入库单表体失败,受影响行数<=0;");
  408. }
  409. }
  410. catch (Exception ex)
  411. {
  412. log.Error("委外采购入库单表体失败!SQL:\r\n" + sql, ex);
  413. throw new Exception("委外采购入库单表体失败!SQL:\r\n" + sql, ex);
  414. }
  415. //OM_MODetails
  416. sql = @"UPDATE b SET freceivedqty=ISNULL(freceivedqty,0)+" + body.Quantity + @"
  417. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  418. WHERE a.Autoid='" + body.ODNDetailID + "'";
  419. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  420. //回写PU_ArrivalVouchs失败
  421. sql = "UPDATE dbo.PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+" + body.Quantity + " WHERE Autoid='" + body.ODNDetailID + "'";
  422. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写PU_ArrivalVouchs失败!");
  423. sql = @"select isnull(fValidInQuan,0)fOutQuantity,isnull(iQuantity,0)iQuantity,ISNULL(iNum, 0)iNum,ISNULL(NiNum, 0)NiNum,ISNULL(iNum, 0)-ISNULL(NiNum, 0) SiNum from PU_ArrivalVouchs a
  424. LEFT JOIN (select sum(iNum) NiNum,iArrsId from rdrecords01 GROUP BY iArrsId) b on a.Autoid=b.iArrsId
  425. where a.AutoID='" + body.ODNDetailID + "'";
  426. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  427. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  428. {
  429. foreach (DataRow item in dtDisCheck.Rows)
  430. {
  431. if (Convert.ToDecimal(item["fOutQuantity"].ToString()) == Convert.ToDecimal(item["iQuantity"].ToString()))
  432. {
  433. if (Convert.ToDecimal(item["iNum"].ToString()) != 0)
  434. {
  435. if (Convert.ToDecimal(item["SiNum"].ToString()) != 0)
  436. {
  437. sql = @"Update rdrecords09 set iNum=isnull(iNum,0)+(" + item["SiNum"].ToString() + ") where autoid='" + iChildId + "' ";
  438. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item["SiNum"].ToString()));
  439. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  440. }
  441. }
  442. }
  443. }
  444. }
  445. //if (isEnable != 0)
  446. //{
  447. // key.cBustypeUN = "委外加工";
  448. // key.cVouchTypeUN = "01";
  449. // key.TableName = "IA_ST_UnAccountVouch01";
  450. // //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  451. // DBHelper.UpdateCurrentStockNEWDG(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, gys, key);
  452. //}
  453. //else
  454. //{
  455. // //更新现存量
  456. // key.cBustypeUN = "委外加工";
  457. // key.cVouchTypeUN = "01";
  458. // key.TableName = "IA_ST_UnAccountVouch01";
  459. // //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  460. // 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);
  461. //}
  462. key.cBustypeUN = "委外加工";
  463. key.cVouchTypeUN = "01";
  464. key.TableName = "IA_ST_UnAccountVouch01";
  465. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, body.Quantity, body.Amount, body.cFree1,
  466. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  467. gys, key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  468. iFatherIdTwo = "'" + iFatherId + "',";
  469. iChildId--;
  470. }
  471. #endregion
  472. sql = @" select DISTINCT a.ID as ID,a.ID as IDs,a.cCode as ORCVTCode ,a.cVenCode,d.cVenName,a.cOrderCode as OOCode,
  473. a.cMaker as CreateUser,dnmaketime as CreateDateTime,cHandler as Checker,dnverifytime as CheckDateTime
  474. from rdrecord01 a
  475. left join Department c on a.cDepCode=c.cDepCode
  476. left join Vendor d on a.cVenCode=d.cVenCode
  477. where a.cBusType='' AND A.ID in ({0})
  478. select DISTINCT a.ID as IDs,b.AutoID as DetailID,b.irowno as Sequence,b.cInvCode as InvCode,b.iQuantity as Quantity,iNum as Amount,iArrsId as ODNDetailID
  479. ,isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  480. isnull(b.cFree1,'') as cFree1,
  481. isnull(b.cFree2,'') as cFree2,
  482. isnull(b.cFree3,'') as cFree3,
  483. isnull(b.cFree4,'') as cFree4,
  484. isnull(b.cFree5,'') as cFree5,
  485. isnull(b.cFree6,'') as cFree6,
  486. isnull(b.cFree7,'') as cFree7,
  487. isnull(b.cFree8,'') as cFree8,
  488. isnull(b.cFree9,'') as cFree9,
  489. isnull(b.cFree10,'') as cFree10
  490. from rdrecord01 a
  491. left join rdrecords01 b on a.ID=b.ID
  492. left join Department c on a.cDepCode=c.cDepCode
  493. left join Vendor d on a.cVenCode=d.cVenCode
  494. where a.cBusType='' AND A.ID in ({0}) ";
  495. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  496. if (ds != null)
  497. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  498. else
  499. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  500. }
  501. cmd.Transaction.Commit();
  502. }
  503. catch (Exception ex)
  504. {
  505. cmd.Transaction.Rollback();
  506. log.Error(ex.Message);
  507. throw new Exception(ex.Message);
  508. }
  509. finally
  510. {
  511. if (conn.State == ConnectionState.Open)
  512. {
  513. conn.Close();
  514. }
  515. conn.Dispose();
  516. }
  517. }
  518. msg = JSON.DataSetToJson(ds, "details", "IDs");
  519. return msg;
  520. }
  521. /// <summary>
  522. /// 创建委外采购入(委外到货单)
  523. /// </summary>
  524. /// <param name="Bills"></param>
  525. /// <returns></returns>
  526. public string CreateOutsourcingReceiveDocByPU(List<ICSOutsourcingReceiveDoc> Bills)
  527. {
  528. string msg = "";
  529. string gys = "";
  530. int isEnable = 0;
  531. string connS = "";
  532. int num = 0;
  533. //取得out库单的默认显示模版
  534. //取得采购入库单单据 表头ID,表体DID
  535. VouchKey key = new VouchKey();
  536. SqlConnection conn = new SqlConnection();
  537. SqlCommand cmd = new SqlCommand();
  538. if (Bills.Count <= 0)
  539. {
  540. throw new Exception("传送数据为空!");
  541. }
  542. LogInfo(Bills);
  543. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  544. foreach (string WorkPoint in result)
  545. {
  546. try
  547. {
  548. connS = string.Format(connString, WorkPoint);
  549. conn = new System.Data.SqlClient.SqlConnection(connS);
  550. conn.Open();
  551. SqlTransaction sqlTran = conn.BeginTransaction();
  552. cmd = new SqlCommand();
  553. cmd.Transaction = sqlTran;
  554. cmd.Connection = conn;
  555. foreach (ICSOutsourcingReceiveDoc head in Bills)
  556. {
  557. num = head.details.Count();
  558. if (WorkPoint != head.WorkPoint)
  559. continue;
  560. string[] ss = head.WorkPoint.Split('_');
  561. ERPDB = ss[1];
  562. string[] dd = CRNALL.Split('~');
  563. string crdname = dd[0];
  564. string carname = dd[1];
  565. string surface = dd[2];
  566. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  567. //取得out库单的默认显示模版
  568. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  569. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  570. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  571. DateTime date = DateTime.Now;
  572. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  573. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode, head.WorkPoint);
  574. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  575. string sql = "";
  576. ICSUserInfo userInfo = new ICSUserInfo();
  577. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  578. #region 验证数量不能大于到货单数量
  579. foreach (ICSOutsourcingReceiveDocs body in head.details)
  580. {
  581. sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNDetailID + "'";
  582. DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  583. if (dtQty != null && dtQty.Rows.Count > 0)
  584. {
  585. //到货单可入数量
  586. decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  587. sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNDetailID + "'";
  588. DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  589. decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  590. if (ReceQty + body.Quantity > PUQty)
  591. {
  592. throw new Exception("入库数量大于当前到货单行可入库数量!");
  593. }
  594. }
  595. else
  596. {
  597. throw new Exception("U8不存在该到货单行!");
  598. }
  599. }
  600. #endregion
  601. string cfcode = string.Empty;
  602. string zq = string.Empty;
  603. string lz = string.Empty;
  604. string dz = string.Empty;
  605. sql = @"select a.cCode,a.iLZYJ,a.iLZFS,iPLNum,dblZQNum,c.dDate,
  606. (case when ilzfs=1 then dateadd(day,-day(getdate()),dateadd(month,1,getdate())) else DATEADD(day,isnull(iPLNum,0),c.dDate) end)
  607. lz,DATEADD(day,isnull(dblZQNum,0),DATEADD(day,isnull(iPLNum,0),c.dDate)) dz
  608. from
  609. (select b.cVenPUOMProtocol,a.cvencode,a.dDate,a.cCode from PU_ArrivalVouch a
  610. left join Vendor b on a.cvencode=b.cvencode
  611. where a.cCode='" + head.ODNCode + @"'
  612. ) c
  613. left join AA_Agreement a on a.cCode=c.cVenPUOMProtocol
  614. ";
  615. DataTable dtDisCheck1 = DBHelper.SQlReturnData(sql, cmd);
  616. if (dtDisCheck1 != null && dtDisCheck1.Rows.Count > 0)
  617. {
  618. foreach (DataRow item in dtDisCheck1.Rows)
  619. {
  620. if (item["iLZYJ"].ToString() == "10" & (item["iLZFS"].ToString() == "0" | item["iLZFS"].ToString() == "1"))
  621. {
  622. cfcode = item["cCode"].ToString();
  623. zq = item["dblZQNum"].ToString();
  624. lz = item["lz"].ToString();
  625. dz = item["dz"].ToString();
  626. }
  627. else
  628. {
  629. if (item["cCode"].ToString() == "")
  630. cfcode = item["cCode"].ToString();
  631. }
  632. }
  633. }
  634. if (head.Enable == true)
  635. {
  636. sql = @"SELECT a.POID,a.cVenCode FROM PO_Pomain a
  637. LEFT JOIN PU_ArrivalVouch b ON a.cPOID=b.cpocode WHERE a.cBusType='' and b.cCode ='" + head.ODNCode + "' ";
  638. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  639. if (dtCheck != null && dtCheck.Rows.Count > 0)
  640. {
  641. gys = dtCheck.Rows[0][1].ToString();
  642. isEnable = 0;
  643. sql = @"SELECT cWhCode FROM Warehouse WHERE bProxyWh ='1' ";
  644. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  645. if (dtCheck != null && dtCheck.Rows.Count > 0)
  646. {
  647. foreach (DataRow item in dtCheck.Rows)
  648. {
  649. if (head.WHCode == item["cWhCode"].ToString())
  650. {
  651. isEnable++;
  652. }
  653. }
  654. }
  655. if (isEnable == 0)
  656. {
  657. throw new Exception("该仓库:" + head.WHCode + @"不是代管仓,请重新输入!");
  658. }
  659. }
  660. sql = @"SELECT POID FROM PO_Pomain a
  661. LEFT JOIN PU_ArrivalVouch b ON a.cPOID=b.cpocode WHERE a.cBusType='' and b.cCode ='" + head.ODNCode + "' ";
  662. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  663. if (dtCheck != null && dtCheck.Rows.Count > 0)
  664. {
  665. int isZL = 0;
  666. sql = @"SELECT cWhCode FROM Warehouse WHERE bWhAsset ='1' ";
  667. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  668. if (dtCheck != null && dtCheck.Rows.Count > 0)
  669. {
  670. foreach (DataRow item in dtCheck.Rows)
  671. {
  672. if (head.WHCode == item["cWhCode"].ToString())
  673. {
  674. isZL++;
  675. }
  676. }
  677. }
  678. if (isZL == 0)
  679. {
  680. throw new Exception("该仓库:" + head.WHCode + @"不是资产仓,请重新输入!");
  681. }
  682. }
  683. }
  684. #region 委外采购入库单 表头
  685. sql = @"INSERT INTO dbo.RdRecord01
  686. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  687. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  688. cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  689. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  690. cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  691. cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  692. dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  693. cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  694. ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  695. bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  696. ireturncount ,iverifystate ,iswfcontrolled ,
  697. cModifyPerson ,dnmaketime ,dnverifytime ,
  698. bredvouch ,bCredit ,iPrintCount,cVenPUOMProtocol,iCreditPeriod,dCreditStart,dGatheringDate)
  699. SELECT @ID,1,'01','','',@cWhCode,
  700. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,a.cDepCode,a.cPersonCode,a.cPTCode,
  701. a.cVenCode,b.cCode,a.cCode,@cHandler,a.cMemo,
  702. 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  703. a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  704. a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  705. a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  706. a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  707. a.ID,a.iTaxRate,a.iExchRate,a.cexch_name,0,
  708. 0,0,0,
  709. 0,0,0,
  710. '',GETDATE(),GETDATE(),
  711. 0,0,0,@cVenPUOMProtocol,@iCreditPeriod,@dCreditStart,@dGatheringDate
  712. FROM dbo.PU_ArrivalVouch a LEFT JOIN dbo.OM_MOMain b ON a.cpocode=b.cCode WHERE a.cCode ='" + head.ODNCode + "'";
  713. cmd.Parameters.Clear();
  714. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  715. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  716. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  717. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  718. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  719. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  720. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  721. cmd.Parameters.Add(new SqlParameter("@cVenPUOMProtocol", cfcode));
  722. cmd.Parameters.Add(new SqlParameter("@iCreditPeriod", zq));
  723. cmd.Parameters.Add(new SqlParameter("@dCreditStart", lz));
  724. cmd.Parameters.Add(new SqlParameter("@dGatheringDate", dz));
  725. cmd.CommandText = sql;
  726. try
  727. {
  728. int count = cmd.ExecuteNonQuery();
  729. if (count <= 0)
  730. {
  731. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  732. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  733. }
  734. }
  735. catch (Exception ex)
  736. {
  737. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  738. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  739. }
  740. #endregion
  741. #region 采购入库单 表体
  742. int irowno = 0;
  743. foreach (ICSOutsourcingReceiveDocs body in head.details)
  744. {
  745. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  746. {
  747. if (body.BatchCode == "")
  748. {
  749. log.Error("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  750. throw new Exception("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  751. }
  752. }
  753. else
  754. {
  755. if (body.BatchCode != "")
  756. {
  757. log.Error("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  758. throw new Exception("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  759. }
  760. }
  761. sql = @"INSERT INTO dbo.rdrecords01
  762. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,iNum,
  763. iFlag ,iSQuantity ,iSNum ,iMoney ,
  764. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  765. cItem_class ,cItemCode ,iPOsID ,fACost ,cName ,iNQuantity ,
  766. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  767. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  768. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode ,
  769. dbarvdate ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 )
  770. SELECT @AutoID,@ID1,@cInvCode,@iQuantity,0,0,0,@iNum,
  771. 0,0,0,0,
  772. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  773. a.cItem_class,a.cItemCode,a.iPOsID,0,a.cItemName,a.iQuantity,
  774. c.cVenCode,a.Autoid,0,0,0,0,
  775. 0,a.iTaxRate,0,0,1,d.cCode,0,
  776. 0,0,0,1,c.cCode,
  777. c.dDate,0,0,0,@irowno,0,@cBatch,a.iCost*convert(decimal, @iQuantity),a.iCost,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  778. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  779. INNER JOIN dbo.OM_MOMain d ON b.MOID=d.MOID
  780. INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where a.Autoid='" + body.ODNDetailID + @"'
  781. EXEC PROC_Update_OM_RdRecord01 @ID= @ID1
  782. ";
  783. cmd.Parameters.Clear();
  784. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  785. cmd.Parameters.Add(new SqlParameter("@ID1", iFatherId));
  786. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  787. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  788. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount.ToString("0.00")));
  789. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  790. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  791. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  792. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  793. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  794. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  795. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  796. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  797. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  798. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  799. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  800. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  801. cmd.CommandText = sql;
  802. try
  803. {
  804. int count = cmd.ExecuteNonQuery();
  805. if (count <= 0)
  806. {
  807. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  808. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  809. }
  810. }
  811. catch (Exception ex)
  812. {
  813. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  814. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  815. }
  816. //OM_MODetails
  817. sql = @"UPDATE b SET freceivedqty=ISNULL(freceivedqty,0)+" + body.Quantity + @"
  818. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  819. WHERE a.Autoid='" + body.ODNDetailID + "'";
  820. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  821. //回写PU_ArrivalVouchs失败
  822. sql = "UPDATE dbo.PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+" + body.Quantity + " WHERE Autoid='" + body.ODNDetailID + "'";
  823. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写PU_ArrivalVouchs失败!");
  824. sql = @"select isnull(fValidInQuan,0)fOutQuantity,isnull(iQuantity,0)iQuantity,ISNULL(iNum, 0)iNum,ISNULL(NiNum, 0)NiNum,ISNULL(iNum, 0)-ISNULL(NiNum, 0) SiNum from PU_ArrivalVouchs a
  825. LEFT JOIN (select sum(iNum) NiNum,iArrsId from rdrecords01 GROUP BY iArrsId) b on a.Autoid=b.iArrsId
  826. where a.AutoID='" + body.ODNDetailID + "'";
  827. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  828. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  829. {
  830. foreach (DataRow item in dtDisCheck.Rows)
  831. {
  832. if (Convert.ToDecimal(item["fOutQuantity"].ToString()) == Convert.ToDecimal(item["iQuantity"].ToString()))
  833. {
  834. if (Convert.ToDecimal(item["iNum"].ToString()) != 0)
  835. {
  836. if (Convert.ToDecimal(item["SiNum"].ToString()) != 0)
  837. {
  838. sql = @"Update rdrecords09 set iNum=isnull(iNum,0)+(" + item["SiNum"].ToString() + ") where autoid='" + iChildId + "' ";
  839. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item["SiNum"].ToString()));
  840. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  841. }
  842. }
  843. }
  844. }
  845. }
  846. //更新待入库数量
  847. sql = "UPDATE dbo.CurrentStock SET fInQuantity=ISNULL(fInQuantity,0)-" + body.Quantity + " WHERE cInvCode='" + body.InvCode + "' AND cWhCode ='" + head.WHCode + "'";
  848. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  849. {
  850. if (!string.IsNullOrWhiteSpace(body.BatchCode))
  851. {
  852. sql += " and cBatch='" + body.BatchCode + "'";
  853. }
  854. }
  855. else
  856. {
  857. sql += " and cBatch=''";
  858. }
  859. DBHelper.CmdExecuteNonQuery(sql, cmd, "更新待入库数量失败!");
  860. //更新现存量
  861. key.cBustypeUN = "委外加工";
  862. key.cVouchTypeUN = "01";
  863. key.TableName = "IA_ST_UnAccountVouch01";
  864. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  865. //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);
  866. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, body.Quantity, body.Amount, body.cFree1,
  867. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  868. gys, key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  869. // #region 判断现存量是否足够
  870. // sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fInQuantity<0)
  871. // BEGIN
  872. // DECLARE @MSG NVARCHAR(100)
  873. // SELECT @MSG='ERP待入库数量不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fInQuantity<0
  874. // RAISERROR(@MSG,16,1)
  875. // END";
  876. // cmd.CommandText = sql;
  877. // cmd.ExecuteNonQuery();
  878. // #endregion
  879. iChildId--;
  880. }
  881. #endregion
  882. //GetSerialCode(iFatherId.ToString());
  883. }
  884. cmd.Transaction.Commit();
  885. }
  886. catch (Exception ex)
  887. {
  888. cmd.Transaction.Rollback();
  889. log.Error(ex.Message);
  890. throw new Exception(ex.Message);
  891. }
  892. finally
  893. {
  894. if (conn.State == ConnectionState.Open)
  895. {
  896. conn.Close();
  897. }
  898. conn.Dispose();
  899. }
  900. }
  901. return msg;
  902. }
  903. /// <summary>
  904. /// 创建委外倒冲
  905. /// </summary>
  906. /// <param name="Bills"></param>
  907. /// <returns></returns>
  908. public string CreateOutsourcingReceiveDocByPO(List<ICSOutsourcingReceiveDoc> Bills)
  909. {
  910. int moidinbatch = 0;
  911. int isif = 0;
  912. string msg = "";
  913. string connS = "";
  914. string mocode = "";
  915. string seq = "";
  916. int num = 0;
  917. string iFatherIdTree = "";
  918. int out11 = 0;
  919. string gys = "";
  920. int isEnable = 0;
  921. int irownnew = 0;
  922. int news = 0;
  923. int iseq = 0;
  924. string iFatherIdTwo = "";
  925. string sql = "";
  926. DataTable dt = null;
  927. DataSet ds = null;
  928. VouchKey key = new VouchKey();
  929. SqlConnection conn = new SqlConnection();
  930. SqlCommand cmd = new SqlCommand();
  931. if (Bills.Count <= 0)
  932. {
  933. throw new Exception("传送数据为空!");
  934. }
  935. LogInfo(Bills);
  936. foreach (ICSOutsourcingReceiveDoc head in Bills)
  937. {
  938. foreach (ICSOutsourcingReceiveDocs load in head.details)
  939. {
  940. foreach (ICSOutsourcingReceiveDocss loads in load.detailss)
  941. {
  942. news++;
  943. }
  944. }
  945. }
  946. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  947. foreach (string WorkPoint in result)
  948. {
  949. try
  950. {
  951. connS = string.Format(connString, WorkPoint);
  952. conn = new System.Data.SqlClient.SqlConnection(connS);
  953. conn.Open();
  954. SqlTransaction sqlTran = conn.BeginTransaction();
  955. cmd = new SqlCommand();
  956. cmd.Transaction = sqlTran;
  957. cmd.Connection = conn;
  958. foreach (ICSOutsourcingReceiveDoc head in Bills)
  959. {
  960. moidinbatch = 0;
  961. iFatherIdTwo = "";
  962. iFatherIdTree = "";
  963. if (WorkPoint != head.WorkPoint)
  964. continue;
  965. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  966. throw new Exception("U8正在整理现存量,请稍后再试");
  967. string[] ss = head.WorkPoint.Split('_');
  968. ERPDB = ss[1];
  969. ICSUserInfo userInfo = new ICSUserInfo();
  970. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  971. string[] dd = CRNALL.Split('~');
  972. string crdname = dd[0];
  973. string carname = dd[1];
  974. string surface = dd[2];
  975. string[] d = CRNA.Split('~');
  976. string crdname1 = d[0];
  977. string carname1 = d[1];
  978. string surface1 = d[2];
  979. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  980. string cardnewcode1 = DBHelper.GetCardNumber(carname1, cmd);
  981. num = head.details.Count;
  982. //取得out库单的默认显示模版
  983. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  984. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  985. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  986. DateTime date = DateTime.Now;
  987. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  988. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode, head.WorkPoint);
  989. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  990. #region 验证数量不能大于到货单数量
  991. //foreach (ICSOutsourcingReceiveDocs body in head.details)
  992. //{
  993. // sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNDetailID + "'";
  994. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  995. // if (dtQty != null && dtQty.Rows.Count > 0)
  996. // {
  997. // //到货单可入数量
  998. // decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  999. // sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNDetailID + "'";
  1000. // DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  1001. // decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  1002. // if (ReceQty + body.Quantity > PUQty)
  1003. // {
  1004. // throw new Exception("入库数量大于当前到货单行可入库数量!");
  1005. // }
  1006. // }
  1007. // else
  1008. // {
  1009. // throw new Exception("U8不存在该到货单行!");
  1010. // }
  1011. //}
  1012. #endregion
  1013. string cfcode = string.Empty;
  1014. string zq = string.Empty;
  1015. string lz = string.Empty;
  1016. string dz = string.Empty;
  1017. sql = @"select a.cCode,a.iLZYJ,a.iLZFS,iPLNum,dblZQNum,c.dDate,
  1018. (case when ilzfs=1 then dateadd(day,-day(getdate()),dateadd(month,1,getdate())) else DATEADD(day,isnull(iPLNum,0),c.dDate) end)
  1019. lz,DATEADD(day,isnull(dblZQNum,0),DATEADD(day,isnull(iPLNum,0),c.dDate)) dz
  1020. from
  1021. (select b.cVenPUOMProtocol,a.cvencode,a.dDate,a.cCode from PU_ArrivalVouch a
  1022. left join Vendor b on a.cvencode=b.cvencode
  1023. where a.cCode='" + head.ODNCode + @"'
  1024. ) c
  1025. left join AA_Agreement a on a.cCode=c.cVenPUOMProtocol
  1026. ";
  1027. DataTable dtDisCheck1 = DBHelper.SQlReturnData(sql, cmd);
  1028. if (dtDisCheck1 != null && dtDisCheck1.Rows.Count > 0)
  1029. {
  1030. foreach (DataRow item in dtDisCheck1.Rows)
  1031. {
  1032. if (item["iLZYJ"].ToString() == "10" & (item["iLZFS"].ToString() == "0" | item["iLZFS"].ToString() == "1"))
  1033. {
  1034. cfcode = item["cCode"].ToString();
  1035. zq = item["dblZQNum"].ToString();
  1036. lz = item["lz"].ToString();
  1037. dz = item["dz"].ToString();
  1038. }
  1039. else
  1040. {
  1041. if (item["cCode"].ToString() == "")
  1042. cfcode = item["cCode"].ToString();
  1043. }
  1044. }
  1045. }
  1046. #region 委外采购入库单 表头
  1047. sql = @"INSERT INTO dbo.RdRecord01
  1048. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  1049. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  1050. cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  1051. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  1052. cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  1053. cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  1054. dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  1055. cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  1056. ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  1057. bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  1058. ireturncount ,iverifystate ,iswfcontrolled ,
  1059. cModifyPerson ,dnmaketime ,dnverifytime ,
  1060. bredvouch ,bCredit ,iPrintCount,cVenPUOMProtocol,iCreditPeriod,dCreditStart,dGatheringDate)
  1061. SELECT @ID,1,'01','','',@cWhCode,
  1062. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@RdCode,a.cDepCode,a.cPersonCode,a.cPTCode,
  1063. a.cVenCode,'',a.cCode,@cHandler,a.cMemo,
  1064. 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  1065. a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  1066. a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  1067. a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  1068. a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  1069. '',a.iTaxRate,a.nflat,a.cexch_name,0,
  1070. 0,0,0,
  1071. 0,0,0,
  1072. '',GETDATE(),GETDATE(),
  1073. 0,0,0,@cVenPUOMProtocol,@iCreditPeriod,@dCreditStart,@dGatheringDate
  1074. FROM dbo.OM_MOMain a WHERE a.cCode='" + head.OOCode + "'";
  1075. cmd.Parameters.Clear();
  1076. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  1077. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  1078. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  1079. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  1080. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  1081. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  1082. cmd.Parameters.Add(new SqlParameter("@RdCode", cRDcode));
  1083. cmd.Parameters.Add(new SqlParameter("@cVenPUOMProtocol", cfcode));
  1084. cmd.Parameters.Add(new SqlParameter("@iCreditPeriod", zq));
  1085. cmd.Parameters.Add(new SqlParameter("@dCreditStart", lz));
  1086. cmd.Parameters.Add(new SqlParameter("@dGatheringDate", dz));
  1087. cmd.CommandText = sql;
  1088. try
  1089. {
  1090. int count = cmd.ExecuteNonQuery();
  1091. if (count <= 0)
  1092. {
  1093. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  1094. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  1095. }
  1096. }
  1097. catch (Exception ex)
  1098. {
  1099. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  1100. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  1101. }
  1102. List<WH> LWH = new List<WH>();
  1103. #endregion
  1104. if (news != 0)
  1105. {
  1106. foreach (ICSOutsourcingReceiveDocs load in head.details)
  1107. {
  1108. foreach (ICSOutsourcingReceiveDocss loads in load.detailss)
  1109. {
  1110. WH wh = new WH();
  1111. wh.WHCODE = loads.WHCode;
  1112. wh.seq = 0;
  1113. if (LWH.Count != 0)
  1114. {
  1115. int whnumber = 0;
  1116. foreach (WH item in LWH)
  1117. {
  1118. if (item.WHCODE == wh.WHCODE)
  1119. whnumber++;
  1120. }
  1121. if (whnumber == 0)
  1122. LWH.Add(wh);
  1123. }
  1124. else
  1125. LWH.Add(wh);
  1126. }
  1127. }
  1128. foreach (WH item in LWH)
  1129. {
  1130. Dictionary<string, int> dic1 = DBHelper.GetAllCode("" + ERPDB + "", surface1, "1", head.WorkPoint);
  1131. int iFatherId1 = Convert.ToInt32(dic1["iFatherId"].ToString());
  1132. out11 = iFatherId1;
  1133. int iChildId1 = Convert.ToInt32(dic1["iChildId"].ToString());
  1134. DateTime date1 = DateTime.Now;
  1135. string cRDcode1 = DBHelper.GetRDCode(crdname1, cmd);
  1136. string iBaseCodeLen1 = DBHelper.GetAllRDCode(cardnewcode1, "" + date1 + "", "admin", cRDcode1, head.WorkPoint);
  1137. string DEF_ID1 = DBHelper.GetDefaultTemplate(cardnewcode1, cmd);
  1138. #region 材料出库单表头
  1139. sql = @"
  1140. INSERT INTO dbo.rdrecord11
  1141. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cBusCode,
  1142. cWhCode ,dDate ,cCode ,cRdCode ,cDepCode,
  1143. cPersonCode,cHandler ,bTransFlag ,cMaker ,dVeriDate ,
  1144. bpufirst ,biafirst ,VT_ID ,bIsSTQc ,cPsPcode ,
  1145. cMPoCode ,iproorderid ,bFromPreYear ,bIsLsQuery ,bIsComplement ,
  1146. iDiscountTaxType ,ireturncount ,iverifystate ,iswfcontrolled ,dnmaketime ,
  1147. dnverifytime ,bredvouch ,iPrintCount,cVenCode,iMQuantity)
  1148. SELECT distinct @ID,0,'11','','',@cBusCode,
  1149. @cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,c.cDepCode,
  1150. c.cPersonCode,@cHandler,0,@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),
  1151. 0,0,@VT_ID,0,null,
  1152. c.cCode,a.MoId,0,0,0,
  1153. 0,0,0,0,GETDATE(),
  1154. GETDATE(),@bredvouch,0,c.cVenCode,null
  1155. from dbo.OM_MOMaterials a
  1156. INNER JOIN dbo.OM_MODetails b ON a.MoDetailsID = b.MoDetailsID
  1157. LEFT JOIN dbo.OM_MOMain c ON b.MOID = c.MOID
  1158. WHERE c.cCode='" + head.OOCode + "'";
  1159. cmd.Parameters.Clear();
  1160. cmd.Parameters.Add(new SqlParameter("@ID", out11));
  1161. cmd.Parameters.Add(new SqlParameter("@cBusCode", iBaseCodeLen));
  1162. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  1163. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen1));
  1164. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  1165. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  1166. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID1));
  1167. cmd.Parameters.Add(new SqlParameter("@bredvouch", "0"));
  1168. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode1));
  1169. cmd.CommandText = sql;
  1170. try
  1171. {
  1172. int count = cmd.ExecuteNonQuery();
  1173. if (count <= 0)
  1174. {
  1175. log.Error("生成成品入库单表体失败,受影响行数<=0;");
  1176. throw new Exception("生成成品入库单表体失败,受影响行数<=0;");
  1177. }
  1178. }
  1179. catch (Exception ex)
  1180. {
  1181. log.Error("生成成品入库单表体失败!SQL:\r\n" + sql, ex);
  1182. throw new Exception("生成成品入库单表体失败!SQL:\r\n" + sql, ex);
  1183. }
  1184. item.ID = iFatherId1;
  1185. #endregion
  1186. }
  1187. }
  1188. #region 采购入库单 表体
  1189. foreach (ICSOutsourcingReceiveDocs body in head.details)
  1190. {
  1191. sql = @"SELECT b.cCode,a.iVouchRowNo FROM dbo.OM_MODetails a INNER JOIN
  1192. dbo.OM_MOMain b ON b.MoId = a.MoId WHERE a.MODetailsID = '" + body.OODetailID + "'";
  1193. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  1194. if (dtCheck != null && dtCheck.Rows.Count > 0)
  1195. {
  1196. mocode = dtCheck.Rows[0]["cCode"].ToString();
  1197. seq = dtCheck.Rows[0]["iVouchRowNo"].ToString();
  1198. sql = @" select
  1199. count(c.MOMaterialsID) num,d.number
  1200. from DBO.OM_MOMain a
  1201. left join DBO.OM_MODetails b on a.moid = b.moid
  1202. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  1203. left join(select MODetailsID,COUNT(MOMaterialsID) number from DBO.OM_MOMaterials where iWIPtype=1 GROUP BY MODetailsID
  1204. ) d on b.MODetailsID = d.MODetailsID
  1205. where a.cCode='{0}' and b.iVouchRowNo='{1}'
  1206. GROUP BY d.number ";
  1207. sql = string.Format(sql, mocode, seq);
  1208. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  1209. if (dtCheck != null && dtCheck.Rows.Count > 0)
  1210. {
  1211. if (dtCheck.Rows[0]["num"].ToString() != dtCheck.Rows[0]["number"].ToString())
  1212. {
  1213. isif = 1;
  1214. }
  1215. else
  1216. {
  1217. isif = 0;
  1218. }
  1219. }
  1220. }
  1221. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  1222. {
  1223. if (body.BatchCode == "")
  1224. {
  1225. log.Error("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  1226. throw new Exception("该物料:" + body.InvCode + "已启用批次,请重新填写!");
  1227. }
  1228. }
  1229. else
  1230. {
  1231. if (body.BatchCode != "")
  1232. {
  1233. log.Error("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  1234. throw new Exception("该物料:" + body.InvCode + "未启用批次,请重新填写!");
  1235. }
  1236. }
  1237. moidinbatch = iChildId;
  1238. if (head.ODNCode != "")
  1239. {
  1240. sql = @"INSERT INTO dbo.rdrecords01
  1241. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,iNum,
  1242. iFlag ,iSQuantity ,iSNum ,iMoney ,
  1243. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  1244. cItem_class ,cItemCode ,iOMoDID ,fACost ,cName ,iNQuantity ,
  1245. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  1246. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  1247. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode ,
  1248. iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost,
  1249. cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 )
  1250. SELECT @AutoID,@ID1,@cInvCode,@iQuantity,0,0,0,@iNum,
  1251. 0,0,0,0,
  1252. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  1253. a.cItem_class,a.cItemCode,a.MODetailsID,0,a.cItemName,a.iQuantity,
  1254. @VenCode,(select autoid from PU_ArrivalVouchs a left join PU_ArrivalVouch b on a.ID=b.ID where b.cCode='" + head.ODNCode + @"' and a.ivouchrowno='" + body.ODNSequence + @"'),0,0,0,0,
  1255. 0,d.nflat,0,0,1,'',0,
  1256. 0,0,0,1,'',
  1257. 0,0,0,@irowno,0,@cBatch,a.iNatUnitPrice *convert(decimal, @iQuantity),a.iNatUnitPrice ,
  1258. @cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  1259. FROM dbo.OM_MODetails a
  1260. INNER JOIN dbo.OM_MOMain d ON a.MOID=d.MOID
  1261. where a.MODetailsID='" + body.OODetailID + @"'
  1262. ";
  1263. }
  1264. else
  1265. {
  1266. sql = @"INSERT INTO dbo.rdrecords01
  1267. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,iNum,
  1268. iFlag ,iSQuantity ,iSNum ,iMoney ,
  1269. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  1270. cItem_class ,cItemCode ,iOMoDID ,fACost ,cName ,iNQuantity ,
  1271. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  1272. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  1273. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode ,
  1274. iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost,
  1275. cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 )
  1276. SELECT @AutoID,@ID1,@cInvCode,@iQuantity,0,0,0,@iNum,
  1277. 0,0,0,0,
  1278. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  1279. a.cItem_class,@cItemCode,a.MODetailsID,0,a.cItemName,a.iQuantity,
  1280. @VenCode,'',0,0,0,0,
  1281. 0,d.nflat,0,0,1,'',0,
  1282. 0,0,0,1,'',
  1283. 0,0,0,@irowno,0,@cBatch,a.iNatUnitPrice *convert(decimal, @iQuantity),a.iNatUnitPrice ,
  1284. @cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  1285. FROM dbo.OM_MODetails a
  1286. INNER JOIN dbo.OM_MOMain d ON a.MOID=d.MOID
  1287. where a.MODetailsID='" + body.OODetailID + @"'
  1288. ";
  1289. }
  1290. cmd.Parameters.Clear();
  1291. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  1292. cmd.Parameters.Add(new SqlParameter("@ID1", iFatherId));
  1293. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  1294. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  1295. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount.ToString("0.00")));
  1296. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  1297. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  1298. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  1299. cmd.Parameters.Add(new SqlParameter("@VenCode", head.VenCode));
  1300. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  1301. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  1302. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  1303. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  1304. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  1305. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  1306. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  1307. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  1308. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  1309. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  1310. cmd.CommandText = sql;
  1311. try
  1312. {
  1313. int count = cmd.ExecuteNonQuery();
  1314. if (count <= 0)
  1315. {
  1316. log.Error("委外采购入库单表体失败,受影响行数<=0;");
  1317. throw new Exception("委外采购入库单表体失败,受影响行数<=0;");
  1318. }
  1319. }
  1320. catch (Exception ex)
  1321. {
  1322. log.Error("委外采购入库单表体失败!SQL:\r\n" + sql, ex);
  1323. throw new Exception("委外采购入库单表体失败!SQL:\r\n" + sql, ex);
  1324. }
  1325. if (isif == 1)
  1326. {
  1327. if (head.CompleteVerification)
  1328. {
  1329. #region 齐套检验
  1330. sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' ";
  1331. DataTable qt = DBHelper.SQlReturnData(sql, cmd);
  1332. if (qt != null && qt.Rows.Count > 0)
  1333. {
  1334. if (qt.Rows[0]["cValue"].ToString() == "1")
  1335. {
  1336. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  1337. (
  1338. select
  1339. a.cCode,b.iVouchRowNo,
  1340. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  1341. from DBO.OM_MOMain a
  1342. left join DBO.OM_MODetails b on a.moid = b.moid
  1343. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  1344. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  1345. where a.cCode='{0}' and b.iVouchRowNo='{1}'
  1346. and c.iQuantity<>0 and c.iWIPtype<>1
  1347. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  1348. ) tt
  1349. group by cCode,iVouchRowNo";
  1350. sql = string.Format(sql, mocode, seq);
  1351. qt = DBHelper.SQlReturnData(sql, cmd);
  1352. if (qt == null || qt.Rows.Count == 0)
  1353. {
  1354. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  1355. }
  1356. else
  1357. {
  1358. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  1359. {
  1360. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料<=0!");
  1361. }
  1362. }
  1363. }
  1364. else if (qt.Rows[0]["cValue"].ToString() == "2")
  1365. {
  1366. sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'";
  1367. qt = DBHelper.SQlReturnData(sql, cmd);
  1368. if (qt != null && qt.Rows.Count > 0)
  1369. {
  1370. if (qt.Rows[0]["cValue"].ToString() == "true")
  1371. {
  1372. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  1373. (
  1374. select
  1375. a.cCode,b.iVouchRowNo,
  1376. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  1377. from DBO.OM_MOMain a
  1378. left join DBO.OM_MODetails b on a.moid = b.moid
  1379. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  1380. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  1381. where a.cCode='{0}' and b.iVouchRowNo='{1}'
  1382. and c.iQuantity<>0 and c.iWIPtype<>1
  1383. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  1384. ) tt
  1385. group by cCode,iVouchRowNo";
  1386. sql = string.Format(sql, mocode, seq);
  1387. qt = DBHelper.SQlReturnData(sql, cmd);
  1388. if (qt == null || qt.Rows.Count == 0)
  1389. {
  1390. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  1391. }
  1392. else
  1393. {
  1394. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  1395. {
  1396. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  1397. }
  1398. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity <= 0)
  1399. {
  1400. throw new Exception("该物料:" + body.InvCode + @"入库数量大于关键物料领料套数!");
  1401. }
  1402. }
  1403. }
  1404. else
  1405. {
  1406. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  1407. (
  1408. select
  1409. a.cCode,b.iVouchRowNo,
  1410. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  1411. from DBO.OM_MOMain a
  1412. left join DBO.OM_MODetails b on a.moid = b.moid
  1413. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  1414. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  1415. where a.cCode='{0}' and b.iVouchRowNo='{1}'
  1416. and c.iQuantity<>0 and c.iWIPtype<>1
  1417. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  1418. ) tt
  1419. group by cCode,iVouchRowNo";
  1420. sql = string.Format(sql, mocode, seq);
  1421. qt = DBHelper.SQlReturnData(sql, cmd);
  1422. if (qt == null || qt.Rows.Count == 0)
  1423. {
  1424. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  1425. }
  1426. else
  1427. {
  1428. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  1429. {
  1430. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  1431. }
  1432. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity <= 0)
  1433. {
  1434. throw new Exception("该物料:" + body.InvCode + @"入库数量大于领料套数!");
  1435. }
  1436. }
  1437. }
  1438. }
  1439. }
  1440. }
  1441. #endregion
  1442. }
  1443. }
  1444. //OM_MODetails
  1445. sql = @"UPDATE a SET freceivedqty=ISNULL(freceivedqty,0)+" + body.Quantity + @"
  1446. FROM dbo.OM_MODetails a
  1447. WHERE a.MODetailsID='" + body.OODetailID + "'";
  1448. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  1449. sql = @"select isnull(freceivedqty,0)fOutQuantity,isnull(iQuantity,0)iQuantity,ISNULL(iNum, 0)iNum,ISNULL(NiNum, 0)NiNum,ISNULL(iNum, 0)-ISNULL(NiNum, 0) SiNum from OM_MODetails a
  1450. LEFT JOIN (select sum(iNum) NiNum,iOMoDID from rdrecords01 GROUP BY iOMoDID) b on a.MODetailsID=b.iOMoDID
  1451. where a.MODetailsID='" + body.OODetailID + "'";
  1452. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  1453. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  1454. {
  1455. foreach (DataRow item in dtDisCheck.Rows)
  1456. {
  1457. if (Convert.ToDecimal(item["fOutQuantity"].ToString()) == Convert.ToDecimal(item["iQuantity"].ToString()))
  1458. {
  1459. if (Convert.ToDecimal(item["iNum"].ToString()) != 0)
  1460. {
  1461. if (Convert.ToDecimal(item["SiNum"].ToString()) != 0)
  1462. {
  1463. sql = @"Update rdrecords01 set iNum=isnull(iNum,0)+(" + item["SiNum"].ToString() + ") where autoid='" + iChildId + "' ";
  1464. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item["SiNum"].ToString()));
  1465. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  1466. }
  1467. }
  1468. }
  1469. }
  1470. }
  1471. //更新待入库数量
  1472. sql = "UPDATE dbo.CurrentStock SET fInQuantity=ISNULL(fInQuantity,0)-" + body.Quantity + " WHERE cInvCode='" + body.InvCode + "' AND cWhCode ='" + head.WHCode + "'";
  1473. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  1474. {
  1475. if (!string.IsNullOrWhiteSpace(body.BatchCode))
  1476. {
  1477. sql += " and cBatch='" + body.BatchCode + "'";
  1478. }
  1479. }
  1480. else
  1481. {
  1482. sql += " and cBatch=''";
  1483. }
  1484. DBHelper.CmdExecuteNonQuery(sql, cmd, "更新待入库数量失败!");
  1485. //if (isEnable != 0)
  1486. //{
  1487. // key.cBustypeUN = "委外加工";
  1488. // key.cVouchTypeUN = "01";
  1489. // key.TableName = "IA_ST_UnAccountVouch01";
  1490. // //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  1491. // DBHelper.UpdateCurrentStockNEWDG(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, gys, key);
  1492. //}
  1493. //else
  1494. //{
  1495. // //更新现存量
  1496. // key.cBustypeUN = "委外加工";
  1497. // key.cVouchTypeUN = "01";
  1498. // key.TableName = "IA_ST_UnAccountVouch01";
  1499. // //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  1500. // 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);
  1501. //}
  1502. key.cBustypeUN = "委外加工";
  1503. key.cVouchTypeUN = "01";
  1504. key.TableName = "IA_ST_UnAccountVouch01";
  1505. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, body.Quantity, body.Amount, body.cFree1,
  1506. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  1507. "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  1508. num = body.detailss.Count();
  1509. Dictionary<string, int> dic2 = DBHelper.GetAllCode("" + ERPDB + "", surface1, "" + num + "", head.WorkPoint);
  1510. int iFatherId2 = Convert.ToInt32(dic2["iFatherId"].ToString());
  1511. int iChildId2 = Convert.ToInt32(dic2["iChildId"].ToString());
  1512. irownnew = 0;
  1513. foreach (ICSOutsourcingReceiveDocss item in body.detailss)
  1514. {
  1515. if (item.PickID != null && item.PickID != "")
  1516. {
  1517. foreach (WH wh in LWH)
  1518. {
  1519. if (wh.WHCODE == item.WHCode)
  1520. {
  1521. out11 = wh.ID;
  1522. iseq = wh.seq + 1;
  1523. wh.seq += 1;
  1524. }
  1525. }
  1526. irownnew++;
  1527. sql = @"INSERT INTO dbo.rdrecords11
  1528. ( AutoID,ID ,cInvCode,iQuantity ,cBatch ,iNum,
  1529. iFlag ,cItemCode ,cName ,iNQuantity ,iMPoIds ,
  1530. bLPUseFree ,iOriTrackID ,bCosting ,bVMIUsed ,cmocode ,
  1531. invcode ,imoseq ,iopseq ,iExpiratDateCalcu ,iorderdid ,
  1532. iordertype ,isotype ,ipesodid ,ipesotype ,cpesocode ,
  1533. ipesoseq ,irowno ,bcanreplace ,iposflag,iOMoDID,
  1534. iOMoMID,comcode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  1535. SELECT distinct
  1536. @AutoID,@ID,a.cInvCode,@iQuantity,@cBatch,@iNum,
  1537. 0,@cItemCode,null,@iQuantity,NULL,
  1538. 0,0,1,0,NULL,
  1539. b.cInvCode,NULL,NULL,0,0,
  1540. 0,0,a.MOMaterialsID,'6',c.cCode,
  1541. NULL, @irowno,0,NULL,b.MODetailsID,
  1542. a.MOMaterialsID,c.cCode,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  1543. FROM dbo.OM_MOMaterials a
  1544. INNER JOIN dbo.OM_MODetails b ON a.MoDetailsID=b.MODetailsID
  1545. INNER JOIN dbo.OM_MOMain c ON b.MOID=c.MOID
  1546. WHERE a.MOMaterialsID='" + item.PickID + "'";
  1547. cmd.Parameters.Clear();
  1548. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId2));
  1549. cmd.Parameters.Add(new SqlParameter("@ID", out11));
  1550. cmd.Parameters.Add(new SqlParameter("@iQuantity", item.Quantity));
  1551. cmd.Parameters.Add(new SqlParameter("@iNum", item.Amount));
  1552. cmd.Parameters.Add(new SqlParameter("@cBatch", item.BatchCode));
  1553. cmd.Parameters.Add(new SqlParameter("@cItemCode", item.ProjectCode));
  1554. cmd.Parameters.Add(new SqlParameter("@irowno", iseq));
  1555. cmd.Parameters.Add(new SqlParameter("@cFree1", item.cFree1));
  1556. cmd.Parameters.Add(new SqlParameter("@cFree2", item.cFree2));
  1557. cmd.Parameters.Add(new SqlParameter("@cFree3", item.cFree3));
  1558. cmd.Parameters.Add(new SqlParameter("@cFree4", item.cFree4));
  1559. cmd.Parameters.Add(new SqlParameter("@cFree5", item.cFree5));
  1560. cmd.Parameters.Add(new SqlParameter("@cFree6", item.cFree6));
  1561. cmd.Parameters.Add(new SqlParameter("@cFree7", item.cFree7));
  1562. cmd.Parameters.Add(new SqlParameter("@cFree8", item.cFree8));
  1563. cmd.Parameters.Add(new SqlParameter("@cFree9", item.cFree9));
  1564. cmd.Parameters.Add(new SqlParameter("@cFree10", item.cFree10));
  1565. cmd.CommandText = sql;
  1566. try
  1567. {
  1568. int count = cmd.ExecuteNonQuery();
  1569. if (count <= 0)
  1570. {
  1571. log.Error("生成材料出库单表体失败,受影响行数<=0;");
  1572. throw new Exception("生成材料出库单表体失败,受影响行数<=0;");
  1573. }
  1574. }
  1575. catch (Exception ex)
  1576. {
  1577. log.Error("生成材料出库单表体失败" + sql, ex);
  1578. throw new Exception("生成材料出库单表体失败 " + sql, ex);
  1579. }
  1580. #region 回写委外子件已领数量
  1581. sql = "update OM_MOMaterials set iSendQTY=isnull(iSendQTY,0)+" + item.Quantity + ",fsendapplyqty=isnull(fsendapplyqty,0)+(" + item.Quantity + ") where MOMaterialsID='" + item.PickID + "' ";
  1582. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写委外子件已领数量失败!");
  1583. #endregion
  1584. sql = @"select isnull(iSendQTY,0)fOutQuantity,isnull(iQuantity,0)iQuantity,ISNULL(iNum, 0)iNum,ISNULL(NiNum, 0)NiNum,ISNULL(iNum, 0)-ISNULL(NiNum, 0) SiNum from OM_MOMaterials a
  1585. LEFT JOIN (select sum(iNum) NiNum,ipesodid from rdrecords11 GROUP BY ipesodid) b on a.MOMaterialsID=b.ipesodid
  1586. where a.MOMaterialsID='" + item.PickID + "'";
  1587. dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  1588. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  1589. {
  1590. foreach (DataRow item1 in dtDisCheck.Rows)
  1591. {
  1592. if (Convert.ToDecimal(item1["fOutQuantity"].ToString()) == Convert.ToDecimal(item1["iQuantity"].ToString()))
  1593. {
  1594. if (Convert.ToDecimal(item1["iNum"].ToString()) != 0)
  1595. {
  1596. if (Convert.ToDecimal(item1["SiNum"].ToString()) != 0)
  1597. {
  1598. sql = @"Update rdrecords11 set iNum=isnull(iNum,0)+(" + item1["SiNum"].ToString() + ") where autoid='" + iChildId2 + "' ";
  1599. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item1["SiNum"].ToString()));
  1600. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  1601. }
  1602. }
  1603. }
  1604. }
  1605. }
  1606. //更新现存量
  1607. key.cBustypeUN = "领料";
  1608. key.cVouchTypeUN = "11";
  1609. key.TableName = "IA_ST_UnAccountVouch11";
  1610. //DBHelper.UpdateCurrentStockNEW(cmd, item.InvCode, head.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);
  1611. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, -body.Quantity, -body.Amount, body.cFree1,
  1612. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  1613. "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, out11, iChildId2);
  1614. iFatherIdTree += "'" + out11 + "',";
  1615. iChildId2--;
  1616. }
  1617. }
  1618. iFatherIdTwo = "'" + iFatherId + "',";
  1619. iChildId--;
  1620. }
  1621. #endregion
  1622. #region 查询
  1623. if (news == 0)
  1624. {
  1625. sql = @" select DISTINCT a.ID as ID,a.ID as IDs,a.cCode as ORCVTCode ,a.cVenCode,d.cVenName,a.cOrderCode as OOCode,
  1626. a.cMaker as CreateUser,dnmaketime as CreateDateTime,cHandler as Checker,dnverifytime as CheckDateTime
  1627. from rdrecord01 a
  1628. left join Department c on a.cDepCode=c.cDepCode
  1629. left join Vendor d on a.cVenCode=d.cVenCode
  1630. where A.ID in ({0})
  1631. select DISTINCT a.ID as IDs,b.AutoID as DetailID,b.irowno as Sequence,b.cInvCode as InvCode,b.iQuantity as Quantity,iNum as Amount,b.iOMoDID as OODetailID,CAST(b.AutoID as char) IDSS
  1632. ,isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  1633. isnull(b.cFree1,'') as cFree1,
  1634. isnull(b.cFree2,'') as cFree2,
  1635. isnull(b.cFree3,'') as cFree3,
  1636. isnull(b.cFree4,'') as cFree4,
  1637. isnull(b.cFree5,'') as cFree5,
  1638. isnull(b.cFree6,'') as cFree6,
  1639. isnull(b.cFree7,'') as cFree7,
  1640. isnull(b.cFree8,'') as cFree8,
  1641. isnull(b.cFree9,'') as cFree9,
  1642. isnull(b.cFree10,'') as cFree10
  1643. from rdrecord01 a
  1644. left join rdrecords01 b on a.ID=b.ID
  1645. left join Department c on a.cDepCode=c.cDepCode
  1646. left join Vendor d on a.cVenCode=d.cVenCode
  1647. where A.ID in ({0})
  1648. select DISTINCT a.ID as IDs,b.AutoID as DetailID, h.MOMaterialsID as SourceDetailID,b.irowno as Sequence ,a.cCode as MRCVCode,b.iordercode,b.cInvCode,CAST('{2}' as char) IDSS,b.iQuantity,b.iNum
  1649. ,isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  1650. isnull(b.cFree1,'') as cFree1,
  1651. isnull(b.cFree2,'') as cFree2,
  1652. isnull(b.cFree3,'') as cFree3,
  1653. isnull(b.cFree4,'') as cFree4,
  1654. isnull(b.cFree5,'') as cFree5,
  1655. isnull(b.cFree6,'') as cFree6,
  1656. isnull(b.cFree7,'') as cFree7,
  1657. isnull(b.cFree8,'') as cFree8,
  1658. isnull(b.cFree9,'') as cFree9,
  1659. isnull(b.cFree10,'') as cFree10
  1660. from rdrecord11 a
  1661. left join rdrecords11 b on a.ID=b.ID
  1662. left join Department c on a.cDepCode=c.cDepCode
  1663. left join Warehouse d on a.cWhCode=d.cWhCode
  1664. left join OM_MOMaterials h on b.iOMoMID=h.MOMaterialsID
  1665. where a.ID in ({1})
  1666. ";
  1667. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','), "''", moidinbatch);
  1668. if (ds != null)
  1669. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  1670. else
  1671. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  1672. }
  1673. else
  1674. {
  1675. sql = @" select DISTINCT a.ID as ID,a.ID as IDs,a.cCode as ORCVTCode ,a.cVenCode,d.cVenName,a.cOrderCode as OOCode,
  1676. a.cMaker as CreateUser,dnmaketime as CreateDateTime,cHandler as Checker,dnverifytime as CheckDateTime
  1677. from rdrecord01 a
  1678. left join Department c on a.cDepCode=c.cDepCode
  1679. left join Vendor d on a.cVenCode=d.cVenCode
  1680. where A.ID in ({0})
  1681. select DISTINCT a.ID as IDs,b.AutoID as DetailID,b.irowno as Sequence,b.cInvCode as InvCode,CAST(b.AutoID as char) IDSS,b.iQuantity as Quantity,
  1682. iNum as Amount,b.iOMoDID as OODetailID,isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand,
  1683. isnull(b.cFree1,'') as cFree1,isnull(b.cFree2,'') as cFree2,isnull(b.cFree3,'') as cFree3,
  1684. isnull(b.cFree4,'') as cFree4,isnull(b.cFree5,'') as cFree5,isnull(b.cFree6,'') as cFree6,
  1685. isnull(b.cFree7,'') as cFree7,isnull(b.cFree8,'') as cFree8,isnull(b.cFree9,'') as cFree9,
  1686. isnull(b.cFree10,'') as cFree10
  1687. from rdrecord01 a
  1688. left join rdrecords01 b on a.ID=b.ID
  1689. left join Department c on a.cDepCode=c.cDepCode
  1690. left join Vendor d on a.cVenCode=d.cVenCode
  1691. where A.ID in ({0})
  1692. select DISTINCT a.ID as IDs,b.AutoID as DetailID, h.MOMaterialsID as SourceDetailID,b.irowno as Sequence,a.cCode as MRCVCode ,b.iordercode,
  1693. CAST('{2}' as char) IDSS,b.cInvCode,b.iQuantity,b.iNum,a.ID ERPID,b.AutoID as ERPDetailID,isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,
  1694. '' version ,'' brand,isnull(b.cFree1,'') as cFree1,isnull(b.cFree2,'') as cFree2,isnull(b.cFree3,'') as cFree3,isnull(b.cFree4,'') as cFree4,
  1695. isnull(b.cFree5,'') as cFree5,isnull(b.cFree6,'') as cFree6,isnull(b.cFree7,'') as cFree7,isnull(b.cFree8,'') as cFree8,isnull(b.cFree9,'') as cFree9,
  1696. isnull(b.cFree10,'') as cFree10
  1697. from rdrecord11 a
  1698. left join rdrecords11 b on a.ID=b.ID
  1699. left join Department c on a.cDepCode=c.cDepCode
  1700. left join Warehouse d on a.cWhCode=d.cWhCode
  1701. left join OM_MOMaterials h on b.iOMoMID=h.MOMaterialsID
  1702. where a.ID in ({1})
  1703. "; if (iFatherIdTree.TrimEnd(',')=="")
  1704. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','), "''", moidinbatch);
  1705. else
  1706. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','), iFatherIdTree.TrimEnd(','), moidinbatch);
  1707. if (ds != null)
  1708. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  1709. else
  1710. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  1711. }
  1712. #endregion
  1713. if (news != 0)
  1714. {
  1715. msg = JSON.DataSetToJson(ds, "details", "IDs", "detailss", "IDSS");
  1716. }
  1717. else
  1718. {
  1719. msg = JSON.DataSetToJson(ds, "details", "IDs");
  1720. }
  1721. }
  1722. cmd.Transaction.Commit();
  1723. }
  1724. catch (Exception ex)
  1725. {
  1726. cmd.Transaction.Rollback();
  1727. log.Error(ex.Message);
  1728. throw new Exception(ex.Message);
  1729. }
  1730. finally
  1731. {
  1732. if (conn.State == ConnectionState.Open)
  1733. {
  1734. conn.Close();
  1735. }
  1736. conn.Dispose();
  1737. }
  1738. }
  1739. return msg;
  1740. }
  1741. /// <summary>
  1742. /// 审核委外采购入库
  1743. /// </summary>
  1744. /// <param name="infos"></param>
  1745. /// <returns></returns>
  1746. public string Approve(List<ICSOutsourcingReceiveDoc> infos)
  1747. {
  1748. List<ICSSalesShipmentDoc> szJson = new List<ICSSalesShipmentDoc>();
  1749. string json = "";
  1750. DataTable dtNew = null;
  1751. string connS = "";
  1752. if (infos.Count <= 0)
  1753. {
  1754. throw new Exception("传送数据为空!");
  1755. }
  1756. string res = string.Empty;
  1757. SqlConnection conn = new SqlConnection();
  1758. SqlCommand cmd = new SqlCommand();
  1759. string sql = string.Empty;
  1760. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  1761. foreach (string WorkPoint in result)
  1762. {
  1763. try
  1764. {
  1765. connS = string.Format(connString, WorkPoint);
  1766. conn = new System.Data.SqlClient.SqlConnection(connS);
  1767. conn.Open();
  1768. SqlTransaction sqlTran = conn.BeginTransaction();
  1769. cmd = new SqlCommand();
  1770. cmd.Transaction = sqlTran;
  1771. cmd.Connection = conn;
  1772. foreach (ICSOutsourcingReceiveDoc info in infos)
  1773. {
  1774. if (WorkPoint != info.WorkPoint)
  1775. continue;
  1776. ICSUserInfo userInfo = new ICSUserInfo();
  1777. userInfo = DBHelper.GetPersonInfo(info.User, cmd);
  1778. if (info.MTime < new DateTime(2000, 01, 01))
  1779. throw new Exception("请输入正确的操作时间:" + info.MTime);
  1780. sql = @"UPDATE RdRecord01 SET cHandler ='" + userInfo.UserName + @"' ,
  1781. dnverifytime=CONVERT(VARCHAR(50),GETDATE(),112),dVeriDate=GETDATE() WHERE ID='{0}'";
  1782. sql = string.Format(sql, info.ID);
  1783. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核委外采购入库单失败!");
  1784. }
  1785. cmd.Transaction.Commit();
  1786. }
  1787. catch (Exception ex)
  1788. {
  1789. cmd.Transaction.Rollback();
  1790. log.Error(ex.Message);
  1791. throw new Exception(ex.Message);
  1792. }
  1793. finally
  1794. {
  1795. if (conn.State == ConnectionState.Open)
  1796. {
  1797. conn.Close();
  1798. }
  1799. conn.Dispose();
  1800. }
  1801. }
  1802. return json;
  1803. }
  1804. /// <summary>
  1805. /// 删除委外采购入库单
  1806. /// </summary>
  1807. /// <param name="infos"></param>
  1808. /// <returns></returns>
  1809. public string Delete(List<ICSOutsourcingReceiveDoc> infos)
  1810. {
  1811. string connS = "";
  1812. List<ICSSalesShipmentDoc> szJson = new List<ICSSalesShipmentDoc>();
  1813. if (infos.Count <= 0)
  1814. {
  1815. throw new Exception("传送数据为空!");
  1816. }
  1817. string res = string.Empty;
  1818. SqlConnection conn = new SqlConnection();
  1819. SqlCommand cmd = new SqlCommand();
  1820. string sql = string.Empty;
  1821. List<string> result = infos.Select(t => t.WorkPoint).Distinct().ToList();
  1822. foreach (string WorkPoint in result)
  1823. {
  1824. try
  1825. {
  1826. connS = string.Format(connString, WorkPoint);
  1827. conn = new System.Data.SqlClient.SqlConnection(connS);
  1828. conn.Open();
  1829. SqlTransaction sqlTran = conn.BeginTransaction();
  1830. cmd = new SqlCommand();
  1831. cmd.Transaction = sqlTran;
  1832. cmd.Connection = conn;
  1833. foreach (ICSOutsourcingReceiveDoc info in infos)
  1834. {
  1835. if (WorkPoint != info.WorkPoint)
  1836. continue;
  1837. if (info.MTime < new DateTime(2000, 01, 01))
  1838. throw new Exception("请输入正确的操作时间:" + info.MTime);
  1839. sql = @"delete RdRecord01 where RdRecord01.ID='" + info.ID + "'";
  1840. sql += @"delete RdRecords01 where RdRecords01.ID='" + info.ID + "'";
  1841. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  1842. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  1843. // WHERE a.ID='" + info.ID + "'";
  1844. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除委外采购入库单失败!");
  1845. }
  1846. cmd.Transaction.Commit();
  1847. }
  1848. catch (Exception ex)
  1849. {
  1850. cmd.Transaction.Rollback();
  1851. log.Error(ex.Message);
  1852. throw new Exception(ex.Message);
  1853. }
  1854. finally
  1855. {
  1856. if (conn.State == ConnectionState.Open)
  1857. {
  1858. conn.Close();
  1859. }
  1860. conn.Dispose();
  1861. }
  1862. }
  1863. return res;
  1864. }
  1865. /// <summary>
  1866. ///
  1867. /// 记录日志
  1868. /// </summary>
  1869. /// <param name="Bills"></param>
  1870. private void LogInfo(List<ICSOutsourcingReceiveDoc> Bills)
  1871. {
  1872. string HeadList = string.Empty;
  1873. string BodyList = string.Empty;
  1874. foreach (ICSOutsourcingReceiveDoc head in Bills)
  1875. {
  1876. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  1877. foreach (ICSOutsourcingReceiveDocs body in head.details)
  1878. {
  1879. BodyList += "\r\n 表体主键ID: " + body.ODNDetailID + ",数量:" + body.Quantity;
  1880. }
  1881. }
  1882. log.Info(HeadList);
  1883. log.Info(BodyList);
  1884. }
  1885. public string GetSerialCode(string ID, string connS)
  1886. {
  1887. string sql = "EXEC Addins_GetSerialCode '{0}'";
  1888. sql = string.Format(sql, new object[] { ID });
  1889. return DBHelper.ExecuteScalar(CommandType.Text, sql, connS).ToString();
  1890. }
  1891. }
  1892. }