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

468 lines
22 KiB

3 years ago
  1. using ICSSoft.Common;
  2. using ICSSoft.Entity;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ICSSoft.DataProject
  12. {
  13. /// <summary>
  14. /// 委外发料
  15. /// </summary>
  16. public class OutsourcingIssueDoc
  17. {
  18. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  19. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
  20. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  21. private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"];
  22. public string Get(List<ICSOutsourcingIssueDoc> infos)
  23. {
  24. List<ICSOutsourcingIssueDoc> szJson = new List<ICSOutsourcingIssueDoc>();
  25. DataTable dt = null;
  26. string json = "";
  27. if (infos.Count <= 0)
  28. {
  29. throw new Exception("传送数据为空!");
  30. }
  31. string res = string.Empty;
  32. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  33. conn.Open();
  34. SqlTransaction sqlTran = conn.BeginTransaction();
  35. SqlCommand cmd = new SqlCommand();
  36. cmd.Transaction = sqlTran;
  37. cmd.Connection = conn;
  38. try
  39. {
  40. string sql = string.Empty;
  41. foreach (ICSOutsourcingIssueDoc info in infos)
  42. {
  43. if (info.MTime < new DateTime(2000, 01, 01))
  44. throw new Exception("请输入正确的操作时间:" + info.MTime);
  45. sql = @" select a.ID,iMPoIds,iordercode,a.cCode,a.cDepCode,c.cDepName,a.cWhCode,
  46. d.cWhName,a.cSource,a.cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,
  47. b.AutoID ,b.irowno,b.cInvCode,b.iQuantity,e.MoDetailsID,b.iNum,e.csocode
  48. from rdrecord11 a
  49. left join rdrecords11 b on a.ID=b.ID
  50. left join Department c on a.cDepCode=c.cDepCode
  51. left join Warehouse d on a.cWhCode=d.cWhCode
  52. left join OM_MODetails e on b.iOMoDID=e.MoDetailsID
  53. where a.cBusType=''";
  54. if (!string.IsNullOrWhiteSpace(info.IssueCode))
  55. {
  56. sql += " and a.cCode='{0}'";
  57. }
  58. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  59. {
  60. sql += " and ISNULL(a.dnmodifytime,ISNULL(a.dnverifytime, ISNULL(a.dnmodifytime, a.dnmaketime)))>='{1}'";
  61. }
  62. if (!string.IsNullOrWhiteSpace(info.User))
  63. {
  64. sql += "and a.CMAKER='{2}'";
  65. }
  66. sql = string.Format(sql, info.IssueCode, info.MTime, info.User);
  67. dt = DBHelper.SQlReturnData(sql, cmd);
  68. json = JsonConvert.SerializeObject(dt);
  69. if (dt.Rows.Count <= 0 || dt == null)
  70. throw new Exception("委外发料单:" + info.IssueCode + ",无信息!");
  71. }
  72. cmd.Transaction.Commit();
  73. return json;
  74. }
  75. catch (Exception ex)
  76. {
  77. cmd.Transaction.Rollback();
  78. log.Error(ex.Message);
  79. throw new Exception(ex.Message);
  80. }
  81. finally
  82. {
  83. if (conn.State == ConnectionState.Open)
  84. {
  85. conn.Close();
  86. }
  87. conn.Dispose();
  88. }
  89. }
  90. public string CreateOutsourcingIssueDoc(List<ICSOutsourcingIssueDoc> Bills)
  91. {
  92. bool ResultFlag = false;
  93. string msg = "";
  94. int num = 0;
  95. //取得out库单的默认显示模版
  96. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "PuArrival", "" + num + "");
  97. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  98. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  99. DateTime date = DateTime.Now;
  100. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  101. //取得采购入库单单据 表头ID,表体DID
  102. VouchKey key = new VouchKey();
  103. foreach (ICSOutsourcingIssueDoc head in Bills)
  104. {
  105. num = head.details.Count();
  106. }
  107. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  108. conn.Open();
  109. SqlTransaction sqlTran = conn.BeginTransaction();
  110. SqlCommand cmd = new SqlCommand();
  111. cmd.Transaction = sqlTran;
  112. cmd.Connection = conn;
  113. try
  114. {
  115. if (Bills.Count <= 0)
  116. {
  117. throw new Exception("传送数据为空!");
  118. }
  119. LogInfo(Bills);
  120. LogInfo(Bills);
  121. foreach (ICSOutsourcingIssueDoc head in Bills)
  122. {
  123. string sql = string.Empty;
  124. if (head.details.Count <= 0)
  125. {
  126. throw new Exception("表体信息不存在!");
  127. }
  128. #region 判断委外订单是否已关闭
  129. sql = @"SELECT distinct isnull(b.cbCloser,'') as CloseUser
  130. from dbo.OM_MOMaterials a
  131. INNER JOIN OM_MODetails b ON a.MoDetailsID = b.MoDetailsID
  132. where a.MOMaterialsID='" + head.details[0].SourceDetailID + "'";
  133. DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  134. if (dtClose != null && dtClose.Rows.Count > 0)
  135. {
  136. if (!string.IsNullOrEmpty(dtClose.Rows[0][0].ToString()))
  137. {
  138. throw new Exception("该委外订单行已关闭,无法领料!");
  139. }
  140. }
  141. #endregion
  142. #region 判断物料是否超额领取
  143. foreach (ICSOutsourcingIssueDocs body in head.details)
  144. {
  145. sql = @"SELECT isnull(b.fOutExcess,0) as fOutExcess,isnull(a.iSendQTY,0) as IssQty,a.iQuantity
  146. FROM dbo.OM_MOMaterials a
  147. LEFT JOIN dbo.Inventory b ON a.cInvCode=b.cInvCode
  148. WHERE a.MOMaterialsID='" + body.SourceDetailID + "'";
  149. DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  150. // if (dtQty != null && dtQty.Rows.Count > 0)
  151. // {
  152. // //可超领比率
  153. // decimal fOutExcess = Convert.ToDecimal(dtQty.Rows[0]["fOutExcess"]);
  154. // //已领数量
  155. // decimal IssQty = Convert.ToDecimal(dtQty.Rows[0]["IssQty"]);
  156. // //应领数量
  157. // decimal Qty = Convert.ToDecimal(dtQty.Rows[0]["iQuantity"]);
  158. // //当前可总领数量
  159. // decimal TotalQty = Qty + fOutExcess * Qty;
  160. // if (head.IsReturn != "1")
  161. // {
  162. // if (IssQty + body.Quantity > TotalQty)
  163. // {
  164. // throw new Exception("物料领取超过当前可领用最大数,AllocateId:" + body.SourceDetailID);
  165. // }
  166. // }
  167. // else
  168. // {
  169. // if (IssQty < body.Quantity)
  170. // {
  171. // throw new Exception("物料退料超过当前已领用最大数,AllocateId:" + body.SourceDetailID);
  172. // }
  173. // }
  174. // }
  175. // else
  176. // {
  177. // throw new Exception("委外订单行子件不存在!AllocateId:" + body.SourceDetailID);
  178. // }
  179. //}
  180. #endregion
  181. #region 委外材料出库单表头
  182. sql = @"
  183. INSERT INTO dbo.rdrecord11
  184. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,
  185. cWhCode ,dDate ,cCode ,cRdCode ,cDepCode,
  186. cPersonCode,cHandler ,bTransFlag ,cMaker ,dVeriDate ,
  187. bpufirst ,biafirst ,VT_ID ,bIsSTQc ,cPsPcode ,
  188. cMPoCode ,iproorderid ,bFromPreYear ,bIsLsQuery ,bIsComplement ,
  189. iDiscountTaxType ,ireturncount ,iverifystate ,iswfcontrolled ,dnmaketime ,
  190. dnverifytime ,bredvouch ,iPrintCount,cVenCode,iMQuantity)
  191. SELECT distinct @ID,0,'11','','',
  192. @cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,'22',c.cDepCode,
  193. c.cPersonCode,@cHandler,0,@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),
  194. 0,0,@VT_ID,0,b.cInvCode,
  195. c.cCode,a.MoId,0,0,0,
  196. 0,0,0,0,GETDATE(),
  197. GETDATE(),0,0,c.cVenCode,b.iQuantity
  198. from dbo.OM_MOMaterials a
  199. INNER JOIN dbo.OM_MODetails b ON a.MoDetailsID = b.MoDetailsID
  200. LEFT JOIN dbo.OM_MOMain c ON b.MOID = c.MOID
  201. WHERE a.MOMaterialsID='" + head.details[0].SourceDetailID + "'";
  202. cmd.Parameters.Clear();
  203. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  204. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  205. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  206. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  207. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  208. cmd.Parameters.Add(new SqlParameter("@VT_ID", iChildId));
  209. cmd.CommandText = sql;
  210. try
  211. {
  212. int count = cmd.ExecuteNonQuery();
  213. if (count <= 0)
  214. {
  215. log.Error("生成委外发料单表头失败,受影响行数<=0;");
  216. throw new Exception("生成委外发料单表头失败,受影响行数<=0;");
  217. }
  218. }
  219. catch (Exception ex)
  220. {
  221. log.Error("生成委外发料单表头失败!SQL:\r\n" + sql, ex);
  222. throw new Exception("生成委外发料单表头失败!SQL:\r\n" + sql, ex);
  223. }
  224. }
  225. #endregion
  226. #region 委外材料出库单表体
  227. int irowno = 0;
  228. string iNQuantity = "";
  229. foreach (ICSOutsourcingIssueDocs body in head.details)
  230. {
  231. irowno += 1;
  232. iChildId -= 1;
  233. //if (head.IsReturn == "1")
  234. //{
  235. // body.iQuantity = -body.iQuantity;
  236. // iNQuantity = "-a.iQuantity";
  237. //}
  238. //else
  239. //{
  240. iNQuantity = "a.iQuantity";
  241. //}
  242. //if (head.IsReturn == "0")
  243. //{
  244. // //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
  245. // sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.cInvCode + "'AND cBatch='" + body.cBatch + "'and cWhCode='" + head.cWhCode + "'";
  246. // DataTable dtItem = U8Helper.SQlReturnData(sql, cmd);
  247. // if (dtItem != null && dtItem.Rows.Count > 0)
  248. // {
  249. // if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cBatch))
  250. // {
  251. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.cInvCode);
  252. // }
  253. // if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < body.iQuantity)
  254. // {
  255. // throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.cInvCode);
  256. // }
  257. // }
  258. // else
  259. // {
  260. // throw new Exception("物料:" + body.cInvCode + "在现存量表中不存在!");
  261. // }
  262. //}
  263. sql = @"INSERT INTO dbo.rdrecords11
  264. ( AutoID,ID ,cInvCode,iQuantity ,cBatch ,
  265. iFlag ,cItemCode ,cName ,iNQuantity ,iMPoIds ,
  266. bLPUseFree ,iOriTrackID ,bCosting ,bVMIUsed ,cmocode ,
  267. invcode ,imoseq ,iopseq ,iExpiratDateCalcu ,iorderdid ,
  268. iordertype ,isotype ,ipesodid ,ipesotype ,cpesocode ,
  269. ipesoseq ,irowno ,bcanreplace ,iposflag,iOMoDID,
  270. iOMoMID,comcode)
  271. SELECT distinct
  272. @AutoID,@ID,a.cInvCode,@iQuantity,@cBatch,
  273. 0,null,null,@iQuantity,NULL,
  274. 0,0,1,0,NULL,
  275. b.cInvCode,NULL,NULL,0,0,
  276. 0,0,a.MOMaterialsID,'6',c.cCode,
  277. NULL, @irowno,0,NULL,b.MODetailsID,
  278. a.MOMaterialsID,c.cCode
  279. FROM dbo.OM_MOMaterials a
  280. INNER JOIN dbo.OM_MODetails b ON a.MoDetailsID=b.MODetailsID
  281. INNER JOIN dbo.OM_MOMain c ON b.MOID=c.MOID
  282. WHERE a.MOMaterialsID='" + body.SourceDetailID + "'";
  283. cmd.Parameters.Clear();
  284. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  285. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  286. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  287. //cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  288. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  289. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  290. cmd.Parameters.Add(new SqlParameter("@iNQuantity", iNQuantity));
  291. cmd.CommandText = sql;
  292. try
  293. {
  294. int count = cmd.ExecuteNonQuery();
  295. if (count <= 0)
  296. {
  297. log.Error("生成委外发料单表体失败,受影响行数<=0;");
  298. throw new Exception("生成委外发料单表体失败,受影响行数<=0;");
  299. }
  300. }
  301. catch (Exception ex)
  302. {
  303. log.Error("生成委外发料单表体失败" + sql, ex);
  304. throw new Exception("生成委外发料单表体失败 " + sql, ex);
  305. }
  306. //更新现存量
  307. key.cBustypeUN = "委外发料";
  308. key.cVouchTypeUN = "11";
  309. key.TableName = "IA_ST_UnAccountVouch11";
  310. DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key);
  311. #region 回写工单子件已领数量
  312. sql = "update OM_MOMaterials set iSendQTY=isnull(iSendQTY,0)+" + body.Quantity + " where MOMaterialsID='" + body.SourceDetailID + "' ";
  313. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写委外订单子件已领数量失败!");
  314. #endregion
  315. }
  316. #endregion
  317. }
  318. cmd.Transaction.Commit();
  319. ResultFlag = true;
  320. return msg;
  321. }
  322. catch (Exception ex)
  323. {
  324. cmd.Transaction.Rollback();
  325. log.Error(ex.Message);
  326. throw new Exception(ex.Message);
  327. }
  328. finally
  329. {
  330. if (conn.State == ConnectionState.Open)
  331. {
  332. conn.Close();
  333. }
  334. conn.Dispose();
  335. }
  336. }
  337. public string Approve(List<ICSOutsourcingIssueDoc> infos)
  338. {
  339. List<ICSOutsourcingIssueDoc> szJson = new List<ICSOutsourcingIssueDoc>();
  340. DataTable dt = null;
  341. string json = "";
  342. if (infos.Count <= 0)
  343. {
  344. throw new Exception("传送数据为空!");
  345. }
  346. string res = string.Empty;
  347. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  348. conn.Open();
  349. SqlTransaction sqlTran = conn.BeginTransaction();
  350. SqlCommand cmd = new SqlCommand();
  351. cmd.Transaction = sqlTran;
  352. cmd.Connection = conn;
  353. try
  354. {
  355. string sql = string.Empty;
  356. foreach (ICSOutsourcingIssueDoc info in infos)
  357. {
  358. if (info.MTime < new DateTime(2000, 01, 01))
  359. throw new Exception("请输入正确的操作时间:" + info.MTime);
  360. sql = @"UPDATE dbo.rdrecord11 SET cHandler ='" + info.User + @"' ,
  361. dnmodifytime =CONVERT(VARCHAR(50),GETDATE(),112),dModifyDate =GETDATE() WHERE ID='{0}'";
  362. sql = string.Format(sql, info.ID);
  363. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  364. }
  365. cmd.Transaction.Commit();
  366. return json;
  367. }
  368. catch (Exception ex)
  369. {
  370. cmd.Transaction.Rollback();
  371. log.Error(ex.Message);
  372. throw new Exception(ex.Message);
  373. }
  374. finally
  375. {
  376. if (conn.State == ConnectionState.Open)
  377. {
  378. conn.Close();
  379. }
  380. conn.Dispose();
  381. }
  382. }
  383. /// <summary>
  384. /// 删除请购单
  385. /// </summary>
  386. /// <param name="infos"></param>
  387. /// <returns></returns>
  388. public string Delete(List<ICSOutsourcingIssueDoc> infos)
  389. {
  390. List<ICSOutsourcingIssueDoc> szJson = new List<ICSOutsourcingIssueDoc>();
  391. if (infos.Count <= 0)
  392. {
  393. throw new Exception("传送数据为空!");
  394. }
  395. string res = string.Empty;
  396. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  397. conn.Open();
  398. SqlTransaction sqlTran = conn.BeginTransaction();
  399. SqlCommand cmd = new SqlCommand();
  400. cmd.Transaction = sqlTran;
  401. cmd.Connection = conn;
  402. try
  403. {
  404. string sql = string.Empty;
  405. foreach (ICSOutsourcingIssueDoc info in infos)
  406. {
  407. if (info.MTime < new DateTime(2000, 01, 01))
  408. throw new Exception("请输入正确的操作时间:" + info.MTime);
  409. sql = @" DELETE dbo.rdrecord11 WHERE ID='{0}'";
  410. sql = string.Format(sql, info.ID);
  411. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  412. }
  413. cmd.Transaction.Commit();
  414. return res;
  415. ;
  416. }
  417. catch (Exception ex)
  418. {
  419. cmd.Transaction.Rollback();
  420. log.Error(ex.Message);
  421. throw new Exception(ex.Message);
  422. }
  423. finally
  424. {
  425. if (conn.State == ConnectionState.Open)
  426. {
  427. conn.Close();
  428. }
  429. conn.Dispose();
  430. }
  431. }
  432. /// <summary>
  433. ///
  434. /// 记录日志
  435. /// </summary>
  436. /// <param name="Bills"></param>
  437. private void LogInfo(List<ICSOutsourcingIssueDoc> Bills)
  438. {
  439. string HeadList = string.Empty;
  440. string BodyList = string.Empty;
  441. foreach (ICSOutsourcingIssueDoc head in Bills)
  442. {
  443. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User;
  444. foreach (ICSOutsourcingIssueDocs body in head.details)
  445. {
  446. BodyList += "\r\n 表体主键ID: " + body.SourceDetailID + ",数量:" + body.Quantity;
  447. }
  448. }
  449. log.Info(HeadList);
  450. log.Info(BodyList);
  451. }
  452. }
  453. }