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

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