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

303 lines
17 KiB

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