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.

4055 lines
170 KiB

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