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

321 lines
17 KiB

3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 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. 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, cmd);
  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, cmd);
  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. select @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. from HY_DZ_BorrowOut where ccode=@SourceCode
  101. ";
  102. cmd.Parameters.Clear();
  103. cmd.Parameters.Add(new SqlParameter("SourceCode", head.SourceCode));
  104. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  105. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  106. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  107. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  108. cmd.Parameters.Add(new SqlParameter("@cDepCode", head.DepCode));
  109. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  110. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  111. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID09));
  112. cmd.Parameters.Add(new SqlParameter("@cBusCode", head.SourceCode));
  113. cmd.CommandText = sql;
  114. try
  115. {
  116. cmd.ExecuteNonQuery();
  117. }
  118. catch (Exception)
  119. {
  120. log.Error("生成其他出库单失败!SQL:\r\n" + sql);
  121. throw new Exception("程序异常,请联系开发人员!");
  122. }
  123. #endregion
  124. foreach (ICSBrrowDocs body in head.details)
  125. {
  126. #region 3.5.1 表体
  127. sql = @"INSERT INTO dbo.rdrecords09
  128. ( AutoID ,ID ,cInvCode ,iQuantity ,iFlag ,bLPUseFree ,iRSRowNO ,iOriTrackID ,cBatch,iNNum,cItemCode,
  129. bCosting ,bVMIUsed ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,iposflag,iDebitIDs ,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  130. select @AutoID,@ID,@cInvCode,@iQuantity,0,0,0,0,@cBatch,@iNNum,@cItemCode,
  131. 1,0,0,0,0,@irowno,1,@iDebitIDs,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  132. from HY_DZ_BorrowOuts where AutoID= @SourceDetailID
  133. ";
  134. cmd.Parameters.Clear();
  135. cmd.Parameters.Add(new SqlParameter("@SourceDetailID", body.SourceDetailID));
  136. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  137. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  138. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  139. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  140. cmd.Parameters.Add(new SqlParameter("@iNNum", body.Amount.ToString("0.00")));
  141. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  142. cmd.Parameters.Add(new SqlParameter("@iDebitIDs", body.SourceDetailID));
  143. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  144. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  145. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  146. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  147. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  148. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  149. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  150. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  151. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  152. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  153. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  154. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  155. cmd.CommandText = sql;
  156. try
  157. {
  158. cmd.ExecuteNonQuery();
  159. }
  160. catch (Exception)
  161. {
  162. log.Error("生成其他出库单表体失败!SQL:\r\n" + sql);
  163. throw new Exception("程序异常,请联系开发人员!");
  164. }
  165. #endregion
  166. 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
  167. LEFT JOIN (select sum(iNum) NiNum,iDebitIDs from rdrecords09 GROUP BY iDebitIDs) b on a.AutoID=b.iDebitIDs
  168. where a.AutoID='" + body.SourceDetailID + "'";
  169. DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd);
  170. if (dtDisCheck != null && dtDisCheck.Rows.Count > 0)
  171. {
  172. foreach (DataRow item in dtDisCheck.Rows)
  173. {
  174. if (Convert.ToDecimal(item["fOutQuantity"].ToString()) == Convert.ToDecimal(item["iQuantity"].ToString()))
  175. {
  176. if (Convert.ToDecimal(item["iNum"].ToString()) != 0)
  177. {
  178. if (Convert.ToDecimal(item["SiNum"].ToString()) != 0)
  179. {
  180. sql = @"Update rdrecords09 set iNum=isnull(iNum,0)+(" + item["SiNum"].ToString() + ") where autoid='" + iChildId + "' ";
  181. body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item["SiNum"].ToString()));
  182. DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!");
  183. }
  184. }
  185. }
  186. }
  187. }
  188. sql = @"UPDATE a SET iQtyOut=ISNULL(iQtyOut,0)+" + body.Quantity + @"
  189. FROM dbo.HY_DZ_BorrowOuts a
  190. WHERE a.Autoid='" + body.SourceDetailID + "'";
  191. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写借入单数量失败!");
  192. //更新现存量
  193. key09.cBustypeUN = "调拨出库";
  194. key09.cVouchTypeUN = "09";
  195. key09.TableName = "IA_ST_UnAccountVouch09";
  196. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key09);
  197. //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);
  198. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, -body.Quantity, -body.Amount, body.cFree1,
  199. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  200. "", key09, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  201. iFatherIdTwo = "'" + iFatherId + "',";
  202. iChildId--;
  203. }
  204. //OM_MODetails
  205. #region 查询
  206. sql = @"
  207. select DISTINCT a.ID as ID,a.ID as IDs,cBusCode as SourceCode ,
  208. cCode as OtherOutCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,
  209. cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime,
  210. cHandler as Checker,dnverifytime as CheckDateTime
  211. from rdrecord09 a
  212. left join Department c on a.cDepCode=c.cDepCode
  213. left join Warehouse d on a.cWhCode=d.cWhCode
  214. WHERE a.ID in ({0})
  215. select DISTINCT a.ID as IDs,b.AutoID as DetailID,irowno as Sequence,b.cInvCode as InvCode
  216. ,iQuantity as Quantity,b.iDebitIDs as SourceDetailID,iNum as Amount
  217. ,isnull(b.cItemCode,'') ProjectCode,
  218. isnull(b.cbatch,'') cBatch,
  219. '' version ,'' brand,
  220. isnull(b.cFree1,'') as cFree1,
  221. isnull(b.cFree2,'') as cFree2,
  222. isnull(b.cFree3,'') as cFree3,
  223. isnull(b.cFree4,'') as cFree4,
  224. isnull(b.cFree5,'') as cFree5,
  225. isnull(b.cFree6,'') as cFree6,
  226. isnull(b.cFree7,'') as cFree7,
  227. isnull(b.cFree8,'') as cFree8,
  228. isnull(b.cFree9,'') as cFree9,
  229. isnull(b.cFree10,'') as cFree10
  230. from rdrecord09 a
  231. left join rdrecords09 b on a.ID=b.ID
  232. left join Department c on a.cDepCode=c.cDepCode
  233. left join Warehouse d on a.cWhCode=d.cWhCode
  234. WHERE a.ID in ({0})";
  235. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  236. if (ds != null)
  237. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  238. else
  239. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  240. #endregion
  241. cmd.CommandType = CommandType.Text;
  242. cmd.Parameters.Clear();
  243. }
  244. cmd.Transaction.Commit();
  245. }
  246. catch (Exception ex)
  247. {
  248. if (cmd.Transaction != null)
  249. cmd.Transaction.Rollback();
  250. log.Error(ex.Message);
  251. throw new Exception(ex.Message);
  252. }
  253. finally
  254. {
  255. if (conn.State == ConnectionState.Open)
  256. {
  257. conn.Close();
  258. }
  259. conn.Dispose();
  260. }
  261. }
  262. msg = JSON.DataSetToJson(ds, "details", "IDs");
  263. return msg;
  264. }
  265. /// <summary>
  266. /// 记录日志
  267. /// </summary>
  268. /// <param name="Bills"></param>
  269. private void LogInfo(List<ICSBrrowDoc> Bills)
  270. {
  271. string HeadList = string.Empty;
  272. string BodyList = string.Empty;
  273. foreach (ICSBrrowDoc head in Bills)
  274. {
  275. HeadList += "\r\n 借用单号:" + head.SourceCode + ",用户:" + head.User + ",站点:" + head.WorkPoint + "";
  276. }
  277. log.Info(HeadList);
  278. log.Info(BodyList);
  279. }
  280. }
  281. }