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

4004 lines
168 KiB

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