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

1597 lines
83 KiB

3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 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
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 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
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
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
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
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
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
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
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. using ICSSoft.Common;
  2. using ICSSoft.Entity;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ICSSoft.DataProject
  12. {
  13. /// <summary>
  14. /// 委外采购入库单
  15. /// </summary>
  16. public class 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. foreach (ICSOutsourcingReceiveDoc info in infos)
  45. {
  46. try
  47. {
  48. connS = string.Format(connString, info.WorkPoint);
  49. conn = new System.Data.SqlClient.SqlConnection(connS);
  50. conn.Open();
  51. SqlTransaction sqlTran = conn.BeginTransaction();
  52. cmd = new SqlCommand();
  53. cmd.Transaction = sqlTran;
  54. cmd.Connection = conn;
  55. if (info.MTime < new DateTime(2000, 01, 01))
  56. throw new Exception("请输入正确的操作时间:" + info.MTime);
  57. sql = @" select a.ID,iMPoIds,iordercode,a.cCode,a.cDepCode,c.cDepName,a.cWhCode,
  58. d.cWhName,a.cSource,a.cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,
  59. b.AutoID ,b.irowno,b.cInvCode,b.iQuantity,e.MoDetailsID,b.iNum,e.csocode
  60. from rdrecord11 a
  61. left join rdrecords11 b on a.ID=b.ID
  62. left join Department c on a.cDepCode=c.cDepCode
  63. left join Warehouse d on a.cWhCode=d.cWhCode
  64. left join OM_MODetails e on b.iOMoDID=e.MoDetailsID
  65. where a.cBusType=''";
  66. if (!string.IsNullOrWhiteSpace(info.ORCVTCode))
  67. {
  68. sql += " and a.cCode='{0}'";
  69. }
  70. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  71. {
  72. sql += " and ISNULL(a.dnmodifytime,ISNULL(a.dnverifytime, ISNULL(a.dnmodifytime, a.dnmaketime)))>='{1}'";
  73. }
  74. if (!string.IsNullOrWhiteSpace(info.User))
  75. {
  76. sql += "and a.CMAKER='{2}'";
  77. }
  78. sql = string.Format(sql, info.ORCVTCode, info.MTime, info.User);
  79. dt = DBHelper.SQlReturnData(sql, cmd);
  80. if (dt.Rows.Count <= 0 || dt == null)
  81. throw new Exception("委外发料单:" + info.ORCVTCode + ",无信息!");
  82. if (dtNew == null)
  83. dtNew = dt;
  84. else
  85. dtNew.Merge(dt);
  86. cmd.Transaction.Commit();
  87. }
  88. catch (Exception ex)
  89. {
  90. cmd.Transaction.Rollback();
  91. log.Error(ex.Message);
  92. throw new Exception(ex.Message);
  93. }
  94. finally
  95. {
  96. if (conn.State == ConnectionState.Open)
  97. {
  98. conn.Close();
  99. }
  100. conn.Dispose();
  101. }
  102. }
  103. json = JsonConvert.SerializeObject(dtNew);
  104. return json;
  105. }
  106. /// <summary>
  107. /// 创建委外采购入(到货单)
  108. /// </summary>
  109. /// <param name="Bills"></param>
  110. /// <returns></returns>
  111. public string CreateOutsourcingReceiveDocByPOArrive(List<ICSOutsourcingReceiveDoc> Bills)
  112. {
  113. string msg = "";
  114. DataTable dtNew1 = null;
  115. DataTable dtNew2 = null;
  116. string connS = "";
  117. string sql = "";
  118. string iFatherIdTwo = "";
  119. int num = 0;
  120. int isEnable = 0;
  121. string gys = "";
  122. //取得out库单的默认显示模版
  123. //取得采购入库单单据 表头ID,表体DID
  124. VouchKey key = new VouchKey();
  125. SqlConnection conn = new SqlConnection();
  126. SqlCommand cmd = new SqlCommand();
  127. if (Bills.Count <= 0)
  128. {
  129. throw new Exception("传送数据为空!");
  130. }
  131. LogInfo(Bills);
  132. foreach (ICSOutsourcingReceiveDoc head in Bills)
  133. {
  134. try
  135. {
  136. num = head.details.Count();
  137. connS = string.Format(connString, head.WorkPoint);
  138. conn = new System.Data.SqlClient.SqlConnection(connS);
  139. conn.Open();
  140. SqlTransaction sqlTran = conn.BeginTransaction();
  141. cmd = new SqlCommand();
  142. cmd.Transaction = sqlTran;
  143. cmd.Connection = conn;
  144. if (DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  145. {
  146. }
  147. else
  148. {
  149. throw new Exception("U8正在整理现存量,请稍后再试");
  150. }
  151. string[] ss = head.WorkPoint.Split('_');
  152. ERPDB = ss[1];
  153. string[] dd = CRNALL.Split('~');
  154. string crdname = dd[0];
  155. string carname = dd[1];
  156. string surface = dd[2];
  157. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  158. //取得out库单的默认显示模版
  159. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  160. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  161. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  162. DateTime date = DateTime.Now;
  163. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", head.WorkPoint);
  164. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  165. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  166. #region 验证数量不能大于到货单数量
  167. //foreach (ICSOutsourcingReceiveDocs body in head.details)
  168. // {
  169. // sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNDetailID + "'";
  170. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  171. // if (dtQty != null && dtQty.Rows.Count > 0)
  172. // {
  173. // //到货单可入数量
  174. // decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  175. // sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNDetailID + "'";
  176. // DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  177. // decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  178. // if (ReceQty + body.Quantity > PUQty)
  179. // {
  180. // throw new Exception("入库数量大于当前到货单行可入库数量!");
  181. // }
  182. // }
  183. // else
  184. // {
  185. // throw new Exception("U8不存在该到货单行!");
  186. // }
  187. // }
  188. #endregion
  189. if (head.Enable == true)
  190. {
  191. sql = @"SELECT MOID,cVenCode FROM OM_MOMain WHERE cBusType='代管采购' and cCode='" + head.OOCode + "' ";
  192. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  193. if (dtCheck != null && dtCheck.Rows.Count > 0)
  194. {
  195. gys = dtCheck.Rows[0][1].ToString();
  196. isEnable = 0;
  197. sql = @"SELECT cWhCode FROM Warehouse WHERE bProxyWh ='1' ";
  198. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  199. if (dtCheck != null && dtCheck.Rows.Count > 0)
  200. {
  201. foreach (DataRow item in dtCheck.Rows)
  202. {
  203. if (head.WHCode == item["cWhCode"].ToString())
  204. {
  205. isEnable++;
  206. }
  207. }
  208. }
  209. if (isEnable == 0)
  210. {
  211. throw new Exception("该仓库:" + head.WHCode + @"不是代管仓,请重新输入!");
  212. }
  213. }
  214. sql = @"SELECT MOID FROM OM_MOMain WHERE cBusType='固定资产' and cCode='" + head.OOCode + "' ";
  215. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  216. if (dtCheck != null && dtCheck.Rows.Count > 0)
  217. {
  218. int isZL = 0;
  219. sql = @"SELECT cWhCode FROM Warehouse WHERE bWhAsset ='1' ";
  220. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  221. if (dtCheck != null && dtCheck.Rows.Count > 0)
  222. {
  223. foreach (DataRow item in dtCheck.Rows)
  224. {
  225. if (head.WHCode == item["cWhCode"].ToString())
  226. {
  227. isZL++;
  228. }
  229. }
  230. }
  231. if (isZL == 0)
  232. {
  233. throw new Exception("该仓库:" + head.WHCode + @"不是资产仓,请重新输入!");
  234. }
  235. }
  236. }
  237. #region 委外采购入库单 表头
  238. sql = @"INSERT INTO dbo.RdRecord01
  239. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  240. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  241. cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  242. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  243. cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  244. cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  245. dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  246. cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  247. ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  248. bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  249. ireturncount ,iverifystate ,iswfcontrolled ,
  250. cModifyPerson ,dnmaketime ,dnverifytime ,
  251. bredvouch ,bCredit ,iPrintCount)
  252. SELECT @ID,1,'01','','',@cWhCode,
  253. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,@cDepCode,a.cPersonCode,'01',
  254. a.cVenCode,b.cCode,a.cCode,@cHandler,a.cMemo,
  255. 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  256. a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  257. a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  258. a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  259. a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  260. a.ID,a.iTaxRate,a.iExchRate,a.cexch_name,0,
  261. 0,0,0,
  262. 0,0,0,
  263. '',GETDATE(),GETDATE(),
  264. 0,0,0
  265. FROM dbo.PU_ArrivalVouch a LEFT JOIN dbo.OM_MOMain b ON a.cpocode=b.cCode WHERE a.cCode='" + head.ODNCode + "'";
  266. cmd.Parameters.Clear();
  267. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  268. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  269. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  270. cmd.Parameters.Add(new SqlParameter("@cDepCode", ""));
  271. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  272. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  273. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  274. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  275. cmd.CommandText = sql;
  276. try
  277. {
  278. int count = cmd.ExecuteNonQuery();
  279. if (count <= 0)
  280. {
  281. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  282. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  283. }
  284. }
  285. catch (Exception ex)
  286. {
  287. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  288. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  289. }
  290. #endregion
  291. #region 采购入库单 表体
  292. foreach (ICSOutsourcingReceiveDocs body in head.details)
  293. {
  294. sql = @"INSERT INTO dbo.rdrecords01
  295. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,iNum,
  296. iFlag ,iSQuantity ,iSNum ,iMoney ,
  297. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  298. cItem_class ,cItemCode ,iPOsID ,fACost ,cName ,iNQuantity ,
  299. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  300. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  301. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode ,
  302. dbarvdate ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 )
  303. SELECT @AutoID,@ID1,@cInvCode,@iQuantity,0,0,0,@iNum,
  304. 0,0,0,0,
  305. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  306. a.cItem_class,a.cItemCode,a.iPOsID,0,a.cItemName,a.iQuantity,
  307. c.cVenCode,a.Autoid,0,0,0,0,
  308. 0,a.iTaxRate,0,0,1,d.cCode,0,
  309. 0,0,0,1,c.cCode,
  310. 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
  311. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  312. INNER JOIN dbo.OM_MOMain d ON b.MOID=d.MOID
  313. INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where a.Autoid='" + body.ODNDetailID + @"'
  314. EXEC PROC_Update_OM_RdRecord01 @ID= @ID1
  315. ";
  316. cmd.Parameters.Clear();
  317. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  318. cmd.Parameters.Add(new SqlParameter("@ID1", iFatherId));
  319. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  320. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  321. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount));
  322. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  323. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  324. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  325. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  326. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  327. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  328. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  329. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  330. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  331. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  332. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  333. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  334. cmd.CommandText = sql;
  335. try
  336. {
  337. int count = cmd.ExecuteNonQuery();
  338. if (count <= 0)
  339. {
  340. log.Error("委外采购入库单表体失败,受影响行数<=0;");
  341. throw new Exception("委外采购入库单表体失败,受影响行数<=0;");
  342. }
  343. }
  344. catch (Exception ex)
  345. {
  346. log.Error("委外采购入库单表体失败!SQL:\r\n" + sql, ex);
  347. throw new Exception("委外采购入库单表体失败!SQL:\r\n" + sql, ex);
  348. }
  349. if (head.UpdateStock)
  350. {
  351. if (isEnable!=0)
  352. {
  353. key.cBustypeUN = "委外加工";
  354. key.cVouchTypeUN = "01";
  355. key.TableName = "IA_ST_UnAccountVouch01";
  356. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  357. 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);
  358. }
  359. else
  360. {
  361. //更新现存量
  362. key.cBustypeUN = "委外加工";
  363. key.cVouchTypeUN = "01";
  364. key.TableName = "IA_ST_UnAccountVouch01";
  365. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  366. 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);
  367. }
  368. }
  369. //OM_MODetails
  370. sql = @"UPDATE b SET freceivedqty=ISNULL(freceivedqty,0)+" + body.Quantity + @"
  371. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  372. WHERE a.Autoid='" + body.ODNDetailID + "'";
  373. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  374. //回写PU_ArrivalVouchs失败
  375. sql = "UPDATE dbo.PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+" + body.Quantity + " WHERE Autoid='" + body.ODNDetailID + "'";
  376. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写PU_ArrivalVouchs失败!");
  377. iFatherIdTwo += "'" + iFatherId + "',";
  378. iChildId--;
  379. }
  380. #endregion
  381. sql = @" select a.ID as ID,a.ID as IDs,a.cCode as ORCVTCode ,a.cVenCode,d.cVenName,a.cOrderCode as OOCode,
  382. a.cMaker as CreateUser,dnmaketime as CreateDateTime,cHandler as Checker,dnverifytime as CheckDateTime
  383. from rdrecord01 a
  384. left join Department c on a.cDepCode=c.cDepCode
  385. left join Vendor d on a.cVenCode=d.cVenCode
  386. where a.cBusType='' AND A.ID in ({0})
  387. select 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
  388. from rdrecord01 a
  389. left join rdrecords01 b on a.ID=b.ID
  390. left join Department c on a.cDepCode=c.cDepCode
  391. left join Vendor d on a.cVenCode=d.cVenCode
  392. where a.cBusType='' AND A.ID in ({0}) ";
  393. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  394. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  395. if (dtNew1 == null)
  396. dtNew1 = ds.Tables[0];
  397. else
  398. dtNew1.Merge(ds.Tables[0]);
  399. if (dtNew2 == null)
  400. dtNew2 = ds.Tables[1];
  401. else
  402. dtNew2.Merge(ds.Tables[1]);
  403. cmd.Transaction.Commit();
  404. }
  405. catch (Exception ex)
  406. {
  407. cmd.Transaction.Rollback();
  408. log.Error(ex.Message);
  409. throw new Exception(ex.Message);
  410. }
  411. finally
  412. {
  413. if (conn.State == ConnectionState.Open)
  414. {
  415. conn.Close();
  416. }
  417. conn.Dispose();
  418. }
  419. }
  420. string RelationName = "details";
  421. DataSet dz = new DataSet();
  422. dtNew1.TableName = "Table0";
  423. dtNew2.TableName = "Table1";
  424. dz.Tables.Add(dtNew1.Copy());
  425. dz.Tables.Add(dtNew2.Copy());
  426. DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["IDs"], dz.Tables[1].Columns["IDs"]);
  427. dz.Relations.Add(dh);
  428. msg = DBHelper.DataSetToJson(dz, RelationName);
  429. return msg;
  430. }
  431. /// <summary>
  432. /// 创建委外采购入(委外到货单)
  433. /// </summary>
  434. /// <param name="Bills"></param>
  435. /// <returns></returns>
  436. public string CreateOutsourcingReceiveDocByPU(List<ICSOutsourcingReceiveDoc> Bills)
  437. {
  438. string msg = "";
  439. string connS = "";
  440. int num = 0;
  441. //取得out库单的默认显示模版
  442. //取得采购入库单单据 表头ID,表体DID
  443. VouchKey key = new VouchKey();
  444. SqlConnection conn = new SqlConnection();
  445. SqlCommand cmd = new SqlCommand();
  446. if (Bills.Count <= 0)
  447. {
  448. throw new Exception("传送数据为空!");
  449. }
  450. LogInfo(Bills);
  451. foreach (ICSOutsourcingReceiveDoc head in Bills)
  452. {
  453. try
  454. {
  455. num = head.details.Count();
  456. connS = string.Format(connString, head.WorkPoint);
  457. conn = new System.Data.SqlClient.SqlConnection(connS);
  458. conn.Open();
  459. SqlTransaction sqlTran = conn.BeginTransaction();
  460. cmd = new SqlCommand();
  461. cmd.Transaction = sqlTran;
  462. cmd.Connection = conn;
  463. string[] ss = head.WorkPoint.Split('_');
  464. ERPDB = ss[1];
  465. string[] dd = CRNALL.Split('~');
  466. string crdname = dd[0];
  467. string carname = dd[1];
  468. string surface = dd[2];
  469. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  470. //取得out库单的默认显示模版
  471. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  472. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  473. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  474. DateTime date = DateTime.Now;
  475. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", head.WorkPoint);
  476. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  477. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  478. string sql = "";
  479. #region 验证数量不能大于到货单数量
  480. foreach (ICSOutsourcingReceiveDocs body in head.details)
  481. {
  482. sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNDetailID + "'";
  483. DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  484. if (dtQty != null && dtQty.Rows.Count > 0)
  485. {
  486. //到货单可入数量
  487. decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  488. sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNDetailID + "'";
  489. DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  490. decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  491. if (ReceQty + body.Quantity > PUQty)
  492. {
  493. throw new Exception("入库数量大于当前到货单行可入库数量!");
  494. }
  495. }
  496. else
  497. {
  498. throw new Exception("U8不存在该到货单行!");
  499. }
  500. }
  501. #endregion
  502. #region 委外采购入库单 表头
  503. sql = @"INSERT INTO dbo.RdRecord01
  504. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  505. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  506. cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  507. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  508. cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  509. cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  510. dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  511. cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  512. ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  513. bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  514. ireturncount ,iverifystate ,iswfcontrolled ,
  515. cModifyPerson ,dnmaketime ,dnverifytime ,
  516. bredvouch ,bCredit ,iPrintCount)
  517. SELECT @ID,1,'01','','',@cWhCode,
  518. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,@cDepCode,a.cPersonCode,'01',
  519. a.cVenCode,b.cCode,a.cCode,@cHandler,a.cMemo,
  520. 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  521. a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  522. a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  523. a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  524. a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  525. a.ID,a.iTaxRate,a.iExchRate,a.cexch_name,0,
  526. 0,0,0,
  527. 0,0,0,
  528. '',GETDATE(),GETDATE(),
  529. 0,0,0
  530. FROM dbo.PU_ArrivalVouch a LEFT JOIN dbo.OM_MOMain b ON a.cpocode=b.cCode WHERE a.cCode ='" + head.ODNCode + "'";
  531. cmd.Parameters.Clear();
  532. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  533. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  534. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  535. cmd.Parameters.Add(new SqlParameter("@cDepCode", ""));
  536. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  537. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  538. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  539. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  540. cmd.CommandText = sql;
  541. try
  542. {
  543. int count = cmd.ExecuteNonQuery();
  544. if (count <= 0)
  545. {
  546. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  547. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  548. }
  549. }
  550. catch (Exception ex)
  551. {
  552. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  553. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  554. }
  555. #endregion
  556. #region 采购入库单 表体
  557. int irowno = 0;
  558. foreach (ICSOutsourcingReceiveDocs body in head.details)
  559. {
  560. sql = @"INSERT INTO dbo.rdrecords01
  561. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,iNum,
  562. iFlag ,iSQuantity ,iSNum ,iMoney ,
  563. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  564. cItem_class ,cItemCode ,iPOsID ,fACost ,cName ,iNQuantity ,
  565. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  566. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  567. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode ,
  568. dbarvdate ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 )
  569. SELECT @AutoID,@ID1,@cInvCode,@iQuantity,0,0,0,@iNum,
  570. 0,0,0,0,
  571. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  572. a.cItem_class,a.cItemCode,a.iPOsID,0,a.cItemName,a.iQuantity,
  573. c.cVenCode,a.Autoid,0,0,0,0,
  574. 0,a.iTaxRate,0,0,1,d.cCode,0,
  575. 0,0,0,1,c.cCode,
  576. 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
  577. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  578. INNER JOIN dbo.OM_MOMain d ON b.MOID=d.MOID
  579. INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where a.Autoid='" + body.ODNDetailID + @"'
  580. EXEC PROC_Update_OM_RdRecord01 @ID= @ID1
  581. ";
  582. cmd.Parameters.Clear();
  583. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  584. cmd.Parameters.Add(new SqlParameter("@ID1", iFatherId));
  585. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  586. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  587. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount));
  588. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  589. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  590. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  591. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  592. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  593. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  594. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  595. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  596. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  597. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  598. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  599. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  600. cmd.CommandText = sql;
  601. try
  602. {
  603. int count = cmd.ExecuteNonQuery();
  604. if (count <= 0)
  605. {
  606. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  607. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  608. }
  609. }
  610. catch (Exception ex)
  611. {
  612. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  613. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  614. }
  615. if (head.UpdateStock)
  616. {
  617. //更新现存量
  618. key.cBustypeUN = "委外加工";
  619. key.cVouchTypeUN = "01";
  620. key.TableName = "IA_ST_UnAccountVouch01";
  621. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  622. 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);
  623. }
  624. //OM_MODetails
  625. sql = @"UPDATE b SET freceivedqty=ISNULL(freceivedqty,0)+" + body.Quantity + @"
  626. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  627. WHERE a.Autoid='" + body.ODNDetailID + "'";
  628. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  629. //回写PU_ArrivalVouchs失败
  630. sql = "UPDATE dbo.PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+" + body.Quantity + " WHERE Autoid='" + body.ODNDetailID + "'";
  631. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写PU_ArrivalVouchs失败!");
  632. //更新待入库数量
  633. sql = "UPDATE dbo.CurrentStock SET fInQuantity=ISNULL(fInQuantity,0)-" + body.Quantity + " WHERE cInvCode='" + body.InvCode + "' AND cWhCode ='" + head.WHCode + "'";
  634. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  635. {
  636. //if (!string.IsNullOrWhiteSpace(body.cBatch))
  637. //{
  638. // sql += " and cBatch='" + body.cBatch + "'";
  639. //}
  640. }
  641. DBHelper.CmdExecuteNonQuery(sql, cmd, "更新待入库数量失败!");
  642. // #region 判断现存量是否足够
  643. // sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fInQuantity<0)
  644. // BEGIN
  645. // DECLARE @MSG NVARCHAR(100)
  646. // SELECT @MSG='ERP待入库数量不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fInQuantity<0
  647. // RAISERROR(@MSG,16,1)
  648. // END";
  649. // cmd.CommandText = sql;
  650. // cmd.ExecuteNonQuery();
  651. // #endregion
  652. }
  653. #endregion
  654. cmd.Transaction.Commit();
  655. }
  656. catch (Exception ex)
  657. {
  658. cmd.Transaction.Rollback();
  659. log.Error(ex.Message);
  660. throw new Exception(ex.Message);
  661. }
  662. finally
  663. {
  664. if (conn.State == ConnectionState.Open)
  665. {
  666. conn.Close();
  667. }
  668. conn.Dispose();
  669. }
  670. //GetSerialCode(iFatherId.ToString());
  671. }
  672. return msg;
  673. }
  674. /// <summary>
  675. /// 创建委外采购入(委外到货单)
  676. /// </summary>
  677. /// <param name="Bills"></param>
  678. /// <returns></returns>
  679. public string CreateOutsourcingReceiveDocByPO(List<ICSOutsourcingReceiveDoc> Bills)
  680. {
  681. string msg = "";
  682. string connS = "";
  683. string mocode = "";
  684. string seq = "";
  685. int num = 0;
  686. int out11 = 0;
  687. string gys = "";
  688. int isEnable = 0;
  689. int irownnew = 0;
  690. int news = 0;
  691. string iFatherIdTwo = "";
  692. string sql = "";
  693. DataTable dt = null;
  694. DataTable dtNew1 = null;
  695. DataTable dtNew2 = null;
  696. VouchKey key = new VouchKey();
  697. SqlConnection conn = new SqlConnection();
  698. SqlCommand cmd = new SqlCommand();
  699. if (Bills.Count <= 0)
  700. {
  701. throw new Exception("传送数据为空!");
  702. }
  703. LogInfo(Bills);
  704. foreach (ICSOutsourcingReceiveDoc head in Bills)
  705. {
  706. foreach (ICSOutsourcingReceiveDocs load in head.details)
  707. {
  708. foreach (ICSOutsourcingReceiveDocss loads in load.detailss)
  709. {
  710. news++;
  711. }
  712. }
  713. }
  714. foreach (ICSOutsourcingReceiveDoc head in Bills)
  715. {
  716. try
  717. {
  718. connS = string.Format(connString, head.WorkPoint);
  719. conn = new System.Data.SqlClient.SqlConnection(connS);
  720. conn.Open();
  721. SqlTransaction sqlTran = conn.BeginTransaction();
  722. cmd = new SqlCommand();
  723. cmd.Transaction = sqlTran;
  724. cmd.Connection = conn;
  725. if (DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  726. {
  727. }
  728. else
  729. {
  730. throw new Exception("U8正在整理现存量,请稍后再试");
  731. }
  732. string[] ss = head.WorkPoint.Split('_');
  733. ERPDB = ss[1];
  734. string[] dd = CRNALL.Split('~');
  735. string crdname = dd[0];
  736. string carname = dd[1];
  737. string surface = dd[2];
  738. string[] d = CRNA.Split('~');
  739. string crdname1 = d[0];
  740. string carname1 = d[1];
  741. string surface1 = d[2];
  742. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  743. string cardnewcode1 = DBHelper.GetCardNumber(carname1, cmd);
  744. //取得out库单的默认显示模版
  745. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  746. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  747. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  748. DateTime date = DateTime.Now;
  749. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", head.WorkPoint);
  750. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  751. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  752. #region 验证数量不能大于到货单数量
  753. //foreach (ICSOutsourcingReceiveDocs body in head.details)
  754. //{
  755. // sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNDetailID + "'";
  756. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  757. // if (dtQty != null && dtQty.Rows.Count > 0)
  758. // {
  759. // //到货单可入数量
  760. // decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  761. // sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNDetailID + "'";
  762. // DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  763. // decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  764. // if (ReceQty + body.Quantity > PUQty)
  765. // {
  766. // throw new Exception("入库数量大于当前到货单行可入库数量!");
  767. // }
  768. // }
  769. // else
  770. // {
  771. // throw new Exception("U8不存在该到货单行!");
  772. // }
  773. //}
  774. #endregion
  775. if (head.Enable == true)
  776. {
  777. sql = @"SELECT MOID,cVenCode FROM OM_MOMain WHERE cBusType='代管采购' and cCode='" + head.OOCode + "' ";
  778. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  779. if (dtCheck != null && dtCheck.Rows.Count > 0)
  780. {
  781. gys = dtCheck.Rows[0][1].ToString();
  782. isEnable = 0;
  783. sql = @"SELECT cWhCode FROM Warehouse WHERE bProxyWh ='1' ";
  784. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  785. if (dtCheck != null && dtCheck.Rows.Count > 0)
  786. {
  787. foreach (DataRow item in dtCheck.Rows)
  788. {
  789. if (head.WHCode == item["cWhCode"].ToString())
  790. {
  791. isEnable++;
  792. }
  793. }
  794. }
  795. if (isEnable == 0)
  796. {
  797. throw new Exception("该仓库:" + head.WHCode + @"不是代管仓,请重新输入!");
  798. }
  799. }
  800. sql = @"SELECT MOID FROM OM_MOMain WHERE cBusType='固定资产' and cCode='" + head.OOCode + "' ";
  801. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  802. if (dtCheck != null && dtCheck.Rows.Count > 0)
  803. {
  804. int isZL = 0;
  805. sql = @"SELECT cWhCode FROM Warehouse WHERE bWhAsset ='1' ";
  806. dtCheck = DBHelper.SQlReturnData(sql, cmd);
  807. if (dtCheck != null && dtCheck.Rows.Count > 0)
  808. {
  809. foreach (DataRow item in dtCheck.Rows)
  810. {
  811. if (head.WHCode == item["cWhCode"].ToString())
  812. {
  813. isZL++;
  814. }
  815. }
  816. }
  817. if (isZL == 0)
  818. {
  819. throw new Exception("该仓库:" + head.WHCode + @"不是资产仓,请重新输入!");
  820. }
  821. }
  822. }
  823. #region 委外采购入库单 表头
  824. sql = @"INSERT INTO dbo.RdRecord01
  825. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  826. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  827. cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  828. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  829. cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  830. cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  831. dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  832. cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  833. ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  834. bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  835. ireturncount ,iverifystate ,iswfcontrolled ,
  836. cModifyPerson ,dnmaketime ,dnverifytime ,
  837. bredvouch ,bCredit ,iPrintCount)
  838. SELECT @ID,1,'01','','',@cWhCode,
  839. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@RdCode,a.cDepCode,a.cPersonCode,'01',
  840. a.cVenCode,'',a.cCode,@cHandler,a.cMemo,
  841. 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  842. a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  843. a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  844. a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  845. a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  846. '',a.iTaxRate,a.nflat,a.cexch_name,0,
  847. 0,0,0,
  848. 0,0,0,
  849. '',GETDATE(),GETDATE(),
  850. 0,0,0
  851. FROM dbo.OM_MOMain a WHERE a.cCode='" + head.OOCode + "'";
  852. cmd.Parameters.Clear();
  853. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  854. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  855. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  856. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  857. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  858. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  859. cmd.Parameters.Add(new SqlParameter("@RdCode", cRDcode));
  860. cmd.CommandText = sql;
  861. try
  862. {
  863. int count = cmd.ExecuteNonQuery();
  864. if (count <= 0)
  865. {
  866. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  867. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  868. }
  869. }
  870. catch (Exception ex)
  871. {
  872. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  873. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  874. }
  875. #endregion
  876. if (news != 0)
  877. {
  878. Dictionary<string, int> dic1 = DBHelper.GetAllCode("" + ERPDB + "", surface1, "1", head.WorkPoint);
  879. int iFatherId1 = Convert.ToInt32(dic1["iFatherId"].ToString());
  880. out11 = iFatherId1;
  881. int iChildId1 = Convert.ToInt32(dic1["iChildId"].ToString());
  882. DateTime date1 = DateTime.Now;
  883. string iBaseCodeLen1 = DBHelper.GetAllRDCode(cardnewcode1, "" + date1 + "", "admin", head.WorkPoint);
  884. string cRDcode1 = DBHelper.GetRDCode(crdname1, cmd);
  885. string DEF_ID1 = DBHelper.GetDefaultTemplate(cardnewcode1, cmd);
  886. #region 材料出库单表头
  887. sql = @"
  888. INSERT INTO dbo.rdrecord11
  889. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,
  890. cWhCode ,dDate ,cCode ,cRdCode ,cDepCode,
  891. cPersonCode,cHandler ,bTransFlag ,cMaker ,dVeriDate ,
  892. bpufirst ,biafirst ,VT_ID ,bIsSTQc ,cPsPcode ,
  893. cMPoCode ,iproorderid ,bFromPreYear ,bIsLsQuery ,bIsComplement ,
  894. iDiscountTaxType ,ireturncount ,iverifystate ,iswfcontrolled ,dnmaketime ,
  895. dnverifytime ,bredvouch ,iPrintCount,cVenCode,iMQuantity)
  896. SELECT distinct @ID,0,'11','','',
  897. @cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,c.cDepCode,
  898. c.cPersonCode,@cHandler,0,@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),
  899. 0,0,@VT_ID,0,null,
  900. c.cCode,a.MoId,0,0,0,
  901. 0,0,0,0,GETDATE(),
  902. GETDATE(),@bredvouch,0,c.cVenCode,null
  903. from dbo.OM_MOMaterials a
  904. INNER JOIN dbo.OM_MODetails b ON a.MoDetailsID = b.MoDetailsID
  905. LEFT JOIN dbo.OM_MOMain c ON b.MOID = c.MOID
  906. WHERE c.cCode='" + head.OOCode + "'";
  907. cmd.Parameters.Clear();
  908. cmd.Parameters.Add(new SqlParameter("@ID", out11));
  909. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  910. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen1));
  911. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  912. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  913. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID1));
  914. cmd.Parameters.Add(new SqlParameter("@bredvouch", "0"));
  915. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode1));
  916. cmd.CommandText = sql;
  917. try
  918. {
  919. int count = cmd.ExecuteNonQuery();
  920. if (count <= 0)
  921. {
  922. log.Error("生成成品入库单表体失败,受影响行数<=0;");
  923. throw new Exception("生成成品入库单表体失败,受影响行数<=0;");
  924. }
  925. }
  926. catch (Exception ex)
  927. {
  928. log.Error("生成成品入库单表体失败!SQL:\r\n" + sql, ex);
  929. throw new Exception("生成成品入库单表体失败!SQL:\r\n" + sql, ex);
  930. }
  931. #endregion
  932. }
  933. #region 采购入库单 表体
  934. foreach (ICSOutsourcingReceiveDocs body in head.details)
  935. {
  936. sql = @"SELECT b.cCode,a.iVouchRowNo FROM dbo.OM_MODetails a INNER JOIN
  937. dbo.OM_MOMain b ON b.MoId = a.MoId WHERE a.MODetailsID = '" + body.OODetailID + "'";
  938. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  939. if (dtCheck != null && dtCheck.Rows.Count > 0)
  940. {
  941. mocode = dtCheck.Rows[0]["cCode"].ToString();
  942. seq = dtCheck.Rows[0]["iVouchRowNo"].ToString();
  943. }
  944. if (head.ODNCode!="")
  945. {
  946. sql = @"INSERT INTO dbo.rdrecords01
  947. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,iNum,
  948. iFlag ,iSQuantity ,iSNum ,iMoney ,
  949. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  950. cItem_class ,cItemCode ,iOMoDID ,fACost ,cName ,iNQuantity ,
  951. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  952. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  953. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode
  954. ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 )
  955. SELECT @AutoID,@ID1,@cInvCode,@iQuantity,0,0,0,@iNum,
  956. 0,0,0,0,
  957. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  958. a.cItem_class,a.cItemCode,a.MODetailsID,0,a.cItemName,a.iQuantity,
  959. @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,
  960. 0,d.nflat,0,0,1,'',0,
  961. 0,0,0,1,'',
  962. 0,0,0,@irowno,0,@cBatch,a.iNatUnitPrice *convert(decimal, @iQuantity),a.iNatUnitPrice ,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  963. FROM dbo.OM_MODetails a
  964. INNER JOIN dbo.OM_MOMain d ON a.MOID=d.MOID
  965. where a.MODetailsID='" + body.OODetailID + @"'
  966. ";
  967. }
  968. else
  969. {
  970. sql = @"INSERT INTO dbo.rdrecords01
  971. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,iNum,
  972. iFlag ,iSQuantity ,iSNum ,iMoney ,
  973. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  974. cItem_class ,cItemCode ,iOMoDID ,fACost ,cName ,iNQuantity ,
  975. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  976. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  977. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode
  978. ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 )
  979. SELECT @AutoID,@ID1,@cInvCode,@iQuantity,0,0,0,@iNum,
  980. 0,0,0,0,
  981. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  982. a.cItem_class,a.cItemCode,a.MODetailsID,0,a.cItemName,a.iQuantity,
  983. @VenCode,'',0,0,0,0,
  984. 0,d.nflat,0,0,1,'',0,
  985. 0,0,0,1,'',
  986. 0,0,0,@irowno,0,@cBatch,a.iNatUnitPrice *convert(decimal, @iQuantity),a.iNatUnitPrice ,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  987. FROM dbo.OM_MODetails a
  988. INNER JOIN dbo.OM_MOMain d ON a.MOID=d.MOID
  989. where a.MODetailsID='" + body.OODetailID + @"'
  990. ";
  991. }
  992. cmd.Parameters.Clear();
  993. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  994. cmd.Parameters.Add(new SqlParameter("@ID1", iFatherId));
  995. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  996. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  997. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount));
  998. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  999. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  1000. cmd.Parameters.Add(new SqlParameter("@VenCode", head.VenCode));
  1001. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  1002. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  1003. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  1004. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  1005. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  1006. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  1007. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  1008. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  1009. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  1010. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  1011. cmd.CommandText = sql;
  1012. try
  1013. {
  1014. int count = cmd.ExecuteNonQuery();
  1015. if (count <= 0)
  1016. {
  1017. log.Error("委外采购入库单表体失败,受影响行数<=0;");
  1018. throw new Exception("委外采购入库单表体失败,受影响行数<=0;");
  1019. }
  1020. }
  1021. catch (Exception ex)
  1022. {
  1023. log.Error("委外采购入库单表体失败!SQL:\r\n" + sql, ex);
  1024. throw new Exception("委外采购入库单表体失败!SQL:\r\n" + sql, ex);
  1025. }
  1026. if (head.CompleteVerification)
  1027. {
  1028. #region 齐套检验
  1029. sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' ";
  1030. DataTable qt = DBHelper.SQlReturnData(sql, cmd);
  1031. if (qt != null && qt.Rows.Count > 0)
  1032. {
  1033. if (qt.Rows[0]["cValue"].ToString() == "1")
  1034. {
  1035. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  1036. (
  1037. select
  1038. a.cCode,b.iVouchRowNo,
  1039. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  1040. from DBO.OM_MOMain a
  1041. left join DBO.OM_MODetails b on a.moid = b.moid
  1042. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  1043. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  1044. where a.cCode='{0}' and b.iVouchRowNo='{1}'
  1045. and c.iQuantity<>0
  1046. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  1047. ) tt
  1048. group by cCode,iVouchRowNo";
  1049. sql = string.Format(sql, mocode, seq);
  1050. qt = DBHelper.SQlReturnData(sql, cmd);
  1051. if (qt == null || qt.Rows.Count == 0)
  1052. {
  1053. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  1054. }
  1055. else
  1056. {
  1057. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  1058. {
  1059. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料<=0!");
  1060. }
  1061. }
  1062. }
  1063. else if (qt.Rows[0]["cValue"].ToString() == "2")
  1064. {
  1065. sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'";
  1066. qt = DBHelper.SQlReturnData(sql, cmd);
  1067. if (qt != null && qt.Rows.Count > 0)
  1068. {
  1069. if (qt.Rows[0]["cValue"].ToString() == "true")
  1070. {
  1071. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  1072. (
  1073. select
  1074. a.cCode,b.iVouchRowNo,
  1075. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  1076. from DBO.OM_MOMain a
  1077. left join DBO.OM_MODetails b on a.moid = b.moid
  1078. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  1079. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  1080. where a.cCode='{0}' and b.iVouchRowNo='{1}'
  1081. and c.iQuantity<>0
  1082. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  1083. ) tt
  1084. group by cCode,iVouchRowNo";
  1085. sql = string.Format(sql, mocode, seq);
  1086. qt = DBHelper.SQlReturnData(sql, cmd);
  1087. if (qt == null || qt.Rows.Count == 0)
  1088. {
  1089. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  1090. }
  1091. else
  1092. {
  1093. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  1094. {
  1095. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  1096. }
  1097. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity <= 0)
  1098. {
  1099. throw new Exception("该物料:" + body.InvCode + @"入库数量大于关键物料领料套数!");
  1100. }
  1101. }
  1102. }
  1103. else
  1104. {
  1105. sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from
  1106. (
  1107. select
  1108. a.cCode,b.iVouchRowNo,
  1109. c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty
  1110. from DBO.OM_MOMain a
  1111. left join DBO.OM_MODetails b on a.moid = b.moid
  1112. left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID
  1113. left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode
  1114. where a.cCode='{0}' and b.iVouchRowNo='{1}'
  1115. and c.iQuantity<>0
  1116. group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity
  1117. ) tt
  1118. group by cCode,iVouchRowNo";
  1119. sql = string.Format(sql, mocode, seq);
  1120. qt = DBHelper.SQlReturnData(sql, cmd);
  1121. if (qt == null || qt.Rows.Count == 0)
  1122. {
  1123. throw new Exception("该物料:" + body.InvCode + @"在订单内未有可用领料!");
  1124. }
  1125. else
  1126. {
  1127. if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) <= 0)
  1128. {
  1129. throw new Exception("该物料:" + body.InvCode + @"在订单内未领料!");
  1130. }
  1131. else if (Convert.ToDecimal(qt.Rows[0]["min_lotqty"]) - body.Quantity <= 0)
  1132. {
  1133. throw new Exception("该物料:" + body.InvCode + @"入库数量大于领料套数!");
  1134. }
  1135. }
  1136. }
  1137. }
  1138. }
  1139. }
  1140. #endregion
  1141. }
  1142. if (head.UpdateStock)
  1143. {
  1144. if (isEnable != 0)
  1145. {
  1146. key.cBustypeUN = "委外加工";
  1147. key.cVouchTypeUN = "01";
  1148. key.TableName = "IA_ST_UnAccountVouch01";
  1149. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  1150. 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);
  1151. }
  1152. else
  1153. {
  1154. //更新现存量
  1155. key.cBustypeUN = "委外加工";
  1156. key.cVouchTypeUN = "01";
  1157. key.TableName = "IA_ST_UnAccountVouch01";
  1158. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  1159. 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);
  1160. }
  1161. }
  1162. //OM_MODetails
  1163. sql = @"UPDATE a SET freceivedqty=ISNULL(freceivedqty,0)+" + body.Quantity + @"
  1164. FROM dbo.OM_MODetails a
  1165. WHERE a.MODetailsID='" + body.OODetailID + "'";
  1166. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  1167. //更新待入库数量
  1168. sql = "UPDATE dbo.CurrentStock SET fInQuantity=ISNULL(fInQuantity,0)-" + body.Quantity + " WHERE cInvCode='" + body.InvCode + "' AND cWhCode ='" + head.WHCode + "'";
  1169. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  1170. {
  1171. if (!string.IsNullOrWhiteSpace(body.BatchCode))
  1172. {
  1173. sql += " and cBatch='" + body.BatchCode + "'";
  1174. }
  1175. }
  1176. DBHelper.CmdExecuteNonQuery(sql, cmd, "更新待入库数量失败!");
  1177. num = body.detailss.Count();
  1178. Dictionary<string, int> dic2 = DBHelper.GetAllCode("" + ERPDB + "", surface1, "" + num + "", head.WorkPoint);
  1179. int iFatherId2 = Convert.ToInt32(dic2["iFatherId"].ToString());
  1180. int iChildId2 = Convert.ToInt32(dic2["iChildId"].ToString());
  1181. foreach (ICSOutsourcingReceiveDocss item in body.detailss)
  1182. {
  1183. irownnew++;
  1184. sql = @"INSERT INTO dbo.rdrecords11
  1185. ( AutoID,ID ,cInvCode,iQuantity ,cBatch ,iNum,
  1186. iFlag ,cItemCode ,cName ,iNQuantity ,iMPoIds ,
  1187. bLPUseFree ,iOriTrackID ,bCosting ,bVMIUsed ,cmocode ,
  1188. invcode ,imoseq ,iopseq ,iExpiratDateCalcu ,iorderdid ,
  1189. iordertype ,isotype ,ipesodid ,ipesotype ,cpesocode ,
  1190. ipesoseq ,irowno ,bcanreplace ,iposflag,iOMoDID,
  1191. iOMoMID,comcode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  1192. SELECT distinct
  1193. @AutoID,@ID,a.cInvCode,@iQuantity,@cBatch,@iNum,
  1194. 0,null,null,@iQuantity,NULL,
  1195. 0,0,1,0,NULL,
  1196. b.cInvCode,NULL,NULL,0,0,
  1197. 0,0,a.MOMaterialsID,'6',c.cCode,
  1198. NULL, @irowno,0,NULL,b.MODetailsID,
  1199. a.MOMaterialsID,c.cCode,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  1200. FROM dbo.OM_MOMaterials a
  1201. INNER JOIN dbo.OM_MODetails b ON a.MoDetailsID=b.MODetailsID
  1202. INNER JOIN dbo.OM_MOMain c ON b.MOID=c.MOID
  1203. WHERE a.MOMaterialsID='" + body.OODetailID + "'";
  1204. cmd.Parameters.Clear();
  1205. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId2));
  1206. cmd.Parameters.Add(new SqlParameter("@ID", out11));
  1207. cmd.Parameters.Add(new SqlParameter("@iQuantity", item.Quantity));
  1208. cmd.Parameters.Add(new SqlParameter("@iNum", item.Amount));
  1209. cmd.Parameters.Add(new SqlParameter("@cBatch", item.BatchCode));
  1210. cmd.Parameters.Add(new SqlParameter("@irowno", irownnew));
  1211. cmd.Parameters.Add(new SqlParameter("@cFree1", item.cFree1));
  1212. cmd.Parameters.Add(new SqlParameter("@cFree2", item.cFree2));
  1213. cmd.Parameters.Add(new SqlParameter("@cFree3", item.cFree3));
  1214. cmd.Parameters.Add(new SqlParameter("@cFree4", item.cFree4));
  1215. cmd.Parameters.Add(new SqlParameter("@cFree5", item.cFree5));
  1216. cmd.Parameters.Add(new SqlParameter("@cFree6", item.cFree6));
  1217. cmd.Parameters.Add(new SqlParameter("@cFree7", item.cFree7));
  1218. cmd.Parameters.Add(new SqlParameter("@cFree8", item.cFree8));
  1219. cmd.Parameters.Add(new SqlParameter("@cFree9", item.cFree9));
  1220. cmd.Parameters.Add(new SqlParameter("@cFree10", item.cFree10));
  1221. cmd.CommandText = sql;
  1222. try
  1223. {
  1224. int count = cmd.ExecuteNonQuery();
  1225. if (count <= 0)
  1226. {
  1227. log.Error("生成材料出库单表体失败,受影响行数<=0;");
  1228. throw new Exception("生成材料出库单表体失败,受影响行数<=0;");
  1229. }
  1230. }
  1231. catch (Exception ex)
  1232. {
  1233. log.Error("生成材料出库单表体失败" + sql, ex);
  1234. throw new Exception("生成材料出库单表体失败 " + sql, ex);
  1235. }
  1236. if (head.UpdateStock)
  1237. {
  1238. //更新现存量
  1239. key.cBustypeUN = "领料";
  1240. key.cVouchTypeUN = "11";
  1241. key.TableName = "IA_ST_UnAccountVouch11";
  1242. 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);
  1243. }
  1244. #region 回写工单子件已领数量
  1245. sql = "update mom_moallocate set IssQty=isnull(IssQty,0)+" + item.Quantity + ",RequisitionIssQty=isnull(RequisitionIssQty,0)+(" + item.Quantity + ") where AllocateId='" + item.OODetailID + "' ";
  1246. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写工单子件已领数量失败!");
  1247. #endregion
  1248. iChildId2--;
  1249. }
  1250. iFatherIdTwo += "'" + iFatherId + "',";
  1251. iChildId--;
  1252. }
  1253. #endregion
  1254. #region 查询
  1255. sql = @" select a.ID as ID,a.ID as IDs,a.cCode as ORCVTCode ,a.cVenCode,d.cVenName,a.cOrderCode as OOCode,
  1256. a.cMaker as CreateUser,dnmaketime as CreateDateTime,cHandler as Checker,dnverifytime as CheckDateTime
  1257. from rdrecord01 a
  1258. left join Department c on a.cDepCode=c.cDepCode
  1259. left join Vendor d on a.cVenCode=d.cVenCode
  1260. where a.cBusType='' AND A.ID in ({0})
  1261. select 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
  1262. from rdrecord01 a
  1263. left join rdrecords01 b on a.ID=b.ID
  1264. left join Department c on a.cDepCode=c.cDepCode
  1265. left join Vendor d on a.cVenCode=d.cVenCode
  1266. where a.cBusType='' AND A.ID in ({0}) ";
  1267. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  1268. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  1269. if (dtNew1 == null)
  1270. dtNew1 = ds.Tables[0];
  1271. else
  1272. dtNew1.Merge(ds.Tables[0]);
  1273. if (dtNew2 == null)
  1274. dtNew2 = ds.Tables[1];
  1275. else
  1276. dtNew2.Merge(ds.Tables[1]);
  1277. #endregion
  1278. cmd.Transaction.Commit();
  1279. }
  1280. catch (Exception ex)
  1281. {
  1282. cmd.Transaction.Rollback();
  1283. log.Error(ex.Message);
  1284. throw new Exception(ex.Message);
  1285. }
  1286. finally
  1287. {
  1288. if (conn.State == ConnectionState.Open)
  1289. {
  1290. conn.Close();
  1291. }
  1292. conn.Dispose();
  1293. }
  1294. }
  1295. string RelationName = "details";
  1296. DataSet dz = new DataSet();
  1297. dtNew1.TableName = "Table0";
  1298. dtNew2.TableName = "Table1";
  1299. dz.Tables.Add(dtNew1.Copy());
  1300. dz.Tables.Add(dtNew2.Copy());
  1301. DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["IDs"], dz.Tables[1].Columns["IDs"]);
  1302. dz.Relations.Add(dh);
  1303. msg = DBHelper.DataSetToJson(dz, RelationName);
  1304. return msg;
  1305. }
  1306. /// <summary>
  1307. /// 审核委外采购入库
  1308. /// </summary>
  1309. /// <param name="infos"></param>
  1310. /// <returns></returns>
  1311. public string Approve(List<ICSOutsourcingReceiveDoc> infos)
  1312. {
  1313. List<ICSSalesShipmentDoc> szJson = new List<ICSSalesShipmentDoc>();
  1314. string json = "";
  1315. DataTable dtNew = null;
  1316. string connS = "";
  1317. if (infos.Count <= 0)
  1318. {
  1319. throw new Exception("传送数据为空!");
  1320. }
  1321. string res = string.Empty;
  1322. SqlConnection conn = new SqlConnection();
  1323. SqlCommand cmd = new SqlCommand();
  1324. string sql = string.Empty;
  1325. foreach (ICSOutsourcingReceiveDoc info in infos)
  1326. {
  1327. try
  1328. {
  1329. connS = string.Format(connString, info.WorkPoint);
  1330. conn = new System.Data.SqlClient.SqlConnection(connS);
  1331. conn.Open();
  1332. SqlTransaction sqlTran = conn.BeginTransaction();
  1333. cmd = new SqlCommand();
  1334. cmd.Transaction = sqlTran;
  1335. cmd.Connection = conn;
  1336. if (info.MTime < new DateTime(2000, 01, 01))
  1337. throw new Exception("请输入正确的操作时间:" + info.MTime);
  1338. sql = @"UPDATE RdRecord01 SET cHandler ='" + info.User + @"' ,
  1339. dnverifytime=CONVERT(VARCHAR(50),GETDATE(),112),dVeriDate=GETDATE() WHERE ID='{0}'";
  1340. sql = string.Format(sql, info.ID);
  1341. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核委外采购入库单失败!");
  1342. cmd.Transaction.Commit();
  1343. }
  1344. catch (Exception ex)
  1345. {
  1346. cmd.Transaction.Rollback();
  1347. log.Error(ex.Message);
  1348. throw new Exception(ex.Message);
  1349. }
  1350. finally
  1351. {
  1352. if (conn.State == ConnectionState.Open)
  1353. {
  1354. conn.Close();
  1355. }
  1356. conn.Dispose();
  1357. }
  1358. }
  1359. return json;
  1360. }
  1361. /// <summary>
  1362. /// 删除委外采购入库单
  1363. /// </summary>
  1364. /// <param name="infos"></param>
  1365. /// <returns></returns>
  1366. public string Delete(List<ICSOutsourcingReceiveDoc> infos)
  1367. {
  1368. string connS = "";
  1369. List<ICSSalesShipmentDoc> szJson = new List<ICSSalesShipmentDoc>();
  1370. if (infos.Count <= 0)
  1371. {
  1372. throw new Exception("传送数据为空!");
  1373. }
  1374. string res = string.Empty;
  1375. SqlConnection conn = new SqlConnection();
  1376. SqlCommand cmd = new SqlCommand();
  1377. string sql = string.Empty;
  1378. foreach (ICSOutsourcingReceiveDoc info in infos)
  1379. {
  1380. try
  1381. {
  1382. connS = string.Format(connString, info.WorkPoint);
  1383. conn = new System.Data.SqlClient.SqlConnection(connS);
  1384. conn.Open();
  1385. SqlTransaction sqlTran = conn.BeginTransaction();
  1386. cmd = new SqlCommand();
  1387. cmd.Transaction = sqlTran;
  1388. cmd.Connection = conn;
  1389. if (info.MTime < new DateTime(2000, 01, 01))
  1390. throw new Exception("请输入正确的操作时间:" + info.MTime);
  1391. sql = @"delete RdRecord01 where RdRecord01.ID='" + info.ID + "'";
  1392. sql += @"delete RdRecords01 where RdRecords01.ID='" + info.ID + "'";
  1393. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  1394. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  1395. // WHERE a.ID='" + info.ID + "'";
  1396. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除委外采购入库单失败!");
  1397. cmd.Transaction.Commit();
  1398. }
  1399. catch (Exception ex)
  1400. {
  1401. cmd.Transaction.Rollback();
  1402. log.Error(ex.Message);
  1403. throw new Exception(ex.Message);
  1404. }
  1405. finally
  1406. {
  1407. if (conn.State == ConnectionState.Open)
  1408. {
  1409. conn.Close();
  1410. }
  1411. conn.Dispose();
  1412. }
  1413. }
  1414. return res;
  1415. }
  1416. /// <summary>
  1417. ///
  1418. /// 记录日志
  1419. /// </summary>
  1420. /// <param name="Bills"></param>
  1421. private void LogInfo(List<ICSOutsourcingReceiveDoc> Bills)
  1422. {
  1423. string HeadList = string.Empty;
  1424. string BodyList = string.Empty;
  1425. foreach (ICSOutsourcingReceiveDoc head in Bills)
  1426. {
  1427. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  1428. foreach (ICSOutsourcingReceiveDocs body in head.details)
  1429. {
  1430. BodyList += "\r\n 表体主键ID: " + body.ODNDetailID + ",数量:" + body.Quantity;
  1431. }
  1432. }
  1433. log.Info(HeadList);
  1434. log.Info(BodyList);
  1435. }
  1436. public string GetSerialCode(string ID, string connS)
  1437. {
  1438. string sql = "EXEC Addins_GetSerialCode '{0}'";
  1439. sql = string.Format(sql, new object[] { ID });
  1440. return DBHelper.ExecuteScalar(CommandType.Text, sql, connS).ToString();
  1441. }
  1442. }
  1443. }