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

1031 lines
52 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
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. /// <summary>
  23. /// 获取委外采购入库单
  24. /// </summary>
  25. /// <param name="infos"></param>
  26. /// <returns></returns>
  27. public string Get(List<ICSOutsourcingReceiveDoc> infos)
  28. {
  29. List<ICSOutsourcingReceiveDoc> szJson = new List<ICSOutsourcingReceiveDoc>();
  30. DataTable dt = null;
  31. DataTable dtNew = null;
  32. string connS = "";
  33. string json = "";
  34. if (infos.Count <= 0)
  35. {
  36. throw new Exception("传送数据为空!");
  37. }
  38. string res = string.Empty;
  39. SqlConnection conn = new SqlConnection();
  40. SqlCommand cmd = new SqlCommand();
  41. string sql = string.Empty;
  42. foreach (ICSOutsourcingReceiveDoc info in infos)
  43. {
  44. try
  45. {
  46. connS = string.Format(connString, info.WorkPoint);
  47. conn = new System.Data.SqlClient.SqlConnection(connS);
  48. conn.Open();
  49. SqlTransaction sqlTran = conn.BeginTransaction();
  50. cmd = new SqlCommand();
  51. cmd.Transaction = sqlTran;
  52. cmd.Connection = conn;
  53. if (info.MTime < new DateTime(2000, 01, 01))
  54. throw new Exception("请输入正确的操作时间:" + info.MTime);
  55. sql = @" select a.ID,iMPoIds,iordercode,a.cCode,a.cDepCode,c.cDepName,a.cWhCode,
  56. d.cWhName,a.cSource,a.cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,
  57. b.AutoID ,b.irowno,b.cInvCode,b.iQuantity,e.MoDetailsID,b.iNum,e.csocode
  58. from rdrecord11 a
  59. left join rdrecords11 b on a.ID=b.ID
  60. left join Department c on a.cDepCode=c.cDepCode
  61. left join Warehouse d on a.cWhCode=d.cWhCode
  62. left join OM_MODetails e on b.iOMoDID=e.MoDetailsID
  63. where a.cBusType=''";
  64. if (!string.IsNullOrWhiteSpace(info.ORCVTCode))
  65. {
  66. sql += " and a.cCode='{0}'";
  67. }
  68. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  69. {
  70. sql += " and ISNULL(a.dnmodifytime,ISNULL(a.dnverifytime, ISNULL(a.dnmodifytime, a.dnmaketime)))>='{1}'";
  71. }
  72. if (!string.IsNullOrWhiteSpace(info.User))
  73. {
  74. sql += "and a.CMAKER='{2}'";
  75. }
  76. sql = string.Format(sql, info.ORCVTCode, info.MTime, info.User);
  77. dt = DBHelper.SQlReturnData(sql, cmd);
  78. if (dt.Rows.Count <= 0 || dt == null)
  79. throw new Exception("委外发料单:" + info.ORCVTCode + ",无信息!");
  80. dtNew.Merge(dt);
  81. cmd.Transaction.Commit();
  82. }
  83. catch (Exception ex)
  84. {
  85. cmd.Transaction.Rollback();
  86. log.Error(ex.Message);
  87. throw new Exception(ex.Message);
  88. }
  89. finally
  90. {
  91. if (conn.State == ConnectionState.Open)
  92. {
  93. conn.Close();
  94. }
  95. conn.Dispose();
  96. }
  97. }
  98. json = JsonConvert.SerializeObject(dtNew);
  99. return json;
  100. }
  101. /// <summary>
  102. /// 创建委外采购入(到货单)
  103. /// </summary>
  104. /// <param name="Bills"></param>
  105. /// <returns></returns>
  106. public string CreateOutsourcingReceiveDocByPOArrive(List<ICSOutsourcingReceiveDoc> Bills)
  107. {
  108. string msg = "";
  109. DataTable dtNew1 = null;
  110. DataTable dtNew2 = null;
  111. string connS = "";
  112. string sql = "";
  113. string iFatherIdTwo = "";
  114. int num = 0;
  115. //取得out库单的默认显示模版
  116. //取得采购入库单单据 表头ID,表体DID
  117. VouchKey key = new VouchKey();
  118. SqlConnection conn = new SqlConnection();
  119. SqlCommand cmd = new SqlCommand();
  120. if (Bills.Count <= 0)
  121. {
  122. throw new Exception("传送数据为空!");
  123. }
  124. LogInfo(Bills);
  125. foreach (ICSOutsourcingReceiveDoc head in Bills)
  126. {
  127. try
  128. {
  129. num = head.details.Count();
  130. connS = string.Format(connString, head.WorkPoint);
  131. conn = new System.Data.SqlClient.SqlConnection(connS);
  132. conn.Open();
  133. SqlTransaction sqlTran = conn.BeginTransaction();
  134. cmd = new SqlCommand();
  135. cmd.Transaction = sqlTran;
  136. cmd.Connection = conn;
  137. string[] ss = head.WorkPoint.Split('_');
  138. ERPDB = ss[1];
  139. //取得out库单的默认显示模版
  140. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "PuArrival", "" + num + "");
  141. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  142. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  143. DateTime date = DateTime.Now;
  144. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  145. #region 验证数量不能大于到货单数量
  146. //foreach (ICSOutsourcingReceiveDocs body in head.details)
  147. // {
  148. // sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNDetailID + "'";
  149. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  150. // if (dtQty != null && dtQty.Rows.Count > 0)
  151. // {
  152. // //到货单可入数量
  153. // decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  154. // sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNDetailID + "'";
  155. // DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  156. // decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  157. // if (ReceQty + body.Quantity > PUQty)
  158. // {
  159. // throw new Exception("入库数量大于当前到货单行可入库数量!");
  160. // }
  161. // }
  162. // else
  163. // {
  164. // throw new Exception("U8不存在该到货单行!");
  165. // }
  166. // }
  167. #endregion
  168. #region 委外采购入库单 表头
  169. sql = @"INSERT INTO dbo.RdRecord01
  170. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  171. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  172. cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  173. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  174. cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  175. cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  176. dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  177. cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  178. ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  179. bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  180. ireturncount ,iverifystate ,iswfcontrolled ,
  181. cModifyPerson ,dnmaketime ,dnverifytime ,
  182. bredvouch ,bCredit ,iPrintCount)
  183. SELECT @ID,1,'01','','',@cWhCode,
  184. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,'101',@cDepCode,a.cPersonCode,'01',
  185. a.cVenCode,b.cCode,a.cCode,@cHandler,a.cMemo,
  186. 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  187. a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  188. a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  189. a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  190. a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  191. a.ID,a.iTaxRate,a.iExchRate,a.cexch_name,0,
  192. 0,0,0,
  193. 0,0,0,
  194. '',GETDATE(),GETDATE(),
  195. 0,0,0
  196. FROM dbo.PU_ArrivalVouch a LEFT JOIN dbo.OM_MOMain b ON a.cpocode=b.cCode WHERE a.cCode='" + head.ODNCode + "'";
  197. cmd.Parameters.Clear();
  198. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  199. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  200. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  201. cmd.Parameters.Add(new SqlParameter("@cDepCode", ""));
  202. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  203. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  204. cmd.Parameters.Add(new SqlParameter("@VT_ID", iChildId));
  205. cmd.CommandText = sql;
  206. try
  207. {
  208. int count = cmd.ExecuteNonQuery();
  209. if (count <= 0)
  210. {
  211. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  212. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  213. }
  214. }
  215. catch (Exception ex)
  216. {
  217. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  218. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  219. }
  220. #endregion
  221. #region 采购入库单 表体
  222. foreach (ICSOutsourcingReceiveDocs body in head.details)
  223. {
  224. sql = @"INSERT INTO dbo.rdrecords01
  225. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,
  226. iFlag ,iSQuantity ,iSNum ,iMoney ,
  227. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  228. cItem_class ,cItemCode ,iPOsID ,fACost ,cName ,iNQuantity ,
  229. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  230. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  231. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode ,
  232. dbarvdate ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 )
  233. SELECT @AutoID,@ID,@cInvCode,@iQuantity,0,0,0,
  234. 0,0,0,0,
  235. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  236. a.cItem_class,a.cItemCode,a.iPOsID,0,a.cItemName,a.iQuantity,
  237. c.cVenCode,a.Autoid,0,0,0,0,
  238. 0,a.iTaxRate,0,0,1,d.cCode,0,
  239. 0,0,0,1,c.cCode,
  240. 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
  241. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  242. INNER JOIN dbo.OM_MOMain d ON b.MOID=d.MOID
  243. INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where a.Autoid='" + body.ODNDetailID + "'";
  244. cmd.Parameters.Clear();
  245. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  246. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  247. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  248. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  249. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  250. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  251. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  252. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  253. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  254. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  255. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  256. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  257. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  258. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  259. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  260. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  261. cmd.CommandText = sql;
  262. try
  263. {
  264. int count = cmd.ExecuteNonQuery();
  265. if (count <= 0)
  266. {
  267. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  268. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  269. }
  270. }
  271. catch (Exception ex)
  272. {
  273. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  274. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  275. }
  276. if (head.UpdateStock)
  277. {
  278. //更新现存量
  279. key.cBustypeUN = "委外加工";
  280. key.cVouchTypeUN = "01";
  281. key.TableName = "IA_ST_UnAccountVouch01";
  282. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  283. 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);
  284. }
  285. //OM_MODetails
  286. sql = @"UPDATE b SET freceivedqty=ISNULL(freceivedqty,0)+" + body.Quantity + @"
  287. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  288. WHERE a.Autoid='" + body.ODNDetailID + "'";
  289. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  290. //回写PU_ArrivalVouchs失败
  291. sql = "UPDATE dbo.PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+" + body.Quantity + " WHERE Autoid='" + body.ODNDetailID + "'";
  292. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写PU_ArrivalVouchs失败!");
  293. iFatherIdTwo += "'" + iFatherId + "',";
  294. iChildId--;
  295. }
  296. #endregion
  297. sql = @" select a.ID as ID,a.ID as IDs,a.cCode as ORCVTCode ,a.cVenCode,d.cVenName,a.cOrderCode as OOCode,
  298. a.cMaker as CreateUser,dnmaketime as CreateDateTime,cHandler as Checker,dnverifytime as CheckDateTime
  299. from rdrecord01 a
  300. left join Department c on a.cDepCode=c.cDepCode
  301. left join Vendor d on a.cVenCode=d.cVenCode
  302. where a.cBusType='' AND A.ID in ({0})
  303. 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
  304. from rdrecord01 a
  305. left join rdrecords01 b on a.ID=b.ID
  306. left join Department c on a.cDepCode=c.cDepCode
  307. left join Vendor d on a.cVenCode=d.cVenCode
  308. where a.cBusType='' AND A.ID in ({0}) ";
  309. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  310. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  311. dtNew1.Merge(ds.Tables[0]);
  312. dtNew2.Merge(ds.Tables[1]);
  313. cmd.Transaction.Commit();
  314. }
  315. catch (Exception ex)
  316. {
  317. cmd.Transaction.Rollback();
  318. log.Error(ex.Message);
  319. throw new Exception(ex.Message);
  320. }
  321. finally
  322. {
  323. if (conn.State == ConnectionState.Open)
  324. {
  325. conn.Close();
  326. }
  327. conn.Dispose();
  328. }
  329. }
  330. string RelationName = "details";
  331. DataRelation dh = new DataRelation(RelationName, dtNew1.Columns["IDs"], dtNew2.Columns["IDs"]);
  332. DataSet dz = new DataSet();
  333. dz.Relations.Add(dh);
  334. msg = DBHelper.DataSetToJson(dz, RelationName);
  335. return msg;
  336. }
  337. /// <summary>
  338. /// 创建委外采购入(委外到货单)
  339. /// </summary>
  340. /// <param name="Bills"></param>
  341. /// <returns></returns>
  342. public string CreateOutsourcingReceiveDocByPU(List<ICSOutsourcingReceiveDoc> Bills)
  343. {
  344. string msg = "";
  345. string connS = "";
  346. int num = 0;
  347. //取得out库单的默认显示模版
  348. //取得采购入库单单据 表头ID,表体DID
  349. VouchKey key = new VouchKey();
  350. SqlConnection conn = new SqlConnection();
  351. SqlCommand cmd = new SqlCommand();
  352. if (Bills.Count <= 0)
  353. {
  354. throw new Exception("传送数据为空!");
  355. }
  356. LogInfo(Bills);
  357. foreach (ICSOutsourcingReceiveDoc head in Bills)
  358. {
  359. try
  360. {
  361. num = head.details.Count();
  362. connS = string.Format(connString, head.WorkPoint);
  363. conn = new System.Data.SqlClient.SqlConnection(connS);
  364. conn.Open();
  365. SqlTransaction sqlTran = conn.BeginTransaction();
  366. cmd = new SqlCommand();
  367. cmd.Transaction = sqlTran;
  368. cmd.Connection = conn;
  369. string[] ss = head.WorkPoint.Split('_');
  370. ERPDB = ss[1];
  371. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "PuArrival", "" + num + "");
  372. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  373. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  374. DateTime date = DateTime.Now;
  375. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  376. string sql = "";
  377. #region 验证数量不能大于到货单数量
  378. foreach (ICSOutsourcingReceiveDocs body in head.details)
  379. {
  380. sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNDetailID + "'";
  381. DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  382. if (dtQty != null && dtQty.Rows.Count > 0)
  383. {
  384. //到货单可入数量
  385. decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  386. sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNDetailID + "'";
  387. DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  388. decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  389. if (ReceQty + body.Quantity > PUQty)
  390. {
  391. throw new Exception("入库数量大于当前到货单行可入库数量!");
  392. }
  393. }
  394. else
  395. {
  396. throw new Exception("U8不存在该到货单行!");
  397. }
  398. }
  399. #endregion
  400. #region 委外采购入库单 表头
  401. sql = @"INSERT INTO dbo.RdRecord01
  402. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  403. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  404. cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  405. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  406. cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  407. cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  408. dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  409. cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  410. ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  411. bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  412. ireturncount ,iverifystate ,iswfcontrolled ,
  413. cModifyPerson ,dnmaketime ,dnverifytime ,
  414. bredvouch ,bCredit ,iPrintCount)
  415. SELECT @ID,1,'01','','',@cWhCode,
  416. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,'101',@cDepCode,a.cPersonCode,'01',
  417. a.cVenCode,b.cCode,a.cCode,@cHandler,a.cMemo,
  418. 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  419. a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  420. a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  421. a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  422. a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  423. a.ID,a.iTaxRate,a.iExchRate,a.cexch_name,0,
  424. 0,0,0,
  425. 0,0,0,
  426. '',GETDATE(),GETDATE(),
  427. 0,0,0
  428. FROM dbo.PU_ArrivalVouch a LEFT JOIN dbo.OM_MOMain b ON a.cpocode=b.cCode WHERE a.cCode ='" + head.ODNCode + "'";
  429. cmd.Parameters.Clear();
  430. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  431. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  432. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  433. cmd.Parameters.Add(new SqlParameter("@cDepCode", ""));
  434. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  435. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  436. cmd.Parameters.Add(new SqlParameter("@VT_ID", iChildId));
  437. cmd.CommandText = sql;
  438. try
  439. {
  440. int count = cmd.ExecuteNonQuery();
  441. if (count <= 0)
  442. {
  443. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  444. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  445. }
  446. }
  447. catch (Exception ex)
  448. {
  449. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  450. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  451. }
  452. #endregion
  453. #region 采购入库单 表体
  454. int irowno = 0;
  455. foreach (ICSOutsourcingReceiveDocs body in head.details)
  456. {
  457. sql = @"INSERT INTO dbo.rdrecords01
  458. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,
  459. iFlag ,iSQuantity ,iSNum ,iMoney ,
  460. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  461. cItem_class ,cItemCode ,iPOsID ,fACost ,cName ,iNQuantity ,
  462. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  463. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  464. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode ,
  465. dbarvdate ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 )
  466. SELECT @AutoID,@ID,@cInvCode,@iQuantity,0,0,0,
  467. 0,0,0,0,
  468. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  469. a.cItem_class,a.cItemCode,a.iPOsID,0,a.cItemName,a.iQuantity,
  470. c.cVenCode,a.Autoid,0,0,0,0,
  471. 0,a.iTaxRate,0,0,1,d.cCode,0,
  472. 0,0,0,1,c.cCode,
  473. 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
  474. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  475. INNER JOIN dbo.OM_MOMain d ON b.MOID=d.MOID
  476. INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where a.Autoid='" + body.ODNDetailID + "'";
  477. cmd.Parameters.Clear();
  478. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  479. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  480. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  481. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  482. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  483. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  484. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  485. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  486. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  487. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  488. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  489. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  490. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  491. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  492. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  493. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  494. cmd.CommandText = sql;
  495. try
  496. {
  497. int count = cmd.ExecuteNonQuery();
  498. if (count <= 0)
  499. {
  500. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  501. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  502. }
  503. }
  504. catch (Exception ex)
  505. {
  506. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  507. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  508. }
  509. if (head.UpdateStock)
  510. {
  511. //更新现存量
  512. key.cBustypeUN = "委外加工";
  513. key.cVouchTypeUN = "01";
  514. key.TableName = "IA_ST_UnAccountVouch01";
  515. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  516. 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);
  517. }
  518. //OM_MODetails
  519. sql = @"UPDATE b SET freceivedqty=ISNULL(freceivedqty,0)+" + body.Quantity + @"
  520. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  521. WHERE a.Autoid='" + body.ODNDetailID + "'";
  522. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  523. //回写PU_ArrivalVouchs失败
  524. sql = "UPDATE dbo.PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+" + body.Quantity + " WHERE Autoid='" + body.ODNDetailID + "'";
  525. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写PU_ArrivalVouchs失败!");
  526. //更新待入库数量
  527. sql = "UPDATE dbo.CurrentStock SET fInQuantity=ISNULL(fInQuantity,0)-" + body.Quantity + " WHERE cInvCode='" + body.InvCode + "' AND cWhCode ='" + head.WHCode + "'";
  528. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  529. {
  530. //if (!string.IsNullOrWhiteSpace(body.cBatch))
  531. //{
  532. // sql += " and cBatch='" + body.cBatch + "'";
  533. //}
  534. }
  535. DBHelper.CmdExecuteNonQuery(sql, cmd, "更新待入库数量失败!");
  536. // #region 判断现存量是否足够
  537. // sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fInQuantity<0)
  538. // BEGIN
  539. // DECLARE @MSG NVARCHAR(100)
  540. // SELECT @MSG='ERP待入库数量不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fInQuantity<0
  541. // RAISERROR(@MSG,16,1)
  542. // END";
  543. // cmd.CommandText = sql;
  544. // cmd.ExecuteNonQuery();
  545. // #endregion
  546. }
  547. #endregion
  548. cmd.Transaction.Commit();
  549. }
  550. catch (Exception ex)
  551. {
  552. cmd.Transaction.Rollback();
  553. log.Error(ex.Message);
  554. throw new Exception(ex.Message);
  555. }
  556. finally
  557. {
  558. if (conn.State == ConnectionState.Open)
  559. {
  560. conn.Close();
  561. }
  562. conn.Dispose();
  563. }
  564. //GetSerialCode(iFatherId.ToString());
  565. }
  566. return msg;
  567. }
  568. /// <summary>
  569. /// 创建委外采购入(委外到货单)
  570. /// </summary>
  571. /// <param name="Bills"></param>
  572. /// <returns></returns>
  573. public string CreateOutsourcingReceiveDocByPO(List<ICSOutsourcingReceiveDoc> Bills)
  574. {
  575. string msg = "";
  576. string connS = "";
  577. int num = 0;
  578. string iFatherIdTwo = "";
  579. string sql = "";
  580. DataTable dt = null;
  581. DataTable dtNew1 = null;
  582. DataTable dtNew2 = null;
  583. VouchKey key = new VouchKey();
  584. SqlConnection conn = new SqlConnection();
  585. SqlCommand cmd = new SqlCommand();
  586. if (Bills.Count <= 0)
  587. {
  588. throw new Exception("传送数据为空!");
  589. }
  590. LogInfo(Bills);
  591. foreach (ICSOutsourcingReceiveDoc head in Bills)
  592. {
  593. try
  594. {
  595. connS = string.Format(connString, head.WorkPoint);
  596. conn = new System.Data.SqlClient.SqlConnection(connS);
  597. conn.Open();
  598. SqlTransaction sqlTran = conn.BeginTransaction();
  599. cmd = new SqlCommand();
  600. cmd.Transaction = sqlTran;
  601. cmd.Connection = conn;
  602. string[] ss = head.WorkPoint.Split('_');
  603. ERPDB = ss[1];
  604. string RdCode = DBHelper.GetRDCode("采购入库", cmd);
  605. num = head.details.Count();
  606. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + num + "");
  607. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  608. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  609. DateTime date = DateTime.Now;
  610. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  611. #region 验证数量不能大于到货单数量
  612. //foreach (ICSOutsourcingReceiveDocs body in head.details)
  613. //{
  614. // sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNDetailID + "'";
  615. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  616. // if (dtQty != null && dtQty.Rows.Count > 0)
  617. // {
  618. // //到货单可入数量
  619. // decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  620. // sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNDetailID + "'";
  621. // DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  622. // decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  623. // if (ReceQty + body.Quantity > PUQty)
  624. // {
  625. // throw new Exception("入库数量大于当前到货单行可入库数量!");
  626. // }
  627. // }
  628. // else
  629. // {
  630. // throw new Exception("U8不存在该到货单行!");
  631. // }
  632. //}
  633. #endregion
  634. #region 委外采购入库单 表头
  635. sql = @"INSERT INTO dbo.RdRecord01
  636. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  637. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  638. cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  639. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  640. cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  641. cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  642. dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  643. cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  644. ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  645. bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  646. ireturncount ,iverifystate ,iswfcontrolled ,
  647. cModifyPerson ,dnmaketime ,dnverifytime ,
  648. bredvouch ,bCredit ,iPrintCount)
  649. SELECT @ID,1,'01','','',@cWhCode,
  650. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@RdCode,@cDepCode,a.cPersonCode,'01',
  651. a.cVenCode,'',a.cCode,@cHandler,a.cMemo,
  652. 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  653. a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  654. a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  655. a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  656. a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  657. '',a.iTaxRate,a.nflat,a.cexch_name,0,
  658. 0,0,0,
  659. 0,0,0,
  660. '',GETDATE(),GETDATE(),
  661. 0,0,0
  662. FROM dbo.OM_MOMain a WHERE a.cCode='" + head.OOCode + "'";
  663. cmd.Parameters.Clear();
  664. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  665. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  666. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  667. cmd.Parameters.Add(new SqlParameter("@cDepCode", ""));
  668. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  669. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  670. cmd.Parameters.Add(new SqlParameter("@VT_ID", iChildId));
  671. cmd.Parameters.Add(new SqlParameter("@RdCode", RdCode));
  672. cmd.CommandText = sql;
  673. try
  674. {
  675. int count = cmd.ExecuteNonQuery();
  676. if (count <= 0)
  677. {
  678. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  679. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  680. }
  681. }
  682. catch (Exception ex)
  683. {
  684. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  685. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  686. }
  687. #endregion
  688. #region 采购入库单 表体
  689. foreach (ICSOutsourcingReceiveDocs body in head.details)
  690. {
  691. sql = @"INSERT INTO dbo.rdrecords01
  692. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,
  693. iFlag ,iSQuantity ,iSNum ,iMoney ,
  694. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  695. cItem_class ,cItemCode ,iOMoDID ,fACost ,cName ,iNQuantity ,
  696. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  697. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  698. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode
  699. ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 )
  700. SELECT @AutoID,@ID,@cInvCode,@iQuantity,0,0,0,
  701. 0,0,0,0,
  702. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  703. a.cItem_class,a.cItemCode,a.MODetailsID,0,a.cItemName,a.iQuantity,
  704. @VenCode,'',0,0,0,0,
  705. 0,d.nflat,0,0,1,'',0,
  706. 0,0,0,1,'',
  707. 0,0,0,@irowno,0,@cBatch,a.iNatUnitPrice *convert(decimal, @iQuantity),a.iNatUnitPrice ,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  708. FROM dbo.OM_MODetails a
  709. INNER JOIN dbo.OM_MOMain d ON a.MOID=d.MOID
  710. where a.MODetailsID='" + body.OODetailID + "'";
  711. cmd.Parameters.Clear();
  712. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  713. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  714. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  715. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  716. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  717. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  718. cmd.Parameters.Add(new SqlParameter("@VenCode", head.VenCode));
  719. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  720. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  721. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  722. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  723. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  724. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  725. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  726. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  727. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  728. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  729. cmd.CommandText = sql;
  730. try
  731. {
  732. int count = cmd.ExecuteNonQuery();
  733. if (count <= 0)
  734. {
  735. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  736. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  737. }
  738. }
  739. catch (Exception ex)
  740. {
  741. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  742. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  743. }
  744. if (head.UpdateStock)
  745. {
  746. //更新现存量
  747. key.cBustypeUN = "委外加工";
  748. key.cVouchTypeUN = "01";
  749. key.TableName = "IA_ST_UnAccountVouch01";
  750. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  751. 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);
  752. }
  753. //OM_MODetails
  754. sql = @"UPDATE a SET freceivedqty=ISNULL(freceivedqty,0)+" + body.Quantity + @"
  755. FROM dbo.OM_MODetails a
  756. WHERE a.MODetailsID='" + body.OODetailID + "'";
  757. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  758. //更新待入库数量
  759. sql = "UPDATE dbo.CurrentStock SET fInQuantity=ISNULL(fInQuantity,0)-" + body.Quantity + " WHERE cInvCode='" + body.InvCode + "' AND cWhCode ='" + head.WHCode + "'";
  760. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  761. {
  762. //if (!string.IsNullOrWhiteSpace(body.cBatch))
  763. //{
  764. // sql += " and cBatch='" + body.cBatch + "'";
  765. //}
  766. }
  767. DBHelper.CmdExecuteNonQuery(sql, cmd, "更新待入库数量失败!");
  768. iFatherIdTwo += "'" + iFatherId + "',";
  769. iChildId--;
  770. }
  771. #endregion
  772. sql = @" select a.ID as ID,a.ID as IDs,a.cCode as ORCVTCode ,a.cVenCode,d.cVenName,a.cOrderCode as OOCode,
  773. a.cMaker as CreateUser,dnmaketime as CreateDateTime,cHandler as Checker,dnverifytime as CheckDateTime
  774. from rdrecord01 a
  775. left join Department c on a.cDepCode=c.cDepCode
  776. left join Vendor d on a.cVenCode=d.cVenCode
  777. where a.cBusType='' AND A.ID in ({0})
  778. 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
  779. from rdrecord01 a
  780. left join rdrecords01 b on a.ID=b.ID
  781. left join Department c on a.cDepCode=c.cDepCode
  782. left join Vendor d on a.cVenCode=d.cVenCode
  783. where a.cBusType='' AND A.ID in ({0}) ";
  784. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  785. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  786. dtNew1.Merge(ds.Tables[0]);
  787. dtNew2.Merge(ds.Tables[1]);
  788. cmd.Transaction.Commit();
  789. }
  790. catch (Exception ex)
  791. {
  792. cmd.Transaction.Rollback();
  793. log.Error(ex.Message);
  794. throw new Exception(ex.Message);
  795. }
  796. finally
  797. {
  798. if (conn.State == ConnectionState.Open)
  799. {
  800. conn.Close();
  801. }
  802. conn.Dispose();
  803. }
  804. }
  805. string RelationName = "details";
  806. DataRelation dh = new DataRelation(RelationName, dtNew1.Columns["IDs"], dtNew2.Columns["IDs"]);
  807. DataSet dz = new DataSet();
  808. dz.Relations.Add(dh);
  809. msg = DBHelper.DataSetToJson(dz, RelationName);
  810. return msg;
  811. }
  812. /// <summary>
  813. /// 审核委外采购入库
  814. /// </summary>
  815. /// <param name="infos"></param>
  816. /// <returns></returns>
  817. public string Approve(List<ICSOutsourcingReceiveDoc> infos)
  818. {
  819. List<ICSSalesShipmentDoc> szJson = new List<ICSSalesShipmentDoc>();
  820. string json = "";
  821. DataTable dtNew = null;
  822. string connS = "";
  823. if (infos.Count <= 0)
  824. {
  825. throw new Exception("传送数据为空!");
  826. }
  827. string res = string.Empty;
  828. SqlConnection conn = new SqlConnection();
  829. SqlCommand cmd = new SqlCommand();
  830. string sql = string.Empty;
  831. foreach (ICSOutsourcingReceiveDoc info in infos)
  832. {
  833. try
  834. {
  835. connS = string.Format(connString, info.WorkPoint);
  836. conn = new System.Data.SqlClient.SqlConnection(connS);
  837. conn.Open();
  838. SqlTransaction sqlTran = conn.BeginTransaction();
  839. cmd = new SqlCommand();
  840. cmd.Transaction = sqlTran;
  841. cmd.Connection = conn;
  842. if (info.MTime < new DateTime(2000, 01, 01))
  843. throw new Exception("请输入正确的操作时间:" + info.MTime);
  844. sql = @"UPDATE RdRecord01 SET cHandler ='" + info.User + @"' ,
  845. dnverifytime=CONVERT(VARCHAR(50),GETDATE(),112),dVeriDate=GETDATE() WHERE ID='{0}'";
  846. sql = string.Format(sql, info.ID);
  847. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核委外采购入库单失败!");
  848. cmd.Transaction.Commit();
  849. }
  850. catch (Exception ex)
  851. {
  852. cmd.Transaction.Rollback();
  853. log.Error(ex.Message);
  854. throw new Exception(ex.Message);
  855. }
  856. finally
  857. {
  858. if (conn.State == ConnectionState.Open)
  859. {
  860. conn.Close();
  861. }
  862. conn.Dispose();
  863. }
  864. }
  865. return json;
  866. }
  867. /// <summary>
  868. /// 删除委外采购入库单
  869. /// </summary>
  870. /// <param name="infos"></param>
  871. /// <returns></returns>
  872. public string Delete(List<ICSOutsourcingReceiveDoc> infos)
  873. {
  874. string connS = "";
  875. List<ICSSalesShipmentDoc> szJson = new List<ICSSalesShipmentDoc>();
  876. if (infos.Count <= 0)
  877. {
  878. throw new Exception("传送数据为空!");
  879. }
  880. string res = string.Empty;
  881. SqlConnection conn = new SqlConnection();
  882. SqlCommand cmd = new SqlCommand();
  883. string sql = string.Empty;
  884. foreach (ICSOutsourcingReceiveDoc info in infos)
  885. {
  886. try
  887. {
  888. connS = string.Format(connString, info.WorkPoint);
  889. conn = new System.Data.SqlClient.SqlConnection(connS);
  890. conn.Open();
  891. SqlTransaction sqlTran = conn.BeginTransaction();
  892. cmd = new SqlCommand();
  893. cmd.Transaction = sqlTran;
  894. cmd.Connection = conn;
  895. if (info.MTime < new DateTime(2000, 01, 01))
  896. throw new Exception("请输入正确的操作时间:" + info.MTime);
  897. sql = @"delete RdRecord01 where RdRecord01.ID='" + info.ID + "'";
  898. sql += @"delete RdRecords01 where RdRecords01.ID='" + info.ID + "'";
  899. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  900. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  901. // WHERE a.ID='" + info.ID + "'";
  902. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除委外采购入库单失败!");
  903. cmd.Transaction.Commit();
  904. }
  905. catch (Exception ex)
  906. {
  907. cmd.Transaction.Rollback();
  908. log.Error(ex.Message);
  909. throw new Exception(ex.Message);
  910. }
  911. finally
  912. {
  913. if (conn.State == ConnectionState.Open)
  914. {
  915. conn.Close();
  916. }
  917. conn.Dispose();
  918. }
  919. }
  920. return res;
  921. }
  922. /// <summary>
  923. ///
  924. /// 记录日志
  925. /// </summary>
  926. /// <param name="Bills"></param>
  927. private void LogInfo(List<ICSOutsourcingReceiveDoc> Bills)
  928. {
  929. string HeadList = string.Empty;
  930. string BodyList = string.Empty;
  931. foreach (ICSOutsourcingReceiveDoc head in Bills)
  932. {
  933. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  934. foreach (ICSOutsourcingReceiveDocs body in head.details)
  935. {
  936. BodyList += "\r\n 表体主键ID: " + body.ODNDetailID + ",数量:" + body.Quantity;
  937. }
  938. }
  939. log.Info(HeadList);
  940. log.Info(BodyList);
  941. }
  942. public string GetSerialCode(string ID, string connS)
  943. {
  944. string sql = "EXEC Addins_GetSerialCode '{0}'";
  945. sql = string.Format(sql, new object[] { ID });
  946. return DBHelper.ExecuteScalar(CommandType.Text, sql, connS).ToString();
  947. }
  948. }
  949. }