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

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