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.

3941 lines
165 KiB

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