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

348 lines
19 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
3 years ago
3 years ago
  1. using ICSSoft.Common;
  2. using ICSSoft.Entity;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace ICSSoft.DataProject
  11. {
  12. public class DisassemblyDoc
  13. {
  14. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  15. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  16. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  17. private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"];
  18. /// <summary>
  19. /// 审核拆卸单
  20. /// </summary>
  21. /// <param name="Bills"></param>
  22. /// <returns></returns>
  23. public bool Dismrecord01(List<ICSDisassemblyDoc> Bills)
  24. {
  25. bool ResultFlag = false;
  26. string connS = "";
  27. SqlConnection conn = new SqlConnection();
  28. SqlCommand cmd = new SqlCommand();
  29. if (Bills.Count <= 0)
  30. {
  31. throw new Exception("传送数据为空!");
  32. }
  33. LogInfo(Bills);
  34. foreach (ICSDisassemblyDoc head in Bills)
  35. {
  36. try
  37. {
  38. connS = string.Format(connString, head.WorkPoint);
  39. conn = new System.Data.SqlClient.SqlConnection(connS);
  40. conn.Open();
  41. SqlTransaction sqlTran = conn.BeginTransaction();
  42. cmd = new SqlCommand();
  43. cmd.Transaction = sqlTran;
  44. cmd.Connection = conn;
  45. string[] ss = head.WorkPoint.Split('_');
  46. ERPDB = ss[1];
  47. string sql = "";
  48. ICSUserInfo userInfo = new ICSUserInfo();
  49. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  50. string cHandler = string.Empty;
  51. string cIWhCode = string.Empty;
  52. #region 检验单号是否存在
  53. sql = "select * from AssemVouch where ID='" + head.ID + "'";
  54. DataTable dt = DBHelper.SQlReturnData(sql, cmd);
  55. if (dt != null && dt.Rows.Count > 0)
  56. {
  57. cHandler = dt.Rows[0]["cVerifyPerson"].ToString();
  58. //cIWhCode = head.cWhCode;
  59. if (!string.IsNullOrEmpty(cHandler))
  60. {
  61. throw new Exception("单据ID:" + head.ID + "不是开立状态!");
  62. }
  63. }
  64. else
  65. {
  66. throw new Exception("单据ID:" + head.ID + "在U8中不存在!");
  67. }
  68. #endregion
  69. #region 审核拆卸单
  70. sql = @"UPDATE dbo.AssemVouch SET cVerifyPerson='" + userInfo.UserName + @"' ,
  71. dVerifyDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE() WHERE ID='" + head.ID + "'";
  72. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核拆卸单失败!");
  73. #endregion
  74. #region 其他出库单表头
  75. //取得out库单的默认显示模版
  76. string DEF_ID09 = DBHelper.GetDefaultTemplate("0301", cmd);
  77. //取得采购入库单单据 表头ID,表体DID
  78. VouchKey key09 = new VouchKey();
  79. var nums = Bills.Count;
  80. Dictionary<string, int> dics = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + nums + "");
  81. int iFatherIds = Convert.ToInt32(dics["iFatherId"].ToString());
  82. int iChildIds = Convert.ToInt32(dics["iChildId"].ToString());
  83. DateTime dates = DateTime.Now;
  84. string iBaseCodeLens = DBHelper.GetAllRDCode("0301", "" + dates + "", "admin");
  85. sql = @"INSERT INTO dbo.RdRecord09
  86. (ID,bRdFlag,cVouchType,cBusType,cSource,cBusCode,cWhCode,dDate,cCode,cRdCode,
  87. cHandler,cMemo,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,VT_ID,bIsSTQc,bOMFirst,
  88. bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount,
  89. iverifystate,iswfcontrolled,dnmaketime,dnverifytime,bredvouch,cSourceLs,cDepCode)
  90. SELECT @ID,'0','09','','',a.cAVCode,b.cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,null,
  91. @cHandler,@cMemo,'0',@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),'0','0',
  92. '85','0','0','0','0','0','0','0','0',GETDATE(),GETDATE(),'0',null,cDepCode
  93. from dbo.AssemVouch a
  94. inner join AssemVouchs b on a.id=b.id
  95. WHERE a.ID='" + head.ID + "' and bAVType='' ";
  96. cmd.Parameters.Clear();
  97. cmd.Parameters.Add(new SqlParameter("@ID", iFatherIds));
  98. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLens));
  99. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  100. cmd.Parameters.Add(new SqlParameter("@cMemo", ""));
  101. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  102. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID09));
  103. cmd.CommandText = sql;
  104. try
  105. {
  106. cmd.ExecuteNonQuery();
  107. }
  108. catch (Exception ex)
  109. {
  110. log.Error("生成其他出库单失败!拆卸单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  111. throw new Exception("程序异常,请联系开发人员!");
  112. }
  113. #endregion
  114. #region 3.5 其他出库单表体
  115. #region 检验单号是否存在
  116. sql = "select iAVQuantity,* from AssemVouchs where ID='" + head.ID + "' and bAVType='套件'";
  117. DataTable dsh = DBHelper.SQlReturnData(sql, cmd);
  118. if (dsh != null && dsh.Rows.Count > 0)
  119. {
  120. cIWhCode = dsh.Rows[0]["cWhCode"].ToString();
  121. }
  122. else
  123. {
  124. throw new Exception("单据ID:" + head.ID + "在U8中不存在!");
  125. }
  126. //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
  127. sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + dsh.Rows[0]["cInvCode"].ToString() + "'AND cBatch=''and cWhCode='" + dsh.Rows[0]["cWhCode"].ToString() + "'";
  128. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  129. if (dtItem != null && dtItem.Rows.Count > 0)
  130. {
  131. if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < Convert.ToInt32(dsh.Rows[0]["iAVQuantity"].ToString()))
  132. {
  133. throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + dsh.Rows[0]["cInvCode"].ToString());
  134. }
  135. }
  136. else
  137. {
  138. throw new Exception("物料:" + dsh.Rows[0]["cInvCode"].ToString() + "在现存量表中不存在!");
  139. }
  140. #endregion
  141. #region 3.5.1 表体
  142. sql = @"INSERT INTO dbo.rdrecords09
  143. (AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iTrIds,iNQuantity,
  144. cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  145. SELECT @AutoID,@ID,@iItemcode,@iQuantity,'0','0','0',a.autoID,iAVQuantity,
  146. '0','0','0','0','1','0','0','0',a.irowno,null,cAVBatch,iAVPCost,iAVPPrice,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10
  147. from AssemVouchs a
  148. where ID=" + head.ID + @" and bAVType='' ;";
  149. cmd.Parameters.Clear();
  150. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildIds));
  151. cmd.Parameters.Add(new SqlParameter("@ID", iFatherIds));
  152. cmd.Parameters.Add(new SqlParameter("@iItemcode", dsh.Rows[0]["cInvCode"].ToString()));
  153. cmd.Parameters.Add(new SqlParameter("@iQuantity", dsh.Rows[0]["iAVQuantity"].ToString()));
  154. //cmd.Parameters.Add(new SqlParameter("@iTrIds", head.AutoID));
  155. //cmd.Parameters.Add(new SqlParameter("@irowno", Rd09irowno));
  156. cmd.CommandText = sql;
  157. try
  158. {
  159. cmd.ExecuteNonQuery();
  160. }
  161. catch (Exception ex)
  162. {
  163. log.Error("生成其他出库单表体失败!拆卸单ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  164. throw new Exception("程序异常,请联系开发人员!");
  165. }
  166. sql = @"INSERT INTO dbo.rdrecords09sub
  167. ( AutoID , ID ,iBG_Ctrl , iBGSTSum )
  168. values('" + iChildIds + "','" + iFatherIds + "',0,0)";
  169. cmd.CommandText = sql;
  170. cmd.ExecuteNonQuery();
  171. #endregion
  172. if (head.UpdateStock)
  173. {
  174. //更新现存量
  175. key09.cBustypeUN = "拆卸出库";
  176. key09.cVouchTypeUN = "09";
  177. key09.TableName = "IA_ST_UnAccountVouch09";
  178. //DBHelper.UpdateCurrentStock(cmd, dsh.Rows[0]["cInvCode"].ToString(), cIWhCode, "", -Convert.ToDecimal(dsh.Rows[0]["iAVQuantity"].ToString()), key09);
  179. DBHelper.UpdateCurrentStockNEW(cmd, dsh.Rows[0]["cInvCode"].ToString(), cIWhCode, dsh.Rows[0]["cBatch"].ToString(), -Convert.ToDecimal(dsh.Rows[0]["iAVQuantity"].ToString()), dsh.Rows[0]["cFree1"].ToString(), dsh.Rows[0]["cFree2"].ToString(), dsh.Rows[0]["cFree3"].ToString(), dsh.Rows[0]["cFree4"].ToString(), dsh.Rows[0]["cFree5"].ToString(), dsh.Rows[0]["cFree6"].ToString(), dsh.Rows[0]["cFree7"].ToString(), dsh.Rows[0]["cFree8"].ToString(), dsh.Rows[0]["cFree9"].ToString(), dsh.Rows[0]["cFree10"].ToString(), key09);
  180. }
  181. //DBHelper.UpdateIDandDID("rd", ErpName, head.list.Count(), cmd);
  182. #endregion
  183. sql = "select cWhCode from dbo.AssemVouchs where ID='" + head.ID + "' and bAVType='散件' group by cWhCode;";
  184. DataTable dtChecks = DBHelper.SQlReturnData(sql, cmd);
  185. int iChildId = 0;
  186. int iFatherId = 0;
  187. VouchKey key08 = new VouchKey();
  188. for (int i = 0; i < dtChecks.Rows.Count; i++)
  189. {
  190. #region 其他入库单表头
  191. //取得out库单的默认显示模版
  192. string DEF_ID08 = DBHelper.GetDefaultTemplate("0301", cmd);
  193. //取得采购入库单单据 表头ID,表体DID
  194. var num = Bills.Count;
  195. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + num + "");
  196. iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  197. iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  198. DateTime date = DateTime.Now;
  199. string iBaseCodeLen = DBHelper.GetAllRDCode("0301", "" + date + "", "admin");
  200. sql = @"INSERT INTO dbo.RdRecord08
  201. (ID,bRdFlag,cVouchType,cBusType,cSource,cBusCode,cWhCode,dDate,cCode,cRdCode,
  202. cHandler,cMemo,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,VT_ID,bIsSTQc,bOMFirst,
  203. bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount,
  204. iverifystate,iswfcontrolled,dnmaketime,dnverifytime,bredvouch,cSourceLs,cDepCode)
  205. SELECT @ID,'1','08','','',a.cAVCode,@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,null,
  206. @cHandler,@cMemo,'0',@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),'0','0',
  207. @VT_ID,'0','0','0','0','0','0','0','0',GETDATE(),GETDATE(),'0',null,cDepCode
  208. from dbo.AssemVouch a
  209. WHERE id='" + head.ID + "' ";
  210. cmd.Parameters.Clear();
  211. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  212. //cmd.Parameters.Add(new SqlParameter("@cBusCode", head.cCode));
  213. cmd.Parameters.Add(new SqlParameter("@cWhCode", dtChecks.Rows[0]["cWhCode"].ToString()));
  214. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  215. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  216. cmd.Parameters.Add(new SqlParameter("@cMemo", ""));
  217. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  218. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID08));
  219. cmd.CommandText = sql;
  220. try
  221. {
  222. cmd.ExecuteNonQuery();
  223. }
  224. catch (Exception ex)
  225. {
  226. log.Error("生成其他入库单表头失败!拆卸ID:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  227. throw new Exception("程序异常,请联系开发人员!");
  228. }
  229. #endregion
  230. sql = "select * from AssemVouchs where ID='" + head.ID + "' and bAVType='散件' and cWhCode='" + dtChecks.Rows[i]["cWhCode"].ToString() + "' ";
  231. DataTable ds = DBHelper.SQlReturnData(sql, cmd);
  232. for (int j = 0; j < ds.Rows.Count; j++)
  233. {
  234. #region 其他入库单表体
  235. if (ds != null && ds.Rows.Count > 0)
  236. {
  237. cIWhCode = ds.Rows[j]["cWhCode"].ToString();
  238. }
  239. else
  240. {
  241. throw new Exception("单据号:" + head.ID + "在U8中不存在!");
  242. }
  243. sql = @"INSERT INTO dbo.rdrecords08
  244. (AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iTrIds,iNQuantity,
  245. cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  246. SELECT @AutoID,@ID,cInvCode,a.iAVQuantity,'0','0','0',a.AutoID,iAVQuantity,
  247. '0','0','0','0','1','0','0','0',a.irowno,null,cAVBatch,iAVPCost,iAVPPrice,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 from AssemVouchs a
  248. where a.autoID=" + ds.Rows[j]["autoID"].ToString() + @" ;";
  249. cmd.Parameters.Clear();
  250. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  251. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  252. //cmd.Parameters.Add(new SqlParameter("@iQuantity", body.iQuantity));
  253. //cmd.Parameters.Add(new SqlParameter("@iTrIds", body.AutoID));
  254. //cmd.Parameters.Add(new SqlParameter("@irowno", Rd08irowno));
  255. cmd.CommandText = sql;
  256. try
  257. {
  258. cmd.ExecuteNonQuery();
  259. }
  260. catch (Exception ex)
  261. {
  262. log.Error("生成其他出库单表体失败!拆卸单单号:" + head.ID + ";异常:" + ex.Message + ";SQL:\r\n" + sql);
  263. throw new Exception("程序异常,请联系开发人员!");
  264. }
  265. if (head.UpdateStock)
  266. {
  267. //更新现存量
  268. key08.cBustypeUN = "拆卸入库";
  269. key08.cVouchTypeUN = "08";
  270. key08.TableName = "IA_ST_UnAccountVouch08";
  271. DBHelper.UpdateCurrentStockNEW(cmd, ds.Rows[j]["cInvCode"].ToString(), cIWhCode, ds.Rows[j]["cBatch"].ToString(), Convert.ToDecimal(ds.Rows[j]["iAVQuantity"].ToString()), ds.Rows[j]["cFree1"].ToString(), ds.Rows[j]["cFree2"].ToString(), ds.Rows[j]["cFree3"].ToString(), ds.Rows[j]["cFree4"].ToString(), ds.Rows[j]["cFree5"].ToString(), ds.Rows[j]["cFree6"].ToString(), ds.Rows[j]["cFree7"].ToString(), ds.Rows[j]["cFree8"].ToString(), ds.Rows[j]["cFree9"].ToString(), ds.Rows[j]["cFree10"].ToString(), key08);
  272. //DBHelper.UpdateCurrentStock(cmd, ds.Rows[j]["cInvCode"].ToString(), cIWhCode, "", Convert.ToDecimal(ds.Rows[j]["iAVQuantity"].ToString()), key08);
  273. }
  274. #endregion
  275. iChildId--;
  276. }
  277. }
  278. cmd.Transaction.Commit();
  279. }
  280. catch (Exception ex)
  281. {
  282. cmd.Transaction.Rollback();
  283. log.Error(ex.Message);
  284. throw new Exception(ex.Message);
  285. }
  286. finally
  287. {
  288. if (conn.State == ConnectionState.Open)
  289. {
  290. conn.Close();
  291. }
  292. conn.Dispose();
  293. }
  294. }
  295. ResultFlag = true;
  296. return ResultFlag;
  297. }
  298. /// <summary>
  299. /// 记录日志
  300. /// </summary>
  301. /// <param name="Bills"></param>
  302. private void LogInfo(List<ICSDisassemblyDoc> Bills)
  303. {
  304. string HeadList = string.Empty;
  305. string BodyList = string.Empty;
  306. foreach (ICSDisassemblyDoc head in Bills)
  307. {
  308. HeadList += "\r\n 表头主键ID:" + head.ID + ",用户:" + head.User + ",站点:" + head.WorkPoint + "";
  309. }
  310. log.Info(HeadList);
  311. log.Info(BodyList);
  312. }
  313. }
  314. }