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

343 lines
18 KiB

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