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

703 lines
35 KiB

  1. using ICSSoft.Common;
  2. using ICSSoft.Entity;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ICSSoft.DataProject
  12. {
  13. /// <summary>
  14. /// 委外采购红字入库单
  15. /// </summary>
  16. public class OutsourcingReceiveDocNegative
  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<ICSOutsourcingReceiveDocNegative> infos)
  28. {
  29. List<ICSOutsourcingReceiveDocNegative> szJson = new List<ICSOutsourcingReceiveDocNegative>();
  30. DataTable dt = null;
  31. string json = "";
  32. if (infos.Count <= 0)
  33. {
  34. throw new Exception("传送数据为空!");
  35. }
  36. string res = string.Empty;
  37. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  38. conn.Open();
  39. SqlTransaction sqlTran = conn.BeginTransaction();
  40. SqlCommand cmd = new SqlCommand();
  41. cmd.Transaction = sqlTran;
  42. cmd.Connection = conn;
  43. try
  44. {
  45. string sql = string.Empty;
  46. foreach (ICSOutsourcingReceiveDocNegative info in infos)
  47. {
  48. if (info.MTime < new DateTime(2000, 01, 01))
  49. throw new Exception("请输入正确的操作时间:" + info.MTime);
  50. sql = @" select a.ID,iMPoIds,iordercode,a.cCode,a.cDepCode,c.cDepName,a.cWhCode,
  51. d.cWhName,a.cSource,a.cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,
  52. b.AutoID ,b.irowno,b.cInvCode,b.iQuantity,e.MoDetailsID,b.iNum,e.csocode
  53. from rdrecord01 a
  54. left join rdrecords01 b on a.ID=b.ID
  55. left join Department c on a.cDepCode=c.cDepCode
  56. left join Warehouse d on a.cWhCode=d.cWhCode
  57. left join OM_MODetails e on b.iOMoDID=e.MoDetailsID
  58. where a.cBusType=''and b.iQuantity<0";
  59. if (!string.IsNullOrWhiteSpace(info.ORCVNEGCode))
  60. {
  61. sql += " and a.cCode='{0}'";
  62. }
  63. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  64. {
  65. sql += " and ISNULL(a.dnmodifytime,ISNULL(a.dnverifytime, ISNULL(a.dnmodifytime, a.dnmaketime)))>='{1}'";
  66. }
  67. if (!string.IsNullOrWhiteSpace(info.User))
  68. {
  69. sql += "and a.CMAKER='{2}'";
  70. }
  71. sql = string.Format(sql, info.ORCVNEGCode, info.MTime, info.User);
  72. dt = DBHelper.SQlReturnData(sql, cmd);
  73. json = JsonConvert.SerializeObject(dt);
  74. if (dt.Rows.Count <= 0 || dt == null)
  75. throw new Exception("委外采购红字入库单:" + info.ORCVNEGCode + ",无信息!");
  76. }
  77. cmd.Transaction.Commit();
  78. return json;
  79. }
  80. catch (Exception ex)
  81. {
  82. cmd.Transaction.Rollback();
  83. log.Error(ex.Message);
  84. throw new Exception(ex.Message);
  85. }
  86. finally
  87. {
  88. if (conn.State == ConnectionState.Open)
  89. {
  90. conn.Close();
  91. }
  92. conn.Dispose();
  93. }
  94. }
  95. /// <summary>
  96. /// 创建委外采购入(到货单)
  97. /// </summary>
  98. /// <param name="Bills"></param>
  99. /// <returns></returns>
  100. public string CreateOutsourcingReceiveDocNegative(List<ICSOutsourcingReceiveDocNegative> Bills)
  101. {
  102. string msg = "";
  103. int num = 0;
  104. string sql = "";
  105. DataTable dt = null;
  106. int iFatherId = 0;
  107. int iChildId = 0;
  108. string iFatherIdTwo = "";
  109. //取得采购入库单单据 表头ID,表体DID
  110. VouchKey key = new VouchKey();
  111. foreach (ICSOutsourcingReceiveDocNegative head in Bills)
  112. {
  113. num = head.details.Count();
  114. }
  115. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  116. conn.Open();
  117. SqlTransaction sqlTran = conn.BeginTransaction();
  118. SqlCommand cmd = new SqlCommand();
  119. cmd.Transaction = sqlTran;
  120. cmd.Connection = conn;
  121. try
  122. {
  123. if (Bills.Count <= 0)
  124. {
  125. throw new Exception("传送数据为空!");
  126. }
  127. LogInfo(Bills);
  128. foreach (ICSOutsourcingReceiveDocNegative head in Bills)
  129. {
  130. //取得out库单的默认显示模版
  131. num = head.details.Count;
  132. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "PuArrival", "" + num + "");
  133. iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  134. iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  135. DateTime date = DateTime.Now;
  136. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  137. #region 验证数量不能大于到货单数量
  138. foreach (ICSOutsourcingReceiveDocNegatives body in head.details)
  139. {
  140. sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNRTDetailID + "'";
  141. DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  142. if (dtQty != null && dtQty.Rows.Count > 0)
  143. {
  144. //到货单可入数量
  145. decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  146. sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNRTDetailID + "'";
  147. DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  148. decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  149. if (ReceQty + body.Quantity > PUQty)
  150. {
  151. throw new Exception("入库数量大于当前到货单行可入库数量!");
  152. }
  153. }
  154. else
  155. {
  156. throw new Exception("U8不存在该到货单行!");
  157. }
  158. }
  159. #endregion
  160. #region 委外采购入库单 表头
  161. sql = @"INSERT INTO dbo.RdRecord01
  162. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  163. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  164. cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  165. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  166. cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  167. cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  168. dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  169. cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  170. ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  171. bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  172. ireturncount ,iverifystate ,iswfcontrolled ,
  173. cModifyPerson ,dnmaketime ,dnverifytime ,
  174. bredvouch ,bCredit ,iPrintCount)
  175. SELECT @ID,1,'01','','',@cWhCode,
  176. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,'101',@cDepCode,a.cPersonCode,'01',
  177. a.cVenCode,b.cCode,a.cCode,@cHandler,a.cMemo,
  178. 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  179. a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  180. a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  181. a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  182. a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  183. a.ID,a.iTaxRate,a.iExchRate,a.cexch_name,0,
  184. 0,0,0,
  185. 0,0,0,
  186. '',GETDATE(),GETDATE(),
  187. 0,0,0
  188. FROM dbo.PU_ArrivalVouch a LEFT JOIN dbo.OM_MOMain b ON a.cpocode=b.cCode WHERE a.cCode='" + head.ODNRTCode + "'";
  189. cmd.Parameters.Clear();
  190. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  191. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  192. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  193. cmd.Parameters.Add(new SqlParameter("@cDepCode", ""));
  194. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  195. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  196. cmd.Parameters.Add(new SqlParameter("@VT_ID", iChildId));
  197. cmd.CommandText = sql;
  198. try
  199. {
  200. int count = cmd.ExecuteNonQuery();
  201. if (count <= 0)
  202. {
  203. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  204. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  205. }
  206. }
  207. catch (Exception ex)
  208. {
  209. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  210. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  211. }
  212. #endregion
  213. #region 采购入库单 表体
  214. foreach (ICSOutsourcingReceiveDocNegatives body in head.details)
  215. {
  216. body.Quantity = -body.Quantity;
  217. sql = @"INSERT INTO dbo.rdrecords01
  218. ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,
  219. iFlag ,iSQuantity ,iSNum ,iMoney ,
  220. cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  221. cItem_class ,cItemCode ,iPOsID ,fACost ,cName ,iNQuantity ,
  222. chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  223. ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  224. iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode ,
  225. dbarvdate ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost )
  226. SELECT @AutoID,@ID,@cInvCode,@iQuantity,0,0,0,
  227. 0,0,0,0,
  228. a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  229. a.cItem_class,a.cItemCode,a.iPOsID,0,a.cItemName,a.iQuantity,
  230. c.cVenCode,a.Autoid,0,0,0,0,
  231. 0,a.iTaxRate,0,0,1,d.cCode,0,
  232. 0,0,0,1,c.cCode,
  233. c.dDate,0,0,0,@irowno,0,@cBatch,a.iCost*convert(decimal, @iQuantity),a.iCost
  234. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  235. INNER JOIN dbo.OM_MOMain d ON b.MOID=d.MOID
  236. INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where a.Autoid='" + body.ODNRTDetailID + "'";
  237. cmd.Parameters.Clear();
  238. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  239. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  240. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  241. cmd.Parameters.Add(new SqlParameter("@iQuantity", -body.Quantity));
  242. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  243. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  244. cmd.CommandText = sql;
  245. try
  246. {
  247. int count = cmd.ExecuteNonQuery();
  248. if (count <= 0)
  249. {
  250. log.Error("委外采购入库单表头失败,受影响行数<=0;");
  251. throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  252. }
  253. }
  254. catch (Exception ex)
  255. {
  256. log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  257. throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  258. }
  259. //更新现存量
  260. key.cBustypeUN = "委外加工";
  261. key.cVouchTypeUN = "01";
  262. key.TableName = "IA_ST_UnAccountVouch01";
  263. DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key);
  264. //OM_MODetails
  265. sql = @"UPDATE b SET freceivedqty=ISNULL(freceivedqty,0)+" + -body.Quantity + @"
  266. FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  267. WHERE a.Autoid='" + body.ODNRTDetailID + "'";
  268. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  269. //回写PU_ArrivalVouchs失败
  270. sql = "UPDATE dbo.PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+" + -body.Quantity + " WHERE Autoid='" + body.ODNRTDetailID + "'";
  271. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写PU_ArrivalVouchs失败!");
  272. //更新待入库数量
  273. sql = "UPDATE dbo.CurrentStock SET fInQuantity=ISNULL(fInQuantity,0)-" + -body.Quantity + " WHERE cInvCode='" + body.InvCode + "' AND cWhCode ='" + head.WHCode + "'";
  274. if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  275. {
  276. //if (!string.IsNullOrWhiteSpace(body.cBatch))
  277. //{
  278. // sql += " and cBatch='" + body.cBatch + "'";
  279. //}
  280. }
  281. DBHelper.CmdExecuteNonQuery(sql, cmd, "更新待入库数量失败!");
  282. iChildId--;
  283. }
  284. #endregion
  285. iFatherIdTwo += "'" + iFatherId + "',";
  286. }
  287. #region 查询
  288. sql = @" select a.ID as ID,a.ID as IDs,a.cCode as ORCVNEGCode ,a.cVenCode,d.cVenName,a.cOrderCode as OOCode,
  289. a.cMaker as CreateUser,dnmaketime as CreateDateTime,cHandler as Checker,dnverifytime as CheckDateTime
  290. from rdrecord01 a
  291. left join Department c on a.cDepCode=c.cDepCode
  292. left join Vendor d on a.cVenCode=d.cVenCode
  293. where a.cBusType='' AND A.ID in ({0})
  294. select a.ID as IDs,b.AutoID as DetailID,b.iArrsId as ODNRTDetailID, b.irowno as Sequence,b.cInvCode as InvCode,b.iQuantity as Quantity,iNum as Amount,b.iOMoDID as OODetailID
  295. from rdrecord01 a
  296. left join rdrecords01 b on a.ID=b.ID
  297. left join Department c on a.cDepCode=c.cDepCode
  298. left join Vendor d on a.cVenCode=d.cVenCode
  299. where a.cBusType='' AND A.ID in ({0}) ";
  300. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  301. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  302. string RelationName = "details";
  303. DataRelation dr = new DataRelation(RelationName, ds.Tables[0].Columns["IDs"], ds.Tables[1].Columns["IDs"]);
  304. ds.Relations.Add(dr);
  305. msg = DBHelper.DataSetToJson(ds, RelationName);
  306. #endregion
  307. //GetSerialCode(iFatherId.ToString());
  308. cmd.Transaction.Commit();
  309. return msg;
  310. }
  311. catch (Exception ex)
  312. {
  313. cmd.Transaction.Rollback();
  314. log.Error(ex.Message);
  315. throw new Exception(ex.Message);
  316. }
  317. finally
  318. {
  319. if (conn.State == ConnectionState.Open)
  320. {
  321. conn.Close();
  322. }
  323. conn.Dispose();
  324. }
  325. }
  326. /// <summary>
  327. /// 创建委外采购入(委外订单)
  328. /// </summary>
  329. /// <param name="Bills"></param>
  330. /// <returns></returns>
  331. // public string CreateOutsourcingReceiveDocByPO(List<ICSOutsourcingReceiveDoc> Bills)
  332. // {
  333. // string msg = "";
  334. // int num = 0;
  335. // //取得out库单的默认显示模版
  336. // Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "PuArrival", "" + num + "");
  337. // int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  338. // int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  339. // DateTime date = DateTime.Now;
  340. // string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  341. // //取得采购入库单单据 表头ID,表体DID
  342. // VouchKey key = new VouchKey();
  343. // foreach (ICSOutsourcingReceiveDoc head in Bills)
  344. // {
  345. // num = head.ICSoutReceDoc.Count();
  346. // }
  347. // SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  348. // conn.Open();
  349. // SqlTransaction sqlTran = conn.BeginTransaction();
  350. // SqlCommand cmd = new SqlCommand();
  351. // cmd.Transaction = sqlTran;
  352. // cmd.Connection = conn;
  353. // try
  354. // {
  355. // if (Bills.Count <= 0)
  356. // {
  357. // throw new Exception("传送数据为空!");
  358. // }
  359. // LogInfo(Bills);
  360. // foreach (ICSOutsourcingReceiveDoc head in Bills)
  361. // {
  362. // string sql = "";
  363. // #region 验证数量不能大于到货单数量
  364. // foreach (ICSOutsourcingReceiveDocs body in head.ICSoutReceDoc)
  365. // {
  366. // sql = "SELECT isnull(-iQuantity,0) FROM dbo.PU_ArrivalVouchs WHERE Autoid='" + body.ODNDetailID + "'";
  367. // DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  368. // if (dtQty != null && dtQty.Rows.Count > 0)
  369. // {
  370. // //到货单可入数量
  371. // decimal PUQty = Convert.ToDecimal(dtQty.Rows[0][0]);
  372. // sql = "SELECT ISNULL(SUM(-iQuantity),0) FROM dbo.RdRecords01 WHERE iArrsId='" + body.ODNDetailID + "'";
  373. // DataTable dtReceQty = DBHelper.SQlReturnData(sql, cmd);
  374. // decimal ReceQty = Convert.ToDecimal(dtReceQty.Rows[0][0]);
  375. // if (ReceQty + body.Quantity > PUQty)
  376. // {
  377. // throw new Exception("入库数量大于当前到货单行可入库数量!");
  378. // }
  379. // }
  380. // else
  381. // {
  382. // throw new Exception("U8不存在该到货单行!");
  383. // }
  384. // }
  385. // #endregion
  386. // #region 委外采购入库单 表头
  387. // sql = @"INSERT INTO dbo.RdRecord01
  388. // ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,
  389. // dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cPTCode ,
  390. // cVenCode ,cOrderCode ,cARVCode ,cHandler ,cMemo ,
  391. // bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,
  392. // cDefine4 ,cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,
  393. // cDefine9 ,cDefine10 ,dVeriDate ,bpufirst ,biafirst ,
  394. // dARVDate ,VT_ID ,bIsSTQc ,cDefine11 ,cDefine12 ,
  395. // cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  396. // ipurarriveid ,iTaxRate ,iExchRate ,cExch_Name ,bOMFirst,
  397. // bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  398. // ireturncount ,iverifystate ,iswfcontrolled ,
  399. // cModifyPerson ,dnmaketime ,dnverifytime ,
  400. // bredvouch ,bCredit ,iPrintCount)
  401. // SELECT @ID,1,'01','委外加工','委外到货单',@cWhCode,
  402. // CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,'101',@cDepCode,a.cPersonCode,'01',
  403. // a.cVenCode,b.cCode,a.cCode,@cHandler,a.cMemo,
  404. // 0,@cMaker,a.cDefine1,a.cDefine2,a.cDefine3,
  405. // a.cDefine4,a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,
  406. // a.cDefine9,a.cDefine10,CONVERT(NVARCHAR(15),GETDATE(),23),0,0,
  407. // a.dDate,@VT_ID,0,a.cDefine11,a.cDefine12,
  408. // a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  409. // a.ID,a.iTaxRate,a.iExchRate,a.cexch_name,0,
  410. // 0,0,0,
  411. // 0,0,0,
  412. // '',GETDATE(),GETDATE(),
  413. // 0,0,0
  414. // FROM dbo.PU_ArrivalVouch a LEFT JOIN dbo.OM_MOMain b ON a.cpocode=b.cCode WHERE b.cCode='" + head.ODNCode + "'";
  415. // cmd.Parameters.Clear();
  416. // cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  417. // cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  418. // cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  419. // cmd.Parameters.Add(new SqlParameter("@cDepCode", ""));
  420. // cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  421. // cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  422. // cmd.Parameters.Add(new SqlParameter("@VT_ID", iChildId));
  423. // cmd.CommandText = sql;
  424. // try
  425. // {
  426. // int count = cmd.ExecuteNonQuery();
  427. // if (count <= 0)
  428. // {
  429. // log.Error("委外采购入库单表头失败,受影响行数<=0;");
  430. // throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  431. // }
  432. // }
  433. // catch (Exception ex)
  434. // {
  435. // log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  436. // throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  437. // }
  438. // #endregion
  439. // #region 采购入库单 表体
  440. // int irowno = 0;
  441. // foreach (ICSOutsourcingReceiveDocs body in head.ICSoutReceDoc)
  442. // {
  443. // sql = @"INSERT INTO dbo.rdrecords01
  444. // ( AutoID ,ID ,cInvCode ,iQuantity ,iUnitCost ,iPrice ,iAPrice ,
  445. // iFlag ,iSQuantity ,iSNum ,iMoney ,
  446. // cDefine22 ,cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  447. // cItem_class ,cItemCode ,iPOsID ,fACost ,cName ,iNQuantity ,
  448. // chVencode ,iArrsId ,iOriTaxCost ,iOriCost ,iOriMoney ,iOriTaxPrice ,
  449. // ioriSum ,iTaxRate ,iTaxPrice ,iSum ,bTaxCost ,cPOID ,iMatSettleState ,
  450. // iBillSettleCount ,bLPUseFree ,iOriTrackID ,bCosting ,cbarvcode ,
  451. // dbarvdate ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,bgift,cBatch,iProcessFee, iProcessCost )
  452. // SELECT @AutoID,@ID,@cInvCode,@iQuantity,0,0,0,
  453. // 0,0,0,0,
  454. // a.cDefine22,a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  455. // a.cItem_class,a.cItemCode,a.iPOsID,0,a.cItemName,a.iQuantity,
  456. // c.cVenCode,a.Autoid,0,0,0,0,
  457. // 0,a.iTaxRate,0,0,1,d.cCode,0,
  458. // 0,0,0,1,c.cCode,
  459. // c.dDate,0,0,0,@irowno,0,@cBatch,a.iCost*convert(decimal, @iQuantity),a.iCost
  460. // FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  461. // INNER JOIN dbo.OM_MOMain d ON b.MOID=d.MOID
  462. // INNER JOIN dbo.PU_ArrivalVouch c ON a.ID=c.ID where b.Autoid='" + body.ODNDetailID + "'";
  463. // cmd.Parameters.Clear();
  464. // cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  465. // cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  466. // cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  467. // cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  468. // cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  469. // cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  470. // cmd.CommandText = sql;
  471. // try
  472. // {
  473. // int count = cmd.ExecuteNonQuery();
  474. // if (count <= 0)
  475. // {
  476. // log.Error("委外采购入库单表头失败,受影响行数<=0;");
  477. // throw new Exception("委外采购入库单表头失败,受影响行数<=0;");
  478. // }
  479. // }
  480. // catch (Exception ex)
  481. // {
  482. // log.Error("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  483. // throw new Exception("委外采购入库单表头失败!SQL:\r\n" + sql, ex);
  484. // }
  485. // //更新现存量
  486. // key.cBustypeUN = "委外加工";
  487. // key.cVouchTypeUN = "01";
  488. // key.TableName = "IA_ST_UnAccountVouch01";
  489. // DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  490. // //OM_MODetails
  491. // sql = @"UPDATE b SET freceivedqty=ISNULL(freceivedqty,0)+" + body.Quantity + @"
  492. // FROM dbo.PU_ArrivalVouchs a LEFT JOIN dbo.OM_MODetails b ON a.iPOsID=b.MODetailsID
  493. // WHERE a.Autoid='" + body.ODNDetailID + "'";
  494. // DBHelper.CmdExecuteNonQuery(sql, cmd, "回写OM_MODetails失败!");
  495. // //回写PU_ArrivalVouchs失败
  496. // sql = "UPDATE dbo.PU_ArrivalVouchs SET fValidInQuan=ISNULL(fValidInQuan,0)+" + body.Quantity + " WHERE Autoid='" + body.ODNDetailID + "'";
  497. // DBHelper.CmdExecuteNonQuery(sql, cmd, "回写PU_ArrivalVouchs失败!");
  498. // //更新待入库数量
  499. // sql = "UPDATE dbo.CurrentStock SET fInQuantity=ISNULL(fInQuantity,0)-" + body.Quantity + " WHERE cInvCode='" + body.InvCode + "' AND cWhCode ='" + head.WHCode + "'";
  500. // if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
  501. // {
  502. // //if (!string.IsNullOrWhiteSpace(body.cBatch))
  503. // //{
  504. // // sql += " and cBatch='" + body.cBatch + "'";
  505. // //}
  506. // }
  507. // DBHelper.CmdExecuteNonQuery(sql, cmd, "更新待入库数量失败!");
  508. // #region 判断现存量是否足够
  509. // sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fInQuantity<0)
  510. // BEGIN
  511. // DECLARE @MSG NVARCHAR(100)
  512. // SELECT @MSG='ERP待入库数量不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fInQuantity<0
  513. // RAISERROR(@MSG,16,1)
  514. // END";
  515. // cmd.CommandText = sql;
  516. // cmd.ExecuteNonQuery();
  517. // #endregion
  518. // }
  519. // #endregion
  520. // }
  521. // GetSerialCode(iFatherId.ToString());
  522. // cmd.Transaction.Commit();
  523. // return msg;
  524. // }
  525. // catch (Exception ex)
  526. // {
  527. // cmd.Transaction.Rollback();
  528. // log.Error(ex.Message);
  529. // throw new Exception(ex.Message);
  530. // }
  531. // finally
  532. // {
  533. // if (conn.State == ConnectionState.Open)
  534. // {
  535. // conn.Close();
  536. // }
  537. // conn.Dispose();
  538. // }
  539. // }
  540. /// <summary>
  541. /// 审核委外采购入库
  542. /// </summary>
  543. /// <param name="infos"></param>
  544. /// <returns></returns>
  545. public string Approve(List<ICSOutsourcingReceiveDocNegative> infos)
  546. {
  547. List<ICSSalesShipmentDoc> szJson = new List<ICSSalesShipmentDoc>();
  548. string json = "";
  549. if (infos.Count <= 0)
  550. {
  551. throw new Exception("传送数据为空!");
  552. }
  553. string res = string.Empty;
  554. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  555. conn.Open();
  556. SqlTransaction sqlTran = conn.BeginTransaction();
  557. SqlCommand cmd = new SqlCommand();
  558. cmd.Transaction = sqlTran;
  559. cmd.Connection = conn;
  560. try
  561. {
  562. string sql = string.Empty;
  563. foreach (ICSOutsourcingReceiveDocNegative info in infos)
  564. {
  565. if (info.MTime < new DateTime(2000, 01, 01))
  566. throw new Exception("请输入正确的操作时间:" + info.MTime);
  567. sql = @"UPDATE RdRecord01 SET cHandler ='" + info.User + @"' ,
  568. dnverifytime=CONVERT(VARCHAR(50),GETDATE(),112),dVeriDate=GETDATE() WHERE ID='{0}'";
  569. sql = string.Format(sql, info.ID);
  570. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核委外采购入库单失败!");
  571. }
  572. cmd.Transaction.Commit();
  573. return json;
  574. }
  575. catch (Exception ex)
  576. {
  577. cmd.Transaction.Rollback();
  578. log.Error(ex.Message);
  579. throw new Exception(ex.Message);
  580. }
  581. finally
  582. {
  583. if (conn.State == ConnectionState.Open)
  584. {
  585. conn.Close();
  586. }
  587. conn.Dispose();
  588. }
  589. }
  590. /// <summary>
  591. /// 删除委外采购入库单
  592. /// </summary>
  593. /// <param name="infos"></param>
  594. /// <returns></returns>
  595. public string Delete(List<ICSOutsourcingReceiveDocNegative> infos)
  596. {
  597. List<ICSSalesShipmentDoc> szJson = new List<ICSSalesShipmentDoc>();
  598. if (infos.Count <= 0)
  599. {
  600. throw new Exception("传送数据为空!");
  601. }
  602. string res = string.Empty;
  603. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  604. conn.Open();
  605. SqlTransaction sqlTran = conn.BeginTransaction();
  606. SqlCommand cmd = new SqlCommand();
  607. cmd.Transaction = sqlTran;
  608. cmd.Connection = conn;
  609. try
  610. {
  611. string sql = string.Empty;
  612. foreach (ICSOutsourcingReceiveDocNegative info in infos)
  613. {
  614. if (info.MTime < new DateTime(2000, 01, 01))
  615. throw new Exception("请输入正确的操作时间:" + info.MTime);
  616. sql = @"delete RdRecord01 where RdRecord01.ID='" + info.ID + "'";
  617. sql += @"delete RdRecords01 where RdRecords01.ID='" + info.ID + "'";
  618. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  619. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  620. // WHERE a.ID='" + info.ID + "'";
  621. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除委外采购入库单失败!");
  622. }
  623. cmd.Transaction.Commit();
  624. return res;
  625. ;
  626. }
  627. catch (Exception ex)
  628. {
  629. cmd.Transaction.Rollback();
  630. log.Error(ex.Message);
  631. throw new Exception(ex.Message);
  632. }
  633. finally
  634. {
  635. if (conn.State == ConnectionState.Open)
  636. {
  637. conn.Close();
  638. }
  639. conn.Dispose();
  640. }
  641. }
  642. /// <summary>
  643. ///
  644. /// 记录日志
  645. /// </summary>
  646. /// <param name="Bills"></param>
  647. private void LogInfo(List<ICSOutsourcingReceiveDocNegative> Bills)
  648. {
  649. string HeadList = string.Empty;
  650. string BodyList = string.Empty;
  651. foreach (ICSOutsourcingReceiveDocNegative head in Bills)
  652. {
  653. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User;
  654. foreach (ICSOutsourcingReceiveDocNegatives body in head.details)
  655. {
  656. BodyList += "\r\n 表体主键ID: " + body.ODNRTDetailID + ",数量:" + body.Quantity;
  657. }
  658. }
  659. log.Info(HeadList);
  660. log.Info(BodyList);
  661. }
  662. public string GetSerialCode(string ID)
  663. {
  664. string sql = "EXEC Addins_GetSerialCode '{0}'";
  665. sql = string.Format(sql, new object[] { ID });
  666. return DBHelper.ExecuteScalar(CommandType.Text, sql, connString).ToString();
  667. }
  668. }
  669. }