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

190 lines
9.5 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. /// <summary>
  13. /// 归还(生成其他入库单)
  14. /// </summary>
  15. public class ReturnDoc
  16. {
  17. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  18. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  19. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  20. private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"];
  21. /// <summary>
  22. /// 借用(生成其他出库单)
  23. /// </summary>
  24. /// <param name="Bills"></param>
  25. /// <returns></returns>
  26. public string ConfirmRd08(List<ICSReturnDoc> Bills)
  27. {
  28. string msg = string.Empty;
  29. string sql = string.Empty;
  30. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  31. string iFatherIdTwo = "";
  32. conn.Open();
  33. int num = 0;
  34. SqlTransaction sqlTran = conn.BeginTransaction();
  35. SqlCommand cmd = new SqlCommand();
  36. cmd.Transaction = sqlTran;
  37. cmd.Connection = conn;
  38. try
  39. {
  40. //List<RdRecord09> Vouchers = new List<RdRecord09>();
  41. foreach (ICSReturnDoc head in Bills)
  42. {
  43. num = head.details.Count();
  44. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + num + "");
  45. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  46. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  47. DateTime date = DateTime.Now;
  48. string iBaseCodeLen = DBHelper.GetAllRDCode("0302", "" + date + "", "admin");
  49. string cRDcode = DBHelper.GetRDCode("样品出库", cmd);
  50. //取得out库单的默认显示模版
  51. string DEF_ID08 = DBHelper.GetDefaultTemplate("0302", cmd);
  52. //取得其他出库单单据 表头ID,表体DID
  53. VouchKey key08 = new VouchKey();
  54. #region 其他出库单表头
  55. sql = @"INSERT INTO dbo.RdRecord08
  56. (ID,bRdFlag,cVouchType,cBusType,cSource,cBusCode,cWhCode,dDate,cCode,cRdCode,
  57. cHandler,cMemo,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,VT_ID,bIsSTQc,bOMFirst,
  58. bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount,
  59. iverifystate,iswfcontrolled,dnmaketime,dnverifytime,bredvouch,cSourceLs,cDepCode)
  60. VALUES(@ID,1,'08','','',@cBusCode,@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,
  61. @cHandler,@cMemo,'0',@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),'0','0',
  62. @VT_ID,'0','0','0','0','0','0','0','0',GETDATE(),GETDATE(),'0','1',@cDepCode) ";
  63. cmd.Parameters.Clear();
  64. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  65. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  66. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  67. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  68. cmd.Parameters.Add(new SqlParameter("@cDepCode", head.DepCode));
  69. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  70. cmd.Parameters.Add(new SqlParameter("@cMemo", head.User));
  71. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  72. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID08));
  73. cmd.Parameters.Add(new SqlParameter("@cBusCode", head.SourceCode));
  74. cmd.CommandText = sql;
  75. try
  76. {
  77. cmd.ExecuteNonQuery();
  78. }
  79. catch (Exception ex)
  80. {
  81. log.Error("生成其他出库单失败!SQL:\r\n" + sql);
  82. throw new Exception("程序异常,请联系开发人员!");
  83. }
  84. #endregion
  85. foreach (ICSBrrowDocs body in head.details)
  86. {
  87. #region 3.5.1 表体
  88. sql = @"INSERT INTO dbo.rdrecords08
  89. (AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iDebitIDs,iNQuantity,
  90. cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice)
  91. VALUES(@AutoID,@ID,@cInvCode,@iQuantity,'0','0','0',@iDebitIDs,'0',
  92. '0','0','0','0','1','0','0','0',@irowno,null,null,null,null )";
  93. cmd.Parameters.Clear();
  94. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  95. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  96. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  97. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  98. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  99. cmd.Parameters.Add(new SqlParameter("@iDebitIDs", body.SourceDetailID));
  100. cmd.CommandText = sql;
  101. try
  102. {
  103. cmd.ExecuteNonQuery();
  104. }
  105. catch (Exception ex)
  106. {
  107. log.Error("生成其他出库单表体失败!SQL:\r\n" + sql);
  108. throw new Exception("程序异常,请联系开发人员!");
  109. }
  110. #endregion
  111. //更新现存量
  112. key08.cBustypeUN = "调拨入库";
  113. key08.cVouchTypeUN = "08";
  114. key08.TableName = "IA_ST_UnAccountVouch08";
  115. DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key08);
  116. iChildId--;
  117. sql = @"UPDATE a SET iQtyRK =ISNULL(iQtyRK,0)+" + body.Quantity + @"
  118. FROM dbo.HY_DZ_BorrowOutBacks a
  119. WHERE a.Autoid='" + body.SourceDetailID + "'";
  120. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写借入单数量失败!");
  121. }
  122. iFatherIdTwo += "'" + iFatherId + "',";
  123. }
  124. #region 查询
  125. sql = @"
  126. select a.ID as ID,a.ID as IDs,cBusCode as SourceCode ,
  127. cCode as OtherInCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,
  128. cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime,
  129. cHandler as Checker,dnverifytime as CheckDateTime
  130. from rdrecord08 a
  131. left join Department c on a.cDepCode=c.cDepCode
  132. left join Warehouse d on a.cWhCode=d.cWhCode
  133. WHERE a.ID in ({0})
  134. select a.ID as IDs,b.AutoID as DetailID,irowno as Sequence,b.cInvCode as InvCode
  135. ,iQuantity as Quantity,b.iDebitIDs as SourceDetailID,iNum as Amount
  136. from rdrecord08 a
  137. left join rdrecords08 b on a.ID=b.ID
  138. left join Department c on a.cDepCode=c.cDepCode
  139. left join Warehouse d on a.cWhCode=d.cWhCode
  140. WHERE a.ID in ({0})";
  141. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  142. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  143. string RelationName = "details";
  144. DataRelation dr = new DataRelation(RelationName, ds.Tables[0].Columns["IDs"], ds.Tables[1].Columns["IDs"]);
  145. ds.Relations.Add(dr);
  146. msg = DBHelper.DataSetToJson(ds, RelationName);
  147. #endregion
  148. cmd.Transaction.Commit();
  149. return msg;
  150. }
  151. catch (Exception ex)
  152. {
  153. cmd.Transaction.Rollback();
  154. log.Error(ex.Message);
  155. throw new Exception(ex.Message);
  156. }
  157. finally
  158. {
  159. if (conn.State == ConnectionState.Open)
  160. {
  161. conn.Close();
  162. }
  163. conn.Dispose();
  164. }
  165. }
  166. /// <summary>
  167. /// 记录日志
  168. /// </summary>
  169. /// <param name="Bills"></param>
  170. private void LogInfo(List<ICSReturnDoc> Bills)
  171. {
  172. string HeadList = string.Empty;
  173. string BodyList = string.Empty;
  174. foreach (ICSReturnDoc head in Bills)
  175. {
  176. HeadList += "\r\n 借用单号:" + head.SourceCode + ",用户:" + head.User + "";
  177. }
  178. log.Info(HeadList);
  179. log.Info(BodyList);
  180. }
  181. }
  182. }