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

using NFine.Data.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NFine.Code;
using NFine.Repository;
using System.Data.Common;
using NFine.Domain._03_Entity.SRM;
using ICS.Application.Entity;
using Newtonsoft.Json;
using System.Configuration;
using System.Data.SqlClient;
using ICS.Data;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using NFine.Domain._03_Entity.WMS;
namespace NFine.Application.WMS
{
public class ICSMTDOCApp : RepositoryFactory<ICSVendor>
{
public DataTable GetGridJson(string queryJson, ref Pagination jqgridparam)
{
DataTable dt = new DataTable();
var queryParam = queryJson.ToJObject();
List<DbParameter> parameter = new List<DbParameter>();
#region [SQL]
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";
sql += " WHERE 1=1";
sql = string.Format(sql, DbHelper.GetErpIp(), DbHelper.GetErpName());
#endregion
if (!string.IsNullOrWhiteSpace(queryJson))
{
if (!string.IsNullOrWhiteSpace(queryParam["MTDOCCode"].ToString()))
{
sql += " and a.MTDOCCode like '%" + queryParam["MTDOCCode"].ToString() + "%' ";
}
}
if (NFine.Code.OperatorProvider.Provider.GetCurrent().RoleEnCode != "admin")
{
sql += " and a.WorkPoint='" + NFine.Code.OperatorProvider.Provider.GetCurrent().Location + "'";
}
//if (NFine.Code.OperatorProvider.Provider.GetCurrent().RoleEnCode == "Vendor")
//{
// sql += " and a.VenCode='" + NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode + "'";
//}
return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
}
public DataTable GetSubGridJson(string MTDOCCode )
{
DataTable dt = new DataTable();
//var queryParam = queryJson.ToJObject();
string sql = string.Empty;
List<DbParameter> parameter = new List<DbParameter>();
string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location.TrimEnd(',');
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
from ICSMTDOC a
left join dbo.ICSInventoryLot e on a.LotNo=e.LotNo and a.WorkPoint=e.WorkPoint
left join ICSInventory b on e.InvCode=b.InvCode and a.WorkPoint=b.WorkPoint
left join ICSWarehouse c on a.WHCode=c.WarehouseCode and a.WorkPoint=c.WorkPoint
left join ICSDepartment d on a.DepCode=d.DepCode and a.WorkPoint=d.WorkPoint
WHERE a.MTDOCCode='{0}' and a.WorkPoint in ('{1}') order by a.LotNo ";
sql = string.Format(sql, MTDOCCode, WorkPoint);
return Repository().FindTableBySql(sql.ToString());
}
public DataTable GetRepertory(string LotNo, ref Pagination jqgridparam)
{
string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
List<DbParameter> parameter = new List<DbParameter>();
DataTable table = new DataTable();
string wherestr = "";
if (!string.IsNullOrEmpty(LotNo))
{
wherestr += " and a.LotNO like '%" + LotNo + "%'";
}
string sql = @"select a.WarehouseCode,a.LotNo,b.Amount,a.InvCode,c.InvName,c.InvDesc,c.InvStd,c.InvUnit,a.Quantity
from ICSWareHouseLotInfo a
inner join ICSInventoryLot b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
left join ICSInventory c on b.InvCode=c.InvCode and b.WorkPoint=c.WorkPoint
WHERE a.WorkPoint = '" + WorkPoint + "'"+ wherestr;
return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
}
public DataTable GetInvcode(string Invcode, ref Pagination jqgridparam)
{
string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
List<DbParameter> parameter = new List<DbParameter>();
DataTable table = new DataTable();
string wherestr = "";
if (!string.IsNullOrEmpty(Invcode))
{
wherestr += " and InvCode like '%" + Invcode + "%'";
}
string sql = @"select distinct InvCode,InvName,InvStd,InvUnit,InvDesc from dbo.ICSInventory
WHERE WorkPoint = '" + WorkPoint + "'" + wherestr;
return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
}
public DataTable GetCode(string BatchCode, ref Pagination jqgridparam)
{
string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
List<DbParameter> parameter = new List<DbParameter>();
DataTable dt = new DataTable();
DataTable table = new DataTable();
string wherestr = "";
if (!string.IsNullOrEmpty(BatchCode))
{
wherestr += " and BatchCode like '%" + BatchCode + "%'";
}
string sql = @"select distinct ID as ZYID, ProjectCode,BatchCode,Version,Brand from ICSExtension
WHERE WorkPoint = '" + WorkPoint + "'" + wherestr;
return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
}
//新增形态转换
public string CreateICSMTDOC(string ICSMTDOC, string InvCode, string ExtensionID, string Memo)
{
string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
string msg = "";
string MTDOCCode = "";
DateTime TimeNow = DateTime.Now;
string sql = string.Empty;
JArray res = (JArray)JsonConvert.DeserializeObject(ICSMTDOC);
int Num = 1;
DataTable dts = new DataTable();
string Time = DateTime.Now.ToString("yyyyMMdd");
string sqlss = @"select max(MTDOCCode) as NewMTDOCCode from ICSMTDOC where substring(MTDOCCode,1,8)='{0}'";
sqlss = string.Format(sqlss, Time);
dts = Repository().FindTableBySql(sqlss.ToString());
if (dts == null || dts.Rows.Count == 0 || dts.Rows[0]["NewMTDOCCode"].ToString() == "")
{
MTDOCCode = Time + "000001";
}
else
{
string NewMTDOCCode = dts.Rows[0]["NewMTDOCCode"].ToString();
int COUNT = Convert.ToInt32(NewMTDOCCode.Substring(8)) + 1;
MTDOCCode = Time + COUNT.ToString().PadLeft(6,'0');
}
foreach (var item in res)
{
JObject jo = (JObject)item;
DataTable dt = new DataTable();
string NewLotNo = "";
string LotNo = jo["LotNo"].ToString();
string sqls = @"select max(LotNo) as NewLotNo from ICSInventoryLot where EATTRIBUTE1='{0}' ";
sqls = string.Format(sqls, LotNo);
dt = Repository().FindTableBySql(sqls.ToString());
if (dt == null || dt.Rows.Count == 0 || dt.Rows[0]["NewLotNo"].ToString() == "")
{
NewLotNo = LotNo + "-1";
}
else
{
string newLotNO = dt.Rows[0]["NewLotNo"].ToString();
int COUNT = Convert.ToInt32(newLotNO.Substring(newLotNO.LastIndexOf('-') + 1)) + 1;
NewLotNo= LotNo + "-" + COUNT.ToString();
}
//老
sql += @"INSERT INTO dbo.ICSMTDOC
( ID ,MTDOCCode,WHCode ,Sequence ,LotNo,Quantity,Amount,MTDOCQuantity,MTDOCType,Memo,Status,CreatePerson,
CreateDateTime,MTDOCID,MTDOCDetailID,ExtensionID,MUSER,MUSERName ,MTIME ,WorkPoint )
values(NEWID(),'{0}','{1}','{2}','{3}','{4}','{5}','{6}','1','{7}','1','{8}','{16}',newid(),newid(),'{9}','{10}','{11}','{16}','{12}')";
//新
sql += @"INSERT INTO dbo.ICSMTDOC
( ID ,MTDOCCode,WHCode ,Sequence ,LotNo,Quantity,Amount,MTDOCQuantity,MTDOCType,Memo,Status,CreatePerson,
CreateDateTime,MTDOCID,MTDOCDetailID,ExtensionID,MUSER,MUSERName ,MTIME ,WorkPoint )
values(NEWID(),'{0}','{1}','{14}','{13}','{4}','{5}','{6}','2','{7}','1','{8}','{16}',newid(),newid(),'{9}','{10}','{11}','{16}','{12}')";
//新增条码
sql += @"INSERT into ICSInventoryLot(ID,LotNo,InvCode,ProductDate,ExpirationDate,Quantity,Amount,ExtensionID,Type,MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE1)
select newid(),'{13}','{15}',ProductDate,ExpirationDate,Quantity,Amount,ExtensionID,'8',MUSER,MUSERName,MTIME,WorkPoint,'{3}' from ICSInventoryLot
where LotNo='{3}'";
//新增条码关联
sql+= @" INSERT into ICSInventoryLotDetail(LotNo,TransCode,TransSequence,MUSER,MUSERName,MTIME,WorkPoint)
select '{13}',TransCode,TransSequence,MUSER,MUSERName,MTIME,WorkPoint from ICSInventoryLotDetail
where LotNo='{3}'";
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);
}
if (SqlHelper.CmdExecuteNonQueryLi(sql) > 0)
{
msg ="";
}
else
{
msg = "形态转换失败!";
}
return msg;
}
//审核
public string ICSMTDOCAudit(string MTDOCCode)
{
string msg = "";
string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
DateTime TimeNow = DateTime.Now;
string sql = string.Empty;
string sqlAsn = @" select LotNo from ICSMTDOC where MTDOCCode='{0}' and MTDOCType='2'";
sqlAsn = string.Format(sqlAsn, MTDOCCode);
DataTable dt = Repository().FindTableBySql(sqlAsn.ToString());
for (int i = 0; i < dt.Rows.Count; i++)
{
string NewLotNo = dt.Rows[i]["LotNo"].ToString();
string sqlAsnD = @"select EATTRIBUTE1 from dbo.ICSInventoryLot where LotNo='{0}' ";
sqlAsnD = string.Format(sqlAsnD, NewLotNo);
DataTable dtD = Repository().FindTableBySql(sqlAsnD.ToString());
string OldLotNo = dtD.Rows[0]["EATTRIBUTE1"].ToString();
//修改形态表状态
sql += @"UPDATE ICSMTDOC set Status='2',MTIME='" + TimeNow + "',MUSER ='" + MUSER + "',MUSERName='" + MUSERNAME + "' WHERE WorkPoint='" + WorkPoint + "'";
//加入一条老条码的出库记录
sql += @"INSERT into ICSWareHouseLotInfoLog(ID,TransCode,TransSequence,LotNo,InvCode,FromWarehouseCode,FromLocationCode,
Quantity,Lock,TransType,BusinessCode,
MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE1)
select newid(),c.TransCode,c.TransSequence, a.LotNo,a.InvCode,a.WarehouseCode,a.LocationCode,a.Quantity,'0' ,'10','35','" + MUSER + @"','" + MUSERNAME + @"','" + TimeNow + @"',a.WorkPoint,''
from ICSWareHouseLotInfo a
left join dbo.ICSInventoryLot b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
left join dbo.ICSInventoryLotDetail c on b.LotNo=c.LotNo and b.WorkPoint=c.WorkPoint
where a.LotNo='" + OldLotNo + "' and a.WorkPoint='" + WorkPoint + "'";
//新条码加入库存表
sql += @"INSERT into ICSWareHouseLotInfo(ID,LotNo,WarehouseCode,LocationCode,InvCode,Quantity,InDate,LockQuantity,MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE1)
select newid(),'" + NewLotNo + @"',WarehouseCode,LocationCode,InvCode,Quantity,InDate,LockQuantity,'" + MUSER + @"','" + MUSERNAME + @"','" + TimeNow + @"',WorkPoint,EATTRIBUTE1 from ICSWareHouseLotInfo
where LotNo='" + OldLotNo + "'";
//修改老条码库存
sql += @"UPDATE ICSWareHouseLotInfo set Quantity=0,MTIME='" + TimeNow + "',MUSER='" + MUSER + "',MUSERName='" + MUSERNAME + "' WHERE LotNo='" + OldLotNo + "'";
//库存记录加入新条码记录
sql += @"INSERT into ICSWareHouseLotInfoLog(ID,TransCode,TransSequence,LotNo,InvCode,ToWarehouseCode,ToLocationCode,
Quantity,Lock,TransType,BusinessCode,
MUSER,MUSERName,MTIME,WorkPoint,EATTRIBUTE1)
select newid(),c.TransCode,c.TransSequence, a.LotNo,a.InvCode,a.WarehouseCode,a.LocationCode,a.Quantity,'0' ,'10','36','" + MUSER + @"','" + MUSERNAME + @"','" + TimeNow + @"',a.WorkPoint,''
from ICSWareHouseLotInfo a
left join dbo.ICSInventoryLot b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
left join dbo.ICSInventoryLotDetail c on b.LotNo=c.LotNo and b.WorkPoint=c.WorkPoint
where a.LotNo='" + NewLotNo + "' and a.WorkPoint='" + WorkPoint + "'";
}
//sql += @"select distinct DepCode from ICSMTDOC where MTDOCCode='" + MTDOCCode + @"' and WorkPoint='" + WorkPoint + @"'";
//sql += @"select
// row_number() over(order by a.MTDOCType,b.InvCode) as Sequence,
// ((row_number() over(order by a.MTDOCType,b.InvCode))+1)/2 as GroupNO,
// a.MTDOCType,b.InvCode,a.WHCode,c.BatchCode,c.ProjectCode,sum(a.Quantity) as Quantity ,sum(a.Amount)as Amount
// from ICSMTDOC a
// left join dbo.ICSInventoryLot b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
// left join ICSExtension c on a.ExtensionID=c.ID and a.WorkPoint=c.WorkPoint
// where a.MTDOCCode='" + MTDOCCode + @"' and a.WorkPoint='" + WorkPoint + @"'
// group by a.MTDOCType,b.InvCode,a.WHCode,c.BatchCode,c.ProjectCode";
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,
sum(a.Quantity) as Quantity ,a.Amount,a.MUSER as [User]
INTO #TempERP
from ICSMTDOC a
left join ICSInventoryLot b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
left join ICSExtension c on a.ExtensionID=c.ID and a.WorkPoint=c.WorkPoint
left join ICSType d on d.TableCode='ICSMTDOC' and d.ColumnCode='MTDOCType' and d.Code=a.MTDOCType
where a.MTDOCCode='{0}' and a.WorkPoint='{1}'
group by isnull(a.DepCode,'') ,a.WHCode ,b.InvCode,d.Name ,isnull(c.BatchCode,'') ,isnull(c.ProjectCode,'') ,
a.Amount,a.MUSER
select distinct Costre, DepCode,InWhCode,OutWhCode,[User],getdate() as MTime FROM #TempERP
select Costre, Sequence,GroupNO,[Type],InvCode,WHCode,BatchCode,ProjectCode,Quantity,Amount FROM #TempERP
DROP TABLE #TempERP";
sql = string.Format(sql, MTDOCCode, WorkPoint);
CmdExecuteData(sql);
return msg;
}
public static void CmdExecuteData(string sql)
{
try
{
string connString = SqlHelper.DataCenterConnString;
SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
conn.Open();
SqlTransaction sqlTran = conn.BeginTransaction();
SqlCommand cmd = new SqlCommand();
cmd.Transaction = sqlTran;
cmd.Connection = conn;
try
{
DataSet DSet = SqlCommandHelper.SQlReturnDataSet(sql, cmd);
string Inputstr = SqlHelper.DataSetToJson(DSet, "details", "Costre");
string APIURL = ConfigurationManager.ConnectionStrings["ERPAPIURL"].ConnectionString + "MorphologicalTransformationDoc/Create";
string result = HttpPost(APIURL, Inputstr);
JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
string MessAge = Obj["Message"].ToString();
string Success = Obj["Success"].ToString();
if (Success.ToUpper() == "FALSE")
{
throw new Exception(MessAge);
}
cmd.Transaction.Commit();
//return dt;
}
catch (Exception ex)
{
cmd.Transaction.Rollback();
throw new Exception(ex.Message);
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn.Dispose();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public static string HttpPost(string url, string body)
{
try
{
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
request.ContentType = "application/json; charset=utf-8";
byte[] buffer = encoding.GetBytes(body);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
return reader.ReadToEnd();
}
}
catch (WebException ex)
{
throw new Exception(ex.Message);
}
}
}
}