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

420 lines
24 KiB

3 years ago
3 years ago
3 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
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. public class OtherOutDoc
  13. {
  14. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  15. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  16. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  17. private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"];
  18. private static string CRNALL = System.Configuration.ConfigurationManager.AppSettings["OtherOutCRDNAME"];
  19. /// <summary>
  20. /// 生成其他出库单
  21. /// </summary>
  22. /// <param name="Bills"></param>
  23. /// <returns></returns>
  24. public string CreateRd09(List<ICSOtherOutCreateDoc> Bills)
  25. {
  26. string msg = string.Empty;
  27. string connS = "";
  28. string sql = string.Empty;
  29. SqlConnection conn = new SqlConnection();
  30. DataSet ds = null;
  31. int num = 0;
  32. SqlCommand cmd = new SqlCommand();
  33. string iFatherIdTwo = "";
  34. if (Bills.Count <= 0)
  35. {
  36. throw new Exception("传送数据为空!");
  37. }
  38. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  39. foreach (string WorkPoint in result)
  40. {
  41. try
  42. {
  43. connS = string.Format(connString, WorkPoint);
  44. conn = new System.Data.SqlClient.SqlConnection(connS);
  45. conn.Open();
  46. SqlTransaction sqlTran = conn.BeginTransaction();
  47. cmd = new SqlCommand();
  48. cmd.Transaction = sqlTran;
  49. cmd.Connection = conn;
  50. foreach (ICSOtherOutCreateDoc head in Bills)
  51. {
  52. iFatherIdTwo = "";
  53. if (WorkPoint != head.WorkPoint)
  54. {
  55. continue;
  56. }
  57. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  58. throw new Exception("U8正在整理现存量,请稍后再试");
  59. string[] ss = head.WorkPoint.Split('_');
  60. ERPDB = ss[1];
  61. string[] dd = CRNALL.Split('~');
  62. string crdname = dd[0];
  63. string carname = dd[1];
  64. string surface = dd[2];
  65. num = head.details.Count();
  66. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  67. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint, cmd);
  68. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  69. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  70. DateTime date = DateTime.Now;
  71. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  72. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode, head.WorkPoint, cmd);
  73. ICSUserInfo userInfo = new ICSUserInfo();
  74. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  75. //取得out库单的默认显示模版
  76. string DEF_ID09 = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  77. //取得其他出库单单据 表头ID,表体DID
  78. VouchKey key09 = new VouchKey();
  79. #region 其他出库单表头
  80. key09.ID += 1;
  81. sql = @"INSERT INTO dbo.RdRecord09
  82. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,cCode ,
  83. cRdCode ,cDepCode ,cHandler ,bTransFlag ,cMaker ,dVeriDate ,bpufirst ,
  84. biafirst ,VT_ID ,bIsSTQc ,bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  85. iBG_OverFlag ,cBG_Auditor ,cBG_AuditTime ,ControlResult ,ireturncount ,
  86. iverifystate ,iswfcontrolled ,cModifyPerson ,dnmaketime ,dnverifytime ,
  87. iPrintCount)
  88. VALUES(@ID,0,'09','','',@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,
  89. @cRdCode,@cDepCode,@cHandler,0,@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),0,
  90. 0,@VT_ID,0,0,0,0,
  91. 0,'','',-1,0,
  92. 0,0,'',GETDATE(),GETDATE(),0) ";
  93. cmd.Parameters.Clear();
  94. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  95. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  96. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  97. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  98. cmd.Parameters.Add(new SqlParameter("@cDepCode", head.DepCode));
  99. cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName));
  100. cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName));
  101. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID09));
  102. cmd.CommandText = sql;
  103. try
  104. {
  105. cmd.ExecuteNonQuery();
  106. }
  107. catch (Exception)
  108. {
  109. log.Error("生成其他出库单失败!SQL:\r\n" + sql);
  110. throw new Exception("程序异常,请联系开发人员!");
  111. }
  112. #endregion
  113. foreach (ICSOtherOutCreateDocs body in head.details)
  114. {
  115. #region 3.5.1 表体
  116. sql = @"INSERT INTO dbo.rdrecords09
  117. ( AutoID ,ID ,cInvCode ,iQuantity ,iFlag ,bLPUseFree ,iRSRowNO ,iOriTrackID ,cBatch,iNNum,cItemCode,
  118. bCosting ,bVMIUsed ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,iposflag,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  119. VALUES(@AutoID,@ID,@cInvCode,@iQuantity,0,0,0,0,@cBatch,@iNNum,@cItemCode,
  120. 1,0,0,0,0,@irowno,1,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10)";
  121. cmd.Parameters.Clear();
  122. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  123. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  124. cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode));
  125. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  126. cmd.Parameters.Add(new SqlParameter("@iNNum", body.Amount.ToString("0.00")));
  127. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  128. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  129. cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
  130. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  131. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  132. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  133. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  134. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  135. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  136. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  137. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  138. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  139. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  140. cmd.CommandText = sql;
  141. try
  142. {
  143. cmd.ExecuteNonQuery();
  144. }
  145. catch (Exception)
  146. {
  147. log.Error("生成其他出库单表体失败!SQL:\r\n" + sql);
  148. throw new Exception("程序异常,请联系开发人员!");
  149. }
  150. #endregion
  151. //更新现存量
  152. key09.cBustypeUN = "其他出库";
  153. key09.cVouchTypeUN = "09";
  154. key09.TableName = "IA_ST_UnAccountVouch09";
  155. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key09);
  156. //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);
  157. DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, -body.Quantity, -body.Amount, body.cFree1,
  158. body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10,
  159. "", key09, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
  160. iFatherIdTwo = "'" + iFatherId + "',";
  161. iChildId--;
  162. }
  163. #region 查询
  164. sql = @"
  165. select DISTINCT a.ID as ID,a.ID as IDs,cBusCode as SourceCode ,
  166. cCode as OtherOutCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,
  167. cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime,
  168. cHandler as Checker,dnverifytime as CheckDateTime
  169. from rdrecord09 a
  170. left join Department c on a.cDepCode=c.cDepCode
  171. left join Warehouse d on a.cWhCode=d.cWhCode
  172. WHERE a.ID in ({0})
  173. select DISTINCT a.ID as IDs,b.AutoID as DetailID,irowno as Sequence,b.cInvCode as InvCode
  174. ,iQuantity as Quantity,b.iDebitIDs as SourceDetailID,iNum as Amount
  175. ,isnull(b.cItemCode,'') ProjectCode,
  176. isnull(b.cbatch,'') cBatch,
  177. '' version ,'' brand,
  178. isnull(b.cFree1,'') as cFree1,
  179. isnull(b.cFree2,'') as cFree2,
  180. isnull(b.cFree3,'') as cFree3,
  181. isnull(b.cFree4,'') as cFree4,
  182. isnull(b.cFree5,'') as cFree5,
  183. isnull(b.cFree6,'') as cFree6,
  184. isnull(b.cFree7,'') as cFree7,
  185. isnull(b.cFree8,'') as cFree8,
  186. isnull(b.cFree9,'') as cFree9,
  187. isnull(b.cFree10,'') as cFree10
  188. from rdrecord09 a
  189. left join rdrecords09 b on a.ID=b.ID
  190. left join Department c on a.cDepCode=c.cDepCode
  191. left join Warehouse d on a.cWhCode=d.cWhCode
  192. WHERE a.ID in ({0})";
  193. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  194. if (ds != null)
  195. ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd));
  196. else
  197. ds = DBHelper.SQlReturnDataSet(sql, cmd);
  198. #endregion
  199. }
  200. cmd.Transaction.Commit();
  201. }
  202. catch (Exception ex)
  203. {
  204. if (cmd.Transaction != null)
  205. cmd.Transaction.Rollback();
  206. log.Error(ex.Message);
  207. throw new Exception(ex.Message);
  208. }
  209. finally
  210. {
  211. if (conn.State == ConnectionState.Open)
  212. {
  213. conn.Close();
  214. }
  215. conn.Dispose();
  216. }
  217. }
  218. msg = JSON.DataSetToJson(ds, "details", "IDs");
  219. return msg;
  220. }
  221. /// <summary>
  222. /// 审核其他出库单
  223. /// </summary>
  224. /// <param name="Bills"></param>
  225. /// <returns></returns>
  226. public bool ConfirmRd09(List<ICSOtherOutDoc> Bills)
  227. {
  228. bool ResultFlag = false;
  229. string connS = "";
  230. SqlConnection conn = new SqlConnection();
  231. SqlCommand cmd = new SqlCommand();
  232. if (Bills.Count <= 0)
  233. {
  234. throw new Exception("传送数据为空!");
  235. }
  236. LogInfo(Bills);
  237. List<string> result = Bills.Select(t => t.WorkPoint).Distinct().ToList();
  238. foreach (string WorkPoint in result)
  239. {
  240. try
  241. {
  242. connS = string.Format(connString, WorkPoint);
  243. conn = new System.Data.SqlClient.SqlConnection(connS);
  244. conn.Open();
  245. SqlTransaction sqlTran = conn.BeginTransaction();
  246. cmd = new SqlCommand();
  247. cmd.Transaction = sqlTran;
  248. cmd.Connection = conn;
  249. foreach (ICSOtherOutDoc head in Bills)
  250. {
  251. if (WorkPoint != head.WorkPoint)
  252. continue;
  253. if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  254. throw new Exception("U8正在整理现存量,请稍后再试");
  255. string sql = "";
  256. ICSUserInfo userInfo = new ICSUserInfo();
  257. userInfo = DBHelper.GetPersonInfo(head.User, cmd);
  258. #region 检验单号是否存在
  259. sql = "select * from RdRecord09 where ID='" + head.ID + "'";
  260. DataTable dt = DBHelper.SQlReturnData(sql, cmd);
  261. if (dt != null && dt.Rows.Count > 0)
  262. {
  263. string cHandler = dt.Rows[0]["cHandler"].ToString();
  264. if (!string.IsNullOrEmpty(cHandler))
  265. {
  266. throw new Exception("单据ID:" + head.ID + "不是开立状态!");
  267. }
  268. }
  269. else
  270. {
  271. throw new Exception("单据ID:" + head.ID + "在U8中不存在!");
  272. }
  273. #endregion
  274. #region 审核其他出库单
  275. sql = @"UPDATE dbo.RdRecord09 SET cHandler='" + userInfo.UserName + @"' ,
  276. dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE() WHERE ID='" + head.ID + "'";
  277. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核其他出库单失败!");
  278. #endregion
  279. sql = "select * from dbo.RdRecords09 a inner join RdRecord09 b on a.ID=b.ID where b.ID='" + head.ID + "';";
  280. DataTable dtChecks = DBHelper.SQlReturnData(sql, cmd);
  281. if (head.UpdateStock)
  282. {
  283. #region 更新现存量
  284. for (int i = 0; i < dtChecks.Rows.Count; i++)
  285. {
  286. //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
  287. //sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + dtChecks.Rows[i]["cInvCode"].ToString() + "'AND cBatch=''and cWhCode='" + dtChecks.Rows[i]["cWhCode"].ToString() + "'";
  288. sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + dtChecks.Rows[i]["cInvCode"].ToString() + "'AND cBatch='" + dtChecks.Rows[i]["cBatch"].ToString() + @"'AND cFree1='" + dtChecks.Rows[i]["cFree1"].ToString() + @"' AND cFree2='" + dtChecks.Rows[i]["cFree2"].ToString() + @"' AND cFree3='" + dtChecks.Rows[i]["cFree3"].ToString() + @"' AND cFree4='" + dtChecks.Rows[i]["cFree4"].ToString() + @"' AND cFree5='" + dtChecks.Rows[i]["cFree5"].ToString() + @"' AND cFree6='" + dtChecks.Rows[i]["cFree6"].ToString() + @"' AND cFree7='" + dtChecks.Rows[i]["cFree7"].ToString() + @"' AND cFree8='" + dtChecks.Rows[i]["cFree8"].ToString() + @"' AND cFree9='" + dtChecks.Rows[i]["cFree9"].ToString() + @"' AND cFree10='" + dtChecks.Rows[i]["cFree10"].ToString() + @"' and cWhCode='" + dtChecks.Rows[i]["cWhCode"].ToString() + "'";
  289. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  290. if (dtItem != null && dtItem.Rows.Count > 0)
  291. {
  292. //if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cBatch))
  293. //{
  294. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.cInvCode);
  295. //}
  296. if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString()))
  297. {
  298. throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + dtChecks.Rows[i]["cInvCode"].ToString());
  299. }
  300. }
  301. else
  302. {
  303. throw new Exception("物料:" + dtChecks.Rows[i]["cInvCode"].ToString() + "在现存量表中不存在!");
  304. }
  305. VouchKey key = new VouchKey();
  306. key.cBustypeUN = "其他出库";
  307. key.cVouchTypeUN = "09";
  308. key.TableName = "IA_ST_UnAccountVouch09";
  309. //DBHelper.UpdateCurrentStock(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), dtChecks.Rows[i]["cWhCode"].ToString(),"", -Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString()), key);
  310. //DBHelper.UpdateCurrentStockNEW(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), dtChecks.Rows[i]["cWhCode"].ToString(), dtChecks.Rows[i]["cBatch"].ToString(), -Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString()), dtChecks.Rows[i]["cFree1"].ToString(), dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(), key);
  311. string inum = dtChecks.Rows[i]["iNum"].ToString();
  312. if (inum == "")
  313. {
  314. inum = "0";
  315. }
  316. DBHelper.UpdateCurrentStockCCGC(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), dtChecks.Rows[i]["cWhCode"].ToString(), dtChecks.Rows[i]["cBatch"].ToString(), -Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString()), -Convert.ToDecimal(inum), dtChecks.Rows[i]["cFree1"].ToString(),
  317. dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(),
  318. "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, Convert.ToInt32(dtChecks.Rows[i]["ID"].ToString()), Convert.ToInt32(dtChecks.Rows[i]["AutoID"].ToString()));
  319. //回写fOutQuantityy
  320. // sql = @"Update CurrentStock set fOutQuantity=isnull(fOutQuantity,0)-" + Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString()) + @"
  321. // where cInvCode='" + dtChecks.Rows[i]["cInvCode"].ToString() + "' and cWhCode='" + dtChecks.Rows[i]["cWhCode"].ToString() + "' ";
  322. //if (body.cBatch != null)
  323. //{
  324. // sql += "and cBatch='" + body.cBatch + "'";
  325. //}
  326. //else
  327. //{
  328. // sql += "and cBatch=''";
  329. //}
  330. //DBHelper.CmdExecuteNonQuery(sql, cmd, "回写fOutQuantity失败!");
  331. if (head.UpdateTodoQuantity == true)
  332. {
  333. #region 判断现存量是否足够
  334. sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fOutQuantity<0)
  335. BEGIN
  336. DECLARE @MSG NVARCHAR(100)
  337. SELECT @MSG='ERP待出库数量不足AutoID'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fOutQuantity<0
  338. RAISERROR(@MSG,16,1)
  339. END";
  340. cmd.CommandText = sql;
  341. cmd.ExecuteNonQuery();
  342. #endregion
  343. }
  344. }
  345. #endregion
  346. }
  347. }
  348. cmd.Transaction.Commit();
  349. }
  350. catch (Exception ex)
  351. {
  352. if (cmd.Transaction != null)
  353. cmd.Transaction.Rollback();
  354. log.Error(ex.Message);
  355. throw new Exception(ex.Message);
  356. }
  357. finally
  358. {
  359. if (conn.State == ConnectionState.Open)
  360. {
  361. conn.Close();
  362. }
  363. conn.Dispose();
  364. }
  365. }
  366. ResultFlag = true;
  367. return ResultFlag;
  368. }
  369. /// <summary>
  370. /// 记录日志
  371. /// </summary>
  372. /// <param name="Bills"></param>
  373. private void LogInfo(List<ICSOtherOutDoc> Bills)
  374. {
  375. string HeadList = string.Empty;
  376. string BodyList = string.Empty;
  377. foreach (ICSOtherOutDoc head in Bills)
  378. {
  379. HeadList += "\r\n 表头主键ID:" + head.ID + ",用户:" + head.User + ",站点:" + head.WorkPoint+"";
  380. }
  381. log.Info(HeadList);
  382. log.Info(BodyList);
  383. }
  384. }
  385. }