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

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