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

2148 lines
119 KiB

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