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.

4140 lines
172 KiB

3 weeks 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.IO;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace ICSSoft.Common
  14. {
  15. public class DBHelper
  16. {
  17. private static log4net.ILog log = log4net.LogManager.GetLogger("U8Helper");
  18. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ConnStr"];
  19. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  20. //// 访问数据库所需要的适配器
  21. //public static string connString = AppConfig.StrConnection;
  22. ///// <summary>
  23. ///// 根据查询语句获取一个DataTable,对异常捕获无指定要求
  24. ///// </summary>
  25. ///// <param name="select">查询语句</param>
  26. ///// <returns>所查询数据</returns>
  27. //public static DataTable GetDataTable(string select)
  28. //{
  29. // using (SqlConnection m_cnn = new SqlConnection(connString))
  30. // {
  31. // DataTable dt = new DataTable();
  32. // SqlDataAdapter m_da = new SqlDataAdapter(select, m_cnn);
  33. // m_da.SelectCommand.CommandTimeout = 600;
  34. // m_da.SelectCommand.CommandType = CommandType.Text;
  35. // m_da.SelectCommand.CommandText = select;
  36. // m_da.Fill(dt);
  37. // return dt;
  38. // }
  39. //}
  40. public static DataTable ConvertCellToString(DataTable data)
  41. {
  42. DataTable dtCloned = data.Clone();
  43. foreach (DataColumn col in dtCloned.Columns)
  44. {
  45. col.DataType = typeof(string);
  46. }
  47. foreach (DataRow row in data.Rows)
  48. {
  49. DataRow newrow = dtCloned.NewRow();
  50. foreach (DataColumn column in dtCloned.Columns)
  51. {
  52. newrow[column.ColumnName] = row[column.ColumnName].ToString();
  53. }
  54. dtCloned.Rows.Add(newrow);
  55. }
  56. return dtCloned;
  57. }
  58. /// <summary>
  59. /// 事物取DataTable
  60. /// </summary>
  61. /// <param name="SQl"></param>
  62. /// <param name="cmd"></param>
  63. /// <returns></returns>
  64. public static DataTable SQlReturnData(string SQl, SqlCommand cmd)
  65. {
  66. DataTable dt = new DataTable();
  67. SqlDataAdapter dr = new SqlDataAdapter();
  68. try
  69. {
  70. cmd.CommandText = SQl;
  71. dr.SelectCommand = cmd;
  72. dr.Fill(dt);
  73. return dt;
  74. }
  75. catch (SqlException ex)
  76. {
  77. throw new Exception(ex.Message);
  78. }
  79. finally
  80. {
  81. dr?.Dispose();
  82. }
  83. }
  84. public static DataTable SQlReturnData(string SQl)
  85. {
  86. DataTable dt = new DataTable();
  87. using (SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString))
  88. {
  89. try
  90. {
  91. conn.Open();
  92. SqlCommand cmd = new SqlCommand();
  93. cmd.Connection = conn;
  94. cmd.CommandTimeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["TimeOut"]);
  95. SqlDataAdapter dr = new System.Data.SqlClient.SqlDataAdapter();
  96. cmd.CommandText = SQl;
  97. dr.SelectCommand = cmd;
  98. dr.Fill(dt);
  99. }
  100. catch (Exception ex)
  101. {
  102. throw new Exception(ex.Message);
  103. }
  104. finally
  105. {
  106. if (conn.State == ConnectionState.Open)
  107. {
  108. conn.Close();
  109. }
  110. conn.Dispose();
  111. }
  112. }
  113. return dt;
  114. }
  115. public static bool IsU9()
  116. {
  117. return null==System.Configuration.ConfigurationManager.AppSettings["IsU9Api"]?false : Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["IsU9Api"]);
  118. }
  119. public static bool IsPNU9()
  120. {
  121. return null == System.Configuration.ConfigurationManager.AppSettings["IsPNU9Api"] ? false : Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["IsPNU9Api"]);
  122. }
  123. public static bool IsJHU9()
  124. {
  125. return null == System.Configuration.ConfigurationManager.AppSettings["IsJHU9Api"] ? false : Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["IsJHU9Api"]);
  126. }
  127. public static bool IsBBU9()
  128. {
  129. return null == System.Configuration.ConfigurationManager.AppSettings["IsBBU9Api"] ? false : Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["IsBBU9Api"]);
  130. }
  131. public static bool IsWZU9()
  132. {
  133. return null == System.Configuration.ConfigurationManager.AppSettings["IsWZApi"] ? false : Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["IsWZApi"]);
  134. }
  135. public static bool IsRTU9()
  136. {
  137. return null == System.Configuration.ConfigurationManager.AppSettings["IsRTU9Api"] ? false : Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["IsRTU9Api"]);
  138. }
  139. //达翔
  140. public static bool IsDX()
  141. {
  142. return null == System.Configuration.ConfigurationManager.AppSettings["IsDXApi"] ? false : Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["IsDXApi"]);
  143. }
  144. /// <summary>
  145. /// 获取自动出库每一批最大条数
  146. /// </summary>
  147. /// <returns></returns>
  148. public static int GetAutoBatchesNum()
  149. {
  150. return null == System.Configuration.ConfigurationManager.AppSettings["AutoBatchesNum"] ? 999 : Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["AutoBatchesNum"]);
  151. }
  152. public static DataSet SQlReturnDataSet(string SQl, SqlCommand cmd)
  153. {
  154. DataSet ds = new DataSet();
  155. SqlDataAdapter dr = new System.Data.SqlClient.SqlDataAdapter();
  156. cmd.CommandText = SQl;
  157. dr.SelectCommand = cmd;
  158. dr.Fill(ds);
  159. return ds;
  160. }
  161. public static object ExecuteScalar(string sql, SqlCommand cmd)
  162. {
  163. try
  164. {
  165. cmd.CommandText = sql;
  166. object val = cmd.ExecuteScalar();
  167. cmd.Parameters.Clear();
  168. return val;
  169. }
  170. catch (Exception ex)
  171. {
  172. //log.Error(ex.Message);
  173. throw;
  174. }
  175. }
  176. public static object ExecuteScalar(string sql)
  177. {
  178. DataTable dt = new DataTable();
  179. using (SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString))
  180. {
  181. try
  182. {
  183. conn.Open();
  184. SqlCommand cmd = new SqlCommand();
  185. cmd.Connection = conn;
  186. cmd.CommandTimeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["TimeOut"]);
  187. cmd.CommandText = sql;
  188. object val = cmd.ExecuteScalar();
  189. cmd.Parameters.Clear();
  190. return val;
  191. }
  192. catch (Exception ex)
  193. {
  194. //log.Error(ex.Message);
  195. throw;
  196. }
  197. finally
  198. {
  199. if (conn.State == ConnectionState.Open)
  200. {
  201. conn.Close();
  202. }
  203. conn.Dispose();
  204. }
  205. }
  206. return dt;
  207. }
  208. /// <summary>
  209. /// 读取打印数据源
  210. /// </summary>
  211. /// <returns></returns>
  212. public static string ReadPrintStream()
  213. {
  214. string filePath = System.AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["PrintDataUrl"];
  215. //String filePath = @"\\192.168.1.88\d\WMS基础版本文件\WMSAPI\\打印模板.txt";
  216. log.Debug("打印数据源路径:" + filePath);
  217. string strContent = string.Empty;
  218. FileStream fs = new FileStream(filePath, FileMode.Open);
  219. byte[] buffer = new byte[fs.Length];
  220. fs.Read(buffer, 0, buffer.Length);
  221. strContent = Encoding.UTF8.GetString(buffer);
  222. fs.Close();
  223. return strContent;
  224. }
  225. /// <summary>
  226. /// 依靠数据库连接字符串connectionString,
  227. /// 使用所提供参数,执行返回首行首列命令
  228. /// </summary>
  229. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  230. /// <param name="commandText">存储过程名称或者T-SQL命令行</param>
  231. /// <returns>返回一个对象,使用Convert.To{Type}将该对象转换成想要的数据类型。</returns>
  232. public static object ExecuteScalar(CommandType cmdType, string cmdText, string conn)
  233. {
  234. try
  235. {
  236. DbCommand cmd = CreateDbCommand();
  237. using (DbConnection connection = CreateDbConnection(conn))
  238. {
  239. PrepareCommand(cmd, connection, null, cmdType, cmdText, null);
  240. object val = cmd.ExecuteScalar();
  241. cmd.Parameters.Clear();
  242. return val;
  243. }
  244. }
  245. catch (Exception ex)
  246. {
  247. //log.Error(ex.Message);
  248. throw;
  249. }
  250. }
  251. /// <summary>
  252. /// 根据传入的日期,需要增加的天数返回一个字符串
  253. /// </summary>
  254. /// <param name="data"></param>
  255. /// <param name="str"></param>
  256. public static string ReTime(DateTime data, int str)
  257. {
  258. int year = data.Year;
  259. int month = data.Month;
  260. int day = data.Day;
  261. int n = DateTime.DaysInMonth(year, month);
  262. int k = day + str;
  263. if (k > n)
  264. {
  265. day = str - (n - day);
  266. month = month + 1;
  267. if (month > 12)
  268. {
  269. month = 1;
  270. year = year + 1;
  271. }
  272. }
  273. else
  274. {
  275. day = day + str;
  276. }
  277. string c = year + "-" + month + "-" + day;
  278. return c;
  279. }
  280. /// <summary>
  281. /// 依靠数据库连接字符串connectionString,
  282. /// 使用所提供参数,执行返回首行首列命令
  283. /// </summary>
  284. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  285. /// <param name="commandText">存储过程名称或者T-SQL命令行</param>
  286. /// <returns>返回一个对象,使用Convert.To{Type}将该对象转换成想要的数据类型。</returns>
  287. //public static object ExecuteScalar(CommandType cmdType, string cmdText ,string conn)
  288. //{
  289. // try
  290. // {
  291. // DbCommand cmd = CreateDbCommand();
  292. // using (DbConnection connection = CreateDbConnection(conn))
  293. // {
  294. // PrepareCommand(cmd, connection, null, cmdType, cmdText, null);
  295. // object val = cmd.ExecuteScalar();
  296. // cmd.Parameters.Clear();
  297. // return val;
  298. // }
  299. // }
  300. // catch (Exception ex)
  301. // {
  302. // //log.Error(ex.Message);
  303. // throw;
  304. // }
  305. //}
  306. //public static object ExecuteScalar(CommandType cmdType, string cmdText, string conn)
  307. //{
  308. // try
  309. // {
  310. // DbCommand cmd = CreateDbCommand();
  311. // using (DbConnection connection = CreateDbConnection(conn))
  312. // {
  313. // PrepareCommand(cmd, connection, null, cmdType, cmdText, null);
  314. // object val = cmd.ExecuteScalar();
  315. // cmd.Parameters.Clear();
  316. // return val;
  317. // }
  318. // }
  319. // catch (Exception ex)
  320. // {
  321. // //log.Error(ex.Message);
  322. // throw;
  323. // }
  324. //}
  325. /// <summary>
  326. /// 为即将执行准备一个命令
  327. /// </summary>
  328. /// <param name="cmd">SqlCommand对象</param>
  329. /// <param name="conn">SqlConnection对象</param>
  330. /// <param name="isOpenTrans">DbTransaction对象</param>
  331. /// <param name="cmdType">执行命令的类型(存储过程或T-SQL,等等)</param>
  332. /// <param name="cmdText">存储过程名称或者T-SQL命令行, e.g. Select * from Products</param>
  333. /// <param name="cmdParms">SqlParameters to use in the command</param>
  334. private static void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction isOpenTrans, CommandType cmdType, string cmdText, DbParameter[] cmdParms)
  335. {
  336. if (conn.State != ConnectionState.Open)
  337. conn.Open();
  338. cmd.Connection = conn;
  339. cmd.CommandText = cmdText;
  340. if (isOpenTrans != null)
  341. cmd.Transaction = isOpenTrans;
  342. cmd.CommandType = cmdType;
  343. if (cmdParms != null)
  344. {
  345. cmd.Parameters.AddRange(cmdParms);
  346. }
  347. }
  348. /// <summary>
  349. /// 数据库类型
  350. /// </summary>
  351. public static DatabaseType DbType { get; set; }
  352. /// <summary>
  353. /// 根据配置文件中所配置的数据库类型和传入的
  354. /// 数据库链接字符串来创建相应数据库连接对象
  355. /// </summary>
  356. /// <param name="connectionString"></param>
  357. /// <returns></returns>
  358. public static DbConnection CreateDbConnection(string connectionString)
  359. {
  360. DbConnection conn = null;
  361. switch (DbType)
  362. {
  363. case DatabaseType.SqlServer:
  364. conn = new SqlConnection(connectionString);
  365. break;
  366. //case DatabaseType.Oracle:
  367. // conn = new OracleConnection(connectionString);
  368. // break;
  369. //case DatabaseType.MySql:
  370. // conn = new MySqlConnection(connectionString);
  371. // break;
  372. //case DatabaseType.Access:
  373. // conn = new OleDbConnection(connectionString);
  374. // break;
  375. //case DatabaseType.SQLite:
  376. // conn = new SQLiteConnection(connectionString);
  377. // break;
  378. default:
  379. throw new Exception("数据库类型目前不支持!");
  380. }
  381. return conn;
  382. }
  383. /// <summary>
  384. /// 读取打印模板
  385. /// </summary>
  386. /// <returns></returns>
  387. public static string ReadFileStream()
  388. {
  389. string filePath = System.AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["PrintUrl"];
  390. //String filePath = @"\\192.168.1.88\d\WMS基础版本文件\WMSAPI\\打印模板.txt";
  391. log.Debug("打印模板路径:" + filePath);
  392. string strContent = string.Empty;
  393. FileStream fs = new FileStream(filePath, FileMode.Open);
  394. byte[] buffer = new byte[fs.Length];
  395. fs.Read(buffer, 0, buffer.Length);
  396. strContent = Encoding.UTF8.GetString(buffer);
  397. fs.Close();
  398. return strContent;
  399. }
  400. /// <summary>
  401. /// 根据配置文件中所配置的数据库类型
  402. /// 来创建相应数据库命令对象
  403. /// </summary>
  404. /// <returns></returns>
  405. public static DbCommand CreateDbCommand()
  406. {
  407. DbCommand cmd = null;
  408. switch (DbType)
  409. {
  410. case DatabaseType.SqlServer:
  411. cmd = new SqlCommand();
  412. break;
  413. //case DatabaseType.Oracle:
  414. // cmd = new OracleCommand();
  415. // break;
  416. //case DatabaseType.MySql:
  417. // cmd = new MySqlCommand();
  418. // break;
  419. //case DatabaseType.Access:
  420. // cmd = new OleDbCommand();
  421. // break;
  422. //case DatabaseType.SQLite:
  423. // cmd = new SQLiteCommand();
  424. // break;
  425. default:
  426. throw new Exception("数据库类型目前不支持!");
  427. }
  428. return cmd;
  429. }
  430. public enum DatabaseType
  431. {
  432. /// <summary>
  433. /// 数据库类型:Oracle
  434. /// </summary>
  435. Oracle,
  436. /// <summary>
  437. /// 数据库类型:SqlServer
  438. /// </summary>
  439. SqlServer,
  440. /// <summary>
  441. /// 数据库类型:Access
  442. /// </summary>
  443. Access,
  444. /// <summary>
  445. /// 数据库类型:MySql
  446. /// </summary>
  447. MySql,
  448. /// <summary>
  449. /// 数据库类型:SQLite
  450. /// </summary>
  451. SQLite
  452. }
  453. public static bool ExecuteNonQuery(string sql, SqlCommand cmd)
  454. {
  455. try
  456. {
  457. cmd.CommandText = sql;
  458. int result = cmd.ExecuteNonQuery();
  459. if (result > 0)
  460. {
  461. return true;
  462. }
  463. else
  464. {
  465. log.Info("SQL执行受影响行数<0;" + sql);
  466. return false;
  467. }
  468. }
  469. catch (Exception ex)
  470. {
  471. string Params = string.Empty;
  472. foreach (SqlParameter parameter in cmd.Parameters)
  473. {
  474. Params += parameter.SqlValue + "||";
  475. }
  476. log.Error("异常:" + ex.Message + ";\r\n SQL:" + sql + "参数:" + Params);
  477. throw new Exception(ex.Message);
  478. }
  479. }
  480. /// <summary>
  481. /// 获取插入单据的表头表体最大ID
  482. /// </summary>
  483. /// <param name="IDtype"></param>
  484. /// <param name="cAcc_id"></param>
  485. /// <param name="rowCount"></param>
  486. /// <param name="id"></param>
  487. /// <param name="did"></param>
  488. public static void SaveGetrdIDandDID(string IDtype, string cAcc_id, int rowCount, out int id, out int did, SqlCommand cmd)
  489. {
  490. cmd.Parameters.Clear();
  491. try
  492. {
  493. string[] ss = cAcc_id.Split('_');
  494. string ErpCount = ss[1];
  495. string str = @"DECLARE @ID int
  496. DECLARE @DID int
  497. SET @ID = 0
  498. SET @DID = 0
  499. IF NOT EXISTS (SELECT * FROM UFSystem..ua_identity WHERE cacc_id = '{0}' AND cVouchType = '{1}')
  500. BEGIN
  501. INSERT INTO UFSystem..ua_identity(cAcc_Id,cVouchType,iFatherId,iChildId) VALUES('{0}','{1}',1,1)
  502. END
  503. ELSE
  504. BEGIN
  505. UPDATE UFSystem..ua_identity
  506. SET ifatherID = ifatherID +1,ichildID = ichildID + {2}
  507. WHERE cVouchType = '{1}' AND cAcc_id = '{0}'
  508. END
  509. select ifatherID as ID,ichildID as DID FROM UFSystem..ua_identity WHERE cVouchType = '{1}' AND cAcc_id = '{0}' ";
  510. str = string.Format(str, ErpCount, IDtype, rowCount.ToString());
  511. DataTable dt = SQlReturnData(str, cmd);
  512. if (dt.Rows.Count == 0)
  513. {
  514. throw new Exception("ID取得失败");
  515. }
  516. id = Convert.ToInt32(dt.Rows[0]["ID"]);
  517. did = Convert.ToInt32(dt.Rows[0]["DID"]);
  518. #region 测试时屏蔽
  519. // if (IDtype == "rd")
  520. // {
  521. // 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 (
  522. // SELECT MAX(ID) AS ID,MAX(AutoID) AS DID FROM rdrecords01
  523. // UNION
  524. // SELECT MAX(ID) AS ID,MAX(AutoID) AS DID FROM rdrecords08
  525. // UNION
  526. // SELECT MAX(ID) AS ID,MAX(AutoID) AS DID FROM rdrecords09
  527. // UNION
  528. // SELECT MAX(ID) AS ID,MAX(AutoID) AS DID FROM rdrecords10
  529. // UNION
  530. // SELECT MAX(ID) AS ID,MAX(AutoID) AS DID FROM rdrecords11
  531. // UNION
  532. // SELECT MAX(ID) AS ID,MAX(AutoID) AS DID FROM rdrecords32
  533. // ) a";
  534. // DataTable dtCheck = SQlReturnData(sql, cmd);
  535. // if (dtCheck != null && dtCheck.Rows.Count > 0)
  536. // {
  537. // if (id <= Convert.ToInt32(dtCheck.Rows[0]["ID"].ToString()) || did <= Convert.ToInt32(dtCheck.Rows[0]["DID"].ToString()))
  538. // {
  539. // id = Convert.ToInt32(dtCheck.Rows[0]["ID"].ToString()) + 1;
  540. // did = Convert.ToInt32(dtCheck.Rows[0]["DID"].ToString()) + 1;
  541. // sql = string.Format(@" UPDATE UFSystem..ua_identity
  542. // SET ifatherID = {0}, ichildID = {1}
  543. // WHERE cVouchType = '{2}' AND cAcc_id = '{3}' ", id, did, IDtype, ErpCount);
  544. // cmd.CommandText = sql;
  545. // int i = cmd.ExecuteNonQuery();
  546. // }
  547. // }
  548. // }
  549. #endregion
  550. }
  551. catch (Exception ex)
  552. {
  553. throw new Exception(ex.Message);
  554. }
  555. }
  556. public static void SaveGetrdIDandDIDs(string IDtype, string cAcc_id, int rowCount, out int id, out int did, SqlCommand cmd)
  557. {
  558. cmd.Parameters.Clear();
  559. try
  560. {
  561. string[] ss = cAcc_id.Split('_');
  562. string ErpCount = ss[1];
  563. string str = @"DECLARE @ID int
  564. DECLARE @DID int
  565. SET @ID = 0
  566. SET @DID = 0
  567. IF NOT EXISTS (SELECT * FROM UFSystem..ua_identity WHERE cacc_id = '{0}' AND cVouchType = '{1}')
  568. BEGIN
  569. INSERT INTO UFSystem..ua_identity(cAcc_Id,cVouchType,iFatherId,iChildId) VALUES('{0}','{1}',1,1)
  570. END
  571. ELSE
  572. BEGIN
  573. UPDATE UFSystem..ua_identity SET ichildID = ichildID + {2}
  574. WHERE cVouchType = '{1}' AND cAcc_id = '{0}'
  575. END
  576. select ifatherID as ID,ichildID as DID FROM UFSystem..ua_identity WHERE cVouchType = '{1}' AND cAcc_id = '{0}' ";
  577. str = string.Format(str, ErpCount, IDtype, rowCount.ToString());
  578. DataTable dt = SQlReturnData(str, cmd);
  579. if (dt.Rows.Count == 0)
  580. {
  581. throw new Exception("ID取得失败");
  582. }
  583. id = Convert.ToInt32(dt.Rows[0]["ID"]);
  584. did = Convert.ToInt32(dt.Rows[0]["DID"]);
  585. }
  586. catch (Exception ex)
  587. {
  588. throw new Exception(ex.Message);
  589. }
  590. }
  591. /// <summary>
  592. /// 更新主键ID
  593. /// </summary>
  594. /// <param name="IDtype"></param>
  595. /// <param name="cAcc_id"></param>
  596. /// <param name="rowCount"></param>
  597. public static void UpdateIDandDID(string IDtype, string cAcc_id, int rowCount, SqlCommand cmd)
  598. {
  599. try
  600. {
  601. string[] ss = cAcc_id.Split('_');
  602. string ErpCount = ss[1];
  603. string sql = @" UPDATE UFSystem..ua_identity
  604. SET ifatherID = ifatherID +1,ichildID = ichildID + {2}
  605. WHERE cVouchType = '{1}' AND cAcc_id = '{0}' ";
  606. sql = string.Format(sql, ErpCount, IDtype, rowCount.ToString());
  607. cmd.CommandText = sql;
  608. int i = cmd.ExecuteNonQuery();
  609. if (i <= 0)
  610. {
  611. throw new Exception("更新主键ID,DID失败!");
  612. }
  613. }
  614. catch (Exception ex)
  615. {
  616. throw new Exception(ex.Message);
  617. }
  618. }
  619. //public static int ExecuteSql(string sqlUpdate)
  620. //{
  621. // using (SqlConnection conn = new SqlConnection(connString))
  622. // {
  623. // try
  624. // {
  625. // SqlCommand cmd = new SqlCommand();
  626. // cmd.CommandText = sqlUpdate;
  627. // if (conn.State != ConnectionState.Open)
  628. // {
  629. // conn.Open();
  630. // }
  631. // cmd.Connection = conn;
  632. // return cmd.ExecuteNonQuery();
  633. // }
  634. // catch (Exception ex)
  635. // {
  636. // throw new Exception(ex.Message);
  637. // }
  638. // finally
  639. // {
  640. // if (conn.State != ConnectionState.Closed)
  641. // {
  642. // conn.Close();
  643. // }
  644. // }
  645. // }
  646. //}
  647. /// <summary>
  648. /// 执行Insert 或者 Update
  649. /// </summary>
  650. /// <param name="sql"></param>
  651. /// <param name="cmd"></param>
  652. /// <param name="message"></param>
  653. public static void CmdExecuteNonQuery(string sql, SqlCommand cmd, string message)
  654. {
  655. try
  656. {
  657. cmd.CommandText = sql;
  658. int count = cmd.ExecuteNonQuery();
  659. if (count <= 0)
  660. {
  661. string Msg = string.Empty;
  662. foreach (SqlParameter parameter in cmd.Parameters)
  663. {
  664. Msg += "参数名:" + parameter.ParameterName + "参数值:" + parameter.Value;
  665. }
  666. log.Info("受影响行数小于0;" + sql + "\r\n" + Msg);
  667. throw new Exception(message);
  668. }
  669. }
  670. catch (Exception ex)
  671. {
  672. string Msg = string.Empty;
  673. foreach (SqlParameter parameter in cmd.Parameters)
  674. {
  675. Msg += "参数名:" + parameter.ParameterName + "参数值:" + parameter.Value;
  676. }
  677. log.Info("异常:" + ex.Message + "\r\n " + message + "\r\n SQL:" + sql + "\r\n" + Msg);
  678. throw new Exception(message + Environment.NewLine + ex.Message);
  679. }
  680. }
  681. /// <summary>
  682. /// 1 取得单据的默认模板
  683. /// </summary>
  684. /// <param name="ErpName">账套名</param>
  685. /// <param name="CardNumber"></param>
  686. /// <param name="cmd"></param>
  687. /// <returns></returns>
  688. public static string GetDefaultTemplate(string ErpName, string CardNumber, SqlCommand cmd)
  689. {
  690. try
  691. {
  692. string sql = "";
  693. string VouchDEF_ID = string.Empty;
  694. sql = string.Format("SELECT DEF_ID FROM {0}.dbo.Vouchers WHERE CardNumber = '{1}' ", ErpName, CardNumber);
  695. cmd.CommandText = sql;
  696. DataTable dtDEF_ID = SQlReturnData(sql, cmd);
  697. if (dtDEF_ID != null && dtDEF_ID.Rows.Count > 0)
  698. {
  699. VouchDEF_ID = dtDEF_ID.Rows[0]["DEF_ID"].ToString();
  700. }
  701. else
  702. {
  703. throw new Exception("获取默认显示模板失败!" + sql);
  704. }
  705. return VouchDEF_ID;
  706. }
  707. catch (Exception ex)
  708. {
  709. throw new Exception(ex.Message);
  710. }
  711. }
  712. /// <summary>
  713. /// 2 取得单据的表头ID,表体DID
  714. /// </summary>
  715. /// <param name="VouchType">单据类型 如“rd”</param>
  716. /// <param name="ErpName">账套名</param>
  717. /// <param name="RowCount">表体行</param>
  718. /// <returns></returns>
  719. public static VouchKey GetPrimaryKey(string VouchType, string ErpName, int RowCount, SqlCommand cmd)
  720. {
  721. try
  722. {
  723. string num = "1000000000";
  724. int id = 0;
  725. int did = 0;
  726. VouchKey model = new VouchKey();
  727. SaveGetrdIDandDID(VouchType, ErpName, RowCount, out id, out did, cmd);
  728. model.ID = int.Parse(num.Substring(0, 10 - (id.ToString().Length)) + id.ToString());
  729. model.DID = int.Parse(num.Substring(0, 10 - (did.ToString().Length)) + did.ToString());
  730. return model;
  731. }
  732. catch (Exception ex)
  733. {
  734. throw new Exception(ex.Message);
  735. }
  736. }
  737. public static VouchKey GetPrimaryKeys(string VouchType, string ErpName, int RowCount, SqlCommand cmd)
  738. {
  739. try
  740. {
  741. string num = "1000000000";
  742. int id = 0;
  743. int did = 0;
  744. VouchKey model = new VouchKey();
  745. SaveGetrdIDandDIDs(VouchType, ErpName, RowCount, out id, out did, cmd);
  746. model.DID = int.Parse(num.Substring(0, 10 - (did.ToString().Length)) + did.ToString());
  747. return model;
  748. }
  749. catch (Exception ex)
  750. {
  751. throw new Exception(ex.Message);
  752. }
  753. }
  754. /// <summary>
  755. /// 3 取得单据编号
  756. /// </summary>
  757. /// <param name="ErpName">账套名</param>
  758. /// <param name="CardNumber"></param>
  759. /// <param name="PreStr">单据前缀</param>
  760. /// <param name="cmd"></param>
  761. /// <returns></returns>
  762. public static int GetVouchCode(string ErpName, string CardNumber, string PreStr, string TableName, SqlCommand cmd)
  763. {
  764. string sql = "";
  765. sql = string.Format("select * from {0}.dbo.VoucherHistory where cSeed = Substring(Convert( varchar(100),GetDate(),112),3,4) and CardNumber = '{1}'", ErpName, CardNumber);
  766. DataTable dt = SQlReturnData(sql, cmd);
  767. if (dt != null && dt.Rows.Count > 0)
  768. {
  769. sql = string.Format(@"UPDATE {0}.dbo.VoucherHistory SET cNumber =cast(( cast(cNumber as int)+1) as nvarchar(30))
  770. WHERE cSeed = SUBSTRING(CONVERT(varchar(100), GETDATE(), 112),3,4) AND CardNumber = '{1}'", ErpName, CardNumber);
  771. CmdExecuteNonQuery(sql, cmd, "更新VoucherHistory表失败!");
  772. }
  773. else
  774. {
  775. sql = string.Format(@"INSERT INTO {0}.dbo.VoucherHistory
  776. SELECT '{1}',NULL,'','',SUBSTRING(CONVERT(varchar(100), GETDATE(), 112),3,4),1,0", ErpName, CardNumber);
  777. CmdExecuteNonQuery(sql, cmd, "插入VoucherHistory表失败!");
  778. }
  779. sql = string.Format(@"DECLARE @Code nvarchar(100)
  780. SELECT @Code = '00000' + CAST(cNumber AS NVARCHAR(50)) FROM {0}.dbo.VoucherHistory
  781. WHERE cSeed = SUBSTRING(CONVERT(varchar(100), GETDATE(), 112),3,4) AND CardNumber = '{1}'
  782. SET @Code = SUBSTRING(CONVERT(varchar(100), GETDATE(), 112),3,4)+RIGHT(@Code,4)
  783. select @Code", ErpName, CardNumber);
  784. int cCode = 0;
  785. cmd.CommandText = sql;
  786. DataTable dtCode = SQlReturnData(sql, cmd);
  787. if (dtCode != null && dtCode.Rows.Count > 0)
  788. {
  789. cCode = int.Parse(dtCode.Rows[0][0].ToString());
  790. }
  791. else
  792. {
  793. throw new Exception("获取单据号失败!");
  794. }
  795. #region
  796. //sql = string.Format("select cCode from " + TableName + " where cCode='{0}'", (PreStr + cCode).ToString());
  797. //cmd.CommandText = sql;
  798. //DataTable dtCodeCheck = SQlReturnData(sql, cmd);
  799. //if (dtCodeCheck != null && dtCodeCheck.Rows.Count > 0)
  800. //{
  801. // throw new Exception("获取单据号重复,保存失败!");
  802. //}
  803. #endregion
  804. return cCode;
  805. }
  806. public static int GetVouchCode(string ErpName, string CardNumber, string TableName, SqlCommand cmd)
  807. {
  808. string sql = "";
  809. sql = string.Format("select * from {0}.dbo.VoucherHistory where CardNumber = '{1}'", ErpName, CardNumber);
  810. DataTable dt = SQlReturnData(sql, cmd);
  811. if (dt != null && dt.Rows.Count > 0)
  812. {
  813. sql = string.Format(@"UPDATE {0}.dbo.VoucherHistory SET cNumber = cNumber+1
  814. WHERE CardNumber = '{1}'", ErpName, CardNumber);
  815. CmdExecuteNonQuery(sql, cmd, "更新VoucherHistory表失败!");
  816. }
  817. else
  818. {
  819. sql = string.Format(@"INSERT INTO {0}.dbo.VoucherHistory
  820. SELECT '{1}',NULL,'','',null,1,0", ErpName, CardNumber);
  821. CmdExecuteNonQuery(sql, cmd, "插入VoucherHistory表失败!");
  822. }
  823. sql = string.Format(@"DECLARE @Code nvarchar(100)
  824. SELECT @Code = CAST(cNumber AS NVARCHAR(50)) FROM {0}.dbo.VoucherHistory
  825. WHERE CardNumber = '{1}'
  826. SET @Code = RIGHT(@Code,10)
  827. select @Code", ErpName, CardNumber);
  828. int cCode = 0;
  829. cmd.CommandText = sql;
  830. DataTable dtCode = SQlReturnData(sql, cmd);
  831. if (dtCode != null && dtCode.Rows.Count > 0)
  832. {
  833. cCode = int.Parse(dtCode.Rows[0][0].ToString());
  834. }
  835. else
  836. {
  837. throw new Exception("获取单据号失败!");
  838. }
  839. return cCode;
  840. }
  841. public static string GetPreVouchCode(string ErpName, string CardNumber, string PreStr, SqlCommand cmd)
  842. {
  843. string cCode = "";
  844. string sql = string.Format("SELECT * FROM {0}.dbo.VoucherHistory WHERE CardNumber = '{1}' ", ErpName, CardNumber);
  845. DataTable dt = SQlReturnData(sql, cmd);
  846. if (dt != null && dt.Rows.Count > 0)
  847. {
  848. sql = string.Format(@"UPDATE {0}.dbo.VoucherHistory SET cNumber = CAST((CAST(cNumber AS INT) + 1) AS VARCHAR(30)) WHERE CardNumber = '{1}' ", ErpName, CardNumber);
  849. CmdExecuteNonQuery(sql, cmd, "更新VoucherHistory表失败!");
  850. }
  851. else
  852. {
  853. sql = string.Format(@"INSERT INTO {0}.dbo.VoucherHistory SELECT '{1}', NULL, NULL, NULL, NULL, 1, 0 ", ErpName, CardNumber);
  854. CmdExecuteNonQuery(sql, cmd, "插入VoucherHistory表失败!");
  855. }
  856. sql = string.Format(@"
  857. DECLARE @Code NVARCHAR(100)
  858. SELECT @Code = '0000000' + CAST(cNumber AS NVARCHAR(50)) FROM {0}.dbo.VoucherHistory WHERE CardNumber = '{1}'
  859. SET @Code = '{2}' + RIGHT(@Code, 3)
  860. SELECT @Code ", ErpName, CardNumber, PreStr);
  861. cmd.CommandText = sql;
  862. DataTable dtCode = SQlReturnData(sql, cmd);
  863. if (dtCode != null && dtCode.Rows.Count > 0)
  864. {
  865. cCode = dtCode.Rows[0][0].ToString();
  866. }
  867. else
  868. {
  869. throw new Exception("获取单据号失败!");
  870. }
  871. return cCode;
  872. }
  873. public static string GetRdVouchCode(string ErpName, string CardNumber, SqlCommand cmd)
  874. {
  875. string cCode = "";
  876. string sql = string.Format("SELECT * FROM {0}.dbo.VoucherHistory WHERE CardNumber = '{1}' ", ErpName, CardNumber);
  877. DataTable dt = SQlReturnData(sql, cmd);
  878. if (dt != null && dt.Rows.Count > 0)
  879. {
  880. sql = string.Format(@"UPDATE {0}.dbo.VoucherHistory SET cNumber = CAST((CAST(cNumber AS INT) + 1) AS VARCHAR(30)) WHERE CardNumber = '{1}' ", ErpName, CardNumber);
  881. CmdExecuteNonQuery(sql, cmd, "更新VoucherHistory表失败!");
  882. }
  883. else
  884. {
  885. sql = string.Format(@"INSERT INTO {0}.dbo.VoucherHistory SELECT '{1}', NULL, NULL, NULL, NULL, 1, 0 ", ErpName, CardNumber);
  886. CmdExecuteNonQuery(sql, cmd, "插入VoucherHistory表失败!");
  887. }
  888. sql = string.Format(@"
  889. DECLARE @Code NVARCHAR(100)
  890. SELECT @Code = CAST(cNumber AS NVARCHAR(50)) FROM {0}.dbo.VoucherHistory WHERE CardNumber = '{1}'
  891. SET @Code = @Code
  892. SELECT @Code ", ErpName, CardNumber);
  893. cmd.CommandText = sql;
  894. DataTable dtCode = SQlReturnData(sql, cmd);
  895. if (dtCode != null && dtCode.Rows.Count > 0)
  896. {
  897. cCode = dtCode.Rows[0][0].ToString();
  898. }
  899. else
  900. {
  901. throw new Exception("获取单据号失败!");
  902. }
  903. return cCode;
  904. }
  905. ///// <summary>
  906. ///// 获取用户的姓名与部门
  907. ///// </summary>
  908. ///// <param name="cPer_Num">用户编号</param>
  909. ///// <param name="cmd"></param>
  910. ///// <returns></returns>
  911. //public static UserInfo GetPersonInfo(string cPer_Num, SqlCommand cmd)
  912. //{
  913. // UserInfo person = new UserInfo();
  914. // string sql = @"exec sp_refreshview ua_user ;
  915. // SELECT a.cUser_Id,a.cUser_Name,b.cDepCode FROM ua_user a
  916. // LEFT JOIN dbo.Department b ON a.cDept=b.cDepName
  917. // WHERE cUser_Id ='" + cPer_Num + "'";
  918. // cmd.CommandText = sql;
  919. // DataTable dt = SQlReturnData(sql, cmd);
  920. // if (dt != null && dt.Rows.Count > 0)
  921. // {
  922. // person.DepCode = dt.Rows[0]["cDepCode"].ToString();
  923. // person.UserName = dt.Rows[0]["cUser_Name"].ToString();
  924. // person.UserCode = cPer_Num;
  925. // }
  926. // return person;
  927. //}
  928. #region 更新现存量
  929. /// <summary>
  930. /// 更新现存量
  931. /// </summary>
  932. /// <param name="cmd"></param>
  933. /// <param name="cInvCode"></param>
  934. /// <param name="cWhCode"></param>
  935. /// <param name="cBatch"></param>
  936. public static void UpdateCurrentStock(SqlCommand cmd, string cInvCode, string cWhCode, string cBatch, decimal iQuantity, VouchKey key, string cFree1, string cFree2, string cFree3, string cFree4, string cFree5, string cFree6, string cFree7, string cFree8)
  937. {
  938. #region 自由项管控
  939. //if (!U8Helper.bFree1(cInvCode, cmd))
  940. //{
  941. // cFree1 = "";
  942. //}
  943. //if (!U8Helper.bFree2(cInvCode, cmd))
  944. //{
  945. // cFree2 = "";
  946. //}
  947. //if (!U8Helper.bFree3(cInvCode, cmd))
  948. //{
  949. // cFree3 = "";
  950. //}
  951. //if (!U8Helper.bFree4(cInvCode, cmd))
  952. //{
  953. // cFree4 = "";
  954. //}
  955. //if (!U8Helper.bFree5(cInvCode, cmd))
  956. //{
  957. // cFree5 = "";
  958. //}
  959. //if (!U8Helper.bFree6(cInvCode, cmd))
  960. //{
  961. // cFree6 = "";
  962. //}
  963. //if (!U8Helper.bFree7(cInvCode, cmd))
  964. //{
  965. // cFree7 = "";
  966. //}
  967. //if (!U8Helper.bFree8(cInvCode, cmd))
  968. //{
  969. // cFree8 = "";
  970. //}
  971. DataTable dtFree = DBHelper.bFree(cInvCode, cmd);
  972. if (dtFree.Rows.Count > 0 && dtFree != null)
  973. {
  974. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree1"]) == true)
  975. {
  976. cFree1 = "";
  977. }
  978. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree2"]) == true)
  979. {
  980. cFree2 = "";
  981. }
  982. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree3"]) == true)
  983. {
  984. cFree3 = "";
  985. }
  986. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree4"]) == true)
  987. {
  988. cFree4 = "";
  989. }
  990. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree5"]) == true)
  991. {
  992. cFree5 = "";
  993. }
  994. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree6"]) == true)
  995. {
  996. cFree6 = "";
  997. }
  998. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree7"]) == true)
  999. {
  1000. cFree7 = "";
  1001. }
  1002. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree8"]) == true)
  1003. {
  1004. cFree8 = "";
  1005. }
  1006. }
  1007. else
  1008. {
  1009. throw new Exception("存货编码:" + cInvCode + "不存在");
  1010. }
  1011. #endregion
  1012. #region 1 取得物料的itemID
  1013. string sql = @"
  1014. IF NOT EXISTS(SELECT Id FROM dbo.SCM_Item
  1015. WHERE cinvcode = '{0}' and cFree1 = '{1}' and cFree2 = '{2}' and cFree3 = '{3}' and cFree4 = '{4}' and cFree5 = '{5}' and cFree6 = '{6}' and cFree7 = '{7}' and cFree8 = '{8}')
  1016. BEGIN INSERT INTO dbo.SCM_Item(cInvCode ,cFree1 ,cFree2 ,cFree3 ,cFree4 , cFree5 , cFree6 ,cFree7 ,cFree8 ,cFree9 ,cFree10 ,PartId) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','','',0) END
  1017. SELECT Id FROM dbo.SCM_Item WHERE cinvcode = '{0}' and cFree1 = '{1}' and cFree2 = '{2}' and cFree3 = '{3}' and cFree4 = '{4}' and cFree5 = '{5}' and cFree6 = '{6}' and cFree7 = '{7}' and cFree8 = '{8}' ";
  1018. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8);
  1019. cmd.CommandText = sql;
  1020. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  1021. if (dtItem.Rows.Count == 0)
  1022. {
  1023. throw new Exception("物料的ItemID取得失败");
  1024. }
  1025. int ItemID = int.Parse(dtItem.Rows[0]["Id"].ToString());
  1026. log.Info("取得物料的itemID" + sql);
  1027. #endregion
  1028. #region 2 更新失败,插入现存量
  1029. sql = @"SELECT AutoID FROM dbo.CurrentStock
  1030. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  1031. AND ItemId = @ItemId and cFree1 = @cFree1 and cFree2 = @cFree2 and cFree3 = @cFree3 and cFree4 = @cFree4
  1032. and cFree5 = @cFree5 and cFree6 = @cFree6 and cFree7 = @cFree7 and cFree8 = @cFree8 ";
  1033. if (cBatch != null)
  1034. {
  1035. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  1036. sql += " and cBatch='" + cBatch + "' ";
  1037. else
  1038. sql += " and cBatch='' ";
  1039. }
  1040. else
  1041. {
  1042. sql += " and cBatch='' ";
  1043. }
  1044. cmd.CommandText = sql;
  1045. cmd.Parameters.Clear();
  1046. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1047. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1048. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1049. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  1050. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  1051. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  1052. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  1053. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  1054. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  1055. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  1056. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  1057. DataTable dtCurrentStock = DBHelper.SQlReturnData(sql, cmd);
  1058. log.Info("查找现存量:" + sql);
  1059. if (dtCurrentStock != null && dtCurrentStock.Rows.Count > 0)
  1060. {
  1061. sql = @"UPDATE dbo.CurrentStock SET iQuantity = iQuantity + @iQuantity
  1062. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  1063. AND ItemId = @ItemId and cFree1 = @cFree1 and cFree2 = @cFree2 and cFree3 = @cFree3 and cFree4 = @cFree4
  1064. and cFree5 = @cFree5 and cFree6 = @cFree6 and cFree7 = @cFree7 and cFree8 = @cFree8 ";
  1065. if (cBatch != null)
  1066. {
  1067. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  1068. sql += " and cBatch='" + cBatch + "' ";
  1069. else
  1070. sql += " and cBatch='' ";
  1071. }
  1072. else
  1073. {
  1074. sql += " and cBatch='' ";
  1075. }
  1076. cmd.CommandText = sql;
  1077. cmd.Parameters.Clear();
  1078. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  1079. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1080. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1081. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1082. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  1083. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  1084. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  1085. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  1086. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  1087. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  1088. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  1089. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  1090. CmdExecuteNonQuery(sql, cmd, "更新现存量失败!");
  1091. log.Info("现存量更新:" + sql);
  1092. }
  1093. else
  1094. {
  1095. sql = @" INSERT INTO dbo.CurrentStock
  1096. (cWhCode,cInvCode,ItemId,cBatch,iSoType,iSodid,iQuantity,
  1097. iNum,fOutQuantity,fOutNum,fInQuantity,fInNum,
  1098. bStopFlag,fTransInQuantity,fTransInNum,
  1099. fTransOutQuantity,fTransOutNum,fPlanQuantity,fPlanNum,fDisableQuantity,
  1100. fDisabNum,fAvaQuantity,fAvaNum,BGSPSTOP,fStopQuantity,
  1101. fStopNum,ipeqty,ipenum,
  1102. cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8)
  1103. SELECT @cWhCode,@cInvCode,@ItemId,@cBatch,'0','',@iQuantity,
  1104. '0','0','0','0','0',
  1105. '0','0','0','0','0','0','0','0',
  1106. '0','0','0','0','0','0','0','0',
  1107. @cFree1, @cFree2, @cFree3, @cFree4, @cFree5, @cFree6, @cFree7, @cFree8";
  1108. cmd.CommandText = sql;
  1109. cmd.Parameters.Clear();
  1110. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1111. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1112. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1113. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  1114. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  1115. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  1116. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  1117. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  1118. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  1119. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  1120. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  1121. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  1122. if (cBatch != null)
  1123. {
  1124. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  1125. cmd.Parameters.Add(new SqlParameter("@cBatch", cBatch));
  1126. else
  1127. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  1128. }
  1129. else
  1130. {
  1131. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  1132. }
  1133. CmdExecuteNonQuery(sql, cmd, "插入现存量失败!");
  1134. log.Info("现存量插入:" + sql);
  1135. }
  1136. #endregion
  1137. #region 插入UserDefine
  1138. if (cFree1 != "")
  1139. {
  1140. sql = @"
  1141. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 20 AND cValue = '{0}')
  1142. BEGIN
  1143. INSERT INTO UserDefine
  1144. (cID, cValue)
  1145. VALUES (20, '{0}') END ";
  1146. sql = string.Format(sql, cFree1);
  1147. cmd.CommandText = sql;
  1148. cmd.ExecuteNonQuery();
  1149. }
  1150. if (cFree2 != "")
  1151. {
  1152. sql = @"
  1153. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 21 AND cValue = '{0}')
  1154. BEGIN
  1155. INSERT INTO UserDefine
  1156. (cID, cValue)
  1157. VALUES (21, '{0}') END ";
  1158. sql = string.Format(sql, cFree2);
  1159. cmd.CommandText = sql;
  1160. cmd.ExecuteNonQuery();
  1161. }
  1162. if (cFree3 != "")
  1163. {
  1164. sql = @"
  1165. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 28 AND cValue = '{0}')
  1166. BEGIN
  1167. INSERT INTO UserDefine
  1168. (cID, cValue)
  1169. VALUES (28, '{0}') END ";
  1170. sql = string.Format(sql, cFree3);
  1171. cmd.CommandText = sql;
  1172. cmd.ExecuteNonQuery();
  1173. }
  1174. if (cFree4 != "")
  1175. {
  1176. sql = @"
  1177. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 29 AND cValue = '{0}')
  1178. BEGIN
  1179. INSERT INTO UserDefine
  1180. (cID, cValue)
  1181. VALUES (29, '{0}') END ";
  1182. sql = string.Format(sql, cFree4);
  1183. cmd.CommandText = sql;
  1184. cmd.ExecuteNonQuery();
  1185. }
  1186. if (cFree5 != "")
  1187. {
  1188. sql = @"
  1189. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 30 AND cValue = '{0}')
  1190. BEGIN
  1191. INSERT INTO UserDefine
  1192. (cID, cValue)
  1193. VALUES (30, '{0}') END ";
  1194. sql = string.Format(sql, cFree5);
  1195. cmd.CommandText = sql;
  1196. cmd.ExecuteNonQuery();
  1197. }
  1198. if (cFree6 != "")
  1199. {
  1200. sql = @"
  1201. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 31 AND cValue = '{0}')
  1202. BEGIN
  1203. INSERT INTO UserDefine
  1204. (cID, cValue)
  1205. VALUES (31, '{0}') END ";
  1206. sql = string.Format(sql, cFree6);
  1207. cmd.CommandText = sql;
  1208. cmd.ExecuteNonQuery();
  1209. }
  1210. if (cFree7 != "")
  1211. {
  1212. sql = @"
  1213. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 32 AND cValue = '{0}')
  1214. BEGIN
  1215. INSERT INTO UserDefine
  1216. (cID, cValue)
  1217. VALUES (32, '{0}') END ";
  1218. sql = string.Format(sql, cFree7);
  1219. cmd.CommandText = sql;
  1220. cmd.ExecuteNonQuery();
  1221. }
  1222. if (cFree8 != "")
  1223. {
  1224. sql = @"
  1225. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 33 AND cValue = '{0}')
  1226. BEGIN
  1227. INSERT INTO UserDefine
  1228. (cID, cValue)
  1229. VALUES (33, '{0}') END ";
  1230. sql = string.Format(sql, cFree8);
  1231. cmd.CommandText = sql;
  1232. cmd.ExecuteNonQuery();
  1233. }
  1234. #endregion
  1235. #region 3 写入记账表
  1236. if (DBHelper.bInCost(cWhCode, cmd) == true)
  1237. {
  1238. sql = @"SELECT IDUN, IDSUN FROM [dbo].[" + key.TableName + @"]
  1239. WHERE IDUN = @IDUN AND IDSUN = @IDSUN";
  1240. cmd.CommandText = sql;
  1241. cmd.Parameters.Clear();
  1242. cmd.Parameters.Add(new SqlParameter("@IDUN", key.ID));
  1243. cmd.Parameters.Add(new SqlParameter("@IDSUN", key.DID));
  1244. DataTable IA_ST = DBHelper.SQlReturnData(sql, cmd);
  1245. if (IA_ST != null && IA_ST.Rows.Count > 0)
  1246. {
  1247. }
  1248. else
  1249. {
  1250. sql = string.Format(@" INSERT INTO [dbo].[" + key.TableName + @"]
  1251. SELECT '{0}','{1}','{2}','{3}'", key.ID, key.DID, key.cVouchTypeUN, key.cBustypeUN);
  1252. cmd.CommandText = sql;
  1253. CmdExecuteNonQuery(sql, cmd, "采购入库单写入记账表失败!");
  1254. }
  1255. }
  1256. #endregion
  1257. }
  1258. /// <summary>
  1259. /// 验证扫描的是条码还是箱号
  1260. /// </summary>
  1261. /// <param name="ScanCode"></param>
  1262. /// <param name="WorkPoint"></param>
  1263. /// <param name="dsconn"></param>
  1264. /// <returns></returns>
  1265. public static string ScanTypeCheck(string code, string workPoint, SqlCommand cmd)
  1266. {
  1267. string sql = @" select ContainerCode from ICSContainer
  1268. where ContainerCode='{0}' and WorkPoint='{1}'
  1269. select LotNo from ICSInventoryLot
  1270. where LotNo='{0}' and WorkPoint='{1}'";
  1271. sql = string.Format(sql, code, workPoint);
  1272. DataSet dt = SQlReturnDataSet(sql, cmd);
  1273. if (dt.Tables[0].Rows.Count > 0)
  1274. {
  1275. return "CARTON";
  1276. }
  1277. else
  1278. {
  1279. return "LOTNO";
  1280. }
  1281. throw new NotImplementedException();
  1282. }
  1283. /// <summary>
  1284. /// 根据数据字典主表Code获取自定义档案的所有查询条件
  1285. /// </summary>
  1286. /// <param name="EnCode"></param>
  1287. /// <param name="ItemId"></param>
  1288. /// <param name="cmd"></param>
  1289. /// <returns></returns>
  1290. public static string SearchConditonGet(string EnCode, string UserCode, SqlCommand cmd)
  1291. {
  1292. string SearchCondition = "";
  1293. string sql = @"DECLARE @flag int
  1294. SET @flag=0
  1295. SELECT @flag=F_EnabledMark FROM Sys_SRM_Items WHERE F_EnCode='{0}'
  1296. IF @flag=1
  1297. BEGIN
  1298. SELECT @flag=F_EnabledMark FROM Sys_SRM_ItemsDetail WHERE F_ItemId=(SELECT F_Id FROM Sys_SRM_Items WHERE F_EnCode='{0}')
  1299. END
  1300. SELECT @flag";
  1301. sql = string.Format(sql, EnCode);
  1302. int i = (int)ExecuteScalar(sql, cmd);
  1303. if(i == 0)
  1304. {
  1305. SearchCondition = "";
  1306. }
  1307. else
  1308. {
  1309. sql = string.Format(@"SELECT ISNULL(' ' + F_Define1 + ' ' + F_Define2 + ' ' + F_Define3 + ' ' + F_Define4, '') AS searchCondition FROM Sys_SRM_ItemsDetail
  1310. WHERE F_ItemId=(SELECT F_Id FROM Sys_SRM_Items WHERE F_EnCode='{0}')", EnCode);
  1311. DataTable dt = DBHelper.SQlReturnData(sql, cmd);
  1312. foreach(DataRow row in dt.Rows)
  1313. {
  1314. SearchCondition += row["searchCondition"].ToString();
  1315. }
  1316. }
  1317. return SearchCondition.Replace("[AppConfig.UserCode]", UserCode);
  1318. }
  1319. public static void UpdateCurrentStock(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, DateTime dVDate, DateTime dMdate, VouchKey key)
  1320. {
  1321. #region 自由项管控
  1322. //if (!U8Helper.bFree1(cInvCode, cmd))
  1323. //{
  1324. // cFree1 = "";
  1325. //}
  1326. //if (!U8Helper.bFree2(cInvCode, cmd))
  1327. //{
  1328. // cFree2 = "";
  1329. //}
  1330. //if (!U8Helper.bFree3(cInvCode, cmd))
  1331. //{
  1332. // cFree3 = "";
  1333. //}
  1334. //if (!U8Helper.bFree4(cInvCode, cmd))
  1335. //{
  1336. // cFree4 = "";
  1337. //}
  1338. //if (!U8Helper.bFree5(cInvCode, cmd))
  1339. //{
  1340. // cFree5 = "";
  1341. //}
  1342. //if (!U8Helper.bFree6(cInvCode, cmd))
  1343. //{
  1344. // cFree6 = "";
  1345. //}
  1346. //if (!U8Helper.bFree7(cInvCode, cmd))
  1347. //{
  1348. // cFree7 = "";
  1349. //}
  1350. //if (!U8Helper.bFree8(cInvCode, cmd))
  1351. //{
  1352. // cFree8 = "";
  1353. //}
  1354. DataTable dtFree = DBHelper.bFree(cInvCode, cmd);
  1355. if (dtFree.Rows.Count > 0 && dtFree != null)
  1356. {
  1357. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree1"]) == true)
  1358. {
  1359. cFree1 = "";
  1360. }
  1361. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree2"]) == true)
  1362. {
  1363. cFree2 = "";
  1364. }
  1365. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree3"]) == true)
  1366. {
  1367. cFree3 = "";
  1368. }
  1369. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree4"]) == true)
  1370. {
  1371. cFree4 = "";
  1372. }
  1373. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree5"]) == true)
  1374. {
  1375. cFree5 = "";
  1376. }
  1377. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree6"]) == true)
  1378. {
  1379. cFree6 = "";
  1380. }
  1381. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree7"]) == true)
  1382. {
  1383. cFree7 = "";
  1384. }
  1385. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree8"]) == true)
  1386. {
  1387. cFree8 = "";
  1388. }
  1389. }
  1390. else
  1391. {
  1392. throw new Exception("存货编码:" + cInvCode + "不存在");
  1393. }
  1394. #endregion
  1395. #region 1 取得物料的itemID
  1396. string sql = @"
  1397. IF NOT EXISTS(SELECT Id FROM dbo.SCM_Item
  1398. WHERE cinvcode = '{0}' and cFree1 = '{1}' and cFree2 = '{2}' and cFree3 = '{3}' and cFree4 = '{4}' and cFree5 = '{5}' and cFree6 = '{6}' and cFree7 = '{7}' and cFree8 = '{8}')
  1399. BEGIN INSERT INTO dbo.SCM_Item(cInvCode ,cFree1 ,cFree2 ,cFree3 ,cFree4 , cFree5 , cFree6 ,cFree7 ,cFree8 ,cFree9 ,cFree10 ,PartId) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','','',0) END
  1400. SELECT Id FROM dbo.SCM_Item WHERE cinvcode = '{0}' and cFree1 = '{1}' and cFree2 = '{2}' and cFree3 = '{3}' and cFree4 = '{4}' and cFree5 = '{5}' and cFree6 = '{6}' and cFree7 = '{7}' and cFree8 = '{8}' ";
  1401. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8);
  1402. cmd.CommandText = sql;
  1403. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  1404. if (dtItem.Rows.Count == 0)
  1405. {
  1406. throw new Exception("物料的ItemID取得失败");
  1407. }
  1408. int ItemID = int.Parse(dtItem.Rows[0]["Id"].ToString());
  1409. log.Info("取得物料的itemID" + sql);
  1410. #endregion
  1411. #region 2 更新失败,插入现存量
  1412. sql = @"SELECT AutoID FROM dbo.CurrentStock
  1413. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  1414. AND ItemId = @ItemId and cFree1 = @cFree1 and cFree2 = @cFree2 and cFree3 = @cFree3 and cFree4 = @cFree4
  1415. and cFree5 = @cFree5 and cFree6 = @cFree6 and cFree7 = @cFree7 and cFree8 = @cFree8 ";
  1416. if (cBatch != null)
  1417. {
  1418. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  1419. sql += " and cBatch='" + cBatch + "' ";
  1420. else
  1421. sql += " and cBatch='' ";
  1422. }
  1423. else
  1424. {
  1425. sql += " and cBatch='' ";
  1426. }
  1427. //if (U8Helper.bInvQuality(cInvCode, cmd) == true)
  1428. // sql += " AND CONVERT(DATE, dMdate) = CONVERT(DATE, '" + dMdate + "') AND CONVERT(DATE, dVDate) = CONVERT(DATE, '" + dVDate + "') ";
  1429. cmd.CommandText = sql;
  1430. cmd.Parameters.Clear();
  1431. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1432. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1433. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1434. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  1435. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  1436. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  1437. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  1438. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  1439. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  1440. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  1441. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  1442. DataTable dtCurrentStock = DBHelper.SQlReturnData(sql, cmd);
  1443. log.Info("查找现存量:" + sql);
  1444. if (dtCurrentStock != null && dtCurrentStock.Rows.Count > 0)
  1445. {
  1446. sql = @"UPDATE dbo.CurrentStock SET iQuantity = iQuantity + @iQuantity
  1447. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  1448. AND ItemId = @ItemId and cFree1 = @cFree1 and cFree2 = @cFree2 and cFree3 = @cFree3 and cFree4 = @cFree4
  1449. and cFree5 = @cFree5 and cFree6 = @cFree6 and cFree7 = @cFree7 and cFree8 = @cFree8";
  1450. if (cBatch != null)
  1451. {
  1452. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  1453. sql += " and cBatch='" + cBatch + "' ";
  1454. else
  1455. sql += " and cBatch='' ";
  1456. }
  1457. else
  1458. {
  1459. sql += " and cBatch='' ";
  1460. }
  1461. //if (U8Helper.bInvQuality(cInvCode, cmd) == true)
  1462. // sql += " AND CONVERT(DATE, dMdate) = CONVERT(DATE, '" + dMdate + "') AND CONVERT(DATE, dVDate) = CONVERT(DATE, '" + dVDate + "') ";
  1463. cmd.CommandText = sql;
  1464. cmd.Parameters.Clear();
  1465. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  1466. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1467. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1468. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1469. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  1470. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  1471. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  1472. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  1473. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  1474. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  1475. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  1476. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  1477. CmdExecuteNonQuery(sql, cmd, "更新现存量失败!");
  1478. log.Info("现存量更新:" + sql);
  1479. }
  1480. else
  1481. {
  1482. sql = @" INSERT INTO dbo.CurrentStock
  1483. (cWhCode,cInvCode,ItemId,cBatch,iSoType,iSodid,iQuantity,
  1484. iNum,fOutQuantity,fOutNum,fInQuantity,fInNum,
  1485. bStopFlag,fTransInQuantity,fTransInNum,
  1486. fTransOutQuantity,fTransOutNum,fPlanQuantity,fPlanNum,fDisableQuantity,
  1487. fDisableNum,fAvaQuantity,fAvaNum,BGSPSTOP,fStopQuantity,
  1488. fStopNum,ipeqty,ipenum,
  1489. cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, dMdate, dVDate, iMassDate, cMassUnit)
  1490. SELECT @cWhCode,@cInvCode,@ItemId,@cBatch,'0','',@iQuantity,
  1491. '0','0','0','0','0',
  1492. '0','0','0','0','0','0','0','0',
  1493. '0','0','0','0','0','0','0','0',
  1494. @cFree1, @cFree2, @cFree3, @cFree4, @cFree5, @cFree6, @cFree7, @cFree8, @dMdate, @dVDate, @iMassDate, @cMassUnit";
  1495. cmd.CommandText = sql;
  1496. cmd.Parameters.Clear();
  1497. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1498. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1499. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1500. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  1501. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  1502. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  1503. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  1504. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  1505. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  1506. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  1507. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  1508. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  1509. if (DBHelper.bInvQuality(cInvCode, cmd) == true)
  1510. {
  1511. #region
  1512. int iMassDate = 0;
  1513. int cMassUnit = 0;
  1514. string sqlCost = @" SELECT iMassDate, cMassUnit FROM Inventory WHERE 1=1 AND cInvCode = '" + cInvCode + "' ";
  1515. DataTable dtINV = DBHelper.SQlReturnData(sqlCost, cmd);
  1516. if (dtINV != null && dtINV.Rows.Count > 0)
  1517. {
  1518. iMassDate = Convert.ToInt16(dtINV.Rows[0]["iMassDate"].ToString());
  1519. cMassUnit = Convert.ToInt16(dtINV.Rows[0]["cMassUnit"].ToString());
  1520. }
  1521. #endregion
  1522. cmd.Parameters.Add(new SqlParameter("@dMdate", dMdate));
  1523. cmd.Parameters.Add(new SqlParameter("@dVDate", dVDate));
  1524. cmd.Parameters.Add(new SqlParameter("@iMassDate", iMassDate));
  1525. cmd.Parameters.Add(new SqlParameter("@cMassUnit", cMassUnit));
  1526. }
  1527. else
  1528. {
  1529. cmd.Parameters.Add(new SqlParameter("@dMdate", DBNull.Value));
  1530. cmd.Parameters.Add(new SqlParameter("@dVDate", DBNull.Value));
  1531. cmd.Parameters.Add(new SqlParameter("@iMassDate", DBNull.Value));
  1532. cmd.Parameters.Add(new SqlParameter("@cMassUnit", DBNull.Value));
  1533. }
  1534. if (cBatch != null)
  1535. {
  1536. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  1537. cmd.Parameters.Add(new SqlParameter("@cBatch", cBatch));
  1538. else
  1539. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  1540. }
  1541. else
  1542. {
  1543. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  1544. }
  1545. CmdExecuteNonQuery(sql, cmd, "插入现存量失败!");
  1546. log.Info("现存量插入:" + sql);
  1547. }
  1548. #endregion
  1549. #region 插入UserDefine
  1550. if (cFree1 != "")
  1551. {
  1552. sql = @"
  1553. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 20 AND cValue = '{0}')
  1554. BEGIN
  1555. INSERT INTO UserDefine
  1556. (cID, cValue)
  1557. VALUES (20, '{0}') END ";
  1558. sql = string.Format(sql, cFree1);
  1559. cmd.CommandText = sql;
  1560. cmd.ExecuteNonQuery();
  1561. }
  1562. if (cFree2 != "")
  1563. {
  1564. sql = @"
  1565. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 21 AND cValue = '{0}')
  1566. BEGIN
  1567. INSERT INTO UserDefine
  1568. (cID, cValue)
  1569. VALUES (21, '{0}') END ";
  1570. sql = string.Format(sql, cFree2);
  1571. cmd.CommandText = sql;
  1572. cmd.ExecuteNonQuery();
  1573. }
  1574. if (cFree3 != "")
  1575. {
  1576. sql = @"
  1577. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 28 AND cValue = '{0}')
  1578. BEGIN
  1579. INSERT INTO UserDefine
  1580. (cID, cValue)
  1581. VALUES (28, '{0}') END ";
  1582. sql = string.Format(sql, cFree3);
  1583. cmd.CommandText = sql;
  1584. cmd.ExecuteNonQuery();
  1585. }
  1586. if (cFree4 != "")
  1587. {
  1588. sql = @"
  1589. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 29 AND cValue = '{0}')
  1590. BEGIN
  1591. INSERT INTO UserDefine
  1592. (cID, cValue)
  1593. VALUES (29, '{0}') END ";
  1594. sql = string.Format(sql, cFree4);
  1595. cmd.CommandText = sql;
  1596. cmd.ExecuteNonQuery();
  1597. }
  1598. if (cFree5 != "")
  1599. {
  1600. sql = @"
  1601. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 30 AND cValue = '{0}')
  1602. BEGIN
  1603. INSERT INTO UserDefine
  1604. (cID, cValue)
  1605. VALUES (30, '{0}') END ";
  1606. sql = string.Format(sql, cFree5);
  1607. cmd.CommandText = sql;
  1608. cmd.ExecuteNonQuery();
  1609. }
  1610. if (cFree6 != "")
  1611. {
  1612. sql = @"
  1613. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 31 AND cValue = '{0}')
  1614. BEGIN
  1615. INSERT INTO UserDefine
  1616. (cID, cValue)
  1617. VALUES (31, '{0}') END ";
  1618. sql = string.Format(sql, cFree6);
  1619. cmd.CommandText = sql;
  1620. cmd.ExecuteNonQuery();
  1621. }
  1622. if (cFree7 != "")
  1623. {
  1624. sql = @"
  1625. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 32 AND cValue = '{0}')
  1626. BEGIN
  1627. INSERT INTO UserDefine
  1628. (cID, cValue)
  1629. VALUES (32, '{0}') END ";
  1630. sql = string.Format(sql, cFree7);
  1631. cmd.CommandText = sql;
  1632. cmd.ExecuteNonQuery();
  1633. }
  1634. if (cFree8 != "")
  1635. {
  1636. sql = @"
  1637. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 33 AND cValue = '{0}')
  1638. BEGIN
  1639. INSERT INTO UserDefine
  1640. (cID, cValue)
  1641. VALUES (33, '{0}') END ";
  1642. sql = string.Format(sql, cFree8);
  1643. cmd.CommandText = sql;
  1644. cmd.ExecuteNonQuery();
  1645. }
  1646. #endregion
  1647. #region 3 写入记账表
  1648. if (DBHelper.bInCost(cWhCode, cmd) == true)
  1649. {
  1650. sql = @"SELECT IDUN, IDSUN FROM [dbo].[" + key.TableName + @"]
  1651. WHERE IDUN = @IDUN AND IDSUN = @IDSUN";
  1652. cmd.CommandText = sql;
  1653. cmd.Parameters.Clear();
  1654. cmd.Parameters.Add(new SqlParameter("@IDUN", key.ID));
  1655. cmd.Parameters.Add(new SqlParameter("@IDSUN", key.DID));
  1656. DataTable IA_ST = DBHelper.SQlReturnData(sql, cmd);
  1657. if (IA_ST != null && IA_ST.Rows.Count > 0)
  1658. {
  1659. }
  1660. else
  1661. {
  1662. sql = string.Format(@" INSERT INTO [dbo].[" + key.TableName + @"]
  1663. SELECT '{0}','{1}','{2}','{3}'", key.ID, key.DID, key.cVouchTypeUN, key.cBustypeUN);
  1664. cmd.CommandText = sql;
  1665. CmdExecuteNonQuery(sql, cmd, "采购入库单写入记账表失败!");
  1666. }
  1667. }
  1668. #endregion
  1669. }
  1670. public static void UpdateCurrentStock08(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, DateTime dVDate, DateTime dMdate, VouchKey key)
  1671. {
  1672. #region 自由项管控
  1673. //if (!U8Helper.bFree1(cInvCode, cmd))
  1674. //{
  1675. // cFree1 = "";
  1676. //}
  1677. //if (!U8Helper.bFree2(cInvCode, cmd))
  1678. //{
  1679. // cFree2 = "";
  1680. //}
  1681. //if (!U8Helper.bFree3(cInvCode, cmd))
  1682. //{
  1683. // cFree3 = "";
  1684. //}
  1685. //if (!U8Helper.bFree4(cInvCode, cmd))
  1686. //{
  1687. // cFree4 = "";
  1688. //}
  1689. //if (!U8Helper.bFree5(cInvCode, cmd))
  1690. //{
  1691. // cFree5 = "";
  1692. //}
  1693. //if (!U8Helper.bFree6(cInvCode, cmd))
  1694. //{
  1695. // cFree6 = "";
  1696. //}
  1697. //if (!U8Helper.bFree7(cInvCode, cmd))
  1698. //{
  1699. // cFree7 = "";
  1700. //}
  1701. //if (!U8Helper.bFree8(cInvCode, cmd))
  1702. //{
  1703. // cFree8 = "";
  1704. //}
  1705. DataTable dtFree = DBHelper.bFree(cInvCode, cmd);
  1706. if (dtFree.Rows.Count > 0 && dtFree != null)
  1707. {
  1708. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree1"]) == true)
  1709. {
  1710. cFree1 = "";
  1711. }
  1712. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree2"]) == true)
  1713. {
  1714. cFree2 = "";
  1715. }
  1716. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree3"]) == true)
  1717. {
  1718. cFree3 = "";
  1719. }
  1720. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree4"]) == true)
  1721. {
  1722. cFree4 = "";
  1723. }
  1724. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree5"]) == true)
  1725. {
  1726. cFree5 = "";
  1727. }
  1728. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree6"]) == true)
  1729. {
  1730. cFree6 = "";
  1731. }
  1732. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree7"]) == true)
  1733. {
  1734. cFree7 = "";
  1735. }
  1736. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree8"]) == true)
  1737. {
  1738. cFree8 = "";
  1739. }
  1740. }
  1741. else
  1742. {
  1743. throw new Exception("存货编码:" + cInvCode + "不存在");
  1744. }
  1745. #endregion
  1746. #region 1 取得物料的itemID
  1747. string sql = @"
  1748. IF NOT EXISTS(SELECT Id FROM dbo.SCM_Item
  1749. WHERE cinvcode = '{0}' and cFree1 = '{1}' and cFree2 = '{2}' and cFree3 = '{3}' and cFree4 = '{4}' and cFree5 = '{5}' and cFree6 = '{6}' and cFree7 = '{7}' and cFree8 = '{8}')
  1750. BEGIN INSERT INTO dbo.SCM_Item(cInvCode ,cFree1 ,cFree2 ,cFree3 ,cFree4 , cFree5 , cFree6 ,cFree7 ,cFree8 ,cFree9 ,cFree10 ,PartId) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','','',0) END
  1751. SELECT Id FROM dbo.SCM_Item WHERE cinvcode = '{0}' and cFree1 = '{1}' and cFree2 = '{2}' and cFree3 = '{3}' and cFree4 = '{4}' and cFree5 = '{5}' and cFree6 = '{6}' and cFree7 = '{7}' and cFree8 = '{8}' ";
  1752. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8);
  1753. cmd.CommandText = sql;
  1754. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  1755. if (dtItem.Rows.Count == 0)
  1756. {
  1757. throw new Exception("物料的ItemID取得失败");
  1758. }
  1759. int ItemID = int.Parse(dtItem.Rows[0]["Id"].ToString());
  1760. log.Info("取得物料的itemID" + sql);
  1761. #endregion
  1762. #region 2 更新失败,插入现存量
  1763. sql = @"SELECT AutoID FROM dbo.CurrentStock
  1764. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  1765. AND ItemId = @ItemId and cFree1 = @cFree1 and cFree2 = @cFree2 and cFree3 = @cFree3 and cFree4 = @cFree4
  1766. and cFree5 = @cFree5 and cFree6 = @cFree6 and cFree7 = @cFree7 and cFree8 = @cFree8 ";
  1767. if (cBatch != null)
  1768. {
  1769. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  1770. sql += " and cBatch='" + cBatch + "' ";
  1771. else
  1772. sql += " and cBatch='' ";
  1773. }
  1774. else
  1775. {
  1776. sql += " and cBatch='' ";
  1777. }
  1778. //if (U8Helper.bInvQuality(cInvCode, cmd) == true)
  1779. // sql += " AND CONVERT(DATE, dMdate) = CONVERT(DATE, '" + dMdate + "') AND CONVERT(DATE, dVDate) = CONVERT(DATE, '" + dVDate + "') ";
  1780. cmd.CommandText = sql;
  1781. cmd.Parameters.Clear();
  1782. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1783. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1784. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1785. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  1786. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  1787. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  1788. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  1789. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  1790. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  1791. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  1792. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  1793. DataTable dtCurrentStock = DBHelper.SQlReturnData(sql, cmd);
  1794. log.Info("查找现存量:" + sql);
  1795. if (dtCurrentStock != null && dtCurrentStock.Rows.Count > 0)
  1796. {
  1797. sql = @"UPDATE dbo.CurrentStock SET iQuantity = iQuantity + @iQuantity, fInQuantity = fInQuantity + @fInQuantity
  1798. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  1799. AND ItemId = @ItemId and cFree1 = @cFree1 and cFree2 = @cFree2 and cFree3 = @cFree3 and cFree4 = @cFree4
  1800. and cFree5 = @cFree5 and cFree6 = @cFree6 and cFree7 = @cFree7 and cFree8 = @cFree8";
  1801. if (cBatch != null)
  1802. {
  1803. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  1804. sql += " and cBatch='" + cBatch + "' ";
  1805. else
  1806. sql += " and cBatch='' ";
  1807. }
  1808. else
  1809. {
  1810. sql += " and cBatch='' ";
  1811. }
  1812. //if (U8Helper.bInvQuality(cInvCode, cmd) == true)
  1813. // sql += " AND CONVERT(DATE, dMdate) = CONVERT(DATE, '" + dMdate + "') AND CONVERT(DATE, dVDate) = CONVERT(DATE, '" + dVDate + "') ";
  1814. cmd.CommandText = sql;
  1815. cmd.Parameters.Clear();
  1816. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  1817. cmd.Parameters.Add(new SqlParameter("@fInQuantity", iQuantity));
  1818. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1819. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1820. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1821. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  1822. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  1823. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  1824. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  1825. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  1826. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  1827. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  1828. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  1829. CmdExecuteNonQuery(sql, cmd, "更新现存量失败!");
  1830. log.Info("现存量更新:" + sql);
  1831. }
  1832. else
  1833. {
  1834. sql = @" INSERT INTO dbo.CurrentStock
  1835. (cWhCode,cInvCode,ItemId,cBatch,iSoType,iSodid,iQuantity,
  1836. iNum,fOutQuantity,fOutNum,fInQuantity,fInNum,
  1837. bStopFlag,fTransInQuantity,fTransInNum,
  1838. fTransOutQuantity,fTransOutNum,fPlanQuantity,fPlanNum,fDisableQuantity,
  1839. fDisableNum,fAvaQuantity,fAvaNum,BGSPSTOP,fStopQuantity,
  1840. fStopNum,ipeqty,ipenum,
  1841. cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, dMdate, dVDate, iMassDate, cMassUnit)
  1842. SELECT @cWhCode,@cInvCode,@ItemId,@cBatch,'0','',@iQuantity,
  1843. '0','0','0',@fInQuantity,'0',
  1844. '0','0','0','0','0','0','0','0',
  1845. '0','0','0','0','0','0','0','0',
  1846. @cFree1, @cFree2, @cFree3, @cFree4, @cFree5, @cFree6, @cFree7, @cFree8, @dMdate, @dVDate, @iMassDate, @cMassUnit";
  1847. cmd.CommandText = sql;
  1848. cmd.Parameters.Clear();
  1849. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  1850. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  1851. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  1852. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  1853. cmd.Parameters.Add(new SqlParameter("@fInQuantity", iQuantity));
  1854. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  1855. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  1856. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  1857. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  1858. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  1859. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  1860. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  1861. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  1862. if (DBHelper.bInvQuality(cInvCode, cmd) == true)
  1863. {
  1864. #region
  1865. int iMassDate = 0;
  1866. int cMassUnit = 0;
  1867. string sqlCost = @" SELECT iMassDate, cMassUnit FROM Inventory WHERE 1=1 AND cInvCode = '" + cInvCode + "' ";
  1868. DataTable dtINV = DBHelper.SQlReturnData(sqlCost, cmd);
  1869. if (dtINV != null && dtINV.Rows.Count > 0)
  1870. {
  1871. iMassDate = Convert.ToInt16(dtINV.Rows[0]["iMassDate"].ToString());
  1872. cMassUnit = Convert.ToInt16(dtINV.Rows[0]["cMassUnit"].ToString());
  1873. }
  1874. #endregion
  1875. cmd.Parameters.Add(new SqlParameter("@dMdate", dMdate));
  1876. cmd.Parameters.Add(new SqlParameter("@dVDate", dVDate));
  1877. cmd.Parameters.Add(new SqlParameter("@iMassDate", iMassDate));
  1878. cmd.Parameters.Add(new SqlParameter("@cMassUnit", cMassUnit));
  1879. }
  1880. else
  1881. {
  1882. cmd.Parameters.Add(new SqlParameter("@dMdate", DBNull.Value));
  1883. cmd.Parameters.Add(new SqlParameter("@dVDate", DBNull.Value));
  1884. cmd.Parameters.Add(new SqlParameter("@iMassDate", DBNull.Value));
  1885. cmd.Parameters.Add(new SqlParameter("@cMassUnit", DBNull.Value));
  1886. }
  1887. if (cBatch != null)
  1888. {
  1889. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  1890. cmd.Parameters.Add(new SqlParameter("@cBatch", cBatch));
  1891. else
  1892. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  1893. }
  1894. else
  1895. {
  1896. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  1897. }
  1898. CmdExecuteNonQuery(sql, cmd, "插入现存量失败!");
  1899. log.Info("现存量插入:" + sql);
  1900. }
  1901. #endregion
  1902. #region 插入UserDefine
  1903. if (cFree1 != "")
  1904. {
  1905. sql = @"
  1906. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 20 AND cValue = '{0}')
  1907. BEGIN
  1908. INSERT INTO UserDefine
  1909. (cID, cValue)
  1910. VALUES (20, '{0}') END ";
  1911. sql = string.Format(sql, cFree1);
  1912. cmd.CommandText = sql;
  1913. cmd.ExecuteNonQuery();
  1914. }
  1915. if (cFree2 != "")
  1916. {
  1917. sql = @"
  1918. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 21 AND cValue = '{0}')
  1919. BEGIN
  1920. INSERT INTO UserDefine
  1921. (cID, cValue)
  1922. VALUES (21, '{0}') END ";
  1923. sql = string.Format(sql, cFree2);
  1924. cmd.CommandText = sql;
  1925. cmd.ExecuteNonQuery();
  1926. }
  1927. if (cFree3 != "")
  1928. {
  1929. sql = @"
  1930. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 28 AND cValue = '{0}')
  1931. BEGIN
  1932. INSERT INTO UserDefine
  1933. (cID, cValue)
  1934. VALUES (28, '{0}') END ";
  1935. sql = string.Format(sql, cFree3);
  1936. cmd.CommandText = sql;
  1937. cmd.ExecuteNonQuery();
  1938. }
  1939. if (cFree4 != "")
  1940. {
  1941. sql = @"
  1942. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 29 AND cValue = '{0}')
  1943. BEGIN
  1944. INSERT INTO UserDefine
  1945. (cID, cValue)
  1946. VALUES (29, '{0}') END ";
  1947. sql = string.Format(sql, cFree4);
  1948. cmd.CommandText = sql;
  1949. cmd.ExecuteNonQuery();
  1950. }
  1951. if (cFree5 != "")
  1952. {
  1953. sql = @"
  1954. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 30 AND cValue = '{0}')
  1955. BEGIN
  1956. INSERT INTO UserDefine
  1957. (cID, cValue)
  1958. VALUES (30, '{0}') END ";
  1959. sql = string.Format(sql, cFree5);
  1960. cmd.CommandText = sql;
  1961. cmd.ExecuteNonQuery();
  1962. }
  1963. if (cFree6 != "")
  1964. {
  1965. sql = @"
  1966. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 31 AND cValue = '{0}')
  1967. BEGIN
  1968. INSERT INTO UserDefine
  1969. (cID, cValue)
  1970. VALUES (31, '{0}') END ";
  1971. sql = string.Format(sql, cFree6);
  1972. cmd.CommandText = sql;
  1973. cmd.ExecuteNonQuery();
  1974. }
  1975. if (cFree7 != "")
  1976. {
  1977. sql = @"
  1978. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 32 AND cValue = '{0}')
  1979. BEGIN
  1980. INSERT INTO UserDefine
  1981. (cID, cValue)
  1982. VALUES (32, '{0}') END ";
  1983. sql = string.Format(sql, cFree7);
  1984. cmd.CommandText = sql;
  1985. cmd.ExecuteNonQuery();
  1986. }
  1987. if (cFree8 != "")
  1988. {
  1989. sql = @"
  1990. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 33 AND cValue = '{0}')
  1991. BEGIN
  1992. INSERT INTO UserDefine
  1993. (cID, cValue)
  1994. VALUES (33, '{0}') END ";
  1995. sql = string.Format(sql, cFree8);
  1996. cmd.CommandText = sql;
  1997. cmd.ExecuteNonQuery();
  1998. }
  1999. #endregion
  2000. #region 3 写入记账表
  2001. if (DBHelper.bInCost(cWhCode, cmd) == true)
  2002. {
  2003. sql = @"SELECT IDUN, IDSUN FROM [dbo].[" + key.TableName + @"]
  2004. WHERE IDUN = @IDUN AND IDSUN = @IDSUN";
  2005. cmd.CommandText = sql;
  2006. cmd.Parameters.Clear();
  2007. cmd.Parameters.Add(new SqlParameter("@IDUN", key.ID));
  2008. cmd.Parameters.Add(new SqlParameter("@IDSUN", key.DID));
  2009. DataTable IA_ST = DBHelper.SQlReturnData(sql, cmd);
  2010. if (IA_ST != null && IA_ST.Rows.Count > 0)
  2011. {
  2012. }
  2013. else
  2014. {
  2015. sql = string.Format(@" INSERT INTO [dbo].[" + key.TableName + @"]
  2016. SELECT '{0}','{1}','{2}','{3}'", key.ID, key.DID, key.cVouchTypeUN, key.cBustypeUN);
  2017. cmd.CommandText = sql;
  2018. CmdExecuteNonQuery(sql, cmd, "采购入库单写入记账表失败!");
  2019. }
  2020. }
  2021. #endregion
  2022. }
  2023. public static void UpdateCurrentStockL(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, DateTime dVDate, DateTime dMdate, VouchKey key, bool isOutQuantity = false)
  2024. {
  2025. #region 自由项管控
  2026. DataTable dtFree = DBHelper.bFree(cInvCode, cmd);
  2027. if (dtFree.Rows.Count > 0 && dtFree != null)
  2028. {
  2029. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree1"]) == true)
  2030. {
  2031. cFree1 = "";
  2032. }
  2033. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree2"]) == true)
  2034. {
  2035. cFree2 = "";
  2036. }
  2037. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree3"]) == true)
  2038. {
  2039. cFree3 = "";
  2040. }
  2041. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree4"]) == true)
  2042. {
  2043. cFree4 = "";
  2044. }
  2045. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree5"]) == true)
  2046. {
  2047. cFree5 = "";
  2048. }
  2049. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree6"]) == true)
  2050. {
  2051. cFree6 = "";
  2052. }
  2053. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree7"]) == true)
  2054. {
  2055. cFree7 = "";
  2056. }
  2057. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree8"]) == true)
  2058. {
  2059. cFree8 = "";
  2060. }
  2061. }
  2062. else
  2063. {
  2064. throw new Exception("存货编码:" + cInvCode + "不存在");
  2065. }
  2066. #endregion
  2067. #region 1 取得物料的itemID
  2068. string sql = @"
  2069. IF NOT EXISTS(SELECT Id FROM dbo.SCM_Item
  2070. WHERE cinvcode = '{0}' and cFree1 = '{1}' and cFree2 = '{2}' and cFree3 = '{3}' and cFree4 = '{4}' and cFree5 = '{5}' and cFree6 = '{6}' and cFree7 = '{7}' and cFree8 = '{8}')
  2071. BEGIN INSERT INTO dbo.SCM_Item(cInvCode ,cFree1 ,cFree2 ,cFree3 ,cFree4 , cFree5 , cFree6 ,cFree7 ,cFree8 ,cFree9 ,cFree10 ,PartId) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','','',0) END
  2072. SELECT Id FROM dbo.SCM_Item WHERE cinvcode = '{0}' and cFree1 = '{1}' and cFree2 = '{2}' and cFree3 = '{3}' and cFree4 = '{4}' and cFree5 = '{5}' and cFree6 = '{6}' and cFree7 = '{7}' and cFree8 = '{8}' ";
  2073. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8);
  2074. cmd.CommandText = sql;
  2075. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  2076. if (dtItem.Rows.Count == 0)
  2077. {
  2078. throw new Exception("物料的ItemID取得失败");
  2079. }
  2080. int ItemID = int.Parse(dtItem.Rows[0]["Id"].ToString());
  2081. log.Info("取得物料的itemID" + sql);
  2082. #endregion
  2083. #region 2 更新失败,插入现存量
  2084. sql = @"SELECT AutoID FROM dbo.CurrentStock
  2085. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  2086. AND ItemId = @ItemId and cFree1 = @cFree1 and cFree2 = @cFree2 and cFree3 = @cFree3 and cFree4 = @cFree4
  2087. and cFree5 = @cFree5 and cFree6 = @cFree6 and cFree7 = @cFree7 and cFree8 = @cFree8 ";
  2088. if (cBatch != null)
  2089. {
  2090. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  2091. sql += " and cBatch='" + cBatch + "' ";
  2092. else
  2093. sql += " and cBatch='' ";
  2094. }
  2095. else
  2096. {
  2097. sql += " and cBatch='' ";
  2098. }
  2099. //if (U8Helper.bInvQuality(cInvCode, cmd) == true)
  2100. // sql += " AND CONVERT(DATE, dMdate) = CONVERT(DATE, '" + dMdate + "') AND CONVERT(DATE, dVDate) = CONVERT(DATE, '" + dVDate + "') ";
  2101. cmd.CommandText = sql;
  2102. cmd.Parameters.Clear();
  2103. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  2104. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  2105. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  2106. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  2107. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  2108. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  2109. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  2110. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  2111. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  2112. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  2113. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  2114. DataTable dtCurrentStock = DBHelper.SQlReturnData(sql, cmd);
  2115. log.Info("查找现存量:" + sql);
  2116. if (dtCurrentStock != null && dtCurrentStock.Rows.Count > 0)
  2117. {
  2118. sql = @"UPDATE dbo.CurrentStock SET iQuantity = iQuantity + @iQuantity, fInQuantity = fInQuantity + @fInQuantity, fOutQuantity = fOutQuantity + @fOutQuantity
  2119. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  2120. AND ItemId = @ItemId and cFree1 = @cFree1 and cFree2 = @cFree2 and cFree3 = @cFree3 and cFree4 = @cFree4
  2121. and cFree5 = @cFree5 and cFree6 = @cFree6 and cFree7 = @cFree7 and cFree8 = @cFree8";
  2122. if (cBatch != null)
  2123. {
  2124. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  2125. sql += " and cBatch='" + cBatch + "' ";
  2126. else
  2127. sql += " and cBatch='' ";
  2128. }
  2129. else
  2130. {
  2131. sql += " and cBatch='' ";
  2132. }
  2133. //if (U8Helper.bInvQuality(cInvCode, cmd) == true)
  2134. // sql += " AND CONVERT(DATE, dMdate) = CONVERT(DATE, '" + dMdate + "') AND CONVERT(DATE, dVDate) = CONVERT(DATE, '" + dVDate + "') ";
  2135. cmd.CommandText = sql;
  2136. cmd.Parameters.Clear();
  2137. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  2138. cmd.Parameters.Add(new SqlParameter("@fInQuantity", (isOutQuantity && iQuantity >= 0) ? -iQuantity : 0));
  2139. cmd.Parameters.Add(new SqlParameter("@fOutQuantity", (isOutQuantity && iQuantity <= 0) ? iQuantity : 0));
  2140. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  2141. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  2142. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  2143. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  2144. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  2145. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  2146. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  2147. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  2148. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  2149. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  2150. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  2151. CmdExecuteNonQuery(sql, cmd, "更新现存量失败!");
  2152. log.Info("现存量更新:" + sql);
  2153. }
  2154. else
  2155. {
  2156. sql = @" INSERT INTO dbo.CurrentStock
  2157. (cWhCode,cInvCode,ItemId,cBatch,iSoType,iSodid,iQuantity,
  2158. iNum,fOutQuantity,fOutNum,fInQuantity,fInNum,
  2159. bStopFlag,fTransInQuantity,fTransInNum,
  2160. fTransOutQuantity,fTransOutNum,fPlanQuantity,fPlanNum,fDisableQuantity,
  2161. fDisableNum,fAvaQuantity,fAvaNum,BGSPSTOP,fStopQuantity,
  2162. fStopNum,ipeqty,ipenum,
  2163. cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, dMdate, dVDate, iMassDate, cMassUnit)
  2164. SELECT @cWhCode,@cInvCode,@ItemId,@cBatch,'0','',@iQuantity,
  2165. '0',@fOutQuantity,'0',@fInQuantity,'0',
  2166. '0','0','0','0','0','0','0','0',
  2167. '0','0','0','0','0','0','0','0',
  2168. @cFree1, @cFree2, @cFree3, @cFree4, @cFree5, @cFree6, @cFree7, @cFree8, @dMdate, @dVDate, @iMassDate, @cMassUnit";
  2169. cmd.CommandText = sql;
  2170. cmd.Parameters.Clear();
  2171. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  2172. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  2173. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  2174. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  2175. cmd.Parameters.Add(new SqlParameter("@fInQuantity", (isOutQuantity && iQuantity >= 0) ? -iQuantity : 0));
  2176. cmd.Parameters.Add(new SqlParameter("@fOutQuantity", (isOutQuantity && iQuantity <= 0) ? iQuantity : 0));
  2177. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  2178. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  2179. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  2180. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  2181. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  2182. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  2183. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  2184. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  2185. if (DBHelper.bInvQuality(cInvCode, cmd) == true)
  2186. {
  2187. #region
  2188. int iMassDate = 0;
  2189. int cMassUnit = 0;
  2190. string sqlCost = @" SELECT iMassDate, cMassUnit FROM Inventory WHERE cInvCode = '" + cInvCode + "' ";
  2191. DataTable dtINV = DBHelper.SQlReturnData(sqlCost, cmd);
  2192. if (dtINV != null && dtINV.Rows.Count > 0)
  2193. {
  2194. iMassDate = Convert.ToInt16(dtINV.Rows[0]["iMassDate"].ToString());
  2195. cMassUnit = Convert.ToInt16(dtINV.Rows[0]["cMassUnit"].ToString());
  2196. }
  2197. #endregion
  2198. cmd.Parameters.Add(new SqlParameter("@dMdate", dMdate));
  2199. cmd.Parameters.Add(new SqlParameter("@dVDate", dVDate));
  2200. cmd.Parameters.Add(new SqlParameter("@iMassDate", iMassDate));
  2201. cmd.Parameters.Add(new SqlParameter("@cMassUnit", cMassUnit));
  2202. }
  2203. else
  2204. {
  2205. cmd.Parameters.Add(new SqlParameter("@dMdate", DBNull.Value));
  2206. cmd.Parameters.Add(new SqlParameter("@dVDate", DBNull.Value));
  2207. cmd.Parameters.Add(new SqlParameter("@iMassDate", DBNull.Value));
  2208. cmd.Parameters.Add(new SqlParameter("@cMassUnit", DBNull.Value));
  2209. }
  2210. if (cBatch != null)
  2211. {
  2212. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  2213. cmd.Parameters.Add(new SqlParameter("@cBatch", cBatch));
  2214. else
  2215. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  2216. }
  2217. else
  2218. {
  2219. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  2220. }
  2221. CmdExecuteNonQuery(sql, cmd, "插入现存量失败!");
  2222. log.Info("现存量插入:" + sql);
  2223. }
  2224. #endregion
  2225. #region 判断是否存在负库存
  2226. sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock
  2227. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  2228. AND ItemId = @ItemId and cFree1 = @cFree1 and cFree2 = @cFree2 and cFree3 = @cFree3 and cFree4 = @cFree4
  2229. and cFree5 = @cFree5 and cFree6 = @cFree6 and cFree7 = @cFree7 and cFree8 = @cFree8
  2230. AND ( iQuantity < 0 OR iNum<0 OR fInQuantity<0 OR fOutQuantity<0 )";
  2231. if (cBatch != null)
  2232. {
  2233. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  2234. sql += " and cBatch='" + cBatch + "' ";
  2235. else
  2236. sql += " and cBatch='' ";
  2237. }
  2238. else
  2239. {
  2240. sql += " and cBatch='' ";
  2241. }
  2242. sql += @" )
  2243. BEGIN
  2244. RAISERROR('ERP库存不足', 16, 1)
  2245. END ";
  2246. cmd.CommandText = sql;
  2247. cmd.Parameters.Clear();
  2248. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  2249. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  2250. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  2251. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  2252. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  2253. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  2254. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  2255. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  2256. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  2257. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  2258. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  2259. DBHelper.SQlReturnData(sql, cmd);
  2260. //CmdExecuteNonQuery(sql, cmd, "库存不足,不能执行该操作!");
  2261. #endregion
  2262. #region 插入UserDefine
  2263. if (cFree1 != "")
  2264. {
  2265. sql = @"
  2266. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 20 AND cValue = '{0}')
  2267. BEGIN
  2268. INSERT INTO UserDefine
  2269. (cID, cValue)
  2270. VALUES (20, '{0}') END ";
  2271. sql = string.Format(sql, cFree1);
  2272. cmd.CommandText = sql;
  2273. cmd.ExecuteNonQuery();
  2274. }
  2275. if (cFree2 != "")
  2276. {
  2277. sql = @"
  2278. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 21 AND cValue = '{0}')
  2279. BEGIN
  2280. INSERT INTO UserDefine
  2281. (cID, cValue)
  2282. VALUES (21, '{0}') END ";
  2283. sql = string.Format(sql, cFree2);
  2284. cmd.CommandText = sql;
  2285. cmd.ExecuteNonQuery();
  2286. }
  2287. if (cFree3 != "")
  2288. {
  2289. sql = @"
  2290. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 28 AND cValue = '{0}')
  2291. BEGIN
  2292. INSERT INTO UserDefine
  2293. (cID, cValue)
  2294. VALUES (28, '{0}') END ";
  2295. sql = string.Format(sql, cFree3);
  2296. cmd.CommandText = sql;
  2297. cmd.ExecuteNonQuery();
  2298. }
  2299. if (cFree4 != "")
  2300. {
  2301. sql = @"
  2302. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 29 AND cValue = '{0}')
  2303. BEGIN
  2304. INSERT INTO UserDefine
  2305. (cID, cValue)
  2306. VALUES (29, '{0}') END ";
  2307. sql = string.Format(sql, cFree4);
  2308. cmd.CommandText = sql;
  2309. cmd.ExecuteNonQuery();
  2310. }
  2311. if (cFree5 != "")
  2312. {
  2313. sql = @"
  2314. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 30 AND cValue = '{0}')
  2315. BEGIN
  2316. INSERT INTO UserDefine
  2317. (cID, cValue)
  2318. VALUES (30, '{0}') END ";
  2319. sql = string.Format(sql, cFree5);
  2320. cmd.CommandText = sql;
  2321. cmd.ExecuteNonQuery();
  2322. }
  2323. if (cFree6 != "")
  2324. {
  2325. sql = @"
  2326. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 31 AND cValue = '{0}')
  2327. BEGIN
  2328. INSERT INTO UserDefine
  2329. (cID, cValue)
  2330. VALUES (31, '{0}') END ";
  2331. sql = string.Format(sql, cFree6);
  2332. cmd.CommandText = sql;
  2333. cmd.ExecuteNonQuery();
  2334. }
  2335. if (cFree7 != "")
  2336. {
  2337. sql = @"
  2338. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 32 AND cValue = '{0}')
  2339. BEGIN
  2340. INSERT INTO UserDefine
  2341. (cID, cValue)
  2342. VALUES (32, '{0}') END ";
  2343. sql = string.Format(sql, cFree7);
  2344. cmd.CommandText = sql;
  2345. cmd.ExecuteNonQuery();
  2346. }
  2347. if (cFree8 != "")
  2348. {
  2349. sql = @"
  2350. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 33 AND cValue = '{0}')
  2351. BEGIN
  2352. INSERT INTO UserDefine
  2353. (cID, cValue)
  2354. VALUES (33, '{0}') END ";
  2355. sql = string.Format(sql, cFree8);
  2356. cmd.CommandText = sql;
  2357. cmd.ExecuteNonQuery();
  2358. }
  2359. #endregion
  2360. #region 3 写入记账表
  2361. //log.Debug(key.TableName+"------------" + key.ID + "------------" + key.DID);
  2362. if (DBHelper.bInCost(cWhCode, cmd) == true)
  2363. {
  2364. sql = @"SELECT IDUN, IDSUN FROM [dbo].[" + key.TableName + @"]
  2365. WHERE IDUN = @IDUN AND IDSUN = @IDSUN";
  2366. cmd.CommandText = sql;
  2367. cmd.Parameters.Clear();
  2368. cmd.Parameters.Add(new SqlParameter("@IDUN", key.ID));
  2369. cmd.Parameters.Add(new SqlParameter("@IDSUN", key.DID));
  2370. DataTable IA_ST = DBHelper.SQlReturnData(sql, cmd);
  2371. if (IA_ST != null && IA_ST.Rows.Count > 0)
  2372. {
  2373. log.Debug(key.TableName + "------------" + key.ID + "------------" + key.DID + " 已存在!");
  2374. }
  2375. else
  2376. {
  2377. sql = string.Format(@" INSERT INTO [dbo].[" + key.TableName + @"]
  2378. SELECT '{0}','{1}','{2}','{3}'", key.ID, key.DID, key.cVouchTypeUN, key.cBustypeUN);
  2379. cmd.CommandText = sql;
  2380. CmdExecuteNonQuery(sql, cmd, "采购入库单写入记账表失败!");
  2381. log.Debug(key.TableName + "------------" + key.ID + "------------" + key.DID + " 记账成功!");
  2382. }
  2383. }
  2384. else
  2385. {
  2386. log.Debug("仓库未启用记账:" + cWhCode);
  2387. }
  2388. #endregion
  2389. }
  2390. public static void UpdateCurrentStockTrans09(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, DateTime dVDate, DateTime dMdate, VouchKey key)
  2391. {
  2392. #region 自由项管控
  2393. //if (!U8Helper.bFree1(cInvCode, cmd))
  2394. //{
  2395. // cFree1 = "";
  2396. //}
  2397. //if (!U8Helper.bFree2(cInvCode, cmd))
  2398. //{
  2399. // cFree2 = "";
  2400. //}
  2401. //if (!U8Helper.bFree3(cInvCode, cmd))
  2402. //{
  2403. // cFree3 = "";
  2404. //}
  2405. //if (!U8Helper.bFree4(cInvCode, cmd))
  2406. //{
  2407. // cFree4 = "";
  2408. //}
  2409. //if (!U8Helper.bFree5(cInvCode, cmd))
  2410. //{
  2411. // cFree5 = "";
  2412. //}
  2413. //if (!U8Helper.bFree6(cInvCode, cmd))
  2414. //{
  2415. // cFree6 = "";
  2416. //}
  2417. //if (!U8Helper.bFree7(cInvCode, cmd))
  2418. //{
  2419. // cFree7 = "";
  2420. //}
  2421. //if (!U8Helper.bFree8(cInvCode, cmd))
  2422. //{
  2423. // cFree8 = "";
  2424. //}
  2425. DataTable dtFree = DBHelper.bFree(cInvCode, cmd);
  2426. if (dtFree.Rows.Count > 0 && dtFree != null)
  2427. {
  2428. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree1"]) == true)
  2429. {
  2430. cFree1 = "";
  2431. }
  2432. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree2"]) == true)
  2433. {
  2434. cFree2 = "";
  2435. }
  2436. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree3"]) == true)
  2437. {
  2438. cFree3 = "";
  2439. }
  2440. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree4"]) == true)
  2441. {
  2442. cFree4 = "";
  2443. }
  2444. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree5"]) == true)
  2445. {
  2446. cFree5 = "";
  2447. }
  2448. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree6"]) == true)
  2449. {
  2450. cFree6 = "";
  2451. }
  2452. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree7"]) == true)
  2453. {
  2454. cFree7 = "";
  2455. }
  2456. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree8"]) == true)
  2457. {
  2458. cFree8 = "";
  2459. }
  2460. }
  2461. else
  2462. {
  2463. throw new Exception("存货编码:" + cInvCode + "不存在");
  2464. }
  2465. #endregion
  2466. #region 1 取得物料的itemID
  2467. string sql = @"
  2468. IF NOT EXISTS(SELECT Id FROM dbo.SCM_Item
  2469. WHERE cinvcode = '{0}' and cFree1 = '{1}' and cFree2 = '{2}' and cFree3 = '{3}' and cFree4 = '{4}' and cFree5 = '{5}' and cFree6 = '{6}' and cFree7 = '{7}' and cFree8 = '{8}')
  2470. BEGIN INSERT INTO dbo.SCM_Item(cInvCode ,cFree1 ,cFree2 ,cFree3 ,cFree4 , cFree5 , cFree6 ,cFree7 ,cFree8 ,cFree9 ,cFree10 ,PartId) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','','',0) END
  2471. SELECT Id FROM dbo.SCM_Item WHERE cinvcode = '{0}' and cFree1 = '{1}' and cFree2 = '{2}' and cFree3 = '{3}' and cFree4 = '{4}' and cFree5 = '{5}' and cFree6 = '{6}' and cFree7 = '{7}' and cFree8 = '{8}' ";
  2472. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8);
  2473. cmd.CommandText = sql;
  2474. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  2475. if (dtItem.Rows.Count == 0)
  2476. {
  2477. throw new Exception("物料的ItemID取得失败");
  2478. }
  2479. int ItemID = int.Parse(dtItem.Rows[0]["Id"].ToString());
  2480. log.Info("取得物料的itemID" + sql);
  2481. #endregion
  2482. #region 2 更新失败,插入现存量
  2483. sql = @"SELECT AutoID FROM dbo.CurrentStock
  2484. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  2485. AND ItemId = @ItemId and cFree1 = @cFree1 and cFree2 = @cFree2 and cFree3 = @cFree3 and cFree4 = @cFree4
  2486. and cFree5 = @cFree5 and cFree6 = @cFree6 and cFree7 = @cFree7 and cFree8 = @cFree8 ";
  2487. if (cBatch != null)
  2488. {
  2489. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  2490. sql += " and cBatch='" + cBatch + "' ";
  2491. else
  2492. sql += " and cBatch='' ";
  2493. }
  2494. else
  2495. {
  2496. sql += " and cBatch='' ";
  2497. }
  2498. //if (U8Helper.bInvQuality(cInvCode, cmd) == true)
  2499. // sql += " AND CONVERT(DATE, dMdate) = CONVERT(DATE, '" + dMdate + "') AND CONVERT(DATE, dVDate) = CONVERT(DATE, '" + dVDate + "') ";
  2500. cmd.CommandText = sql;
  2501. cmd.Parameters.Clear();
  2502. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  2503. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  2504. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  2505. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  2506. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  2507. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  2508. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  2509. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  2510. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  2511. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  2512. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  2513. DataTable dtCurrentStock = DBHelper.SQlReturnData(sql, cmd);
  2514. log.Info("查找现存量:" + sql);
  2515. if (dtCurrentStock != null && dtCurrentStock.Rows.Count > 0)
  2516. {
  2517. sql = @"UPDATE dbo.CurrentStock SET iQuantity = iQuantity + @iQuantity, fTransOutQuantity = fTransOutQuantity + @fTransOutQuantity
  2518. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  2519. AND ItemId = @ItemId and cFree1 = @cFree1 and cFree2 = @cFree2 and cFree3 = @cFree3 and cFree4 = @cFree4
  2520. and cFree5 = @cFree5 and cFree6 = @cFree6 and cFree7 = @cFree7 and cFree8 = @cFree8";
  2521. if (cBatch != null)
  2522. {
  2523. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  2524. sql += " and cBatch='" + cBatch + "' ";
  2525. else
  2526. sql += " and cBatch='' ";
  2527. }
  2528. else
  2529. {
  2530. sql += " and cBatch='' ";
  2531. }
  2532. //if (U8Helper.bInvQuality(cInvCode, cmd) == true)
  2533. // sql += " AND CONVERT(DATE, dMdate) = CONVERT(DATE, '" + dMdate + "') AND CONVERT(DATE, dVDate) = CONVERT(DATE, '" + dVDate + "') ";
  2534. cmd.CommandText = sql;
  2535. cmd.Parameters.Clear();
  2536. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  2537. cmd.Parameters.Add(new SqlParameter("@fTransOutQuantity", -iQuantity));
  2538. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  2539. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  2540. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  2541. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  2542. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  2543. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  2544. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  2545. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  2546. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  2547. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  2548. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  2549. CmdExecuteNonQuery(sql, cmd, "更新现存量失败!");
  2550. log.Info("现存量更新:" + sql);
  2551. }
  2552. else
  2553. {
  2554. sql = @" INSERT INTO dbo.CurrentStock
  2555. (cWhCode,cInvCode,ItemId,cBatch,iSoType,iSodid,iQuantity,
  2556. iNum,fOutQuantity,fOutNum,fInQuantity,fInNum,
  2557. bStopFlag,fTransInQuantity,fTransInNum,
  2558. fTransOutQuantity,fTransOutNum,fPlanQuantity,fPlanNum,fDisableQuantity,
  2559. fDisableNum,fAvaQuantity,fAvaNum,BGSPSTOP,fStopQuantity,
  2560. fStopNum,ipeqty,ipenum,
  2561. cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, dMdate, dVDate, iMassDate, cMassUnit)
  2562. SELECT @cWhCode,@cInvCode,@ItemId,@cBatch,'0','',@iQuantity,
  2563. '0','0','0','0','0',
  2564. '0','0','0',
  2565. @fTransOutQuantity,'0','0','0','0',
  2566. '0','0','0','0','0',
  2567. '0','0','0',
  2568. @cFree1, @cFree2, @cFree3, @cFree4, @cFree5, @cFree6, @cFree7, @cFree8, @dMdate, @dVDate, @iMassDate, @cMassUnit";
  2569. cmd.CommandText = sql;
  2570. cmd.Parameters.Clear();
  2571. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  2572. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  2573. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  2574. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  2575. cmd.Parameters.Add(new SqlParameter("@fTransOutQuantity", -iQuantity));
  2576. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  2577. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  2578. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  2579. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  2580. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  2581. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  2582. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  2583. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  2584. if (DBHelper.bInvQuality(cInvCode, cmd) == true)
  2585. {
  2586. #region
  2587. int iMassDate = 0;
  2588. int cMassUnit = 0;
  2589. string sqlCost = @" SELECT iMassDate, cMassUnit FROM Inventory WHERE 1=1 AND cInvCode = '" + cInvCode + "' ";
  2590. DataTable dtINV = DBHelper.SQlReturnData(sqlCost, cmd);
  2591. if (dtINV != null && dtINV.Rows.Count > 0)
  2592. {
  2593. iMassDate = Convert.ToInt16(dtINV.Rows[0]["iMassDate"].ToString());
  2594. cMassUnit = Convert.ToInt16(dtINV.Rows[0]["cMassUnit"].ToString());
  2595. }
  2596. #endregion
  2597. cmd.Parameters.Add(new SqlParameter("@dMdate", dMdate));
  2598. cmd.Parameters.Add(new SqlParameter("@dVDate", dVDate));
  2599. cmd.Parameters.Add(new SqlParameter("@iMassDate", iMassDate));
  2600. cmd.Parameters.Add(new SqlParameter("@cMassUnit", cMassUnit));
  2601. }
  2602. else
  2603. {
  2604. cmd.Parameters.Add(new SqlParameter("@dMdate", DBNull.Value));
  2605. cmd.Parameters.Add(new SqlParameter("@dVDate", DBNull.Value));
  2606. cmd.Parameters.Add(new SqlParameter("@iMassDate", DBNull.Value));
  2607. cmd.Parameters.Add(new SqlParameter("@cMassUnit", DBNull.Value));
  2608. }
  2609. if (cBatch != null)
  2610. {
  2611. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  2612. cmd.Parameters.Add(new SqlParameter("@cBatch", cBatch));
  2613. else
  2614. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  2615. }
  2616. else
  2617. {
  2618. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  2619. }
  2620. CmdExecuteNonQuery(sql, cmd, "插入现存量失败!");
  2621. log.Info("现存量插入:" + sql);
  2622. }
  2623. #endregion
  2624. #region 插入UserDefine
  2625. if (cFree1 != "")
  2626. {
  2627. sql = @"
  2628. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 20 AND cValue = '{0}')
  2629. BEGIN
  2630. INSERT INTO UserDefine
  2631. (cID, cValue)
  2632. VALUES (20, '{0}') END ";
  2633. sql = string.Format(sql, cFree1);
  2634. cmd.CommandText = sql;
  2635. cmd.ExecuteNonQuery();
  2636. }
  2637. if (cFree2 != "")
  2638. {
  2639. sql = @"
  2640. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 21 AND cValue = '{0}')
  2641. BEGIN
  2642. INSERT INTO UserDefine
  2643. (cID, cValue)
  2644. VALUES (21, '{0}') END ";
  2645. sql = string.Format(sql, cFree2);
  2646. cmd.CommandText = sql;
  2647. cmd.ExecuteNonQuery();
  2648. }
  2649. if (cFree3 != "")
  2650. {
  2651. sql = @"
  2652. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 28 AND cValue = '{0}')
  2653. BEGIN
  2654. INSERT INTO UserDefine
  2655. (cID, cValue)
  2656. VALUES (28, '{0}') END ";
  2657. sql = string.Format(sql, cFree3);
  2658. cmd.CommandText = sql;
  2659. cmd.ExecuteNonQuery();
  2660. }
  2661. if (cFree4 != "")
  2662. {
  2663. sql = @"
  2664. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 29 AND cValue = '{0}')
  2665. BEGIN
  2666. INSERT INTO UserDefine
  2667. (cID, cValue)
  2668. VALUES (29, '{0}') END ";
  2669. sql = string.Format(sql, cFree4);
  2670. cmd.CommandText = sql;
  2671. cmd.ExecuteNonQuery();
  2672. }
  2673. if (cFree5 != "")
  2674. {
  2675. sql = @"
  2676. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 30 AND cValue = '{0}')
  2677. BEGIN
  2678. INSERT INTO UserDefine
  2679. (cID, cValue)
  2680. VALUES (30, '{0}') END ";
  2681. sql = string.Format(sql, cFree5);
  2682. cmd.CommandText = sql;
  2683. cmd.ExecuteNonQuery();
  2684. }
  2685. if (cFree6 != "")
  2686. {
  2687. sql = @"
  2688. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 31 AND cValue = '{0}')
  2689. BEGIN
  2690. INSERT INTO UserDefine
  2691. (cID, cValue)
  2692. VALUES (31, '{0}') END ";
  2693. sql = string.Format(sql, cFree6);
  2694. cmd.CommandText = sql;
  2695. cmd.ExecuteNonQuery();
  2696. }
  2697. if (cFree7 != "")
  2698. {
  2699. sql = @"
  2700. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 32 AND cValue = '{0}')
  2701. BEGIN
  2702. INSERT INTO UserDefine
  2703. (cID, cValue)
  2704. VALUES (32, '{0}') END ";
  2705. sql = string.Format(sql, cFree7);
  2706. cmd.CommandText = sql;
  2707. cmd.ExecuteNonQuery();
  2708. }
  2709. if (cFree8 != "")
  2710. {
  2711. sql = @"
  2712. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 33 AND cValue = '{0}')
  2713. BEGIN
  2714. INSERT INTO UserDefine
  2715. (cID, cValue)
  2716. VALUES (33, '{0}') END ";
  2717. sql = string.Format(sql, cFree8);
  2718. cmd.CommandText = sql;
  2719. cmd.ExecuteNonQuery();
  2720. }
  2721. #endregion
  2722. #region 3 写入记账表
  2723. if (DBHelper.bInCost(cWhCode, cmd) == true)
  2724. {
  2725. sql = @"SELECT IDUN, IDSUN FROM [dbo].[" + key.TableName + @"]
  2726. WHERE IDUN = @IDUN AND IDSUN = @IDSUN";
  2727. cmd.CommandText = sql;
  2728. cmd.Parameters.Clear();
  2729. cmd.Parameters.Add(new SqlParameter("@IDUN", key.ID));
  2730. cmd.Parameters.Add(new SqlParameter("@IDSUN", key.DID));
  2731. DataTable IA_ST = DBHelper.SQlReturnData(sql, cmd);
  2732. if (IA_ST != null && IA_ST.Rows.Count > 0)
  2733. {
  2734. }
  2735. else
  2736. {
  2737. sql = string.Format(@" INSERT INTO [dbo].[" + key.TableName + @"]
  2738. SELECT '{0}','{1}','{2}','{3}'", key.ID, key.DID, key.cVouchTypeUN, key.cBustypeUN);
  2739. cmd.CommandText = sql;
  2740. CmdExecuteNonQuery(sql, cmd, "采购入库单写入记账表失败!");
  2741. }
  2742. }
  2743. #endregion
  2744. }
  2745. public static void UpdateCurrentStockTrans08(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, DateTime dVDate, DateTime dMdate, VouchKey key)
  2746. {
  2747. #region 自由项管控
  2748. //if (!U8Helper.bFree1(cInvCode, cmd))
  2749. //{
  2750. // cFree1 = "";
  2751. //}
  2752. //if (!U8Helper.bFree2(cInvCode, cmd))
  2753. //{
  2754. // cFree2 = "";
  2755. //}
  2756. //if (!U8Helper.bFree3(cInvCode, cmd))
  2757. //{
  2758. // cFree3 = "";
  2759. //}
  2760. //if (!U8Helper.bFree4(cInvCode, cmd))
  2761. //{
  2762. // cFree4 = "";
  2763. //}
  2764. //if (!U8Helper.bFree5(cInvCode, cmd))
  2765. //{
  2766. // cFree5 = "";
  2767. //}
  2768. //if (!U8Helper.bFree6(cInvCode, cmd))
  2769. //{
  2770. // cFree6 = "";
  2771. //}
  2772. //if (!U8Helper.bFree7(cInvCode, cmd))
  2773. //{
  2774. // cFree7 = "";
  2775. //}
  2776. //if (!U8Helper.bFree8(cInvCode, cmd))
  2777. //{
  2778. // cFree8 = "";
  2779. //}
  2780. DataTable dtFree = DBHelper.bFree(cInvCode, cmd);
  2781. if (dtFree.Rows.Count > 0 && dtFree != null)
  2782. {
  2783. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree1"]) == true)
  2784. {
  2785. cFree1 = "";
  2786. }
  2787. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree2"]) == true)
  2788. {
  2789. cFree2 = "";
  2790. }
  2791. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree3"]) == true)
  2792. {
  2793. cFree3 = "";
  2794. }
  2795. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree4"]) == true)
  2796. {
  2797. cFree4 = "";
  2798. }
  2799. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree5"]) == true)
  2800. {
  2801. cFree5 = "";
  2802. }
  2803. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree6"]) == true)
  2804. {
  2805. cFree6 = "";
  2806. }
  2807. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree7"]) == true)
  2808. {
  2809. cFree7 = "";
  2810. }
  2811. if (!Convert.ToBoolean(dtFree.Rows[0]["bFree8"]) == true)
  2812. {
  2813. cFree8 = "";
  2814. }
  2815. }
  2816. else
  2817. {
  2818. throw new Exception("存货编码:" + cInvCode + "不存在");
  2819. }
  2820. #endregion
  2821. #region 1 取得物料的itemID
  2822. string sql = @"
  2823. IF NOT EXISTS(SELECT Id FROM dbo.SCM_Item
  2824. WHERE cinvcode = '{0}' and cFree1 = '{1}' and cFree2 = '{2}' and cFree3 = '{3}' and cFree4 = '{4}' and cFree5 = '{5}' and cFree6 = '{6}' and cFree7 = '{7}' and cFree8 = '{8}')
  2825. BEGIN INSERT INTO dbo.SCM_Item(cInvCode ,cFree1 ,cFree2 ,cFree3 ,cFree4 , cFree5 , cFree6 ,cFree7 ,cFree8 ,cFree9 ,cFree10 ,PartId) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','','',0) END
  2826. SELECT Id FROM dbo.SCM_Item WHERE cinvcode = '{0}' and cFree1 = '{1}' and cFree2 = '{2}' and cFree3 = '{3}' and cFree4 = '{4}' and cFree5 = '{5}' and cFree6 = '{6}' and cFree7 = '{7}' and cFree8 = '{8}' ";
  2827. sql = string.Format(sql, cInvCode, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8);
  2828. cmd.CommandText = sql;
  2829. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  2830. if (dtItem.Rows.Count == 0)
  2831. {
  2832. throw new Exception("物料的ItemID取得失败");
  2833. }
  2834. int ItemID = int.Parse(dtItem.Rows[0]["Id"].ToString());
  2835. log.Info("取得物料的itemID" + sql);
  2836. #endregion
  2837. #region 2 更新失败,插入现存量
  2838. sql = @"SELECT AutoID FROM dbo.CurrentStock
  2839. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  2840. AND ItemId = @ItemId and cFree1 = @cFree1 and cFree2 = @cFree2 and cFree3 = @cFree3 and cFree4 = @cFree4
  2841. and cFree5 = @cFree5 and cFree6 = @cFree6 and cFree7 = @cFree7 and cFree8 = @cFree8 ";
  2842. if (cBatch != null)
  2843. {
  2844. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  2845. sql += " and cBatch='" + cBatch + "' ";
  2846. else
  2847. sql += " and cBatch='' ";
  2848. }
  2849. else
  2850. {
  2851. sql += " and cBatch='' ";
  2852. }
  2853. //if (U8Helper.bInvQuality(cInvCode, cmd) == true)
  2854. // sql += " AND CONVERT(DATE, dMdate) = CONVERT(DATE, '" + dMdate + "') AND CONVERT(DATE, dVDate) = CONVERT(DATE, '" + dVDate + "') ";
  2855. cmd.CommandText = sql;
  2856. cmd.Parameters.Clear();
  2857. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  2858. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  2859. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  2860. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  2861. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  2862. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  2863. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  2864. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  2865. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  2866. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  2867. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  2868. DataTable dtCurrentStock = DBHelper.SQlReturnData(sql, cmd);
  2869. log.Info("查找现存量:" + sql);
  2870. if (dtCurrentStock != null && dtCurrentStock.Rows.Count > 0)
  2871. {
  2872. sql = @"UPDATE dbo.CurrentStock SET iQuantity = iQuantity + @iQuantity, fTransInQuantity = fTransInQuantity + @fTransInQuantity
  2873. WHERE cWhCode = @cWhCode AND cInvCode = @cInvCode
  2874. AND ItemId = @ItemId and cFree1 = @cFree1 and cFree2 = @cFree2 and cFree3 = @cFree3 and cFree4 = @cFree4
  2875. and cFree5 = @cFree5 and cFree6 = @cFree6 and cFree7 = @cFree7 and cFree8 = @cFree8";
  2876. if (cBatch != null)
  2877. {
  2878. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  2879. sql += " and cBatch='" + cBatch + "' ";
  2880. else
  2881. sql += " and cBatch='' ";
  2882. }
  2883. else
  2884. {
  2885. sql += " and cBatch='' ";
  2886. }
  2887. //if (U8Helper.bInvQuality(cInvCode, cmd) == true)
  2888. // sql += " AND CONVERT(DATE, dMdate) = CONVERT(DATE, '" + dMdate + "') AND CONVERT(DATE, dVDate) = CONVERT(DATE, '" + dVDate + "') ";
  2889. cmd.CommandText = sql;
  2890. cmd.Parameters.Clear();
  2891. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  2892. cmd.Parameters.Add(new SqlParameter("@fTransInQuantity", -iQuantity));
  2893. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  2894. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  2895. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  2896. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  2897. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  2898. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  2899. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  2900. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  2901. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  2902. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  2903. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  2904. CmdExecuteNonQuery(sql, cmd, "更新现存量失败!");
  2905. log.Info("现存量更新:" + sql);
  2906. }
  2907. else
  2908. {
  2909. sql = @" INSERT INTO dbo.CurrentStock
  2910. (cWhCode,cInvCode,ItemId,cBatch,iSoType,iSodid,iQuantity,
  2911. iNum,fOutQuantity,fOutNum,fInQuantity,fInNum,
  2912. bStopFlag,fTransInQuantity,fTransInNum,
  2913. fTransOutQuantity,fTransOutNum,fPlanQuantity,fPlanNum,fDisableQuantity,
  2914. fDisableNum,fAvaQuantity,fAvaNum,BGSPSTOP,fStopQuantity,
  2915. fStopNum,ipeqty,ipenum,
  2916. cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, dMdate, dVDate, iMassDate, cMassUnit)
  2917. SELECT @cWhCode,@cInvCode,@ItemId,@cBatch,'0','',@iQuantity,
  2918. '0','0','0','0','0',
  2919. '0',@fTransInQuantity,'0',
  2920. '0','0','0','0','0',
  2921. '0','0','0','0','0',
  2922. '0','0','0',
  2923. @cFree1, @cFree2, @cFree3, @cFree4, @cFree5, @cFree6, @cFree7, @cFree8, @dMdate, @dVDate, @iMassDate, @cMassUnit";
  2924. cmd.CommandText = sql;
  2925. cmd.Parameters.Clear();
  2926. cmd.Parameters.Add(new SqlParameter("@cWhCode", cWhCode));
  2927. cmd.Parameters.Add(new SqlParameter("@cInvCode", cInvCode));
  2928. cmd.Parameters.Add(new SqlParameter("@ItemId", ItemID));
  2929. cmd.Parameters.Add(new SqlParameter("@iQuantity", iQuantity));
  2930. cmd.Parameters.Add(new SqlParameter("@fTransInQuantity", -iQuantity));
  2931. cmd.Parameters.Add(new SqlParameter("@cFree1", cFree1));
  2932. cmd.Parameters.Add(new SqlParameter("@cFree2", cFree2));
  2933. cmd.Parameters.Add(new SqlParameter("@cFree3", cFree3));
  2934. cmd.Parameters.Add(new SqlParameter("@cFree4", cFree4));
  2935. cmd.Parameters.Add(new SqlParameter("@cFree5", cFree5));
  2936. cmd.Parameters.Add(new SqlParameter("@cFree6", cFree6));
  2937. cmd.Parameters.Add(new SqlParameter("@cFree7", cFree7));
  2938. cmd.Parameters.Add(new SqlParameter("@cFree8", cFree8));
  2939. if (DBHelper.bInvQuality(cInvCode, cmd) == true)
  2940. {
  2941. #region
  2942. int iMassDate = 0;
  2943. int cMassUnit = 0;
  2944. string sqlCost = @" SELECT iMassDate, cMassUnit FROM Inventory WHERE 1=1 AND cInvCode = '" + cInvCode + "' ";
  2945. DataTable dtINV = DBHelper.SQlReturnData(sqlCost, cmd);
  2946. if (dtINV != null && dtINV.Rows.Count > 0)
  2947. {
  2948. iMassDate = Convert.ToInt16(dtINV.Rows[0]["iMassDate"].ToString());
  2949. cMassUnit = Convert.ToInt16(dtINV.Rows[0]["cMassUnit"].ToString());
  2950. }
  2951. #endregion
  2952. cmd.Parameters.Add(new SqlParameter("@dMdate", dMdate));
  2953. cmd.Parameters.Add(new SqlParameter("@dVDate", dVDate));
  2954. cmd.Parameters.Add(new SqlParameter("@iMassDate", iMassDate));
  2955. cmd.Parameters.Add(new SqlParameter("@cMassUnit", cMassUnit));
  2956. }
  2957. else
  2958. {
  2959. cmd.Parameters.Add(new SqlParameter("@dMdate", DBNull.Value));
  2960. cmd.Parameters.Add(new SqlParameter("@dVDate", DBNull.Value));
  2961. cmd.Parameters.Add(new SqlParameter("@iMassDate", DBNull.Value));
  2962. cmd.Parameters.Add(new SqlParameter("@cMassUnit", DBNull.Value));
  2963. }
  2964. if (cBatch != null)
  2965. {
  2966. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  2967. cmd.Parameters.Add(new SqlParameter("@cBatch", cBatch));
  2968. else
  2969. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  2970. }
  2971. else
  2972. {
  2973. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  2974. }
  2975. CmdExecuteNonQuery(sql, cmd, "插入现存量失败!");
  2976. log.Info("现存量插入:" + sql);
  2977. }
  2978. #endregion
  2979. #region 插入UserDefine
  2980. if (cFree1 != "")
  2981. {
  2982. sql = @"
  2983. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 20 AND cValue = '{0}')
  2984. BEGIN
  2985. INSERT INTO UserDefine
  2986. (cID, cValue)
  2987. VALUES (20, '{0}') END ";
  2988. sql = string.Format(sql, cFree1);
  2989. cmd.CommandText = sql;
  2990. cmd.ExecuteNonQuery();
  2991. }
  2992. if (cFree2 != "")
  2993. {
  2994. sql = @"
  2995. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 21 AND cValue = '{0}')
  2996. BEGIN
  2997. INSERT INTO UserDefine
  2998. (cID, cValue)
  2999. VALUES (21, '{0}') END ";
  3000. sql = string.Format(sql, cFree2);
  3001. cmd.CommandText = sql;
  3002. cmd.ExecuteNonQuery();
  3003. }
  3004. if (cFree3 != "")
  3005. {
  3006. sql = @"
  3007. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 28 AND cValue = '{0}')
  3008. BEGIN
  3009. INSERT INTO UserDefine
  3010. (cID, cValue)
  3011. VALUES (28, '{0}') END ";
  3012. sql = string.Format(sql, cFree3);
  3013. cmd.CommandText = sql;
  3014. cmd.ExecuteNonQuery();
  3015. }
  3016. if (cFree4 != "")
  3017. {
  3018. sql = @"
  3019. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 29 AND cValue = '{0}')
  3020. BEGIN
  3021. INSERT INTO UserDefine
  3022. (cID, cValue)
  3023. VALUES (29, '{0}') END ";
  3024. sql = string.Format(sql, cFree4);
  3025. cmd.CommandText = sql;
  3026. cmd.ExecuteNonQuery();
  3027. }
  3028. if (cFree5 != "")
  3029. {
  3030. sql = @"
  3031. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 30 AND cValue = '{0}')
  3032. BEGIN
  3033. INSERT INTO UserDefine
  3034. (cID, cValue)
  3035. VALUES (30, '{0}') END ";
  3036. sql = string.Format(sql, cFree5);
  3037. cmd.CommandText = sql;
  3038. cmd.ExecuteNonQuery();
  3039. }
  3040. if (cFree6 != "")
  3041. {
  3042. sql = @"
  3043. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 31 AND cValue = '{0}')
  3044. BEGIN
  3045. INSERT INTO UserDefine
  3046. (cID, cValue)
  3047. VALUES (31, '{0}') END ";
  3048. sql = string.Format(sql, cFree6);
  3049. cmd.CommandText = sql;
  3050. cmd.ExecuteNonQuery();
  3051. }
  3052. if (cFree7 != "")
  3053. {
  3054. sql = @"
  3055. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 32 AND cValue = '{0}')
  3056. BEGIN
  3057. INSERT INTO UserDefine
  3058. (cID, cValue)
  3059. VALUES (32, '{0}') END ";
  3060. sql = string.Format(sql, cFree7);
  3061. cmd.CommandText = sql;
  3062. cmd.ExecuteNonQuery();
  3063. }
  3064. if (cFree8 != "")
  3065. {
  3066. sql = @"
  3067. IF NOT EXISTS (SELECT * FROM UserDefine WHERE cID = 33 AND cValue = '{0}')
  3068. BEGIN
  3069. INSERT INTO UserDefine
  3070. (cID, cValue)
  3071. VALUES (33, '{0}') END ";
  3072. sql = string.Format(sql, cFree8);
  3073. cmd.CommandText = sql;
  3074. cmd.ExecuteNonQuery();
  3075. }
  3076. #endregion
  3077. #region 3 写入记账表
  3078. if (DBHelper.bInCost(cWhCode, cmd) == true)
  3079. {
  3080. sql = @"SELECT IDUN, IDSUN FROM [dbo].[" + key.TableName + @"]
  3081. WHERE IDUN = @IDUN AND IDSUN = @IDSUN";
  3082. cmd.CommandText = sql;
  3083. cmd.Parameters.Clear();
  3084. cmd.Parameters.Add(new SqlParameter("@IDUN", key.ID));
  3085. cmd.Parameters.Add(new SqlParameter("@IDSUN", key.DID));
  3086. DataTable IA_ST = DBHelper.SQlReturnData(sql, cmd);
  3087. if (IA_ST != null && IA_ST.Rows.Count > 0)
  3088. {
  3089. }
  3090. else
  3091. {
  3092. sql = string.Format(@" INSERT INTO [dbo].[" + key.TableName + @"]
  3093. SELECT '{0}','{1}','{2}','{3}'", key.ID, key.DID, key.cVouchTypeUN, key.cBustypeUN);
  3094. cmd.CommandText = sql;
  3095. CmdExecuteNonQuery(sql, cmd, "采购入库单写入记账表失败!");
  3096. }
  3097. }
  3098. #endregion
  3099. }
  3100. #endregion
  3101. /// <summary>
  3102. /// 判断是否启用批次管理
  3103. /// </summary>
  3104. /// <param name="cInvCode"></param>
  3105. /// <returns></returns>
  3106. public static bool bInvBatch(string cInvCode, SqlCommand cmd)
  3107. {
  3108. try
  3109. {
  3110. bool flag = false;
  3111. string sql = "SELECT bInvBatch FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  3112. DataTable dt = SQlReturnData(sql, cmd);
  3113. if (dt != null && dt.Rows.Count > 0)
  3114. {
  3115. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  3116. {
  3117. flag = true;
  3118. }
  3119. else
  3120. {
  3121. flag = false;
  3122. }
  3123. }
  3124. return flag;
  3125. }
  3126. catch (Exception ex)
  3127. {
  3128. throw new Exception(ex.Message);
  3129. }
  3130. }
  3131. /// <summary>
  3132. /// 判断是否启自由项管理
  3133. /// </summary>
  3134. /// <param name="cInvCode"></param>
  3135. /// <returns></returns>
  3136. public static bool bFree1(string cInvCode, SqlCommand cmd)
  3137. {
  3138. try
  3139. {
  3140. bool flag = false;
  3141. string sql = "SELECT bFree1 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  3142. DataTable dt = SQlReturnData(sql, cmd);
  3143. if (dt != null && dt.Rows.Count > 0)
  3144. {
  3145. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  3146. {
  3147. flag = true;
  3148. }
  3149. else
  3150. {
  3151. flag = false;
  3152. }
  3153. }
  3154. return flag;
  3155. }
  3156. catch (Exception ex)
  3157. {
  3158. throw new Exception(ex.Message);
  3159. }
  3160. }
  3161. public static bool bFree2(string cInvCode, SqlCommand cmd)
  3162. {
  3163. try
  3164. {
  3165. bool flag = false;
  3166. string sql = "SELECT bFree2 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  3167. DataTable dt = SQlReturnData(sql, cmd);
  3168. if (dt != null && dt.Rows.Count > 0)
  3169. {
  3170. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  3171. {
  3172. flag = true;
  3173. }
  3174. else
  3175. {
  3176. flag = false;
  3177. }
  3178. }
  3179. return flag;
  3180. }
  3181. catch (Exception ex)
  3182. {
  3183. throw new Exception(ex.Message);
  3184. }
  3185. }
  3186. public static bool bFree3(string cInvCode, SqlCommand cmd)
  3187. {
  3188. try
  3189. {
  3190. bool flag = false;
  3191. string sql = "SELECT bFree3 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  3192. DataTable dt = SQlReturnData(sql, cmd);
  3193. if (dt != null && dt.Rows.Count > 0)
  3194. {
  3195. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  3196. {
  3197. flag = true;
  3198. }
  3199. else
  3200. {
  3201. flag = false;
  3202. }
  3203. }
  3204. return flag;
  3205. }
  3206. catch (Exception ex)
  3207. {
  3208. throw new Exception(ex.Message);
  3209. }
  3210. }
  3211. public static bool bFree4(string cInvCode, SqlCommand cmd)
  3212. {
  3213. try
  3214. {
  3215. bool flag = false;
  3216. string sql = "SELECT bFree4 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  3217. DataTable dt = SQlReturnData(sql, cmd);
  3218. if (dt != null && dt.Rows.Count > 0)
  3219. {
  3220. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  3221. {
  3222. flag = true;
  3223. }
  3224. else
  3225. {
  3226. flag = false;
  3227. }
  3228. }
  3229. return flag;
  3230. }
  3231. catch (Exception ex)
  3232. {
  3233. throw new Exception(ex.Message);
  3234. }
  3235. }
  3236. public static bool bFree5(string cInvCode, SqlCommand cmd)
  3237. {
  3238. try
  3239. {
  3240. bool flag = false;
  3241. string sql = "SELECT bFree5 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  3242. DataTable dt = SQlReturnData(sql, cmd);
  3243. if (dt != null && dt.Rows.Count > 0)
  3244. {
  3245. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  3246. {
  3247. flag = true;
  3248. }
  3249. else
  3250. {
  3251. flag = false;
  3252. }
  3253. }
  3254. return flag;
  3255. }
  3256. catch (Exception ex)
  3257. {
  3258. throw new Exception(ex.Message);
  3259. }
  3260. }
  3261. public static bool bFree6(string cInvCode, SqlCommand cmd)
  3262. {
  3263. try
  3264. {
  3265. bool flag = false;
  3266. string sql = "SELECT bFree6 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  3267. DataTable dt = SQlReturnData(sql, cmd);
  3268. if (dt != null && dt.Rows.Count > 0)
  3269. {
  3270. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  3271. {
  3272. flag = true;
  3273. }
  3274. else
  3275. {
  3276. flag = false;
  3277. }
  3278. }
  3279. return flag;
  3280. }
  3281. catch (Exception ex)
  3282. {
  3283. throw new Exception(ex.Message);
  3284. }
  3285. }
  3286. public static bool bFree7(string cInvCode, SqlCommand cmd)
  3287. {
  3288. try
  3289. {
  3290. bool flag = false;
  3291. string sql = "SELECT bFree7 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  3292. DataTable dt = SQlReturnData(sql, cmd);
  3293. if (dt != null && dt.Rows.Count > 0)
  3294. {
  3295. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  3296. {
  3297. flag = true;
  3298. }
  3299. else
  3300. {
  3301. flag = false;
  3302. }
  3303. }
  3304. return flag;
  3305. }
  3306. catch (Exception ex)
  3307. {
  3308. throw new Exception(ex.Message);
  3309. }
  3310. }
  3311. public static bool bFree8(string cInvCode, SqlCommand cmd)
  3312. {
  3313. try
  3314. {
  3315. bool flag = false;
  3316. string sql = "SELECT bFree8 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  3317. DataTable dt = SQlReturnData(sql, cmd);
  3318. if (dt != null && dt.Rows.Count > 0)
  3319. {
  3320. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  3321. {
  3322. flag = true;
  3323. }
  3324. else
  3325. {
  3326. flag = false;
  3327. }
  3328. }
  3329. return flag;
  3330. }
  3331. catch (Exception ex)
  3332. {
  3333. throw new Exception(ex.Message);
  3334. }
  3335. }
  3336. public static DataTable bFree(string cInvCode, SqlCommand cmd)
  3337. {
  3338. try
  3339. {
  3340. string sql = "SELECT bFree1, bFree2, bFree3, bFree4, bFree5, bFree6, bFree7, bFree8, bFree9, bFree10 FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  3341. DataTable dt = SQlReturnData(sql, cmd);
  3342. return dt;
  3343. }
  3344. catch (Exception ex)
  3345. {
  3346. throw new Exception(ex.Message);
  3347. }
  3348. }
  3349. /// <summary>
  3350. /// 判断是否启用保质期管理
  3351. /// </summary>
  3352. /// <param name="cInvCode"></param>
  3353. /// <returns></returns>
  3354. public static bool bInvQuality(string cInvCode, SqlCommand cmd)
  3355. {
  3356. try
  3357. {
  3358. bool flag = false;
  3359. string sql = "SELECT bInvQuality FROM dbo.Inventory WHERE cInvCode='" + cInvCode + "'";
  3360. DataTable dt = SQlReturnData(sql, cmd);
  3361. if (dt != null && dt.Rows.Count > 0)
  3362. {
  3363. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  3364. {
  3365. flag = true;
  3366. }
  3367. else
  3368. {
  3369. flag = false;
  3370. }
  3371. }
  3372. return flag;
  3373. }
  3374. catch (Exception ex)
  3375. {
  3376. throw new Exception(ex.Message);
  3377. }
  3378. }
  3379. /// <summary>
  3380. /// 判断是否记账
  3381. /// </summary>
  3382. /// <param name="cInvCode"></param>
  3383. /// <returns></returns>
  3384. public static bool bInCost(string cWhCode, SqlCommand cmd)
  3385. {
  3386. try
  3387. {
  3388. bool flag = false;
  3389. string sql = "SELECT bInCost FROM Warehouse WHERE cWhCode ='" + cWhCode + "'";
  3390. DataTable dt = SQlReturnData(sql, cmd);
  3391. if (dt != null && dt.Rows.Count > 0)
  3392. {
  3393. if (Convert.ToBoolean(dt.Rows[0][0]) == true)
  3394. {
  3395. flag = true;
  3396. }
  3397. else
  3398. {
  3399. flag = false;
  3400. }
  3401. }
  3402. return flag;
  3403. }
  3404. catch (Exception ex)
  3405. {
  3406. throw new Exception(ex.Message);
  3407. }
  3408. }
  3409. /// <summary>
  3410. /// Convert a List{T} to a DataTable.
  3411. /// </summary>
  3412. public static DataTable ToDataTable<T>(List<T> items)
  3413. {
  3414. var tb = new DataTable(typeof(T).Name);
  3415. PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  3416. foreach (PropertyInfo prop in props)
  3417. {
  3418. Type t = GetCoreType(prop.PropertyType);
  3419. tb.Columns.Add(prop.Name, t);
  3420. }
  3421. foreach (T item in items)
  3422. {
  3423. var values = new object[props.Length];
  3424. for (int i = 0; i < props.Length; i++)
  3425. {
  3426. values[i] = props[i].GetValue(item, null);
  3427. }
  3428. tb.Rows.Add(values);
  3429. }
  3430. return tb;
  3431. }
  3432. /// <summary>
  3433. /// Determine of specified type is nullable
  3434. /// </summary>
  3435. public static bool IsNullable(Type t)
  3436. {
  3437. return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
  3438. }
  3439. /// <summary>
  3440. /// Return underlying type if type is Nullable otherwise return the type
  3441. /// </summary>
  3442. public static Type GetCoreType(Type t)
  3443. {
  3444. if (t != null && IsNullable(t))
  3445. {
  3446. if (!t.IsValueType)
  3447. {
  3448. return t;
  3449. }
  3450. else
  3451. {
  3452. return Nullable.GetUnderlyingType(t);
  3453. }
  3454. }
  3455. else
  3456. {
  3457. return t;
  3458. }
  3459. }
  3460. #region 原
  3461. 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)
  3462. {
  3463. DataTable dtNew = dt.Clone();
  3464. 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] };
  3465. foreach (DataRow row in dt.Rows)
  3466. {
  3467. if (string.IsNullOrEmpty(cBatch))
  3468. {
  3469. cBatch = "";
  3470. }
  3471. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  3472. {
  3473. row[cBatch] = "";
  3474. }
  3475. 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() });
  3476. if (srow == null)
  3477. {
  3478. dtNew.Rows.Add(row.ItemArray);
  3479. }
  3480. else
  3481. {
  3482. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  3483. }
  3484. }
  3485. return dtNew;
  3486. }
  3487. public static DataTable MergeDataTable(DataTable dt, string AuotID, string cBatch, string iQuantity, string cInvCode, SqlCommand cmd)
  3488. {
  3489. DataTable dtNew = dt.Clone();
  3490. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID], dtNew.Columns[cBatch] };
  3491. foreach (DataRow row in dt.Rows)
  3492. {
  3493. if (string.IsNullOrEmpty(cBatch))
  3494. {
  3495. cBatch = "";
  3496. }
  3497. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  3498. {
  3499. row[cBatch] = "";
  3500. }
  3501. DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString(), row[cBatch].ToString() });
  3502. if (srow == null)
  3503. {
  3504. dtNew.Rows.Add(row.ItemArray);
  3505. }
  3506. else
  3507. {
  3508. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  3509. }
  3510. }
  3511. return dtNew;
  3512. }
  3513. 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)
  3514. {
  3515. DataTable dtNew = dt.Clone();
  3516. 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] };
  3517. foreach (DataRow row in dt.Rows)
  3518. {
  3519. if (string.IsNullOrEmpty(cBatch))
  3520. {
  3521. cBatch = "";
  3522. }
  3523. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  3524. {
  3525. row[cBatch] = "";
  3526. }
  3527. 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() });
  3528. if (srow == null)
  3529. {
  3530. dtNew.Rows.Add(row.ItemArray);
  3531. }
  3532. else
  3533. {
  3534. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  3535. }
  3536. }
  3537. return dtNew;
  3538. }
  3539. public static DataTable MergeDataTableQC(DataTable dt, string AuotID, string cBatch, string iQuantity, string iNGQuantity, string cInvCode, SqlCommand cmd)
  3540. {
  3541. DataTable dtNew = dt.Clone();
  3542. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID], dtNew.Columns[cBatch] };
  3543. foreach (DataRow row in dt.Rows)
  3544. {
  3545. if (string.IsNullOrEmpty(cBatch))
  3546. {
  3547. cBatch = "";
  3548. }
  3549. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  3550. {
  3551. row[cBatch] = "";
  3552. }
  3553. DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString(), row[cBatch].ToString() });
  3554. if (srow == null)
  3555. {
  3556. dtNew.Rows.Add(row.ItemArray);
  3557. }
  3558. else
  3559. {
  3560. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  3561. srow[iNGQuantity] = decimal.Parse(srow[iNGQuantity].ToString()) + decimal.Parse(row[iNGQuantity].ToString());
  3562. }
  3563. }
  3564. return dtNew;
  3565. }
  3566. 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)
  3567. {
  3568. DataTable dtNew = dt.Clone();
  3569. 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] };
  3570. foreach (DataRow row in dt.Rows)
  3571. {
  3572. if (string.IsNullOrEmpty(cBatch))
  3573. {
  3574. cBatch = "";
  3575. }
  3576. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  3577. {
  3578. row[cBatch] = "";
  3579. }
  3580. 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() });
  3581. if (srow == null)
  3582. {
  3583. dtNew.Rows.Add(row.ItemArray);
  3584. }
  3585. else
  3586. {
  3587. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  3588. }
  3589. }
  3590. return dtNew;
  3591. }
  3592. 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)
  3593. {
  3594. DataTable dtNew = dt.Clone();
  3595. 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] };
  3596. foreach (DataRow row in dt.Rows)
  3597. {
  3598. if (string.IsNullOrEmpty(cBatch))
  3599. {
  3600. cBatch = "";
  3601. }
  3602. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  3603. {
  3604. row[cBatch] = "";
  3605. }
  3606. 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() });
  3607. if (srow == null)
  3608. {
  3609. dtNew.Rows.Add(row.ItemArray);
  3610. }
  3611. else
  3612. {
  3613. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  3614. }
  3615. }
  3616. return dtNew;
  3617. }
  3618. #endregion
  3619. #region 现
  3620. public static DataTable MergeDataTableX(DataTable dt, string AuotID, string cBatch, string iQuantity, string cInvCode, string iNum, SqlCommand cmd)
  3621. {
  3622. DataTable dtNew = dt.Clone();
  3623. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[cInvCode], dtNew.Columns[cBatch] };
  3624. foreach (DataRow row in dt.Rows)
  3625. {
  3626. if (string.IsNullOrEmpty(cBatch))
  3627. {
  3628. cBatch = "";
  3629. }
  3630. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  3631. {
  3632. row[cBatch] = "";
  3633. }
  3634. DataRow srow = dtNew.Rows.Find(new object[] { row[cInvCode].ToString(), row[cBatch].ToString() });
  3635. if (srow == null)
  3636. {
  3637. dtNew.Rows.Add(row.ItemArray);
  3638. }
  3639. else
  3640. {
  3641. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  3642. srow[iNum] = decimal.Parse(srow[iNum].ToString()) + decimal.Parse(row[iNum].ToString());
  3643. }
  3644. }
  3645. return dtNew;
  3646. }
  3647. public static DataTable MergeDataTable(DataTable dt, string AuotID, string cBatch, string iQuantity, string cInvCode, string iNum, SqlCommand cmd)
  3648. {
  3649. DataTable dtNew = dt.Clone();
  3650. //dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID], dtNew.Columns[cInvCode], dtNew.Columns[cBatch] };
  3651. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID], dtNew.Columns[cBatch] };//原
  3652. foreach (DataRow row in dt.Rows)
  3653. {
  3654. if (string.IsNullOrEmpty(cBatch))
  3655. {
  3656. cBatch = "";
  3657. }
  3658. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  3659. {
  3660. row[cBatch] = "";
  3661. }
  3662. DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString(), row[cBatch].ToString() });
  3663. //DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString(), dtNew.Columns[cInvCode], row[cBatch].ToString() });
  3664. if (srow == null)
  3665. {
  3666. dtNew.Rows.Add(row.ItemArray);
  3667. }
  3668. else
  3669. {
  3670. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  3671. srow[iNum] = decimal.Parse(srow[iNum].ToString()) + decimal.Parse(row[iNum].ToString());
  3672. }
  3673. }
  3674. return dtNew;
  3675. }
  3676. public static DataTable MergeRd09(DataTable dt, string cBatch, string iQuantity, string cInvCode, string iNum, SqlCommand cmd)
  3677. {
  3678. DataTable dtNew = dt.Clone();
  3679. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[cInvCode], dtNew.Columns[cBatch] };
  3680. foreach (DataRow row in dt.Rows)
  3681. {
  3682. if (string.IsNullOrEmpty(cBatch))
  3683. {
  3684. cBatch = "";
  3685. }
  3686. if (DBHelper.bInvBatch(row[cInvCode].ToString(), cmd) == false)
  3687. {
  3688. row[cBatch] = "";
  3689. }
  3690. DataRow srow = dtNew.Rows.Find(new object[] { row[cInvCode].ToString(), row[cBatch].ToString() });
  3691. if (srow == null)
  3692. {
  3693. dtNew.Rows.Add(row.ItemArray);
  3694. }
  3695. else
  3696. {
  3697. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  3698. srow[iNum] = decimal.Parse(srow[iNum].ToString()) + decimal.Parse(row[iNum].ToString());
  3699. }
  3700. }
  3701. return dtNew;
  3702. }
  3703. #endregion
  3704. public static IList<T> ConvertTo<T>(DataTable table)
  3705. {
  3706. if (table == null)
  3707. {
  3708. return null;
  3709. }
  3710. List<DataRow> rows = new List<DataRow>();
  3711. foreach (DataRow row in table.Rows)
  3712. {
  3713. rows.Add(row);
  3714. }
  3715. return ConvertTo<T>(rows);
  3716. }
  3717. public static IList<T> ConvertTo<T>(IList<DataRow> rows)
  3718. {
  3719. IList<T> list = null;
  3720. if (rows != null)
  3721. {
  3722. list = new List<T>();
  3723. foreach (DataRow row in rows)
  3724. {
  3725. T item = CreateItem<T>(row);
  3726. list.Add(item);
  3727. }
  3728. }
  3729. return list;
  3730. }
  3731. public static T CreateItem<T>(DataRow row)
  3732. {
  3733. T obj = default(T);
  3734. if (row != null)
  3735. {
  3736. obj = Activator.CreateInstance<T>();
  3737. foreach (DataColumn column in row.Table.Columns)
  3738. {
  3739. PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
  3740. try
  3741. {
  3742. object value = row[column.ColumnName];
  3743. prop.SetValue(obj, value, null);
  3744. }
  3745. catch
  3746. { //You can log something here
  3747. //throw;
  3748. }
  3749. }
  3750. }
  3751. return obj;
  3752. }
  3753. public static DataTable MergeDataTable(DataTable dt, string AuotID, string iQuantity, string cBatch)
  3754. {
  3755. DataTable dtNew = dt.Clone();
  3756. dtNew.PrimaryKey = new DataColumn[] { dtNew.Columns[AuotID] };
  3757. foreach (DataRow row in dt.Rows)
  3758. {
  3759. DataRow srow = dtNew.Rows.Find(new object[] { row[AuotID].ToString() });
  3760. if (srow == null)
  3761. {
  3762. dtNew.Rows.Add(row.ItemArray);
  3763. }
  3764. else
  3765. {
  3766. srow[iQuantity] = decimal.Parse(srow[iQuantity].ToString()) + decimal.Parse(row[iQuantity].ToString());
  3767. }
  3768. }
  3769. return dtNew;
  3770. }
  3771. #region 返回默认的出入库类别
  3772. /// <summary>
  3773. /// 返回默认的出入库类别
  3774. /// </summary>
  3775. /// <returns></returns>
  3776. public static string returnDefaultRdType(string VTID, string BTChName, SqlCommand cmd)
  3777. {
  3778. string sql = @"select VouchRdContrapose.cVRGUID,cVTChName,cBTChName,cVRRCode,R.cRdName,cVRSCode,S.cRdName
  3779. from VouchRdContrapose with(nolock)
  3780. left join vouchTypeDic with(nolock) on VouchRdContrapose.cVBTID=VouchTypeDic.cVBTID
  3781. left join Rd_Style as R with(nolock) On cVRRCode=R.cRdCode and R.bRDFlag=1
  3782. left join Rd_Style as S with(nolock) ON cVRSCode=S.cRdCode and S.bRDFlag=0
  3783. where 1=1 And (cVTID = N'{0}') And (cBTChName = N'{1}') order by cSerial ";
  3784. sql = string.Format(sql, VTID, BTChName);
  3785. DataTable dt = SQlReturnData(sql, cmd);
  3786. if (dt.Rows.Count == 0)
  3787. {
  3788. throw new Exception("倒冲材料出库单的出库类别取得失败");
  3789. }
  3790. return dt.Rows[0]["cVRSCode"].ToString();
  3791. }
  3792. #endregion
  3793. 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)
  3794. {
  3795. string sql = "";
  3796. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  3797. sql += " and cBatch='" + cBatch + "' ";
  3798. else
  3799. sql += " and cBatch='' ";
  3800. if (DBHelper.bFree1(cInvCode, cmd) == true)
  3801. sql += " and cFree1='" + cFree1 + "' ";
  3802. else
  3803. sql += " and cFree1='' ";
  3804. if (DBHelper.bFree2(cInvCode, cmd) == true)
  3805. sql += " and cFree2='" + cFree2 + "' ";
  3806. else
  3807. sql += " and cFree2='' ";
  3808. if (DBHelper.bFree3(cInvCode, cmd) == true)
  3809. sql += " and cFree3='" + cFree3 + "' ";
  3810. else
  3811. sql += " and cFree3='' ";
  3812. if (DBHelper.bFree4(cInvCode, cmd) == true)
  3813. sql += " and cFree4='" + cFree4 + "' ";
  3814. else
  3815. sql += " and cFree4='' ";
  3816. if (DBHelper.bFree5(cInvCode, cmd) == true)
  3817. sql += " and cFree5='" + cFree5 + "' ";
  3818. else
  3819. sql += " and cFree5='' ";
  3820. if (DBHelper.bFree6(cInvCode, cmd) == true)
  3821. sql += " and cFree6='" + cFree6 + "' ";
  3822. else
  3823. sql += " and cFree6='' ";
  3824. if (DBHelper.bFree7(cInvCode, cmd) == true)
  3825. sql += " and cFree7='" + cFree7 + "' ";
  3826. else
  3827. sql += " and cFree7='' ";
  3828. if (DBHelper.bFree8(cInvCode, cmd) == true)
  3829. sql += " and cFree8='" + cFree8 + "' ";
  3830. else
  3831. sql += " and cFree8='' ";
  3832. return sql;
  3833. }
  3834. 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)
  3835. {
  3836. bool bFree1 = false;
  3837. bool bFree2 = false;
  3838. bool bFree3 = false;
  3839. bool bFree4 = false;
  3840. bool bFree5 = false;
  3841. bool bFree6 = false;
  3842. bool bFree7 = false;
  3843. bool bFree8 = false;
  3844. #region 自由项管控
  3845. DataTable SubdtFree = DBHelper.bFree(cInvCode, cmd);
  3846. if (SubdtFree.Rows.Count > 0 && SubdtFree != null)
  3847. {
  3848. bFree1 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree1"]);
  3849. bFree2 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree2"]);
  3850. bFree3 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree3"]);
  3851. bFree4 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree4"]);
  3852. bFree5 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree5"]);
  3853. bFree6 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree6"]);
  3854. bFree7 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree7"]);
  3855. bFree8 = Convert.ToBoolean(SubdtFree.Rows[0]["bFree8"]);
  3856. }
  3857. else
  3858. {
  3859. throw new Exception("存货编码:" + cInvCode + "不存在");
  3860. }
  3861. #endregion
  3862. string sql = "";
  3863. if (DBHelper.bInvBatch(cInvCode, cmd) == true)
  3864. sql += " and cBatch='" + cBatch + "' ";
  3865. else
  3866. sql += " and cBatch='' ";
  3867. if (bFree1)
  3868. {
  3869. sql += " and cFree1='" + cFree1 + "' ";
  3870. }
  3871. else
  3872. {
  3873. sql += " and cFree1='' ";
  3874. }
  3875. if (bFree2)
  3876. {
  3877. sql += " and cFree2='" + cFree2 + "' ";
  3878. }
  3879. else
  3880. {
  3881. sql += " and cFree2='' ";
  3882. }
  3883. if (bFree3)
  3884. {
  3885. sql += " and cFree3='" + cFree3 + "' ";
  3886. }
  3887. else
  3888. {
  3889. sql += " and cFree3='' ";
  3890. }
  3891. if (bFree4)
  3892. {
  3893. sql += " and cFree4='" + cFree4 + "' ";
  3894. }
  3895. else
  3896. {
  3897. sql += " and cFree4='' ";
  3898. }
  3899. if (bFree5)
  3900. {
  3901. sql += " and cFree5='" + cFree5 + "' ";
  3902. }
  3903. else
  3904. {
  3905. sql += " and cFree5='' ";
  3906. }
  3907. if (bFree6)
  3908. {
  3909. sql += " and cFree6='" + cFree6 + "' ";
  3910. }
  3911. else
  3912. {
  3913. sql += " and cFree6='' ";
  3914. }
  3915. if (bFree7)
  3916. {
  3917. sql += " and cFree7='" + cFree7 + "' ";
  3918. }
  3919. else
  3920. {
  3921. sql += " and cFree7='' ";
  3922. }
  3923. if (bFree8)
  3924. {
  3925. sql += " and cFree8='" + cFree8 + "' ";
  3926. }
  3927. else
  3928. {
  3929. sql += " and cFree8='' ";
  3930. }
  3931. return sql;
  3932. }
  3933. /// <summary>
  3934. /// table转list
  3935. /// </summary>
  3936. /// <param name="dt"></param>
  3937. /// <returns></returns>
  3938. public class ModelConvertHelper<T> where T : new()
  3939. {
  3940. public static IList<T> ConvertToModel(DataTable dt)
  3941. {
  3942. // 定义集合
  3943. IList<T> ts = new List<T>();
  3944. // 获得此模型的类型
  3945. Type type = typeof(T);
  3946. string tempName = "";
  3947. foreach (DataRow dr in dt.Rows)
  3948. {
  3949. T t = new T();
  3950. // 获得此模型的公共属性
  3951. PropertyInfo[] propertys = t.GetType().GetProperties();
  3952. foreach (PropertyInfo pi in propertys)
  3953. {
  3954. tempName = pi.Name;
  3955. // 检查DataTable是否包含此列
  3956. if (dt.Columns.Contains(tempName))
  3957. {
  3958. // 判断此属性是否有Setter
  3959. if (!pi.CanWrite) continue;
  3960. object value = dr[tempName];
  3961. if (value != DBNull.Value)
  3962. pi.SetValue(t, value, null);
  3963. }
  3964. }
  3965. ts.Add(t);
  3966. }
  3967. return ts;
  3968. }
  3969. /// <summary>
  3970. /// 根据传入的日期,需要增加的天数返回一个字符串
  3971. /// </summary>
  3972. /// <param name="data"></param>
  3973. /// <param name="str"></param>
  3974. public static string ReTime(DateTime data, int str)
  3975. {
  3976. int year = data.Year;
  3977. int month = data.Month;
  3978. int day = data.Day;
  3979. int n = DateTime.DaysInMonth(year, month);
  3980. int k = day + str;
  3981. if (k > n)
  3982. {
  3983. day = str - (n - day);
  3984. month = month + 1;
  3985. if (month > 12)
  3986. {
  3987. month = 1;
  3988. year = year + 1;
  3989. }
  3990. }
  3991. else
  3992. {
  3993. day = day + str;
  3994. }
  3995. string c = year + "-" + month + "-" + day;
  3996. return c;
  3997. }
  3998. }
  3999. }
  4000. }