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

3078 lines
128 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
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
  1. using ICSSoft.Entity;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using ICSSoft.Entity;
  13. using System.IO;
  14. namespace ICSSoft.Common
  15. {
  16. public class DBHelper
  17. {
  18. private static log4net.ILog log = log4net.LogManager.GetLogger("U8Helper");
  19. //private static string connString = System.Configuration.ConfigurationManager.AppSettings["ConnStr"];
  20. private static string ERPConnString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  21. //private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  22. //// 访问数据库所需要的适配器
  23. //public static string connString = AppConfig.StrConnection;
  24. ///// <summary>
  25. ///// 根据查询语句获取一个DataTable,对异常捕获无指定要求
  26. ///// </summary>
  27. ///// <param name="select">查询语句</param>
  28. ///// <returns>所查询数据</returns>
  29. //public static DataTable GetDataTable(string select)
  30. //{
  31. // using (SqlConnection m_cnn = new SqlConnection(connString))
  32. // {
  33. // DataTable dt = new DataTable();
  34. // SqlDataAdapter m_da = new SqlDataAdapter(select, m_cnn);
  35. // m_da.SelectCommand.CommandTimeout = 600;
  36. // m_da.SelectCommand.CommandType = CommandType.Text;
  37. // m_da.SelectCommand.CommandText = select;
  38. // m_da.Fill(dt);
  39. // return dt;
  40. // }
  41. //}
  42. public static void WriteLogFile(string input, string txtName)
  43. {
  44. try
  45. {
  46. string logAdress = ConfigurationManager.AppSettings["logAdress"].ToString() + "\\Log\\";
  47. if (!System.IO.Directory.Exists(logAdress))
  48. {
  49. System.IO.Directory.CreateDirectory(logAdress);//不存在就创建目录
  50. }
  51. string adress = logAdress + txtName;
  52. if (!System.IO.Directory.Exists(adress))
  53. {
  54. System.IO.Directory.CreateDirectory(adress);//不存在就创建目录
  55. }
  56. // string logAdress = ConfigurationManager.AppSettings["logAdress"].ToString();
  57. /**/
  58. ///指定日志文件的目录
  59. string fname = adress + "\\" + "log" + DateTime.Now.ToString("yy-MM-dd") + ".txt";
  60. /**/
  61. ///定义文件信息对象
  62. FileInfo finfo = new FileInfo(fname);
  63. if (!finfo.Exists)
  64. {
  65. FileStream fs;
  66. fs = File.Create(fname);
  67. fs.Close();
  68. finfo = new FileInfo(fname);
  69. }
  70. /**/
  71. ///判断文件是否存在以及是否大于2K
  72. if (finfo.Length > 1024 * 1024 * 10)
  73. {
  74. /**/
  75. ///文件超过10MB则重命名
  76. ///
  77. string adressnew = logAdress + "\\" + "备份" + "\\" + txtName + DateTime.Now.ToString("yyyy-MM-dd HHmmss") + ".txt";
  78. File.Move(fname, adressnew);
  79. /**/
  80. ///删除该文件
  81. //finfo.Delete();
  82. }
  83. //finfo.AppendText();
  84. /**/
  85. ///创建只写文件流
  86. using (FileStream fs = finfo.OpenWrite())
  87. {
  88. /**/
  89. ///根据上面创建的文件流创建写数据流
  90. StreamWriter w = new StreamWriter(fs);
  91. /**/
  92. ///设置写数据流的起始位置为文件流的末尾
  93. w.BaseStream.Seek(0, SeekOrigin.End);
  94. w.WriteLine("*****************Start*****************");
  95. w.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  96. /**/
  97. ///写入当前系统时间并换行
  98. /**/
  99. ///写入日志内容并换行
  100. w.WriteLine(input);
  101. /**/
  102. ///写入------------------------------------“并换行
  103. w.WriteLine("------------------END------------------------");
  104. /**/
  105. ///清空缓冲区内容,并把缓冲区内容写入基础流
  106. w.Flush();
  107. /**/
  108. ///关闭写数据流
  109. w.Close();
  110. }
  111. }
  112. catch (Exception ex)
  113. { }
  114. }
  115. /// <summary>
  116. /// 事物取DataTable
  117. /// </summary>
  118. /// <param name="SQl"></param>
  119. /// <param name="cmd"></param>
  120. /// <returns></returns>
  121. public static DataTable SQlReturnData(string SQl, SqlCommand cmd)
  122. {
  123. DataTable dt = new DataTable();
  124. SqlDataAdapter dr = new System.Data.SqlClient.SqlDataAdapter();
  125. cmd.CommandText = SQl;
  126. dr.SelectCommand = cmd;
  127. dr.Fill(dt);
  128. return dt;
  129. }
  130. /// <summary>
  131. /// 依靠数据库连接字符串connectionString,
  132. /// 使用所提供参数,执行返回首行首列命令
  133. /// </summary>
  134. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  135. /// <param name="commandText">存储过程名称或者T-SQL命令行</param>
  136. /// <returns>返回一个对象,使用Convert.To{Type}将该对象转换成想要的数据类型。</returns>
  137. public static object ExecuteScalar(CommandType cmdType, string cmdText, string conn)
  138. {
  139. try
  140. {
  141. DbCommand cmd = CreateDbCommand();
  142. using (DbConnection connection = CreateDbConnection(conn))
  143. {
  144. PrepareCommand(cmd, connection, null, cmdType, cmdText, null);
  145. object val = cmd.ExecuteScalar();
  146. cmd.Parameters.Clear();
  147. return val;
  148. }
  149. }
  150. catch (Exception ex)
  151. {
  152. //log.Error(ex.Message);
  153. throw;
  154. }
  155. }
  156. /// <summary>
  157. /// 为即将执行准备一个命令
  158. /// </summary>
  159. /// <param name="cmd">SqlCommand对象</param>
  160. /// <param name="conn">SqlConnection对象</param>
  161. /// <param name="isOpenTrans">DbTransaction对象</param>
  162. /// <param name="cmdType">执行命令的类型(存储过程或T-SQL,等等)</param>
  163. /// <param name="cmdText">存储过程名称或者T-SQL命令行, e.g. Select * from Products</param>
  164. /// <param name="cmdParms">SqlParameters to use in the command</param>
  165. private static void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction isOpenTrans, CommandType cmdType, string cmdText, DbParameter[] cmdParms)
  166. {
  167. if (conn.State != ConnectionState.Open)
  168. conn.Open();
  169. cmd.Connection = conn;
  170. cmd.CommandText = cmdText;
  171. if (isOpenTrans != null)
  172. cmd.Transaction = isOpenTrans;
  173. cmd.CommandType = cmdType;
  174. if (cmdParms != null)
  175. {
  176. cmd.Parameters.AddRange(cmdParms);
  177. }
  178. }
  179. /// <summary>
  180. /// 数据库类型
  181. /// </summary>
  182. public static DatabaseType DbType { get; set; }
  183. /// <summary>
  184. /// 根据配置文件中所配置的数据库类型和传入的
  185. /// 数据库链接字符串来创建相应数据库连接对象
  186. /// </summary>
  187. /// <param name="connectionString"></param>
  188. /// <returns></returns>
  189. public static DbConnection CreateDbConnection(string connectionString)
  190. {
  191. DbConnection conn = null;
  192. switch (DbType)
  193. {
  194. case DatabaseType.SqlServer:
  195. conn = new SqlConnection(connectionString);
  196. break;
  197. //case DatabaseType.Oracle:
  198. // conn = new OracleConnection(connectionString);
  199. // break;
  200. //case DatabaseType.MySql:
  201. // conn = new MySqlConnection(connectionString);
  202. // break;
  203. //case DatabaseType.Access:
  204. // conn = new OleDbConnection(connectionString);
  205. // break;
  206. //case DatabaseType.SQLite:
  207. // conn = new SQLiteConnection(connectionString);
  208. // break;
  209. default:
  210. throw new Exception("数据库类型目前不支持!");
  211. }
  212. return conn;
  213. }
  214. /// <summary>
  215. /// 根据配置文件中所配置的数据库类型
  216. /// 来创建相应数据库命令对象
  217. /// </summary>
  218. /// <returns></returns>
  219. public static DbCommand CreateDbCommand()
  220. {
  221. DbCommand cmd = null;
  222. switch (DbType)
  223. {
  224. case DatabaseType.SqlServer:
  225. cmd = new SqlCommand();
  226. break;
  227. //case DatabaseType.Oracle:
  228. // cmd = new OracleCommand();
  229. // break;
  230. //case DatabaseType.MySql:
  231. // cmd = new MySqlCommand();
  232. // break;
  233. //case DatabaseType.Access:
  234. // cmd = new OleDbCommand();
  235. // break;
  236. //case DatabaseType.SQLite:
  237. // cmd = new SQLiteCommand();
  238. // break;
  239. default:
  240. throw new Exception("数据库类型目前不支持!");
  241. }
  242. return cmd;
  243. }
  244. public enum DatabaseType
  245. {
  246. /// <summary>
  247. /// 数据库类型:Oracle
  248. /// </summary>
  249. Oracle,
  250. /// <summary>
  251. /// 数据库类型:SqlServer
  252. /// </summary>
  253. SqlServer,
  254. /// <summary>
  255. /// 数据库类型:Access
  256. /// </summary>
  257. Access,
  258. /// <summary>
  259. /// 数据库类型:MySql
  260. /// </summary>
  261. MySql,
  262. /// <summary>
  263. /// 数据库类型:SQLite
  264. /// </summary>
  265. SQLite
  266. }
  267. public static bool ExecuteNonQuery(string sql, SqlCommand cmd)
  268. {
  269. try
  270. {
  271. cmd.CommandText = sql;
  272. int result = cmd.ExecuteNonQuery();
  273. if (result > 0)
  274. {
  275. return true;
  276. }
  277. else
  278. {
  279. log.Info("SQL执行受影响行数<0;" + sql);
  280. return false;
  281. }
  282. }
  283. catch (Exception ex)
  284. {
  285. string Params = string.Empty;
  286. foreach (SqlParameter parameter in cmd.Parameters)
  287. {
  288. Params += parameter.SqlValue + "||";
  289. }
  290. log.Error("异常:" + ex.Message + ";\r\n SQL:" + sql + "参数:" + Params);
  291. throw new Exception("程序异常!");
  292. }
  293. }
  294. /// <summary>
  295. /// 获取插入单据的表头表体最大ID
  296. /// </summary>
  297. /// <param name="IDtype"></param>
  298. /// <param name="cAcc_id"></param>
  299. /// <param name="rowCount"></param>
  300. /// <param name="id"></param>
  301. /// <param name="did"></param>
  302. public static void SaveGetrdIDandDID(string IDtype, string cAcc_id, int rowCount, out int id, out int did, SqlCommand cmd)
  303. {
  304. cmd.Parameters.Clear();
  305. try
  306. {
  307. string[] ss = cAcc_id.Split('_');
  308. string ErpCount = ss[1];
  309. string str = @"DECLARE @ID int
  310. DECLARE @DID int
  311. SET @ID = 0
  312. SET @DID = 0
  313. IF NOT EXISTS (SELECT * FROM UFSystem..ua_identity WHERE cacc_id = '{0}' AND cVouchType = '{1}')
  314. BEGIN
  315. INSERT INTO UFSystem..ua_identity(cAcc_Id,cVouchType,iFatherId,iChildId) VALUES('{0}','{1}',1,1)
  316. END
  317. ELSE
  318. BEGIN
  319. UPDATE UFSystem..ua_identity
  320. SET ifatherID = ifatherID +1,ichildID = ichildID + {2}
  321. WHERE cVouchType = '{1}' AND cAcc_id = '{0}'
  322. END
  323. select ifatherID as ID,ichildID as DID FROM UFSystem..ua_identity WHERE cVouchType = '{1}' AND cAcc_id = '{0}' ";
  324. str = string.Format(str, ErpCount, IDtype, rowCount.ToString());
  325. DataTable dt = SQlReturnData(str, cmd);
  326. if (dt.Rows.Count == 0)
  327. {
  328. throw new Exception("ID取得失败");
  329. }
  330. id = Convert.ToInt32(dt.Rows[0]["ID"]);
  331. did = Convert.ToInt32(dt.Rows[0]["DID"]);
  332. #region 测试时屏蔽
  333. // if (IDtype == "rd")
  334. // {
  335. // string sql = @"SELECT CONVERT(BIGINT,SUBSTRING(CONVERT(NVARCHAR(50),MAX(ID)) ,2,LEN(CONVERT(NVARCHAR(50),MAX(ID)))-1)) AS ID ,CONVERT(BIGINT,SUBSTRING(CONVERT(NVARCHAR(50),MAX(DID)) ,2,LEN(CONVERT(NVARCHAR(50),MAX(DID)))-1)) AS DID FROM (
  336. // SELECT MAX(ID) AS ID,MAX(AutoID) AS DID FROM rdrecords01
  337. // UNION
  338. // SELECT MAX(ID) AS ID,MAX(AutoID) AS DID FROM rdrecords08
  339. // UNION
  340. // SELECT MAX(ID) AS ID,MAX(AutoID) AS DID FROM rdrecords09
  341. // UNION
  342. // SELECT MAX(ID) AS ID,MAX(AutoID) AS DID FROM rdrecords10
  343. // UNION
  344. // SELECT MAX(ID) AS ID,MAX(AutoID) AS DID FROM rdrecords11
  345. // UNION
  346. // SELECT MAX(ID) AS ID,MAX(AutoID) AS DID FROM rdrecords32
  347. // ) a";
  348. // DataTable dtCheck = SQlReturnData(sql, cmd);
  349. // if (dtCheck != null && dtCheck.Rows.Count > 0)
  350. // {
  351. // if (id <= Convert.ToInt32(dtCheck.Rows[0]["ID"].ToString()) || did <= Convert.ToInt32(dtCheck.Rows[0]["DID"].ToString()))
  352. // {
  353. // id = Convert.ToInt32(dtCheck.Rows[0]["ID"].ToString()) + 1;
  354. // did = Convert.ToInt32(dtCheck.Rows[0]["DID"].ToString()) + 1;
  355. // sql = string.Format(@" UPDATE UFSystem..ua_identity
  356. // SET ifatherID = {0}, ichildID = {1}
  357. // WHERE cVouchType = '{2}' AND cAcc_id = '{3}' ", id, did, IDtype, ErpCount);
  358. // cmd.CommandText = sql;
  359. // int i = cmd.ExecuteNonQuery();
  360. // }
  361. // }
  362. // }
  363. #endregion
  364. }
  365. catch (Exception ex)
  366. {
  367. throw new Exception(ex.Message);
  368. }
  369. }
  370. public static void SaveGetrdIDandDIDs(string IDtype, string cAcc_id, int rowCount, out int id, out int did, SqlCommand cmd)
  371. {
  372. cmd.Parameters.Clear();
  373. try
  374. {
  375. string[] ss = cAcc_id.Split('_');
  376. string ErpCount = ss[1];
  377. string str = @"DECLARE @ID int
  378. DECLARE @DID int
  379. SET @ID = 0
  380. SET @DID = 0
  381. IF NOT EXISTS (SELECT * FROM UFSystem..ua_identity WHERE cacc_id = '{0}' AND cVouchType = '{1}')
  382. BEGIN
  383. INSERT INTO UFSystem..ua_identity(cAcc_Id,cVouchType,iFatherId,iChildId) VALUES('{0}','{1}',1,1)
  384. END
  385. ELSE
  386. BEGIN
  387. UPDATE UFSystem..ua_identity SET ichildID = ichildID + {2}
  388. WHERE cVouchType = '{1}' AND cAcc_id = '{0}'
  389. END
  390. select ifatherID as ID,ichildID as DID FROM UFSystem..ua_identity WHERE cVouchType = '{1}' AND cAcc_id = '{0}' ";
  391. str = string.Format(str, ErpCount, IDtype, rowCount.ToString());
  392. DataTable dt = SQlReturnData(str, cmd);
  393. if (dt.Rows.Count == 0)
  394. {
  395. throw new Exception("ID取得失败");
  396. }
  397. id = Convert.ToInt32(dt.Rows[0]["ID"]);
  398. did = Convert.ToInt32(dt.Rows[0]["DID"]);
  399. }
  400. catch (Exception ex)
  401. {
  402. throw new Exception(ex.Message);
  403. }
  404. }
  405. /// <summary>
  406. /// 更新主键ID
  407. /// </summary>
  408. /// <param name="IDtype"></param>
  409. /// <param name="cAcc_id"></param>
  410. /// <param name="rowCount"></param>
  411. public static void UpdateIDandDID(string IDtype, string cAcc_id, int rowCount, SqlCommand cmd)
  412. {
  413. try
  414. {
  415. string[] ss = cAcc_id.Split('_');
  416. string ErpCount = ss[1];
  417. string sql = @" UPDATE UFSystem..ua_identity
  418. SET ifatherID = ifatherID +1,ichildID = ichildID + {2}
  419. WHERE cVouchType = '{1}' AND cAcc_id = '{0}' ";
  420. sql = string.Format(sql, ErpCount, IDtype, rowCount.ToString());
  421. cmd.CommandText = sql;
  422. int i = cmd.ExecuteNonQuery();
  423. if (i <= 0)
  424. {
  425. throw new Exception("更新主键ID,DID失败!");
  426. }
  427. }
  428. catch (Exception ex)
  429. {
  430. throw new Exception(ex.Message);
  431. }
  432. }
  433. //public static int ExecuteSql(string sqlUpdate)
  434. //{
  435. // using (SqlConnection conn = new SqlConnection(connString))
  436. // {
  437. // try
  438. // {
  439. // SqlCommand cmd = new SqlCommand();
  440. // cmd.CommandText = sqlUpdate;
  441. // if (conn.State != ConnectionState.Open)
  442. // {
  443. // conn.Open();
  444. // }
  445. // cmd.Connection = conn;
  446. // return cmd.ExecuteNonQuery();
  447. // }
  448. // catch (Exception ex)
  449. // {
  450. // throw new Exception(ex.Message);
  451. // }
  452. // finally
  453. // {
  454. // if (conn.State != ConnectionState.Closed)
  455. // {
  456. // conn.Close();
  457. // }
  458. // }
  459. // }
  460. //}
  461. /// <summary>
  462. /// 执行Insert 或者 Update
  463. /// </summary>
  464. /// <param name="sql"></param>
  465. /// <param name="cmd"></param>
  466. /// <param name="message"></param>
  467. public static void CmdExecuteNonQuery(string sql, SqlCommand cmd, string message)
  468. {
  469. try
  470. {
  471. cmd.CommandText = sql;
  472. int count = cmd.ExecuteNonQuery();
  473. if (count <= 0)
  474. {
  475. string Msg = string.Empty;
  476. foreach (SqlParameter parameter in cmd.Parameters)
  477. {
  478. Msg += "参数名:" + parameter.ParameterName + "参数值:" + parameter.Value;
  479. }
  480. log.Info("受影响行数小于0;" + sql + "\r\n" + Msg);
  481. throw new Exception(message);
  482. }
  483. }
  484. catch (Exception ex)
  485. {
  486. string Msg = string.Empty;
  487. foreach (SqlParameter parameter in cmd.Parameters)
  488. {
  489. Msg += "参数名:" + parameter.ParameterName + "参数值:" + parameter.Value;
  490. }
  491. log.Info("异常:" + ex.Message + "\r\n " + message + "\r\n SQL:" + sql + "\r\n" + Msg);
  492. throw new Exception(message + Environment.NewLine + ex.Message);
  493. }
  494. }
  495. /// <summary>
  496. /// 1 取得单据的默认模板
  497. /// </summary>
  498. /// <param name="ErpName">账套名</param>
  499. /// <param name="CardNumber"></param>
  500. /// <param name="cmd"></param>
  501. /// <returns></returns>
  502. public static string GetDefaultTemplate( string CardNumber, SqlCommand cmd)
  503. {
  504. try
  505. {
  506. string sql = "";
  507. string VouchDEF_ID = string.Empty;
  508. sql = string.Format("SELECT DEF_ID FROM Vouchers WHERE CardNumber = '{0}' ", CardNumber);
  509. cmd.CommandText = sql;
  510. DataTable dtDEF_ID = SQlReturnData(sql, cmd);
  511. if (dtDEF_ID != null && dtDEF_ID.Rows.Count > 0)
  512. {
  513. VouchDEF_ID = dtDEF_ID.Rows[0]["DEF_ID"].ToString();
  514. }
  515. else
  516. {
  517. throw new Exception("获取默认显示模板失败!" + sql);
  518. }
  519. return VouchDEF_ID;
  520. }
  521. catch (Exception ex)
  522. {
  523. throw new Exception(ex.Message);
  524. }
  525. }
  526. /// <summary>
  527. /// 2 取得单据的表头ID,表体DID
  528. /// </summary>
  529. /// <param name="VouchType">单据类型 如“rd”</param>
  530. /// <param name="ErpName">账套名</param>
  531. /// <param name="RowCount">表体行</param>
  532. /// <returns></returns>
  533. public static VouchKey GetPrimaryKey(string VouchType, string ErpName, int RowCount, SqlCommand cmd)
  534. {
  535. try
  536. {
  537. string num = "1000000000";
  538. int id = 0;
  539. int did = 0;
  540. VouchKey model = new VouchKey();
  541. SaveGetrdIDandDID(VouchType, ErpName, RowCount, out id, out did, cmd);
  542. model.ID = int.Parse(num.Substring(0, 10 - (id.ToString().Length)) + id.ToString());
  543. model.DID = int.Parse(num.Substring(0, 10 - (did.ToString().Length)) + did.ToString());
  544. return model;
  545. }
  546. catch (Exception ex)
  547. {
  548. throw new Exception(ex.Message);
  549. }
  550. }
  551. public static VouchKey GetPrimaryKeys(string VouchType, string ErpName, int RowCount, SqlCommand cmd)
  552. {
  553. try
  554. {
  555. string num = "1000000000";
  556. int id = 0;
  557. int did = 0;
  558. VouchKey model = new VouchKey();
  559. SaveGetrdIDandDIDs(VouchType, ErpName, RowCount, out id, out did, cmd);
  560. model.DID = int.Parse(num.Substring(0, 10 - (did.ToString().Length)) + did.ToString());
  561. return model;
  562. }
  563. catch (Exception ex)
  564. {
  565. throw new Exception(ex.Message);
  566. }
  567. }
  568. /// <summary>
  569. /// 3 取得单据编号
  570. /// </summary>
  571. /// <param name="ErpName">账套名</param>
  572. /// <param name="CardNumber"></param>
  573. /// <param name="PreStr">单据前缀</param>
  574. /// <param name="cmd"></param>
  575. /// <returns></returns>
  576. public static int GetVouchCode(string ErpName, string CardNumber, string PreStr, string TableName, SqlCommand cmd)
  577. {
  578. string sql = "";
  579. sql = string.Format("select * from {0}.dbo.VoucherHistory where cSeed = Substring(Convert( varchar(100),GetDate(),112),3,4) and CardNumber = '{1}'", ErpName, CardNumber);
  580. DataTable dt = SQlReturnData(sql, cmd);
  581. if (dt != null && dt.Rows.Count > 0)
  582. {
  583. sql = string.Format(@"UPDATE {0}.dbo.VoucherHistory SET cNumber =cast(( cast(cNumber as int)+1) as nvarchar(30))
  584. WHERE cSeed = SUBSTRING(CONVERT(varchar(100), GETDATE(), 112),3,4) AND CardNumber = '{1}'", ErpName, CardNumber);
  585. CmdExecuteNonQuery(sql, cmd, "更新VoucherHistory表失败!");
  586. }
  587. else
  588. {
  589. sql = string.Format(@"INSERT INTO {0}.dbo.VoucherHistory
  590. SELECT '{1}',NULL,'','',SUBSTRING(CONVERT(varchar(100), GETDATE(), 112),3,4),1,0", ErpName, CardNumber);
  591. CmdExecuteNonQuery(sql, cmd, "插入VoucherHistory表失败!");
  592. }
  593. sql = string.Format(@"DECLARE @Code nvarchar(100)
  594. SELECT @Code = '00000' + CAST(cNumber AS NVARCHAR(50)) FROM {0}.dbo.VoucherHistory
  595. WHERE cSeed = SUBSTRING(CONVERT(varchar(100), GETDATE(), 112),3,4) AND CardNumber = '{1}'
  596. SET @Code = SUBSTRING(CONVERT(varchar(100), GETDATE(), 112),3,4)+RIGHT(@Code,4)
  597. select @Code", ErpName, CardNumber);
  598. int cCode = 0;
  599. cmd.CommandText = sql;
  600. DataTable dtCode = SQlReturnData(sql, cmd);
  601. if (dtCode != null && dtCode.Rows.Count > 0)
  602. {
  603. cCode = int.Parse(dtCode.Rows[0][0].ToString());
  604. }
  605. else
  606. {
  607. throw new Exception("获取单据号失败!");
  608. }
  609. #region
  610. //sql = string.Format("select cCode from " + TableName + " where cCode='{0}'", (PreStr + cCode).ToString());
  611. //cmd.CommandText = sql;
  612. //DataTable dtCodeCheck = SQlReturnData(sql, cmd);
  613. //if (dtCodeCheck != null && dtCodeCheck.Rows.Count > 0)
  614. //{
  615. // throw new Exception("获取单据号重复,保存失败!");
  616. //}
  617. #endregion
  618. return cCode;
  619. }
  620. public static int GetVouchCode(string ErpName, string CardNumber, string TableName, SqlCommand cmd)
  621. {
  622. string sql = "";
  623. sql = string.Format("select * from {0}.dbo.VoucherHistory where CardNumber = '{1}'", ErpName, CardNumber);
  624. DataTable dt = SQlReturnData(sql, cmd);
  625. if (dt != null && dt.Rows.Count > 0)
  626. {
  627. sql = string.Format(@"UPDATE {0}.dbo.VoucherHistory SET cNumber = cNumber+1
  628. WHERE CardNumber = '{1}'", ErpName, CardNumber);
  629. CmdExecuteNonQuery(sql, cmd, "更新VoucherHistory表失败!");
  630. }
  631. else
  632. {
  633. sql = string.Format(@"INSERT INTO {0}.dbo.VoucherHistory
  634. SELECT '{1}',NULL,'','',null,1,0", ErpName, CardNumber);
  635. CmdExecuteNonQuery(sql, cmd, "插入VoucherHistory表失败!");
  636. }
  637. sql = string.Format(@"DECLARE @Code nvarchar(100)
  638. SELECT @Code = CAST(cNumber AS NVARCHAR(50)) FROM {0}.dbo.VoucherHistory
  639. WHERE CardNumber = '{1}'
  640. SET @Code = RIGHT(@Code,10)
  641. select @Code", ErpName, CardNumber);
  642. int cCode = 0;
  643. cmd.CommandText = sql;
  644. DataTable dtCode = SQlReturnData(sql, cmd);
  645. if (dtCode != null && dtCode.Rows.Count > 0)
  646. {
  647. cCode = int.Parse(dtCode.Rows[0][0].ToString());
  648. }
  649. else
  650. {
  651. throw new Exception("获取单据号失败!");
  652. }
  653. return cCode;
  654. }
  655. public static string GetPreVouchCode(string ErpName, string CardNumber, string PreStr, SqlCommand cmd)
  656. {
  657. string cCode = "";
  658. string sql = string.Format("SELECT * FROM {0}.dbo.VoucherHistory WHERE CardNumber = '{1}' ", ErpName, CardNumber);
  659. DataTable dt = SQlReturnData(sql, cmd);
  660. if (dt != null && dt.Rows.Count > 0)
  661. {
  662. sql = string.Format(@"UPDATE {0}.dbo.VoucherHistory SET cNumber = CAST((CAST(cNumber AS INT) + 1) AS VARCHAR(30)) WHERE CardNumber = '{1}' ", ErpName, CardNumber);
  663. CmdExecuteNonQuery(sql, cmd, "更新VoucherHistory表失败!");
  664. }
  665. else
  666. {
  667. sql = string.Format(@"INSERT INTO {0}.dbo.VoucherHistory SELECT '{1}', NULL, NULL, NULL, NULL, 1, 0 ", ErpName, CardNumber);
  668. CmdExecuteNonQuery(sql, cmd, "插入VoucherHistory表失败!");
  669. }
  670. sql = string.Format(@"
  671. DECLARE @Code NVARCHAR(100)
  672. SELECT @Code = '0000000' + CAST(cNumber AS NVARCHAR(50)) FROM {0}.dbo.VoucherHistory WHERE CardNumber = '{1}'
  673. SET @Code = '{2}' + RIGHT(@Code, 3)
  674. SELECT @Code ", ErpName, CardNumber, PreStr);
  675. cmd.CommandText = sql;
  676. DataTable dtCode = SQlReturnData(sql, cmd);
  677. if (dtCode != null && dtCode.Rows.Count > 0)
  678. {
  679. cCode = dtCode.Rows[0][0].ToString();
  680. }
  681. else
  682. {
  683. throw new Exception("获取单据号失败!");
  684. }
  685. return cCode;
  686. }
  687. public static string GetRdVouchCode(string ErpName, string CardNumber, SqlCommand cmd)
  688. {
  689. string cCode = "";
  690. string sql = string.Format("SELECT * FROM {0}.dbo.VoucherHistory WHERE CardNumber = '{1}' ", ErpName, CardNumber);
  691. DataTable dt = SQlReturnData(sql, cmd);
  692. if (dt != null && dt.Rows.Count > 0)
  693. {
  694. sql = string.Format(@"UPDATE {0}.dbo.VoucherHistory SET cNumber = CAST((CAST(cNumber AS INT) + 1) AS VARCHAR(30)) WHERE CardNumber = '{1}' ", ErpName, CardNumber);
  695. CmdExecuteNonQuery(sql, cmd, "更新VoucherHistory表失败!");
  696. }
  697. else
  698. {
  699. sql = string.Format(@"INSERT INTO {0}.dbo.VoucherHistory SELECT '{1}', NULL, NULL, NULL, NULL, 1, 0 ", ErpName, CardNumber);
  700. CmdExecuteNonQuery(sql, cmd, "插入VoucherHistory表失败!");
  701. }
  702. sql = string.Format(@"
  703. DECLARE @Code NVARCHAR(100)
  704. SELECT @Code = CAST(cNumber AS NVARCHAR(50)) FROM {0}.dbo.VoucherHistory WHERE CardNumber = '{1}'
  705. SET @Code = @Code
  706. SELECT @Code ", ErpName, CardNumber);
  707. cmd.CommandText = sql;
  708. DataTable dtCode = SQlReturnData(sql, cmd);
  709. if (dtCode != null && dtCode.Rows.Count > 0)
  710. {
  711. cCode = dtCode.Rows[0][0].ToString();
  712. }
  713. else
  714. {
  715. throw new Exception("获取单据号失败!");
  716. }
  717. return cCode;
  718. }
  719. ///// <summary>
  720. ///// 获取用户的姓名与部门
  721. ///// </summary>
  722. ///// <param name="cPer_Num">用户编号</param>
  723. ///// <param name="cmd"></param>
  724. ///// <returns></returns>
  725. //public static UserInfo GetPersonInfo(string cPer_Num, SqlCommand cmd)
  726. //{
  727. // UserInfo person = new UserInfo();
  728. // string sql = @"exec sp_refreshview ua_user ;
  729. // SELECT a.cUser_Id,a.cUser_Name,b.cDepCode FROM ua_user a
  730. // LEFT JOIN dbo.Department b ON a.cDept=b.cDepName
  731. // WHERE cUser_Id ='" + cPer_Num + "'";
  732. // cmd.CommandText = sql;
  733. // DataTable dt = SQlReturnData(sql, cmd);
  734. // if (dt != null && dt.Rows.Count > 0)
  735. // {
  736. // person.DepCode = dt.Rows[0]["cDepCode"].ToString();
  737. // person.UserName = dt.Rows[0]["cUser_Name"].ToString();
  738. // person.UserCode = cPer_Num;
  739. // }
  740. // return person;
  741. //}
  742. /// <summary>
  743. /// 更新现存量
  744. /// </summary>
  745. /// <param name="cmd"></param>
  746. /// <param name="cInvCode"></param>
  747. /// <param name="cWhCode"></param>
  748. /// <param name="cBatch"></param>
  749. public static void UpdateCurrentStock(SqlCommand cmd, string cInvCode, string cWhCode, string cBatch, decimal iQuantity, VouchKey key)
  750. {
  751. #region 1 取得物料的itemID
  752. string sql = @"IF NOT EXISTS(
  753. SELECT Id FROM dbo.SCM_Item WHERE
  754. cinvcode = '{0}')
  755. BEGIN
  756. INSERT INTO dbo.SCM_Item(cInvCode ,cFree1 ,cFree2 ,cFree3 ,cFree4 , cFree5 ,
  757. cFree6 ,cFree7 ,cFree8 ,cFree9 ,cFree10 ,PartId) VALUES('{0}','','','','','','','','','','',0)
  758. END
  759. SELECT Id FROM
  760. dbo.SCM_Item WHERE cinvcode = '{0}'";
  761. sql = string.Format(sql, cInvCode);
  762. cmd.CommandText = sql;
  763. DataTable dtItem = SQlReturnData(sql, cmd);
  764. if (dtItem.Rows.Count == 0)
  765. {
  766. throw new Exception("物料的ItemID取得失败");
  767. }
  768. int ItemID = int.Parse(dtItem.Rows[0]["Id"].ToString());
  769. //log.Info("取得物料的itemID" + sql);
  770. #endregion
  771. #region 2 更新失败,插入现存量
  772. sql = @"SELECT AutoID FROM dbo.CurrentStock
  773. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  774. AND ItemId = @ItemId";
  775. if (cBatch != null)
  776. {
  777. if (bInvBatch(cInvCode, cmd) == true)
  778. sql += " and cBatch='" + cBatch + "' ";
  779. else
  780. sql += " and cBatch='' ";
  781. }
  782. else
  783. {
  784. sql += " and cBatch='' ";
  785. }
  786. cmd.CommandText = sql;
  787. cmd.Parameters.Clear();
  788. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  789. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  790. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  791. DataTable dtCurrentStock = SQlReturnData(sql, cmd);
  792. //log.Info("查找现存量:" + sql);
  793. if (dtCurrentStock != null && dtCurrentStock.Rows.Count > 0)
  794. {
  795. sql = @"UPDATE dbo.CurrentStock SET iQuantity = iQuantity + @iQuantity
  796. {0}
  797. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  798. AND ItemId = @ItemId";
  799. if (key.cBustypeUN.Contains("入库") && key.UpdateTodoQuantity == true)
  800. {
  801. sql = string.Format(sql, ",fInQuantity=isnull(fInQuantity,0)-@iQuantity");
  802. }
  803. else if (key.cBustypeUN.Contains("出库") && key.UpdateTodoQuantity == true)
  804. {
  805. sql = string.Format(sql, ",fOutQuantity=isnull(fOutQuantity,0)-@iQuantity");
  806. }
  807. else if (key.cBustypeUN == "调拨" && key.UpdateTodoQuantity == true)
  808. {
  809. sql = string.Format(sql, ", fTransOutQuantity=ISNULL(a.fTransOutQuantity,0)-ISNULL(a.iQuantity,0),fTransInQuantity=ISNULL(fTransInQuantity,0)-ISNULL(a.iQuantity,0)");
  810. }
  811. else
  812. {
  813. sql = string.Format(sql, "");
  814. }
  815. if (cBatch != null)
  816. {
  817. if (bInvBatch(cInvCode, cmd) == true)
  818. sql += " and cBatch='" + cBatch + "' ";
  819. else
  820. sql += " and cBatch='' ";
  821. }
  822. else
  823. {
  824. sql += " and cBatch='' ";
  825. }
  826. cmd.CommandText = sql;
  827. cmd.Parameters.Clear();
  828. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  829. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  830. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  831. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  832. CmdExecuteNonQuery(sql, cmd, "更新现存量失败!");
  833. //log.Info("现存量更新:" + sql);
  834. }
  835. else
  836. {
  837. sql = @" INSERT INTO dbo.CurrentStock
  838. (cWhCode,cInvCode,ItemId,cBatch,iSoType,iSodid,iQuantity,
  839. iNum,cFree1,fOutQuantity,fOutNum,fInQuantity,fInNum,cFree2,
  840. cFree3,bStopFlag,fTransInQuantity,fTransInNum,
  841. fTransOutQuantity,fTransOutNum,fPlanQuantity,fPlanNum,fDisableQuantity,
  842. fDisableNum,fAvaQuantity,fAvaNum,BGSPSTOP,fStopQuantity,
  843. fStopNum,ipeqty,ipenum)
  844. SELECT @cWhCode,@cInvCode,@ItemId,@cBatch,'0','',@iQuantity,
  845. '0','','0','0','0','0','',
  846. '','0','0','0','0','0','0','0','0',
  847. '0','0','0','0','0','0','0','0'";
  848. cmd.CommandText = sql;
  849. cmd.Parameters.Clear();
  850. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  851. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  852. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  853. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  854. if (cBatch != null)
  855. {
  856. if (bInvBatch(cInvCode, cmd) == true)
  857. cmd.Parameters.Add(new SqlParameter("@cBatch", cBatch));
  858. else
  859. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  860. }
  861. else
  862. {
  863. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  864. }
  865. CmdExecuteNonQuery(sql, cmd, "插入现存量失败!");
  866. //log.Info("现存量插入:" + sql);
  867. }
  868. #endregion
  869. #region 判断现存量是否足够
  870. //sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0)
  871. // BEGIN
  872. // DECLARE @MSG NVARCHAR(100)
  873. // SELECT @MSG='ERP库存不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0
  874. // RAISERROR(@MSG,16,1)
  875. // END";
  876. //cmd.CommandText = sql;
  877. //cmd.ExecuteNonQuery();
  878. #endregion
  879. //判断仓库是否记入成本
  880. sql = "SELECT bInCost FROM Warehouse WHERE cWhCode='{0}'";
  881. sql = string.Format(sql, cWhCode);
  882. cmd.CommandText = sql;
  883. DataTable dtwh = SQlReturnData(sql, cmd);
  884. if (!dtwh.Rows[0]["bInCost"].ToString().Equals("0"))
  885. {
  886. #region 3 写入记账表
  887. sql = string.Format(@" INSERT INTO [dbo].[" + key.TableName + @"]
  888. SELECT '{0}','{1}','{2}','{3}'", key.ID, key.DID, key.cVouchTypeUN, key.cBustypeUN);
  889. cmd.CommandText = sql;
  890. CmdExecuteNonQuery(sql, cmd, "采购入库单写入记账表失败!");
  891. #endregion
  892. }
  893. #region 判断现存量是否足够
  894. sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE (iQuantity<0 OR iNum<0 OR fOutQuantity<0) and cWhCode = '{0}' AND cInvCode = '{1}')
  895. BEGIN
  896. DECLARE @MSG NVARCHAR(100)
  897. SELECT @MSG='ERP待出库数量不足AutoID'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE (iQuantity<0 OR iNum<0 OR fOutQuantity<0)
  898. RAISERROR(@MSG,16,1)
  899. END";
  900. sql = string.Format(sql,cWhCode,cInvCode);
  901. cmd.CommandText = sql;
  902. cmd.ExecuteNonQuery();
  903. #endregion
  904. }
  905. /// <summary>
  906. /// 更新现存量
  907. /// </summary>
  908. /// <param name="cmd"></param>
  909. /// <param name="cInvCode"></param>
  910. /// <param name="cWhCode"></param>
  911. /// <param name="cBatch"></param>
  912. public static void UpdateCurrentStockNEW(SqlCommand cmd, string cInvCode, string cWhCode, string cBatch, decimal iQuantity, string cFree1, string cFree2, string cFree3, string cFree4, string cFree5, string cFree6, string cFree7, string cFree8, string cFree9, string cFree10, VouchKey key)
  913. {
  914. string newsql = "";
  915. string newsqlvalues = "";
  916. string NewcBatch="";
  917. NewcBatch += " and cFree1='{1}' ";
  918. newsql += ",cFree1";
  919. newsqlvalues = ",'" + cFree1 + @"'";
  920. NewcBatch += " and cFree2='{2}' ";
  921. newsql += ",cFree2";
  922. newsqlvalues = ",'" + cFree2 + @"'";
  923. NewcBatch += " and cFree3='{3}' ";
  924. newsql += ",cFree3";
  925. newsqlvalues = ",'" + cFree3 + @"'";
  926. NewcBatch += " and cFree4='{4}' ";
  927. newsql += ",cFree4";
  928. newsqlvalues = ",'" + cFree4 + @"'";
  929. NewcBatch += " and cFree5='{5}' ";
  930. newsql += ",cFree5";
  931. newsqlvalues = ",'" + cFree5 + @"'";
  932. NewcBatch += " and cFree6='{6}' ";
  933. newsql += ",cFree6";
  934. newsqlvalues = ",'" + cFree6 + @"'";
  935. NewcBatch += " and cFree7='{7}' ";
  936. newsql += ",cFree7";
  937. newsqlvalues = ",'" + cFree7 + @"'";
  938. NewcBatch += " and cFree8='{8}' ";
  939. newsql += ",cFree8";
  940. newsqlvalues = ",'" + cFree8 + @"'";
  941. NewcBatch += " and cFree9='{9}' ";
  942. newsql += ",cFree9";
  943. newsqlvalues = ",'" + cFree9 + @"'";
  944. NewcBatch += " and cFree10='{10}' ";
  945. newsql += ",cFree10";
  946. newsqlvalues = ",'" + cFree10 + @"'";
  947. #region 1 取得物料的itemID
  948. string sql = @"IF NOT EXISTS(
  949. SELECT Id FROM dbo.SCM_Item WHERE
  950. cinvcode = '{0}' 1=1 )
  951. BEGIN
  952. INSERT INTO dbo.SCM_Item(cInvCode ,cFree1 ,cFree2 ,cFree3 ,cFree4 , cFree5 ,
  953. cFree6 ,cFree7 ,cFree8 ,cFree9 ,cFree10 ,PartId) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}',0)
  954. END
  955. SELECT Id FROM
  956. dbo.SCM_Item WHERE cinvcode = '{0}' 1=1 ";
  957. if (NewcBatch!="")
  958. sql = sql.Replace("1=1",NewcBatch);
  959. else
  960. sql = sql.Replace("1=1", "");
  961. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10);
  962. cmd.CommandText = sql;
  963. DataTable dtItem = SQlReturnData(sql, cmd);
  964. if (dtItem.Rows.Count == 0)
  965. {
  966. throw new Exception("物料的ItemID取得失败");
  967. }
  968. int ItemID = int.Parse(dtItem.Rows[0]["Id"].ToString());
  969. //log.Info("取得物料的itemID" + sql);
  970. #endregion
  971. #region 2 更新失败,插入现存量
  972. sql = @"SELECT AutoID FROM dbo.CurrentStock
  973. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  974. AND ItemId = @ItemId";
  975. if (cBatch != null)
  976. {
  977. if (bInvBatch(cInvCode, cmd) == true)
  978. sql += " and cBatch='" + cBatch + "' ";
  979. else
  980. sql += " and cBatch='' ";
  981. }
  982. else
  983. sql += " and cBatch='' ";
  984. sql += " and cFree1='{1}' ";
  985. sql += " and cFree2='{2}' ";
  986. sql += " and cFree3='{3}' ";
  987. sql += " and cFree4='{4}' ";
  988. sql += " and cFree5='{5}' ";
  989. sql += " and cFree6='{6}' ";
  990. sql += " and cFree7='{7}' ";
  991. sql += " and cFree8='{8}' ";
  992. sql += " and cFree9='{9}' ";
  993. sql += " and cFree10='{10}' ";
  994. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10);
  995. cmd.CommandText = sql;
  996. cmd.Parameters.Clear();
  997. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  998. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  999. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1000. DataTable dtCurrentStock = SQlReturnData(sql, cmd);
  1001. //log.Info("查找现存量:" + sql);
  1002. if (dtCurrentStock != null && dtCurrentStock.Rows.Count > 0)
  1003. {
  1004. sql = @"UPDATE dbo.CurrentStock SET iQuantity = iQuantity + @iQuantity
  1005. {0}
  1006. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  1007. AND ItemId = @ItemId";
  1008. if (key.cBustypeUN.Contains("入库") && key.UpdateTodoQuantity == true)
  1009. {
  1010. sql = string.Format(sql, ",fInQuantity=isnull(fInQuantity,0)-@iQuantity");
  1011. }
  1012. else if (key.cBustypeUN.Contains("出库") && key.UpdateTodoQuantity == true)
  1013. {
  1014. sql = string.Format(sql, ",fOutQuantity=isnull(fOutQuantity,0)-@iQuantity");
  1015. }
  1016. else if (key.cBustypeUN == "调拨" && key.UpdateTodoQuantity == true)
  1017. {
  1018. sql = string.Format(sql, ", fTransOutQuantity=ISNULL(a.fTransOutQuantity,0)-ISNULL(a.iQuantity,0),fTransInQuantity=ISNULL(fTransInQuantity,0)-ISNULL(a.iQuantity,0)");
  1019. }
  1020. else
  1021. {
  1022. sql = string.Format(sql, "");
  1023. }
  1024. if (cBatch != null)
  1025. {
  1026. if (bInvBatch(cInvCode, cmd) == true)
  1027. sql += " and cBatch='" + cBatch + "' ";
  1028. else
  1029. sql += " and cBatch='' ";
  1030. }
  1031. else
  1032. {
  1033. sql += " and cBatch='' ";
  1034. }
  1035. sql += " and cFree1='{1}' ";
  1036. sql += " and cFree2='{2}' ";
  1037. sql += " and cFree3='{3}' ";
  1038. sql += " and cFree4='{4}' ";
  1039. sql += " and cFree5='{5}' ";
  1040. sql += " and cFree6='{6}' ";
  1041. sql += " and cFree7='{7}' ";
  1042. sql += " and cFree8='{8}' ";
  1043. sql += " and cFree9='{9}' ";
  1044. sql += " and cFree10='{10}' ";
  1045. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10);
  1046. cmd.CommandText = sql;
  1047. cmd.Parameters.Clear();
  1048. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  1049. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1050. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1051. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1052. CmdExecuteNonQuery(sql, cmd, "更新现存量失败!");
  1053. //log.Info("现存量更新:" + sql);
  1054. }
  1055. else
  1056. {
  1057. sql = @" INSERT INTO dbo.CurrentStock
  1058. (cWhCode,cInvCode,ItemId,cBatch,iSoType,iSodid,iQuantity,
  1059. iNum,fOutQuantity,fOutNum,fInQuantity,fInNum,
  1060. bStopFlag,fTransInQuantity,fTransInNum,
  1061. fTransOutQuantity,fTransOutNum,fPlanQuantity,fPlanNum,fDisableQuantity,
  1062. fDisableNum,fAvaQuantity,fAvaNum,BGSPSTOP,fStopQuantity,
  1063. fStopNum,ipeqty,ipenum,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  1064. SELECT @cWhCode,@cInvCode,@ItemId,@cBatch,'0','',@iQuantity,
  1065. '0','0','0','0','0',
  1066. '0','0','0','0','0','0','0','0',
  1067. '0','0','0','0','0','0','0','0',@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10 ";
  1068. if (newsql!="")
  1069. sql = sql.Replace("1=1", newsql);
  1070. else
  1071. sql = sql.Replace("1=1", "");
  1072. if (newsqlvalues != "")
  1073. sql = sql.Replace("2=2", newsqlvalues);
  1074. else
  1075. sql = sql.Replace("2=2", "");
  1076. cmd.CommandText = sql;
  1077. cmd.Parameters.Clear();
  1078. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1079. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1080. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1081. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  1082. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  1083. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  1084. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  1085. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  1086. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  1087. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  1088. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  1089. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  1090. cmd.Parameters.Add(new SqlParameter("@cFree9", cFree9));
  1091. cmd.Parameters.Add(new SqlParameter("@cFree10", cFree10));
  1092. if (cBatch != null)
  1093. {
  1094. if (bInvBatch(cInvCode, cmd) == true)
  1095. cmd.Parameters.Add(new SqlParameter("@cBatch", cBatch));
  1096. else
  1097. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  1098. }
  1099. else
  1100. {
  1101. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  1102. }
  1103. CmdExecuteNonQuery(sql, cmd, "插入现存量失败!");
  1104. //log.Info("现存量插入:" + sql);
  1105. }
  1106. #endregion
  1107. #region 判断现存量是否足够
  1108. //sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0)
  1109. // BEGIN
  1110. // DECLARE @MSG NVARCHAR(100)
  1111. // SELECT @MSG='ERP库存不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0
  1112. // RAISERROR(@MSG,16,1)
  1113. // END";
  1114. //cmd.CommandText = sql;
  1115. //cmd.ExecuteNonQuery();
  1116. #endregion
  1117. //判断仓库是否记入成本
  1118. sql = "SELECT bInCost FROM Warehouse WHERE cWhCode='{0}'";
  1119. sql = string.Format(sql, cWhCode);
  1120. cmd.CommandText = sql;
  1121. DataTable dtwh = SQlReturnData(sql, cmd);
  1122. if (!dtwh.Rows[0]["bInCost"].ToString().Equals("0"))
  1123. {
  1124. #region 3 写入记账表
  1125. sql = string.Format(@" INSERT INTO [dbo].[" + key.TableName + @"]
  1126. SELECT '{0}','{1}','{2}','{3}'", key.ID, key.DID, key.cVouchTypeUN, key.cBustypeUN);
  1127. cmd.CommandText = sql;
  1128. CmdExecuteNonQuery(sql, cmd, "采购入库单写入记账表失败!");
  1129. #endregion
  1130. }
  1131. #region 判断现存量是否足够
  1132. sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE (iQuantity<0 OR iNum<0 OR fOutQuantity<0) and cWhCode = '{0}' AND cInvCode = '{1}' 1=1)
  1133. BEGIN
  1134. DECLARE @MSG NVARCHAR(100)
  1135. SELECT @MSG='ERP待出库数量不足AutoID'+CAST(AutoID AS NVARCHAR(100))+','+cInvCode FROM dbo.CurrentStock WHERE (iQuantity<0 OR iNum<0 OR fOutQuantity<0) and cWhCode = '{0}' AND cInvCode = '{1}' 1=1
  1136. RAISERROR(@MSG,16,1)
  1137. END";
  1138. sql = string.Format(sql, cWhCode, cInvCode);
  1139. string sql1 = "";
  1140. if (cBatch != null)
  1141. {
  1142. if (bInvBatch(cInvCode, cmd) == true)
  1143. sql1 += " and cBatch='" + cBatch + "' ";
  1144. else
  1145. sql1 += " and cBatch='' ";
  1146. }
  1147. else
  1148. {
  1149. sql1 += " and cBatch='' ";
  1150. }
  1151. sql1 += " and cFree1='{1}' ";
  1152. sql1 += " and cFree2='{2}' ";
  1153. sql1 += " and cFree3='{3}' ";
  1154. sql1 += " and cFree4='{4}' ";
  1155. sql1 += " and cFree5='{5}' ";
  1156. sql1 += " and cFree6='{6}' ";
  1157. sql1 += " and cFree7='{7}' ";
  1158. sql1 += " and cFree8='{8}' ";
  1159. sql1 += " and cFree9='{9}' ";
  1160. sql1 += " and cFree10='{10}' ";
  1161. sql1 = string.Format(sql1, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10);
  1162. if (sql1!="")
  1163. {
  1164. sql = sql.Replace("1=1", sql1);
  1165. }
  1166. else
  1167. {
  1168. sql = sql.Replace("1=1", "");
  1169. }
  1170. cmd.CommandText = sql;
  1171. cmd.ExecuteNonQuery();
  1172. #endregion
  1173. }
  1174. /// <summary>
  1175. /// 更新现存量
  1176. /// </summary>
  1177. /// <param name="cmd"></param>
  1178. /// <param name="cInvCode"></param>
  1179. /// <param name="cWhCode"></param>
  1180. /// <param name="cBatch"></param>
  1181. public static void UpdateCurrentStockNEWDG(SqlCommand cmd, string cInvCode, string cWhCode, string cBatch, decimal iQuantity, string cFree1, string cFree2, string cFree3, string cFree4, string cFree5, string cFree6, string cFree7, string cFree8, string cFree9, string cFree10,string cVenCode, VouchKey key)
  1182. {
  1183. string newsql = "";
  1184. string newsqlvalues = "";
  1185. string NewcBatch = "";
  1186. NewcBatch += " and cFree1='{1}' ";
  1187. newsql += ",cFree1";
  1188. newsqlvalues = ",'" + cFree1 + @"'";
  1189. NewcBatch += " and cFree2='{2}' ";
  1190. newsql += ",cFree2";
  1191. newsqlvalues = ",'" + cFree2 + @"'";
  1192. NewcBatch += " and cFree3='{3}' ";
  1193. newsql += ",cFree3";
  1194. newsqlvalues = ",'" + cFree3 + @"'";
  1195. NewcBatch += " and cFree4='{4}' ";
  1196. newsql += ",cFree4";
  1197. newsqlvalues = ",'" + cFree4 + @"'";
  1198. NewcBatch += " and cFree5='{5}' ";
  1199. newsql += ",cFree5";
  1200. newsqlvalues = ",'" + cFree5 + @"'";
  1201. NewcBatch += " and cFree6='{6}' ";
  1202. newsql += ",cFree6";
  1203. newsqlvalues = ",'" + cFree6 + @"'";
  1204. NewcBatch += " and cFree7='{7}' ";
  1205. newsql += ",cFree7";
  1206. newsqlvalues = ",'" + cFree7 + @"'";
  1207. NewcBatch += " and cFree8='{8}' ";
  1208. newsql += ",cFree8";
  1209. newsqlvalues = ",'" + cFree8 + @"'";
  1210. NewcBatch += " and cFree9='{9}' ";
  1211. newsql += ",cFree9";
  1212. newsqlvalues = ",'" + cFree9 + @"'";
  1213. NewcBatch += " and cFree10='{10}' ";
  1214. newsql += ",cFree10";
  1215. newsqlvalues = ",'" + cFree10 + @"'";
  1216. NewcBatch += " and cVMIVenCode='{11}' ";
  1217. newsql += ",cVMIVenCode";
  1218. newsqlvalues = ",'" + cVenCode + @"'";
  1219. #region 1 取得物料的itemID
  1220. string sql = @"IF NOT EXISTS(
  1221. SELECT Id FROM dbo.SCM_Item WHERE
  1222. cinvcode = '{0}' 1=1 )
  1223. BEGIN
  1224. INSERT INTO dbo.SCM_Item(cInvCode ,cFree1 ,cFree2 ,cFree3 ,cFree4 , cFree5 ,
  1225. cFree6 ,cFree7 ,cFree8 ,cFree9 ,cFree10 ,PartId,cVMIVenCode)
  1226. VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}',0,'{11}')
  1227. END
  1228. SELECT Id FROM
  1229. dbo.SCM_Item WHERE cinvcode = '{0}' 1=1 ";
  1230. if (NewcBatch != "")
  1231. sql = sql.Replace("1=1", NewcBatch);
  1232. else
  1233. sql = sql.Replace("1=1", "");
  1234. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,cVenCode);
  1235. cmd.CommandText = sql;
  1236. DataTable dtItem = SQlReturnData(sql, cmd);
  1237. if (dtItem.Rows.Count == 0)
  1238. {
  1239. throw new Exception("物料的ItemID取得失败");
  1240. }
  1241. int ItemID = int.Parse(dtItem.Rows[0]["Id"].ToString());
  1242. //log.Info("取得物料的itemID" + sql);
  1243. #endregion
  1244. #region 2 更新失败,插入现存量
  1245. sql = @"SELECT AutoID FROM dbo.CurrentStock
  1246. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  1247. AND ItemId = @ItemId";
  1248. if (cBatch != null)
  1249. {
  1250. if (bInvBatch(cInvCode, cmd) == true)
  1251. sql += " and cBatch='" + cBatch + "' ";
  1252. else
  1253. sql += " and cBatch='' ";
  1254. }
  1255. else
  1256. sql += " and cBatch='' ";
  1257. sql += " and cFree1='{1}' ";
  1258. sql += " and cFree2='{2}' ";
  1259. sql += " and cFree3='{3}' ";
  1260. sql += " and cFree4='{4}' ";
  1261. sql += " and cFree5='{5}' ";
  1262. sql += " and cFree6='{6}' ";
  1263. sql += " and cFree7='{7}' ";
  1264. sql += " and cFree8='{8}' ";
  1265. sql += " and cFree9='{9}' ";
  1266. sql += " and cFree10='{10}' ";
  1267. sql += " and cVMIVenCode='{11}' ";
  1268. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,cVenCode);
  1269. cmd.CommandText = sql;
  1270. cmd.Parameters.Clear();
  1271. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1272. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1273. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1274. DataTable dtCurrentStock = SQlReturnData(sql, cmd);
  1275. //log.Info("查找现存量:" + sql);
  1276. if (dtCurrentStock != null && dtCurrentStock.Rows.Count > 0)
  1277. {
  1278. sql = @"UPDATE dbo.CurrentStock SET iQuantity = iQuantity + @iQuantity
  1279. {0}
  1280. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  1281. AND ItemId = @ItemId";
  1282. if (key.cBustypeUN.Contains("入库") && key.UpdateTodoQuantity == true)
  1283. {
  1284. sql = string.Format(sql, ",fInQuantity=isnull(fInQuantity,0)-@iQuantity");
  1285. }
  1286. else if (key.cBustypeUN.Contains("出库") && key.UpdateTodoQuantity == true)
  1287. {
  1288. sql = string.Format(sql, ",fOutQuantity=isnull(fOutQuantity,0)-@iQuantity");
  1289. }
  1290. else if (key.cBustypeUN == "调拨" && key.UpdateTodoQuantity == true)
  1291. {
  1292. sql = string.Format(sql, ", fTransOutQuantity=ISNULL(a.fTransOutQuantity,0)-ISNULL(a.iQuantity,0),fTransInQuantity=ISNULL(fTransInQuantity,0)-ISNULL(a.iQuantity,0)");
  1293. }
  1294. else
  1295. {
  1296. sql = string.Format(sql, "");
  1297. }
  1298. if (cBatch != null)
  1299. {
  1300. if (bInvBatch(cInvCode, cmd) == true)
  1301. sql += " and cBatch='" + cBatch + "' ";
  1302. else
  1303. sql += " and cBatch='' ";
  1304. }
  1305. else
  1306. {
  1307. sql += " and cBatch='' ";
  1308. }
  1309. sql += " and cFree1='{1}' ";
  1310. sql += " and cFree2='{2}' ";
  1311. sql += " and cFree3='{3}' ";
  1312. sql += " and cFree4='{4}' ";
  1313. sql += " and cFree5='{5}' ";
  1314. sql += " and cFree6='{6}' ";
  1315. sql += " and cFree7='{7}' ";
  1316. sql += " and cFree8='{8}' ";
  1317. sql += " and cFree9='{9}' ";
  1318. sql += " and cFree10='{10}' ";
  1319. sql += " and cVMIVenCode='{11}' ";
  1320. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,cVenCode);
  1321. cmd.CommandText = sql;
  1322. cmd.Parameters.Clear();
  1323. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  1324. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1325. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1326. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1327. CmdExecuteNonQuery(sql, cmd, "更新现存量失败!");
  1328. //log.Info("现存量更新:" + sql);
  1329. }
  1330. else
  1331. {
  1332. sql = @" INSERT INTO dbo.CurrentStock
  1333. (cWhCode,cInvCode,ItemId,cBatch,iSoType,iSodid,iQuantity,
  1334. iNum,fOutQuantity,fOutNum,fInQuantity,fInNum,
  1335. bStopFlag,fTransInQuantity,fTransInNum,
  1336. fTransOutQuantity,fTransOutNum,fPlanQuantity,fPlanNum,fDisableQuantity,
  1337. fDisableNum,fAvaQuantity,fAvaNum,BGSPSTOP,fStopQuantity,
  1338. fStopNum,ipeqty,ipenum,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,cVMIVenCode)
  1339. SELECT @cWhCode,@cInvCode,@ItemId,@cBatch,'0','',@iQuantity,
  1340. '0','0','0','0','0',
  1341. '0','0','0','0','0','0','0','0',
  1342. '0','0','0','0','0','0','0','0',@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10,@cVMIVenCode ";
  1343. if (newsql != "")
  1344. sql = sql.Replace("1=1", newsql);
  1345. else
  1346. sql = sql.Replace("1=1", "");
  1347. if (newsqlvalues != "")
  1348. sql = sql.Replace("2=2", newsqlvalues);
  1349. else
  1350. sql = sql.Replace("2=2", "");
  1351. cmd.CommandText = sql;
  1352. cmd.Parameters.Clear();
  1353. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1354. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1355. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1356. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  1357. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  1358. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  1359. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  1360. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  1361. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  1362. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  1363. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  1364. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  1365. cmd.Parameters.Add(new SqlParameter("@cFree9", cFree9));
  1366. cmd.Parameters.Add(new SqlParameter("@cFree10", cFree10));
  1367. cmd.Parameters.Add(new SqlParameter("@cVMIVenCode", cVenCode));
  1368. if (cBatch != null)
  1369. {
  1370. if (bInvBatch(cInvCode, cmd) == true)
  1371. cmd.Parameters.Add(new SqlParameter("@cBatch", cBatch));
  1372. else
  1373. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  1374. }
  1375. else
  1376. {
  1377. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  1378. }
  1379. CmdExecuteNonQuery(sql, cmd, "插入现存量失败!");
  1380. //log.Info("现存量插入:" + sql);
  1381. }
  1382. #endregion
  1383. #region 判断现存量是否足够
  1384. //sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0)
  1385. // BEGIN
  1386. // DECLARE @MSG NVARCHAR(100)
  1387. // SELECT @MSG='ERP库存不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0
  1388. // RAISERROR(@MSG,16,1)
  1389. // END";
  1390. //cmd.CommandText = sql;
  1391. //cmd.ExecuteNonQuery();
  1392. #endregion
  1393. //判断仓库是否记入成本
  1394. sql = "SELECT bInCost FROM Warehouse WHERE cWhCode='{0}'";
  1395. sql = string.Format(sql, cWhCode);
  1396. cmd.CommandText = sql;
  1397. DataTable dtwh = SQlReturnData(sql, cmd);
  1398. if (!dtwh.Rows[0]["bInCost"].ToString().Equals("0"))
  1399. {
  1400. #region 3 写入记账表
  1401. sql = string.Format(@" INSERT INTO [dbo].[" + key.TableName + @"]
  1402. SELECT '{0}','{1}','{2}','{3}'", key.ID, key.DID, key.cVouchTypeUN, key.cBustypeUN);
  1403. cmd.CommandText = sql;
  1404. CmdExecuteNonQuery(sql, cmd, "采购入库单写入记账表失败!");
  1405. #endregion
  1406. }
  1407. #region 判断现存量是否足够
  1408. sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE (iQuantity<0 OR iNum<0 OR fOutQuantity<0) and cWhCode = '{0}' AND cInvCode = '{1}' 1=1)
  1409. BEGIN
  1410. DECLARE @MSG NVARCHAR(100)
  1411. SELECT @MSG='ERP待出库数量不足AutoID'+CAST(AutoID AS NVARCHAR(100))+','+cInvCode FROM dbo.CurrentStock WHERE (iQuantity<0 OR iNum<0 OR fOutQuantity<0) and cWhCode = '{0}' AND cInvCode = '{1}' 1=1
  1412. RAISERROR(@MSG,16,1)
  1413. END";
  1414. sql = string.Format(sql, cWhCode, cInvCode);
  1415. string sql1 = "";
  1416. if (cBatch != null)
  1417. {
  1418. if (bInvBatch(cInvCode, cmd) == true)
  1419. sql1 += " and cBatch='" + cBatch + "' ";
  1420. else
  1421. sql1 += " and cBatch='' ";
  1422. }
  1423. else
  1424. {
  1425. sql1 += " and cBatch='' ";
  1426. }
  1427. sql1 += " and cFree1='{1}' ";
  1428. sql1 += " and cFree2='{2}' ";
  1429. sql1 += " and cFree3='{3}' ";
  1430. sql1 += " and cFree4='{4}' ";
  1431. sql1 += " and cFree5='{5}' ";
  1432. sql1 += " and cFree6='{6}' ";
  1433. sql1 += " and cFree7='{7}' ";
  1434. sql1 += " and cFree8='{8}' ";
  1435. sql1 += " and cFree9='{9}' ";
  1436. sql1 += " and cFree10='{10}' ";
  1437. sql1 = string.Format(sql1, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10);
  1438. if (sql1 != "")
  1439. {
  1440. sql = sql.Replace("1=1", sql1);
  1441. }
  1442. else
  1443. {
  1444. sql = sql.Replace("1=1", "");
  1445. }
  1446. cmd.CommandText = sql;
  1447. cmd.ExecuteNonQuery();
  1448. #endregion
  1449. }
  1450. /// <summary>
  1451. /// 更新现存量
  1452. /// </summary>
  1453. /// <param name="cmd"></param>
  1454. /// <param name="cInvCode"></param>
  1455. /// <param name="cWhCode"></param>
  1456. /// <param name="cBatch"></param>
  1457. public static void UpdateCurrentStockCCGC(SqlCommand cmd, string cInvCode, string cWhCode, string cBatch, decimal iQuantity,decimal iNum,
  1458. string cFree1, string cFree2, string cFree3, string cFree4, string cFree5, string cFree6, string cFree7, string cFree8, string cFree9, string cFree10,
  1459. string cVenCode, VouchKey key, string dMdate, string dVDate, string iMassDate, string cMassUnit, decimal fOutQuantity, decimal fOutNum,
  1460. decimal fInQuantity, decimal fInNum, decimal fTransInQuantity, decimal fTransInNum, decimal fTransOutQuantity, decimal fTransOutNum, int id, int autoid)
  1461. {
  1462. string sql = "SELECT bInCost FROM Warehouse WHERE cWhCode='{0}'";
  1463. sql = string.Format(sql, cWhCode);
  1464. cmd.CommandText = sql;
  1465. DataTable dtwh = SQlReturnData(sql, cmd);
  1466. if (!dtwh.Rows[0]["bInCost"].ToString().Equals("0"))
  1467. {
  1468. sql = string.Format(@" EXEC dbo.ICS_CurrentStock @cInvCode ='{0}' ,--物料
  1469. @cWhCode='{1}' ,--
  1470. @iQuantity='{2}', --
  1471. @iNum ='{3}', --
  1472. @cVMIVenCode ='{4}',--
  1473. @cBatch ='{5}',--
  1474. @cFree1 ='{6}',--1
  1475. @cFree2 ='{7}',--2
  1476. @cFree3 ='{8}',--3
  1477. @cFree4 ='{9}',--4
  1478. @cFree5 ='{10}',--5
  1479. @cFree6 ='{11}',--6
  1480. @cFree7 ='{12}',--7
  1481. @cFree8 ='{13}',--8
  1482. @cFree9 ='{14}',--9
  1483. @cFree10 ='{15}',--10
  1484. -- bInCost=1
  1485. @tablename = '{16}',--
  1486. @cBustypeUN = '{17}',--
  1487. @cVouTypeUN = '{18}',--
  1488. @IDUN = '{19}',--ID
  1489. @IDSUN = '{20}'
  1490. ",
  1491. cInvCode,cWhCode,iQuantity,iNum,cVenCode,cBatch,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,
  1492. key.TableName, key.cBustypeUN, key.cVouchTypeUN, id, autoid);
  1493. if (dMdate!="")
  1494. {
  1495. sql += @",@dMdate ='{0}'";
  1496. sql = string.Format(sql,dMdate);
  1497. }
  1498. if (dVDate != "")
  1499. {
  1500. sql += @",@dVDate ='{0}'";
  1501. sql = string.Format(sql, dVDate);
  1502. }
  1503. if (iMassDate != "")
  1504. {
  1505. sql += @",@iMassDate ='{0}'";
  1506. sql = string.Format(sql, iMassDate);
  1507. }
  1508. if (cMassUnit != "")
  1509. {
  1510. sql += @",@cMassUnit ='{0}'";
  1511. sql = string.Format(sql, cMassUnit);
  1512. }
  1513. if (fOutQuantity != 0)
  1514. {
  1515. sql += @",@fOutQuantity ='{0}'";
  1516. sql = string.Format(sql, fOutQuantity);
  1517. }
  1518. if (fOutNum != 0)
  1519. {
  1520. sql += @",@fOutNum ='{0}'";
  1521. sql = string.Format(sql, fOutNum);
  1522. }
  1523. if (fInQuantity != 0)
  1524. {
  1525. sql += @",@fInQuantity ='{0}'";
  1526. sql = string.Format(sql, fInQuantity);
  1527. }
  1528. if (fInNum != 0)
  1529. {
  1530. sql += @",@fInNum ='{0}'";
  1531. sql = string.Format(sql, fInNum);
  1532. }
  1533. if (fTransInQuantity != 0)
  1534. {
  1535. sql += @",@fTransInQuantity ='{0}'";
  1536. sql = string.Format(sql, fTransInQuantity);
  1537. }
  1538. if (fTransInNum != 0)
  1539. {
  1540. sql += @",@fTransInNum ='{0}'";
  1541. sql = string.Format(sql, fTransInNum);
  1542. }
  1543. if (fTransOutQuantity != 0)
  1544. {
  1545. sql += @",@fTransOutQuantity ='{0}'";
  1546. sql = string.Format(sql, fTransOutQuantity);
  1547. }
  1548. if (fTransOutNum != 0)
  1549. {
  1550. sql += @",@fTransOutNum ='{0}'";
  1551. sql = string.Format(sql, fTransOutNum);
  1552. }
  1553. cmd.CommandText = sql;
  1554. CmdExecuteNonQuery(sql, cmd, "现存量更新失败!");
  1555. }
  1556. else
  1557. {
  1558. sql = string.Format(@" EXEC dbo.ICS_CurrentStock @cInvCode ='{0}' ,--物料
  1559. @cWhCode='{1}' ,--
  1560. @iQuantity='{2}', --
  1561. @iNum ='{3}', --
  1562. @cVMIVenCode ='{4}',--
  1563. @cBatch ='{5}',--
  1564. @cFree1 ='{6}',--1
  1565. @cFree2 ='{7}',--2
  1566. @cFree3 ='{8}',--3
  1567. @cFree4 ='{9}',--4
  1568. @cFree5 ='{10}',--5
  1569. @cFree6 ='{11}',--6
  1570. @cFree7 ='{12}',--7
  1571. @cFree8 ='{13}',--8
  1572. @cFree9 ='{14}',--9
  1573. @cFree10 ='{15}'",
  1574. cInvCode, cWhCode, iQuantity, iNum, cVenCode, cBatch, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10);
  1575. if (dMdate != "")
  1576. {
  1577. sql += @",@dMdate ='{0}'";
  1578. sql = string.Format(sql, dMdate);
  1579. }
  1580. if (dVDate != "")
  1581. {
  1582. sql += @",@dVDate ='{0}'";
  1583. sql = string.Format(sql, dVDate);
  1584. }
  1585. if (iMassDate != "")
  1586. {
  1587. sql += @",@iMassDate ='{0}'";
  1588. sql = string.Format(sql, iMassDate);
  1589. }
  1590. if (cMassUnit != "")
  1591. {
  1592. sql += @",@cMassUnit ='{0}'";
  1593. sql = string.Format(sql, cMassUnit);
  1594. }
  1595. if (fOutQuantity != 0)
  1596. {
  1597. sql += @",@fOutQuantity ='{0}'";
  1598. sql = string.Format(sql, fOutQuantity);
  1599. }
  1600. if (fOutNum != 0)
  1601. {
  1602. sql += @",@fOutNum ='{0}'";
  1603. sql = string.Format(sql, fOutNum);
  1604. }
  1605. if (fInQuantity != 0)
  1606. {
  1607. sql += @",@fInQuantity ='{0}'";
  1608. sql = string.Format(sql, fInQuantity);
  1609. }
  1610. if (fInNum != 0)
  1611. {
  1612. sql += @",@fInNum ='{0}'";
  1613. sql = string.Format(sql, fInNum);
  1614. }
  1615. if (fTransInQuantity != 0)
  1616. {
  1617. sql += @",@fTransInQuantity ='{0}'";
  1618. sql = string.Format(sql, fTransInQuantity);
  1619. }
  1620. if (fTransInNum != 0)
  1621. {
  1622. sql += @",@fTransInNum ='{0}'";
  1623. sql = string.Format(sql, fTransInNum);
  1624. }
  1625. if (fTransOutQuantity != 0)
  1626. {
  1627. sql += @",@fTransOutQuantity ='{0}'";
  1628. sql = string.Format(sql, fTransOutQuantity);
  1629. }
  1630. if (fTransOutNum != 0)
  1631. {
  1632. sql += @",@fTransOutNum ='{0}'";
  1633. sql = string.Format(sql, fTransOutNum);
  1634. }
  1635. cmd.CommandText = sql;
  1636. CmdExecuteNonQuery(sql, cmd, "现存量更新失败!");
  1637. }
  1638. }
  1639. public static bool IsInventoryConsolidation(SqlCommand cmd,string WorkPoint)
  1640. {
  1641. string sql = @"SELECT cAcc_Id, iYear, cUser_Id, cAuth_Id, cStation, cTaskId, iLogId, cSub_id, cAppServer, cAuthClassCode, iBeginYear
  1642. FROM UFSystem.dbo.ua_Task_Common
  1643. where cAuth_Id in (
  1644. SELECT distinct cAuth_ID
  1645. FROM {0}.dbo.UA_Auth_lang
  1646. WHERE (cAuth_Name = N''))";
  1647. sql = string.Format(sql, WorkPoint);
  1648. DataTable dt = SQlReturnData(sql, cmd);
  1649. if (dt != null && dt.Rows.Count > 0)
  1650. {
  1651. return false;
  1652. }
  1653. else
  1654. {
  1655. return true;
  1656. }
  1657. }
  1658. /// <summary>
  1659. /// 判断是否启用批次管理
  1660. /// </summary>
  1661. /// <param name="cInvCode"></param>
  1662. /// <returns></returns>
  1663. public static bool bInvBatch(string cInvCode, SqlCommand cmd)
  1664. {
  1665. try
  1666. {
  1667. bool flag = false;
  1668. string sql = "SELECT bInvBatch FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1669. DataTable dt = SQlReturnData(sql, cmd);
  1670. if (dt != null && dt.Rows.Count > 0)
  1671. {
  1672. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1673. {
  1674. flag = true;
  1675. }
  1676. else
  1677. {
  1678. flag = false;
  1679. }
  1680. }
  1681. return flag;
  1682. }
  1683. catch (Exception ex)
  1684. {
  1685. throw new Exception(ex.Message);
  1686. }
  1687. }
  1688. /// <summary>
  1689. /// 判断是否启自由项管理
  1690. /// </summary>
  1691. /// <param name="cInvCode"></param>
  1692. /// <returns></returns>
  1693. public static bool bFree1(string cInvCode, SqlCommand cmd)
  1694. {
  1695. try
  1696. {
  1697. bool flag = false;
  1698. string sql = "SELECT bFree1 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1699. DataTable dt = SQlReturnData(sql, cmd);
  1700. if (dt != null && dt.Rows.Count > 0)
  1701. {
  1702. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1703. {
  1704. flag = true;
  1705. }
  1706. else
  1707. {
  1708. flag = false;
  1709. }
  1710. }
  1711. return flag;
  1712. }
  1713. catch (Exception ex)
  1714. {
  1715. throw new Exception(ex.Message);
  1716. }
  1717. }
  1718. public static bool bFree2(string cInvCode, SqlCommand cmd)
  1719. {
  1720. try
  1721. {
  1722. bool flag = false;
  1723. string sql = "SELECT bFree2 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1724. DataTable dt = SQlReturnData(sql, cmd);
  1725. if (dt != null && dt.Rows.Count > 0)
  1726. {
  1727. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1728. {
  1729. flag = true;
  1730. }
  1731. else
  1732. {
  1733. flag = false;
  1734. }
  1735. }
  1736. return flag;
  1737. }
  1738. catch (Exception ex)
  1739. {
  1740. throw new Exception(ex.Message);
  1741. }
  1742. }
  1743. public static bool bFree3(string cInvCode, SqlCommand cmd)
  1744. {
  1745. try
  1746. {
  1747. bool flag = false;
  1748. string sql = "SELECT bFree3 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1749. DataTable dt = SQlReturnData(sql, cmd);
  1750. if (dt != null && dt.Rows.Count > 0)
  1751. {
  1752. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1753. {
  1754. flag = true;
  1755. }
  1756. else
  1757. {
  1758. flag = false;
  1759. }
  1760. }
  1761. return flag;
  1762. }
  1763. catch (Exception ex)
  1764. {
  1765. throw new Exception(ex.Message);
  1766. }
  1767. }
  1768. public static bool bFree4(string cInvCode, SqlCommand cmd)
  1769. {
  1770. try
  1771. {
  1772. bool flag = false;
  1773. string sql = "SELECT bFree4 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1774. DataTable dt = SQlReturnData(sql, cmd);
  1775. if (dt != null && dt.Rows.Count > 0)
  1776. {
  1777. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1778. {
  1779. flag = true;
  1780. }
  1781. else
  1782. {
  1783. flag = false;
  1784. }
  1785. }
  1786. return flag;
  1787. }
  1788. catch (Exception ex)
  1789. {
  1790. throw new Exception(ex.Message);
  1791. }
  1792. }
  1793. public static bool bFree5(string cInvCode, SqlCommand cmd)
  1794. {
  1795. try
  1796. {
  1797. bool flag = false;
  1798. string sql = "SELECT bFree5 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1799. DataTable dt = SQlReturnData(sql, cmd);
  1800. if (dt != null && dt.Rows.Count > 0)
  1801. {
  1802. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1803. {
  1804. flag = true;
  1805. }
  1806. else
  1807. {
  1808. flag = false;
  1809. }
  1810. }
  1811. return flag;
  1812. }
  1813. catch (Exception ex)
  1814. {
  1815. throw new Exception(ex.Message);
  1816. }
  1817. }
  1818. public static bool bFree6(string cInvCode, SqlCommand cmd)
  1819. {
  1820. try
  1821. {
  1822. bool flag = false;
  1823. string sql = "SELECT bFree6 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1824. DataTable dt = SQlReturnData(sql, cmd);
  1825. if (dt != null && dt.Rows.Count > 0)
  1826. {
  1827. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1828. {
  1829. flag = true;
  1830. }
  1831. else
  1832. {
  1833. flag = false;
  1834. }
  1835. }
  1836. return flag;
  1837. }
  1838. catch (Exception ex)
  1839. {
  1840. throw new Exception(ex.Message);
  1841. }
  1842. }
  1843. public static bool bFree7(string cInvCode, SqlCommand cmd)
  1844. {
  1845. try
  1846. {
  1847. bool flag = false;
  1848. string sql = "SELECT bFree7 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1849. DataTable dt = SQlReturnData(sql, cmd);
  1850. if (dt != null && dt.Rows.Count > 0)
  1851. {
  1852. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1853. {
  1854. flag = true;
  1855. }
  1856. else
  1857. {
  1858. flag = false;
  1859. }
  1860. }
  1861. return flag;
  1862. }
  1863. catch (Exception ex)
  1864. {
  1865. throw new Exception(ex.Message);
  1866. }
  1867. }
  1868. public static bool bFree8(string cInvCode, SqlCommand cmd)
  1869. {
  1870. try
  1871. {
  1872. bool flag = false;
  1873. string sql = "SELECT bFree8 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1874. DataTable dt = SQlReturnData(sql, cmd);
  1875. if (dt != null && dt.Rows.Count > 0)
  1876. {
  1877. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1878. {
  1879. flag = true;
  1880. }
  1881. else
  1882. {
  1883. flag = false;
  1884. }
  1885. }
  1886. return flag;
  1887. }
  1888. catch (Exception ex)
  1889. {
  1890. throw new Exception(ex.Message);
  1891. }
  1892. }
  1893. public static DataTable bFree(string cInvCode, SqlCommand cmd)
  1894. {
  1895. try
  1896. {
  1897. string sql = "SELECT bFree1, bFree2, bFree3, bFree4, bFree5, bFree6, bFree7, bFree8, bFree9, bFree10 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1898. DataTable dt = SQlReturnData(sql, cmd);
  1899. return dt;
  1900. }
  1901. catch (Exception ex)
  1902. {
  1903. throw new Exception(ex.Message);
  1904. }
  1905. }
  1906. /// <summary>
  1907. /// 判断是否启用保质期管理
  1908. /// </summary>
  1909. /// <param name="cInvCode"></param>
  1910. /// <returns></returns>
  1911. public static bool bInvQuality(string cInvCode, SqlCommand cmd)
  1912. {
  1913. try
  1914. {
  1915. bool flag = false;
  1916. string sql = "SELECT bInvQuality FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1917. DataTable dt = SQlReturnData(sql, cmd);
  1918. if (dt != null && dt.Rows.Count > 0)
  1919. {
  1920. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1921. {
  1922. flag = true;
  1923. }
  1924. else
  1925. {
  1926. flag = false;
  1927. }
  1928. }
  1929. return flag;
  1930. }
  1931. catch (Exception ex)
  1932. {
  1933. throw new Exception(ex.Message);
  1934. }
  1935. }
  1936. /// <summary>
  1937. /// 判断是否记账
  1938. /// </summary>
  1939. /// <param name="cInvCode"></param>
  1940. /// <returns></returns>
  1941. public static bool bInCost(string cWhCode, SqlCommand cmd)
  1942. {
  1943. try
  1944. {
  1945. bool flag = false;
  1946. string sql = "SELECT bInCost FROM Warehouse WHERE cWhCode ='" + cWhCode + "'";
  1947. DataTable dt = SQlReturnData(sql, cmd);
  1948. if (dt != null && dt.Rows.Count > 0)
  1949. {
  1950. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1951. {
  1952. flag = true;
  1953. }
  1954. else
  1955. {
  1956. flag = false;
  1957. }
  1958. }
  1959. return flag;
  1960. }
  1961. catch (Exception ex)
  1962. {
  1963. throw new Exception(ex.Message);
  1964. }
  1965. }
  1966. /// <summary>
  1967. /// Convert a List{T} to a DataTable.
  1968. /// </summary>
  1969. public static DataTable ToDataTable<T>(List<T> items)
  1970. {
  1971. var tb = new DataTable(typeof(T).Name);
  1972. PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  1973. foreach (PropertyInfo prop in props)
  1974. {
  1975. Type t = GetCoreType(prop.PropertyType);
  1976. tb.Columns.Add(prop.Name, t);
  1977. }
  1978. foreach (T item in items)
  1979. {
  1980. var values = new object[props.Length];
  1981. for (int i = 0; i < props.Length; i++)
  1982. {
  1983. values[i] = props[i].GetValue(item, null);
  1984. }
  1985. tb.Rows.Add(values);
  1986. }
  1987. return tb;
  1988. }
  1989. /// <summary>
  1990. /// Determine of specified type is nullable
  1991. /// </summary>
  1992. public static bool IsNullable(Type t)
  1993. {
  1994. return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
  1995. }
  1996. /// <summary>
  1997. /// Return underlying type if type is Nullable otherwise return the type
  1998. /// </summary>
  1999. public static Type GetCoreType(Type t)
  2000. {
  2001. if (t != null && IsNullable(t))
  2002. {
  2003. if (!t.IsValueType)
  2004. {
  2005. return t;
  2006. }
  2007. else
  2008. {
  2009. return Nullable.GetUnderlyingType(t);
  2010. }
  2011. }
  2012. else
  2013. {
  2014. return t;
  2015. }
  2016. }
  2017. #region 原
  2018. public static DataTable MergeDataTableX(DataTable dt, string AuotID, string cBatch, string iQuantity, string cInvCode, string dMDate, string cFree1, string cFree2, string cFree3, string cFree4, string cFree5, string cFree6, string cFree7, string cFree8, SqlCommand cmd)
  2019. {
  2020. DataTable dtNew = dt.Clone();
  2021. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[cInvCode], dtNew.Columns[dMDate], dtNew.Columns[cBatch], dtNew.Columns[cFree1], dtNew.Columns[cFree2], dtNew.Columns[cFree3], dtNew.Columns[cFree4], dtNew.Columns[cFree5], dtNew.Columns[cFree6], dtNew.Columns[cFree7], dtNew.Columns[cFree8] };
  2022. foreach (DataRow row in dt.Rows)
  2023. {
  2024. if (string.IsNullOrEmpty(cBatch))
  2025. {
  2026. cBatch = "";
  2027. }
  2028. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  2029. {
  2030. row[cBatch] = "";
  2031. }
  2032. DataRow srow = dtNew.Rows.Find(new object[] { row[cInvCode].ToString(), row[dMDate].ToString(), row[cBatch].ToString(), row[cFree1].ToString(), row[cFree2].ToString(), row[cFree3].ToString(), row[cFree4].ToString(), row[cFree5].ToString(), row[cFree6].ToString(), row[cFree7].ToString(), row[cFree8].ToString() });
  2033. if (srow == null)
  2034. {
  2035. dtNew.Rows.Add(row.ItemArray);
  2036. }
  2037. else
  2038. {
  2039. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  2040. }
  2041. }
  2042. return dtNew;
  2043. }
  2044. public static DataTable MergeDataTable(DataTable dt, string AuotID, string cBatch, string iQuantity, string cInvCode, SqlCommand cmd)
  2045. {
  2046. DataTable dtNew = dt.Clone();
  2047. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID], dtNew.Columns[cBatch] };
  2048. foreach (DataRow row in dt.Rows)
  2049. {
  2050. if (string.IsNullOrEmpty(cBatch))
  2051. {
  2052. cBatch = "";
  2053. }
  2054. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  2055. {
  2056. row[cBatch] = "";
  2057. }
  2058. DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString(), row[cBatch].ToString() });
  2059. if (srow == null)
  2060. {
  2061. dtNew.Rows.Add(row.ItemArray);
  2062. }
  2063. else
  2064. {
  2065. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  2066. }
  2067. }
  2068. return dtNew;
  2069. }
  2070. public static DataTable MergeDataTableFree(DataTable dt, string AuotID, string cBatch, string iQuantity, string cInvCode, string cFree1, string cFree2, string cFree3, string cFree4, string cFree5, string cFree6, string cFree7, string cFree8, SqlCommand cmd)
  2071. {
  2072. DataTable dtNew = dt.Clone();
  2073. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID], dtNew.Columns[cBatch], dtNew.Columns[cFree1], dtNew.Columns[cFree2], dtNew.Columns[cFree3], dtNew.Columns[cFree4], dtNew.Columns[cFree5], dtNew.Columns[cFree6], dtNew.Columns[cFree7], dtNew.Columns[cFree8] };
  2074. foreach (DataRow row in dt.Rows)
  2075. {
  2076. if (string.IsNullOrEmpty(cBatch))
  2077. {
  2078. cBatch = "";
  2079. }
  2080. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  2081. {
  2082. row[cBatch] = "";
  2083. }
  2084. DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString(), row[cBatch].ToString(), row[cFree1].ToString(), row[cFree2].ToString(), row[cFree3].ToString(), row[cFree4].ToString(), row[cFree5].ToString(), row[cFree6].ToString(), row[cFree7].ToString(), row[cFree8].ToString() });
  2085. if (srow == null)
  2086. {
  2087. dtNew.Rows.Add(row.ItemArray);
  2088. }
  2089. else
  2090. {
  2091. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  2092. }
  2093. }
  2094. return dtNew;
  2095. }
  2096. public static DataTable MergeDataTableQC(DataTable dt, string AuotID, string cBatch, string iQuantity, string iNGQuantity, string cInvCode, SqlCommand cmd)
  2097. {
  2098. DataTable dtNew = dt.Clone();
  2099. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID], dtNew.Columns[cBatch] };
  2100. foreach (DataRow row in dt.Rows)
  2101. {
  2102. if (string.IsNullOrEmpty(cBatch))
  2103. {
  2104. cBatch = "";
  2105. }
  2106. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  2107. {
  2108. row[cBatch] = "";
  2109. }
  2110. DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString(), row[cBatch].ToString() });
  2111. if (srow == null)
  2112. {
  2113. dtNew.Rows.Add(row.ItemArray);
  2114. }
  2115. else
  2116. {
  2117. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  2118. srow[iNGQuantity] = decimal.Parse(srow[iNGQuantity].ToString()) + decimal.Parse(row[iNGQuantity].ToString());
  2119. }
  2120. }
  2121. return dtNew;
  2122. }
  2123. public static DataTable MergeRd08(DataTable dt, string cBatch, string iQuantity, string cInvCode, string dMDate, string cFree1, string cFree2, string cFree3, string cFree4, string cFree5, string cFree6, string cFree7, string cFree8, SqlCommand cmd)
  2124. {
  2125. DataTable dtNew = dt.Clone();
  2126. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[cInvCode], dtNew.Columns[dMDate], dtNew.Columns[cBatch], dtNew.Columns[cFree1], dtNew.Columns[cFree2], dtNew.Columns[cFree3], dtNew.Columns[cFree4], dtNew.Columns[cFree5], dtNew.Columns[cFree6], dtNew.Columns[cFree7], dtNew.Columns[cFree8] };
  2127. foreach (DataRow row in dt.Rows)
  2128. {
  2129. if (string.IsNullOrEmpty(cBatch))
  2130. {
  2131. cBatch = "";
  2132. }
  2133. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  2134. {
  2135. row[cBatch] = "";
  2136. }
  2137. DataRow srow = dtNew.Rows.Find(new object[] { row[cInvCode].ToString(), row[dMDate].ToString(), row[cBatch].ToString(), row[cFree1].ToString(), row[cFree2].ToString(), row[cFree3].ToString(), row[cFree4].ToString(), row[cFree5].ToString(), row[cFree6].ToString(), row[cFree7].ToString(), row[cFree8].ToString() });
  2138. if (srow == null)
  2139. {
  2140. dtNew.Rows.Add(row.ItemArray);
  2141. }
  2142. else
  2143. {
  2144. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  2145. }
  2146. }
  2147. return dtNew;
  2148. }
  2149. public static DataTable MergeRd09(DataTable dt, string cBatch, string iQuantity, string cInvCode, string dMDate, string cFree1, string cFree2, string cFree3, string cFree4, string cFree5, string cFree6, string cFree7, string cFree8, SqlCommand cmd)
  2150. {
  2151. DataTable dtNew = dt.Clone();
  2152. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[cInvCode], dtNew.Columns[dMDate], dtNew.Columns[cBatch], dtNew.Columns[cFree1], dtNew.Columns[cFree2], dtNew.Columns[cFree3], dtNew.Columns[cFree4], dtNew.Columns[cFree5], dtNew.Columns[cFree6], dtNew.Columns[cFree7], dtNew.Columns[cFree8] };
  2153. foreach (DataRow row in dt.Rows)
  2154. {
  2155. if (string.IsNullOrEmpty(cBatch))
  2156. {
  2157. cBatch = "";
  2158. }
  2159. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  2160. {
  2161. row[cBatch] = "";
  2162. }
  2163. DataRow srow = dtNew.Rows.Find(new object[] { row[cInvCode].ToString(), row[dMDate].ToString(), row[cBatch].ToString(), row[cFree1].ToString(), row[cFree2].ToString(), row[cFree3].ToString(), row[cFree4].ToString(), row[cFree5].ToString(), row[cFree6].ToString(), row[cFree7].ToString(), row[cFree8].ToString() });
  2164. if (srow == null)
  2165. {
  2166. dtNew.Rows.Add(row.ItemArray);
  2167. }
  2168. else
  2169. {
  2170. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  2171. }
  2172. }
  2173. return dtNew;
  2174. }
  2175. #endregion
  2176. #region 现
  2177. public static DataTable MergeDataTableX(DataTable dt, string AuotID, string cBatch, string iQuantity, string cInvCode, string iNum, SqlCommand cmd)
  2178. {
  2179. DataTable dtNew = dt.Clone();
  2180. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[cInvCode], dtNew.Columns[cBatch] };
  2181. foreach (DataRow row in dt.Rows)
  2182. {
  2183. if (string.IsNullOrEmpty(cBatch))
  2184. {
  2185. cBatch = "";
  2186. }
  2187. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  2188. {
  2189. row[cBatch] = "";
  2190. }
  2191. DataRow srow = dtNew.Rows.Find(new object[] { row[cInvCode].ToString(), row[cBatch].ToString() });
  2192. if (srow == null)
  2193. {
  2194. dtNew.Rows.Add(row.ItemArray);
  2195. }
  2196. else
  2197. {
  2198. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  2199. srow[iNum] = decimal.Parse(srow[iNum].ToString()) + decimal.Parse(row[iNum].ToString());
  2200. }
  2201. }
  2202. return dtNew;
  2203. }
  2204. public static DataTable MergeDataTable(DataTable dt, string AuotID, string cBatch, string iQuantity, string cInvCode, string iNum, SqlCommand cmd)
  2205. {
  2206. DataTable dtNew = dt.Clone();
  2207. //dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID], dtNew.Columns[cInvCode], dtNew.Columns[cBatch] };
  2208. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID], dtNew.Columns[cBatch] };//原
  2209. foreach (DataRow row in dt.Rows)
  2210. {
  2211. if (string.IsNullOrEmpty(cBatch))
  2212. {
  2213. cBatch = "";
  2214. }
  2215. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  2216. {
  2217. row[cBatch] = "";
  2218. }
  2219. DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString(), row[cBatch].ToString() });
  2220. //DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString(), dtNew.Columns[cInvCode], row[cBatch].ToString() });
  2221. if (srow == null)
  2222. {
  2223. dtNew.Rows.Add(row.ItemArray);
  2224. }
  2225. else
  2226. {
  2227. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  2228. srow[iNum] = decimal.Parse(srow[iNum].ToString()) + decimal.Parse(row[iNum].ToString());
  2229. }
  2230. }
  2231. return dtNew;
  2232. }
  2233. public static DataTable MergeRd09(DataTable dt, string cBatch, string iQuantity, string cInvCode, string iNum, SqlCommand cmd)
  2234. {
  2235. DataTable dtNew = dt.Clone();
  2236. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[cInvCode], dtNew.Columns[cBatch] };
  2237. foreach (DataRow row in dt.Rows)
  2238. {
  2239. if (string.IsNullOrEmpty(cBatch))
  2240. {
  2241. cBatch = "";
  2242. }
  2243. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  2244. {
  2245. row[cBatch] = "";
  2246. }
  2247. DataRow srow = dtNew.Rows.Find(new object[] { row[cInvCode].ToString(), row[cBatch].ToString() });
  2248. if (srow == null)
  2249. {
  2250. dtNew.Rows.Add(row.ItemArray);
  2251. }
  2252. else
  2253. {
  2254. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  2255. srow[iNum] = decimal.Parse(srow[iNum].ToString()) + decimal.Parse(row[iNum].ToString());
  2256. }
  2257. }
  2258. return dtNew;
  2259. }
  2260. #endregion
  2261. public static IList<T> ConvertTo<T>(DataTable table)
  2262. {
  2263. if (table == null)
  2264. {
  2265. return null;
  2266. }
  2267. List<DataRow> rows = new List<DataRow>();
  2268. foreach (DataRow row in table.Rows)
  2269. {
  2270. rows.Add(row);
  2271. }
  2272. return ConvertTo<T>(rows);
  2273. }
  2274. public static IList<T> ConvertTo<T>(IList<DataRow> rows)
  2275. {
  2276. IList<T> list = null;
  2277. if (rows != null)
  2278. {
  2279. list = new List<T>();
  2280. foreach (DataRow row in rows)
  2281. {
  2282. T item = CreateItem<T>(row);
  2283. list.Add(item);
  2284. }
  2285. }
  2286. return list;
  2287. }
  2288. public static T CreateItem<T>(DataRow row)
  2289. {
  2290. T obj = default(T);
  2291. if (row != null)
  2292. {
  2293. obj = Activator.CreateInstance<T>();
  2294. foreach (DataColumn column in row.Table.Columns)
  2295. {
  2296. PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
  2297. try
  2298. {
  2299. object value = row[column.ColumnName];
  2300. prop.SetValue(obj, value, null);
  2301. }
  2302. catch
  2303. { //You can log something here
  2304. //throw;
  2305. }
  2306. }
  2307. }
  2308. return obj;
  2309. }
  2310. public static DataTable MergeDataTable(DataTable dt, string AuotID, string iQuantity, string cBatch)
  2311. {
  2312. DataTable dtNew = dt.Clone();
  2313. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID] };
  2314. foreach (DataRow row in dt.Rows)
  2315. {
  2316. DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString() });
  2317. if (srow == null)
  2318. {
  2319. dtNew.Rows.Add(row.ItemArray);
  2320. }
  2321. else
  2322. {
  2323. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  2324. }
  2325. }
  2326. return dtNew;
  2327. }
  2328. #region 返回默认的出入库类别
  2329. /// <summary>
  2330. /// 返回默认的出入库类别
  2331. /// </summary>
  2332. /// <returns></returns>
  2333. public static string returnDefaultRdType(string VTID, string BTChName, SqlCommand cmd)
  2334. {
  2335. string sql = @"select VouchRdContrapose.cVRGUID,cVTChName,cBTChName,cVRRCode,R.cRdName,cVRSCode,S.cRdName
  2336. from VouchRdContrapose with(nolock)
  2337. left join vouchTypeDic with(nolock) on VouchRdContrapose.cVBTID=VouchTypeDic.cVBTID
  2338. left join Rd_Style as R with(nolock) On cVRRCode=R.cRdCode and R.bRDFlag=1
  2339. left join Rd_Style as S with(nolock) ON cVRSCode=S.cRdCode and S.bRDFlag=0
  2340. where 1=1 And (cVTID = N'{0}') And (cBTChName = N'{1}') order by cSerial ";
  2341. sql = string.Format(sql, VTID, BTChName);
  2342. DataTable dt = SQlReturnData(sql, cmd);
  2343. if (dt.Rows.Count == 0)
  2344. {
  2345. throw new Exception("倒冲材料出库单的出库类别取得失败");
  2346. }
  2347. return dt.Rows[0]["cVRSCode"].ToString();
  2348. }
  2349. #endregion
  2350. public static string sqltext(string cInvCode, string cBatch, string cFree1, string cFree2, string cFree3, string cFree4, string cFree5, string cFree6, string cFree7, string cFree8, SqlCommand cmd)
  2351. {
  2352. string sql = "";
  2353. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  2354. sql += " and cBatch='" + cBatch + "' ";
  2355. else
  2356. sql += " and cBatch='' ";
  2357. if (DBHelper.bFree1(cInvCode, cmd) == true)
  2358. sql += " and cFree1='" + cFree1 + "' ";
  2359. else
  2360. sql += " and cFree1='' ";
  2361. if (DBHelper.bFree2(cInvCode, cmd) == true)
  2362. sql += " and cFree2='" + cFree2 + "' ";
  2363. else
  2364. sql += " and cFree2='' ";
  2365. if (DBHelper.bFree3(cInvCode, cmd) == true)
  2366. sql += " and cFree3='" + cFree3 + "' ";
  2367. else
  2368. sql += " and cFree3='' ";
  2369. if (DBHelper.bFree4(cInvCode, cmd) == true)
  2370. sql += " and cFree4='" + cFree4 + "' ";
  2371. else
  2372. sql += " and cFree4='' ";
  2373. if (DBHelper.bFree5(cInvCode, cmd) == true)
  2374. sql += " and cFree5='" + cFree5 + "' ";
  2375. else
  2376. sql += " and cFree5='' ";
  2377. if (DBHelper.bFree6(cInvCode, cmd) == true)
  2378. sql += " and cFree6='" + cFree6 + "' ";
  2379. else
  2380. sql += " and cFree6='' ";
  2381. if (DBHelper.bFree7(cInvCode, cmd) == true)
  2382. sql += " and cFree7='" + cFree7 + "' ";
  2383. else
  2384. sql += " and cFree7='' ";
  2385. if (DBHelper.bFree8(cInvCode, cmd) == true)
  2386. sql += " and cFree8='" + cFree8 + "' ";
  2387. else
  2388. sql += " and cFree8='' ";
  2389. return sql;
  2390. }
  2391. public static string sqlnew(string cInvCode, string cBatch, string cFree1, string cFree2, string cFree3, string cFree4, string cFree5, string cFree6, string cFree7, string cFree8, SqlCommand cmd)
  2392. {
  2393. bool bFree1 = false;
  2394. bool bFree2 = false;
  2395. bool bFree3 = false;
  2396. bool bFree4 = false;
  2397. bool bFree5 = false;
  2398. bool bFree6 = false;
  2399. bool bFree7 = false;
  2400. bool bFree8 = false;
  2401. #region 自由项管控
  2402. DataTable SubdtFree = DBHelper.bFree(cInvCode, cmd);
  2403. if (SubdtFree.Rows.Count > 0 && SubdtFree != null)
  2404. {
  2405. bFree1 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree1"]);
  2406. bFree2 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree2"]);
  2407. bFree3 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree3"]);
  2408. bFree4 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree4"]);
  2409. bFree5 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree5"]);
  2410. bFree6 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree6"]);
  2411. bFree7 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree7"]);
  2412. bFree8 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree8"]);
  2413. }
  2414. else
  2415. {
  2416. throw new Exception("存货编码:" + cInvCode + "不存在");
  2417. }
  2418. #endregion
  2419. string sql = "";
  2420. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  2421. sql += " and cBatch='" + cBatch + "' ";
  2422. else
  2423. sql += " and cBatch='' ";
  2424. if (bFree1)
  2425. {
  2426. sql += " and cFree1='" + cFree1 + "' ";
  2427. }
  2428. else
  2429. {
  2430. sql += " and cFree1='' ";
  2431. }
  2432. if (bFree2)
  2433. {
  2434. sql += " and cFree2='" + cFree2 + "' ";
  2435. }
  2436. else
  2437. {
  2438. sql += " and cFree2='' ";
  2439. }
  2440. if (bFree3)
  2441. {
  2442. sql += " and cFree3='" + cFree3 + "' ";
  2443. }
  2444. else
  2445. {
  2446. sql += " and cFree3='' ";
  2447. }
  2448. if (bFree4)
  2449. {
  2450. sql += " and cFree4='" + cFree4 + "' ";
  2451. }
  2452. else
  2453. {
  2454. sql += " and cFree4='' ";
  2455. }
  2456. if (bFree5)
  2457. {
  2458. sql += " and cFree5='" + cFree5 + "' ";
  2459. }
  2460. else
  2461. {
  2462. sql += " and cFree5='' ";
  2463. }
  2464. if (bFree6)
  2465. {
  2466. sql += " and cFree6='" + cFree6 + "' ";
  2467. }
  2468. else
  2469. {
  2470. sql += " and cFree6='' ";
  2471. }
  2472. if (bFree7)
  2473. {
  2474. sql += " and cFree7='" + cFree7 + "' ";
  2475. }
  2476. else
  2477. {
  2478. sql += " and cFree7='' ";
  2479. }
  2480. if (bFree8)
  2481. {
  2482. sql += " and cFree8='" + cFree8 + "' ";
  2483. }
  2484. else
  2485. {
  2486. sql += " and cFree8='' ";
  2487. }
  2488. return sql;
  2489. }
  2490. /// <summary>
  2491. /// 方法一:获取编号 返回Dictionary字典,调用该方法用Dictionary字典接收,只需要遍历Dictionary即可得到响应的值
  2492. /// </summary>
  2493. /// <param name="workPointCode"></param>
  2494. /// <param name="tbName"></param>
  2495. /// <param name="colName"></param>
  2496. /// <param name="Pre"></param>
  2497. /// <param name="numLen"></param>
  2498. /// <returns>返回Dictionary字典</returns>
  2499. public static Dictionary<string, int> GetAllCode(string cAcc_Id, string cVouchType, string iAmount)
  2500. {
  2501. string iFatherId = string.Empty, iChildId = string.Empty;
  2502. Dictionary<string, int> dic = new Dictionary<string, int>();
  2503. try
  2504. {
  2505. SqlConnection conn = new SqlConnection(ERPConnString);
  2506. SqlCommand cmd = new SqlCommand("sp_GetIDWithoutRemote", conn);
  2507. cmd.CommandType = CommandType.StoredProcedure;
  2508. cmd.Parameters.AddWithValue("@cAcc_Id", cAcc_Id);  //给输入参数赋值
  2509. cmd.Parameters.AddWithValue("@cVouchType", cVouchType);  //给输入参数赋值
  2510. cmd.Parameters.AddWithValue("@iAmount", iAmount);  //给输入参数赋值
  2511. //cmd.Parameters.AddWithValue("@iFatherId", iFatherId);  //给输入参数赋值
  2512. //cmd.Parameters.AddWithValue("@iChildId", iChildId);  //给输入参数赋值
  2513. SqlParameter parOutput = cmd.Parameters.Add("@iFatherId", SqlDbType.NVarChar, 50);  //定义输出参数
  2514. cmd.Parameters["@iFatherId"].Direction = ParameterDirection.Output;
  2515. SqlParameter parOutputs = cmd.Parameters.Add("@iChildId", SqlDbType.NVarChar, 50);  //定义输出参数
  2516. cmd.Parameters["@iChildId"].Direction = ParameterDirection.Output;
  2517. SqlParameter parReturn = new SqlParameter("@return", SqlDbType.Int);
  2518. parReturn.Direction = ParameterDirection.ReturnValue;   //参数类型为ReturnValue
  2519. cmd.Parameters.Add(parReturn);
  2520. conn.Open();
  2521. cmd.ExecuteNonQuery();
  2522. iFatherId = cmd.Parameters["@iFatherId"].Value.ToString();
  2523. iChildId = cmd.Parameters["@iChildId"].Value.ToString();
  2524. if (!string.IsNullOrWhiteSpace(iFatherId))//判断iFatherId是否为空,不为空的话将值放入dictionary字典中,否则int.Parse()会抛出异常
  2525. {
  2526. dic.Add("iFatherId", int.Parse(iFatherId));
  2527. dic.Add("iChildId", int.Parse(iChildId));
  2528. }
  2529. }
  2530. catch (System.Exception ex)
  2531. {
  2532. throw ex;
  2533. }
  2534. return dic;
  2535. }
  2536. public static Dictionary<string, int> GetAllCode(string cAcc_Id, string cVouchType, string iAmount,string WorkPoint)
  2537. {
  2538. string iFatherId = string.Empty, iChildId = string.Empty;
  2539. Dictionary<string, int> dic = new Dictionary<string, int>();
  2540. try
  2541. {
  2542. ERPConnString = string.Format(ERPConnString,WorkPoint);
  2543. SqlConnection conn = new SqlConnection(ERPConnString);
  2544. SqlCommand cmd = new SqlCommand("sp_GetIDWithoutRemote", conn);
  2545. cmd.CommandType = CommandType.StoredProcedure;
  2546. cmd.Parameters.AddWithValue("@cAcc_Id", cAcc_Id);  //给输入参数赋值
  2547. cmd.Parameters.AddWithValue("@cVouchType", cVouchType);  //给输入参数赋值
  2548. cmd.Parameters.AddWithValue("@iAmount", iAmount);  //给输入参数赋值
  2549. //cmd.Parameters.AddWithValue("@iFatherId", iFatherId);  //给输入参数赋值
  2550. //cmd.Parameters.AddWithValue("@iChildId", iChildId);  //给输入参数赋值
  2551. SqlParameter parOutput = cmd.Parameters.Add("@iFatherId", SqlDbType.NVarChar, 50);  //定义输出参数
  2552. cmd.Parameters["@iFatherId"].Direction = ParameterDirection.Output;
  2553. SqlParameter parOutputs = cmd.Parameters.Add("@iChildId", SqlDbType.NVarChar, 50);  //定义输出参数
  2554. cmd.Parameters["@iChildId"].Direction = ParameterDirection.Output;
  2555. SqlParameter parReturn = new SqlParameter("@return", SqlDbType.Int);
  2556. parReturn.Direction = ParameterDirection.ReturnValue;   //参数类型为ReturnValue
  2557. cmd.Parameters.Add(parReturn);
  2558. conn.Open();
  2559. cmd.ExecuteNonQuery();
  2560. iFatherId = cmd.Parameters["@iFatherId"].Value.ToString();
  2561. iChildId = cmd.Parameters["@iChildId"].Value.ToString();
  2562. if (!string.IsNullOrWhiteSpace(iFatherId))//判断iFatherId是否为空,不为空的话将值放入dictionary字典中,否则int.Parse()会抛出异常
  2563. {
  2564. dic.Add("iFatherId", int.Parse(iFatherId));
  2565. dic.Add("iChildId", int.Parse(iChildId));
  2566. }
  2567. }
  2568. catch (System.Exception ex)
  2569. {
  2570. throw ex;
  2571. }
  2572. return dic;
  2573. }
  2574. /// <summary>
  2575. /// 方法二 使用输出参数值
  2576. /// </summary>
  2577. /// <param name="cAcc_Id"></param>
  2578. /// <param name="cVouchType"></param>
  2579. /// <param name="iAmount"></param>
  2580. /// <returns></returns>
  2581. public static void GetAllCode(string cAcc_Id, string cVouchType, string iAmount, out int iFatherId, out int iChildId)
  2582. {
  2583. try
  2584. {
  2585. iFatherId = 0; iChildId = 0;
  2586. SqlConnection conn = new SqlConnection(ERPConnString);
  2587. SqlCommand cmd = new SqlCommand("sp_GetIDWithoutRemote", conn);
  2588. cmd.CommandType = CommandType.StoredProcedure;
  2589. cmd.Parameters.AddWithValue("@cAcc_Id", cAcc_Id);  //给输入参数赋值
  2590. cmd.Parameters.AddWithValue("@cVouchType", cVouchType);  //给输入参数赋值
  2591. cmd.Parameters.AddWithValue("@iAmount", iAmount);  //给输入参数赋值
  2592. //cmd.Parameters.AddWithValue("@iFatherId", iFatherId);  //给输入参数赋值
  2593. //cmd.Parameters.AddWithValue("@iChildId", iChildId);  //给输入参数赋值
  2594. SqlParameter parOutput = cmd.Parameters.Add("@iFatherId", SqlDbType.NVarChar, 50);  //定义输出参数
  2595. cmd.Parameters["@iFatherId"].Direction = ParameterDirection.Output;
  2596. SqlParameter parOutputs = cmd.Parameters.Add("@iChildId", SqlDbType.NVarChar, 50);  //定义输出参数
  2597. cmd.Parameters["@iChildId"].Direction = ParameterDirection.Output;
  2598. //SqlParameter parReturn = new SqlParameter("@return", SqlDbType.Int);
  2599. //parReturn.Direction = ParameterDirection.ReturnValue;   //参数类型为ReturnValue
  2600. //cmd.Parameters.Add(parReturn);
  2601. conn.Open();
  2602. cmd.ExecuteNonQuery();
  2603. string _iFatherId = cmd.Parameters["@iFatherId"].Value.ToString();
  2604. string _iChildId = cmd.Parameters["@iChildId"].Value.ToString();
  2605. if (!string.IsNullOrWhiteSpace(_iFatherId))
  2606. {
  2607. iFatherId = int.Parse(_iFatherId);
  2608. }
  2609. if (!string.IsNullOrWhiteSpace(_iChildId))
  2610. {
  2611. iChildId = int.Parse(_iChildId);
  2612. }
  2613. }
  2614. catch (System.Exception ex)
  2615. {
  2616. throw ex;
  2617. }
  2618. }
  2619. /// <summary>
  2620. /// 方法二 使用输出参数值
  2621. /// </summary>
  2622. /// <param name="cAcc_Id"></param>
  2623. /// <param name="cVouchType"></param>
  2624. /// <param name="iAmount"></param>
  2625. /// <returns></returns>
  2626. public static string GetAllRDCode(string CardNumber, string dDate, string UserCode)
  2627. {
  2628. string iBaseCodeLen = string.Empty; string cVouchCodeBase = string.Empty;
  2629. Dictionary<string, int> dic = new Dictionary<string, int>();
  2630. try
  2631. {
  2632. SqlConnection conn = new SqlConnection(ERPConnString);
  2633. SqlCommand cmd = new SqlCommand("ICS_VoucherNumber", conn);
  2634. cmd.CommandType = CommandType.StoredProcedure;
  2635. cmd.Parameters.AddWithValue("@CardNumber", CardNumber);  //给输入参数赋值
  2636. cmd.Parameters.AddWithValue("@dDate", dDate);  //给输入参数赋值
  2637. cmd.Parameters.AddWithValue("@UserCode", UserCode);  //给输入参数赋值
  2638. SqlParameter parOutput = cmd.Parameters.Add("@number", SqlDbType.NVarChar, 50);  //定义输出参数
  2639. parOutput.Direction = ParameterDirection.Output;
  2640. //SqlParameter parReturn = new SqlParameter("@return", SqlDbType.Int);
  2641. //parReturn.Direction = ParameterDirection.ReturnValue;   //参数类型为ReturnValue
  2642. //cmd.Parameters.Add(parReturn);
  2643. conn.Open();
  2644. cmd.ExecuteNonQuery();
  2645. return parOutput.Value.ToString();
  2646. //iBaseCodeLen = cmd.Parameters["@iBaseCodeLen"].Value.ToString();
  2647. // cVouchCodeBase = cmd.Parameters["@cVouchCodeBase"].Value.ToString();
  2648. //UserCode= cmd.Parameters["@cVouchCodePreFix"].Value.ToString();
  2649. ////if (!string.IsNullOrWhiteSpace(iBaseCodeLen))//判断iFatherId是否为空,不为空的话将值放入dictionary字典中,否则int.Parse()会抛出异常
  2650. ////{
  2651. //// dic.Add("iBaseCodeLen", int.Parse(iBaseCodeLen));
  2652. ////}
  2653. ////else
  2654. ////{
  2655. //// dic.Add("cVouchCodeBase", int.Parse(cVouchCodeBase));
  2656. // UserCode= UserCode + (cVouchCodeBase.ToString().PadLeft(int.Parse(iBaseCodeLen), '0'));
  2657. ////}
  2658. }
  2659. catch (System.Exception ex)
  2660. {
  2661. throw ex;
  2662. }
  2663. //return UserCode;
  2664. }
  2665. public static string GetAllRDCode(string CardNumber, string dDate, string UserCode,string cRdCode, string WorkPoint)
  2666. {
  2667. string iBaseCodeLen = string.Empty; string cVouchCodeBase = string.Empty;
  2668. Dictionary<string, int> dic = new Dictionary<string, int>();
  2669. try
  2670. {
  2671. ERPConnString = string.Format(ERPConnString, WorkPoint);
  2672. SqlConnection conn = new SqlConnection(ERPConnString);
  2673. SqlCommand cmd = new SqlCommand("ICS_VoucherNumber", conn);
  2674. cmd.CommandType = CommandType.StoredProcedure;
  2675. cmd.Parameters.AddWithValue("@CardNumber", CardNumber);  //给输入参数赋值
  2676. cmd.Parameters.AddWithValue("@dDate", dDate);  //给输入参数赋值
  2677. cmd.Parameters.AddWithValue("@UserCode", UserCode);  //给输入参数赋值
  2678. cmd.Parameters.AddWithValue("@cRdCode", cRdCode);  //给输入参数赋值
  2679. SqlParameter parOutput = cmd.Parameters.Add("@number", SqlDbType.NVarChar, 50);  //定义输出参数
  2680. parOutput.Direction = ParameterDirection.Output;
  2681. //SqlParameter parReturn = new SqlParameter("@return", SqlDbType.Int);
  2682. //parReturn.Direction = ParameterDirection.ReturnValue;   //参数类型为ReturnValue
  2683. //cmd.Parameters.Add(parReturn);
  2684. conn.Open();
  2685. cmd.ExecuteNonQuery();
  2686. return parOutput.Value.ToString();
  2687. //iBaseCodeLen = cmd.Parameters["@iBaseCodeLen"].Value.ToString();
  2688. // cVouchCodeBase = cmd.Parameters["@cVouchCodeBase"].Value.ToString();
  2689. //UserCode= cmd.Parameters["@cVouchCodePreFix"].Value.ToString();
  2690. ////if (!string.IsNullOrWhiteSpace(iBaseCodeLen))//判断iFatherId是否为空,不为空的话将值放入dictionary字典中,否则int.Parse()会抛出异常
  2691. ////{
  2692. //// dic.Add("iBaseCodeLen", int.Parse(iBaseCodeLen));
  2693. ////}
  2694. ////else
  2695. ////{
  2696. //// dic.Add("cVouchCodeBase", int.Parse(cVouchCodeBase));
  2697. // UserCode= UserCode + (cVouchCodeBase.ToString().PadLeft(int.Parse(iBaseCodeLen), '0'));
  2698. ////}
  2699. }
  2700. catch (System.Exception ex)
  2701. {
  2702. throw ex;
  2703. }
  2704. //return UserCode;
  2705. }
  2706. public static string GetRDCode(string RDName,SqlCommand cmd)
  2707. {
  2708. string sql = @"select cRdCode from Rd_Style where cRdName='{0}'";
  2709. sql = string.Format(sql,RDName);
  2710. DataTable dt = SQlReturnData(sql,cmd);
  2711. return dt.Rows[0]["cRdCode"].ToString();
  2712. }
  2713. public static string GetCardNumber(string RDName, SqlCommand cmd)
  2714. {
  2715. string sql = @"select CardNumber from vouchernumber where CardName='{0}'";
  2716. sql = string.Format(sql, RDName);
  2717. DataTable dt = SQlReturnData(sql, cmd);
  2718. return dt.Rows[0]["CardNumber"].ToString();
  2719. }
  2720. /// <summary>
  2721. /// DataSet转Json字符串,主子结构
  2722. /// 需要建立主子表关系,第一张表为主表
  2723. /// 会排除关联列
  2724. /// </summary>
  2725. /// <param name="dataSet"></param>
  2726. /// <param name="RelationName">关系名称</param>
  2727. /// <returns></returns>
  2728. public static string DataSetToJson(DataSet dataSet, string RelationName)
  2729. {
  2730. StringBuilder jsonString = new StringBuilder();
  2731. //foreach (DataTable table in dataSet.Tables)
  2732. //{
  2733. DataTable table = dataSet.Tables[0];
  2734. jsonString.Append("[");
  2735. DataRowCollection drc = table.Rows;
  2736. for (int i = 0; i < drc.Count; i++)
  2737. {
  2738. DataRow dataRow = drc[i];
  2739. jsonString.Append("{");
  2740. for (int j = 0; j < table.Columns.Count; j++)
  2741. {
  2742. string strKey = table.Columns[j].ColumnName;
  2743. if (dataSet.Relations[RelationName].ParentColumns.Select(a => a.Caption).Contains(strKey))
  2744. continue;
  2745. string strValue = dataRow[j].ToString();
  2746. Type type = table.Columns[j].DataType;
  2747. jsonString.Append("\"" + strKey + "\":");
  2748. strValue = StringFormat(strValue, type);
  2749. if (j < table.Columns.Count - 1)
  2750. {
  2751. jsonString.Append(strValue + ",");
  2752. }
  2753. else
  2754. {
  2755. jsonString.Append(strValue);
  2756. }
  2757. }
  2758. jsonString.Append(",\"" + RelationName + "\":");
  2759. DataRow[] drs = dataRow.GetChildRows(RelationName);
  2760. jsonString.Append("[");
  2761. foreach (DataRow dr in drs)
  2762. {
  2763. DataTable dt = dr.Table;
  2764. jsonString.Append("{");
  2765. for (int j = 0; j < dt.Columns.Count; j++)
  2766. {
  2767. string strKey = dt.Columns[j].ColumnName;
  2768. if (dataSet.Relations[RelationName].ChildColumns.Select(a => a.Caption).Contains(strKey))
  2769. continue;
  2770. string strValue = dr[j].ToString();
  2771. Type type = dt.Columns[j].DataType;
  2772. jsonString.Append("\"" + strKey + "\":");
  2773. strValue = StringFormat(strValue, type);
  2774. if (j < dt.Columns.Count - 1)
  2775. {
  2776. jsonString.Append(strValue + ",");
  2777. }
  2778. else
  2779. {
  2780. jsonString.Append(strValue);
  2781. }
  2782. }
  2783. jsonString.Append("},");
  2784. }
  2785. if (drs.Length > 0)
  2786. jsonString.Remove(jsonString.Length - 1, 1);
  2787. jsonString.Append("]");
  2788. jsonString.Append("},");
  2789. }
  2790. jsonString.Remove(jsonString.Length - 1, 1);
  2791. jsonString.Append("]");
  2792. //}
  2793. string res = jsonString.ToString();
  2794. return res;
  2795. }
  2796. #region 私有方法
  2797. /// <summary>
  2798. /// 过滤特殊字符
  2799. /// </summary>
  2800. /// <param name="s">字符串</param>
  2801. /// <returns>json字符串</returns>
  2802. private static string String2Json(String s)
  2803. {
  2804. StringBuilder sb = new StringBuilder();
  2805. for (int i = 0; i < s.Length; i++)
  2806. {
  2807. char c = s.ToCharArray()[i];
  2808. switch (c)
  2809. {
  2810. case '\"':
  2811. sb.Append("\\\""); break;
  2812. case '\\':
  2813. sb.Append("\\\\"); break;
  2814. case '/':
  2815. sb.Append("\\/"); break;
  2816. case '\b':
  2817. sb.Append("\\b"); break;
  2818. case '\f':
  2819. sb.Append("\\f"); break;
  2820. case '\n':
  2821. sb.Append("\\n"); break;
  2822. case '\r':
  2823. sb.Append("\\r"); break;
  2824. case '\t':
  2825. sb.Append("\\t"); break;
  2826. default:
  2827. sb.Append(c); break;
  2828. }
  2829. }
  2830. return sb.ToString();
  2831. }
  2832. /// <summary>
  2833. /// 格式化字符型、日期型、布尔型
  2834. /// </summary>
  2835. /// <param name="str"></param>
  2836. /// <param name="type"></param>
  2837. /// <returns></returns>
  2838. private static string StringFormat(string str, Type type)
  2839. {
  2840. if (type == typeof(string))
  2841. {
  2842. str = String2Json(str);
  2843. str = "\"" + str + "\"";
  2844. }
  2845. else if (type == typeof(DateTime))
  2846. {
  2847. str = "\"" + str + "\"";
  2848. }
  2849. else if (type == typeof(bool))
  2850. {
  2851. str = str.ToLower();
  2852. }
  2853. else if (type != typeof(string) && string.IsNullOrEmpty(str))
  2854. {
  2855. str = "\"" + str + "\"";
  2856. }
  2857. return str;
  2858. }
  2859. #endregion
  2860. public static DataSet SQlReturnDataSet(string SQl, SqlCommand cmd)
  2861. {
  2862. DataSet ds = new DataSet();
  2863. SqlDataAdapter dr = new System.Data.SqlClient.SqlDataAdapter();
  2864. cmd.CommandText = SQl;
  2865. dr.SelectCommand = cmd;
  2866. dr.Fill(ds);
  2867. return ds;
  2868. }
  2869. /// <summary>
  2870. /// 获取用户的姓名与部门
  2871. /// </summary>
  2872. /// <param name="cPer_Num">用户编号</param>
  2873. /// <param name="cmd"></param>
  2874. /// <returns></returns>
  2875. public static ICSUserInfo GetPersonInfo(string cPer_Num, SqlCommand cmd)
  2876. {
  2877. ICSUserInfo person = new ICSUserInfo();
  2878. string sql = @" exec sp_refreshview ua_user ;
  2879. SELECT cPsn_Num,cPsn_Name,cDept_num FROM hr_hi_person
  2880. WHERE cPsn_Num ='" + cPer_Num + "'";
  2881. cmd.CommandText = sql;
  2882. DataTable dt = SQlReturnData(sql, cmd);
  2883. if (dt != null && dt.Rows.Count > 0)
  2884. {
  2885. person.DepCode = dt.Rows[0]["cDept_num"].ToString();
  2886. person.UserName = dt.Rows[0]["cPsn_Name"].ToString();
  2887. person.UserCode = cPer_Num;
  2888. }
  2889. else
  2890. throw new Exception("ERP人员不存在,编码:" + cPer_Num);
  2891. return person;
  2892. }
  2893. }
  2894. }