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.

436 lines
24 KiB

using ICSSoft.ERPWMS.Entity;
using Microsoft.Data.SqlClient;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ICSSoft.ERPWMS.SQL
{
public class CreateOrDeleteInventoryLot
{
public static Result Create(List<CreateInventoryLotEntity> entityList)
{
Result res = new Result();
try
{
string jsonstr = JsonConvert.SerializeObject(entityList);
Log.WriteLogFile(jsonstr, "创建删除条码日志");
StringBuilder sb = new StringBuilder();//接口返回Message
foreach (CreateInventoryLotEntity entity in entityList)
{
SqlConnection conn = new SqlConnection(ICSHelper.ReadConfig(ICSHelper.FileNameCompanyCon)["WMS"].ToString());
conn.Open();
SqlTransaction sqlTran = conn.BeginTransaction();
SqlCommand cmd = new SqlCommand();
cmd.Transaction = sqlTran;
cmd.Connection = conn;
try
{
if (string.IsNullOrWhiteSpace(entity.LotNo))
{
throw new Exception("存在物料条码为空!");
}
if (string.IsNullOrWhiteSpace(entity.InvCode))
{
throw new Exception("物料编码为空!");
}
if (string.IsNullOrWhiteSpace(entity.TransNo))
{
throw new Exception("来源单据号为空!");
}
if (entity.TransLine == null)
{
throw new Exception("来源单据行号为空!");
}
if (entity.Quantity == null)
{
throw new Exception("数量为空!");
}
if (string.IsNullOrWhiteSpace(entity.Type))
{
throw new Exception("类型为空!");
}
if (string.IsNullOrWhiteSpace(entity.WorkPoint))
{
throw new Exception("站点为空!");
}
if (string.IsNullOrWhiteSpace(entity.SRMLotGroup))
{
throw new Exception("SRM条码Group为空为空!");
}
string WorkPoint = ICSHelper.GetConnectStringTest(entity.WorkPoint);
if (WorkPoint == "NotExit")
{
throw new Exception("站点编码不存在!");
}
string sqlLot = "Select LotNo,ExtensionID from ICSInventoryLot where workpoint='{0}' and LotNo='{1}'";
sqlLot = string.Format(sqlLot, WorkPoint, entity.LotNo);
DataTable dtLot = ICSHelper.SQlReturnData(sqlLot, cmd);
if (dtLot != null && dtLot.Rows.Count > 0)
{
//检查条码是否绑定送货单,若绑定则不能删除
string sqlASN = "Select LotNo from ICSASNDetail Where workpoint='{0}' and LotNo ='{1}'";
sqlASN = string.Format(sqlASN, WorkPoint, entity.LotNo);
DataTable dtASN = ICSHelper.SQlReturnData(sqlASN, cmd);
if (dtASN != null && dtASN.Rows.Count > 0)
{
throw new Exception("条码已绑定送货单,不能删除!");
}
//删除条码表表头
string sqlLotDelete = "Delete ICSInventoryLot where workpoint='{0}' and LotNo='{1}'";
sqlLotDelete = string.Format(sqlLotDelete, WorkPoint, dtLot.Rows[0]["LotNo"].ToString());
Log.WriteLogFile(sqlLotDelete, "创建删除条码SQL日志");
if (!ICSHelper.ExecuteNonQuery(sqlLotDelete, cmd))
{
throw new Exception("删除条码表表头失败!");
}
//删除条码表表体
string sqlLotDetailDelete = "Delete ICSInventoryLotDetail where workpoint='{0}' and LotNo='{1}'";
sqlLotDetailDelete = string.Format(sqlLotDetailDelete, WorkPoint, dtLot.Rows[0]["LotNo"].ToString());
Log.WriteLogFile(sqlLotDetailDelete, "创建删除条码SQL日志");
if (!ICSHelper.ExecuteNonQuery(sqlLotDetailDelete, cmd))
{
throw new Exception("删除条码表表体失败!");
}
//删除自由项表
//string sqlCheck = "Select * from ICSInventoryLot where ExtensionID ='" + dtLot.Rows[0]["ExtensionID"].ToString() + "' and";
//DataTable dtCheck = ICSHelper.SQlReturnData(sqlCheck, cmd);
//if (dtCheck.Rows.Count < 1)
//{
// string sqlFreeDelete = "Delete ICSExtension where workpoint='{0}' and ID ='{1}'";
// sqlFreeDelete = string.Format(sqlFreeDelete, WorkPoint, dtLot.Rows[0]["ExtensionID"].ToString());
// Log.WriteLogFile(sqlFreeDelete, "创建删除条码SQL日志");
// if (!ICSHelper.ExecuteNonQuery(sqlFreeDelete, cmd))
// {
// throw new Exception("删除自由项表失败!");
// }
//}
#region 新增条码
string sqlInvCheck = "Select InvCode,BatchEnable from ICSInventory Where workpoint='{0}' and InvCode ='{1}'";
sqlInvCheck = string.Format(sqlInvCheck, WorkPoint, entity.InvCode);
DataTable dtInvCheck = ICSHelper.SQlReturnData(sqlInvCheck, cmd);
if (dtInvCheck.Rows.Count < 1)
{
throw new Exception("物料代码不存在!");
}
string BatchEnable = dtInvCheck.Rows[0]["BatchEnable"].ToString().ToUpper();
Log.WriteLogFile("是否启用批次:" + BatchEnable, "创建删除条码日志");
String EffectiveEnable = "";
String Colspan = "";
String IDD = "";
int EffectiveDays = 0;
String Time = "";
DateTime? dtt;
DateTime now = DateTime.Now;
string sql = @"select EffectiveEnable,EffectiveDays from ICSInventory
where InvCode='{0}' and WorkPoint='{1}'";
sql = string.Format(sql, entity.InvCode, WorkPoint);
DataTable dt = ICSHelper.SQlReturnData(sql, cmd);
if (dt.Rows.Count == 0)
{
throw new Exception("有效天数为空!");
}
else
{
EffectiveEnable = dt.Rows[0]["EffectiveEnable"].ToString();
EffectiveDays = Convert.ToInt32(dt.Rows[0]["EffectiveDays"]);
}
//EffectiveDayss = Convert.ToInt32(EffectiveDays);
if (!EffectiveEnable.Equals("False"))
{
Time = Convert.ToDateTime(entity.ProductDate).AddDays(EffectiveDays).ToString();
}
else
{
Time = "2999-12-31 00:00:00";
}
dtt = Convert.ToDateTime(Time);
if (entity.dDisableDate != null)
{
dtt = entity.dDisableDate;
}
//检验自由项
if (BatchEnable == "TRUE")
{
Colspan = "" + "~" + entity.BatchNo + "~" + ""
+ "~" + "" + "~" + entity.Extension1
+ "~" + entity.Extension2 + "~" + entity.Extension3 + "~" + entity.Extension4
+ "~" + entity.Extension5 + "~" + "" + "~" + ""
+ "~" + "" + "~" + "" + "~" + "";
}
else
{
Colspan = "" + "~" + "" + "~" + ""
+ "~" + "" + "~" + entity.Extension1
+ "~" + entity.Extension2 + "~" + entity.Extension3 + "~" + entity.Extension4
+ "~" + entity.Extension5 + "~" + "" + "~" + ""
+ "~" + "" + "~" + "" + "~" + "";
}
Log.WriteLogFile("生成的自由项:" + Colspan, "创建删除条码日志");
sql = @"select ID,Colspan from ICSExtension a
where Colspan='{0}' and WorkPoint='{1}'";
sql = string.Format(sql, Colspan, WorkPoint);
DataTable dttt = ICSHelper.SQlReturnData(sql, cmd);
if (dttt.Rows.Count == 0)
{
IDD = Guid.NewGuid().ToString();
if (BatchEnable == "TRUE")
{
sql = @"Insert into ICSExtension(ID, Colspan, ProjectCode, BatchCode, Version, Brand, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10, MTIME, MUSER, MUSERName, WorkPoint)
select '{18}','{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}',GETDATE(),'{15}','{16}','{17}'";
sql = string.Format(sql, Colspan, "", entity.BatchNo, "", "", entity.Extension1, entity.Extension2, entity.Extension3,
entity.Extension4, entity.Extension5, "", "", "", "", "", entity.MUSER, entity.MUSERName, WorkPoint, IDD);
}
else
{
sql = @"Insert into ICSExtension(ID, Colspan, ProjectCode, BatchCode, Version, Brand, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10, MTIME, MUSER, MUSERName, WorkPoint)
select '{18}','{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}',GETDATE(),'{15}','{16}','{17}'";
sql = string.Format(sql, Colspan, "", "", "", "", entity.Extension1, entity.Extension2, entity.Extension3,
entity.Extension4, entity.Extension5, "", "", "", "", "", entity.MUSER, entity.MUSERName, WorkPoint, IDD);
}
Log.WriteLogFile(sql, "创建删除条码SQL日志");
if (!ICSHelper.ExecuteNonQuery(sql, cmd))
{
throw new Exception("新增自由项失败!");
}
}
else
{
IDD = dttt.Rows[0]["ID"].ToString();
}
//存入条码表
sql = @"
INSERT INTO ICSInventoryLot(ID,LotNo,InvCode,ProductDate,ExpirationDate,
Quantity,Amount,ExtensionID,Type,PrintTimes,
LastPrintUser,LastPrintTime,MUSER,MUSERName,MTIME,
WorkPoint,EATTRIBUTE1)
SELECT TOP 1 NEWID(),'{0}','{1}','{2}','{3}',
'{4}','{5}','{6}','{7}','{8}',
'{9}','{10}','{11}','{12}','{13}',
'{14}','{15}'";
sql = string.Format(sql, entity.LotNo, entity.InvCode, entity.ProductDate, dtt, entity.Quantity, entity.AmountRate, IDD,
entity.Type, entity.PrintTimes, entity.LastPrintUser, entity.LastPrintTime, entity.MUSER,
entity.MUSERName, entity.MTIME, WorkPoint, entity.SRMLotGroup);
Log.WriteLogFile(sql, "创建删除条码SQL日志");
if (!ICSHelper.ExecuteNonQuery(sql, cmd))
{
throw new Exception("新增条码表表头失败!");
}
///存入条码关联表
sql = @"
INSERT INTO ICSInventoryLotDetail(LotNo,TransCode,TransSequence,MUSER,MUSERName,MTIME,
WorkPoint,EATTRIBUTE1)
SELECT '{0}','{1}','{2}','{3}','{4}','{5}','{6}',''
";
sql = string.Format(sql, entity.LotNo, entity.TransNo, entity.TransLine, entity.MUSER, entity.MUSERName, entity.MTIME, WorkPoint);
Log.WriteLogFile(sql, "创建删除条码SQL日志");
if (!ICSHelper.ExecuteNonQuery(sql, cmd))
{
throw new Exception("新增条码表表体失败!");
}
#endregion
}
else
{
string sqlInvCheck = "Select InvCode,BatchEnable from ICSInventory Where workpoint='{0}' and InvCode ='{1}'";
sqlInvCheck = string.Format(sqlInvCheck, WorkPoint, entity.InvCode);
DataTable dtInvCheck = ICSHelper.SQlReturnData(sqlInvCheck, cmd);
if (dtInvCheck.Rows.Count < 1)
{
throw new Exception("物料代码不存在!");
}
string BatchEnable = dtInvCheck.Rows[0]["BatchEnable"].ToString().ToUpper();
Log.WriteLogFile("是否启用批次:" + BatchEnable, "创建删除条码日志");
String EffectiveEnable = "";
String Colspan = "";
String IDD = "";
int EffectiveDays = 0;
String Time = "";
DateTime? dtt;
DateTime now = DateTime.Now;
string sql = @"select EffectiveEnable,EffectiveDays from ICSInventory
where InvCode='{0}' and WorkPoint='{1}'";
sql = string.Format(sql, entity.InvCode, WorkPoint);
DataTable dt = ICSHelper.SQlReturnData(sql, cmd);
if (dt.Rows.Count == 0)
{
throw new Exception("有效天数为空!");
}
else
{
EffectiveEnable = dt.Rows[0]["EffectiveEnable"].ToString();
EffectiveDays = Convert.ToInt32(dt.Rows[0]["EffectiveDays"]);
}
//EffectiveDayss = Convert.ToInt32(EffectiveDays);
if (!EffectiveEnable.Equals("False"))
{
Time = Convert.ToDateTime(entity.ProductDate).AddDays(EffectiveDays).ToString();
}
else
{
Time = "2999-12-31 00:00:00";
}
dtt = Convert.ToDateTime(Time);
if (entity.dDisableDate != null)
{
dtt = entity.dDisableDate;
}
//检验自由项
if (BatchEnable=="TRUE")
{
Colspan = "" + "~" + entity.BatchNo + "~" + ""
+ "~" + "" + "~" + entity.Extension1
+ "~" + entity.Extension2 + "~" + entity.Extension3 + "~" + entity.Extension4
+ "~" + entity.Extension5 + "~" + "" + "~" + ""
+ "~" + "" + "~" + "" + "~" + "";
}
else
{
Colspan = "" + "~" + "" + "~" + ""
+ "~" + "" + "~" + entity.Extension1
+ "~" + entity.Extension2 + "~" + entity.Extension3 + "~" + entity.Extension4
+ "~" + entity.Extension5 + "~" + "" + "~" + ""
+ "~" + "" + "~" + "" + "~" + "";
}
Log.WriteLogFile("生成的自由项:"+Colspan, "创建删除条码日志");
sql = @"select ID,Colspan from ICSExtension a
where Colspan='{0}' and WorkPoint='{1}'";
sql = string.Format(sql, Colspan, WorkPoint);
DataTable dttt = ICSHelper.SQlReturnData(sql, cmd);
if (dttt.Rows.Count == 0)
{
IDD = Guid.NewGuid().ToString();
if (BatchEnable == "TRUE")
{
sql = @"Insert into ICSExtension(ID, Colspan, ProjectCode, BatchCode, Version, Brand, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10, MTIME, MUSER, MUSERName, WorkPoint)
select '{18}','{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}',GETDATE(),'{15}','{16}','{17}'";
sql = string.Format(sql, Colspan, "", entity.BatchNo, "", "", entity.Extension1, entity.Extension2, entity.Extension3,
entity.Extension4, entity.Extension5, "", "", "", "", "", entity.MUSER, entity.MUSERName, WorkPoint, IDD);
}
else
{
sql = @"Insert into ICSExtension(ID, Colspan, ProjectCode, BatchCode, Version, Brand, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10, MTIME, MUSER, MUSERName, WorkPoint)
select '{18}','{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}',GETDATE(),'{15}','{16}','{17}'";
sql = string.Format(sql, Colspan, "", "", "", "", entity.Extension1, entity.Extension2, entity.Extension3,
entity.Extension4, entity.Extension5, "", "", "", "", "", entity.MUSER, entity.MUSERName, WorkPoint, IDD);
}
Log.WriteLogFile(sql, "创建删除条码SQL日志");
if (!ICSHelper.ExecuteNonQuery(sql, cmd))
{
throw new Exception("新增自由项失败!");
}
}
else
{
IDD = dttt.Rows[0]["ID"].ToString();
}
//存入条码表
sql = @"
INSERT INTO ICSInventoryLot(ID,LotNo,InvCode,ProductDate,ExpirationDate,
Quantity,Amount,ExtensionID,Type,PrintTimes,
LastPrintUser,LastPrintTime,MUSER,MUSERName,MTIME,
WorkPoint,EATTRIBUTE1)
SELECT TOP 1 NEWID(),'{0}','{1}','{2}','{3}',
'{4}','{5}','{6}','{7}','{8}',
'{9}','{10}','{11}','{12}','{13}',
'{14}','{15}'";
sql = string.Format(sql, entity.LotNo, entity.InvCode, entity.ProductDate, dtt, entity.Quantity, entity.AmountRate, IDD,
entity.Type, entity.PrintTimes, entity.LastPrintUser, entity.LastPrintTime, entity.MUSER,
entity.MUSERName, entity.MTIME, WorkPoint, entity.SRMLotGroup);
Log.WriteLogFile(sql, "创建删除条码SQL日志");
if (!ICSHelper.ExecuteNonQuery(sql, cmd))
{
throw new Exception("新增条码表表头失败!");
}
///存入条码关联表
sql = @"
INSERT INTO ICSInventoryLotDetail(LotNo,TransCode,TransSequence,MUSER,MUSERName,MTIME,
WorkPoint,EATTRIBUTE1)
SELECT '{0}','{1}','{2}','{3}','{4}','{5}','{6}',''
";
sql = string.Format(sql, entity.LotNo, entity.TransNo, entity.TransLine, entity.MUSER, entity.MUSERName, entity.MTIME, WorkPoint);
Log.WriteLogFile(sql, "创建删除条码SQL日志");
if (!ICSHelper.ExecuteNonQuery(sql, cmd))
{
throw new Exception("新增条码表表体失败!");
}
}
cmd.Transaction.Commit();
}
catch (Exception ex)
{
cmd.Transaction.Rollback();
sb.Append("执行报错,条码为:" + entity.LotNo + ",报错信息:" + ex.Message + "!!!");
continue;
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn.Dispose();
}
}
if (sb.Length > 0)
{
res.IsSuccess = false;
res.Message = sb.ToString();
}
else
{
res.IsSuccess = true;
res.Message = "执行成功!";
}
return res;
}
catch (Exception ex)
{
res.IsSuccess = false;
res.Message = ex.Message;
return res;
}
}
}
}