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

381 lines
23 KiB

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