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

  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 OutsourcingIssueDoNegative
  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=''and b.iQuantity<0";
  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 CreateOutsourcingIssueDoNegative(List<ICSOutsourcingIssueDoc> Bills)
  91. {
  92. string msg = "";
  93. int num = 0;
  94. string sql = string.Empty;
  95. DataTable dt = null;
  96. string iFatherIdTwo = "";
  97. //取得采购入库单单据 表头ID,表体DID
  98. VouchKey key = new VouchKey();
  99. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  100. conn.Open();
  101. SqlTransaction sqlTran = conn.BeginTransaction();
  102. SqlCommand cmd = new SqlCommand();
  103. cmd.Transaction = sqlTran;
  104. cmd.Connection = conn;
  105. try
  106. {
  107. if (Bills.Count <= 0)
  108. {
  109. throw new Exception("传送数据为空!");
  110. }
  111. LogInfo(Bills);
  112. LogInfo(Bills);
  113. foreach (ICSOutsourcingIssueDoc head in Bills)
  114. {
  115. if (head.details.Count <= 0)
  116. {
  117. throw new Exception("表体信息不存在!");
  118. }
  119. #region 判断委外订单是否已关闭
  120. sql = @"SELECT distinct isnull(b.cbCloser,'') as CloseUser
  121. from dbo.OM_MOMaterials a
  122. INNER JOIN OM_MODetails b ON a.MoDetailsID = b.MoDetailsID
  123. where a.MOMaterialsID='" + head.details[0].SourceDetailID + "'";
  124. DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  125. if (dtClose != null && dtClose.Rows.Count > 0)
  126. {
  127. if (!string.IsNullOrEmpty(dtClose.Rows[0][0].ToString()))
  128. {
  129. throw new Exception("该委外订单行已关闭,无法领料!");
  130. }
  131. }
  132. #endregion
  133. #region 判断物料是否超额领取
  134. foreach (ICSOutsourcingIssueDocs body in head.details)
  135. {
  136. sql = @"SELECT isnull(b.fOutExcess,0) as fOutExcess,isnull(a.iSendQTY,0) as IssQty,a.iQuantity
  137. FROM dbo.OM_MOMaterials a
  138. LEFT JOIN dbo.Inventory b ON a.cInvCode=b.cInvCode
  139. WHERE a.MOMaterialsID='" + body.SourceDetailID + "'";
  140. DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  141. if (dtQty != null && dtQty.Rows.Count > 0)
  142. {
  143. //可超领比率
  144. decimal fOutExcess = Convert.ToDecimal(dtQty.Rows[0]["fOutExcess"]);
  145. //已领数量
  146. decimal IssQty = Convert.ToDecimal(dtQty.Rows[0]["IssQty"]);
  147. //应领数量
  148. decimal Qty = Convert.ToDecimal(dtQty.Rows[0]["iQuantity"]);
  149. //当前可总领数量
  150. decimal TotalQty = Qty + fOutExcess * Qty;
  151. //if (head.IsReturn != "1")
  152. //{
  153. //if (IssQty + body.Quantity > TotalQty)
  154. //{
  155. // throw new Exception("物料领取超过当前可领用最大数,AllocateId:" + body.SourceDetailID);
  156. //}
  157. //}
  158. //else
  159. //{
  160. if (IssQty < body.Quantity)
  161. {
  162. throw new Exception("物料退料超过当前已领用最大数,AllocateId:" + body.SourceDetailID);
  163. }
  164. //}
  165. }
  166. else
  167. {
  168. throw new Exception("委外订单行子件不存在!AllocateId:" + body.SourceDetailID);
  169. }
  170. }
  171. #endregion
  172. #region 委外退料表头
  173. num = head.details.Count();
  174. //取得out库单的默认显示模版
  175. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + num + "");
  176. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  177. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  178. DateTime date = DateTime.Now;
  179. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  180. sql = @"
  181. INSERT INTO dbo.rdrecord11
  182. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,
  183. cWhCode ,dDate ,cCode ,cRdCode ,cDepCode,
  184. cPersonCode,cHandler ,bTransFlag ,cMaker ,dVeriDate ,
  185. bpufirst ,biafirst ,VT_ID ,bIsSTQc ,cPsPcode ,
  186. cMPoCode ,iproorderid ,bFromPreYear ,bIsLsQuery ,bIsComplement ,
  187. iDiscountTaxType ,ireturncount ,iverifystate ,iswfcontrolled ,dnmaketime ,
  188. dnverifytime ,bredvouch ,iPrintCount,cVenCode,iMQuantity)
  189. SELECT distinct @ID,0,'11','','',
  190. @cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,'22',c.cDepCode,
  191. c.cPersonCode,@cHandler,0,@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),
  192. 0,0,@VT_ID,0,b.cInvCode,
  193. c.cCode,a.MoId,0,0,0,
  194. 0,0,0,0,GETDATE(),
  195. GETDATE(),@bredvouch,0,c.cVenCode,b.iQuantity
  196. from dbo.OM_MOMaterials a
  197. INNER JOIN dbo.OM_MODetails b ON a.MoDetailsID = b.MoDetailsID
  198. LEFT JOIN dbo.OM_MOMain c ON b.MOID = c.MOID
  199. WHERE a.MOMaterialsID='" + head.details[0].SourceDetailID + "'";
  200. cmd.Parameters.Clear();
  201. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  202. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  203. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  204. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  205. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  206. cmd.Parameters.Add(new SqlParameter("@VT_ID", iChildId));
  207. cmd.Parameters.Add(new SqlParameter("@bredvouch", "0"));
  208. cmd.CommandText = sql;
  209. try
  210. {
  211. int count = cmd.ExecuteNonQuery();
  212. if (count <= 0)
  213. {
  214. log.Error("生成材料出库单表头失败,受影响行数<=0;");
  215. throw new Exception("生成材料出库单表头失败,受影响行数<=0;");
  216. }
  217. }
  218. catch (Exception ex)
  219. {
  220. log.Error("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  221. throw new Exception("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  222. }
  223. #endregion
  224. #region 委外退料表体
  225. string iNQuantity = "";
  226. foreach (ICSOutsourcingIssueDocs body in head.details)
  227. {
  228. body.Quantity = -body.Quantity;
  229. iNQuantity = "-a.iQuantity";
  230. sql = @"INSERT INTO dbo.rdrecords11
  231. ( AutoID,ID ,cInvCode,iQuantity ,cBatch ,
  232. iFlag ,cItemCode ,cName ,iNQuantity ,iMPoIds ,
  233. bLPUseFree ,iOriTrackID ,bCosting ,bVMIUsed ,cmocode ,
  234. invcode ,imoseq ,iopseq ,iExpiratDateCalcu ,iorderdid ,
  235. iordertype ,isotype ,ipesodid ,ipesotype ,cpesocode ,
  236. ipesoseq ,irowno ,bcanreplace ,iposflag,iOMoDID,
  237. iOMoMID,comcode)
  238. SELECT distinct
  239. @AutoID,@ID,a.cInvCode,@iQuantity,@cBatch,
  240. 0,null,null,@iQuantity,NULL,
  241. 0,0,1,0,NULL,
  242. b.cInvCode,NULL,NULL,0,0,
  243. 0,0,a.MOMaterialsID,'6',c.cCode,
  244. NULL, @irowno,0,NULL,b.MODetailsID,
  245. a.MOMaterialsID,c.cCode
  246. FROM dbo.OM_MOMaterials a
  247. INNER JOIN dbo.OM_MODetails b ON a.MoDetailsID=b.MODetailsID
  248. INNER JOIN dbo.OM_MOMain c ON b.MOID=c.MOID
  249. WHERE a.MOMaterialsID='" + body.SourceDetailID + "'";
  250. cmd.Parameters.Clear();
  251. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  252. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  253. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  254. //cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  255. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  256. cmd.Parameters.Add(new SqlParameter("@irowno",body.Sequence));
  257. cmd.Parameters.Add(new SqlParameter("@iNQuantity", iNQuantity));
  258. cmd.CommandText = sql;
  259. try
  260. {
  261. int count = cmd.ExecuteNonQuery();
  262. if (count <= 0)
  263. {
  264. log.Error("生产退料单表体失败,受影响行数<=0;");
  265. throw new Exception("生产退料单表体失败,受影响行数<=0;");
  266. }
  267. }
  268. catch (Exception ex)
  269. {
  270. log.Error("生产退料单表体失败" + sql, ex);
  271. throw new Exception("生产退料单表体失败 " + sql, ex);
  272. }
  273. iFatherIdTwo += "'" + iFatherId + "',";
  274. //更新现存量
  275. key.cBustypeUN = "委外发料";
  276. key.cVouchTypeUN = "11";
  277. key.TableName = "IA_ST_UnAccountVouch11";
  278. DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key);
  279. #region 回写工单子件已领数量
  280. sql = "update OM_MOMaterials set iSendQTY=isnull(iSendQTY,0)+" + body.Quantity + " where MOMaterialsID='" + body.SourceDetailID + "' ";
  281. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写委外订单子件已领数量失败!");
  282. #endregion
  283. iChildId--;
  284. }
  285. #endregion
  286. }
  287. #region 查询
  288. sql = @"select a.ID as ID,a.ID as IDs,a.cCode as IssueNEGCode,a.cDepCode,c.cDepName,a.cWhCode,
  289. d.cWhName,a.cSource,cMPoCode,a.cMaker,dnmaketime,cHandler,dnverifytime
  290. from rdrecord11 a
  291. left join Department c on a.cDepCode=c.cDepCode
  292. left join Warehouse d on a.cWhCode=d.cWhCode
  293. where a.cBusType='' and a.ID in ({0})
  294. select a.ID as IDs,iOMoMID as SourceDetailID,b.irowno as Sequence ,iordercode,b.cInvCode,b.iQuantity,b.iNum,b.AutoID as DetailID
  295. from rdrecord11 a
  296. left join rdrecords11 b on a.ID=b.ID
  297. left join Department c on a.cDepCode=c.cDepCode
  298. left join Warehouse d on a.cWhCode=d.cWhCode
  299. left join OM_MODetails e on b.iOMoDID=e.MoDetailsID
  300. where a.cBusType='' and a.ID in ({0})";
  301. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  302. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  303. string RelationName = "details";
  304. DataRelation dr = new DataRelation(RelationName, ds.Tables[0].Columns["IDs"], ds.Tables[1].Columns["IDs"]);
  305. ds.Relations.Add(dr);
  306. // sql = @" select a.ID as ID,iMPoIds,iordercode,a.cCode,a.cDepCode,c.cDepName,a.cWhCode,
  307. // d.cWhName,a.cSource,a.cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,
  308. // b.AutoID ,b.irowno,b.cInvCode,b.iQuantity,e.MoDetailsID,b.iNum,e.csocode
  309. // from rdrecord11 a
  310. // left join rdrecords11 b on a.ID=b.ID
  311. // left join Department c on a.cDepCode=c.cDepCode
  312. // left join Warehouse d on a.cWhCode=d.cWhCode
  313. // left join OM_MODetails e on b.iOMoDID=e.MoDetailsID
  314. // where a.cBusType='委外发料'and b.iQuantity<0 and a.ID in ({0})";
  315. // sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  316. // dt = DBHelper.SQlReturnData(sql, cmd);
  317. #endregion
  318. cmd.Transaction.Commit();
  319. msg = DBHelper.DataSetToJson(ds, RelationName);
  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. }