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

200 lines
10 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 BrrowDoc
  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 ConfirmRd09(List<ICSBrrowDoc> 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. //log.Info("-------合并前-------");
  41. //log.Info("-------合并后-------");
  42. //List<RdRecord09> Vouchers = new List<RdRecord09>();
  43. foreach (ICSBrrowDoc head in Bills)
  44. {
  45. num = head.details.Count();
  46. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + num + "");
  47. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  48. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  49. DateTime date = DateTime.Now;
  50. string iBaseCodeLen = DBHelper.GetAllRDCode("0302", "" + date + "", "admin");
  51. string cRDcode = DBHelper.GetRDCode("样品出库", cmd);
  52. //取得out库单的默认显示模版
  53. string DEF_ID09 = DBHelper.GetDefaultTemplate("0302", cmd);
  54. //取得其他出库单单据 表头ID,表体DID
  55. VouchKey key09 = new VouchKey();
  56. #region 其他出库单表头
  57. key09.ID += 1;
  58. sql = @"INSERT INTO dbo.RdRecord09
  59. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,cCode ,
  60. cRdCode ,cDepCode ,cHandler ,bTransFlag ,cMaker ,dVeriDate ,bpufirst ,
  61. biafirst ,VT_ID ,bIsSTQc ,bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  62. iBG_OverFlag ,cBG_Auditor ,cBG_AuditTime ,ControlResult ,ireturncount ,
  63. iverifystate ,iswfcontrolled ,cModifyPerson ,dnmaketime ,dnverifytime ,
  64. iPrintCount,cBusCode )
  65. VALUES(@ID,0,'09','','',@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,
  66. @cRdCode,@cDepCode,@cHandler,0,@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),0,
  67. 0,@VT_ID,0,0,0,0,
  68. 0,'','',-1,0,
  69. 0,0,'',GETDATE(),GETDATE(),0,@cBusCode) ";
  70. cmd.Parameters.Clear();
  71. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  72. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  73. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  74. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  75. cmd.Parameters.Add(new SqlParameter("@cDepCode", head.DepCode));
  76. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  77. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  78. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID09));
  79. cmd.Parameters.Add(new SqlParameter("@cBusCode", head.SourceCode));
  80. cmd.CommandText = sql;
  81. try
  82. {
  83. cmd.ExecuteNonQuery();
  84. }
  85. catch (Exception ex)
  86. {
  87. log.Error("生成其他出库单失败!SQL:\r\n" + sql);
  88. throw new Exception("程序异常,请联系开发人员!");
  89. }
  90. #endregion
  91. foreach (ICSBrrowDocs body in head.details)
  92. {
  93. #region 3.5.1 表体
  94. sql = @"INSERT INTO dbo.rdrecords09
  95. ( AutoID ,ID ,cInvCode ,iQuantity ,iFlag ,bLPUseFree ,iRSRowNO ,iOriTrackID ,
  96. bCosting ,bVMIUsed ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,iposflag,iDebitIDs )
  97. VALUES(@AutoID,@ID,@cInvCode,@iQuantity,0,0,0,0,
  98. 1,0,0,0,0,@irowno,1,@iDebitIDs)";
  99. cmd.Parameters.Clear();
  100. cmd.Parameters.Add(new SqlParameter("@AutoID",iChildId));
  101. cmd.Parameters.Add(new SqlParameter("@ID",iFatherId));
  102. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  103. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  104. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  105. cmd.Parameters.Add(new SqlParameter("@iDebitIDs", body.SourceDetailID));
  106. cmd.CommandText = sql;
  107. try
  108. {
  109. cmd.ExecuteNonQuery();
  110. }
  111. catch (Exception ex)
  112. {
  113. log.Error("生成其他出库单表体失败!SQL:\r\n" + sql);
  114. throw new Exception("程序异常,请联系开发人员!");
  115. }
  116. #endregion
  117. //更新现存量
  118. key09.cBustypeUN = "调拨出库";
  119. key09.cVouchTypeUN = "09";
  120. key09.TableName = "IA_ST_UnAccountVouch09";
  121. DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key09);
  122. iChildId--;
  123. sql = @"UPDATE a SET iQtyOut=ISNULL(iQtyOut,0)+" + body.Quantity + @"
  124. FROM dbo.HY_DZ_BorrowOuts a
  125. WHERE a.Autoid='" + body.SourceDetailID + "'";
  126. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写借入单数量失败!");
  127. }
  128. iFatherIdTwo += "'" + iFatherId + "',";
  129. //OM_MODetails
  130. }
  131. #region 查询
  132. sql = @"
  133. select a.ID as ID,a.ID as IDs,cBusCode as SourceCode ,
  134. cCode as OtherOutCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,
  135. cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime,
  136. cHandler as Checker,dnverifytime as CheckDateTime
  137. from rdrecord09 a
  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. select a.ID as IDs,b.AutoID as DetailID,irowno as Sequence,b.cInvCode as InvCode
  142. ,iQuantity as Quantity,b.iDebitIDs as SourceDetailID,iNum as Amount
  143. from rdrecord09 a
  144. left join rdrecords09 b on a.ID=b.ID
  145. left join Department c on a.cDepCode=c.cDepCode
  146. left join Warehouse d on a.cWhCode=d.cWhCode
  147. WHERE a.ID in ({0})";
  148. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  149. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  150. string RelationName = "details";
  151. DataRelation dr = new DataRelation(RelationName, ds.Tables[0].Columns["IDs"], ds.Tables[1].Columns["IDs"]);
  152. ds.Relations.Add(dr);
  153. msg = DBHelper.DataSetToJson(ds, RelationName);
  154. #endregion
  155. cmd.Transaction.Commit();
  156. return msg;
  157. }
  158. catch (Exception ex)
  159. {
  160. cmd.Transaction.Rollback();
  161. log.Error(ex.Message);
  162. throw new Exception(ex.Message);
  163. }
  164. finally
  165. {
  166. if (conn.State == ConnectionState.Open)
  167. {
  168. conn.Close();
  169. }
  170. conn.Dispose();
  171. }
  172. }
  173. /// <summary>
  174. /// 记录日志
  175. /// </summary>
  176. /// <param name="Bills"></param>
  177. private void LogInfo(List<ICSBrrowDoc> Bills)
  178. {
  179. string HeadList = string.Empty;
  180. string BodyList = string.Empty;
  181. foreach (ICSBrrowDoc head in Bills)
  182. {
  183. HeadList += "\r\n 借用单号:" + head.SourceCode + ",用户:" + head.User + "";
  184. }
  185. log.Info(HeadList);
  186. log.Info(BodyList);
  187. }
  188. }
  189. }