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

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