using ICSSoft.Common;
using ICSSoft.Entity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace ICSSoft.DataProject
{
///
/// 使用中
/// 销售模块
///
public class ICSSalesService
{
private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#region 销售出库
///
/// 销售出库
///
///
///
///
///
///
public static void SalesShipmentDoc(string TransCode, string TransSequence, string Quantity, string WorkPoint, SqlCommand cmd, Dictionary language)
{
try
{
string sql = @"DECLARE @Status VARCHAR(10)
SELECT @Status=a.Status FROM ICSSDN a
WHERE a.SDNCode='{0}' AND a.Sequence='{3}' AND a.WorkPoint='{1}' AND a.Type='1'
IF (@Status IS NULL)
BEGIN
RAISERROR('" + language.GetNameByCode("WMSAPIInfo139") + @"',16,1);
RETURN
END
--ELSE IF (@Status!='2')
--BEGIN
--RAISERROR('" + language.GetNameByCode("WMSAPIInfo140") + @"',16,1);
--RETURN
--END
UPDATE a SET SDNQuantity=ISNULL(SDNQuantity,0)+'{2}'
FROM ICSSDN a
WHERE a.SDNCode='{0}' AND a.Sequence='{3}' AND a.WorkPoint='{1}' AND a.Type='1'
IF EXISTS(SELECT a.ID FROM ICSSDN a
WHERE a.SDNCode='{0}' AND a.Sequence='{3}' and a.WorkPoint='{1}' AND a.Type='1' AND a.Quantity
/// 销售出库接口
///
///
///
///
public static void SalesShipmentDocERP(string TransType, string Identification, SqlCommand cmd, Dictionary language, string BusinessCode)
{
try
{
#region SAP
string IsSuccess = "";
string ErrorMessage = "";
string sql = @" select Distinct A.SDNCode,A.WorkPoint from ICSSDN A
LEFT JOIN ICSWareHouseLotInfoLog B ON B.TransCode=A.SDNCode AND B.WorkPoint=A.WorkPoint
where B.Identification='{0}' AND BusinessCode='19'";
sql = string.Format(sql, Identification);
DataTable dt = DBHelper.SQlReturnData(sql, cmd);
if (dt.Rows.Count > 1)
{
throw new Exception(language.GetNameByCode("WMSAPIInfo202"));//单次提交单据数量卡控(只允许一张单据)
}
for (int i = 0; i < dt.Rows.Count; i++)
{
string chksql = @"select SUM(Quantity) AS Quantity,SUM(SDNQuantity) AS SDNQuantity from ICSSDN
where SDNCode='{0}' and WorkPoint='{1}'
GROUP BY SDNCode,WorkPoint";
chksql = string.Format(chksql, dt.Rows[i]["SDNCode"].ToString(), dt.Rows[i]["WorkPoint"].ToString());
DataTable chkdt = DBHelper.SQlReturnData(chksql, cmd);
if (Convert.ToDecimal(chkdt.Rows[0]["Quantity"]) != Convert.ToDecimal(chkdt.Rows[0]["SDNQuantity"]))
{
throw new Exception(string.Format(language.GetNameByCode("WMSAPIInfo203"), chkdt.Rows[0]["Quantity"].ToString(), chkdt.Rows[0]["SDNQuantity"].ToString()));//单次提交数量卡控(必须整单提交)
}
SAPCallBackDispatchVPN.ZWMS_SK_DO_GZ Client = new SAPCallBackDispatchVPN.ZWMS_SK_DO_GZ();
SAPCallBackDispatchVPN.ZWMS_SK_DO_GZ1 Info = new SAPCallBackDispatchVPN.ZWMS_SK_DO_GZ1();
List headlist = new List();
SAPCallBackDispatchVPN.ZWMS_DO_HEAD head = new SAPCallBackDispatchVPN.ZWMS_DO_HEAD();
head.VBELN = dt.Rows[i]["SDNCode"].ToString();
headlist.Add(head);
Info.T_HEAD = headlist.ToArray();
List ItemList = new List();
Info.T_GERNR = new SAPCallBackDispatchVPN.ZWMS_DO_GERNR[1];
Info.T_RETURN = new SAPCallBackDispatchVPN.ZWMS_DO_RETURN[1];
sql = @" select A.SDNCode,A.Sequence,A.Quantity,A.SDNQuantity,B.BatchCode from ICSSDN A
LEFT JOIN ICSExtension B ON B.ID=A.ExtensionID AND B.WorkPoint=A.WorkPoint
WHERE SDNCode='{0}' AND A.WorkPoint='{1}'";
sql = string.Format(sql, dt.Rows[i]["SDNCode"].ToString(), dt.Rows[i]["WorkPoint"].ToString());
DataTable Sapdt = DBHelper.SQlReturnData(sql, cmd);
foreach (DataRow dr in Sapdt.Rows)
{
if (Convert.ToDecimal(dr["Quantity"].ToString()) == Convert.ToDecimal(dr["SDNQuantity"].ToString()))
{
SAPCallBackDispatchVPN.ZWMS_DO_ITEM Item = new SAPCallBackDispatchVPN.ZWMS_DO_ITEM();
Item.VBELN = dr["SDNCode"].ToString();
Item.POSNR = dr["Sequence"].ToString();
Item.LFIMG = System.Decimal.Round(Convert.ToDecimal(dr["Quantity"].ToString()), 3);
Item.SERAIL = dr["BatchCode"].ToString();
ItemList.Add(Item);
}
}
if (ItemList.Count > 0)
{
Info.T_ITEM = ItemList.ToArray();
SAPCallBackDispatchVPN.ZWMS_SK_DO_GZResponse result = new SAPCallBackDispatchVPN.ZWMS_SK_DO_GZResponse();
result = Client.CallZWMS_SK_DO_GZ(Info);
foreach (SAPCallBackDispatchVPN.ZWMS_DO_RETURN resultItem in result.T_RETURN)
{
if (resultItem.ZFLG == "N")
{
IsSuccess = "N";
ErrorMessage += resultItem.ZMESS + "/r/n";
}
}
}
}
if (IsSuccess == "N")
{
throw new Exception(ErrorMessage);
}
#endregion
}
catch (Exception)
{
throw;
}
}
#endregion
#region 销售退货
///
/// 销售退货
///
///
///
///
///
///
public static void SalesShipmentDocNegative(string LotNo, string Quantity, string WorkPoint, SqlCommand cmd, Dictionary language)
{
try
{
string sql = @"DECLARE @Status VARCHAR(10)
SELECT @Status=c.Status FROM ICSInventoryLot a
INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
INNER JOIN ICSSDN c ON b.TransCode=c.SDNCode AND b.TransSequence=c.Sequence AND b.WorkPoint=c.WorkPoint
WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' AND c.Type='2'
IF (@Status IS NULL)
BEGIN
RAISERROR('" + language.GetNameByCode("WMSAPIInfo081") + @"',16,1);
RETURN
END
--ELSE IF (@Status!='2')
--BEGIN
-- RAISERROR('" + language.GetNameByCode("WMSAPIInfo142") + @"',16,1);
-- RETURN
--END
UPDATE c SET SDNQuantity=ISNULL(SDNQuantity,0)+'{2}'
FROM ICSInventoryLot a
INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
INNER JOIN ICSSDN c ON b.TransCode=c.SDNCode AND b.TransSequence=c.Sequence AND b.WorkPoint=c.WorkPoint
WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' AND c.Type='2'
IF EXISTS(SELECT a.LotNo FROM ICSInventoryLot a
INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
INNER JOIN ICSSDN c ON b.TransCode=c.SDNCode AND b.TransSequence=c.Sequence AND b.WorkPoint=c.WorkPoint
WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' AND c.Type='2' AND c.Quantity
/// 销售退货接口
///
///
///
///
public static void SalesShipmentDocNegativeERP(string TransType, string Identification, SqlCommand cmd, Dictionary language, string BusinessCode)
{
try
{
#region SAP
string IsSuccess = "";
string ErrorMessage = "";
string sql = @" select Distinct A.SDNCode,A.WorkPoint from ICSSDN A
LEFT JOIN ICSWareHouseLotInfoLog B ON B.TransCode=A.SDNCode AND B.WorkPoint=A.WorkPoint
where B.Identification='{0}' AND BusinessCode='18'";
sql = string.Format(sql, Identification);
DataTable dt = DBHelper.SQlReturnData(sql, cmd);
if (dt.Rows.Count > 1)
{
throw new Exception(language.GetNameByCode("WMSAPIInfo202"));//单次提交单据数量卡控(只允许一张单据)
}
for (int i = 0; i < dt.Rows.Count; i++)
{
string chksql = @"select SUM(Quantity) AS Quantity,SUM(SDNQuantity) AS SDNQuantity from ICSSDN
where SDNCode='{0}' and WorkPoint='{1}'
GROUP BY SDNCode,WorkPoint";
chksql = string.Format(chksql, dt.Rows[i]["SDNCode"].ToString(), dt.Rows[i]["WorkPoint"].ToString());
DataTable chkdt = DBHelper.SQlReturnData(chksql, cmd);
if (Convert.ToDecimal(chkdt.Rows[0]["Quantity"]) != Convert.ToDecimal(chkdt.Rows[0]["SDNQuantity"]))
{
throw new Exception(string.Format(language.GetNameByCode("WMSAPIInfo203"), chkdt.Rows[0]["Quantity"].ToString(), chkdt.Rows[0]["SDNQuantity"].ToString()));//单次提交数量卡控(必须整单提交)
}
SAPCallBackDispatchVPN.ZWMS_SK_DO_GZ Client = new SAPCallBackDispatchVPN.ZWMS_SK_DO_GZ();
SAPCallBackDispatchVPN.ZWMS_SK_DO_GZ1 Info = new SAPCallBackDispatchVPN.ZWMS_SK_DO_GZ1();
List headlist = new List();
SAPCallBackDispatchVPN.ZWMS_DO_HEAD head = new SAPCallBackDispatchVPN.ZWMS_DO_HEAD();
head.VBELN = dt.Rows[i]["SDNCode"].ToString();
headlist.Add(head);
Info.T_HEAD = headlist.ToArray();
List ItemList = new List();
Info.T_GERNR = new SAPCallBackDispatchVPN.ZWMS_DO_GERNR[1];
Info.T_RETURN = new SAPCallBackDispatchVPN.ZWMS_DO_RETURN[1];
sql = @" select A.SDNCode,A.Sequence,A.Quantity,A.SDNQuantity,B.BatchCode from ICSSDN A
LEFT JOIN ICSExtension B ON B.ID=A.ExtensionID AND B.WorkPoint=A.WorkPoint
WHERE SDNCode='{0}' AND A.WorkPoint='{1}'";
sql = string.Format(sql, dt.Rows[i]["SDNCode"].ToString(), dt.Rows[i]["WorkPoint"].ToString());
DataTable Sapdt = DBHelper.SQlReturnData(sql, cmd);
foreach (DataRow dr in Sapdt.Rows)
{
if (Convert.ToDecimal(dr["Quantity"].ToString()) == Convert.ToDecimal(dr["SDNQuantity"].ToString()))
{
SAPCallBackDispatchVPN.ZWMS_DO_ITEM Item = new SAPCallBackDispatchVPN.ZWMS_DO_ITEM();
Item.VBELN = dr["SDNCode"].ToString();
Item.POSNR = dr["Sequence"].ToString();
Item.LFIMG = System.Decimal.Round(Convert.ToDecimal(dr["Quantity"].ToString()), 3);
Item.SERAIL = dr["BatchCode"].ToString();
ItemList.Add(Item);
}
}
if (ItemList.Count > 0)
{
Info.T_ITEM = ItemList.ToArray();
SAPCallBackDispatchVPN.ZWMS_SK_DO_GZResponse result = new SAPCallBackDispatchVPN.ZWMS_SK_DO_GZResponse();
result = Client.CallZWMS_SK_DO_GZ(Info);
foreach (SAPCallBackDispatchVPN.ZWMS_DO_RETURN resultItem in result.T_RETURN)
{
if (resultItem.ZFLG == "N")
{
IsSuccess = "N";
ErrorMessage += resultItem.ZMESS + "/r/n";
}
}
}
}
if (IsSuccess == "N")
{
throw new Exception(ErrorMessage);
}
#endregion
}
catch (Exception)
{
throw;
}
}
#endregion
#region 销售退货-原条码
///
/// 销售退货-原条码
///
///
///
///
///
///
public static void SalesReturnBackIn(string TransCode, string TransSequence, string LotNo, string Quantity, string WorkPoint, SqlCommand cmd, Dictionary language)
{
try
{
string sql = @"DECLARE @Status VARCHAR(10)
SELECT @Status=sdn.Status FROM ICSSDN sdn
INNER JOIN ICSWareHouseLotInfoLog log ON sdn.WorkPoint=log.WorkPoint
INNER JOIN ICSWareHouseLotInfo a ON a.LotNo=log.LotNo AND a.WorkPoint=log.WorkPoint
WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' AND sdn.SDNCode='{2}' AND sdn.Sequence='{3}' AND sdn.Type='2'
IF (@Status IS NULL)
BEGIN
RAISERROR('" + language.GetNameByCode("WMSAPIInfo081") + @"',16,1);
RETURN
END
ELSE IF (@Status!='2')
BEGIN
RAISERROR('" + language.GetNameByCode("WMSAPIInfo142") + @"',16,1);
RETURN
END
UPDATE sdn SET SDNQuantity=ISNULL(SDNQuantity,0)+'{4}'
FROM ICSSDN sdn
INNER JOIN ICSWareHouseLotInfoLog log ON sdn.WorkPoint=log.WorkPoint
INNER JOIN ICSWareHouseLotInfo a ON a.LotNo=log.LotNo AND a.WorkPoint=log.WorkPoint
WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' AND sdn.SDNCode='{2}' AND sdn.Sequence='{3}' AND sdn.Type='2'
IF EXISTS(SELECT a.LotNo FROM ICSSDN sdn
INNER JOIN ICSWareHouseLotInfoLog log ON sdn.WorkPoint=log.WorkPoint
INNER JOIN ICSWareHouseLotInfo a ON a.LotNo=log.LotNo AND a.WorkPoint=log.WorkPoint
WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' AND sdn.SDNCode='{2}' AND sdn.Sequence='{3}' AND sdn.Type='2' AND sdn.Quantity
/// 销售退货-原条码接口
///
///
///
///
public static void SalesReturnBackInERP(string TransType, string Identification, SqlCommand cmd, Dictionary language, string BusinessCode)
{
try
{
#region ERP
string sql = @"SELECT c.CusCode+a.ToWarehouseCode+c.SDNCode+a.MUSER AS Costre,c.CusCode,a.ToWarehouseCode AS WarehouseCode,c.SDNCode,a.MUSER,ROW_NUMBER() OVER (ORDER BY c.CusCode,a.ToWarehouseCode,c.SDNCode,c.SDNDetailID,a.InvCode) AS Sequence,
a.InvCode,SUM(a.Quantity) AS Quantity,SUM(a.Quantity*(lot.Amount/lot.Quantity)) AS Amount,c.SDNDetailID,Enable AS UpdateTodoQuantity
,a.WorkPoint,ISNULL(ext.ProjectCode, '') AS ProjectCode,CASE WHEN (invBat.BatchEnable IS NULL AND ISNULL(inv.BatchEnable, '0')='1') OR ISNULL(invBat.BatchEnable, '0')='1' THEN ISNULL(ext.BatchCode, '') ELSE '' END AS BatchCode,ISNULL(ext.Version, '') AS Version,ISNULL(ext.Brand, '') AS Brand,
ISNULL(ext.cFree1, '') AS cFree1,ISNULL(ext.cFree2, '') AS cFree2,ISNULL(ext.cFree3, '') AS cFree3,ISNULL(ext.cFree4, '') AS cFree4,ISNULL(ext.cFree5, '') AS cFree5,ISNULL(ext.cFree6, '') AS cFree6,ISNULL(ext.cFree7, '') AS cFree7,ISNULL(ext.cFree8, '') AS cFree8,ISNULL(ext.cFree9, '') AS cFree9,ISNULL(ext.cFree10, '') AS cFree10
INTO #TempERP
FROM ICSWareHouseLotInfoLog a
INNER JOIN ICSInventoryLot lot ON a.LotNo=lot.LotNo AND a.WorkPoint=lot.WorkPoint
INNER JOIN ICSExtension ext ON lot.ExtensionID=ext.ID AND lot.WorkPoint=ext.WorkPoint
INNER JOIN ICSInventory inv ON a.InvCode=inv.InvCode AND a.WorkPoint=inv.WorkPoint
LEFT JOIN ICSInventoryBatchEnable invBat ON a.InvCode=invBat.InvCode AND a.ToWarehouseCode=invBat.WHCode AND a.WorkPoint=invBat.WorkPoint
INNER JOIN ICSSDN c ON a.TransCode=c.SDNCode AND a.TransSequence=c.Sequence AND a.WorkPoint=c.WorkPoint
INNER JOIN ICSConfiguration con ON con.Code='Stock001' AND a.WorkPoint=con.WorkPoint
WHERE a.Identification='{0}' AND ERPUpload='0' AND c.Type='2' AND BusinessCode='{1}'
GROUP BY c.CusCode,a.ToWarehouseCode,c.SDNCode,a.MUSER,a.InvCode,c.SDNDetailID,Enable
,a.WorkPoint,ISNULL(ext.ProjectCode, ''),CASE WHEN (invBat.BatchEnable IS NULL AND ISNULL(inv.BatchEnable, '0')='1') OR ISNULL(invBat.BatchEnable, '0')='1' THEN ISNULL(ext.BatchCode, '') ELSE '' END,ISNULL(ext.Version, ''),ISNULL(ext.Brand, ''),
ISNULL(ext.cFree1, ''),ISNULL(ext.cFree2, ''),ISNULL(ext.cFree3, ''),ISNULL(ext.cFree4, ''),ISNULL(ext.cFree5, ''),ISNULL(ext.cFree6, ''),ISNULL(ext.cFree7, ''),ISNULL(ext.cFree8, ''),ISNULL(ext.cFree9, ''),ISNULL(ext.cFree10, '')
SELECT DISTINCT Costre,WorkPoint,CusCode,WarehouseCode AS WHCode,SDNCode AS SDNRTCode,MUSER AS [User],SYSDATETIME() AS MTime,UpdateTodoQuantity FROM #TempERP
SELECT Costre,Sequence,InvCode,Quantity,Amount,SDNDetailID AS SDNRTDetailID,ProjectCode,BatchCode,Version,Brand,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 FROM #TempERP
DROP TABLE #TempERP";
sql = string.Format(sql, Identification, BusinessCode);
DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
string Inputstr = DataToJsonHelper.DataSetToJson(ds, "details", "Costre");
string resultStr = HTTPHelper.HttpPost(TransType, ERPUrl.SalesReturnBackURL, Inputstr);
Result result = new Result();
result = JsonConvert.DeserializeObject(resultStr);
if (result.Success)
{
try
{
JArray res = (JArray)JsonConvert.DeserializeObject(result.Data.ToString());
foreach (var item in res)
{
JObject jo = (JObject)item;
JArray resdetail = (JArray)JsonConvert.DeserializeObject(jo["details"].ToString());
foreach (var detail in resdetail)
{
JObject det = (JObject)detail;
string allcol = jo["cWhCode"].ToString() + det["ProjectCode"].ToString() + det["cBatch"].ToString() + det["version"].ToString() + det["brand"].ToString() + det["cFree1"].ToString() + det["cFree2"].ToString() + det["cFree3"].ToString() + det["cFree4"].ToString() + det["cFree5"].ToString()
+ det["cFree6"].ToString() + det["cFree7"].ToString() + det["cFree8"].ToString() + det["cFree9"].ToString() + det["cFree10"].ToString();
ICSWareHouseLotInfoService.WareHouseLotInfoLogUpdate(TransType, det["SDNRTDetailID"].ToString(), Identification, jo["ID"].ToString(),
det["DetailID"].ToString(), jo["SDNNEGCode"].ToString(), det["Sequence"].ToString(), allcol, cmd, language, BusinessCode);
}
}
}
catch (Exception ex)
{
log.Debug(ex.ToString());
log.Debug(resultStr);
}
}
else
{
throw new Exception(language.GetNameByCode("WMSAPIInfo080") + result.Message);
}
#endregion
}
catch (Exception)
{
throw;
}
}
#endregion
}
}