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

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