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

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