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

313 lines
16 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. private static string CRNALL = System.Configuration.ConfigurationManager.AppSettings["BrrowCRDNAME"];
  22. /// <summary>
  23. /// 借用(生成其他出库单)
  24. /// </summary>
  25. /// <param name="Bills"></param>
  26. /// <returns></returns>
  27. public string ConfirmRd09(List<ICSBrrowDoc> Bills)
  28. {
  29. string msg = string.Empty;
  30. string connS = "";
  31. string sql = string.Empty;
  32. SqlConnection conn = new SqlConnection();
  33. DataSet ds = null;
  34. int num = 0;
  35. SqlCommand cmd = new SqlCommand();
  36. string iFatherIdTwo = "";
  37. if (Bills.Count <= 0)
  38. {
  39. throw new Exception("传送数据为空!");
  40. }
  41. LogInfo(Bills);
  42. //log.Info("-------合并前-------");
  43. //log.Info("-------合并后-------");
  44. //List<RdRecord09> Vouchers = new List<RdRecord09>();
  45. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  46. foreach (string WorkPoint in result)
  47. {
  48. try
  49. {
  50. connS = string.Format(connString, WorkPoint);
  51. conn = new System.Data.SqlClient.SqlConnection(connS);
  52. conn.Open();
  53. SqlTransaction sqlTran = conn.BeginTransaction();
  54. cmd = new SqlCommand();
  55. cmd.Transaction = sqlTran;
  56. cmd.Connection = conn;
  57. foreach (ICSBrrowDoc head in Bills)
  58. {
  59. iFatherIdTwo = "";
  60. if (WorkPoint != head.WorkPoint)
  61. {
  62. continue;
  63. }
  64. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  65. throw new Exception("U8正在整理现存量,请稍后再试");
  66. string[] ss = head.WorkPoint.Split('_');
  67. ERPDB = ss[1];
  68. string[] dd = CRNALL.Split('~');
  69. string crdname = dd[0];
  70. string carname = dd[1];
  71. string surface = dd[2];
  72. num = head.details.Count();
  73. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  74. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  75. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  76. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  77. DateTime date = DateTime.Now;
  78. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  79. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode, head.WorkPoint);
  80. ICSUserInfo userInfo = new ICSUserInfo();
  81. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  82. //取得out库单的默认显示模版
  83. string DEF_ID09 = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  84. //取得其他出库单单据 表头ID,表体DID
  85. VouchKey key09 = new VouchKey();
  86. #region 其他出库单表头
  87. key09.ID += 1;
  88. sql = @"INSERT INTO dbo.RdRecord09
  89. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,cCode ,
  90. cRdCode ,cDepCode ,cHandler ,bTransFlag ,cMaker ,dVeriDate ,bpufirst ,
  91. biafirst ,VT_ID ,bIsSTQc ,bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  92. iBG_OverFlag ,cBG_Auditor ,cBG_AuditTime ,ControlResult ,ireturncount ,
  93. iverifystate ,iswfcontrolled ,cModifyPerson ,dnmaketime ,dnverifytime ,
  94. iPrintCount,cBusCode )
  95. VALUES(@ID,0,'09','','',@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,
  96. @cRdCode,@cDepCode,@cHandler,0,@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),0,
  97. 0,@VT_ID,0,0,0,0,
  98. 0,'','',-1,0,
  99. 0,0,'',GETDATE(),GETDATE(),0,@cBusCode) ";
  100. cmd.Parameters.Clear();
  101. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  102. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  103. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  104. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  105. cmd.Parameters.Add(new SqlParameter("@cDepCode", head.DepCode));
  106. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  107. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  108. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID09));
  109. cmd.Parameters.Add(new SqlParameter("@cBusCode", head.SourceCode));
  110. cmd.CommandText = sql;
  111. try
  112. {
  113. cmd.ExecuteNonQuery();
  114. }
  115. catch (Exception)
  116. {
  117. log.Error("生成其他出库单失败!SQL:\r\n" + sql);
  118. throw new Exception("程序异常,请联系开发人员!");
  119. }
  120. #endregion
  121. foreach (ICSBrrowDocs body in head.details)
  122. {
  123. #region 3.5.1 表体
  124. sql = @"INSERT INTO dbo.rdrecords09
  125. ( AutoID ,ID ,cInvCode ,iQuantity ,iFlag ,bLPUseFree ,iRSRowNO ,iOriTrackID ,cBatch,iNNum,cItemCode,
  126. bCosting ,bVMIUsed ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,iposflag,iDebitIDs ,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  127. VALUES(@AutoID,@ID,@cInvCode,@iQuantity,0,0,0,0,@cBatch,@iNNum,@cItemCode,
  128. 1,0,0,0,0,@irowno,1,@iDebitIDs,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10)";
  129. cmd.Parameters.Clear();
  130. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  131. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  132. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  133. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  134. cmd.Parameters.Add(new SqlParameter("@iNNum", body.Amount.ToString("0.00")));
  135. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  136. cmd.Parameters.Add(new SqlParameter("@iDebitIDs", body.SourceDetailID));
  137. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  138. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  139. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  140. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  141. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  142. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  143. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  144. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  145. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  146. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  147. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  148. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  149. cmd.CommandText = sql;
  150. try
  151. {
  152. cmd.ExecuteNonQuery();
  153. }
  154. catch (Exception)
  155. {
  156. log.Error("生成其他出库单表体失败!SQL:\r\n" + sql);
  157. throw new Exception("程序异常,请联系开发人员!");
  158. }
  159. #endregion
  160. sql = @"select isnull(iQtyRK,0)fOutQuantity,isnull(iQuantity,0)iQuantity,ISNULL(iNum, 0)iNum,ISNULL(NiNum, 0)NiNum,ISNULL(iNum, 0)-ISNULL(NiNum, 0) SiNum from HY_DZ_BorrowOutBacks a
  161. LEFT JOIN (select sum(iNum) NiNum,iDebitIDs from rdrecords09 GROUP BY iDebitIDs) b on a.AutoID=b.iDebitIDs
  162. where a.AutoID='" + body.SourceDetailID + "'";
  163. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  164. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  165. {
  166. foreach (DataRow item in dtDisCheck.Rows)
  167. {
  168. if (Convert.ToDecimal(item["fOutQuantity"].ToString()) == Convert.ToDecimal(item["iQuantity"].ToString()))
  169. {
  170. if (Convert.ToDecimal(item["iNum"].ToString()) != 0)
  171. {
  172. if (Convert.ToDecimal(item["SiNum"].ToString()) != 0)
  173. {
  174. sql = @"Update rdrecords09 set iNum=isnull(iNum,0)+(" + item["SiNum"].ToString() + ") where autoid='" + iChildId + "' ";
  175. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item["SiNum"].ToString()));
  176. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  177. }
  178. }
  179. }
  180. }
  181. }
  182. sql = @"UPDATE a SET iQtyOut=ISNULL(iQtyOut,0)+" + body.Quantity + @"
  183. FROM dbo.HY_DZ_BorrowOuts a
  184. WHERE a.Autoid='" + body.SourceDetailID + "'";
  185. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写借入单数量失败!");
  186. //更新现存量
  187. key09.cBustypeUN = "调拨出库";
  188. key09.cVouchTypeUN = "09";
  189. key09.TableName = "IA_ST_UnAccountVouch09";
  190. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key09);
  191. //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);
  192. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, -body.Quantity, -body.Amount, body.cFree1,
  193. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  194. "", key09, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  195. iFatherIdTwo = "'" + iFatherId + "',";
  196. iChildId--;
  197. }
  198. //OM_MODetails
  199. #region 查询
  200. sql = @"
  201. select DISTINCT a.ID as ID,a.ID as IDs,cBusCode as SourceCode ,
  202. cCode as OtherOutCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,
  203. cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime,
  204. cHandler as Checker,dnverifytime as CheckDateTime
  205. from rdrecord09 a
  206. left join Department c on a.cDepCode=c.cDepCode
  207. left join Warehouse d on a.cWhCode=d.cWhCode
  208. WHERE a.ID in ({0})
  209. select DISTINCT a.ID as IDs,b.AutoID as DetailID,irowno as Sequence,b.cInvCode as InvCode
  210. ,iQuantity as Quantity,b.iDebitIDs as SourceDetailID,iNum as Amount
  211. ,isnull(b.cItemCode,'') ProjectCode,
  212. isnull(b.cbatch,'') cBatch,
  213. '' version ,'' brand,
  214. isnull(b.cFree1,'') as cFree1,
  215. isnull(b.cFree2,'') as cFree2,
  216. isnull(b.cFree3,'') as cFree3,
  217. isnull(b.cFree4,'') as cFree4,
  218. isnull(b.cFree5,'') as cFree5,
  219. isnull(b.cFree6,'') as cFree6,
  220. isnull(b.cFree7,'') as cFree7,
  221. isnull(b.cFree8,'') as cFree8,
  222. isnull(b.cFree9,'') as cFree9,
  223. isnull(b.cFree10,'') as cFree10
  224. from rdrecord09 a
  225. left join rdrecords09 b on a.ID=b.ID
  226. left join Department c on a.cDepCode=c.cDepCode
  227. left join Warehouse d on a.cWhCode=d.cWhCode
  228. WHERE a.ID in ({0})";
  229. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  230. if (ds != null)
  231. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  232. else
  233. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  234. #endregion
  235. }
  236. cmd.Transaction.Commit();
  237. }
  238. catch (Exception ex)
  239. {
  240. cmd.Transaction.Rollback();
  241. log.Error(ex.Message);
  242. throw new Exception(ex.Message);
  243. }
  244. finally
  245. {
  246. if (conn.State == ConnectionState.Open)
  247. {
  248. conn.Close();
  249. }
  250. conn.Dispose();
  251. }
  252. }
  253. msg = JSON.DataSetToJson(ds, "details", "IDs");
  254. return msg;
  255. }
  256. /// <summary>
  257. /// 记录日志
  258. /// </summary>
  259. /// <param name="Bills"></param>
  260. private void LogInfo(List<ICSBrrowDoc> Bills)
  261. {
  262. string HeadList = string.Empty;
  263. string BodyList = string.Empty;
  264. foreach (ICSBrrowDoc head in Bills)
  265. {
  266. HeadList += "\r\n 借用单号:" + head.SourceCode + ",用户:" + head.User + ",站点:" + head.WorkPoint + "";
  267. }
  268. log.Info(HeadList);
  269. log.Info(BodyList);
  270. }
  271. }
  272. }