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

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