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

3824 lines
161 KiB

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