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

894 lines
46 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
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 MOIssueDoc
  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<ICSRdrecord11> infos)
  23. {
  24. List<ICSRdrecord11> szJson = new List<ICSRdrecord11>();
  25. DataTable dt = null;
  26. DataTable dtNew = null;
  27. string connS = "";
  28. string json = "";
  29. if (infos.Count <= 0)
  30. {
  31. throw new Exception("传送数据为空!");
  32. }
  33. string res = string.Empty;
  34. SqlConnection conn = new SqlConnection();
  35. SqlCommand cmd = new SqlCommand();
  36. string sql = string.Empty;
  37. foreach (ICSRdrecord11 info in infos)
  38. {
  39. try
  40. {
  41. connS = string.Format(connString, info.WorkPoint);
  42. conn = new System.Data.SqlClient.SqlConnection(connS);
  43. conn.Open();
  44. SqlTransaction sqlTran = conn.BeginTransaction();
  45. cmd = new SqlCommand();
  46. cmd.Transaction = sqlTran;
  47. cmd.Connection = conn;
  48. if (info.MTime < new DateTime(2000, 01, 01))
  49. throw new Exception("请输入正确的操作时间:" + info.MTime);
  50. sql = @" select a.ID,iMPoIds,iordercode,cCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,cSource,cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,b.AutoID ,irowno,b.cInvCode,iQuantity,f.MoDId,iNum,g.MoCode from rdrecord11 a
  51. left join rdrecords11 b on a.ID=b.ID
  52. left join Department c on a.cDepCode=c.cDepCode
  53. left join Warehouse d on a.cWhCode=d.cWhCode
  54. left join mom_moallocate e on b.iMPoIds=e.AllocateId
  55. INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId
  56. INNER JOIN dbo.mom_order g ON f.MoId=g.MoId WHERE 1=1";
  57. if (!string.IsNullOrWhiteSpace(info.IssueCode))
  58. {
  59. sql += " and a.cCode='{0}'";
  60. }
  61. if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
  62. {
  63. sql += " and ISNULL(a.dnmodifytime ,ISNULL(a.dnverifytime , ISNULL(a.dnmodifytime , a.dnmaketime)))>='{1}'";
  64. }
  65. if (!string.IsNullOrWhiteSpace(info.User))
  66. {
  67. sql += " and a.CMAKER='{2}'";
  68. }
  69. sql = string.Format(sql, info.IssueCode, info.MTime, info.User);
  70. dt = DBHelper.SQlReturnData(sql, cmd);
  71. dtNew.Merge(dt);
  72. cmd.Transaction.Commit();
  73. }
  74. catch (Exception ex)
  75. {
  76. cmd.Transaction.Rollback();
  77. log.Error(ex.Message);
  78. throw new Exception(ex.Message);
  79. }
  80. finally
  81. {
  82. if (conn.State == ConnectionState.Open)
  83. {
  84. conn.Close();
  85. }
  86. conn.Dispose();
  87. }
  88. }
  89. json = JsonConvert.SerializeObject(dtNew);
  90. return json;
  91. }
  92. public string CreateRd11(List<ICSRdrecord11> Bills)
  93. {
  94. string msg = "";
  95. DataTable dt = null;
  96. DataTable dtNew1 = null;
  97. DataTable dtNew2 = null;
  98. string connS = "";
  99. VouchKey key = new VouchKey();
  100. int num = 0;
  101. int iFatherId = 0;
  102. int iChildId = 0;
  103. string sql = string.Empty;
  104. SqlConnection conn = new SqlConnection();
  105. SqlCommand cmd = new SqlCommand();
  106. if (Bills.Count <= 0)
  107. {
  108. throw new Exception("传送数据为空!");
  109. }
  110. LogInfo(Bills);
  111. //MergeObject(Bills, cmd);
  112. foreach (ICSRdrecord11 head in Bills)
  113. {
  114. try
  115. {
  116. string iFatherIdTwo = string.Empty;
  117. num = head.details.Count();
  118. connS = string.Format(connString, head.WorkPoint);
  119. conn = new System.Data.SqlClient.SqlConnection(connS);
  120. conn.Open();
  121. SqlTransaction sqlTran = conn.BeginTransaction();
  122. cmd = new SqlCommand();
  123. cmd.Transaction = sqlTran;
  124. cmd.Connection = conn;
  125. cmd.CommandTimeout = 300;
  126. string[] ss = head.WorkPoint.Split('_');
  127. ERPDB = ss[1];
  128. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + num + "");
  129. iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  130. iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  131. DateTime date = DateTime.Now;
  132. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  133. string DEF_ID = DBHelper.GetDefaultTemplate(Type, cmd);
  134. #region 判断工单是否已关闭
  135. sql = @"SELECT isnull(CloseUser,'') as CloseUser from mom_orderdetail a
  136. inner join mom_order b on a.MoId=b.MoId where b.MoCode='" + head.SourceCode + "' ";
  137. DataTable dtClose = DBHelper.SQlReturnData(sql, cmd);
  138. if (dtClose != null && dtClose.Rows.Count > 0)
  139. {
  140. if (!string.IsNullOrEmpty(dtClose.Rows[0][0].ToString()))
  141. {
  142. throw new Exception("该工单行已关闭,无法领料!");
  143. }
  144. }
  145. #endregion
  146. #region 判断物料是否超额领取
  147. // foreach (rdrecords11 body in head.details)
  148. // {
  149. // sql = @"SELECT isnull(b.fOutExcess,0) as fOutExcess,isnull(a.IssQty,0) as IssQty,a.Qty,a.ReplenishQty FROM dbo.mom_moallocate a
  150. // LEFT JOIN dbo.Inventory b ON a.InvCode=b.cInvCode
  151. // WHERE a.AllocateId='" + 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]["Qty"]);
  161. // //当前可总领数量
  162. // decimal TotalQty = Qty;// + fOutExcess * Qty;
  163. // //补料数量
  164. // decimal ReplenishQty = Convert.ToDecimal(dtQty.Rows[0]["ReplenishQty"]);
  165. // string cql = @"SELECT * from Inventory where cInvCode='" + body.InvCode + "'AND bKCCutMantissa =1 AND bCutMantissa=1";
  166. // DataTable dtInv = DBHelper.SQlReturnData(cql, cmd);
  167. // if (dtInv != null && dtInv.Rows.Count > 0)
  168. // {
  169. // }
  170. // else
  171. // {
  172. // if (IssQty - ReplenishQty + body.Quantity > TotalQty)
  173. // {
  174. // throw new Exception("物料领取超过当前可领用最大数,AllocateId:" + body.SourceDetailID);
  175. // }
  176. // }
  177. // //if (IssQty + body.iQuantity > TotalQty)
  178. // //{
  179. // // throw new Exception("物料领取超过当前可领用最大数,AllocateId:" + body.AllocateId);
  180. // //}
  181. // }
  182. // else
  183. // {
  184. // throw new Exception("工单行子件不存在!AllocateId:" + body.SourceDetailID);
  185. // }
  186. // }
  187. #endregion
  188. #region 材料出库单表头
  189. sql = @"INSERT INTO dbo.rdrecord11
  190. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,
  191. cCode ,cRdCode ,cDepCode,cHandler ,bTransFlag ,cMaker ,
  192. dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc
  193. ,cPsPcode ,cMPoCode ,iproorderid ,bFromPreYear ,
  194. bIsLsQuery ,bIsComplement ,iDiscountTaxType ,ireturncount ,iverifystate ,
  195. iswfcontrolled ,dnmaketime ,dnverifytime ,bredvouch ,iPrintCount,
  196. iMQuantity)
  197. SELECT @ID,0,'11','','',@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),
  198. @cCode,'23',a.MDeptCode,@cHandler,0,@cMaker,
  199. CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0,
  200. a.InvCode,b.MoCode,a.MoId,0,
  201. 0,0,0,0,0,
  202. 0,GETDATE(),GETDATE(),0,0,
  203. a.Qty
  204. FROM dbo.mom_orderdetail a
  205. left JOIN dbo.mom_order b ON a.MoId=b.MoId
  206. left join mom_morder c on a.modid=c.modid
  207. WHERE a.MoDId='" + head.MODetailID + "'";
  208. cmd.Parameters.Clear();
  209. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  210. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  211. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  212. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  213. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  214. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  215. cmd.CommandText = sql;
  216. try
  217. {
  218. int count = cmd.ExecuteNonQuery();
  219. if (count <= 0)
  220. {
  221. log.Error("生成材料出库单表头失败,受影响行数<=0;");
  222. throw new Exception("生成材料出库单表头失败,受影响行数<=0;");
  223. }
  224. }
  225. catch (Exception ex)
  226. {
  227. log.Error("生成材料出库单表头失败" + sql, ex);
  228. throw new Exception("生成材料出库单表头失败" + sql, ex);
  229. }
  230. #endregion
  231. #region 材料出库单表体
  232. int irowno = 0;
  233. foreach (rdrecords11 body in head.details)
  234. {
  235. //if (irowno == 0)
  236. //{
  237. // iChildId = iChildId;
  238. //}
  239. //else
  240. //{
  241. // iChildId -= 1;
  242. //}
  243. //irowno += 1;
  244. //if (head.IsReturn == "1")
  245. //{
  246. // body.iQuantity = -body.iQuantity;
  247. //}
  248. //if (head.IsReturn == "0")
  249. //{
  250. // //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
  251. // sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.cInvCode + "'AND cBatch='" + body.cBatch + "'and cWhCode='" + head.cWhCode + "'";
  252. // DataTable dtItem = U8Helper.SQlReturnData(sql, cmd);
  253. // if (dtItem != null && dtItem.Rows.Count > 0)
  254. // {
  255. // if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cBatch))
  256. // {
  257. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.cInvCode);
  258. // }
  259. // if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < body.iQuantity)
  260. // {
  261. // throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.cInvCode);
  262. // }
  263. // }
  264. // else
  265. // {
  266. // throw new Exception("物料:" + body.cInvCode + "在现存量表中不存在!");
  267. // }
  268. //}
  269. sql = @"INSERT INTO dbo.rdrecords11
  270. ( AutoID ,ID ,cInvCode ,iQuantity ,cBatch ,iFlag ,
  271. cItemCode ,cName ,iNQuantity ,iMPoIds ,bLPUseFree ,
  272. iOriTrackID ,bCosting ,bVMIUsed ,cmocode ,invcode ,imoseq ,
  273. iopseq ,iExpiratDateCalcu ,iorderdid ,iordertype ,isotype ,
  274. ipesodid ,ipesotype ,cpesocode ,ipesoseq ,irowno ,
  275. bcanreplace ,iposflag,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  276. SELECT @AutoID,@ID,a.InvCode,@iQuantity,@cBatch,0,
  277. null,null,a.Qty,a.AllocateId,0,
  278. 0,1,0,c.MoCode,b.InvCode,b.SortSeq,
  279. '0000',0,0,0,0,
  280. a.AllocateId,'7',c.MoCode,b.SortSeq,@irowno,
  281. 0,null,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  282. FROM dbo.mom_moallocate a
  283. INNER JOIN dbo.mom_orderdetail b ON a.MoDId=b.MoDId
  284. INNER JOIN dbo.mom_order c ON b.MoId=c.MoId
  285. WHERE a.AllocateId='" + body.SourceDetailID + "'";
  286. cmd.Parameters.Clear();
  287. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  288. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  289. //cmd.Parameters.Add(new SqlParameter("@cBatch", "''"));
  290. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  291. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  292. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  293. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  294. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  295. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  296. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  297. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  298. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  299. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  300. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  301. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  302. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  303. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  304. cmd.CommandText = sql;
  305. try
  306. {
  307. int count = cmd.ExecuteNonQuery();
  308. if (count <= 0)
  309. {
  310. log.Error("生成材料出库单表体失败,受影响行数<=0;");
  311. throw new Exception("生成材料出库单表体失败,受影响行数<=0;");
  312. }
  313. }
  314. catch (Exception ex)
  315. {
  316. log.Error("生成材料出库单表体失败" + sql, ex);
  317. throw new Exception("生成材料出库单表体失败 " + sql, ex);
  318. }
  319. if (head.UpdateStock)
  320. {
  321. //更新现存量
  322. key.cBustypeUN = "领料";
  323. key.cVouchTypeUN = "11";
  324. key.TableName = "IA_ST_UnAccountVouch11";
  325. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  326. 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, key);
  327. }
  328. #region 回写工单子件已领数量
  329. sql = "update mom_moallocate set IssQty=isnull(IssQty,0)+" + body.Quantity + " where AllocateId='" + body.SourceDetailID + "' ";
  330. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写工单子件已领数量失败!");
  331. #endregion
  332. iFatherIdTwo += "'" + iFatherId + "',";
  333. iChildId--;
  334. }
  335. #endregion
  336. #region 查询
  337. sql = @"
  338. select a.ID as ID,a.ID as IDs,cMPoCode as SourceCode ,
  339. cCode as IssueCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,
  340. cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime,
  341. cHandler as Checker,dnverifytime as CheckDateTime
  342. from rdrecord11 a
  343. left join Department c on a.cDepCode=c.cDepCode
  344. left join Warehouse d on a.cWhCode=d.cWhCode
  345. WHERE a.ID in ({0})
  346. select a.ID as IDs,b.AutoID as DetailID,irowno as Sequence,b.cInvCode as InvCode
  347. ,iQuantity as Quantity,e.AllocateId as SourceDetailID,iNum as Amount
  348. from rdrecord11 a
  349. left join rdrecords11 b on a.ID=b.ID
  350. left join Department c on a.cDepCode=c.cDepCode
  351. left join Warehouse d on a.cWhCode=d.cWhCode
  352. left join mom_moallocate e on b.iMPoIds=e.AllocateId
  353. INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId
  354. INNER JOIN dbo.mom_order g ON f.MoId=g.MoId WHERE a.ID in ({0})";
  355. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  356. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  357. dtNew1.Merge(ds.Tables[0]);
  358. dtNew2.Merge(ds.Tables[1]);
  359. #endregion
  360. cmd.Transaction.Commit();
  361. }
  362. catch (Exception ex)
  363. {
  364. cmd.Transaction.Rollback();
  365. log.Error(ex.Message);
  366. throw new Exception(ex.Message);
  367. }
  368. finally
  369. {
  370. if (conn.State == ConnectionState.Open)
  371. {
  372. conn.Close();
  373. }
  374. conn.Dispose();
  375. }
  376. }
  377. string RelationName = "details";
  378. DataRelation dh = new DataRelation(RelationName, dtNew1.Columns["IDs"], dtNew2.Columns["IDs"]);
  379. DataSet dz = new DataSet();
  380. dz.Relations.Add(dh);
  381. msg = DBHelper.DataSetToJson(dz, RelationName);
  382. return msg;
  383. }
  384. public string MaterialAppVouchs(List<ICSRdrecord11> Bills)
  385. {
  386. string mag = "";
  387. DataTable dtNew1 = null;
  388. DataTable dtNew2 = null;
  389. string connS = "";
  390. string iFatherIdTwo = string.Empty;
  391. SqlConnection conn = new SqlConnection();
  392. SqlCommand cmd = new SqlCommand();
  393. int num = 0;
  394. foreach (ICSRdrecord11 head in Bills)
  395. {
  396. num = head.details.Count();
  397. }
  398. if (Bills.Count <= 0)
  399. {
  400. throw new Exception("传送数据为空!");
  401. }
  402. LogInfo(Bills);
  403. //MergeObject(Bills, cmd);
  404. foreach (ICSRdrecord11 head in Bills)
  405. {
  406. try
  407. {
  408. string sql = string.Empty;
  409. connS = string.Format(connString, head.WorkPoint);
  410. conn = new System.Data.SqlClient.SqlConnection(connS);
  411. conn.Open();
  412. SqlTransaction sqlTran = conn.BeginTransaction();
  413. cmd = new SqlCommand();
  414. cmd.Transaction = sqlTran;
  415. cmd.Connection = conn;
  416. string[] ss = head.WorkPoint.Split('_');
  417. ERPDB = ss[1];
  418. #region 判断物料是否超额领取
  419. foreach (rdrecords11 body in head.details)
  420. {
  421. sql = @"SELECT isnull(fOutQuantity,0) as fOutQuantity,iQuantity FROM dbo.MaterialAppVouchs WHERE AutoID='" + body.SourceDetailID + "'";
  422. DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  423. if (dtQty != null && dtQty.Rows.Count > 0)
  424. {
  425. //已领数量
  426. decimal IssQty = Convert.ToDecimal(dtQty.Rows[0]["fOutQuantity"]);
  427. //应领数量
  428. decimal Qty = Convert.ToDecimal(dtQty.Rows[0]["iQuantity"]);
  429. if (IssQty + body.Quantity > Qty)
  430. {
  431. throw new Exception("物料领取超过当前可领用最大数,AutoID:" + body.SourceDetailID);
  432. }
  433. }
  434. else
  435. {
  436. throw new Exception("领料申请单不存在!AutoID:" + body.SourceDetailID);
  437. }
  438. }
  439. #endregion
  440. #region 现存量是否够领
  441. // //if (head.IsReturn != "1")
  442. // //{
  443. // foreach (rdrecords11 body in head.rdcords)
  444. // {
  445. // sql = string.Format(@"SELECT iQuantity FROM dbo.CurrentStock
  446. // WHERE cInvCode='{0}' AND cWhCode='{1}'", body.cInvCode, head.cWhCode);
  447. // if (!string.IsNullOrWhiteSpace(body.cBatch))
  448. // sql += " and cBatch='" + body.cBatch + "'";
  449. // DataTable dt = DBHelper.SQlReturnData(sql, cmd);
  450. // decimal qtyCurrent = 0;
  451. // if (dt != null && dt.Rows.Count > 0)
  452. // {
  453. // qtyCurrent = Convert.ToDecimal(dt.Rows[0][0]);
  454. // if (qtyCurrent < body.Quantity)
  455. // {
  456. // throw new Exception("物料编号:" + body.cInvCode + ",现存量不足!");
  457. // }
  458. // }
  459. // else
  460. // {
  461. // throw new Exception("该物料:" + body.cInvCode + "在U8中仓库:" + head.cWhCode + "中不存在!");
  462. // }
  463. // }
  464. // //}
  465. #endregion
  466. #region 材料出库单表头
  467. //取得out库单的默认显示模版
  468. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", "rd", "" + num + "");
  469. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  470. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  471. DateTime date = DateTime.Now;
  472. string iBaseCodeLen = DBHelper.GetAllRDCode("26", "" + date + "", "admin");
  473. string DEF_ID = DBHelper.GetDefaultTemplate(Type, cmd);
  474. //取得采购入库单单据 表头ID,表体DID
  475. VouchKey key = new VouchKey();
  476. sql = @"INSERT INTO dbo.rdrecord11
  477. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cBusCode ,cWhCode ,
  478. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cHandler ,cMemo ,
  479. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,cDefine4 ,
  480. cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,cDefine9 ,cDefine10 ,
  481. dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc ,cDefine11 ,
  482. cDefine12 ,cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  483. bOMFirst ,bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  484. ireturncount ,iverifystate ,iswfcontrolled ,cModifyPerson ,
  485. dnmaketime ,dnverifytime ,bredvouch ,bmotran ,bHYVouch ,iPrintCount
  486. )
  487. SELECT @ID,0,'11','','',a.cCode,@cWhCode,
  488. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,a.cRdCode,a.cDepCode,a.cPersonCode,@cHandler,a.cMemo,
  489. 0,@cMaker,a.cDefine1,cdefine2,a.cDefine3,a.cDefine4,
  490. a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,a.cDefine9,a.cDefine10,
  491. CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0,a.cDefine11,
  492. a.cDefine12,a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  493. 0,0,0,0,
  494. 0,0,0,'',
  495. GETDATE(),GETDATE(),0,0,0,0
  496. FROM dbo.MaterialAppVouch a WHERE a.cCode='" + head.SourceCode + "'";
  497. cmd.Parameters.Clear();
  498. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  499. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  500. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  501. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  502. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  503. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  504. //DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表头失败!");
  505. cmd.CommandText = sql;
  506. try
  507. {
  508. int count = cmd.ExecuteNonQuery();
  509. if (count <= 0)
  510. {
  511. log.Error("生成材料出库单表头失败,受影响行数<=0;");
  512. throw new Exception("生成材料出库单表头失败,受影响行数<=0;");
  513. }
  514. }
  515. catch (Exception ex)
  516. {
  517. log.Error("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  518. throw new Exception("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  519. }
  520. #endregion
  521. #region 材料出库单表体
  522. int irowno = 0;
  523. foreach (rdrecords11 body in head.details)
  524. {
  525. //if (head.IsReturn == "1")
  526. //{
  527. body.Quantity = -body.Quantity;
  528. //}
  529. //判断物料批号与现存量表批号是否一致and数量不能超过现存量物料数量
  530. sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.InvCode + "'AND cBatch=''and cWhCode='" + head.WHCode + "' ";
  531. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  532. if (dtItem != null && dtItem.Rows.Count > 0)
  533. {
  534. //if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cBatch))
  535. //{
  536. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.InvCode);
  537. //}
  538. if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < body.Quantity)
  539. {
  540. throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.InvCode);
  541. }
  542. sql = @"INSERT INTO dbo.rdrecords11
  543. ( AutoID ,ID ,cInvCode ,iQuantity ,iFlag ,cDefine22 ,
  544. cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  545. cItem_class ,cItemCode ,cName ,cItemCName ,iNQuantity ,
  546. cDefine28 ,cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,
  547. cDefine33 ,cDefine34 ,cDefine35 ,cDefine36 ,cDefine37 ,
  548. bLPUseFree ,iOriTrackID ,bCosting ,bVMIUsed ,iMaIDs ,
  549. iExpiratDateCalcu ,iordertype ,isotype ,ipesotype ,irowno,
  550. cBatch,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  551. SELECT @AutoID,@ID,a.cInvCode,@iQuantity,0,a.cDefine22,
  552. a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  553. a.cItem_class,a.cItemCode,a.cName,a.cItemCName,a.iQuantity,
  554. a.cDefine28,a.cDefine29,a.cDefine30,a.cDefine31,a.cDefine32,
  555. a.cDefine33,a.cDefine34,a.cDefine35,a.cDefine36,a.cDefine37,
  556. 0,0,1,0,a.AutoID,
  557. 0,0,0,0,@irowno,
  558. @cBatch,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  559. FROM dbo.MaterialAppVouchs a WHERE a.AutoID='" + body.SourceDetailID + "'";
  560. cmd.Parameters.Clear();
  561. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  562. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  563. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  564. cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
  565. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  566. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  567. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  568. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  569. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  570. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  571. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  572. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  573. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  574. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  575. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  576. //DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表头失败!");
  577. cmd.CommandText = sql;
  578. // SqlParameter[] paramBody = {
  579. // new SqlParameter("@AutoID",DbType.String),
  580. // new SqlParameter("@ID",DbType.String),
  581. // new SqlParameter("@iQuantity",DbType.Decimal),
  582. // new SqlParameter("@irowno",DbType.String),
  583. // new SqlParameter("@cBatch",DbType.String)
  584. //};
  585. //paramBody[0].Value = key.DID;
  586. //paramBody[1].Value = key.ID;
  587. //paramBody[2].Value = body.Quantity;
  588. //paramBody[3].Value = irowno;
  589. // paramBody[4].Value = body.cBatch;
  590. try
  591. {
  592. int count = cmd.ExecuteNonQuery();
  593. if (count <= 0)
  594. {
  595. log.Error("生成材料出库单表体失败,受影响行数<=0;");
  596. throw new Exception("生成材料出库单表体失败,受影响行数<=0;");
  597. }
  598. }
  599. catch (Exception ex)
  600. {
  601. log.Error("生成材料出库单表体失败!SQL:\r\n" + sql, ex);
  602. throw new Exception("生成材料出库单表体失败!SQL:\r\n" + sql, ex);
  603. }
  604. if (head.UpdateStock)
  605. {
  606. //更新现存量
  607. key.cBustypeUN = "领料";
  608. key.cVouchTypeUN = "11";
  609. key.TableName = "IA_ST_UnAccountVouch11";
  610. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key);
  611. 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, key);
  612. }
  613. #region 回写领料申请单已领数量
  614. sql = "UPDATE dbo.MaterialAppVouchs SET fOutQuantity=ISNULL(fOutQuantity,0)+" + body.Quantity + " WHERE AutoID='" + body.SourceDetailID + "'";
  615. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写领料申请单已领数量失败!");
  616. #endregion
  617. }
  618. else
  619. {
  620. throw new Exception("物料:" + body.InvCode + "在现存量表中不存在!");
  621. }
  622. iFatherIdTwo += "'" + iFatherId + "',";
  623. iChildId--;
  624. }
  625. #endregion
  626. //DBHelper.UpdateIDandDID("rd", ErpName, head.list.Count(), cmd);
  627. #region 查询
  628. sql = @"
  629. select a.ID as ID,a.ID as IDs,cMPoCode as SourceCode ,
  630. cCode as IssueCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,
  631. cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime,
  632. cHandler as Checker,dnverifytime as CheckDateTime
  633. from rdrecord11 a
  634. left join Department c on a.cDepCode=c.cDepCode
  635. left join Warehouse d on a.cWhCode=d.cWhCode
  636. WHERE a.ID in ({0})
  637. select a.ID as IDs,b.AutoID as DetailID,irowno as Sequence,b.cInvCode as InvCode
  638. ,iQuantity as Quantity,b.iMaIDs as SourceDetailID,iNum as Amount
  639. from rdrecord11 a
  640. left join rdrecords11 b on a.ID=b.ID
  641. left join Department c on a.cDepCode=c.cDepCode
  642. left join Warehouse d on a.cWhCode=d.cWhCode
  643. WHERE a.ID in ({0})";
  644. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  645. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  646. dtNew1.Merge(ds.Tables[0]);
  647. dtNew2.Merge(ds.Tables[1]);
  648. cmd.Transaction.Commit();
  649. //return msg;
  650. #endregion
  651. }
  652. catch (Exception ex)
  653. {
  654. cmd.Transaction.Rollback();
  655. log.Error(ex.Message);
  656. throw new Exception(ex.Message);
  657. }
  658. finally
  659. {
  660. if (conn.State == ConnectionState.Open)
  661. {
  662. conn.Close();
  663. }
  664. conn.Dispose();
  665. }
  666. }
  667. string RelationName = "details";
  668. DataRelation dr = new DataRelation(RelationName, dtNew1.Columns["IDs"], dtNew2.Columns["IDs"]);
  669. DataSet dz = new DataSet();
  670. dz.Relations.Add(dr);
  671. mag = DBHelper.DataSetToJson(dz, RelationName);
  672. return mag;
  673. }
  674. /// <summary>
  675. /// 审核材料出库单据
  676. /// </summary>
  677. /// <param name="infos"></param>
  678. /// <returns></returns>
  679. public string Approve(List<ICSRdrecord11> infos)
  680. {
  681. List<ICSRdrecord11> szJson = new List<ICSRdrecord11>();
  682. DataTable dt = null;
  683. DataTable dtNew = null;
  684. string connS = "";
  685. string json = "";
  686. if (infos.Count <= 0)
  687. {
  688. throw new Exception("传送数据为空!");
  689. }
  690. string res = string.Empty;
  691. SqlConnection conn = new SqlConnection();
  692. SqlCommand cmd = new SqlCommand();
  693. string sql = string.Empty;
  694. foreach (ICSRdrecord11 info in infos)
  695. {
  696. try
  697. {
  698. connS = string.Format(connString, info.WorkPoint);
  699. conn = new System.Data.SqlClient.SqlConnection(connS);
  700. conn.Open();
  701. SqlTransaction sqlTran = conn.BeginTransaction();
  702. cmd = new SqlCommand();
  703. cmd.Transaction = sqlTran;
  704. cmd.Connection = conn;
  705. if (info.MTime < new DateTime(2000, 01, 01))
  706. throw new Exception("请输入正确的操作时间:" + info.MTime);
  707. sql = @"UPDATE dbo.rdrecord11 SET cHandler='" + info.User + @"' ,
  708. dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  709. WHERE ID='" + info.ID + "'";
  710. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核材料出库单失败!");
  711. cmd.Transaction.Commit();
  712. }
  713. catch (Exception ex)
  714. {
  715. cmd.Transaction.Rollback();
  716. log.Error(ex.Message);
  717. throw new Exception(ex.Message);
  718. }
  719. finally
  720. {
  721. if (conn.State == ConnectionState.Open)
  722. {
  723. conn.Close();
  724. }
  725. conn.Dispose();
  726. }
  727. }
  728. return json;
  729. }
  730. /// <summary>
  731. /// 审核材料出库单据
  732. /// </summary>
  733. /// <param name="infos"></param>
  734. /// <returns></returns>
  735. public string Delete(List<ICSRdrecord11> infos)
  736. {
  737. List<ICSRdrecord11> szJson = new List<ICSRdrecord11>();
  738. DataTable dt = null;
  739. DataTable dtNew = null;
  740. string connS = "";
  741. string json = "";
  742. if (infos.Count <= 0)
  743. {
  744. throw new Exception("传送数据为空!");
  745. }
  746. string res = string.Empty;
  747. SqlConnection conn = new SqlConnection();
  748. SqlCommand cmd = new SqlCommand();
  749. string sql = string.Empty;
  750. foreach (ICSRdrecord11 info in infos)
  751. {
  752. try
  753. {
  754. connS = string.Format(connString, info.WorkPoint);
  755. conn = new System.Data.SqlClient.SqlConnection(connS);
  756. conn.Open();
  757. SqlTransaction sqlTran = conn.BeginTransaction();
  758. cmd = new SqlCommand();
  759. cmd.Transaction = sqlTran;
  760. cmd.Connection = conn;
  761. if (info.MTime < new DateTime(2000, 01, 01))
  762. throw new Exception("请输入正确的操作时间:" + info.MTime);
  763. sql = @"delete rdrecord11 where rdrecord11.ID='" + info.ID + "'";
  764. sql += @"delete rdrecords11 where rdrecords11.ID='" + info.ID + "'";
  765. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  766. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  767. // WHERE a.ID='" + info.ID + "'";
  768. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除材料出库单失败!");
  769. cmd.Transaction.Commit();
  770. }
  771. catch (Exception ex)
  772. {
  773. cmd.Transaction.Rollback();
  774. log.Error(ex.Message);
  775. throw new Exception(ex.Message);
  776. }
  777. finally
  778. {
  779. if (conn.State == ConnectionState.Open)
  780. {
  781. conn.Close();
  782. }
  783. conn.Dispose();
  784. }
  785. }
  786. return json;
  787. }
  788. /// <summary>
  789. /// 记录日志
  790. /// </summary>
  791. /// <param name="Bills"></param>
  792. private void LogInfo(List<ICSRdrecord11> Bills)
  793. {
  794. string HeadList = string.Empty;
  795. string BodyList = string.Empty;
  796. foreach (ICSRdrecord11 head in Bills)
  797. {
  798. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  799. foreach (rdrecords11 body in head.details)
  800. {
  801. BodyList += "\r\n 表体主键ID: " + body.SourceDetailID + ",数量:" + body.Quantity;
  802. }
  803. }
  804. log.Info(HeadList);
  805. log.Info(BodyList);
  806. }
  807. /// <summary>
  808. /// 表体合并
  809. /// </summary>
  810. /// <param name="Bills"></param>
  811. //private void MergeObject(List<ICSRdrecord11> Bills, SqlCommand cmd)
  812. //{
  813. // foreach (ICSRdrecord11 head in Bills)
  814. // {
  815. // DataTable dt = DBHelper.ToDataTable(head.details.ToList());
  816. // DataTable dtNew = dt.Clone();
  817. // dtNew = DBHelper.MergeDataTable(dt, "AllocateId", "cBatch", "iQuantity", "cInvCode", cmd);
  818. // List<rdrecords11> list = DBHelper.ConvertTo<rdrecords11>(dtNew).ToList();
  819. // head.details = list;
  820. // }
  821. //}
  822. }
  823. }