爱思开
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.

2162 lines
120 KiB

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