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

2878 lines
121 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  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. if (cFree1 != null && cFree1!="")
  918. {
  919. NewcBatch += " and cFree1='{1}' ";
  920. newsql += ",cFree1";
  921. newsqlvalues = ",'" + cFree1 + @"'";
  922. }
  923. if (cFree2 != null && cFree2 != "")
  924. {
  925. NewcBatch += " and cFree2='{2}' ";
  926. newsql += ",cFree2";
  927. newsqlvalues = ",'" + cFree2 + @"'";
  928. }
  929. if (cFree3 != null && cFree3 != "")
  930. {
  931. NewcBatch += " and cFree3='{3}' ";
  932. newsql += ",cFree3";
  933. newsqlvalues = ",'" + cFree3 + @"'";
  934. }
  935. if (cFree4 != null && cFree4 != "")
  936. {
  937. NewcBatch += " and cFree4='{4}' ";
  938. newsql += ",cFree4";
  939. newsqlvalues = ",'" + cFree4 + @"'";
  940. }
  941. if (cFree5 != null && cFree5 != "")
  942. {
  943. NewcBatch += " and cFree5='{5}' ";
  944. newsql += ",cFree5";
  945. newsqlvalues = ",'" + cFree5 + @"'";
  946. }
  947. if (cFree6 != null && cFree6 != "")
  948. {
  949. NewcBatch += " and cFree6='{6}' ";
  950. newsql += ",cFree6";
  951. newsqlvalues = ",'" + cFree6 + @"'";
  952. }
  953. if (cFree7 != null && cFree7 != "")
  954. {
  955. NewcBatch += " and cFree7='{7}' ";
  956. newsql += ",cFree7";
  957. newsqlvalues = ",'" + cFree7 + @"'";
  958. }
  959. if (cFree8 != null && cFree8 != "")
  960. {
  961. NewcBatch += " and cFree8='{8}' ";
  962. newsql += ",cFree8";
  963. newsqlvalues = ",'" + cFree8 + @"'";
  964. }
  965. if (cFree9 != null && cFree9 != "")
  966. {
  967. NewcBatch += " and cFree9='{9}' ";
  968. newsql += ",cFree9";
  969. newsqlvalues = ",'" + cFree9 + @"'";
  970. }
  971. if (cFree10 != null && cFree10 != "")
  972. {
  973. NewcBatch += " and cFree10='{10}' ";
  974. newsql += ",cFree10";
  975. newsqlvalues = ",'" + cFree10 + @"'";
  976. }
  977. #region 1 取得物料的itemID
  978. string sql = @"IF NOT EXISTS(
  979. SELECT Id FROM dbo.SCM_Item WHERE
  980. cinvcode = '{0}' 1=1 )
  981. BEGIN
  982. INSERT INTO dbo.SCM_Item(cInvCode ,cFree1 ,cFree2 ,cFree3 ,cFree4 , cFree5 ,
  983. cFree6 ,cFree7 ,cFree8 ,cFree9 ,cFree10 ,PartId) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}',0)
  984. END
  985. SELECT Id FROM
  986. dbo.SCM_Item WHERE cinvcode = '{0}' 1=1 ";
  987. if (NewcBatch!="")
  988. sql = sql.Replace("1=1",NewcBatch);
  989. else
  990. sql = sql.Replace("1=1", "");
  991. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10);
  992. cmd.CommandText = sql;
  993. DataTable dtItem = SQlReturnData(sql, cmd);
  994. if (dtItem.Rows.Count == 0)
  995. {
  996. throw new Exception("物料的ItemID取得失败");
  997. }
  998. int ItemID = int.Parse(dtItem.Rows[0]["Id"].ToString());
  999. //log.Info("取得物料的itemID" + sql);
  1000. #endregion
  1001. #region 2 更新失败,插入现存量
  1002. sql = @"SELECT AutoID FROM dbo.CurrentStock
  1003. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  1004. AND ItemId = @ItemId";
  1005. if (cBatch != null)
  1006. {
  1007. if (bInvBatch(cInvCode, cmd) == true)
  1008. sql += " and cBatch='" + cBatch + "' ";
  1009. else
  1010. sql += " and cBatch='' ";
  1011. }
  1012. else
  1013. sql += " and cBatch='' ";
  1014. if (cFree1 != null && cFree1 != "")
  1015. sql += " and cFree1='{1}' ";
  1016. if (cFree2 != null && cFree2 != "")
  1017. sql += " and cFree2='{2}' ";
  1018. if (cFree3 != null && cFree3 != "")
  1019. sql += " and cFree3='{3}' ";
  1020. if (cFree4 != null && cFree4 != "")
  1021. sql += " and cFree4='{4}' ";
  1022. if (cFree5 != null && cFree5 != "")
  1023. sql += " and cFree5='{5}' ";
  1024. if (cFree6 != null && cFree6 != "")
  1025. sql += " and cFree6='{6}' ";
  1026. if (cFree7 != null && cFree7 != "")
  1027. sql += " and cFree7='{7}' ";
  1028. if (cFree8 != null && cFree8 != "")
  1029. sql += " and cFree8='{8}' ";
  1030. if (cFree9 != null && cFree9 != "")
  1031. sql += " and cFree9='{9}' ";
  1032. if (cFree10 != null && cFree10 != "")
  1033. sql += " and cFree10='{10}' ";
  1034. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10);
  1035. cmd.CommandText = sql;
  1036. cmd.Parameters.Clear();
  1037. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1038. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1039. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1040. DataTable dtCurrentStock = SQlReturnData(sql, cmd);
  1041. //log.Info("查找现存量:" + sql);
  1042. if (dtCurrentStock != null && dtCurrentStock.Rows.Count > 0)
  1043. {
  1044. sql = @"UPDATE dbo.CurrentStock SET iQuantity = iQuantity + @iQuantity
  1045. {0}
  1046. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  1047. AND ItemId = @ItemId";
  1048. if (key.cBustypeUN.Contains("入库") && key.UpdateTodoQuantity == true)
  1049. {
  1050. sql = string.Format(sql, ",fInQuantity=isnull(fInQuantity,0)-@iQuantity");
  1051. }
  1052. else if (key.cBustypeUN.Contains("出库") && key.UpdateTodoQuantity == true)
  1053. {
  1054. sql = string.Format(sql, ",fOutQuantity=isnull(fOutQuantity,0)-@iQuantity");
  1055. }
  1056. else if (key.cBustypeUN == "调拨" && key.UpdateTodoQuantity == true)
  1057. {
  1058. sql = string.Format(sql, ", fTransOutQuantity=ISNULL(a.fTransOutQuantity,0)-ISNULL(a.iQuantity,0),fTransInQuantity=ISNULL(fTransInQuantity,0)-ISNULL(a.iQuantity,0)");
  1059. }
  1060. else
  1061. {
  1062. sql = string.Format(sql, "");
  1063. }
  1064. if (cBatch != null)
  1065. {
  1066. if (bInvBatch(cInvCode, cmd) == true)
  1067. sql += " and cBatch='" + cBatch + "' ";
  1068. else
  1069. sql += " and cBatch='' ";
  1070. }
  1071. else
  1072. {
  1073. sql += " and cBatch='' ";
  1074. }
  1075. if (cFree1 != null && cFree1 != "")
  1076. sql += " and cFree1='{1}' ";
  1077. if (cFree2 != null && cFree2 != "")
  1078. sql += " and cFree2='{2}' ";
  1079. if (cFree3 != null && cFree3 != "")
  1080. sql += " and cFree3='{3}' ";
  1081. if (cFree4 != null && cFree4 != "")
  1082. sql += " and cFree4='{4}' ";
  1083. if (cFree5 != null && cFree5 != "")
  1084. sql += " and cFree5='{5}' ";
  1085. if (cFree6 != null && cFree6 != "")
  1086. sql += " and cFree6='{6}' ";
  1087. if (cFree7 != null && cFree7 != "")
  1088. sql += " and cFree7='{7}' ";
  1089. if (cFree8 != null && cFree8 != "")
  1090. sql += " and cFree8='{8}' ";
  1091. if (cFree9 != null && cFree9 != "")
  1092. sql += " and cFree9='{9}' ";
  1093. if (cFree10 != null && cFree10 != "")
  1094. sql += " and cFree10='{10}' ";
  1095. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10);
  1096. cmd.CommandText = sql;
  1097. cmd.Parameters.Clear();
  1098. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  1099. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1100. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1101. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1102. CmdExecuteNonQuery(sql, cmd, "更新现存量失败!");
  1103. //log.Info("现存量更新:" + sql);
  1104. }
  1105. else
  1106. {
  1107. sql = @" INSERT INTO dbo.CurrentStock
  1108. (cWhCode,cInvCode,ItemId,cBatch,iSoType,iSodid,iQuantity,
  1109. iNum,fOutQuantity,fOutNum,fInQuantity,fInNum,
  1110. bStopFlag,fTransInQuantity,fTransInNum,
  1111. fTransOutQuantity,fTransOutNum,fPlanQuantity,fPlanNum,fDisableQuantity,
  1112. fDisableNum,fAvaQuantity,fAvaNum,BGSPSTOP,fStopQuantity,
  1113. fStopNum,ipeqty,ipenum,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  1114. SELECT @cWhCode,@cInvCode,@ItemId,@cBatch,'0','',@iQuantity,
  1115. '0','0','0','0','0',
  1116. '0','0','0','0','0','0','0','0',
  1117. '0','0','0','0','0','0','0','0',@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10 ";
  1118. if (newsql!="")
  1119. sql = sql.Replace("1=1", newsql);
  1120. else
  1121. sql = sql.Replace("1=1", "");
  1122. if (newsqlvalues != "")
  1123. sql = sql.Replace("2=2", newsqlvalues);
  1124. else
  1125. sql = sql.Replace("2=2", "");
  1126. cmd.CommandText = sql;
  1127. cmd.Parameters.Clear();
  1128. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1129. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1130. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1131. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  1132. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  1133. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  1134. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  1135. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  1136. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  1137. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  1138. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  1139. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  1140. cmd.Parameters.Add(new SqlParameter("@cFree9", cFree9));
  1141. cmd.Parameters.Add(new SqlParameter("@cFree10", cFree10));
  1142. if (cBatch != null)
  1143. {
  1144. if (bInvBatch(cInvCode, cmd) == true)
  1145. cmd.Parameters.Add(new SqlParameter("@cBatch", cBatch));
  1146. else
  1147. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  1148. }
  1149. else
  1150. {
  1151. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  1152. }
  1153. CmdExecuteNonQuery(sql, cmd, "插入现存量失败!");
  1154. //log.Info("现存量插入:" + sql);
  1155. }
  1156. #endregion
  1157. #region 判断现存量是否足够
  1158. //sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0)
  1159. // BEGIN
  1160. // DECLARE @MSG NVARCHAR(100)
  1161. // SELECT @MSG='ERP库存不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0
  1162. // RAISERROR(@MSG,16,1)
  1163. // END";
  1164. //cmd.CommandText = sql;
  1165. //cmd.ExecuteNonQuery();
  1166. #endregion
  1167. //判断仓库是否记入成本
  1168. sql = "SELECT bInCost FROM Warehouse WHERE cWhCode='{0}'";
  1169. sql = string.Format(sql, cWhCode);
  1170. cmd.CommandText = sql;
  1171. DataTable dtwh = SQlReturnData(sql, cmd);
  1172. if (!dtwh.Rows[0]["bInCost"].ToString().Equals("0"))
  1173. {
  1174. #region 3 写入记账表
  1175. sql = string.Format(@" INSERT INTO [dbo].[" + key.TableName + @"]
  1176. SELECT '{0}','{1}','{2}','{3}'", key.ID, key.DID, key.cVouchTypeUN, key.cBustypeUN);
  1177. cmd.CommandText = sql;
  1178. CmdExecuteNonQuery(sql, cmd, "采购入库单写入记账表失败!");
  1179. #endregion
  1180. }
  1181. #region 判断现存量是否足够
  1182. sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fOutQuantity<0 and cWhCode = '{0}' AND cInvCode = '{1}')
  1183. BEGIN
  1184. DECLARE @MSG NVARCHAR(100)
  1185. SELECT @MSG='ERP待出库数量不足AutoID'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fOutQuantity<0
  1186. RAISERROR(@MSG,16,1)
  1187. END";
  1188. sql = string.Format(sql, cWhCode, cInvCode);
  1189. cmd.CommandText = sql;
  1190. cmd.ExecuteNonQuery();
  1191. #endregion
  1192. }
  1193. /// <summary>
  1194. /// 更新现存量
  1195. /// </summary>
  1196. /// <param name="cmd"></param>
  1197. /// <param name="cInvCode"></param>
  1198. /// <param name="cWhCode"></param>
  1199. /// <param name="cBatch"></param>
  1200. 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)
  1201. {
  1202. string newsql = "";
  1203. string newsqlvalues = "";
  1204. string NewcBatch = "";
  1205. if (cFree1 != null && cFree1 != "")
  1206. {
  1207. NewcBatch += " and cFree1='{1}' ";
  1208. newsql += ",cFree1";
  1209. newsqlvalues = ",'" + cFree1 + @"'";
  1210. }
  1211. if (cFree2 != null && cFree2 != "")
  1212. {
  1213. NewcBatch += " and cFree2='{2}' ";
  1214. newsql += ",cFree2";
  1215. newsqlvalues = ",'" + cFree2 + @"'";
  1216. }
  1217. if (cFree3 != null && cFree3 != "")
  1218. {
  1219. NewcBatch += " and cFree3='{3}' ";
  1220. newsql += ",cFree3";
  1221. newsqlvalues = ",'" + cFree3 + @"'";
  1222. }
  1223. if (cFree4 != null && cFree4 != "")
  1224. {
  1225. NewcBatch += " and cFree4='{4}' ";
  1226. newsql += ",cFree4";
  1227. newsqlvalues = ",'" + cFree4 + @"'";
  1228. }
  1229. if (cFree5 != null && cFree5 != "")
  1230. {
  1231. NewcBatch += " and cFree5='{5}' ";
  1232. newsql += ",cFree5";
  1233. newsqlvalues = ",'" + cFree5 + @"'";
  1234. }
  1235. if (cFree6 != null && cFree6 != "")
  1236. {
  1237. NewcBatch += " and cFree6='{6}' ";
  1238. newsql += ",cFree6";
  1239. newsqlvalues = ",'" + cFree6 + @"'";
  1240. }
  1241. if (cFree7 != null && cFree7 != "")
  1242. {
  1243. NewcBatch += " and cFree7='{7}' ";
  1244. newsql += ",cFree7";
  1245. newsqlvalues = ",'" + cFree7 + @"'";
  1246. }
  1247. if (cFree8 != null && cFree8 != "")
  1248. {
  1249. NewcBatch += " and cFree8='{8}' ";
  1250. newsql += ",cFree8";
  1251. newsqlvalues = ",'" + cFree8 + @"'";
  1252. }
  1253. if (cFree9 != null && cFree9 != "")
  1254. {
  1255. NewcBatch += " and cFree9='{9}' ";
  1256. newsql += ",cFree9";
  1257. newsqlvalues = ",'" + cFree9 + @"'";
  1258. }
  1259. if (cFree10 != null && cFree10 != "")
  1260. {
  1261. NewcBatch += " and cFree10='{10}' ";
  1262. newsql += ",cFree10";
  1263. newsqlvalues = ",'" + cFree10 + @"'";
  1264. }
  1265. if (cVenCode != null && cVenCode != "")
  1266. {
  1267. NewcBatch += " and cVMIVenCode='{10}' ";
  1268. newsql += ",cVenCode";
  1269. newsqlvalues = ",'" + cVenCode + @"'";
  1270. }
  1271. #region 1 取得物料的itemID
  1272. string sql = @"IF NOT EXISTS(
  1273. SELECT Id FROM dbo.SCM_Item WHERE
  1274. cinvcode = '{0}' 1=1 )
  1275. BEGIN
  1276. INSERT INTO dbo.SCM_Item(cInvCode ,cFree1 ,cFree2 ,cFree3 ,cFree4 , cFree5 ,
  1277. cFree6 ,cFree7 ,cFree8 ,cFree9 ,cFree10 ,PartId,cVMIVenCode)
  1278. VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}',0,'{11}')
  1279. END
  1280. SELECT Id FROM
  1281. dbo.SCM_Item WHERE cinvcode = '{0}' 1=1 ";
  1282. if (NewcBatch != "")
  1283. sql = sql.Replace("1=1", NewcBatch);
  1284. else
  1285. sql = sql.Replace("1=1", "");
  1286. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,cVenCode);
  1287. cmd.CommandText = sql;
  1288. DataTable dtItem = SQlReturnData(sql, cmd);
  1289. if (dtItem.Rows.Count == 0)
  1290. {
  1291. throw new Exception("物料的ItemID取得失败");
  1292. }
  1293. int ItemID = int.Parse(dtItem.Rows[0]["Id"].ToString());
  1294. //log.Info("取得物料的itemID" + sql);
  1295. #endregion
  1296. #region 2 更新失败,插入现存量
  1297. sql = @"SELECT AutoID FROM dbo.CurrentStock
  1298. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  1299. AND ItemId = @ItemId";
  1300. if (cBatch != null)
  1301. {
  1302. if (bInvBatch(cInvCode, cmd) == true)
  1303. sql += " and cBatch='" + cBatch + "' ";
  1304. else
  1305. sql += " and cBatch='' ";
  1306. }
  1307. else
  1308. sql += " and cBatch='' ";
  1309. if (cFree1 != null && cFree1 != "")
  1310. sql += " and cFree1='{1}' ";
  1311. if (cFree2 != null && cFree2 != "")
  1312. sql += " and cFree2='{2}' ";
  1313. if (cFree3 != null && cFree3 != "")
  1314. sql += " and cFree3='{3}' ";
  1315. if (cFree4 != null && cFree4 != "")
  1316. sql += " and cFree4='{4}' ";
  1317. if (cFree5 != null && cFree5 != "")
  1318. sql += " and cFree5='{5}' ";
  1319. if (cFree6 != null && cFree6 != "")
  1320. sql += " and cFree6='{6}' ";
  1321. if (cFree7 != null && cFree7 != "")
  1322. sql += " and cFree7='{7}' ";
  1323. if (cFree8 != null && cFree8 != "")
  1324. sql += " and cFree8='{8}' ";
  1325. if (cFree9 != null && cFree9 != "")
  1326. sql += " and cFree9='{9}' ";
  1327. if (cFree10 != null && cFree10 != "")
  1328. sql += " and cFree10='{10}' ";
  1329. if (cVenCode != null && cVenCode != "")
  1330. sql += " and cVMIVenCode='{11}' ";
  1331. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,cVenCode);
  1332. cmd.CommandText = sql;
  1333. cmd.Parameters.Clear();
  1334. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1335. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1336. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1337. DataTable dtCurrentStock = SQlReturnData(sql, cmd);
  1338. //log.Info("查找现存量:" + sql);
  1339. if (dtCurrentStock != null && dtCurrentStock.Rows.Count > 0)
  1340. {
  1341. sql = @"UPDATE dbo.CurrentStock SET iQuantity = iQuantity + @iQuantity
  1342. {0}
  1343. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  1344. AND ItemId = @ItemId";
  1345. if (key.cBustypeUN.Contains("入库") && key.UpdateTodoQuantity == true)
  1346. {
  1347. sql = string.Format(sql, ",fInQuantity=isnull(fInQuantity,0)-@iQuantity");
  1348. }
  1349. else if (key.cBustypeUN.Contains("出库") && key.UpdateTodoQuantity == true)
  1350. {
  1351. sql = string.Format(sql, ",fOutQuantity=isnull(fOutQuantity,0)-@iQuantity");
  1352. }
  1353. else if (key.cBustypeUN == "调拨" && key.UpdateTodoQuantity == true)
  1354. {
  1355. sql = string.Format(sql, ", fTransOutQuantity=ISNULL(a.fTransOutQuantity,0)-ISNULL(a.iQuantity,0),fTransInQuantity=ISNULL(fTransInQuantity,0)-ISNULL(a.iQuantity,0)");
  1356. }
  1357. else
  1358. {
  1359. sql = string.Format(sql, "");
  1360. }
  1361. if (cBatch != null)
  1362. {
  1363. if (bInvBatch(cInvCode, cmd) == true)
  1364. sql += " and cBatch='" + cBatch + "' ";
  1365. else
  1366. sql += " and cBatch='' ";
  1367. }
  1368. else
  1369. {
  1370. sql += " and cBatch='' ";
  1371. }
  1372. if (cFree1 != null && cFree1 != "")
  1373. sql += " and cFree1='{1}' ";
  1374. if (cFree2 != null && cFree2 != "")
  1375. sql += " and cFree2='{2}' ";
  1376. if (cFree3 != null && cFree3 != "")
  1377. sql += " and cFree3='{3}' ";
  1378. if (cFree4 != null && cFree4 != "")
  1379. sql += " and cFree4='{4}' ";
  1380. if (cFree5 != null && cFree5 != "")
  1381. sql += " and cFree5='{5}' ";
  1382. if (cFree6 != null && cFree6 != "")
  1383. sql += " and cFree6='{6}' ";
  1384. if (cFree7 != null && cFree7 != "")
  1385. sql += " and cFree7='{7}' ";
  1386. if (cFree8 != null && cFree8 != "")
  1387. sql += " and cFree8='{8}' ";
  1388. if (cFree9 != null && cFree9 != "")
  1389. sql += " and cFree9='{9}' ";
  1390. if (cFree10 != null && cFree10 != "")
  1391. sql += " and cFree10='{10}' ";
  1392. if (cVenCode != null && cVenCode != "")
  1393. sql += " and cVMIVenCode='{11}' ";
  1394. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,cVenCode);
  1395. cmd.CommandText = sql;
  1396. cmd.Parameters.Clear();
  1397. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  1398. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1399. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1400. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1401. CmdExecuteNonQuery(sql, cmd, "更新现存量失败!");
  1402. //log.Info("现存量更新:" + sql);
  1403. }
  1404. else
  1405. {
  1406. sql = @" INSERT INTO dbo.CurrentStock
  1407. (cWhCode,cInvCode,ItemId,cBatch,iSoType,iSodid,iQuantity,
  1408. iNum,fOutQuantity,fOutNum,fInQuantity,fInNum,
  1409. bStopFlag,fTransInQuantity,fTransInNum,
  1410. fTransOutQuantity,fTransOutNum,fPlanQuantity,fPlanNum,fDisableQuantity,
  1411. fDisableNum,fAvaQuantity,fAvaNum,BGSPSTOP,fStopQuantity,
  1412. fStopNum,ipeqty,ipenum,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,cVMIVenCode)
  1413. SELECT @cWhCode,@cInvCode,@ItemId,@cBatch,'0','',@iQuantity,
  1414. '0','0','0','0','0',
  1415. '0','0','0','0','0','0','0','0',
  1416. '0','0','0','0','0','0','0','0',@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10,@cVMIVenCode ";
  1417. if (newsql != "")
  1418. sql = sql.Replace("1=1", newsql);
  1419. else
  1420. sql = sql.Replace("1=1", "");
  1421. if (newsqlvalues != "")
  1422. sql = sql.Replace("2=2", newsqlvalues);
  1423. else
  1424. sql = sql.Replace("2=2", "");
  1425. cmd.CommandText = sql;
  1426. cmd.Parameters.Clear();
  1427. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1428. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1429. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1430. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  1431. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  1432. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  1433. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  1434. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  1435. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  1436. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  1437. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  1438. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  1439. cmd.Parameters.Add(new SqlParameter("@cFree9", cFree9));
  1440. cmd.Parameters.Add(new SqlParameter("@cFree10", cFree10));
  1441. cmd.Parameters.Add(new SqlParameter("@cVMIVenCode", cVenCode));
  1442. if (cBatch != null)
  1443. {
  1444. if (bInvBatch(cInvCode, cmd) == true)
  1445. cmd.Parameters.Add(new SqlParameter("@cBatch", cBatch));
  1446. else
  1447. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  1448. }
  1449. else
  1450. {
  1451. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  1452. }
  1453. CmdExecuteNonQuery(sql, cmd, "插入现存量失败!");
  1454. //log.Info("现存量插入:" + sql);
  1455. }
  1456. #endregion
  1457. #region 判断现存量是否足够
  1458. //sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0)
  1459. // BEGIN
  1460. // DECLARE @MSG NVARCHAR(100)
  1461. // SELECT @MSG='ERP库存不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0
  1462. // RAISERROR(@MSG,16,1)
  1463. // END";
  1464. //cmd.CommandText = sql;
  1465. //cmd.ExecuteNonQuery();
  1466. #endregion
  1467. //判断仓库是否记入成本
  1468. sql = "SELECT bInCost FROM Warehouse WHERE cWhCode='{0}'";
  1469. sql = string.Format(sql, cWhCode);
  1470. cmd.CommandText = sql;
  1471. DataTable dtwh = SQlReturnData(sql, cmd);
  1472. if (!dtwh.Rows[0]["bInCost"].ToString().Equals("0"))
  1473. {
  1474. #region 3 写入记账表
  1475. sql = string.Format(@" INSERT INTO [dbo].[" + key.TableName + @"]
  1476. SELECT '{0}','{1}','{2}','{3}'", key.ID, key.DID, key.cVouchTypeUN, key.cBustypeUN);
  1477. cmd.CommandText = sql;
  1478. CmdExecuteNonQuery(sql, cmd, "采购入库单写入记账表失败!");
  1479. #endregion
  1480. }
  1481. #region 判断现存量是否足够
  1482. sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fOutQuantity<0 and cWhCode = '{0}' AND cInvCode = '{1}')
  1483. BEGIN
  1484. DECLARE @MSG NVARCHAR(100)
  1485. SELECT @MSG='ERP待出库数量不足AutoID'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fOutQuantity<0
  1486. RAISERROR(@MSG,16,1)
  1487. END";
  1488. sql = string.Format(sql, cWhCode, cInvCode);
  1489. cmd.CommandText = sql;
  1490. cmd.ExecuteNonQuery();
  1491. #endregion
  1492. }
  1493. public static bool IsInventoryConsolidation(SqlCommand cmd,string WorkPoint)
  1494. {
  1495. string sql = @"SELECT cAcc_Id, iYear, cUser_Id, cAuth_Id, cStation, cTaskId, iLogId, cSub_id, cAppServer, cAuthClassCode, iBeginYear
  1496. FROM UFSystem.dbo.ua_Task_Common
  1497. where cAuth_Id in (
  1498. SELECT distinct cAuth_ID
  1499. FROM {0}.dbo.UA_Auth_lang
  1500. WHERE (cAuth_Name = N''))";
  1501. sql = string.Format(sql, WorkPoint);
  1502. DataTable dt = SQlReturnData(sql, cmd);
  1503. if (dt != null && dt.Rows.Count > 0)
  1504. {
  1505. return false;
  1506. }
  1507. else
  1508. {
  1509. return true;
  1510. }
  1511. }
  1512. /// <summary>
  1513. /// 判断是否启用批次管理
  1514. /// </summary>
  1515. /// <param name="cInvCode"></param>
  1516. /// <returns></returns>
  1517. public static bool bInvBatch(string cInvCode, SqlCommand cmd)
  1518. {
  1519. try
  1520. {
  1521. bool flag = false;
  1522. string sql = "SELECT bInvBatch FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1523. DataTable dt = SQlReturnData(sql, cmd);
  1524. if (dt != null && dt.Rows.Count > 0)
  1525. {
  1526. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1527. {
  1528. flag = true;
  1529. }
  1530. else
  1531. {
  1532. flag = false;
  1533. }
  1534. }
  1535. return flag;
  1536. }
  1537. catch (Exception ex)
  1538. {
  1539. throw new Exception(ex.Message);
  1540. }
  1541. }
  1542. /// <summary>
  1543. /// 判断是否启自由项管理
  1544. /// </summary>
  1545. /// <param name="cInvCode"></param>
  1546. /// <returns></returns>
  1547. public static bool bFree1(string cInvCode, SqlCommand cmd)
  1548. {
  1549. try
  1550. {
  1551. bool flag = false;
  1552. string sql = "SELECT bFree1 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1553. DataTable dt = SQlReturnData(sql, cmd);
  1554. if (dt != null && dt.Rows.Count > 0)
  1555. {
  1556. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1557. {
  1558. flag = true;
  1559. }
  1560. else
  1561. {
  1562. flag = false;
  1563. }
  1564. }
  1565. return flag;
  1566. }
  1567. catch (Exception ex)
  1568. {
  1569. throw new Exception(ex.Message);
  1570. }
  1571. }
  1572. public static bool bFree2(string cInvCode, SqlCommand cmd)
  1573. {
  1574. try
  1575. {
  1576. bool flag = false;
  1577. string sql = "SELECT bFree2 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1578. DataTable dt = SQlReturnData(sql, cmd);
  1579. if (dt != null && dt.Rows.Count > 0)
  1580. {
  1581. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1582. {
  1583. flag = true;
  1584. }
  1585. else
  1586. {
  1587. flag = false;
  1588. }
  1589. }
  1590. return flag;
  1591. }
  1592. catch (Exception ex)
  1593. {
  1594. throw new Exception(ex.Message);
  1595. }
  1596. }
  1597. public static bool bFree3(string cInvCode, SqlCommand cmd)
  1598. {
  1599. try
  1600. {
  1601. bool flag = false;
  1602. string sql = "SELECT bFree3 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1603. DataTable dt = SQlReturnData(sql, cmd);
  1604. if (dt != null && dt.Rows.Count > 0)
  1605. {
  1606. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1607. {
  1608. flag = true;
  1609. }
  1610. else
  1611. {
  1612. flag = false;
  1613. }
  1614. }
  1615. return flag;
  1616. }
  1617. catch (Exception ex)
  1618. {
  1619. throw new Exception(ex.Message);
  1620. }
  1621. }
  1622. public static bool bFree4(string cInvCode, SqlCommand cmd)
  1623. {
  1624. try
  1625. {
  1626. bool flag = false;
  1627. string sql = "SELECT bFree4 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1628. DataTable dt = SQlReturnData(sql, cmd);
  1629. if (dt != null && dt.Rows.Count > 0)
  1630. {
  1631. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1632. {
  1633. flag = true;
  1634. }
  1635. else
  1636. {
  1637. flag = false;
  1638. }
  1639. }
  1640. return flag;
  1641. }
  1642. catch (Exception ex)
  1643. {
  1644. throw new Exception(ex.Message);
  1645. }
  1646. }
  1647. public static bool bFree5(string cInvCode, SqlCommand cmd)
  1648. {
  1649. try
  1650. {
  1651. bool flag = false;
  1652. string sql = "SELECT bFree5 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1653. DataTable dt = SQlReturnData(sql, cmd);
  1654. if (dt != null && dt.Rows.Count > 0)
  1655. {
  1656. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1657. {
  1658. flag = true;
  1659. }
  1660. else
  1661. {
  1662. flag = false;
  1663. }
  1664. }
  1665. return flag;
  1666. }
  1667. catch (Exception ex)
  1668. {
  1669. throw new Exception(ex.Message);
  1670. }
  1671. }
  1672. public static bool bFree6(string cInvCode, SqlCommand cmd)
  1673. {
  1674. try
  1675. {
  1676. bool flag = false;
  1677. string sql = "SELECT bFree6 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1678. DataTable dt = SQlReturnData(sql, cmd);
  1679. if (dt != null && dt.Rows.Count > 0)
  1680. {
  1681. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1682. {
  1683. flag = true;
  1684. }
  1685. else
  1686. {
  1687. flag = false;
  1688. }
  1689. }
  1690. return flag;
  1691. }
  1692. catch (Exception ex)
  1693. {
  1694. throw new Exception(ex.Message);
  1695. }
  1696. }
  1697. public static bool bFree7(string cInvCode, SqlCommand cmd)
  1698. {
  1699. try
  1700. {
  1701. bool flag = false;
  1702. string sql = "SELECT bFree7 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1703. DataTable dt = SQlReturnData(sql, cmd);
  1704. if (dt != null && dt.Rows.Count > 0)
  1705. {
  1706. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1707. {
  1708. flag = true;
  1709. }
  1710. else
  1711. {
  1712. flag = false;
  1713. }
  1714. }
  1715. return flag;
  1716. }
  1717. catch (Exception ex)
  1718. {
  1719. throw new Exception(ex.Message);
  1720. }
  1721. }
  1722. public static bool bFree8(string cInvCode, SqlCommand cmd)
  1723. {
  1724. try
  1725. {
  1726. bool flag = false;
  1727. string sql = "SELECT bFree8 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1728. DataTable dt = SQlReturnData(sql, cmd);
  1729. if (dt != null && dt.Rows.Count > 0)
  1730. {
  1731. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1732. {
  1733. flag = true;
  1734. }
  1735. else
  1736. {
  1737. flag = false;
  1738. }
  1739. }
  1740. return flag;
  1741. }
  1742. catch (Exception ex)
  1743. {
  1744. throw new Exception(ex.Message);
  1745. }
  1746. }
  1747. public static DataTable bFree(string cInvCode, SqlCommand cmd)
  1748. {
  1749. try
  1750. {
  1751. string sql = "SELECT bFree1, bFree2, bFree3, bFree4, bFree5, bFree6, bFree7, bFree8, bFree9, bFree10 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1752. DataTable dt = SQlReturnData(sql, cmd);
  1753. return dt;
  1754. }
  1755. catch (Exception ex)
  1756. {
  1757. throw new Exception(ex.Message);
  1758. }
  1759. }
  1760. /// <summary>
  1761. /// 判断是否启用保质期管理
  1762. /// </summary>
  1763. /// <param name="cInvCode"></param>
  1764. /// <returns></returns>
  1765. public static bool bInvQuality(string cInvCode, SqlCommand cmd)
  1766. {
  1767. try
  1768. {
  1769. bool flag = false;
  1770. string sql = "SELECT bInvQuality FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  1771. DataTable dt = SQlReturnData(sql, cmd);
  1772. if (dt != null && dt.Rows.Count > 0)
  1773. {
  1774. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1775. {
  1776. flag = true;
  1777. }
  1778. else
  1779. {
  1780. flag = false;
  1781. }
  1782. }
  1783. return flag;
  1784. }
  1785. catch (Exception ex)
  1786. {
  1787. throw new Exception(ex.Message);
  1788. }
  1789. }
  1790. /// <summary>
  1791. /// 判断是否记账
  1792. /// </summary>
  1793. /// <param name="cInvCode"></param>
  1794. /// <returns></returns>
  1795. public static bool bInCost(string cWhCode, SqlCommand cmd)
  1796. {
  1797. try
  1798. {
  1799. bool flag = false;
  1800. string sql = "SELECT bInCost FROM Warehouse WHERE cWhCode ='" + cWhCode + "'";
  1801. DataTable dt = SQlReturnData(sql, cmd);
  1802. if (dt != null && dt.Rows.Count > 0)
  1803. {
  1804. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  1805. {
  1806. flag = true;
  1807. }
  1808. else
  1809. {
  1810. flag = false;
  1811. }
  1812. }
  1813. return flag;
  1814. }
  1815. catch (Exception ex)
  1816. {
  1817. throw new Exception(ex.Message);
  1818. }
  1819. }
  1820. /// <summary>
  1821. /// Convert a List{T} to a DataTable.
  1822. /// </summary>
  1823. public static DataTable ToDataTable<T>(List<T> items)
  1824. {
  1825. var tb = new DataTable(typeof(T).Name);
  1826. PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  1827. foreach (PropertyInfo prop in props)
  1828. {
  1829. Type t = GetCoreType(prop.PropertyType);
  1830. tb.Columns.Add(prop.Name, t);
  1831. }
  1832. foreach (T item in items)
  1833. {
  1834. var values = new object[props.Length];
  1835. for (int i = 0; i < props.Length; i++)
  1836. {
  1837. values[i] = props[i].GetValue(item, null);
  1838. }
  1839. tb.Rows.Add(values);
  1840. }
  1841. return tb;
  1842. }
  1843. /// <summary>
  1844. /// Determine of specified type is nullable
  1845. /// </summary>
  1846. public static bool IsNullable(Type t)
  1847. {
  1848. return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
  1849. }
  1850. /// <summary>
  1851. /// Return underlying type if type is Nullable otherwise return the type
  1852. /// </summary>
  1853. public static Type GetCoreType(Type t)
  1854. {
  1855. if (t != null && IsNullable(t))
  1856. {
  1857. if (!t.IsValueType)
  1858. {
  1859. return t;
  1860. }
  1861. else
  1862. {
  1863. return Nullable.GetUnderlyingType(t);
  1864. }
  1865. }
  1866. else
  1867. {
  1868. return t;
  1869. }
  1870. }
  1871. #region 原
  1872. 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)
  1873. {
  1874. DataTable dtNew = dt.Clone();
  1875. 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] };
  1876. foreach (DataRow row in dt.Rows)
  1877. {
  1878. if (string.IsNullOrEmpty(cBatch))
  1879. {
  1880. cBatch = "";
  1881. }
  1882. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  1883. {
  1884. row[cBatch] = "";
  1885. }
  1886. 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() });
  1887. if (srow == null)
  1888. {
  1889. dtNew.Rows.Add(row.ItemArray);
  1890. }
  1891. else
  1892. {
  1893. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  1894. }
  1895. }
  1896. return dtNew;
  1897. }
  1898. public static DataTable MergeDataTable(DataTable dt, string AuotID, string cBatch, string iQuantity, string cInvCode, SqlCommand cmd)
  1899. {
  1900. DataTable dtNew = dt.Clone();
  1901. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID], dtNew.Columns[cBatch] };
  1902. foreach (DataRow row in dt.Rows)
  1903. {
  1904. if (string.IsNullOrEmpty(cBatch))
  1905. {
  1906. cBatch = "";
  1907. }
  1908. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  1909. {
  1910. row[cBatch] = "";
  1911. }
  1912. DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString(), row[cBatch].ToString() });
  1913. if (srow == null)
  1914. {
  1915. dtNew.Rows.Add(row.ItemArray);
  1916. }
  1917. else
  1918. {
  1919. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  1920. }
  1921. }
  1922. return dtNew;
  1923. }
  1924. 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)
  1925. {
  1926. DataTable dtNew = dt.Clone();
  1927. 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] };
  1928. foreach (DataRow row in dt.Rows)
  1929. {
  1930. if (string.IsNullOrEmpty(cBatch))
  1931. {
  1932. cBatch = "";
  1933. }
  1934. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  1935. {
  1936. row[cBatch] = "";
  1937. }
  1938. 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() });
  1939. if (srow == null)
  1940. {
  1941. dtNew.Rows.Add(row.ItemArray);
  1942. }
  1943. else
  1944. {
  1945. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  1946. }
  1947. }
  1948. return dtNew;
  1949. }
  1950. public static DataTable MergeDataTableQC(DataTable dt, string AuotID, string cBatch, string iQuantity, string iNGQuantity, string cInvCode, SqlCommand cmd)
  1951. {
  1952. DataTable dtNew = dt.Clone();
  1953. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID], dtNew.Columns[cBatch] };
  1954. foreach (DataRow row in dt.Rows)
  1955. {
  1956. if (string.IsNullOrEmpty(cBatch))
  1957. {
  1958. cBatch = "";
  1959. }
  1960. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  1961. {
  1962. row[cBatch] = "";
  1963. }
  1964. DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString(), row[cBatch].ToString() });
  1965. if (srow == null)
  1966. {
  1967. dtNew.Rows.Add(row.ItemArray);
  1968. }
  1969. else
  1970. {
  1971. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  1972. srow[iNGQuantity] = decimal.Parse(srow[iNGQuantity].ToString()) + decimal.Parse(row[iNGQuantity].ToString());
  1973. }
  1974. }
  1975. return dtNew;
  1976. }
  1977. 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)
  1978. {
  1979. DataTable dtNew = dt.Clone();
  1980. 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] };
  1981. foreach (DataRow row in dt.Rows)
  1982. {
  1983. if (string.IsNullOrEmpty(cBatch))
  1984. {
  1985. cBatch = "";
  1986. }
  1987. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  1988. {
  1989. row[cBatch] = "";
  1990. }
  1991. 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() });
  1992. if (srow == null)
  1993. {
  1994. dtNew.Rows.Add(row.ItemArray);
  1995. }
  1996. else
  1997. {
  1998. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  1999. }
  2000. }
  2001. return dtNew;
  2002. }
  2003. 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)
  2004. {
  2005. DataTable dtNew = dt.Clone();
  2006. 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] };
  2007. foreach (DataRow row in dt.Rows)
  2008. {
  2009. if (string.IsNullOrEmpty(cBatch))
  2010. {
  2011. cBatch = "";
  2012. }
  2013. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  2014. {
  2015. row[cBatch] = "";
  2016. }
  2017. 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() });
  2018. if (srow == null)
  2019. {
  2020. dtNew.Rows.Add(row.ItemArray);
  2021. }
  2022. else
  2023. {
  2024. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  2025. }
  2026. }
  2027. return dtNew;
  2028. }
  2029. #endregion
  2030. #region 现
  2031. public static DataTable MergeDataTableX(DataTable dt, string AuotID, string cBatch, string iQuantity, string cInvCode, string iNum, SqlCommand cmd)
  2032. {
  2033. DataTable dtNew = dt.Clone();
  2034. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[cInvCode], dtNew.Columns[cBatch] };
  2035. foreach (DataRow row in dt.Rows)
  2036. {
  2037. if (string.IsNullOrEmpty(cBatch))
  2038. {
  2039. cBatch = "";
  2040. }
  2041. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  2042. {
  2043. row[cBatch] = "";
  2044. }
  2045. DataRow srow = dtNew.Rows.Find(new object[] { row[cInvCode].ToString(), row[cBatch].ToString() });
  2046. if (srow == null)
  2047. {
  2048. dtNew.Rows.Add(row.ItemArray);
  2049. }
  2050. else
  2051. {
  2052. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  2053. srow[iNum] = decimal.Parse(srow[iNum].ToString()) + decimal.Parse(row[iNum].ToString());
  2054. }
  2055. }
  2056. return dtNew;
  2057. }
  2058. public static DataTable MergeDataTable(DataTable dt, string AuotID, string cBatch, string iQuantity, string cInvCode, string iNum, SqlCommand cmd)
  2059. {
  2060. DataTable dtNew = dt.Clone();
  2061. //dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID], dtNew.Columns[cInvCode], dtNew.Columns[cBatch] };
  2062. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID], dtNew.Columns[cBatch] };//原
  2063. foreach (DataRow row in dt.Rows)
  2064. {
  2065. if (string.IsNullOrEmpty(cBatch))
  2066. {
  2067. cBatch = "";
  2068. }
  2069. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  2070. {
  2071. row[cBatch] = "";
  2072. }
  2073. DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString(), row[cBatch].ToString() });
  2074. //DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString(), dtNew.Columns[cInvCode], row[cBatch].ToString() });
  2075. if (srow == null)
  2076. {
  2077. dtNew.Rows.Add(row.ItemArray);
  2078. }
  2079. else
  2080. {
  2081. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  2082. srow[iNum] = decimal.Parse(srow[iNum].ToString()) + decimal.Parse(row[iNum].ToString());
  2083. }
  2084. }
  2085. return dtNew;
  2086. }
  2087. public static DataTable MergeRd09(DataTable dt, string cBatch, string iQuantity, string cInvCode, string iNum, SqlCommand cmd)
  2088. {
  2089. DataTable dtNew = dt.Clone();
  2090. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[cInvCode], dtNew.Columns[cBatch] };
  2091. foreach (DataRow row in dt.Rows)
  2092. {
  2093. if (string.IsNullOrEmpty(cBatch))
  2094. {
  2095. cBatch = "";
  2096. }
  2097. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  2098. {
  2099. row[cBatch] = "";
  2100. }
  2101. DataRow srow = dtNew.Rows.Find(new object[] { row[cInvCode].ToString(), row[cBatch].ToString() });
  2102. if (srow == null)
  2103. {
  2104. dtNew.Rows.Add(row.ItemArray);
  2105. }
  2106. else
  2107. {
  2108. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  2109. srow[iNum] = decimal.Parse(srow[iNum].ToString()) + decimal.Parse(row[iNum].ToString());
  2110. }
  2111. }
  2112. return dtNew;
  2113. }
  2114. #endregion
  2115. public static IList<T> ConvertTo<T>(DataTable table)
  2116. {
  2117. if (table == null)
  2118. {
  2119. return null;
  2120. }
  2121. List<DataRow> rows = new List<DataRow>();
  2122. foreach (DataRow row in table.Rows)
  2123. {
  2124. rows.Add(row);
  2125. }
  2126. return ConvertTo<T>(rows);
  2127. }
  2128. public static IList<T> ConvertTo<T>(IList<DataRow> rows)
  2129. {
  2130. IList<T> list = null;
  2131. if (rows != null)
  2132. {
  2133. list = new List<T>();
  2134. foreach (DataRow row in rows)
  2135. {
  2136. T item = CreateItem<T>(row);
  2137. list.Add(item);
  2138. }
  2139. }
  2140. return list;
  2141. }
  2142. public static T CreateItem<T>(DataRow row)
  2143. {
  2144. T obj = default(T);
  2145. if (row != null)
  2146. {
  2147. obj = Activator.CreateInstance<T>();
  2148. foreach (DataColumn column in row.Table.Columns)
  2149. {
  2150. PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
  2151. try
  2152. {
  2153. object value = row[column.ColumnName];
  2154. prop.SetValue(obj, value, null);
  2155. }
  2156. catch
  2157. { //You can log something here
  2158. //throw;
  2159. }
  2160. }
  2161. }
  2162. return obj;
  2163. }
  2164. public static DataTable MergeDataTable(DataTable dt, string AuotID, string iQuantity, string cBatch)
  2165. {
  2166. DataTable dtNew = dt.Clone();
  2167. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID] };
  2168. foreach (DataRow row in dt.Rows)
  2169. {
  2170. DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString() });
  2171. if (srow == null)
  2172. {
  2173. dtNew.Rows.Add(row.ItemArray);
  2174. }
  2175. else
  2176. {
  2177. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  2178. }
  2179. }
  2180. return dtNew;
  2181. }
  2182. #region 返回默认的出入库类别
  2183. /// <summary>
  2184. /// 返回默认的出入库类别
  2185. /// </summary>
  2186. /// <returns></returns>
  2187. public static string returnDefaultRdType(string VTID, string BTChName, SqlCommand cmd)
  2188. {
  2189. string sql = @"select VouchRdContrapose.cVRGUID,cVTChName,cBTChName,cVRRCode,R.cRdName,cVRSCode,S.cRdName
  2190. from VouchRdContrapose with(nolock)
  2191. left join vouchTypeDic with(nolock) on VouchRdContrapose.cVBTID=VouchTypeDic.cVBTID
  2192. left join Rd_Style as R with(nolock) On cVRRCode=R.cRdCode and R.bRDFlag=1
  2193. left join Rd_Style as S with(nolock) ON cVRSCode=S.cRdCode and S.bRDFlag=0
  2194. where 1=1 And (cVTID = N'{0}') And (cBTChName = N'{1}') order by cSerial ";
  2195. sql = string.Format(sql, VTID, BTChName);
  2196. DataTable dt = SQlReturnData(sql, cmd);
  2197. if (dt.Rows.Count == 0)
  2198. {
  2199. throw new Exception("倒冲材料出库单的出库类别取得失败");
  2200. }
  2201. return dt.Rows[0]["cVRSCode"].ToString();
  2202. }
  2203. #endregion
  2204. 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)
  2205. {
  2206. string sql = "";
  2207. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  2208. sql += " and cBatch='" + cBatch + "' ";
  2209. else
  2210. sql += " and cBatch='' ";
  2211. if (DBHelper.bFree1(cInvCode, cmd) == true)
  2212. sql += " and cFree1='" + cFree1 + "' ";
  2213. else
  2214. sql += " and cFree1='' ";
  2215. if (DBHelper.bFree2(cInvCode, cmd) == true)
  2216. sql += " and cFree2='" + cFree2 + "' ";
  2217. else
  2218. sql += " and cFree2='' ";
  2219. if (DBHelper.bFree3(cInvCode, cmd) == true)
  2220. sql += " and cFree3='" + cFree3 + "' ";
  2221. else
  2222. sql += " and cFree3='' ";
  2223. if (DBHelper.bFree4(cInvCode, cmd) == true)
  2224. sql += " and cFree4='" + cFree4 + "' ";
  2225. else
  2226. sql += " and cFree4='' ";
  2227. if (DBHelper.bFree5(cInvCode, cmd) == true)
  2228. sql += " and cFree5='" + cFree5 + "' ";
  2229. else
  2230. sql += " and cFree5='' ";
  2231. if (DBHelper.bFree6(cInvCode, cmd) == true)
  2232. sql += " and cFree6='" + cFree6 + "' ";
  2233. else
  2234. sql += " and cFree6='' ";
  2235. if (DBHelper.bFree7(cInvCode, cmd) == true)
  2236. sql += " and cFree7='" + cFree7 + "' ";
  2237. else
  2238. sql += " and cFree7='' ";
  2239. if (DBHelper.bFree8(cInvCode, cmd) == true)
  2240. sql += " and cFree8='" + cFree8 + "' ";
  2241. else
  2242. sql += " and cFree8='' ";
  2243. return sql;
  2244. }
  2245. 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)
  2246. {
  2247. bool bFree1 = false;
  2248. bool bFree2 = false;
  2249. bool bFree3 = false;
  2250. bool bFree4 = false;
  2251. bool bFree5 = false;
  2252. bool bFree6 = false;
  2253. bool bFree7 = false;
  2254. bool bFree8 = false;
  2255. #region 自由项管控
  2256. DataTable SubdtFree = DBHelper.bFree(cInvCode, cmd);
  2257. if (SubdtFree.Rows.Count > 0 && SubdtFree != null)
  2258. {
  2259. bFree1 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree1"]);
  2260. bFree2 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree2"]);
  2261. bFree3 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree3"]);
  2262. bFree4 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree4"]);
  2263. bFree5 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree5"]);
  2264. bFree6 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree6"]);
  2265. bFree7 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree7"]);
  2266. bFree8 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree8"]);
  2267. }
  2268. else
  2269. {
  2270. throw new Exception("存货编码:" + cInvCode + "不存在");
  2271. }
  2272. #endregion
  2273. string sql = "";
  2274. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  2275. sql += " and cBatch='" + cBatch + "' ";
  2276. else
  2277. sql += " and cBatch='' ";
  2278. if (bFree1)
  2279. {
  2280. sql += " and cFree1='" + cFree1 + "' ";
  2281. }
  2282. else
  2283. {
  2284. sql += " and cFree1='' ";
  2285. }
  2286. if (bFree2)
  2287. {
  2288. sql += " and cFree2='" + cFree2 + "' ";
  2289. }
  2290. else
  2291. {
  2292. sql += " and cFree2='' ";
  2293. }
  2294. if (bFree3)
  2295. {
  2296. sql += " and cFree3='" + cFree3 + "' ";
  2297. }
  2298. else
  2299. {
  2300. sql += " and cFree3='' ";
  2301. }
  2302. if (bFree4)
  2303. {
  2304. sql += " and cFree4='" + cFree4 + "' ";
  2305. }
  2306. else
  2307. {
  2308. sql += " and cFree4='' ";
  2309. }
  2310. if (bFree5)
  2311. {
  2312. sql += " and cFree5='" + cFree5 + "' ";
  2313. }
  2314. else
  2315. {
  2316. sql += " and cFree5='' ";
  2317. }
  2318. if (bFree6)
  2319. {
  2320. sql += " and cFree6='" + cFree6 + "' ";
  2321. }
  2322. else
  2323. {
  2324. sql += " and cFree6='' ";
  2325. }
  2326. if (bFree7)
  2327. {
  2328. sql += " and cFree7='" + cFree7 + "' ";
  2329. }
  2330. else
  2331. {
  2332. sql += " and cFree7='' ";
  2333. }
  2334. if (bFree8)
  2335. {
  2336. sql += " and cFree8='" + cFree8 + "' ";
  2337. }
  2338. else
  2339. {
  2340. sql += " and cFree8='' ";
  2341. }
  2342. return sql;
  2343. }
  2344. /// <summary>
  2345. /// 方法一:获取编号 返回Dictionary字典,调用该方法用Dictionary字典接收,只需要遍历Dictionary即可得到响应的值
  2346. /// </summary>
  2347. /// <param name="workPointCode"></param>
  2348. /// <param name="tbName"></param>
  2349. /// <param name="colName"></param>
  2350. /// <param name="Pre"></param>
  2351. /// <param name="numLen"></param>
  2352. /// <returns>返回Dictionary字典</returns>
  2353. public static Dictionary<string, int> GetAllCode(string cAcc_Id, string cVouchType, string iAmount)
  2354. {
  2355. string iFatherId = string.Empty, iChildId = string.Empty;
  2356. Dictionary<string, int> dic = new Dictionary<string, int>();
  2357. try
  2358. {
  2359. SqlConnection conn = new SqlConnection(ERPConnString);
  2360. SqlCommand cmd = new SqlCommand("sp_GetIDWithoutRemote", conn);
  2361. cmd.CommandType = CommandType.StoredProcedure;
  2362. cmd.Parameters.AddWithValue("@cAcc_Id", cAcc_Id);  //给输入参数赋值
  2363. cmd.Parameters.AddWithValue("@cVouchType", cVouchType);  //给输入参数赋值
  2364. cmd.Parameters.AddWithValue("@iAmount", iAmount);  //给输入参数赋值
  2365. //cmd.Parameters.AddWithValue("@iFatherId", iFatherId);  //给输入参数赋值
  2366. //cmd.Parameters.AddWithValue("@iChildId", iChildId);  //给输入参数赋值
  2367. SqlParameter parOutput = cmd.Parameters.Add("@iFatherId", SqlDbType.NVarChar, 50);  //定义输出参数
  2368. cmd.Parameters["@iFatherId"].Direction = ParameterDirection.Output;
  2369. SqlParameter parOutputs = cmd.Parameters.Add("@iChildId", SqlDbType.NVarChar, 50);  //定义输出参数
  2370. cmd.Parameters["@iChildId"].Direction = ParameterDirection.Output;
  2371. SqlParameter parReturn = new SqlParameter("@return", SqlDbType.Int);
  2372. parReturn.Direction = ParameterDirection.ReturnValue;   //参数类型为ReturnValue
  2373. cmd.Parameters.Add(parReturn);
  2374. conn.Open();
  2375. cmd.ExecuteNonQuery();
  2376. iFatherId = cmd.Parameters["@iFatherId"].Value.ToString();
  2377. iChildId = cmd.Parameters["@iChildId"].Value.ToString();
  2378. if (!string.IsNullOrWhiteSpace(iFatherId))//判断iFatherId是否为空,不为空的话将值放入dictionary字典中,否则int.Parse()会抛出异常
  2379. {
  2380. dic.Add("iFatherId", int.Parse(iFatherId));
  2381. dic.Add("iChildId", int.Parse(iChildId));
  2382. }
  2383. }
  2384. catch (System.Exception ex)
  2385. {
  2386. throw ex;
  2387. }
  2388. return dic;
  2389. }
  2390. public static Dictionary<string, int> GetAllCode(string cAcc_Id, string cVouchType, string iAmount,string WorkPoint)
  2391. {
  2392. string iFatherId = string.Empty, iChildId = string.Empty;
  2393. Dictionary<string, int> dic = new Dictionary<string, int>();
  2394. try
  2395. {
  2396. ERPConnString = string.Format(ERPConnString,WorkPoint);
  2397. SqlConnection conn = new SqlConnection(ERPConnString);
  2398. SqlCommand cmd = new SqlCommand("sp_GetIDWithoutRemote", conn);
  2399. cmd.CommandType = CommandType.StoredProcedure;
  2400. cmd.Parameters.AddWithValue("@cAcc_Id", cAcc_Id);  //给输入参数赋值
  2401. cmd.Parameters.AddWithValue("@cVouchType", cVouchType);  //给输入参数赋值
  2402. cmd.Parameters.AddWithValue("@iAmount", iAmount);  //给输入参数赋值
  2403. //cmd.Parameters.AddWithValue("@iFatherId", iFatherId);  //给输入参数赋值
  2404. //cmd.Parameters.AddWithValue("@iChildId", iChildId);  //给输入参数赋值
  2405. SqlParameter parOutput = cmd.Parameters.Add("@iFatherId", SqlDbType.NVarChar, 50);  //定义输出参数
  2406. cmd.Parameters["@iFatherId"].Direction = ParameterDirection.Output;
  2407. SqlParameter parOutputs = cmd.Parameters.Add("@iChildId", SqlDbType.NVarChar, 50);  //定义输出参数
  2408. cmd.Parameters["@iChildId"].Direction = ParameterDirection.Output;
  2409. SqlParameter parReturn = new SqlParameter("@return", SqlDbType.Int);
  2410. parReturn.Direction = ParameterDirection.ReturnValue;   //参数类型为ReturnValue
  2411. cmd.Parameters.Add(parReturn);
  2412. conn.Open();
  2413. cmd.ExecuteNonQuery();
  2414. iFatherId = cmd.Parameters["@iFatherId"].Value.ToString();
  2415. iChildId = cmd.Parameters["@iChildId"].Value.ToString();
  2416. if (!string.IsNullOrWhiteSpace(iFatherId))//判断iFatherId是否为空,不为空的话将值放入dictionary字典中,否则int.Parse()会抛出异常
  2417. {
  2418. dic.Add("iFatherId", int.Parse(iFatherId));
  2419. dic.Add("iChildId", int.Parse(iChildId));
  2420. }
  2421. }
  2422. catch (System.Exception ex)
  2423. {
  2424. throw ex;
  2425. }
  2426. return dic;
  2427. }
  2428. /// <summary>
  2429. /// 方法二 使用输出参数值
  2430. /// </summary>
  2431. /// <param name="cAcc_Id"></param>
  2432. /// <param name="cVouchType"></param>
  2433. /// <param name="iAmount"></param>
  2434. /// <returns></returns>
  2435. public static void GetAllCode(string cAcc_Id, string cVouchType, string iAmount, out int iFatherId, out int iChildId)
  2436. {
  2437. try
  2438. {
  2439. iFatherId = 0; iChildId = 0;
  2440. SqlConnection conn = new SqlConnection(ERPConnString);
  2441. SqlCommand cmd = new SqlCommand("sp_GetIDWithoutRemote", conn);
  2442. cmd.CommandType = CommandType.StoredProcedure;
  2443. cmd.Parameters.AddWithValue("@cAcc_Id", cAcc_Id);  //给输入参数赋值
  2444. cmd.Parameters.AddWithValue("@cVouchType", cVouchType);  //给输入参数赋值
  2445. cmd.Parameters.AddWithValue("@iAmount", iAmount);  //给输入参数赋值
  2446. //cmd.Parameters.AddWithValue("@iFatherId", iFatherId);  //给输入参数赋值
  2447. //cmd.Parameters.AddWithValue("@iChildId", iChildId);  //给输入参数赋值
  2448. SqlParameter parOutput = cmd.Parameters.Add("@iFatherId", SqlDbType.NVarChar, 50);  //定义输出参数
  2449. cmd.Parameters["@iFatherId"].Direction = ParameterDirection.Output;
  2450. SqlParameter parOutputs = cmd.Parameters.Add("@iChildId", SqlDbType.NVarChar, 50);  //定义输出参数
  2451. cmd.Parameters["@iChildId"].Direction = ParameterDirection.Output;
  2452. //SqlParameter parReturn = new SqlParameter("@return", SqlDbType.Int);
  2453. //parReturn.Direction = ParameterDirection.ReturnValue;   //参数类型为ReturnValue
  2454. //cmd.Parameters.Add(parReturn);
  2455. conn.Open();
  2456. cmd.ExecuteNonQuery();
  2457. string _iFatherId = cmd.Parameters["@iFatherId"].Value.ToString();
  2458. string _iChildId = cmd.Parameters["@iChildId"].Value.ToString();
  2459. if (!string.IsNullOrWhiteSpace(_iFatherId))
  2460. {
  2461. iFatherId = int.Parse(_iFatherId);
  2462. }
  2463. if (!string.IsNullOrWhiteSpace(_iChildId))
  2464. {
  2465. iChildId = int.Parse(_iChildId);
  2466. }
  2467. }
  2468. catch (System.Exception ex)
  2469. {
  2470. throw ex;
  2471. }
  2472. }
  2473. /// <summary>
  2474. /// 方法二 使用输出参数值
  2475. /// </summary>
  2476. /// <param name="cAcc_Id"></param>
  2477. /// <param name="cVouchType"></param>
  2478. /// <param name="iAmount"></param>
  2479. /// <returns></returns>
  2480. public static string GetAllRDCode(string CardNumber, string dDate, string UserCode)
  2481. {
  2482. string iBaseCodeLen = string.Empty; string cVouchCodeBase = string.Empty;
  2483. Dictionary<string, int> dic = new Dictionary<string, int>();
  2484. try
  2485. {
  2486. SqlConnection conn = new SqlConnection(ERPConnString);
  2487. SqlCommand cmd = new SqlCommand("ICS_VoucherNumber", conn);
  2488. cmd.CommandType = CommandType.StoredProcedure;
  2489. cmd.Parameters.AddWithValue("@CardNumber", CardNumber);  //给输入参数赋值
  2490. cmd.Parameters.AddWithValue("@dDate", dDate);  //给输入参数赋值
  2491. cmd.Parameters.AddWithValue("@UserCode", UserCode);  //给输入参数赋值
  2492. SqlParameter parOutput = cmd.Parameters.Add("@number", SqlDbType.NVarChar, 50);  //定义输出参数
  2493. parOutput.Direction = ParameterDirection.Output;
  2494. //SqlParameter parReturn = new SqlParameter("@return", SqlDbType.Int);
  2495. //parReturn.Direction = ParameterDirection.ReturnValue;   //参数类型为ReturnValue
  2496. //cmd.Parameters.Add(parReturn);
  2497. conn.Open();
  2498. cmd.ExecuteNonQuery();
  2499. return parOutput.Value.ToString();
  2500. //iBaseCodeLen = cmd.Parameters["@iBaseCodeLen"].Value.ToString();
  2501. // cVouchCodeBase = cmd.Parameters["@cVouchCodeBase"].Value.ToString();
  2502. //UserCode= cmd.Parameters["@cVouchCodePreFix"].Value.ToString();
  2503. ////if (!string.IsNullOrWhiteSpace(iBaseCodeLen))//判断iFatherId是否为空,不为空的话将值放入dictionary字典中,否则int.Parse()会抛出异常
  2504. ////{
  2505. //// dic.Add("iBaseCodeLen", int.Parse(iBaseCodeLen));
  2506. ////}
  2507. ////else
  2508. ////{
  2509. //// dic.Add("cVouchCodeBase", int.Parse(cVouchCodeBase));
  2510. // UserCode= UserCode + (cVouchCodeBase.ToString().PadLeft(int.Parse(iBaseCodeLen), '0'));
  2511. ////}
  2512. }
  2513. catch (System.Exception ex)
  2514. {
  2515. throw ex;
  2516. }
  2517. //return UserCode;
  2518. }
  2519. public static string GetAllRDCode(string CardNumber, string dDate, string UserCode, string WorkPoint)
  2520. {
  2521. string iBaseCodeLen = string.Empty; string cVouchCodeBase = string.Empty;
  2522. Dictionary<string, int> dic = new Dictionary<string, int>();
  2523. try
  2524. {
  2525. ERPConnString = string.Format(ERPConnString, WorkPoint);
  2526. SqlConnection conn = new SqlConnection(ERPConnString);
  2527. SqlCommand cmd = new SqlCommand("ICS_VoucherNumber", conn);
  2528. cmd.CommandType = CommandType.StoredProcedure;
  2529. cmd.Parameters.AddWithValue("@CardNumber", CardNumber);  //给输入参数赋值
  2530. cmd.Parameters.AddWithValue("@dDate", dDate);  //给输入参数赋值
  2531. cmd.Parameters.AddWithValue("@UserCode", UserCode);  //给输入参数赋值
  2532. SqlParameter parOutput = cmd.Parameters.Add("@number", SqlDbType.NVarChar, 50);  //定义输出参数
  2533. parOutput.Direction = ParameterDirection.Output;
  2534. //SqlParameter parReturn = new SqlParameter("@return", SqlDbType.Int);
  2535. //parReturn.Direction = ParameterDirection.ReturnValue;   //参数类型为ReturnValue
  2536. //cmd.Parameters.Add(parReturn);
  2537. conn.Open();
  2538. cmd.ExecuteNonQuery();
  2539. return parOutput.Value.ToString();
  2540. //iBaseCodeLen = cmd.Parameters["@iBaseCodeLen"].Value.ToString();
  2541. // cVouchCodeBase = cmd.Parameters["@cVouchCodeBase"].Value.ToString();
  2542. //UserCode= cmd.Parameters["@cVouchCodePreFix"].Value.ToString();
  2543. ////if (!string.IsNullOrWhiteSpace(iBaseCodeLen))//判断iFatherId是否为空,不为空的话将值放入dictionary字典中,否则int.Parse()会抛出异常
  2544. ////{
  2545. //// dic.Add("iBaseCodeLen", int.Parse(iBaseCodeLen));
  2546. ////}
  2547. ////else
  2548. ////{
  2549. //// dic.Add("cVouchCodeBase", int.Parse(cVouchCodeBase));
  2550. // UserCode= UserCode + (cVouchCodeBase.ToString().PadLeft(int.Parse(iBaseCodeLen), '0'));
  2551. ////}
  2552. }
  2553. catch (System.Exception ex)
  2554. {
  2555. throw ex;
  2556. }
  2557. //return UserCode;
  2558. }
  2559. public static string GetRDCode(string RDName,SqlCommand cmd)
  2560. {
  2561. string sql = @"select cRdCode from Rd_Style where cRdName='{0}'";
  2562. sql = string.Format(sql,RDName);
  2563. DataTable dt = SQlReturnData(sql,cmd);
  2564. return dt.Rows[0]["cRdCode"].ToString();
  2565. }
  2566. public static string GetCardNumber(string RDName, SqlCommand cmd)
  2567. {
  2568. string sql = @"select CardNumber from vouchernumber where CardName='{0}'";
  2569. sql = string.Format(sql, RDName);
  2570. DataTable dt = SQlReturnData(sql, cmd);
  2571. return dt.Rows[0]["CardNumber"].ToString();
  2572. }
  2573. /// <summary>
  2574. /// DataSet转Json字符串,主子结构
  2575. /// 需要建立主子表关系,第一张表为主表
  2576. /// 会排除关联列
  2577. /// </summary>
  2578. /// <param name="dataSet"></param>
  2579. /// <param name="RelationName">关系名称</param>
  2580. /// <returns></returns>
  2581. public static string DataSetToJson(DataSet dataSet, string RelationName)
  2582. {
  2583. StringBuilder jsonString = new StringBuilder();
  2584. //foreach (DataTable table in dataSet.Tables)
  2585. //{
  2586. DataTable table = dataSet.Tables[0];
  2587. jsonString.Append("[");
  2588. DataRowCollection drc = table.Rows;
  2589. for (int i = 0; i < drc.Count; i++)
  2590. {
  2591. DataRow dataRow = drc[i];
  2592. jsonString.Append("{");
  2593. for (int j = 0; j < table.Columns.Count; j++)
  2594. {
  2595. string strKey = table.Columns[j].ColumnName;
  2596. if (dataSet.Relations[RelationName].ParentColumns.Select(a => a.Caption).Contains(strKey))
  2597. continue;
  2598. string strValue = dataRow[j].ToString();
  2599. Type type = table.Columns[j].DataType;
  2600. jsonString.Append("\"" + strKey + "\":");
  2601. strValue = StringFormat(strValue, type);
  2602. if (j < table.Columns.Count - 1)
  2603. {
  2604. jsonString.Append(strValue + ",");
  2605. }
  2606. else
  2607. {
  2608. jsonString.Append(strValue);
  2609. }
  2610. }
  2611. jsonString.Append(",\"" + RelationName + "\":");
  2612. DataRow[] drs = dataRow.GetChildRows(RelationName);
  2613. jsonString.Append("[");
  2614. foreach (DataRow dr in drs)
  2615. {
  2616. DataTable dt = dr.Table;
  2617. jsonString.Append("{");
  2618. for (int j = 0; j < dt.Columns.Count; j++)
  2619. {
  2620. string strKey = dt.Columns[j].ColumnName;
  2621. if (dataSet.Relations[RelationName].ChildColumns.Select(a => a.Caption).Contains(strKey))
  2622. continue;
  2623. string strValue = dr[j].ToString();
  2624. Type type = dt.Columns[j].DataType;
  2625. jsonString.Append("\"" + strKey + "\":");
  2626. strValue = StringFormat(strValue, type);
  2627. if (j < dt.Columns.Count - 1)
  2628. {
  2629. jsonString.Append(strValue + ",");
  2630. }
  2631. else
  2632. {
  2633. jsonString.Append(strValue);
  2634. }
  2635. }
  2636. jsonString.Append("},");
  2637. }
  2638. if (drs.Length > 0)
  2639. jsonString.Remove(jsonString.Length - 1, 1);
  2640. jsonString.Append("]");
  2641. jsonString.Append("},");
  2642. }
  2643. jsonString.Remove(jsonString.Length - 1, 1);
  2644. jsonString.Append("]");
  2645. //}
  2646. string res = jsonString.ToString();
  2647. return res;
  2648. }
  2649. #region 私有方法
  2650. /// <summary>
  2651. /// 过滤特殊字符
  2652. /// </summary>
  2653. /// <param name="s">字符串</param>
  2654. /// <returns>json字符串</returns>
  2655. private static string String2Json(String s)
  2656. {
  2657. StringBuilder sb = new StringBuilder();
  2658. for (int i = 0; i < s.Length; i++)
  2659. {
  2660. char c = s.ToCharArray()[i];
  2661. switch (c)
  2662. {
  2663. case '\"':
  2664. sb.Append("\\\""); break;
  2665. case '\\':
  2666. sb.Append("\\\\"); break;
  2667. case '/':
  2668. sb.Append("\\/"); break;
  2669. case '\b':
  2670. sb.Append("\\b"); break;
  2671. case '\f':
  2672. sb.Append("\\f"); break;
  2673. case '\n':
  2674. sb.Append("\\n"); break;
  2675. case '\r':
  2676. sb.Append("\\r"); break;
  2677. case '\t':
  2678. sb.Append("\\t"); break;
  2679. default:
  2680. sb.Append(c); break;
  2681. }
  2682. }
  2683. return sb.ToString();
  2684. }
  2685. /// <summary>
  2686. /// 格式化字符型、日期型、布尔型
  2687. /// </summary>
  2688. /// <param name="str"></param>
  2689. /// <param name="type"></param>
  2690. /// <returns></returns>
  2691. private static string StringFormat(string str, Type type)
  2692. {
  2693. if (type == typeof(string))
  2694. {
  2695. str = String2Json(str);
  2696. str = "\"" + str + "\"";
  2697. }
  2698. else if (type == typeof(DateTime))
  2699. {
  2700. str = "\"" + str + "\"";
  2701. }
  2702. else if (type == typeof(bool))
  2703. {
  2704. str = str.ToLower();
  2705. }
  2706. else if (type != typeof(string) && string.IsNullOrEmpty(str))
  2707. {
  2708. str = "\"" + str + "\"";
  2709. }
  2710. return str;
  2711. }
  2712. #endregion
  2713. public static DataSet SQlReturnDataSet(string SQl, SqlCommand cmd)
  2714. {
  2715. DataSet ds = new DataSet();
  2716. SqlDataAdapter dr = new System.Data.SqlClient.SqlDataAdapter();
  2717. cmd.CommandText = SQl;
  2718. dr.SelectCommand = cmd;
  2719. dr.Fill(ds);
  2720. return ds;
  2721. }
  2722. /// <summary>
  2723. /// 获取用户的姓名与部门
  2724. /// </summary>
  2725. /// <param name="cPer_Num">用户编号</param>
  2726. /// <param name="cmd"></param>
  2727. /// <returns></returns>
  2728. public static ICSUserInfo GetPersonInfo(string cPer_Num, SqlCommand cmd)
  2729. {
  2730. ICSUserInfo person = new ICSUserInfo();
  2731. string sql = @" exec sp_refreshview ua_user ;
  2732. SELECT a.cUser_Id,a.cUser_Name,b.cDepCode FROM ua_user a
  2733. LEFT JOIN dbo.Department b ON a.cDept=b.cDepName
  2734. WHERE cUser_Id ='" + cPer_Num + "'";
  2735. cmd.CommandText = sql;
  2736. DataTable dt = SQlReturnData(sql, cmd);
  2737. if (dt != null && dt.Rows.Count > 0)
  2738. {
  2739. person.DepCode = dt.Rows[0]["cDepCode"].ToString();
  2740. person.UserName = dt.Rows[0]["cUser_Name"].ToString();
  2741. person.UserCode = cPer_Num;
  2742. }
  2743. else
  2744. throw new Exception("ERP人员不存在,编码:" + cPer_Num);
  2745. return person;
  2746. }
  2747. }
  2748. }