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

368 lines
19 KiB

3 years ago
  1. using NFine.Data.Extensions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using NFine.Code;
  9. using NFine.Repository;
  10. using System.Data.Common;
  11. using NFine.Domain._03_Entity.SRM;
  12. using ICS.Application.Entity;
  13. using Newtonsoft.Json;
  14. using System.Configuration;
  15. using System.Data.SqlClient;
  16. using ICS.Data;
  17. using System.Net;
  18. using System.IO;
  19. using Newtonsoft.Json.Linq;
  20. using NFine.Domain._03_Entity.WMS;
  21. namespace NFine.Application.WMS
  22. {
  23. public class ICSMTDOCApp : RepositoryFactory<ICSVendor>
  24. {
  25. public DataTable GetGridJson(string queryJson, ref Pagination jqgridparam)
  26. {
  27. DataTable dt = new DataTable();
  28. var queryParam = queryJson.ToJObject();
  29. List<DbParameter> parameter = new List<DbParameter>();
  30. #region [SQL]
  31. string sql = @" select distinct a.MTDOCCode,a.DepCode,a.WHCode,a.CreatePerson,a.CreateDateTime,a.Status,a.MUSER,a.MUSERName,a.MTIME from ICSMTDOC a";
  32. sql += " WHERE 1=1";
  33. sql = string.Format(sql, DbHelper.GetErpIp(), DbHelper.GetErpName());
  34. #endregion
  35. if (!string.IsNullOrWhiteSpace(queryJson))
  36. {
  37. if (!string.IsNullOrWhiteSpace(queryParam["MTDOCCode"].ToString()))
  38. {
  39. sql += " and a.MTDOCCode like '%" + queryParam["MTDOCCode"].ToString() + "%' ";
  40. }
  41. }
  42. if (NFine.Code.OperatorProvider.Provider.GetCurrent().RoleEnCode != "admin")
  43. {
  44. sql += " and a.WorkPoint='" + NFine.Code.OperatorProvider.Provider.GetCurrent().Location + "'";
  45. }
  46. //if (NFine.Code.OperatorProvider.Provider.GetCurrent().RoleEnCode == "Vendor")
  47. //{
  48. // sql += " and a.VenCode='" + NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode + "'";
  49. //}
  50. return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
  51. }
  52. public DataTable GetSubGridJson(string MTDOCCode )
  53. {
  54. DataTable dt = new DataTable();
  55. //var queryParam = queryJson.ToJObject();
  56. string sql = string.Empty;
  57. List<DbParameter> parameter = new List<DbParameter>();
  58. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location.TrimEnd(',');
  59. sql = @" select a.ID,a.DepCode,d.DepName,a.WHCode,c.WarehouseName,a.Sequence,a.LotNo,b.InvName,a.Quantity,a.Amount,a.MTDOCQuantity,a.MTDOCType
  60. from ICSMTDOC a
  61. left join dbo.ICSInventoryLot e on a.LotNo=e.LotNo and a.WorkPoint=e.WorkPoint
  62. left join ICSInventory b on e.InvCode=b.InvCode and a.WorkPoint=b.WorkPoint
  63. left join ICSWarehouse c on a.WHCode=c.WarehouseCode and a.WorkPoint=c.WorkPoint
  64. left join ICSDepartment d on a.DepCode=d.DepCode and a.WorkPoint=d.WorkPoint
  65. WHERE a.MTDOCCode='{0}' and a.WorkPoint in ('{1}') order by a.LotNo ";
  66. sql = string.Format(sql, MTDOCCode, WorkPoint);
  67. return Repository().FindTableBySql(sql.ToString());
  68. }
  69. public DataTable GetRepertory(string LotNo, ref Pagination jqgridparam)
  70. {
  71. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  72. List<DbParameter> parameter = new List<DbParameter>();
  73. DataTable table = new DataTable();
  74. string wherestr = "";
  75. if (!string.IsNullOrEmpty(LotNo))
  76. {
  77. wherestr += " and a.LotNO like '%" + LotNo + "%'";
  78. }
  79. string sql = @"select a.WarehouseCode,a.LotNo,b.AmountRate,a.InvCode,c.InvName,c.InvDesc,c.InvStd,c.InvUnit,a.Quantity
  80. from ICSWareHouseLotInfo a
  81. inner join ICSInventoryLot b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
  82. left join ICSInventory c on b.InvCode=c.InvCode and b.WorkPoint=c.WorkPoint
  83. WHERE a.WorkPoint = '" + WorkPoint + "'"+ wherestr;
  84. return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
  85. }
  86. public DataTable GetInvcode(string Invcode, ref Pagination jqgridparam)
  87. {
  88. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  89. List<DbParameter> parameter = new List<DbParameter>();
  90. DataTable table = new DataTable();
  91. string wherestr = "";
  92. if (!string.IsNullOrEmpty(Invcode))
  93. {
  94. wherestr += " and InvCode like '%" + Invcode + "%'";
  95. }
  96. string sql = @"select distinct InvCode,InvName,InvStd,InvUnit,InvDesc from dbo.ICSInventory
  97. WHERE WorkPoint = '" + WorkPoint + "'" + wherestr;
  98. return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
  99. }
  100. public DataTable GetCode(string BatchCode, ref Pagination jqgridparam)
  101. {
  102. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  103. List<DbParameter> parameter = new List<DbParameter>();
  104. DataTable dt = new DataTable();
  105. DataTable table = new DataTable();
  106. string wherestr = "";
  107. if (!string.IsNullOrEmpty(BatchCode))
  108. {
  109. wherestr += " and BatchCode like '%" + BatchCode + "%'";
  110. }
  111. string sql = @"select distinct ID as ZYID, ProjectCode,BatchCode,Version,Brand from ICSExtension
  112. WHERE WorkPoint = '" + WorkPoint + "'" + wherestr;
  113. return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
  114. }
  115. //新增形态转换
  116. public string CreateICSMTDOC(string ICSMTDOC, string InvCode, string ExtensionID, string Memo)
  117. {
  118. string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
  119. string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
  120. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  121. string msg = "";
  122. string MTDOCCode = "";
  123. DateTime TimeNow = DateTime.Now;
  124. string sql = string.Empty;
  125. JArray res = (JArray)JsonConvert.DeserializeObject(ICSMTDOC);
  126. int Num = 1;
  127. DataTable dts = new DataTable();
  128. string Time = DateTime.Now.ToString("yyyyMMdd");
  129. string sqlss = @"select max(MTDOCCode) as NewMTDOCCode from ICSMTDOC where substring(MTDOCCode,1,8)='{0}'";
  130. sqlss = string.Format(sqlss, Time);
  131. dts = Repository().FindTableBySql(sqlss.ToString());
  132. if (dts == null || dts.Rows.Count == 0 || dts.Rows[0]["NewMTDOCCode"].ToString() == "")
  133. {
  134. MTDOCCode = Time + "000001";
  135. }
  136. else
  137. {
  138. string NewMTDOCCode = dts.Rows[0]["NewMTDOCCode"].ToString();
  139. int COUNT = Convert.ToInt32(NewMTDOCCode.Substring(8)) + 1;
  140. MTDOCCode = Time + COUNT.ToString().PadLeft(6,'0');
  141. }
  142. foreach (var item in res)
  143. {
  144. JObject jo = (JObject)item;
  145. DataTable dt = new DataTable();
  146. string NewLotNo = "";
  147. string LotNo = jo["LotNo"].ToString();
  148. string sqls = @"select max(LotNo) as NewLotNo from ICSInventoryLot where EATTRIBUTE1='{0}' ";
  149. sqls = string.Format(sqls, LotNo);
  150. dt = Repository().FindTableBySql(sqls.ToString());
  151. if (dt == null || dt.Rows.Count == 0 || dt.Rows[0]["NewLotNo"].ToString() == "")
  152. {
  153. NewLotNo = LotNo + "-1";
  154. }
  155. else
  156. {
  157. string newLotNO = dt.Rows[0]["NewLotNo"].ToString();
  158. int COUNT = Convert.ToInt32(newLotNO.Substring(newLotNO.LastIndexOf('-') + 1)) + 1;
  159. NewLotNo= LotNo + "-" + COUNT.ToString();
  160. }
  161. //老
  162. sql += @"INSERT INTO dbo.ICSMTDOC
  163. ( ID ,MTDOCCode,WHCode ,Sequence ,LotNo,Quantity,Amount,MTDOCQuantity,MTDOCType,Memo,Status,CreatePerson,
  164. CreateDateTime,MTDOCID,MTDOCDetailID,ExtensionID,MUSER,MUSERName ,MTIME ,WorkPoint )
  165. values(NEWID(),'{0}','{1}','{2}','{3}','{4}','{5}','{6}','1','{7}','1','{8}','{16}',newid(),newid(),'{9}','{10}','{11}','{16}','{12}')";
  166. //新
  167. sql += @"INSERT INTO dbo.ICSMTDOC
  168. ( ID ,MTDOCCode,WHCode ,Sequence ,LotNo,Quantity,Amount,MTDOCQuantity,MTDOCType,Memo,Status,CreatePerson,
  169. CreateDateTime,MTDOCID,MTDOCDetailID,ExtensionID,MUSER,MUSERName ,MTIME ,WorkPoint )
  170. values(NEWID(),'{0}','{1}','{14}','{13}','{4}','{5}','{6}','2','{7}','1','{8}','{16}',newid(),newid(),'{9}','{10}','{11}','{16}','{12}')";
  171. //新增条码
  172. sql += @"INSERT into ICSInventoryLot(ID,LotNo,InvCode,ProductDate,ExpirationDate,Quantity,AmountRate,ExtensionID,Type,MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE1)
  173. select newid(),'{13}','{15}',ProductDate,ExpirationDate,Quantity,AmountRate,ExtensionID,'8',MUSER,MUSERName,MTIME,WorkPoint,'{3}' from ICSInventoryLot
  174. where LotNo='{3}'";
  175. //新增条码关联
  176. sql+= @" INSERT into ICSInventoryLotDetail(LotNo,TransCode,TransSequence,MUSER,MUSERName,MTIME,WorkPoint)
  177. select '{13}',TransCode,TransSequence,MUSER,MUSERName,MTIME,WorkPoint from ICSInventoryLotDetail
  178. where LotNo='{3}'";
  179. sql = string.Format(sql, MTDOCCode, jo["WHCode"].ToString(), Num++, jo["LotNo"].ToString(), jo["Quantity"].ToString(), jo["Amount"].ToString(), jo["MTDOCQuantity"].ToString(), Memo, MUSER, ExtensionID, MUSER, MUSERNAME, WorkPoint, NewLotNo, Num++, InvCode, TimeNow);
  180. }
  181. if (SqlHelper.CmdExecuteNonQueryLi(sql) > 0)
  182. {
  183. msg ="";
  184. }
  185. else
  186. {
  187. msg = "形态转换失败!";
  188. }
  189. return msg;
  190. }
  191. //审核
  192. public string ICSMTDOCAudit(string MTDOCCode)
  193. {
  194. string msg = "";
  195. string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
  196. string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
  197. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  198. DateTime TimeNow = DateTime.Now;
  199. string sql = string.Empty;
  200. string sqlAsn = @" select LotNo from ICSMTDOC where MTDOCCode='{0}' and MTDOCType='2'";
  201. sqlAsn = string.Format(sqlAsn, MTDOCCode);
  202. DataTable dt = Repository().FindTableBySql(sqlAsn.ToString());
  203. for (int i = 0; i < dt.Rows.Count; i++)
  204. {
  205. string NewLotNo = dt.Rows[i]["LotNo"].ToString();
  206. string sqlAsnD = @"select EATTRIBUTE1 from dbo.ICSInventoryLot where LotNo='{0}' ";
  207. sqlAsnD = string.Format(sqlAsnD, NewLotNo);
  208. DataTable dtD = Repository().FindTableBySql(sqlAsnD.ToString());
  209. string OldLotNo = dtD.Rows[0]["EATTRIBUTE1"].ToString();
  210. //修改形态表状态
  211. sql += @"UPDATE ICSMTDOC set Status='2',MTIME='" + TimeNow + "',MUSER ='" + MUSER + "',MUSERName='" + MUSERNAME + "' WHERE WorkPoint='" + WorkPoint + "'";
  212. //加入一条老条码的出库记录
  213. sql += @"INSERT into ICSWareHouseLotInfoLog(ID,TransCode,TransSequence,LotNo,InvCode,FromWarehouseCode,FromLocationCode,
  214. Quantity,Lock,TransType,BusinessCode,
  215. MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE1)
  216. select newid(),c.TransCode,c.TransSequence, a.LotNo,a.InvCode,a.WarehouseCode,a.LocationCode,a.Quantity,'0' ,'10','35','" + MUSER + @"','" + MUSERNAME + @"','" + TimeNow + @"',a.WorkPoint,''
  217. from ICSWareHouseLotInfo a
  218. left join dbo.ICSInventoryLot b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
  219. left join dbo.ICSInventoryLotDetail c on b.LotNo=c.LotNo and b.WorkPoint=c.WorkPoint
  220. where a.LotNo='" + OldLotNo + "' and a.WorkPoint='" + WorkPoint + "'";
  221. //新条码加入库存表
  222. sql += @"INSERT into ICSWareHouseLotInfo(ID,LotNo,WarehouseCode,LocationCode,InvCode,Quantity,InDate,LockQuantity,MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE1)
  223. select newid(),'" + NewLotNo + @"',WarehouseCode,LocationCode,InvCode,Quantity,InDate,LockQuantity,'" + MUSER + @"','" + MUSERNAME + @"','" + TimeNow + @"',WorkPoint,EATTRIBUTE1 from ICSWareHouseLotInfo
  224. where LotNo='" + OldLotNo + "'";
  225. //修改老条码库存
  226. sql += @"UPDATE ICSWareHouseLotInfo set Quantity=0,MTIME='" + TimeNow + "',MUSER='" + MUSER + "',MUSERName='" + MUSERNAME + "' WHERE LotNo='" + OldLotNo + "'";
  227. //库存记录加入新条码记录
  228. sql += @"INSERT into ICSWareHouseLotInfoLog(ID,TransCode,TransSequence,LotNo,InvCode,ToWarehouseCode,ToLocationCode,
  229. Quantity,Lock,TransType,BusinessCode,
  230. MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE1)
  231. select newid(),c.TransCode,c.TransSequence, a.LotNo,a.InvCode,a.WarehouseCode,a.LocationCode,a.Quantity,'0' ,'10','36','" + MUSER + @"','" + MUSERNAME + @"','" + TimeNow + @"',a.WorkPoint,''
  232. from ICSWareHouseLotInfo a
  233. left join dbo.ICSInventoryLot b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
  234. left join dbo.ICSInventoryLotDetail c on b.LotNo=c.LotNo and b.WorkPoint=c.WorkPoint
  235. where a.LotNo='" + NewLotNo + "' and a.WorkPoint='" + WorkPoint + "'";
  236. }
  237. //sql += @"select distinct DepCode from ICSMTDOC where MTDOCCode='" + MTDOCCode + @"' and WorkPoint='" + WorkPoint + @"'";
  238. //sql += @"select
  239. // row_number() over(order by a.MTDOCType,b.InvCode) as Sequence,
  240. // ((row_number() over(order by a.MTDOCType,b.InvCode))+1)/2 as GroupNO,
  241. // a.MTDOCType,b.InvCode,a.WHCode,c.BatchCode,c.ProjectCode,sum(a.Quantity) as Quantity ,sum(a.Amount)as Amount
  242. // from ICSMTDOC a
  243. // left join dbo.ICSInventoryLot b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
  244. // left join ICSExtension c on a.ExtensionID=c.ID and a.WorkPoint=c.WorkPoint
  245. // where a.MTDOCCode='" + MTDOCCode + @"' and a.WorkPoint='" + WorkPoint + @"'
  246. // group by a.MTDOCType,b.InvCode,a.WHCode,c.BatchCode,c.ProjectCode";
  247. sql += @"select isnull(a.DepCode,'')+a.WHCode+a.MUSER as Costre , isnull(a.DepCode,'') as DepCode ,a.WHCode as InWhCode,a.WHCode as OutWhCode,row_number() over(order by a.WHCode,b.InvCode) as Sequence ,(row_number() over(order by a.WHCode,b.InvCode)-1)/2+1 as GroupNO,b.InvCode,d.Name as [Type] ,a.WHCode as WHCode,isnull(c.BatchCode,'') as BatchCode ,isnull(c.ProjectCode,'') as ProjectCode,
  248. sum(a.Quantity) as Quantity ,a.Amount,a.MUSER as [User]
  249. INTO #TempERP
  250. from ICSMTDOC a
  251. left join ICSInventoryLot b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
  252. left join ICSExtension c on a.ExtensionID=c.ID and a.WorkPoint=c.WorkPoint
  253. left join ICSType d on d.TableCode='ICSMTDOC' and d.ColumnCode='MTDOCType' and d.Code=a.MTDOCType
  254. where a.MTDOCCode='{0}' and a.WorkPoint='{1}'
  255. group by isnull(a.DepCode,'') ,a.WHCode ,b.InvCode,d.Name ,isnull(c.BatchCode,'') ,isnull(c.ProjectCode,'') ,
  256. a.Amount,a.MUSER
  257. select distinct Costre, DepCode,InWhCode,OutWhCode,[User],getdate() as MTime FROM #TempERP
  258. select Costre, Sequence,GroupNO,[Type],InvCode,WHCode,BatchCode,ProjectCode,Quantity,Amount FROM #TempERP
  259. DROP TABLE #TempERP";
  260. sql = string.Format(sql, MTDOCCode, WorkPoint);
  261. CmdExecuteData(sql);
  262. return msg;
  263. }
  264. public static void CmdExecuteData(string sql)
  265. {
  266. try
  267. {
  268. string connString = SqlHelper.DataCenterConnString;
  269. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  270. conn.Open();
  271. SqlTransaction sqlTran = conn.BeginTransaction();
  272. SqlCommand cmd = new SqlCommand();
  273. cmd.Transaction = sqlTran;
  274. cmd.Connection = conn;
  275. try
  276. {
  277. DataSet DSet = SqlCommandHelper.SQlReturnDataSet(sql, cmd);
  278. string Inputstr = SqlHelper.DataSetToJson(DSet, "details", "Costre");
  279. string APIURL = ConfigurationManager.ConnectionStrings["ERPAPIURL"].ConnectionString + "MorphologicalTransformationDoc/Create";
  280. string result = HttpPost(APIURL, Inputstr);
  281. JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
  282. string MessAge = Obj["Message"].ToString();
  283. string Success = Obj["Success"].ToString();
  284. if (Success.ToUpper() == "FALSE")
  285. {
  286. throw new Exception(MessAge);
  287. }
  288. cmd.Transaction.Commit();
  289. //return dt;
  290. }
  291. catch (Exception ex)
  292. {
  293. cmd.Transaction.Rollback();
  294. throw new Exception(ex.Message);
  295. }
  296. finally
  297. {
  298. if (conn.State == ConnectionState.Open)
  299. {
  300. conn.Close();
  301. }
  302. conn.Dispose();
  303. }
  304. }
  305. catch (Exception ex)
  306. {
  307. throw new Exception(ex.Message);
  308. }
  309. }
  310. public static string HttpPost(string url, string body)
  311. {
  312. try
  313. {
  314. Encoding encoding = Encoding.UTF8;
  315. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  316. request.Method = "POST";
  317. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  318. request.ContentType = "application/json; charset=utf-8";
  319. byte[] buffer = encoding.GetBytes(body);
  320. request.ContentLength = buffer.Length;
  321. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  322. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  323. using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
  324. {
  325. return reader.ReadToEnd();
  326. }
  327. }
  328. catch (WebException ex)
  329. {
  330. throw new Exception(ex.Message);
  331. }
  332. }
  333. }
  334. }