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

429 lines
24 KiB

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