Return to Snippet

Revision: 40293
at January 29, 2011 00:43 by coresuite


Initial Code
/*
This function has been delivered by coresystems ag as a sample. 
This sample has been tested with a SAP Business One demo database and is delivered without further warranties.

 ***** Description: *****
 	Name: Get UPS live rates 
	This rule gather the total order weight as well as the ship to address and fills in the live ship costs from UPS.

	NOTES:
			1. Package max weight is 150LB
			2. make sure the ship from address (taken from the company address) is within the US
    

 ***** Parameters: *****
 This function does not contain any parameters.

 ***** External DLLs: *****
 in order to use the rule you will need to copy the following dlls:

UPSrates.XmlSerializers.dll  
UPSrates.dll

 into the following directory:

C:\Program Files (x86)\SAP\SAP Business One\AddOns\COR\coresuite\COR_Customize\DLL

 ***** Function source code: *****
*/

try
{
	SwissAddonFramework.Messaging.StatusBar.WriteSucess("Getting live rates started please wait while contacting UPS rates service...");

	// GET COMPANY OBJEKT AND STORE IN VARIABLE comp
	SAPbobsCOM.Company comp = (SAPbobsCOM.Company) SwissAddonFramework.B1Connector.GetB1Connector().Company;

	// GET EMTPY PO AND SAVE IN prodOrder
	SAPbobsCOM.Items vItem = (SAPbobsCOM.Items) comp.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems);

	
	UPSrates.RateWebReference.RateService rate = new UPSrates.RateWebReference.RateService();
	UPSrates.RateWebReference.RateRequest rateRequest = new UPSrates.RateWebReference.RateRequest();
	UPSrates.RateWebReference.UPSSecurity upss = new UPSrates.RateWebReference.UPSSecurity();
	UPSrates.RateWebReference.UPSSecurityServiceAccessToken upssSvcAccessToken = new UPSrates.RateWebReference.UPSSecurityServiceAccessToken();
	upssSvcAccessToken.AccessLicenseNumber = "0C69CC8370856078";
	upss.ServiceAccessToken = upssSvcAccessToken;
	UPSrates.RateWebReference.UPSSecurityUsernameToken upssUsrNameToken = new UPSrates.RateWebReference.UPSSecurityUsernameToken();
	upssUsrNameToken.Username = "coresuite";
	upssUsrNameToken.Password = "vista7";
	upss.UsernameToken = upssUsrNameToken;
	rate.UPSSecurityValue = upss;

	UPSrates.RateWebReference.RequestType request = new UPSrates.RateWebReference.RequestType();
	String[] requestOption = { "shop" };
	request.RequestOption = requestOption;
	rateRequest.Request = request;
	UPSrates.RateWebReference.ShipmentType shipment = new UPSrates.RateWebReference.ShipmentType();
	UPSrates.RateWebReference.ShipperType shipper = new UPSrates.RateWebReference.ShipperType();

	System.Data.SqlClient.SqlCommand SqlCmd;
	string street = "";
	string city ="";
	string zipcode = "";
	string state = "";
	string country = "";
	string CardCode = ((TextEdit) pVal.Form.Items["4"]).Value;
	string AddName = ((ComboBox) pVal.Form.Items["40"]).Value;
	string SqlTxt = "";
	
	bool useUIAddress = false;
	if (useUIAddress)
	{
		try
		{
		
			//Read ship-to address
			pVal.Form.Freeze(true);
			Folder.GetFromUID(pVal.Form, "114").Click(SwissAddonFramework.UI.Components.Item.ClickType.Regular);		
			Button.GetFromUID(pVal.Form, "10002101").Click(SwissAddonFramework.UI.Components.Item.ClickType.Regular);
		
			Form ActiveForm = Form.GetFormFromUID(customize.B1Connector.GetB1Connector().Application.Forms.ActiveForm.UniqueID);		
			Matrix mtxRows = Matrix.GetFromUID(ActiveForm, "10000003");
			street = mtxRows.GetValue("10000003", 0);
			city = mtxRows.GetValue("10000011", 0);
			zipcode = mtxRows.GetValue("10000013", 0);
			state = mtxRows.GetValue("10000017", 0);
			country = mtxRows.GetValue("10000019", 0);

			// close address form
			Button.GetFromUID(ActiveForm, "10000001").Click(SwissAddonFramework.UI.Components.Item.ClickType.Regular);

			Folder.GetFromUID(pVal.Form, "112").Click(SwissAddonFramework.UI.Components.Item.ClickType.Regular);		
		}
		catch (Exception ex)
		{
			SwissAddonFramework.Messaging.StatusBar.WriteError(ex.Message);
		}		
		finally
		{
			pVal.Form.Freeze(false);
		}
	}
	else
	{	
		SqlTxt = "SELECT 	T1.[Street], T1.[City], T1.[State],T1.[ZipCode], T1.[Country] FROM[dbo].[CRD1]  T1 WHERE T1.[Address] = '" + AddName + "' AND T1.[CardCode]  = '" + CardCode + "' AND T1.[AdresType]  = 'S'";
		SqlCmd = new System.Data.SqlClient.SqlCommand(SqlTxt);
		//SqlCmd.Parameters.AddWithValue("@CardCode", CardCode);
		//SqlCmd.Parameters.AddWithValue("@AddName", AddName);
		System.Data.SqlClient.SqlDataReader addrSdr = customize.B1Connector.GetB1Connector().ExecuteQuery(SqlCmd);
		if(addrSdr.Read())
		{
			street = addrSdr[0].ToString();
			city = addrSdr[1].ToString();
			state = addrSdr[2].ToString();
			zipcode = addrSdr[3].ToString();
			country = addrSdr[4].ToString();
		}
	}
	SwissAddonFramework.Messaging.StatusBar.WriteSucess("Ship-To Address:" + AddName + ":" + CardCode + ": " + street + " " + city + " " + state + " " + zipcode + " " + country);
	
	// read ship-from address
	string companyStreet = "";
	string companyZipCode = "";
	string companyCity = "";
	string companyState = "";
	string companyCountry = "";
	
	SqlCmd = new System.Data.SqlClient.SqlCommand("SELECT Street, ZipCode, City, State, Country FROM ADM1");
	System.Data.SqlClient.SqlDataReader sdr = customize.B1Connector.GetB1Connector().ExecuteQuery(SqlCmd);
	if(sdr.Read())
	{
		companyStreet = sdr[0].ToString();
		companyZipCode = sdr[1].ToString();
		companyCity = sdr[2].ToString();
		companyState = sdr[3].ToString();
		companyCountry = sdr[4].ToString();
	}
	
	shipper.ShipperNumber = "1"; //Shipper's shipper number
	UPSrates.RateWebReference.AddressType shipperAddress = new UPSrates.RateWebReference.AddressType();
	String[] addressLine = { companyStreet };
	shipperAddress.AddressLine = addressLine;
	shipperAddress.City = companyCity;
	shipperAddress.PostalCode = companyZipCode;
	shipperAddress.StateProvinceCode = companyState;
	shipperAddress.CountryCode = companyCountry;
	shipperAddress.AddressLine = addressLine;
	shipper.Address = shipperAddress;
	shipment.Shipper = shipper;
	UPSrates.RateWebReference.ShipFromType shipFrom = new UPSrates.RateWebReference.ShipFromType();
	UPSrates.RateWebReference.AddressType shipFromAddress = new UPSrates.RateWebReference.AddressType();
	shipFromAddress.AddressLine = addressLine;
	shipFromAddress.City = companyCity;
	shipFromAddress.PostalCode = companyZipCode;
	shipFromAddress.StateProvinceCode = companyState;
	shipFromAddress.CountryCode = companyCountry;
	shipFrom.Address = shipFromAddress;
	shipment.ShipFrom = shipFrom;
	UPSrates.RateWebReference.ShipToType shipTo = new UPSrates.RateWebReference.ShipToType();
	UPSrates.RateWebReference.ShipToAddressType shipToAddress = new UPSrates.RateWebReference.ShipToAddressType();
	String[] addressLine1 = { street };
	shipToAddress.AddressLine = addressLine1;
	shipToAddress.City = city;
	shipToAddress.PostalCode = zipcode;
	shipToAddress.StateProvinceCode = state;
	shipToAddress.CountryCode = country;
	shipTo.Address = shipToAddress;
	shipment.ShipTo = shipTo;
	UPSrates.RateWebReference.CodeDescriptionType service = new UPSrates.RateWebReference.CodeDescriptionType();

	double TotalWeight = 0;
	double pkgCurrTotalWeight = 0;
	double currWeight = 0;
	System.Collections.Generic.List<UPSrates.RateWebReference.PackageType> pkgArrayList = new System.Collections.Generic.List<UPSrates.RateWebReference.PackageType>();
	UPSrates.RateWebReference.PackageType package = new UPSrates.RateWebReference.PackageType();
	UPSrates.RateWebReference.PackageWeightType packageWeight = new UPSrates.RateWebReference.PackageWeightType();
				
	// start new packageeightType();
	UPSrates.RateWebReference.CodeDescriptionType uom = new UPSrates.RateWebReference.CodeDescriptionType();
	uom.Code = "lbs";
	uom.Description = "pounds";
	packageWeight.UnitOfMeasurement = uom;
	package.PackageWeight = packageWeight;
	UPSrates.RateWebReference.CodeDescriptionType packType = new UPSrates.RateWebReference.CodeDescriptionType();
	packType.Code = "02";
	package.PackagingType = packType;
	int pckNumber = 1;
	
	System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
	
	// read matrix
	Matrix m = Matrix.GetFromUID(pVal.Form, "38");
	for(int i = 0; i < m.Rows.Count ; i++)
	{
		string itemCode = m.GetValue("1", i);
		//MessageBox.Show(m.GetValue("11", i), "OK");
		double quantity = double.Parse(m.GetValue("11", i), System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
		
		if(string.IsNullOrEmpty(itemCode)) continue;
		
		if(vItem.GetByKey(itemCode) == true)
		{			
			currWeight = vItem.SalesUnitWeight * quantity;
			SwissAddonFramework.Messaging.StatusBar.WriteSucess("OK Item: ItemCode:" + itemCode + " Quantity:" + quantity.ToString() + " Weight:" + currWeight.ToString());

			if ((currWeight + pkgCurrTotalWeight) >= 150)
			{
				
				packageWeight.Weight = pkgCurrTotalWeight.ToString();
				pkgArrayList.Add(package);
				SwissAddonFramework.Messaging.StatusBar.WriteSucess("Package " + pckNumber.ToString() + " weight:" + pkgCurrTotalWeight.ToString());
				
				// start new package
				pckNumber++;
				package = new UPSrates.RateWebReference.PackageType();
				packageWeight = new UPSrates.RateWebReference.PackageWeightType();
				packageWeight.Weight = currWeight.ToString();
				pkgCurrTotalWeight = currWeight;
				uom = new UPSrates.RateWebReference.CodeDescriptionType();
				uom.Code = "lbs";
				uom.Description = "pounds";
				packageWeight.UnitOfMeasurement = uom;
				package.PackageWeight = packageWeight;
				packType = new UPSrates.RateWebReference.CodeDescriptionType();
				packType.Code = "02";
				package.PackagingType = packType;

			}
			else
			{
				//add to existing package
				pkgCurrTotalWeight += currWeight;
			}
		}
	}
	
	packageWeight.Weight = pkgCurrTotalWeight.ToString();
	pkgArrayList.Add(package);
	SwissAddonFramework.Messaging.StatusBar.WriteSucess("Package " + pckNumber.ToString() + " weight:" + pkgCurrTotalWeight.ToString());
	SwissAddonFramework.Messaging.StatusBar.WriteSucess("Total number of packages " + pckNumber.ToString());


	//SwissAddonFramework.Messaging.StatusBar.WriteSucess("Total Weight for Shipment is:" + TotalWeight.ToString());

	// as default we use UPS ground // TODO future dev will be to read dynamic values from SAP
	// 01 - UPS Next Day Air® United States Domestic Shipments
	// 02 - UPS Second Day Air® United States Domestic Shipments
	// 03 - UPS Ground United States Domestic Shipments
	// 12 - UPS Three - Day Select® United States Domestic Shipments
	// 13 - UPS Next Day Air Saver® United States Domestic Shipments
	// 14 - UPS Next Day Air® Early A.M. SM United States Domestic Shipments
	// 59 - UPS Second Day Air A.M.® United States Domestic Shipments
	// 65 - UPS Saver United States Domestic Shipments
	
	service.Code = "03";
	shipment.Service = service;


	UPSrates.RateWebReference.PackageType[] pkgArray = pkgArrayList.ToArray();
	shipment.Package = pkgArray;
	rateRequest.Shipment = shipment;
	System.Net.ServicePointManager.CertificatePolicy = new UPSrates.TrustAllCertificatePolicy();
	Console.WriteLine(rateRequest);
	UPSrates.RateWebReference.RateResponse rateResponse = rate.ProcessRate(rateRequest);
	
	try
	{
		
		// read values from Form
		//Fill total live freight cost from UPS in the freight form
		pVal.Form.Freeze(true);
		LinkedButton.GetFromUID(pVal.Form, "91").Click(SwissAddonFramework.UI.Components.Item.ClickType.Regular);
		
		Form FreightForm = Form.GetFormFromUID(customize.B1Connector.GetB1Connector().Application.Forms.ActiveForm.UniqueID);		
		Matrix FreightMtxRows = Matrix.GetFromUID(FreightForm, "3");

		// row index holds the line in the freight window to fill with freight charges currently we default it to the first row.
		int rowIndex = 0;
		FreightMtxRows.SetValue("3", rowIndex, rateResponse.RatedShipment[0].TotalCharges.MonetaryValue);

		// close freight form
		Button.GetFromUID(FreightForm, "1").Click(SwissAddonFramework.UI.Components.Item.ClickType.Regular);
		Button.GetFromUID(FreightForm, "1").Click(SwissAddonFramework.UI.Components.Item.ClickType.Regular);
	}
	catch (Exception ex)
	{
		SwissAddonFramework.Messaging.StatusBar.WriteError(ex.Message);
	}		
	finally
	{
		pVal.Form.Freeze(false);
	}

	
	SwissAddonFramework.Messaging.StatusBar.WriteSucess("The transaction was a " + rateResponse.Response.ResponseStatus.Description);
	SwissAddonFramework.Messaging.StatusBar.WriteSucess("Total Shipment Charges " + rateResponse.RatedShipment[0].TotalCharges.MonetaryValue + rateResponse.RatedShipment[0].TotalCharges.CurrencyCode);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
	//SwissAddonFramework.Messaging.StatusBar.WriteError("---------Web Service returns error----------------");
	//SwissAddonFramework.Messaging.StatusBar.WriteError("---------\"Hard\" is user error \"Transient\" is system error----------------");
	//SwissAddonFramework.Messaging.StatusBar.WriteError("SoapException Message= " + ex.Message);
	SwissAddonFramework.Messaging.StatusBar.WriteError("SoapException Category:Code:Message= " + ex.Detail.LastChild.InnerText);
	//SwissAddonFramework.Messaging.StatusBar.WriteError("SoapException XML String for all= " + ex.Detail.LastChild.OuterXml);
	//SwissAddonFramework.Messaging.StatusBar.WriteError("SoapException StackTrace= " + ex.StackTrace);
}
catch (System.ServiceModel.CommunicationException ex)
{
	SwissAddonFramework.Messaging.StatusBar.WriteError("CommunicationException= " + ex.Message);
	SwissAddonFramework.Messaging.StatusBar.WriteError("CommunicationException-StackTrace= " + ex.StackTrace);

}
catch (Exception ex)
{
	SwissAddonFramework.Messaging.StatusBar.WriteError(" Generaal Exception= " + ex.Message);
	SwissAddonFramework.Messaging.StatusBar.WriteError(" Generaal Exception-StackTrace= " + ex.StackTrace);
}

Initial URL


Initial Description


Initial Title
CSC-UPS Integration SHIP:0001 Get Rates with Packages

Initial Tags


Initial Language
C#