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

3904 lines
164 KiB

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