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

419 lines
22 KiB

  1. using ICSSoft.Common;
  2. using ICSSoft.Entity;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ICSSoft.DataProject
  12. {
  13. /// <summary>
  14. /// 调拨单
  15. /// </summary>
  16. public class TransferDoc
  17. {
  18. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  19. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  20. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  21. private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"];
  22. /// <summary>
  23. /// 审核调拨单
  24. /// </summary>
  25. /// <param name="Bills"></param>
  26. /// <returns></returns>
  27. public bool ConfirmTrans(List<ICSTransferDoc> Bills)
  28. {
  29. bool ResultFlag = false;
  30. //int num = 0;
  31. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  32. conn.Open();
  33. SqlTransaction sqlTran = conn.BeginTransaction();
  34. SqlCommand cmd = new SqlCommand();
  35. cmd.Transaction = sqlTran;
  36. cmd.Connection = conn;
  37. try
  38. {
  39. if (Bills.Count <= 0)
  40. {
  41. throw new Exception("传送数据为空!");
  42. }
  43. LogInfo(Bills);
  44. //MergeObject(Bills, cmd);
  45. foreach (ICSTransferDoc head in Bills)
  46. {
  47. string sql = "";
  48. ICSUserInfo userInfo = new ICSUserInfo();
  49. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  50. string cIWhCode = string.Empty;
  51. string cOWhCode = string.Empty;
  52. #region 检查
  53. sql = "select * from dbo.TransVouch where ID='" + head.ID + "';";
  54. DataTable dtCheck = DBHelper.SQlReturnData(sql, cmd);
  55. if (dtCheck != null && dtCheck.Rows.Count > 0)
  56. {
  57. string cHandler = dtCheck.Rows[0]["cVerifyPerson"].ToString();
  58. if (!string.IsNullOrEmpty(cHandler))
  59. {
  60. throw new Exception("调拨单ID:" + head.ID + "不是开立状态!");
  61. }
  62. cIWhCode = dtCheck.Rows[0]["cIWhCode"].ToString();
  63. cOWhCode = dtCheck.Rows[0]["cOWhCode"].ToString();
  64. sql = string.Format("select * from Warehouse where cWhCode in ('{0}','{1}')", dtCheck.Rows[0]["cOWhCode"].ToString(), dtCheck.Rows[0]["cIWhCode"].ToString());
  65. DataTable dtWh = DBHelper.SQlReturnData(sql, cmd);
  66. if (dtWh != null && dtWh.Rows.Count > 1)
  67. {
  68. }
  69. else
  70. {
  71. throw new Exception("调拨单中转入或者转出仓库不存在!");
  72. }
  73. }
  74. else
  75. {
  76. throw new Exception("调拨单ID:" + head.ID + "在U8中不存在!");
  77. }
  78. #endregion
  79. #region 审核
  80. sql = "UPDATE dbo.TransVouch SET cVerifyPerson='" + userInfo.UserName + "' ,dVerifyDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE() WHERE ID='" + head.ID + "'";
  81. cmd.CommandText = sql;
  82. try
  83. {
  84. cmd.ExecuteNonQuery();
  85. }
  86. catch (Exception ex)
  87. {
  88. log.Error("审核调拨单失败!异常:" + ex.Message + ";\r\n SQL:" + sql);
  89. throw new Exception("程序异常,请联系开发人员!");
  90. }
  91. int bodyID = 0;
  92. // foreach (ICSTransferDoc body in Bills)
  93. // {
  94. // //回写调出仓库待发数量
  95. // string fOutcBatch = string.Empty;
  96. // //bodyID = Convert.ToInt32(body);
  97. // sql = @"UPDATE a SET fTransOutQuantity=ISNULL(a.fTransOutQuantity,0)-ISNULL(a.iQuantity,0)
  98. // FROM dbo.CurrentStock a
  99. // LEFT JOIN (SELECT a.cOWhCode,a.cIWhCode,b.cInvCode,b.autoID,
  100. // ISNULL(b.cTVBatch,'') AS cTVBatch FROM dbo.TransVouch a
  101. // INNER JOIN dbo.TransVouchs b ON a.cTVCode = b.cTVCode)b
  102. // ON a.cInvCode=b.cInvCode AND a.cBatch=b.cTVBatch and a.cWhCode=b.cOWhCode WHERE b.autoID=@bodyID";
  103. // cmd.Parameters.Clear();
  104. // //cmd.Parameters.Add(new SqlParameter("@Qty", dtCheck.Rows[0]["iQuantity"].ToString()));
  105. // cmd.Parameters.Add(new SqlParameter("@bodyID",body.ID));
  106. // cmd.CommandText = sql;
  107. // try
  108. // {
  109. // cmd.ExecuteNonQuery();
  110. // }
  111. // catch (Exception ex)
  112. // {
  113. // log.Error("回写现存量表 待调拨数量!异常:" + ex.Message + ";\r\n SQL:" + sql);
  114. // throw new Exception("程序异常,请联系开发人员!");
  115. // }
  116. //// #region 判断现存量是否足够
  117. //// sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fTransOutQuantity<0)
  118. //// BEGIN
  119. //// DECLARE @MSG NVARCHAR(100)
  120. //// SELECT @MSG='ERP待调拨出库数量不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fTransOutQuantity<0
  121. //// RAISERROR(@MSG,16,1)
  122. //// END";
  123. //// cmd.CommandText = sql;
  124. //// cmd.ExecuteNonQuery();
  125. //// #endregion
  126. // //回写调入仓库在途数量
  127. // fOutcBatch = string.Empty;
  128. // sql = @"UPDATE a SET fTransInQuantity=ISNULL(fTransInQuantity,0)-ISNULL(a.iQuantity,0)
  129. // FROM dbo.CurrentStock a
  130. // LEFT JOIN (SELECT a.cOWhCode,a.cIWhCode,b.cInvCode,b.autoID,
  131. // ISNULL(b.cTVBatch,'') AS cTVBatch FROM dbo.TransVouch a
  132. // INNER JOIN dbo.TransVouchs b ON a.cTVCode = b.cTVCode)b
  133. // ON a.cInvCode=b.cInvCode AND a.cBatch=b.cTVBatch and a.cWhCode=b.cIWhCode WHERE b.autoID=@bodyID";
  134. // cmd.Parameters.Clear();
  135. // //cmd.Parameters.Add(new SqlParameter("@Qty", dtCheck.Rows[0]["iQuantity"].ToString()));
  136. // cmd.Parameters.Add(new SqlParameter("@bodyID", body.ID));
  137. // cmd.CommandText = sql;
  138. // try
  139. // {
  140. // cmd.ExecuteNonQuery();
  141. // }
  142. // catch (Exception ex)
  143. // {
  144. // log.Error("回写现存量表在途数量!异常:" + ex.Message + ";\r\n SQL:" + sql);
  145. // throw new Exception("程序异常,请联系开发人员!");
  146. // }
  147. //// #region 判断现存量是否足够
  148. //// sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fTransInQuantity<0)
  149. //// BEGIN
  150. //// DECLARE @MSG NVARCHAR(100)
  151. //// SELECT @MSG='ERP待调拨入库数量不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fTransInQuantity<0
  152. //// RAISERROR(@MSG,16,1)
  153. //// END";
  154. //// cmd.CommandText = sql;
  155. //// cmd.ExecuteNonQuery();
  156. //// #endregion
  157. // }
  158. #endregion
  159. //0305 形态转换单CartonNum
  160. sql = "select * from dbo.TransVouchs where ID='" + head.ID + "';";
  161. DataTable dtChecks = DBHelper.SQlReturnData(sql, cmd);
  162. #region 其他入库单
  163. //取得out库单的默认显示模版
  164. string DEF_ID08 = DBHelper.GetDefaultTemplate("0301", cmd);
  165. VouchKey key08 = new VouchKey();
  166. //key08 = U8Helper.GetPrimaryKey("rd", ErpName, 1, cmd);
  167. //// 取得表头单据号
  168. //string Code08 = U8Helper.GetRdrecord08Code(ErpName, "QR", cmd);
  169. var num = Bills.Count;
  170. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + num + "");
  171. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  172. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  173. DateTime date = DateTime.Now;
  174. string iBaseCodeLen = DBHelper.GetAllRDCode("0301", "" + date + "", "admin");
  175. #region 其他入库单表头
  176. sql = @"INSERT INTO dbo.RdRecord08
  177. (ID,bRdFlag,cVouchType,cBusType,cSource,cBusCode,cWhCode,dDate,cCode,cRdCode,
  178. cHandler,cMemo,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,VT_ID,bIsSTQc,bOMFirst,
  179. bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount,
  180. iverifystate,iswfcontrolled,dnmaketime,dnverifytime,bredvouch,cSourceLs,cDepCode)
  181. SELECT @ID,'1','08','','',a.cTVCode,cIWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),a.cTVCode,'14',
  182. @cHandler,@cMemo,'0',@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),'0','0',
  183. @VT_ID,'0','0','0','0','0','0','0','0',GETDATE(),GETDATE(),'0','1',cIDepCode from dbo.TransVouch a WHERE ID='" + head.ID + "' ";
  184. cmd.Parameters.Clear();
  185. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  186. //cmd.Parameters.Add(new SqlParameter("@cBusCode", head.ID));
  187. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  188. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  189. cmd.Parameters.Add(new SqlParameter("@cMemo", ""));
  190. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  191. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID08));
  192. cmd.CommandText = sql;
  193. try
  194. {
  195. cmd.ExecuteNonQuery();
  196. }
  197. catch (Exception ex)
  198. {
  199. log.Error("生成其他入库单表头失败!调拨单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  200. throw new Exception("程序异常,请联系开发人员!");
  201. }
  202. #endregion
  203. #region 3.5 其他入库单表体
  204. for (int i = 0; i < dtChecks.Rows.Count;i++ )
  205. {
  206. #region 3.5.1 表体
  207. sql = @"INSERT INTO dbo.rdrecords08
  208. (AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iTrIds,iNQuantity,
  209. cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice)
  210. SELECT @AutoID,@ID,cInvCode,b.iQuantity,'0','0','0',a.AutoID,iTVQuantity,
  211. '0','0','0','0','1','0','0','0',@irowno,null,cTVBatch,iTVPCost,iTVPPrice
  212. from TransVouchs a
  213. inner join TransVouch b on a.ID=b.ID
  214. where a.ID='" + dtChecks.Rows[i]["AutoID"].ToString() + @"';";
  215. cmd.Parameters.Clear();
  216. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  217. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  218. //cmd.Parameters.Add(new SqlParameter("@iQuantity", dtCheck.Rows[0]["iQuantity"].ToString()));
  219. //cmd.Parameters.Add(new SqlParameter("@iTrIds", body.AutoID));
  220. cmd.Parameters.Add(new SqlParameter("@irowno", dtChecks.Rows[i]["irowno"].ToString()));
  221. cmd.CommandText = sql;
  222. try
  223. {
  224. cmd.ExecuteNonQuery();
  225. }
  226. catch (Exception ex)
  227. {
  228. log.Error("生成其他出库单表体失败!调拨单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  229. throw new Exception("程序异常,请联系开发人员!");
  230. }
  231. #endregion
  232. //更新现存量
  233. key08.cBustypeUN = "调拨入库";
  234. key08.cVouchTypeUN = "08";
  235. key08.TableName = "IA_ST_UnAccountVouch08";
  236. key08.UpdateTodoQuantity = head.UpdateTodoQuantity;
  237. DBHelper.UpdateCurrentStock(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cIWhCode, "", Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), key08);
  238. }
  239. #endregion
  240. #endregion
  241. #region 其他出库单
  242. //取得out库单的默认显示模版
  243. string DEF_ID09 = DBHelper.GetDefaultTemplate( "0301", cmd);
  244. //取得采购入库单单据 表头ID,表体DID
  245. VouchKey key09 = new VouchKey();
  246. //key09 = U8Helper.GetPrimaryKey("rd", ErpName, 1, cmd);
  247. //// 取得表头单据号
  248. //string Code09 = U8Helper.GetRdrecord09Code(ErpName, "QC", cmd);
  249. var nums = dtChecks.Rows.Count;
  250. Dictionary<string, int> dics = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + nums + "");
  251. int iFatherIds = Convert.ToInt32(dics["iFatherId"].ToString());
  252. int iChildIds = Convert.ToInt32(dics["iChildId"].ToString());
  253. DateTime dates = DateTime.Now;
  254. string iBaseCodeLens = DBHelper.GetAllRDCode("0301", "" + date + "", "admin");
  255. #region 其他出库单表头
  256. sql = @"INSERT INTO dbo.RdRecord09
  257. (ID,bRdFlag,cVouchType,cBusType,cSource,cBusCode,cWhCode,dDate,cCode,cRdCode,
  258. cHandler,cMemo,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,VT_ID,bIsSTQc,bOMFirst,
  259. bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount,
  260. iverifystate,iswfcontrolled,dnmaketime,dnverifytime,bredvouch,cSourceLs,cDepCode)
  261. SELECT @ID,'0','09','','',a.cTVCode,cOWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,'24',
  262. @cHandler,@cMemo,'0',@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),'0','0',
  263. '85','0','0','0','0','0','0','0','0',GETDATE(),GETDATE(),'0','1',cODepCode from dbo.TransVouch a WHERE ID='" + head.ID + "' ";
  264. cmd.Parameters.Clear();
  265. cmd.Parameters.Add(new SqlParameter("@ID", iFatherIds));
  266. //cmd.Parameters.Add(new SqlParameter("@cBusCode", dtCheck.Rows[0]["cTVCode"].ToString()));
  267. //cmd.Parameters.Add(new SqlParameter("@cWhCode", cOWhCode));
  268. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLens));
  269. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  270. cmd.Parameters.Add(new SqlParameter("@cMemo", ""));
  271. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  272. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID09));
  273. cmd.CommandText = sql;
  274. try
  275. {
  276. cmd.ExecuteNonQuery();
  277. }
  278. catch (Exception ex)
  279. {
  280. log.Error("生成其他出库单失败!调拨单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  281. throw new Exception("程序异常,请联系开发人员!");
  282. }
  283. #endregion
  284. #region 3.5 其他出库单表体
  285. for (int i = 0; i < dtChecks.Rows.Count; i++)
  286. {
  287. #region 3.5.1 表体
  288. //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
  289. //sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + dtCheck.Rows[0]["cInvCode"].ToString() + "'AND cWhCode='" + dtCheck.Rows[0]["cOWhCode"].ToString() + "'";
  290. //DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  291. //if (dtItem != null && dtItem.Rows.Count > 0)
  292. //{
  293. // if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cTVBatch))
  294. // {
  295. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.cInvCode);
  296. // }
  297. // if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < body.iQuantity)
  298. // {
  299. // throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.cInvCode);
  300. // }
  301. //}
  302. //else
  303. //{
  304. // throw new Exception("物料:" + body.cInvCode + "在现存量表中不存在!");
  305. //}
  306. //key09.DID += 1;
  307. //Rd09irowno += 1;
  308. sql = @"INSERT INTO dbo.rdrecords09
  309. (AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iTrIds,iNQuantity,
  310. cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice)
  311. SELECT @AutoID,@ID,cInvCode,a.iTVQuantity,'0','0','0',a.AutoID,iTVQuantity,
  312. '0','0','0','0','1','0','0','0',a.irowno,null,cTVBatch,iTVPCost,iTVPPrice
  313. from TransVouchs a
  314. inner join TransVouch b on a.ID=b.ID
  315. where b.ID='" + dtChecks.Rows[i]["AutoID"].ToString()+ @"' ;";
  316. cmd.Parameters.Clear();
  317. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildIds));
  318. cmd.Parameters.Add(new SqlParameter("@ID", iFatherIds));
  319. //cmd.Parameters.Add(new SqlParameter("@iQuantity", dtCheck.Rows[0]["iQuantity"].ToString()));
  320. //cmd.Parameters.Add(new SqlParameter("@iTrIds", body.AutoID));
  321. //cmd.Parameters.Add(new SqlParameter("@irowno", Rd09irowno));
  322. cmd.CommandText = sql;
  323. try
  324. {
  325. cmd.ExecuteNonQuery();
  326. }
  327. catch (Exception ex)
  328. {
  329. log.Error("生成其他出库单表体失败!调拨单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  330. throw new Exception("程序异常,请联系开发人员!");
  331. }
  332. sql = @"INSERT INTO dbo.rdrecords09sub
  333. ( AutoID , ID ,iBG_Ctrl , iBGSTSum )
  334. values('" + iChildIds + "','" + iFatherIds + "',0,0)";
  335. cmd.CommandText = sql;
  336. cmd.ExecuteNonQuery();
  337. #endregion
  338. //更新现存量
  339. key09.cBustypeUN = "调拨出库";
  340. key09.cVouchTypeUN = "09";
  341. key09.TableName = "IA_ST_UnAccountVouch09";
  342. DBHelper.UpdateCurrentStock(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), cOWhCode, "", -Convert.ToDecimal(dtChecks.Rows[i]["iTVQuantity"].ToString()), key09);
  343. iChildIds--;
  344. }
  345. #endregion
  346. #endregion
  347. }
  348. cmd.Transaction.Commit();
  349. ResultFlag = true;
  350. return ResultFlag;
  351. }
  352. catch (Exception ex)
  353. {
  354. cmd.Transaction.Rollback();
  355. log.Error(ex.Message);
  356. throw new Exception(ex.Message);
  357. }
  358. finally
  359. {
  360. if (conn.State == ConnectionState.Open)
  361. {
  362. conn.Close();
  363. }
  364. conn.Dispose();
  365. }
  366. }
  367. /// <summary>
  368. /// 记录日志
  369. /// </summary>
  370. /// <param name="Bills"></param>
  371. private void LogInfo(List<ICSTransferDoc> Bills)
  372. {
  373. string HeadList = string.Empty;
  374. string BodyList = string.Empty;
  375. foreach (ICSTransferDoc head in Bills)
  376. {
  377. HeadList += "\r\n 表头调拨单号:" + head.ID + ",用户:" + head.User;
  378. foreach (ICSTransferDoc body in Bills)
  379. {
  380. BodyList += "\r\n 表体主键ID: " + body.ID + "";
  381. }
  382. }
  383. log.Info(HeadList);
  384. log.Info(BodyList);
  385. }
  386. }
  387. }