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

501 lines
25 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 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 as ID,a.ID as IDs,a.cCode as IssueCode,a.cDepCode,c.cDepName,a.cWhCode,
  46. d.cWhName,a.cSource,cMPoCode,a.cMaker,dnmaketime,cHandler,dnverifytime
  47. from rdrecord11 a
  48. left join Department c on a.cDepCode=c.cDepCode
  49. left join Warehouse d on a.cWhCode=d.cWhCode
  50. where a.cBusType=''
  51. select a.ID as IDs,iMPoIds as SourceDetailID ,b.irowno as Sequence,iordercode,b.cInvCode,b.iQuantity,b.iNum,b.AutoID as DetailID
  52. from rdrecord11 a
  53. left join rdrecords11 b on a.ID=b.ID
  54. left join Department c on a.cDepCode=c.cDepCode
  55. left join Warehouse d on a.cWhCode=d.cWhCode
  56. left join OM_MODetails e on b.iOMoDID=e.MoDetailsID
  57. where a.cBusType=''";
  58. if (!string.IsNullOrWhiteSpace(info.IssueCode))
  59. {
  60. sql += " and a.cCode='{0}'";
  61. }
  62. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  63. {
  64. sql += " and ISNULL(a.dnmodifytime,ISNULL(a.dnverifytime, ISNULL(a.dnmodifytime, a.dnmaketime)))>='{1}'";
  65. }
  66. if (!string.IsNullOrWhiteSpace(info.User))
  67. {
  68. sql += "and a.CMAKER='{2}'";
  69. }
  70. sql = string.Format(sql, info.IssueCode, info.MTime, info.User);
  71. dt = DBHelper.SQlReturnData(sql, cmd);
  72. json = JsonConvert.SerializeObject(dt);
  73. if (dt.Rows.Count <= 0 || dt == null)
  74. throw new Exception("委外发料单:" + info.IssueCode + ",无信息!");
  75. }
  76. cmd.Transaction.Commit();
  77. return json;
  78. }
  79. catch (Exception ex)
  80. {
  81. cmd.Transaction.Rollback();
  82. log.Error(ex.Message);
  83. throw new Exception(ex.Message);
  84. }
  85. finally
  86. {
  87. if (conn.State == ConnectionState.Open)
  88. {
  89. conn.Close();
  90. }
  91. conn.Dispose();
  92. }
  93. }
  94. public string CreateOutsourcingIssueDoc(List<ICSOutsourcingIssueDoc> Bills)
  95. {
  96. bool ResultFlag = false;
  97. string msg = "";
  98. DataTable dt = null;
  99. string sql = string.Empty;
  100. int num = 0;
  101. string iFatherIdTwo = "";
  102. //取得out库单的默认显示模版
  103. //取得采购入库单单据 表头ID,表体DID
  104. VouchKey key = new VouchKey();
  105. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  106. conn.Open();
  107. SqlTransaction sqlTran = conn.BeginTransaction();
  108. SqlCommand cmd = new SqlCommand();
  109. cmd.Transaction = sqlTran;
  110. cmd.Connection = conn;
  111. try
  112. {
  113. if (Bills.Count <= 0)
  114. {
  115. throw new Exception("传送数据为空!");
  116. }
  117. LogInfo(Bills);
  118. LogInfo(Bills);
  119. foreach (ICSOutsourcingIssueDoc head in Bills)
  120. {
  121. num = head.details.Count();
  122. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + num + "");
  123. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  124. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  125. DateTime date = DateTime.Now;
  126. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  127. if (head.details.Count <= 0)
  128. {
  129. throw new Exception("表体信息不存在!");
  130. }
  131. #region 判断委外订单是否已关闭
  132. sql = @"SELECT distinct isnull(b.cbCloser,'') as CloseUser
  133. from dbo.OM_MOMaterials a
  134. INNER JOIN OM_MODetails b ON a.MoDetailsID = b.MoDetailsID
  135. where a.MOMaterialsID='" + head.details[0].SourceDetailID + "'";
  136. DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  137. if (dtClose != null && dtClose.Rows.Count > 0)
  138. {
  139. if (!string.IsNullOrEmpty(dtClose.Rows[0][0].ToString()))
  140. {
  141. throw new Exception("该委外订单行已关闭,无法领料!");
  142. }
  143. }
  144. #endregion
  145. #region 判断物料是否超额领取
  146. foreach (ICSOutsourcingIssueDocs body in head.details)
  147. {
  148. sql = @"SELECT isnull(b.fOutExcess,0) as fOutExcess,isnull(a.iSendQTY,0) as IssQty,a.iQuantity
  149. FROM dbo.OM_MOMaterials a
  150. LEFT JOIN dbo.Inventory b ON a.cInvCode=b.cInvCode
  151. WHERE a.MOMaterialsID='" + body.SourceDetailID + "'";
  152. DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  153. // if (dtQty != null && dtQty.Rows.Count > 0)
  154. // {
  155. // //可超领比率
  156. // decimal fOutExcess = Convert.ToDecimal(dtQty.Rows[0]["fOutExcess"]);
  157. // //已领数量
  158. // decimal IssQty = Convert.ToDecimal(dtQty.Rows[0]["IssQty"]);
  159. // //应领数量
  160. // decimal Qty = Convert.ToDecimal(dtQty.Rows[0]["iQuantity"]);
  161. // //当前可总领数量
  162. // decimal TotalQty = Qty + fOutExcess * Qty;
  163. // if (head.IsReturn != "1")
  164. // {
  165. // if (IssQty + body.Quantity > TotalQty)
  166. // {
  167. // throw new Exception("物料领取超过当前可领用最大数,AllocateId:" + body.SourceDetailID);
  168. // }
  169. // }
  170. // else
  171. // {
  172. // if (IssQty < body.Quantity)
  173. // {
  174. // throw new Exception("物料退料超过当前已领用最大数,AllocateId:" + body.SourceDetailID);
  175. // }
  176. // }
  177. // }
  178. // else
  179. // {
  180. // throw new Exception("委外订单行子件不存在!AllocateId:" + body.SourceDetailID);
  181. // }
  182. //}
  183. }
  184. #endregion
  185. #region 委外材料出库单表头
  186. sql = @"
  187. INSERT INTO dbo.rdrecord11
  188. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,
  189. cWhCode ,dDate ,cCode ,cRdCode ,cDepCode,
  190. cPersonCode,cHandler ,bTransFlag ,cMaker ,dVeriDate ,
  191. bpufirst ,biafirst ,VT_ID ,bIsSTQc ,cPsPcode ,
  192. cMPoCode ,iproorderid ,bFromPreYear ,bIsLsQuery ,bIsComplement ,
  193. iDiscountTaxType ,ireturncount ,iverifystate ,iswfcontrolled ,dnmaketime ,
  194. dnverifytime ,bredvouch ,iPrintCount,cVenCode,iMQuantity)
  195. SELECT distinct @ID,0,'11','','',
  196. @cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,'22',c.cDepCode,
  197. c.cPersonCode,@cHandler,0,@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),
  198. 0,0,@VT_ID,0,b.cInvCode,
  199. c.cCode,a.MoId,0,0,0,
  200. 0,0,0,0,GETDATE(),
  201. GETDATE(),0,0,c.cVenCode,b.iQuantity
  202. from dbo.OM_MOMaterials a
  203. INNER JOIN dbo.OM_MODetails b ON a.MoDetailsID = b.MoDetailsID
  204. LEFT JOIN dbo.OM_MOMain c ON b.MOID = c.MOID
  205. WHERE a.MOMaterialsID='" + head.details[0].SourceDetailID + "'";
  206. cmd.Parameters.Clear();
  207. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  208. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  209. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  210. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  211. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  212. cmd.Parameters.Add(new SqlParameter("@VT_ID", iChildId));
  213. cmd.CommandText = sql;
  214. try
  215. {
  216. int count = cmd.ExecuteNonQuery();
  217. if (count <= 0)
  218. {
  219. log.Error("生成委外发料单表头失败,受影响行数<=0;");
  220. throw new Exception("生成委外发料单表头失败,受影响行数<=0;");
  221. }
  222. }
  223. catch (Exception ex)
  224. {
  225. log.Error("生成委外发料单表头失败!SQL:\r\n" + sql, ex);
  226. throw new Exception("生成委外发料单表头失败!SQL:\r\n" + sql, ex);
  227. }
  228. #endregion
  229. #region 委外材料出库单表体
  230. string iNQuantity = "";
  231. foreach (ICSOutsourcingIssueDocs body in head.details)
  232. {
  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,@InvCode,@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("@InvCode", body.InvCode));
  289. cmd.Parameters.Add(new SqlParameter("@cBatch", ""));
  290. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  291. cmd.Parameters.Add(new SqlParameter("@iNQuantity", iNQuantity));
  292. cmd.CommandText = sql;
  293. try
  294. {
  295. int count = cmd.ExecuteNonQuery();
  296. if (count <= 0)
  297. {
  298. log.Error("生成委外发料单表体失败,受影响行数<=0;");
  299. throw new Exception("生成委外发料单表体失败,受影响行数<=0;");
  300. }
  301. }
  302. catch (Exception ex)
  303. {
  304. log.Error("生成委外发料单表体失败" + sql, ex);
  305. throw new Exception("生成委外发料单表体失败 " + sql, ex);
  306. }
  307. iFatherIdTwo += "'" + iFatherId + "',";
  308. //更新现存量
  309. key.cBustypeUN = "委外发料";
  310. key.cVouchTypeUN = "11";
  311. key.TableName = "IA_ST_UnAccountVouch11";
  312. DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key);
  313. #region 回写工单子件已领数量
  314. sql = "update OM_MOMaterials set iSendQTY=isnull(iSendQTY,0)+" + body.Quantity + " where MOMaterialsID='" + body.SourceDetailID + "' ";
  315. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写委外订单子件已领数量失败!");
  316. #endregion
  317. iChildId--;
  318. }
  319. #endregion
  320. }
  321. #region 查询
  322. sql = @"select a.ID as ID,a.ID as IDs,a.cCode as IssueCode,a.cDepCode,c.cDepName,a.cWhCode,
  323. d.cWhName,a.cSource,cMPoCode,a.cMaker,dnmaketime,cHandler,dnverifytime
  324. from rdrecord11 a
  325. left join Department c on a.cDepCode=c.cDepCode
  326. left join Warehouse d on a.cWhCode=d.cWhCode
  327. where a.cBusType=''and a.ID in ({0})
  328. select a.ID as IDs,iOMoMID as SourceDetailID ,b.irowno as Sequence,iordercode,b.cInvCode,b.iQuantity,b.iNum,b.AutoID as DetailID
  329. from rdrecord11 a
  330. left join rdrecords11 b on a.ID=b.ID
  331. left join Department c on a.cDepCode=c.cDepCode
  332. left join Warehouse d on a.cWhCode=d.cWhCode
  333. left join OM_MODetails e on b.iOMoDID=e.MoDetailsID
  334. where a.cBusType=''and a.ID in ({0})";
  335. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  336. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  337. string RelationName = "details";
  338. DataRelation dr = new DataRelation(RelationName, ds.Tables[0].Columns["IDs"], ds.Tables[1].Columns["IDs"]);
  339. ds.Relations.Add(dr);
  340. // sql = @" select a.ID as ID,iMPoIds,iordercode,b.irowno as Sequence,a.cCode as IssueCode,a.cDepCode,c.cDepName,a.cWhCode,
  341. // d.cWhName,a.cSource,a.cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,
  342. // b.AutoID as DetailID ,b.irowno,b.cInvCode,b.iQuantity,e.MoDetailsID,b.iNum,e.csocode
  343. // from rdrecord11 a
  344. // left join rdrecords11 b on a.ID=b.ID
  345. // left join Department c on a.cDepCode=c.cDepCode
  346. // left join Warehouse d on a.cWhCode=d.cWhCode
  347. // left join OM_MODetails e on b.iOMoDID=e.MoDetailsID
  348. // where a.cBusType='委外发料'and a.ID in ({0})";
  349. // sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  350. // dt = DBHelper.SQlReturnData(sql, cmd);
  351. #endregion
  352. cmd.Transaction.Commit();
  353. msg = DBHelper.DataSetToJson(ds, RelationName);
  354. return msg;
  355. }
  356. catch (Exception ex)
  357. {
  358. cmd.Transaction.Rollback();
  359. log.Error(ex.Message);
  360. throw new Exception(ex.Message);
  361. }
  362. finally
  363. {
  364. if (conn.State == ConnectionState.Open)
  365. {
  366. conn.Close();
  367. }
  368. conn.Dispose();
  369. }
  370. }
  371. public string Approve(List<ICSOutsourcingIssueDoc> infos)
  372. {
  373. List<ICSOutsourcingIssueDoc> szJson = new List<ICSOutsourcingIssueDoc>();
  374. DataTable dt = null;
  375. string json = "";
  376. if (infos.Count <= 0)
  377. {
  378. throw new Exception("传送数据为空!");
  379. }
  380. string res = string.Empty;
  381. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  382. conn.Open();
  383. SqlTransaction sqlTran = conn.BeginTransaction();
  384. SqlCommand cmd = new SqlCommand();
  385. cmd.Transaction = sqlTran;
  386. cmd.Connection = conn;
  387. try
  388. {
  389. string sql = string.Empty;
  390. foreach (ICSOutsourcingIssueDoc info in infos)
  391. {
  392. if (info.MTime < new DateTime(2000, 01, 01))
  393. throw new Exception("请输入正确的操作时间:" + info.MTime);
  394. sql = @"UPDATE dbo.rdrecord11 SET cHandler ='" + info.User + @"' ,
  395. dnmodifytime =CONVERT(VARCHAR(50),GETDATE(),112),dModifyDate =GETDATE() WHERE ID='{0}'";
  396. sql = string.Format(sql, info.ID);
  397. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  398. }
  399. cmd.Transaction.Commit();
  400. return json;
  401. }
  402. catch (Exception ex)
  403. {
  404. cmd.Transaction.Rollback();
  405. log.Error(ex.Message);
  406. throw new Exception(ex.Message);
  407. }
  408. finally
  409. {
  410. if (conn.State == ConnectionState.Open)
  411. {
  412. conn.Close();
  413. }
  414. conn.Dispose();
  415. }
  416. }
  417. /// <summary>
  418. /// 删除请购单
  419. /// </summary>
  420. /// <param name="infos"></param>
  421. /// <returns></returns>
  422. public string Delete(List<ICSOutsourcingIssueDoc> infos)
  423. {
  424. List<ICSOutsourcingIssueDoc> szJson = new List<ICSOutsourcingIssueDoc>();
  425. if (infos.Count <= 0)
  426. {
  427. throw new Exception("传送数据为空!");
  428. }
  429. string res = string.Empty;
  430. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  431. conn.Open();
  432. SqlTransaction sqlTran = conn.BeginTransaction();
  433. SqlCommand cmd = new SqlCommand();
  434. cmd.Transaction = sqlTran;
  435. cmd.Connection = conn;
  436. try
  437. {
  438. string sql = string.Empty;
  439. foreach (ICSOutsourcingIssueDoc info in infos)
  440. {
  441. if (info.MTime < new DateTime(2000, 01, 01))
  442. throw new Exception("请输入正确的操作时间:" + info.MTime);
  443. sql = @" DELETE dbo.rdrecord11 WHERE ID='{0}'";
  444. sql = string.Format(sql, info.ID);
  445. DBHelper.CmdExecuteNonQuery(sql, cmd, "未查询到对应数据!");
  446. }
  447. cmd.Transaction.Commit();
  448. return res;
  449. ;
  450. }
  451. catch (Exception ex)
  452. {
  453. cmd.Transaction.Rollback();
  454. log.Error(ex.Message);
  455. throw new Exception(ex.Message);
  456. }
  457. finally
  458. {
  459. if (conn.State == ConnectionState.Open)
  460. {
  461. conn.Close();
  462. }
  463. conn.Dispose();
  464. }
  465. }
  466. /// <summary>
  467. ///
  468. /// 记录日志
  469. /// </summary>
  470. /// <param name="Bills"></param>
  471. private void LogInfo(List<ICSOutsourcingIssueDoc> Bills)
  472. {
  473. string HeadList = string.Empty;
  474. string BodyList = string.Empty;
  475. foreach (ICSOutsourcingIssueDoc head in Bills)
  476. {
  477. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User;
  478. foreach (ICSOutsourcingIssueDocs body in head.details)
  479. {
  480. BodyList += "\r\n 表体主键ID: " + body.SourceDetailID + ",数量:" + body.Quantity;
  481. }
  482. }
  483. log.Info(HeadList);
  484. log.Info(BodyList);
  485. }
  486. }
  487. }