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

976 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
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 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
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 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 ,iNum,
  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,@iNum,
  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("@iNum", body.Amount));
  309. //cmd.Parameters.Add(new SqlParameter("@cBatch", "''"));
  310. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  311. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  312. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  313. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  314. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  315. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  316. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  317. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  318. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  319. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  320. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  321. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  322. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  323. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  324. cmd.CommandText = sql;
  325. try
  326. {
  327. int count = cmd.ExecuteNonQuery();
  328. if (count <= 0)
  329. {
  330. log.Error("生成材料出库单表体失败,受影响行数<=0;");
  331. throw new Exception("生成材料出库单表体失败,受影响行数<=0;");
  332. }
  333. }
  334. catch (Exception ex)
  335. {
  336. log.Error("生成材料出库单表体失败" + sql, ex);
  337. throw new Exception("生成材料出库单表体失败 " + sql, ex);
  338. }
  339. if (head.UpdateStock)
  340. {
  341. //更新现存量
  342. key.cBustypeUN = "领料";
  343. key.cVouchTypeUN = "11";
  344. key.TableName = "IA_ST_UnAccountVouch11";
  345. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key);
  346. 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);
  347. }
  348. #region 回写工单子件已领数量
  349. sql = "update mom_moallocate set IssQty=isnull(IssQty,0)+" + body.Quantity + ",RequisitionIssQty=isnull(RequisitionIssQty,0)+(" + body.Quantity + ") where AllocateId='" + body.SourceDetailID + "' ";
  350. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写工单子件已领数量失败!");
  351. #endregion
  352. iFatherIdTwo += "'" + iFatherId + "',";
  353. iChildId--;
  354. }
  355. #endregion
  356. #region 查询
  357. sql = @"
  358. select a.ID as ID,a.ID as IDs,cMPoCode as SourceCode ,
  359. cCode as IssueCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,
  360. cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime,
  361. cHandler as Checker,dnverifytime as CheckDateTime
  362. from rdrecord11 a
  363. left join Department c on a.cDepCode=c.cDepCode
  364. left join Warehouse d on a.cWhCode=d.cWhCode
  365. WHERE a.ID in ({0})
  366. select a.ID as IDs,b.AutoID as DetailID,irowno as Sequence,b.cInvCode as InvCode
  367. ,iQuantity as Quantity,e.AllocateId as SourceDetailID,iNum as Amount
  368. from rdrecord11 a
  369. left join rdrecords11 b on a.ID=b.ID
  370. left join Department c on a.cDepCode=c.cDepCode
  371. left join Warehouse d on a.cWhCode=d.cWhCode
  372. left join mom_moallocate e on b.iMPoIds=e.AllocateId
  373. INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId
  374. INNER JOIN dbo.mom_order g ON f.MoId=g.MoId WHERE a.ID in ({0})";
  375. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  376. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  377. if (dtNew1 == null)
  378. dtNew1 = ds.Tables[0];
  379. else
  380. dtNew1.Merge(ds.Tables[0]);
  381. if (dtNew2 == null)
  382. dtNew2 = ds.Tables[1];
  383. else
  384. dtNew2.Merge(ds.Tables[1]);
  385. #endregion
  386. cmd.Transaction.Commit();
  387. }
  388. catch (Exception ex)
  389. {
  390. cmd.Transaction.Rollback();
  391. log.Error(ex.Message);
  392. throw new Exception(ex.Message);
  393. }
  394. finally
  395. {
  396. if (conn.State == ConnectionState.Open)
  397. {
  398. conn.Close();
  399. }
  400. conn.Dispose();
  401. }
  402. }
  403. string RelationName = "details";
  404. DataSet dz = new DataSet();
  405. dtNew1.TableName = "Table0";
  406. dtNew2.TableName = "Table1";
  407. dz.Tables.Add(dtNew1.Copy());
  408. dz.Tables.Add(dtNew2.Copy());
  409. DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["IDs"], dz.Tables[1].Columns["IDs"]);
  410. dz.Relations.Add(dh);
  411. msg = DBHelper.DataSetToJson(dz, RelationName);
  412. return msg;
  413. }
  414. public string MaterialAppVouchs(List<ICSRdrecord11> Bills, string IsReturn)
  415. {
  416. string mag = "";
  417. DataTable dtNew1 = null;
  418. DataTable dtNew2 = null;
  419. string connS = "";
  420. string iFatherIdTwo = string.Empty;
  421. SqlConnection conn = new SqlConnection();
  422. SqlCommand cmd = new SqlCommand();
  423. int num = 0;
  424. foreach (ICSRdrecord11 head in Bills)
  425. {
  426. num = head.details.Count();
  427. }
  428. if (Bills.Count <= 0)
  429. {
  430. throw new Exception("传送数据为空!");
  431. }
  432. LogInfo(Bills);
  433. //MergeObject(Bills, cmd);
  434. foreach (ICSRdrecord11 head in Bills)
  435. {
  436. try
  437. {
  438. string sql = string.Empty;
  439. connS = string.Format(connString, head.WorkPoint);
  440. conn = new System.Data.SqlClient.SqlConnection(connS);
  441. conn.Open();
  442. SqlTransaction sqlTran = conn.BeginTransaction();
  443. cmd = new SqlCommand();
  444. cmd.Transaction = sqlTran;
  445. cmd.Connection = conn;
  446. string[] ss = head.WorkPoint.Split('_');
  447. ERPDB = ss[1];
  448. if (DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint))
  449. {
  450. }
  451. else
  452. {
  453. throw new Exception("U8正在整理现存量,请稍后再试");
  454. }
  455. #region 判断物料是否超额领取
  456. foreach (rdrecords11 body in head.details)
  457. {
  458. sql = @"SELECT isnull(fOutQuantity,0) as fOutQuantity,iQuantity FROM dbo.MaterialAppVouchs WHERE AutoID='" + body.SourceDetailID + "'";
  459. DataTable dtQty = DBHelper.SQlReturnData(sql, cmd);
  460. if (dtQty != null && dtQty.Rows.Count > 0)
  461. {
  462. //已领数量
  463. decimal IssQty = Convert.ToDecimal(dtQty.Rows[0]["fOutQuantity"]);
  464. //应领数量
  465. decimal Qty = Convert.ToDecimal(dtQty.Rows[0]["iQuantity"]);
  466. if (IssQty + body.Quantity > Qty)
  467. {
  468. throw new Exception("物料领取超过当前可领用最大数,订单号:" + head.SourceCode + ",行号:" + body.Sequence + " ");
  469. }
  470. }
  471. else
  472. {
  473. throw new Exception("领料申请单不存在!订单号:" + head.SourceCode);
  474. }
  475. }
  476. #endregion
  477. #region 现存量是否够领
  478. // //if (head.IsReturn != "1")
  479. // //{
  480. // foreach (rdrecords11 body in head.rdcords)
  481. // {
  482. // sql = string.Format(@"SELECT iQuantity FROM dbo.CurrentStock
  483. // WHERE cInvCode='{0}' AND cWhCode='{1}'", body.cInvCode, head.cWhCode);
  484. // if (!string.IsNullOrWhiteSpace(body.cBatch))
  485. // sql += " and cBatch='" + body.cBatch + "'";
  486. // DataTable dt = DBHelper.SQlReturnData(sql, cmd);
  487. // decimal qtyCurrent = 0;
  488. // if (dt != null && dt.Rows.Count > 0)
  489. // {
  490. // qtyCurrent = Convert.ToDecimal(dt.Rows[0][0]);
  491. // if (qtyCurrent < body.Quantity)
  492. // {
  493. // throw new Exception("物料编号:" + body.cInvCode + ",现存量不足!");
  494. // }
  495. // }
  496. // else
  497. // {
  498. // throw new Exception("该物料:" + body.cInvCode + "在U8中仓库:" + head.cWhCode + "中不存在!");
  499. // }
  500. // }
  501. // //}
  502. #endregion
  503. #region 材料出库单表头
  504. //取得out库单的默认显示模版
  505. string[] dd = CRNALL.Split('~');
  506. string crdname = dd[0];
  507. string carname = dd[1];
  508. string surface = dd[2];
  509. string cardnewcode = DBHelper.GetCardNumber(carname, cmd);
  510. Dictionary<string, int> dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint);
  511. int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
  512. int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
  513. DateTime date = DateTime.Now;
  514. string cRDcode = DBHelper.GetRDCode(crdname, cmd);
  515. string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", head.WorkPoint);
  516. string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd);
  517. //取得采购入库单单据 表头ID,表体DID
  518. VouchKey key = new VouchKey();
  519. sql = @"INSERT INTO dbo.rdrecord11
  520. ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cBusCode ,cWhCode ,
  521. dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cHandler ,cMemo ,
  522. bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,cDefine4 ,
  523. cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,cDefine9 ,cDefine10 ,
  524. dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc ,cDefine11 ,
  525. cDefine12 ,cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 ,
  526. bOMFirst ,bFromPreYear ,bIsComplement ,iDiscountTaxType ,
  527. ireturncount ,iverifystate ,iswfcontrolled ,cModifyPerson ,
  528. dnmaketime ,dnverifytime ,bredvouch ,bmotran ,bHYVouch ,iPrintCount
  529. )
  530. SELECT @ID,0,'11','','',a.cCode,@cWhCode,
  531. CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,a.cDepCode,a.cPersonCode,@cHandler,a.cMemo,
  532. 0,@cMaker,a.cDefine1,cdefine2,a.cDefine3,a.cDefine4,
  533. a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,a.cDefine9,a.cDefine10,
  534. CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0,a.cDefine11,
  535. a.cDefine12,a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16,
  536. 0,0,0,0,
  537. 0,0,0,'',
  538. GETDATE(),GETDATE(),0,0,0,0
  539. FROM dbo.MaterialAppVouch a WHERE a.cCode='" + head.SourceCode + "'";
  540. cmd.Parameters.Clear();
  541. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  542. cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode));
  543. cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen));
  544. cmd.Parameters.Add(new SqlParameter("@cHandler", head.User));
  545. cmd.Parameters.Add(new SqlParameter("@cMaker", head.User));
  546. cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID));
  547. cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode));
  548. //DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表头失败!");
  549. cmd.CommandText = sql;
  550. try
  551. {
  552. int count = cmd.ExecuteNonQuery();
  553. if (count <= 0)
  554. {
  555. log.Error("生成材料出库单表头失败,受影响行数<=0;");
  556. throw new Exception("生成材料出库单表头失败,受影响行数<=0;");
  557. }
  558. }
  559. catch (Exception ex)
  560. {
  561. log.Error("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  562. throw new Exception("生成材料出库单表头失败!SQL:\r\n" + sql, ex);
  563. }
  564. #endregion
  565. #region 材料出库单表体
  566. int irowno = 0;
  567. foreach (rdrecords11 body in head.details)
  568. {
  569. irowno++;
  570. //if (IsReturn == "1")
  571. //{
  572. //body.Quantity = -body.Quantity;
  573. //}
  574. //判断物料批号与现存量表批号是否一致and数量不能超过现存量物料数量
  575. //sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.InvCode + "'AND cBatch=''and cWhCode='" + head.WHCode + "' ";
  576. 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 + "'";
  577. DataTable dtItem = DBHelper.SQlReturnData(sql, cmd);
  578. if (dtItem != null && dtItem.Rows.Count > 0)
  579. {
  580. //if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cBatch))
  581. //{
  582. // throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.InvCode);
  583. //}
  584. if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < body.Quantity)
  585. {
  586. throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.InvCode);
  587. }
  588. sql = @"INSERT INTO dbo.rdrecords11
  589. ( AutoID ,ID ,cInvCode ,iQuantity ,iFlag ,cDefine22 ,iNum,
  590. cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 ,
  591. cItem_class ,cItemCode ,cName ,cItemCName ,iNQuantity ,
  592. cDefine28 ,cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 ,
  593. cDefine33 ,cDefine34 ,cDefine35 ,cDefine36 ,cDefine37 ,
  594. bLPUseFree ,iOriTrackID ,bCosting ,bVMIUsed ,iMaIDs ,
  595. iExpiratDateCalcu ,iordertype ,isotype ,ipesotype ,irowno,
  596. cBatch,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10)
  597. SELECT @AutoID,@ID,a.cInvCode,@iQuantity,0,a.cDefine22,@iNum,
  598. a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27,
  599. a.cItem_class,a.cItemCode,a.cName,a.cItemCName,a.iQuantity,
  600. a.cDefine28,a.cDefine29,a.cDefine30,a.cDefine31,a.cDefine32,
  601. a.cDefine33,a.cDefine34,a.cDefine35,a.cDefine36,a.cDefine37,
  602. 0,0,1,0,a.AutoID,
  603. 0,0,0,0,@irowno,
  604. @cBatch,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10
  605. FROM dbo.MaterialAppVouchs a WHERE a.AutoID='" + body.SourceDetailID + "'";
  606. cmd.Parameters.Clear();
  607. cmd.Parameters.Add(new SqlParameter("@ID", iFatherId));
  608. cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId));
  609. cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity));
  610. cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount));
  611. cmd.Parameters.Add(new SqlParameter("@irowno", irowno));
  612. cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode));
  613. cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1));
  614. cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2));
  615. cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3));
  616. cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4));
  617. cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5));
  618. cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6));
  619. cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7));
  620. cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8));
  621. cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9));
  622. cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10));
  623. //DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表头失败!");
  624. cmd.CommandText = sql;
  625. // SqlParameter[] paramBody = {
  626. // new SqlParameter("@AutoID",DbType.String),
  627. // new SqlParameter("@ID",DbType.String),
  628. // new SqlParameter("@iQuantity",DbType.Decimal),
  629. // new SqlParameter("@irowno",DbType.String),
  630. // new SqlParameter("@cBatch",DbType.String)
  631. //};
  632. //paramBody[0].Value = key.DID;
  633. //paramBody[1].Value = key.ID;
  634. //paramBody[2].Value = body.Quantity;
  635. //paramBody[3].Value = irowno;
  636. // paramBody[4].Value = body.cBatch;
  637. try
  638. {
  639. int count = cmd.ExecuteNonQuery();
  640. if (count <= 0)
  641. {
  642. log.Error("生成材料出库单表体失败,受影响行数<=0;");
  643. throw new Exception("生成材料出库单表体失败,受影响行数<=0;");
  644. }
  645. }
  646. catch (Exception ex)
  647. {
  648. log.Error("生成材料出库单表体失败!SQL:\r\n" + sql, ex);
  649. throw new Exception("生成材料出库单表体失败!SQL:\r\n" + sql, ex);
  650. }
  651. if (head.UpdateStock)
  652. {
  653. //更新现存量
  654. key.cBustypeUN = "领料";
  655. key.cVouchTypeUN = "11";
  656. key.TableName = "IA_ST_UnAccountVouch11";
  657. //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key);
  658. 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);
  659. }
  660. if (head.SourceType == "委外领料申请单")
  661. {
  662. #region 回写领料申请单已领数量
  663. sql = @"
  664. 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 + @"')
  665. UPDATE dbo.MaterialAppVouchs SET fOutQuantity=ISNULL(fOutQuantity,0)+(" + body.Quantity + ") WHERE AutoID='" + body.SourceDetailID + "'";
  666. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写领料申请单已领数量失败!");
  667. #endregion
  668. }
  669. else
  670. {
  671. #region 回写领料申请单已领数量
  672. sql = @"
  673. 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 + @"')
  674. UPDATE dbo.MaterialAppVouchs SET fOutQuantity=ISNULL(fOutQuantity,0)+(" + body.Quantity + ") WHERE AutoID='" + body.SourceDetailID + "'";
  675. DBHelper.CmdExecuteNonQuery(sql, cmd, "回写领料申请单已领数量失败!");
  676. #endregion
  677. }
  678. }
  679. else
  680. {
  681. throw new Exception("物料:" + body.InvCode + "在现存量表中不存在!");
  682. }
  683. iFatherIdTwo += "'" + iFatherId + "',";
  684. iChildId--;
  685. }
  686. #endregion
  687. //DBHelper.UpdateIDandDID("rd", ErpName, head.list.Count(), cmd);
  688. #region 查询
  689. sql = @"
  690. select a.ID as ID,a.ID as IDs,cMPoCode as SourceCode ,
  691. cCode as IssueCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,
  692. cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime,
  693. cHandler as Checker,dnverifytime as CheckDateTime
  694. from rdrecord11 a
  695. left join Department c on a.cDepCode=c.cDepCode
  696. left join Warehouse d on a.cWhCode=d.cWhCode
  697. WHERE a.ID in ({0})
  698. select a.ID as IDs,b.AutoID as DetailID,irowno as Sequence,b.cInvCode as InvCode
  699. ,iQuantity as Quantity,b.iMaIDs as SourceDetailID,iNum as Amount
  700. from rdrecord11 a
  701. left join rdrecords11 b on a.ID=b.ID
  702. left join Department c on a.cDepCode=c.cDepCode
  703. left join Warehouse d on a.cWhCode=d.cWhCode
  704. WHERE a.ID in ({0})";
  705. sql = string.Format(sql, iFatherIdTwo.TrimEnd(','));
  706. DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
  707. if (dtNew1 == null)
  708. dtNew1 = ds.Tables[0];
  709. else
  710. dtNew1.Merge(ds.Tables[0]);
  711. if (dtNew2 == null)
  712. dtNew2 = ds.Tables[1];
  713. else
  714. dtNew2.Merge(ds.Tables[1]);
  715. cmd.Transaction.Commit();
  716. //return msg;
  717. #endregion
  718. }
  719. catch (Exception ex)
  720. {
  721. cmd.Transaction.Rollback();
  722. log.Error(ex.Message);
  723. throw new Exception(ex.Message);
  724. }
  725. finally
  726. {
  727. if (conn.State == ConnectionState.Open)
  728. {
  729. conn.Close();
  730. }
  731. conn.Dispose();
  732. }
  733. }
  734. string RelationName = "details";
  735. DataSet dz = new DataSet();
  736. dtNew1.TableName = "Table0";
  737. dtNew2.TableName = "Table1";
  738. dz.Tables.Add(dtNew1.Copy());
  739. dz.Tables.Add(dtNew2.Copy());
  740. DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["IDs"], dz.Tables[1].Columns["IDs"]);
  741. dz.Relations.Add(dh);
  742. mag = DBHelper.DataSetToJson(dz, RelationName);
  743. return mag;
  744. }
  745. /// <summary>
  746. /// 审核材料出库单据
  747. /// </summary>
  748. /// <param name="infos"></param>
  749. /// <returns></returns>
  750. public string Approve(List<ICSRdrecord11> infos)
  751. {
  752. List<ICSRdrecord11> szJson = new List<ICSRdrecord11>();
  753. DataTable dt = null;
  754. DataTable dtNew = null;
  755. string connS = "";
  756. string json = "";
  757. if (infos.Count <= 0)
  758. {
  759. throw new Exception("传送数据为空!");
  760. }
  761. string res = string.Empty;
  762. SqlConnection conn = new SqlConnection();
  763. SqlCommand cmd = new SqlCommand();
  764. string sql = string.Empty;
  765. foreach (ICSRdrecord11 info in infos)
  766. {
  767. try
  768. {
  769. connS = string.Format(connString, info.WorkPoint);
  770. conn = new System.Data.SqlClient.SqlConnection(connS);
  771. conn.Open();
  772. SqlTransaction sqlTran = conn.BeginTransaction();
  773. cmd = new SqlCommand();
  774. cmd.Transaction = sqlTran;
  775. cmd.Connection = conn;
  776. if (DBHelper.IsInventoryConsolidation(cmd, info.WorkPoint))
  777. {
  778. }
  779. else
  780. {
  781. throw new Exception("U8正在整理现存量,请稍后再试");
  782. }
  783. if (info.MTime < new DateTime(2000, 01, 01))
  784. throw new Exception("请输入正确的操作时间:" + info.MTime);
  785. sql = @"UPDATE dbo.rdrecord11 SET cHandler='" + info.User + @"' ,
  786. dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  787. WHERE ID='" + info.ID + "'";
  788. DBHelper.CmdExecuteNonQuery(sql, cmd, "审核材料出库单失败!");
  789. cmd.Transaction.Commit();
  790. }
  791. catch (Exception ex)
  792. {
  793. cmd.Transaction.Rollback();
  794. log.Error(ex.Message);
  795. throw new Exception(ex.Message);
  796. }
  797. finally
  798. {
  799. if (conn.State == ConnectionState.Open)
  800. {
  801. conn.Close();
  802. }
  803. conn.Dispose();
  804. }
  805. }
  806. return json;
  807. }
  808. /// <summary>
  809. /// 审核材料出库单据
  810. /// </summary>
  811. /// <param name="infos"></param>
  812. /// <returns></returns>
  813. public string Delete(List<ICSRdrecord11> infos)
  814. {
  815. List<ICSRdrecord11> szJson = new List<ICSRdrecord11>();
  816. DataTable dt = null;
  817. DataTable dtNew = null;
  818. string connS = "";
  819. string json = "";
  820. if (infos.Count <= 0)
  821. {
  822. throw new Exception("传送数据为空!");
  823. }
  824. string res = string.Empty;
  825. SqlConnection conn = new SqlConnection();
  826. SqlCommand cmd = new SqlCommand();
  827. string sql = string.Empty;
  828. foreach (ICSRdrecord11 info in infos)
  829. {
  830. try
  831. {
  832. connS = string.Format(connString, info.WorkPoint);
  833. conn = new System.Data.SqlClient.SqlConnection(connS);
  834. conn.Open();
  835. SqlTransaction sqlTran = conn.BeginTransaction();
  836. cmd = new SqlCommand();
  837. cmd.Transaction = sqlTran;
  838. cmd.Connection = conn;
  839. if (info.MTime < new DateTime(2000, 01, 01))
  840. throw new Exception("请输入正确的操作时间:" + info.MTime);
  841. sql = @"delete rdrecord11 where rdrecord11.ID='" + info.ID + "'";
  842. sql += @"delete rdrecords11 where rdrecords11.ID='" + info.ID + "'";
  843. //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' ,
  844. // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE()
  845. // WHERE a.ID='" + info.ID + "'";
  846. DBHelper.CmdExecuteNonQuery(sql, cmd, "删除材料出库单失败!");
  847. cmd.Transaction.Commit();
  848. }
  849. catch (Exception ex)
  850. {
  851. cmd.Transaction.Rollback();
  852. log.Error(ex.Message);
  853. throw new Exception(ex.Message);
  854. }
  855. finally
  856. {
  857. if (conn.State == ConnectionState.Open)
  858. {
  859. conn.Close();
  860. }
  861. conn.Dispose();
  862. }
  863. }
  864. return json;
  865. }
  866. /// <summary>
  867. /// 记录日志
  868. /// </summary>
  869. /// <param name="Bills"></param>
  870. private void LogInfo(List<ICSRdrecord11> Bills)
  871. {
  872. string HeadList = string.Empty;
  873. string BodyList = string.Empty;
  874. foreach (ICSRdrecord11 head in Bills)
  875. {
  876. HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint;
  877. foreach (rdrecords11 body in head.details)
  878. {
  879. BodyList += "\r\n 表体主键ID: " + body.SourceDetailID + ",数量:" + body.Quantity;
  880. }
  881. }
  882. log.Info(HeadList);
  883. log.Info(BodyList);
  884. }
  885. /// <summary>
  886. /// 表体合并
  887. /// </summary>
  888. /// <param name="Bills"></param>
  889. //private void MergeObject(List<ICSRdrecord11> Bills, SqlCommand cmd)
  890. //{
  891. // foreach (ICSRdrecord11 head in Bills)
  892. // {
  893. // DataTable dt = DBHelper.ToDataTable(head.details.ToList());
  894. // DataTable dtNew = dt.Clone();
  895. // dtNew = DBHelper.MergeDataTable(dt, "AllocateId", "cBatch", "iQuantity", "cInvCode", cmd);
  896. // List<rdrecords11> list = DBHelper.ConvertTo<rdrecords11>(dtNew).ToList();
  897. // head.details = list;
  898. // }
  899. //}
  900. }
  901. }