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
{
    /// <summary>
    /// 使用中
    /// 采购模块
    /// </summary>
    public class ICSPurchaseService
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #region 采购到货
        /// <summary>
        /// 采购到货接口
        /// </summary>
        /// <param name="TransType"></param>
        /// <param name="Identification"></param>
        /// <param name="cmd"></param>
        public static void DeliveryNoticeERP(string DNCode, string WorkPoint, SqlCommand cmd, Dictionary<string, string> language)
        {
            try
            {
                #region ERP
                string sql = @"SELECT a.VenCode+a.DepCode+y.POCode+a.MUSER AS Costre,a.VenCode,a.DepCode,y.POCode,a.MUSER,a.MTIME,
                                a.Sequence,a.InvCode,a.Quantity,a.Amount,ISNULL(a.UnitPrice,0) AS UnitPrice,a.Currency,a.PODetailID 
	                            ,a.WorkPoint,ISNULL(ext.ProjectCode, '') AS ProjectCode,CASE WHEN 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 ICSDeliveryNotice a 
                                INNER JOIN ICSExtension ext ON a.ExtensionID=ext.ID AND a.WorkPoint=ext.WorkPoint
                                LEFT JOIN ICSInventory invBat ON a.InvCode=invBat.InvCode AND a.WorkPoint=invBat.WorkPoint
                                INNER JOIN ICSPurchaseOrder y ON a.PODetailID=y.PODetailID AND a.WorkPoint=y.WorkPoint
                                WHERE a.DNCode='{0}' AND  a.WorkPoint='{1}'

                                SELECT DISTINCT Costre,WorkPoint,VenCode,DepCode,POCode,MUSER AS [User],SYSDATETIME() AS MTime FROM #TempERP
                                SELECT Costre,Sequence,InvCode,Quantity,Amount,UnitPrice,Currency,PODetailID,ProjectCode,BatchCode,Version,Brand,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10
                                FROM #TempERP

                                DROP TABLE #TempERP";
                sql = string.Format(sql, DNCode, WorkPoint);
                DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);
                
                string Inputstr = DataToJsonHelper.DataSetToJson(ds, "details", "Costre");
                string resultStr = HTTPHelper.HttpPost("采购到货", ERPUrl.CreatePOArrivURL, Inputstr);
                Result result = new Result();
                result = JsonConvert.DeserializeObject<Result>(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 ERPupdate = @"update ICSDeliveryNotice set DNCode='{0}',Sequence='{1}',DNID='{2}',DNDetailID='{3}'
                                        where PODetailID='{4}' AND DNType='1'  AND DNCode='{5}' ";
                                ERPupdate = string.Format(ERPupdate, jo["DNCode"], det["Sequence"], jo["ID"], det["DetailID"], det["PODetailID"],DNCode);

                                if (!DBHelper.ExecuteNonQuery(ERPupdate, cmd))
                                {
                                    throw new Exception(language.GetNameByCode("WMSAPIInfo079"));//"到货单更新失败!");
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Debug(ex.ToString());
                        log.Debug(resultStr);
                    }
                }
                else
                {
                    throw new Exception(language.GetNameByCode("WMSAPIInfo080")+result.Message);
                }
                #endregion
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion
        
        #region 采购入库
        /// <summary>
        /// 采购入库
        /// </summary>
        /// <param name="TransCode"></param>
        /// <param name="TransSequence"></param>
        /// <param name="Quantity"></param>
        /// <param name="WorkPoint"></param>
        /// <param name="cmd"></param>
        public static void PurchaseReceiveDoc(string LotNo, string Quantity, string WorkPoint, SqlCommand cmd, Dictionary<string, string> 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 ICSPurchaseOrder c ON b.TransCode=c.POCode AND b.TransSequence=c.Sequence AND b.WorkPoint=c.WorkPoint
                                WHERE a.LotNo='{0}' AND a.WorkPoint='{1}'

                                IF (@Status IS NULL)
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo081") + @"',16,1);
                                    RETURN
                                END
                                ELSE IF (@Status='3')
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo082") + @"',16,1);
                                    RETURN
                                END
                               UPDATE c SET InQuantity=ISNULL(InQuantity,0)+'{2}'
                                FROM ICSInventoryLot a
                                INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
                                INNER JOIN ICSPurchaseOrder c ON b.TransCode=c.POCode AND b.TransSequence=c.Sequence AND b.WorkPoint=c.WorkPoint
                                WHERE a.LotNo='{0}' AND a.WorkPoint='{1}'
                                IF EXISTS(SELECT a.LotNo FROM ICSInventoryLot a
                                    INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
                                    INNER JOIN ICSPurchaseOrder c ON b.TransCode=c.POCode AND b.TransSequence=c.Sequence AND b.WorkPoint=c.WorkPoint
                                    WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' AND c.Quantity<c.InQuantity)
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo083") + @"',16,1);
                                END";
                sql = string.Format(sql, LotNo, WorkPoint, Quantity);
                if (!DBHelper.ExecuteNonQuery(sql, cmd))
                {
                    throw new Exception(language.GetNameByCode("WMSAPIInfo104"));//"采购入库单更新失败!");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 采购入库接口
        /// </summary>
        /// <param name="TransType"></param>
        /// <param name="Identification"></param>
        /// <param name="cmd"></param>
        public static void PurchaseReceiveDocERP(string TransType, string Identification, SqlCommand cmd, Dictionary<string, string> language)
        {
            try
            {
                #region ERP
                string sql = @"SELECT c.VenCode+a.ToWarehouseCode+c.POCode+a.MUSER+f.DNCode AS Costre,c.VenCode,a.ToWarehouseCode AS WarehouseCode,c.POCode,f.DNCode,a.MUSER,f.Sequence AS DNSequence,ROW_NUMBER() OVER (ORDER BY c.VenCode,a.ToWarehouseCode,c.POCode,c.PODetailID,a.InvCode) AS Sequence,
                                    a.InvCode,SUM(a.Quantity) AS Quantity,SUM(a.Quantity*(lot.Amount/lot.Quantity)) AS Amount,isnull((c.UnitPrice),0) as UnitPrice,c.Currency,c.PODetailID,con.Enable AS UpdateTodoQuantity,conn.Enable
	                            ,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 ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
								INNER JOIN ICSASNDetail g ON a.LotNo=g.LotNo AND a.WorkPoint=g.WorkPoint
								INNER JOIN ICSDeliveryNotice f ON g.ASNCode=f.ASNCode AND g.WorkPoint=f.WorkPoint
                                INNER JOIN ICSPurchaseOrder c ON b.TransCode=c.POCode AND b.TransSequence=c.Sequence AND f.PODetailID=c.PODetailID AND b.WorkPoint=c.WorkPoint
                                INNER JOIN ICSConfiguration con ON con.Code='Stock001' AND a.WorkPoint=con.WorkPoint
                                INNER JOIN ICSConfiguration conn ON con.WorkPoint=conn.WorkPoint AND conn.Code='Escrow001'
                                WHERE a.Identification='{0}' AND  ERPUpload='0'
                                GROUP BY c.VenCode,a.ToWarehouseCode,c.POCode,f.DNCode,a.MUSER,a.InvCode,f.Sequence,lot.Amount,lot.Quantity,c.PODetailID,isnull((c.UnitPrice),0),c.Currency,con.Enable,conn.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,VenCode,WarehouseCode AS WHCode,POCode,DNCode,MUSER AS [User],SYSDATETIME() AS MTime,Enable,UpdateTodoQuantity FROM #TempERP
                                SELECT Costre,Sequence,DNSequence,InvCode,Quantity,Amount,PODetailID,Currency,UnitPrice,ProjectCode,BatchCode,Version,Brand,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10
                                FROM #TempERP

                                DROP TABLE #TempERP";
                sql = string.Format(sql, Identification);
                DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);

                string Inputstr = DataToJsonHelper.DataSetToJson(ds, "details", "Costre");
                string resultStr = HTTPHelper.HttpPost(TransType, ERPUrl.PurchaseOrderURL, Inputstr);
                Result result = new Result();
                result = JsonConvert.DeserializeObject<Result>(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;
                                ICSWareHouseLotInfoService.WareHouseLotInfoLogUpdate(TransType, det["PODetailID"].ToString(), Identification, jo["ID"].ToString(), 
                                    det["DetailID"].ToString(), jo["RCVTCode"].ToString(), det["Sequence"].ToString(), cmd, language);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Debug(ex.ToString());
                        log.Debug(resultStr);
                    }
                }
                else
                {
                    throw new Exception(language.GetNameByCode("WMSAPIInfo080")+result.Message);
                }
                #endregion
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region 审核的到货单
        /// <summary>
        /// 审核的到货单
        /// </summary>
        /// <param name="TransCode"></param>
        /// <param name="TransSequence"></param>
        /// <param name="Quantity"></param>
        /// <param name="WorkPoint"></param>
        /// <param name="cmd"></param>
        public static void DeliveryNoticeIn(string LotNo, string Quantity, string WorkPoint, SqlCommand cmd, Dictionary<string, string> 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 ICSDeliveryNotice c ON b.TransCode=c.DNCode AND b.TransSequence=c.Sequence AND b.WorkPoint=c.WorkPoint
                                WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' AND c.DNType='1'

                                IF (@Status IS NULL)
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo081") + @"',16,1);
                                    RETURN
                                END
                                ELSE IF (@Status='3')
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo082") + @"',16,1);
                                    RETURN
                                END
                               UPDATE c SET RCVQuantity=ISNULL(RCVQuantity,0)+'{2}'
                                FROM ICSInventoryLot a
                                INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
                                INNER JOIN ICSDeliveryNotice c ON b.TransCode=c.DNCode AND b.TransSequence=c.Sequence AND b.WorkPoint=c.WorkPoint
                                WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' AND c.DNType='1'
                                IF EXISTS(SELECT a.LotNo FROM ICSInventoryLot a
                                    INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
                                    INNER JOIN ICSDeliveryNotice c ON b.TransCode=c.DNCode AND b.TransSequence=c.Sequence AND b.WorkPoint=c.WorkPoint
                                    WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' AND c.DNType='1' AND c.Quantity<c.RCVQuantity)
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo083") + @"',16,1);
                                END";
                sql = string.Format(sql, LotNo, WorkPoint, Quantity);
                if (!DBHelper.ExecuteNonQuery(sql, cmd))
                {
                    throw new Exception(language.GetNameByCode("WMSAPIInfo079"));//"到货单更新失败!");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 审核的到货单接口
        /// </summary>
        /// <param name="TransType"></param>
        /// <param name="Identification"></param>
        /// <param name="cmd"></param>
        public static void DeliveryNoticeInERP(string TransType, string Identification, SqlCommand cmd, Dictionary<string, string> language)
        {
            try
            {
                #region ERP
                string sql = @"SELECT c.VenCode+a.ToWarehouseCode+c.DNCode+a.MUSER AS Costre,c.VenCode,a.ToWarehouseCode AS WarehouseCode,c.DNCode,a.MUSER,ROW_NUMBER() OVER (ORDER BY c.VenCode,a.ToWarehouseCode,c.DNCode,c.DNDetailID,a.InvCode) AS Sequence,
                                    a.InvCode,SUM(a.Quantity) AS Quantity,SUM(a.Quantity*(lot.Amount/lot.Quantity)) AS Amount,isnull((c.UnitPrice),0) as UnitPrice,c.Currency,c.DNDetailID,con.Enable AS UpdateTodoQuantity,conn.Enable
	                            ,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 ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
                                INNER JOIN ICSDeliveryNotice c ON b.TransCode=c.DNCode AND b.TransSequence=c.Sequence AND b.WorkPoint=c.WorkPoint
                                INNER JOIN ICSConfiguration con ON con.Code='Stock001' AND a.WorkPoint=con.WorkPoint
                                INNER JOIN ICSConfiguration conn ON con.WorkPoint=conn.WorkPoint AND conn.Code='Escrow001'
                                WHERE a.Identification='{0}' AND  ERPUpload='0'
                                GROUP BY c.VenCode,a.ToWarehouseCode,c.DNCode,a.MUSER,lot.Amount,lot.Quantity,a.InvCode,c.DNDetailID,isnull((c.UnitPrice),0),c.Currency,con.Enable,conn.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,VenCode,WarehouseCode AS WHCode,DNCode,MUSER AS [User],SYSDATETIME() AS MTime,Enable,UpdateTodoQuantity FROM #TempERP
                                SELECT Costre,Sequence,InvCode,Quantity,Amount,DNDetailID,Currency,UnitPrice,ProjectCode,BatchCode,Version,Brand,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10
                                FROM #TempERP

                                DROP TABLE #TempERP";
                sql = string.Format(sql, Identification);
                DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);

                string Inputstr = DataToJsonHelper.DataSetToJson(ds, "details", "Costre");
                string resultStr = HTTPHelper.HttpPost(TransType, ERPUrl.DeliveryNoticeURL, Inputstr);
                Result result = new Result();
                result = JsonConvert.DeserializeObject<Result>(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;
                                ICSWareHouseLotInfoService.WareHouseLotInfoLogUpdate(TransType, det["DNDetailID"].ToString(), Identification, jo["ID"].ToString(), 
                                    det["DetailID"].ToString(), jo["RCVTCode"].ToString(), det["Sequence"].ToString(), cmd, language);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Debug(ex.ToString());
                        log.Debug(resultStr);
                    }
                }
                else
                {
                    throw new Exception(language.GetNameByCode("WMSAPIInfo080")+result.Message);
                }
                #endregion
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region 采购拒收
        /// <summary>
        /// 采购拒收
        /// </summary>
        /// <param name="TransCode"></param>
        /// <param name="TransSequence"></param>
        /// <param name="Quantity"></param>
        /// <param name="WorkPoint"></param>
        /// <param name="cmd"></param>
        public static void PurchaseRejectDocIn(string LotNo, string Quantity, string WorkPoint, SqlCommand cmd, Dictionary<string, string> 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 ICSDeliveryNotice c ON b.TransCode=c.DNCode AND b.TransSequence=c.Sequence AND b.WorkPoint=c.WorkPoint
                                WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' AND c.DNType='3'

                                IF (@Status IS NULL)
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo081") + @"',16,1);
                                    RETURN
                                END
                                ELSE IF (@Status='3')
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo082") + @"',16,1);
                                    RETURN
                                END
                               UPDATE c SET RCVQuantity=ISNULL(RCVQuantity,0)+'{2}'
                                FROM ICSInventoryLot a
                                INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
                                INNER JOIN ICSDeliveryNotice c ON b.TransCode=c.DNCode AND b.TransSequence=c.Sequence AND b.WorkPoint=c.WorkPoint
                                WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' AND c.DNType='3'
                                IF EXISTS(SELECT a.LotNo FROM ICSInventoryLot a
                                    INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
                                    INNER JOIN ICSDeliveryNotice c ON b.TransCode=c.DNCode AND b.TransSequence=c.Sequence AND b.WorkPoint=c.WorkPoint
                                    WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' AND c.DNType='3' AND c.Quantity<c.RCVQuantity)
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo083") + @"',16,1);
                                END";
                sql = string.Format(sql, LotNo, WorkPoint, Quantity);
                if (!DBHelper.ExecuteNonQuery(sql, cmd))
                {
                    throw new Exception(language.GetNameByCode("WMSAPIInfo084"));//"拒收单更新失败!");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 采购拒收接口
        /// </summary>
        /// <param name="TransType"></param>
        /// <param name="Identification"></param>
        /// <param name="cmd"></param>
        public static void PurchaseRejectDocInNewERP(string DNCode, string JDNCode, string WorkPoint, SqlCommand cmd, Dictionary<string, string> language)
        {
            //try
            //{
            //    #region ERP
            //    string sql = @"SELECT c.VenCode+a.ToWarehouseCode+c.DNCode+a.MUSER AS Costre,c.VenCode,a.ToWarehouseCode AS WarehouseCode,d.DNCode,a.MUSER,ROW_NUMBER() OVER (ORDER BY c.VenCode,a.ToWarehouseCode,c.DNCode,c.DNDetailID,a.InvCode) AS Sequence,
            //                        a.InvCode,SUM(a.Quantity) AS Quantity,SUM(a.Quantity*(lot.Amount/lot.Quantity)) AS Amount,isnull((c.UnitPrice),0) as UnitPrice,c.Currency,d.DNDetailID,con.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,conv.Enable AS VerificationEnable    
            //                    INTO #TempERP
            //                    FROM ICSWareHouseLotInfoLog a
            //                        INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
            //						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 ICSDeliveryNotice c ON b.TransCode=c.DNCode AND b.TransSequence=c.Sequence AND b.WorkPoint=c.WorkPoint
            //                        INNER JOIN ICSDeliveryNotice d ON c.PODetailID=d.DNDetailID AND d.WorkPoint=c.WorkPoint
            //                        INNER JOIN ICSConfiguration con ON con.Code='Stock001' AND a.WorkPoint=con.WorkPoint
            //                        INNER JOIN ICSConfiguration conv ON con.Code='CompleteVerification' AND a.WorkPoint=conv.WorkPoint
            //                        WHERE a.Identification='{0}' AND  ERPUpload='0'
            //                        GROUP BY c.VenCode,a.ToWarehouseCode,c.DNCode,d.DNCode,a.MUSER,lot.Amount,lot.Quantity,a.InvCode,d.DNDetailID,c.DNDetailID,isnull((c.UnitPrice),0),c.Currency,con.Enable,conv.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,VenCode,WarehouseCode AS WHCode,DNCode,MUSER AS [User],SYSDATETIME() AS MTime,UpdateTodoQuantity,VerificationEnable,WorkPoint FROM #TempERP
            //                    SELECT Costre,Sequence,InvCode,Quantity,Amount,DNDetailID,Currency,UnitPrice,ProjectCode,BatchCode,Version,Brand,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10
            //                    FROM #TempERP

            //                    DROP TABLE #TempERP";
            //    sql = string.Format(sql, Identification);
            //    DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);

            //    string Inputstr = DataToJsonHelper.DataSetToJson(ds, "Vouchs", "Costre");
            //    string resultStr = HTTPHelper.HttpPost(TransType, ERPUrl.CreatePuArrivalVouchURL, Inputstr);
            //    Result result = new Result();
            //    result = JsonConvert.DeserializeObject<Result>(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;
            //                    ICSWareHouseLotInfoService.WareHouseLotInfoLogUpdate(TransType, det["DNDetailID"].ToString(), Identification, jo["ID"].ToString(), det["DetailID"].ToString(), jo["RCVTCode"].ToString(), det["Sequence"].ToString(), cmd, language);
            //                }
            //            }
            //        }
            //        catch (Exception ex)
            //        {
            //            log.Debug(ex.ToString());
            //            log.Debug(resultStr);
            //        }
            //    }
            //    else
            //    {
            //        throw new Exception(language.GetNameByCode("WMSAPIInfo080") + result.Message);
            //    }
            //    #endregion
            //}
            //catch (Exception)
            //{
            //    throw;
            //}
            try
            {
                #region ERP
                string sql = @"SELECT a.VenCode+a.DepCode+a.DNCode+a.MUSER AS Costre,a.VenCode,a.DepCode,a.DNCode,a.MUSER,a.MTIME,
                                a.Sequence,a.InvCode,Sum(x.Quantity) AS Quantity,a.Amount,ISNULL(a.UnitPrice,0) AS UnitPrice,a.Currency,a.DNDetailID 
	                            ,a.WorkPoint,ISNULL(ext.ProjectCode, '') AS ProjectCode,CASE WHEN 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 ICSDeliveryNotice a 
                                INNER JOIN ICSExtension ext ON a.ExtensionID=ext.ID AND a.WorkPoint=ext.WorkPoint
                                INNER JOIN ICSInventory invBat ON a.InvCode=invBat.InvCode AND a.WorkPoint=invBat.WorkPoint
								INNER JOIN ICSDeliveryNotice x ON a.DNDetailID=x.PODetailID  AND a.WorkPoint=x.WorkPoint
                                WHERE a.DNCode='{0}' AND  a.WorkPoint='{1}'
								GROUP BY a.VenCode,a.DepCode,a.DNCode,a.MUSER,a.MTIME,a.Sequence,a.InvCode,a.Amount,a.Quantity,a.UnitPrice,a.Currency,a.DNDetailID,
                                a.WorkPoint,ISNULL(ext.ProjectCode, ''),ISNULL(invBat.BatchEnable, ''),ISNULL(ext.BatchCode, ''),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,VenCode,DepCode,DNCode,MUSER AS [User],SYSDATETIME() AS MTime FROM #TempERP
                                SELECT Costre,Sequence,InvCode,Quantity,Amount,UnitPrice,Currency,DNDetailID,ProjectCode,BatchCode,Version,Brand,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10
                                FROM #TempERP

                                DROP TABLE #TempERP";
                sql = string.Format(sql, DNCode, WorkPoint);
                DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);

                string Inputstr = DataToJsonHelper.DataSetToJson(ds, "Vouchs", "Costre");
                string resultStr = HTTPHelper.HttpPost("采购拒收", ERPUrl.CreatePuArrivalVouchURL, Inputstr);
                Result result = new Result();
                result = JsonConvert.DeserializeObject<Result>(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;
                                //ICSWareHouseLotInfoService.WareHouseLotInfoLogUpdate(TransType, det["DNDetailID"].ToString(), Identification, jo["ID"].ToString(), det["DetailID"].ToString(), jo["RJTCode"].ToString(), det["Sequence"].ToString(), cmd, language);
                                string ERPupdate = @"update ICSDeliveryNotice set DNCode='{0}',Sequence='{1}',DNID='{2}',DNDetailID='{3}'
                                        where PODetailID='{4}' AND DNType='3'  AND DNCode='{5}' ";
                                ERPupdate = string.Format(ERPupdate, jo["RJTCode"], det["Sequence"], jo["ID"], det["DetailID"], det["DNDetailID"], JDNCode);

                                if (!DBHelper.ExecuteNonQuery(ERPupdate, cmd))
                                {
                                    throw new Exception(language.GetNameByCode("WMSAPIInfo079"));//"到货单更新失败!");
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Debug(ex.ToString());
                        log.Debug(resultStr);
                    }
                }
                else
                {
                    throw new Exception(language.GetNameByCode("WMSAPIInfo080") + result.Message);
                }
                #endregion
            }
            catch (Exception)
            {
                throw;
            }
        }


        /// <summary>
        /// 采购拒收接口
        /// </summary>
        /// <param name="TransType"></param>
        /// <param name="Identification"></param>
        /// <param name="cmd"></param>
        public static void PurchaseRejectDocInERP(string TransType, string Identification, SqlCommand cmd, Dictionary<string, string> language)
        {
            //try
            //{
            //    #region ERP
            //    string sql = @"SELECT c.VenCode+a.ToWarehouseCode+c.DNCode+a.MUSER AS Costre,c.VenCode,a.ToWarehouseCode AS WarehouseCode,d.DNCode,a.MUSER,ROW_NUMBER() OVER (ORDER BY c.VenCode,a.ToWarehouseCode,c.DNCode,c.DNDetailID,a.InvCode) AS Sequence,
            //                        a.InvCode,SUM(a.Quantity) AS Quantity,SUM(a.Quantity*(lot.Amount/lot.Quantity)) AS Amount,isnull((c.UnitPrice),0) as UnitPrice,c.Currency,d.DNDetailID,con.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,conv.Enable AS VerificationEnable    
            //                    INTO #TempERP
            //                    FROM ICSWareHouseLotInfoLog a
            //                        INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
            //						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 ICSDeliveryNotice c ON b.TransCode=c.DNCode AND b.TransSequence=c.Sequence AND b.WorkPoint=c.WorkPoint
            //                        INNER JOIN ICSDeliveryNotice d ON c.PODetailID=d.DNDetailID AND d.WorkPoint=c.WorkPoint
            //                        INNER JOIN ICSConfiguration con ON con.Code='Stock001' AND a.WorkPoint=con.WorkPoint
            //                        INNER JOIN ICSConfiguration conv ON con.Code='CompleteVerification' AND a.WorkPoint=conv.WorkPoint
            //                        WHERE a.Identification='{0}' AND  ERPUpload='0'
            //                        GROUP BY c.VenCode,a.ToWarehouseCode,c.DNCode,d.DNCode,a.MUSER,lot.Amount,lot.Quantity,a.InvCode,d.DNDetailID,c.DNDetailID,isnull((c.UnitPrice),0),c.Currency,con.Enable,conv.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,VenCode,WarehouseCode AS WHCode,DNCode,MUSER AS [User],SYSDATETIME() AS MTime,UpdateTodoQuantity,VerificationEnable,WorkPoint FROM #TempERP
            //                    SELECT Costre,Sequence,InvCode,Quantity,Amount,DNDetailID,Currency,UnitPrice,ProjectCode,BatchCode,Version,Brand,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10
            //                    FROM #TempERP

            //                    DROP TABLE #TempERP";
            //    sql = string.Format(sql, Identification);
            //    DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);

            //    string Inputstr = DataToJsonHelper.DataSetToJson(ds, "Vouchs", "Costre");
            //    string resultStr = HTTPHelper.HttpPost(TransType, ERPUrl.CreatePuArrivalVouchURL, Inputstr);
            //    Result result = new Result();
            //    result = JsonConvert.DeserializeObject<Result>(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;
            //                    ICSWareHouseLotInfoService.WareHouseLotInfoLogUpdate(TransType, det["DNDetailID"].ToString(), Identification, jo["ID"].ToString(), det["DetailID"].ToString(), jo["RCVTCode"].ToString(), det["Sequence"].ToString(), cmd, language);
            //                }
            //            }
            //        }
            //        catch (Exception ex)
            //        {
            //            log.Debug(ex.ToString());
            //            log.Debug(resultStr);
            //        }
            //    }
            //    else
            //    {
            //        throw new Exception(language.GetNameByCode("WMSAPIInfo080") + result.Message);
            //    }
            //    #endregion
            //}
            //catch (Exception)
            //{
            //    throw;
            //}

        }
        #endregion

        #region 采购退货
        /// <summary>
        /// 采购退货
        /// </summary>
        /// <param name="TransCode"></param>
        /// <param name="TransSequence"></param>
        /// <param name="Quantity"></param>
        /// <param name="WorkPoint"></param>
        /// <param name="cmd"></param>
        public static void PurchaseReceiveDoctNegative(string TransCode, string TransSequence, string Quantity, string WorkPoint, SqlCommand cmd, Dictionary<string, string> language)
        {
            try
            {
                string sql = @"DECLARE @Status VARCHAR(10)
                                SELECT @Status=Status FROM ICSDeliveryNotice WHERE DNCode='{0}' AND DNType='2'  AND WorkPoint='{1}'

                                IF (@Status IS NULL)
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo085") + @"',16,1);
                                    RETURN
                                END
                                ELSE IF (@Status!='2')
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo086") + @"',16,1);
                                    RETURN
                                END

                                UPDATE a SET RCVQuantity=ISNULL(RCVQuantity,0)+'{2}'
                                FROM ICSDeliveryNotice  a
                                WHERE a.DNCode='{0}' AND a.Sequence='{3}' AND a.WorkPoint='{1}' AND DNType='2'

                                IF EXISTS(SELECT a.ID FROM ICSDeliveryNotice  a
	                                WHERE a.DNCode='{0}' AND a.Sequence='{3}' and a.WorkPoint='{1}' AND a.Quantity<a.RCVQuantity AND DNType='2')
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo087") + @"',16,1);
                                    RETURN
                                END";

                sql = string.Format(sql, TransCode, WorkPoint, Quantity, TransSequence);

                if (!DBHelper.ExecuteNonQuery(sql, cmd))
                {
                    throw new Exception(language.GetNameByCode("WMSAPIInfo088"));//"采购退货单更新失败!");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 采购退货接口
        /// </summary>
        /// <param name="TransType"></param>
        /// <param name="Identification"></param>
        /// <param name="cmd"></param>
        public static void PurchaseReceiveDoctNegativeERP(string TransType, string Identification, SqlCommand cmd, Dictionary<string, string> language)
        {
            try
            {
                #region ERP
                string sql = @"SELECT y.VenCode+a.FromWarehouseCode+y.DNCode+a.MUSER AS Costre,y.VenCode,a.FromWarehouseCode AS WarehouseCode,y.DNCode,a.MUSER,ROW_NUMBER() OVER (ORDER BY y.VenCode,a.FromWarehouseCode,y.DNCode,y.DNDetailID,a.InvCode) AS Sequence,
                                a.InvCode,SUM(a.Quantity) AS Quantity,SUM(a.Quantity*(lot.Amount/lot.Quantity)) AS Amount,y.DNDetailID,y.Currency,ISNULL(y.UnitPrice, 0) AS UnitPrice,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.FromWarehouseCode=invBat.WHCode AND a.WorkPoint=invBat.WorkPoint
                                INNER JOIN ICSDeliveryNotice y ON a.TransCode=y.DNCode AND a.TransSequence=y.Sequence AND a.WorkPoint=y.WorkPoint
                                INNER JOIN ICSConfiguration con ON con.Code='Stock002' AND a.WorkPoint=con.WorkPoint
                                WHERE a.Identification='{0}' AND  ERPUpload='0' AND a.BusinessCode = '3'
                                GROUP BY y.VenCode,a.FromWarehouseCode,y.DNCode,a.MUSER,a.InvCode,lot.Amount,lot.Quantity,y.DNDetailID,y.Currency,ISNULL(y.UnitPrice, 0),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,VenCode,WarehouseCode AS WHCode,DNCode AS DNRTCode,MUSER AS [User],SYSDATETIME() AS MTime,UpdateTodoQuantity FROM #TempERP
                                SELECT Costre,Sequence,InvCode,Quantity,Amount,DNDetailID AS DNRTDetailID,Currency,UnitPrice,ProjectCode,BatchCode,Version,Brand,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10
                                FROM #TempERP

                                DROP TABLE #TempERP";
                sql = string.Format(sql, Identification);
                DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd);

                string Inputstr = DataToJsonHelper.DataSetToJson(ds, "details", "Costre");
                string resultStr = HTTPHelper.HttpPost(TransType, ERPUrl.PurchaseReturnBackURL, Inputstr);
                Result result = new Result();
                result = JsonConvert.DeserializeObject<Result>(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;
                                ICSWareHouseLotInfoService.WareHouseLotInfoLogUpdate(TransType, det["DNRTDetailID"].ToString(), Identification, jo["ID"].ToString(), 
                                    det["DetailID"].ToString(), jo["RCVNEGCode"].ToString(), det["Sequence"].ToString(), cmd, language);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Debug(ex.ToString());
                        log.Debug(resultStr);
                    }
                }
                else
                {
                    throw new Exception(language.GetNameByCode("WMSAPIInfo080")+result.Message);
                }
                #endregion
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region 开立红字入库
        /// <summary>
        /// 开立红字入库
        /// </summary>
        /// <param name="TransCode"></param>
        /// <param name="TransSequence"></param>
        /// <param name="Quantity"></param>
        /// <param name="WorkPoint"></param>
        /// <param name="cmd"></param>
        public static void PurchaseReceive(string TransCode, string TransSequence, string Quantity, string WorkPoint, SqlCommand cmd, Dictionary<string, string> language)
        {
            try
            {
                string sql = @"DECLARE @Status VARCHAR(10)
                                SELECT @Status=a.Status FROM ICSPurchaseReceive  a
                                WHERE a.RCVCode='{0}' AND a.Sequence='{3}' AND a.WorkPoint='{1}' AND a.Type='2'

                                IF (@Status IS NULL)
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo089") + @"',16,1);
                                    RETURN
                                END
                                ELSE IF (@Status!='1')
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo090") + @"',16,1);
                                    RETURN
                                END
                                UPDATE a SET RCVQuantity=ISNULL(RCVQuantity,0)+'{2}'
                                FROM ICSPurchaseReceive  a
                                WHERE a.RCVCode='{0}' AND a.Sequence='{3}' AND a.WorkPoint='{1}' AND a.Type='2'

                                IF EXISTS(SELECT a.ID FROM ICSPurchaseReceive  a
	                                WHERE a.RCVCode='{0}' AND a.Sequence='{3}' and a.WorkPoint='{1}' AND a.Type='2' AND a.Quantity<a.RCVQuantity)
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo091") + @"',16,1);
                                    RETURN
                                END";

                sql = string.Format(sql, TransCode, WorkPoint, Quantity, TransSequence);

                if (!DBHelper.ExecuteNonQuery(sql, cmd))
                {
                    throw new Exception(language.GetNameByCode("WMSAPIInfo092"));//"红字入库单更新失败!");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 开立红字入库接口
        /// </summary>
        /// <param name="TransType"></param>
        /// <param name="Identification"></param>
        /// <param name="cmd"></param>
        public static void PurchaseReceiveERP(string TransType, string Identification, SqlCommand cmd, Dictionary<string, string> language)
        {
            try
            {
                #region ERP开立状态单据审核
                string sql = @"IF EXISTS(SELECT b.ID FROM ICSWareHouseLotInfoLog a
                                    INNER JOIN ICSPurchaseReceive b ON a.TransCode=b.RCVCode AND a.TransSequence=b.Sequence AND a.WorkPoint=b.WorkPoint
                                    WHERE a.Identification='{0}' AND b.Quantity!=b.RCVQuantity)
                                BEGIN
	                                RAISERROR('" + language.GetNameByCode("WMSAPIInfo093") + @"',16,1);
                                    RETURN
                                END
                                SELECT b.RCVID AS ID,a.MUSER AS [User],SYSDATETIME() AS MTime,con.Enable AS UpdateTodoQuantity,conStock.Enable AS UpdateStock,a.WorkPoint
                                FROM ICSWareHouseLotInfoLog a
                                INNER JOIN ICSPurchaseReceive b ON a.TransCode=b.RCVCode AND a.TransSequence=b.Sequence AND a.WorkPoint=b.WorkPoint
                                INNER JOIN ICSConfiguration con ON con.Code='Stock002' AND a.WorkPoint=con.WorkPoint
                                INNER JOIN ICSConfiguration conStock ON conStock.Code='UpdateStock001' AND a.WorkPoint=conStock.WorkPoint
                                WHERE a.Identification='{0}' AND  ERPUpload='0' AND a.BusinessCode = '4' 
                                GROUP BY b.RCVID,a.MUSER,con.Enable,conStock.Enable,a.WorkPoint";
                sql = string.Format(sql, Identification);
                DataTable dt = DBHelper.SQlReturnData(sql, cmd);

                string Inputstr = JsonConvert.SerializeObject(dt);
                string resultStr = HTTPHelper.HttpPost(TransType, ERPUrl.PurchaseReceiveDoctNegativeURL, Inputstr);
                Result result = new Result();
                result = JsonConvert.DeserializeObject<Result>(resultStr);
                if (result.Success)
                {
                    try
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            ICSWareHouseLotInfoService.WareHouseLotInfoLogUpdate(TransType, dr["ID"].ToString(), Identification, "", "", "", "", cmd, language);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Debug(ex.ToString());
                        log.Debug(resultStr);
                    }
                }
                else
                {
                    throw new Exception(language.GetNameByCode("WMSAPIInfo080") + result.Message);
                }
                #endregion
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

    }
}