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

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