You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

495 lines
24 KiB
C#

using System.Linq;
using System.Text;
using System.Web.Services.Protocols;
using System.IO;
using NetLibrary.Log;
using NetLibrary.Data;
using System.Data.Common;
using System.Data;
using TradeManageNew.WebReference2;
using System.Net;
using System;
namespace TradeManageNew
{
public class FedexRateNewHWC2
{
public static string Key = "";
public static string Password = "";
public static string AccountNumber = "";
public static string MeterNumber = "";
public static string PayAccountNumber = "";
public static string PostInfo = "";
public static FedexFeeMdNew ormd = new FedexFeeMdNew();
public static string FeeHtml = "";
public static string HubId = "";
public static int PostId = 0;
public static decimal Fee = 0;
public static decimal AddFeeRate = 0;//加价
public static int Addr2 = 0;
public string GetFee(FedexFeeMdNew md, out string OutError)
{
ormd = md;
FeeHtml = "";
Fee = 0;
string error = "";
//ShipmentPackage shipmentPackage = new ShipmentPackage();
//shipmentPackage.DeveloperKey = Key;
//shipmentPackage.DeveloperPassword = Password;
//shipmentPackage.DeveloperAccountNumber = AccountNumber;
//shipmentPackage.DeveloperMeterNumber = MeterNumber;
////发货地
//shipmentPackage.OriginCity = md.SendCity;// "COMMERCE";
//shipmentPackage.OriginStateProvince = md.SendProv;// "CA";
//shipmentPackage.OriginPostalCode = md.SendPostCode;// "90040";
//shipmentPackage.OriginCountryCode = md.SendCountry;// "US";
////目的地
//shipmentPackage.DestinationCity = md.RevCity;// "New York";
//shipmentPackage.DestinationStateProvince = md.RevProv;// "NY";
//shipmentPackage.DestinationPostalCode = md.RevPostCode;// "10021";
//shipmentPackage.DestinationCountryCode = md.RevCountry;// "US";
//if (md.WeightKg != null && md.WeightKg.Value > 0)
// shipmentPackage.WeightUnits = RateAvailableServiceWebServiceClient.RateServiceWebReference.WeightUnits.KG;
//else
// shipmentPackage.WeightUnits = RateAvailableServiceWebServiceClient.RateServiceWebReference.WeightUnits.LB;
//if (md.WeightKg != null && md.WeightKg.Value > 0)
// shipmentPackage.Weight = md.WeightKg.Value;
//else
// shipmentPackage.Weight = md.Weight.Value * Convert.ToDecimal(0.0625);
RateRequest request = CreateRateRequest();
//
RateService service = new RateService();
//if (usePropertyFile())
//{
// service.Url = getProperty("endpoint");
//}
service.Url = "https://ws.fedex.com:443/web-services/rate";
OutError = "";
try
{
// Call the web service passing in a RateRequest and returning a RateReply
// ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
RateReply reply = service.getRates(request);
if (reply.HighestSeverity == NotificationSeverityType.SUCCESS || reply.HighestSeverity == NotificationSeverityType.NOTE || reply.HighestSeverity == NotificationSeverityType.WARNING)
{
ShowRateReply(reply);
}
else
OutError = ShowNotifications(reply);
}
catch (SoapException e)
{
OutError = e.Detail.InnerText;
// Console.WriteLine(e.Detail.InnerText);
}
catch (Exception e)
{
OutError = e.Message;
// Console.WriteLine(e.Message);
}
return FeeHtml + "@" + Fee;
}
private static RateRequest CreateRateRequest()
{
// Build the RateRequest
RateRequest request = new RateRequest();
//
request.WebAuthenticationDetail = new WebAuthenticationDetail();
request.WebAuthenticationDetail.UserCredential = new WebAuthenticationCredential();
request.WebAuthenticationDetail.UserCredential.Key = Key;// "JFnBk9SNRj1JeegZ"; // Replace "XXX" with the Key
request.WebAuthenticationDetail.UserCredential.Password = Password;// "KE6pVQBrPQWrcXEdv3Gtya6Zy"; // Replace "XXX" with the Password
//if (usePropertyFile()) //Set values from a file for testing purposes
//{
// request.WebAuthenticationDetail.UserCredential.Key = getProperty("key");
// request.WebAuthenticationDetail.UserCredential.Password = getProperty("password");
//}
//
request.ClientDetail = new ClientDetail();
request.ClientDetail.AccountNumber = AccountNumber;// "464127968"; // Replace "XXX" with the client's account number
request.ClientDetail.MeterNumber = MeterNumber;// "112179445"; // Replace "XXX" with the client's meter number
//if (usePropertyFile()) //Set values from a file for testing purposes
//{
// request.ClientDetail.AccountNumber = getProperty("accountnumber");
// request.ClientDetail.MeterNumber = getProperty("meternumber");
//}
//
request.TransactionDetail = new TransactionDetail();
request.TransactionDetail.CustomerTransactionId = "***SmartPost Rate Request using VC#***"; // This is a reference field for the customer. Any value can be used and will be provided in the response.
//
request.Version = new VersionId();
//
request.ReturnTransitAndCommit = true;
request.ReturnTransitAndCommitSpecified = true;
if (ormd.PostType == "SMART_POST")
{
request.CarrierCodes = new CarrierCodeType[1];
request.CarrierCodes[0] = CarrierCodeType.FXSP; // FXSP is for SmartPost
}
else
{
request.CarrierCodes = new CarrierCodeType[1];
request.CarrierCodes[0] = CarrierCodeType.FDXG; // FXSP is for SmartPost
}
//if (ormd.PostType == "GROUND_HOME_DELIVERY")
//{
// request.CarrierCodes = new CarrierCodeType[1];
// request.CarrierCodes[0] = CarrierCodeType.FDXG; // FXSP is for SmartPost
//}
//
SetShipmentDetails(request);
//
return request;
}
private static void SetShipmentDetails(RateRequest request)
{
request.RequestedShipment = new RequestedShipment();
request.RequestedShipment.ShipTimestamp = DateTime.Now; // Shipping date and time
request.RequestedShipment.ShipTimestampSpecified = true;
request.RequestedShipment.DropoffType = DropoffType.REGULAR_PICKUP;
if (ormd.PostType == "SMART_POST")
request.RequestedShipment.ServiceType = ServiceType.SMART_POST; // Service type is SmartPost
else if (ormd.PostType == "FEDEX_GROUND")
request.RequestedShipment.ServiceType = ServiceType.FEDEX_GROUND; // Service type is SmartPost
else if (ormd.PostType == "GROUND_HOME_DELIVERY")
request.RequestedShipment.ServiceType = ServiceType.GROUND_HOME_DELIVERY;
else if (ormd.PostType == "FEDEX_2_DAY")
request.RequestedShipment.ServiceType = ServiceType.FEDEX_2_DAY;
if (ormd.PostType == "GROUND_HOME_DELIVERY")
request.RequestedShipment.ServiceTypeSpecified = false;
else
request.RequestedShipment.ServiceTypeSpecified = true;
request.RequestedShipment.PackagingType = PackagingType.YOUR_PACKAGING;
request.RequestedShipment.PackagingTypeSpecified = true;
//
SetOrigin(request);
//
SetDestination(request);
SetSmartPostDetails(request);
//
SetPackageLineItems(request);
//
request.RequestedShipment.PackageCount = "1";
}
private static void SetOrigin(RateRequest request)
{
request.RequestedShipment.Shipper = new Party();
request.RequestedShipment.Shipper.Address = new Address();
request.RequestedShipment.Shipper.Address.StreetLines = new string[1] { ormd.SendAddr };
request.RequestedShipment.Shipper.Address.City = ormd.SendCity;// "COMMERCE";
request.RequestedShipment.Shipper.Address.StateOrProvinceCode = ormd.SendProv;// "CA";
request.RequestedShipment.Shipper.Address.PostalCode = ormd.SendPostCode;//"90040";
request.RequestedShipment.Shipper.Address.CountryCode = ormd.SendCountry;//"US";
}
private static void SetDestination(RateRequest request)
{
request.RequestedShipment.Recipient = new Party();
request.RequestedShipment.Recipient.Address = new Address();
request.RequestedShipment.Recipient.Address.StreetLines = new string[1] { ormd.RevAddr };
request.RequestedShipment.Recipient.Address.City = ormd.RevCity;// "Morgantown";
request.RequestedShipment.Recipient.Address.StateOrProvinceCode = ormd.RevProv;//"WV";
request.RequestedShipment.Recipient.Address.PostalCode = ormd.RevPostCode;//"26501";
request.RequestedShipment.Recipient.Address.CountryCode = ormd.RevCountry;// "US";
if (ormd.PostType == "GROUND_HOME_DELIVERY")
{
request.RequestedShipment.Recipient.Address.Residential = true;
request.RequestedShipment.Recipient.Address.ResidentialSpecified = true;
request.RequestedShipment.Recipient.Contact = new Contact();
if (ormd.RevName != null && ormd.RevName != "")
request.RequestedShipment.Recipient.Contact.PersonName = ormd.RevName;
else
request.RequestedShipment.Recipient.Contact.PersonName = "jacky";
if (ormd.RevPhone != null && ormd.RevPhone != "")
request.RequestedShipment.Recipient.Contact.PhoneNumber = ormd.RevPhone;
else
request.RequestedShipment.Recipient.Contact.PhoneNumber = "2392276058";
}
}
private static void SetSmartPostDetails(RateRequest request)
{
request.RequestedShipment.SmartPostDetail = new SmartPostShipmentDetail();
request.RequestedShipment.SmartPostDetail.Indicia = SmartPostIndiciaType.PARCEL_SELECT;
request.RequestedShipment.SmartPostDetail.IndiciaSpecified = true;
request.RequestedShipment.SmartPostDetail.AncillaryEndorsement = SmartPostAncillaryEndorsementType.ADDRESS_CORRECTION; // Replace with a valid ancillary endorsement type
request.RequestedShipment.SmartPostDetail.AncillaryEndorsementSpecified = true;
request.RequestedShipment.SmartPostDetail.HubId = HubId;// "5902"; // Replace "XXX" with the hub id
//if (usePropertyFile()) //Set values from a file for testing purposes
//{
// request.RequestedShipment.SmartPostDetail.HubId = getProperty("hubid");
//}
}
private static void SetPackageLineItems(RateRequest request)
{
request.RequestedShipment.RequestedPackageLineItems = new RequestedPackageLineItem[1];
request.RequestedShipment.RequestedPackageLineItems[0] = new RequestedPackageLineItem();
request.RequestedShipment.RequestedPackageLineItems[0].SequenceNumber = "1"; // package sequence number
request.RequestedShipment.RequestedPackageLineItems[0].GroupPackageCount = "1";
// package weight
request.RequestedShipment.RequestedPackageLineItems[0].Weight = new Weight();
request.RequestedShipment.RequestedPackageLineItems[0].Weight.Units = WeightUnits.LB;
request.RequestedShipment.RequestedPackageLineItems[0].Weight.UnitsSpecified = true;
if (ormd.OrderId <= 0)
request.RequestedShipment.RequestedPackageLineItems[0].Weight.Value = ormd.Weight.Value;
else
request.RequestedShipment.RequestedPackageLineItems[0].Weight.Value = ormd.Weight.Value * Convert.ToDecimal(0.0625);
request.RequestedShipment.RequestedPackageLineItems[0].Weight.ValueSpecified = true;
// package dimensions
request.RequestedShipment.RequestedPackageLineItems[0].Dimensions = new Dimensions();
request.RequestedShipment.RequestedPackageLineItems[0].Dimensions.Length = ormd.Lenght;// "50";
request.RequestedShipment.RequestedPackageLineItems[0].Dimensions.Width = ormd.Width;// "20";
request.RequestedShipment.RequestedPackageLineItems[0].Dimensions.Height = ormd.Height;// "3";
request.RequestedShipment.RequestedPackageLineItems[0].Dimensions.Units = LinearUnits.IN;
request.RequestedShipment.RequestedPackageLineItems[0].Dimensions.UnitsSpecified = true;
}
private static void ShowRateReply(RateReply reply)
{
//Console.WriteLine("RateReply details:");
foreach (RateReplyDetail rateReplyDetail in reply.RateReplyDetails)
{
if (rateReplyDetail.ServiceTypeSpecified)
FeeHtml += "Service Type:" + rateReplyDetail.ServiceType + "<br/>";
if (rateReplyDetail.PackagingTypeSpecified)
FeeHtml += "Packaging Type:" + rateReplyDetail.ServiceType + "<br/>";
// Console.WriteLine();
foreach (RatedShipmentDetail shipmentDetail in rateReplyDetail.RatedShipmentDetails)
{
ShowShipmentRateDetails(shipmentDetail);
// Console.WriteLine();
}
// ShowDeliveryDetails(rateReplyDetail);
// Console.WriteLine("**********************************************************");
}
}
private static void ShowShipmentRateDetails(RatedShipmentDetail shipmentDetail)
{
if (shipmentDetail == null) return;
if (shipmentDetail.ShipmentRateDetail == null) return;
ShipmentRateDetail rateDetail = shipmentDetail.ShipmentRateDetail;
// Console.WriteLine("--- Shipment Rate Detail ---");
//
//FeeHtml += "RateType:" + rateDetail.RateType;
//if (rateDetail.TotalBillingWeight != null) FeeHtml += "Total Billing Weight:" + rateDetail.TotalBillingWeight.Value.ToString() + shipmentDetail.ShipmentRateDetail.TotalBillingWeight.Units.ToString() + "<br/>";
//if (rateDetail.TotalBaseCharge != null) FeeHtml += "Total Base Charge:" + (rateDetail.TotalBaseCharge.Amount * (1 + AddFeeRate)).ToString() + rateDetail.TotalBaseCharge.Currency.ToString() + "<br/>";
//if (rateDetail.TotalFreightDiscounts != null) FeeHtml += "Total Freight Discounts:" + (rateDetail.TotalFreightDiscounts.Amount * (1 + AddFeeRate)).ToString() + rateDetail.TotalFreightDiscounts.Currency.ToString() + "<br/>";
//if (rateDetail.TotalSurcharges != null) FeeHtml += "Total Surcharges:" + (rateDetail.TotalSurcharges.Amount * (1 + AddFeeRate)).ToString() + rateDetail.TotalSurcharges.Currency.ToString() + "<br/>";
//if (rateDetail.Surcharges != null)
//{
// // Individual surcharge for each package
// foreach (Surcharge surcharge in rateDetail.Surcharges)
// FeeHtml += surcharge.SurchargeType + " surcharge:" + surcharge.Amount.Amount * (1 + AddFeeRate) + surcharge.Amount.Currency + "<br/>";
//}
if (rateDetail.TotalNetCharge != null)
{
if (Fee > 0 && rateDetail.TotalNetCharge.Amount > Fee)
{
Fee = 0; //多个费用
}
else
{
Fee = rateDetail.TotalNetCharge.Amount * (1 + AddFeeRate);
FeeHtml = "Total Net Charge:" + rateDetail.TotalNetCharge.Amount * (1 + AddFeeRate) + rateDetail.TotalNetCharge.Currency;
// UpdateFee();
}
}
}
#region 修改fee
public static void UpdateFee()
{
if (ormd.OrderGoodsId == null)
ormd.OrderGoodsId = 0;
try
{
if (ormd.OrderId != null && ormd.OrderId > 0 && ormd.OrderGoodsId != null && ormd.OrderGoodsId > 0)
{
string tsql = @"
Update [DT_SpareFee] set [Fee11]=@Fee where Id=@Id
";
if (ormd.PostType == "FEDEX_GROUND")
tsql = @"
Update [DT_SpareFee] set [Fee22]=@Fee where Id=@Id";
if (ormd.PostType == "GROUND_HOME_DELIVERY")
tsql = @"
Update [DT_SpareFee] set [Fee33]=@Fee where Id=@Id";
Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetSqlStringCommand(tsql);
db.AddInParameter(cmd, "@Id", DbType.Int32, ormd.OrderGoodsId);
db.AddInParameter(cmd, "@Fee", DbType.Decimal, Fee);
db.AddInParameter(cmd, "@FeeHtml", DbType.String, FeeHtml);
db.ExecuteNonQuery(cmd);
}
else if (ormd.OrderId == 0 && ormd.OrderGoodsId > 0)
{
string tsql = @"
if(select count(0) from HW_PostFee where GoodsId=@GoodsId)=0
begin
INSERT INTO [HW_PostFee]([GoodsId])VALUES(@GoodsId)
end
";
if (PostId == 237)
{
tsql += @"
Update [HW_PostFee] set Fee5=@Fee where GoodsId=@GoodsId
";
if (ormd.PostType == "FEDEX_GROUND")
tsql += @"
Update [HW_PostFee] set Fee6=@Fee where GoodsId=@GoodsId";
if (ormd.PostType == "GROUND_HOME_DELIVERY")
tsql += @"
Update [HW_PostFee] set Fee7=@Fee where GoodsId=@GoodsId";
if (ormd.PostType == "FEDEX_2_DAY")
tsql += @"
Update [HW_PostFee] set Fee8=@Fee where GoodsId=@GoodsId";
}
else
{
tsql += @"
Update [HW_PostFee] set Fee1=@Fee where GoodsId=@GoodsId
";
if (ormd.PostType == "FEDEX_GROUND")
tsql += @"
Update [HW_PostFee] set Fee2=@Fee where GoodsId=@GoodsId";
if (ormd.PostType == "GROUND_HOME_DELIVERY")
tsql += @"
Update [HW_PostFee] set Fee3=@Fee where GoodsId=@GoodsId";
if (ormd.PostType == "FEDEX_2_DAY")
tsql += @"
Update [HW_PostFee] set Fee4=@Fee where GoodsId=@GoodsId";
}
Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetSqlStringCommand(tsql);
db.AddInParameter(cmd, "@GoodsId", DbType.Int32, ormd.OrderGoodsId);
db.AddInParameter(cmd, "@OrderId", DbType.Int32, ormd.OrderId);
db.AddInParameter(cmd, "@Fee", DbType.Decimal, Fee);
db.AddInParameter(cmd, "@FeeHtml", DbType.String, FeeHtml);
db.ExecuteNonQuery(cmd);
}
else
if (ormd.OrderId != null && ormd.OrderId > 0)
{
string tsql = @"
if(select count(0) from DT_Fedex where OrderId=@OrderId)=0
begin
INSERT INTO [DT_Fedex]([OrderId],Fee6)values(@OrderId,@Fee)
end
else
begin
update [DT_Fedex] set Fee6=@Fee where OrderId=@OrderId
end
";
if (ormd.PostType == "FEDEX_GROUND")
tsql = @"
if(select count(0) from DT_Fedex where OrderId=@OrderId)=0
begin
INSERT INTO [DT_Fedex]([OrderId],Fee7)values(@OrderId,@Fee)
end
else
begin
update [DT_Fedex] set Fee7=@Fee where OrderId=@OrderId
end";
if (ormd.PostType == "GROUND_HOME_DELIVERY")
tsql = @"
if(select count(0) from DT_Fedex where OrderId=@OrderId)=0
begin
INSERT INTO [DT_Fedex]([OrderId],Fee8)values(@OrderId,@Fee)
end
else
begin
update [DT_Fedex] set Fee8=@Fee where OrderId=@OrderId
end";
Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetSqlStringCommand(tsql);
db.AddInParameter(cmd, "@GoodsId", DbType.Int32, ormd.OrderGoodsId);
db.AddInParameter(cmd, "@OrderId", DbType.Int32, ormd.OrderId);
db.AddInParameter(cmd, "@Fee", DbType.Decimal, Fee);
db.AddInParameter(cmd, "@FeeHtml", DbType.String, FeeHtml);
db.ExecuteNonQuery(cmd);
}
}
catch (Exception ex)
{
ErrorFollow.TraceWrite("UpdateFee", "", ex.Message);
}
}
#endregion
private static void ShowDeliveryDetails(RateReplyDetail rateDetail)
{
if (rateDetail.DeliveryTimestampSpecified)
Console.WriteLine("Delivery timestamp: " + rateDetail.DeliveryTimestamp.ToString());
if (rateDetail.TransitTimeSpecified)
Console.WriteLine("Transit time: " + rateDetail.TransitTime);
}
private static string ShowNotifications(RateReply reply)
{
// Console.WriteLine("Notifications");
string a = "";
for (int i = 0; i < reply.Notifications.Length; i++)
{
Notification notification = reply.Notifications[i];
a += "Code:" + notification.Code + ";Message:" + notification.Message + ";Source:" + notification.Source;
//Console.WriteLine("Notification no. {0}", i);
//Console.WriteLine(" Severity: {0}", notification.Severity);
//Console.WriteLine(" Code: {0}", notification.Code);
//Console.WriteLine(" Message: {0}", notification.Message);
//Console.WriteLine(" Source: {0}", notification.Source);
}
return a;
}
private static bool usePropertyFile() //Set to true for common properties to be set with getProperty function.
{
return getProperty("usefile").Equals("True");
}
private static String getProperty(String propertyname) //Sets common properties for testing purposes.
{
try
{
String filename = "C:\\filepath\\filename.txt";
if (System.IO.File.Exists(filename))
{
System.IO.StreamReader sr = new System.IO.StreamReader(filename);
do
{
String[] parts = sr.ReadLine().Split(',');
if (parts[0].Equals(propertyname) && parts.Length == 2)
{
return parts[1];
}
}
while (!sr.EndOfStream);
}
Console.WriteLine("Property {0} set to default 'XXX'", propertyname);
return "XXX";
}
catch (Exception e)
{
Console.WriteLine("Property {0} set to default 'XXX'", propertyname);
return "XXX";
}
}
}
}