Posted By

coresuite on 01/29/11


Tagged

Customize coresuite CSC UPS


Versions (?)

CSC-UPS Integration SHIP:0001 Get Rates with Packages


 / Published in: C#
 

  1. /*
  2. This function has been delivered by coresystems ag as a sample.
  3. This sample has been tested with a SAP Business One demo database and is delivered without further warranties.
  4.  
  5.  ***** Description: *****
  6.   Name: Get UPS live rates
  7. This rule gather the total order weight as well as the ship to address and fills in the live ship costs from UPS.
  8.  
  9. NOTES:
  10. 1. Package max weight is 150LB
  11. 2. make sure the ship from address (taken from the company address) is within the US
  12.  
  13.  
  14.  ***** Parameters: *****
  15.  This function does not contain any parameters.
  16.  
  17.  ***** External DLLs: *****
  18.  in order to use the rule you will need to copy the following dlls:
  19.  
  20. UPSrates.XmlSerializers.dll
  21. UPSrates.dll
  22.  
  23.  into the following directory:
  24.  
  25. C:\Program Files (x86)\SAP\SAP Business One\AddOns\COR\coresuite\COR_Customize\DLL
  26.  
  27.  ***** Function source code: *****
  28. */
  29.  
  30. try
  31. {
  32. SwissAddonFramework.Messaging.StatusBar.WriteSucess("Getting live rates started please wait while contacting UPS rates service...");
  33.  
  34. // GET COMPANY OBJEKT AND STORE IN VARIABLE comp
  35. SAPbobsCOM.Company comp = (SAPbobsCOM.Company) SwissAddonFramework.B1Connector.GetB1Connector().Company;
  36.  
  37. // GET EMTPY PO AND SAVE IN prodOrder
  38. SAPbobsCOM.Items vItem = (SAPbobsCOM.Items) comp.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems);
  39.  
  40.  
  41. UPSrates.RateWebReference.RateService rate = new UPSrates.RateWebReference.RateService();
  42. UPSrates.RateWebReference.RateRequest rateRequest = new UPSrates.RateWebReference.RateRequest();
  43. UPSrates.RateWebReference.UPSSecurity upss = new UPSrates.RateWebReference.UPSSecurity();
  44. UPSrates.RateWebReference.UPSSecurityServiceAccessToken upssSvcAccessToken = new UPSrates.RateWebReference.UPSSecurityServiceAccessToken();
  45. upssSvcAccessToken.AccessLicenseNumber = "0C69CC8370856078";
  46. upss.ServiceAccessToken = upssSvcAccessToken;
  47. UPSrates.RateWebReference.UPSSecurityUsernameToken upssUsrNameToken = new UPSrates.RateWebReference.UPSSecurityUsernameToken();
  48. upssUsrNameToken.Username = "coresuite";
  49. upssUsrNameToken.Password = "vista7";
  50. upss.UsernameToken = upssUsrNameToken;
  51. rate.UPSSecurityValue = upss;
  52.  
  53. UPSrates.RateWebReference.RequestType request = new UPSrates.RateWebReference.RequestType();
  54. String[] requestOption = { "shop" };
  55. request.RequestOption = requestOption;
  56. rateRequest.Request = request;
  57. UPSrates.RateWebReference.ShipmentType shipment = new UPSrates.RateWebReference.ShipmentType();
  58. UPSrates.RateWebReference.ShipperType shipper = new UPSrates.RateWebReference.ShipperType();
  59.  
  60. System.Data.SqlClient.SqlCommand SqlCmd;
  61. string street = "";
  62. string city ="";
  63. string zipcode = "";
  64. string state = "";
  65. string country = "";
  66. string CardCode = ((TextEdit) pVal.Form.Items["4"]).Value;
  67. string AddName = ((ComboBox) pVal.Form.Items["40"]).Value;
  68. string SqlTxt = "";
  69.  
  70. bool useUIAddress = false;
  71. if (useUIAddress)
  72. {
  73. try
  74. {
  75.  
  76. //Read ship-to address
  77. pVal.Form.Freeze(true);
  78. Folder.GetFromUID(pVal.Form, "114").Click(SwissAddonFramework.UI.Components.Item.ClickType.Regular);
  79. Button.GetFromUID(pVal.Form, "10002101").Click(SwissAddonFramework.UI.Components.Item.ClickType.Regular);
  80.  
  81. Form ActiveForm = Form.GetFormFromUID(customize.B1Connector.GetB1Connector().Application.Forms.ActiveForm.UniqueID);
  82. Matrix mtxRows = Matrix.GetFromUID(ActiveForm, "10000003");
  83. street = mtxRows.GetValue("10000003", 0);
  84. city = mtxRows.GetValue("10000011", 0);
  85. zipcode = mtxRows.GetValue("10000013", 0);
  86. state = mtxRows.GetValue("10000017", 0);
  87. country = mtxRows.GetValue("10000019", 0);
  88.  
  89. // close address form
  90. Button.GetFromUID(ActiveForm, "10000001").Click(SwissAddonFramework.UI.Components.Item.ClickType.Regular);
  91.  
  92. Folder.GetFromUID(pVal.Form, "112").Click(SwissAddonFramework.UI.Components.Item.ClickType.Regular);
  93. }
  94. catch (Exception ex)
  95. {
  96. SwissAddonFramework.Messaging.StatusBar.WriteError(ex.Message);
  97. }
  98. finally
  99. {
  100. pVal.Form.Freeze(false);
  101. }
  102. }
  103. else
  104. {
  105. 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'";
  106. SqlCmd = new System.Data.SqlClient.SqlCommand(SqlTxt);
  107. //SqlCmd.Parameters.AddWithValue("@CardCode", CardCode);
  108. //SqlCmd.Parameters.AddWithValue("@AddName", AddName);
  109. System.Data.SqlClient.SqlDataReader addrSdr = customize.B1Connector.GetB1Connector().ExecuteQuery(SqlCmd);
  110. if(addrSdr.Read())
  111. {
  112. street = addrSdr[0].ToString();
  113. city = addrSdr[1].ToString();
  114. state = addrSdr[2].ToString();
  115. zipcode = addrSdr[3].ToString();
  116. country = addrSdr[4].ToString();
  117. }
  118. }
  119. SwissAddonFramework.Messaging.StatusBar.WriteSucess("Ship-To Address:" + AddName + ":" + CardCode + ": " + street + " " + city + " " + state + " " + zipcode + " " + country);
  120.  
  121. // read ship-from address
  122. string companyStreet = "";
  123. string companyZipCode = "";
  124. string companyCity = "";
  125. string companyState = "";
  126. string companyCountry = "";
  127.  
  128. SqlCmd = new System.Data.SqlClient.SqlCommand("SELECT Street, ZipCode, City, State, Country FROM ADM1");
  129. System.Data.SqlClient.SqlDataReader sdr = customize.B1Connector.GetB1Connector().ExecuteQuery(SqlCmd);
  130. if(sdr.Read())
  131. {
  132. companyStreet = sdr[0].ToString();
  133. companyZipCode = sdr[1].ToString();
  134. companyCity = sdr[2].ToString();
  135. companyState = sdr[3].ToString();
  136. companyCountry = sdr[4].ToString();
  137. }
  138.  
  139. shipper.ShipperNumber = "1"; //Shipper's shipper number
  140. UPSrates.RateWebReference.AddressType shipperAddress = new UPSrates.RateWebReference.AddressType();
  141. String[] addressLine = { companyStreet };
  142. shipperAddress.AddressLine = addressLine;
  143. shipperAddress.City = companyCity;
  144. shipperAddress.PostalCode = companyZipCode;
  145. shipperAddress.StateProvinceCode = companyState;
  146. shipperAddress.CountryCode = companyCountry;
  147. shipperAddress.AddressLine = addressLine;
  148. shipper.Address = shipperAddress;
  149. shipment.Shipper = shipper;
  150. UPSrates.RateWebReference.ShipFromType shipFrom = new UPSrates.RateWebReference.ShipFromType();
  151. UPSrates.RateWebReference.AddressType shipFromAddress = new UPSrates.RateWebReference.AddressType();
  152. shipFromAddress.AddressLine = addressLine;
  153. shipFromAddress.City = companyCity;
  154. shipFromAddress.PostalCode = companyZipCode;
  155. shipFromAddress.StateProvinceCode = companyState;
  156. shipFromAddress.CountryCode = companyCountry;
  157. shipFrom.Address = shipFromAddress;
  158. shipment.ShipFrom = shipFrom;
  159. UPSrates.RateWebReference.ShipToType shipTo = new UPSrates.RateWebReference.ShipToType();
  160. UPSrates.RateWebReference.ShipToAddressType shipToAddress = new UPSrates.RateWebReference.ShipToAddressType();
  161. String[] addressLine1 = { street };
  162. shipToAddress.AddressLine = addressLine1;
  163. shipToAddress.City = city;
  164. shipToAddress.PostalCode = zipcode;
  165. shipToAddress.StateProvinceCode = state;
  166. shipToAddress.CountryCode = country;
  167. shipTo.Address = shipToAddress;
  168. shipment.ShipTo = shipTo;
  169. UPSrates.RateWebReference.CodeDescriptionType service = new UPSrates.RateWebReference.CodeDescriptionType();
  170.  
  171. double TotalWeight = 0;
  172. double pkgCurrTotalWeight = 0;
  173. double currWeight = 0;
  174. System.Collections.Generic.List<UPSrates.RateWebReference.PackageType> pkgArrayList = new System.Collections.Generic.List<UPSrates.RateWebReference.PackageType>();
  175. UPSrates.RateWebReference.PackageType package = new UPSrates.RateWebReference.PackageType();
  176. UPSrates.RateWebReference.PackageWeightType packageWeight = new UPSrates.RateWebReference.PackageWeightType();
  177.  
  178. // start new packageeightType();
  179. UPSrates.RateWebReference.CodeDescriptionType uom = new UPSrates.RateWebReference.CodeDescriptionType();
  180. uom.Code = "lbs";
  181. uom.Description = "pounds";
  182. packageWeight.UnitOfMeasurement = uom;
  183. package.PackageWeight = packageWeight;
  184. UPSrates.RateWebReference.CodeDescriptionType packType = new UPSrates.RateWebReference.CodeDescriptionType();
  185. packType.Code = "02";
  186. package.PackagingType = packType;
  187. int pckNumber = 1;
  188.  
  189. System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
  190.  
  191. // read matrix
  192. Matrix m = Matrix.GetFromUID(pVal.Form, "38");
  193. for(int i = 0; i < m.Rows.Count ; i++)
  194. {
  195. string itemCode = m.GetValue("1", i);
  196. //MessageBox.Show(m.GetValue("11", i), "OK");
  197. double quantity = double.Parse(m.GetValue("11", i), System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
  198.  
  199. if(string.IsNullOrEmpty(itemCode)) continue;
  200.  
  201. if(vItem.GetByKey(itemCode) == true)
  202. {
  203. currWeight = vItem.SalesUnitWeight * quantity;
  204. SwissAddonFramework.Messaging.StatusBar.WriteSucess("OK Item: ItemCode:" + itemCode + " Quantity:" + quantity.ToString() + " Weight:" + currWeight.ToString());
  205.  
  206. if ((currWeight + pkgCurrTotalWeight) >= 150)
  207. {
  208.  
  209. packageWeight.Weight = pkgCurrTotalWeight.ToString();
  210. pkgArrayList.Add(package);
  211. SwissAddonFramework.Messaging.StatusBar.WriteSucess("Package " + pckNumber.ToString() + " weight:" + pkgCurrTotalWeight.ToString());
  212.  
  213. // start new package
  214. pckNumber++;
  215. package = new UPSrates.RateWebReference.PackageType();
  216. packageWeight = new UPSrates.RateWebReference.PackageWeightType();
  217. packageWeight.Weight = currWeight.ToString();
  218. pkgCurrTotalWeight = currWeight;
  219. uom = new UPSrates.RateWebReference.CodeDescriptionType();
  220. uom.Code = "lbs";
  221. uom.Description = "pounds";
  222. packageWeight.UnitOfMeasurement = uom;
  223. package.PackageWeight = packageWeight;
  224. packType = new UPSrates.RateWebReference.CodeDescriptionType();
  225. packType.Code = "02";
  226. package.PackagingType = packType;
  227.  
  228. }
  229. else
  230. {
  231. //add to existing package
  232. pkgCurrTotalWeight += currWeight;
  233. }
  234. }
  235. }
  236.  
  237. packageWeight.Weight = pkgCurrTotalWeight.ToString();
  238. pkgArrayList.Add(package);
  239. SwissAddonFramework.Messaging.StatusBar.WriteSucess("Package " + pckNumber.ToString() + " weight:" + pkgCurrTotalWeight.ToString());
  240. SwissAddonFramework.Messaging.StatusBar.WriteSucess("Total number of packages " + pckNumber.ToString());
  241.  
  242.  
  243. //SwissAddonFramework.Messaging.StatusBar.WriteSucess("Total Weight for Shipment is:" + TotalWeight.ToString());
  244.  
  245. // as default we use UPS ground // TODO future dev will be to read dynamic values from SAP
  246. // 01 - UPS Next Day Air® United States Domestic Shipments
  247. // 02 - UPS Second Day Air® United States Domestic Shipments
  248. // 03 - UPS Ground United States Domestic Shipments
  249. // 12 - UPS Three - Day Select® United States Domestic Shipments
  250. // 13 - UPS Next Day Air Saver® United States Domestic Shipments
  251. // 14 - UPS Next Day Air® Early A.M. SM United States Domestic Shipments
  252. // 59 - UPS Second Day Air A.M.® United States Domestic Shipments
  253. // 65 - UPS Saver United States Domestic Shipments
  254.  
  255. service.Code = "03";
  256. shipment.Service = service;
  257.  
  258.  
  259. UPSrates.RateWebReference.PackageType[] pkgArray = pkgArrayList.ToArray();
  260. shipment.Package = pkgArray;
  261. rateRequest.Shipment = shipment;
  262. System.Net.ServicePointManager.CertificatePolicy = new UPSrates.TrustAllCertificatePolicy();
  263. Console.WriteLine(rateRequest);
  264. UPSrates.RateWebReference.RateResponse rateResponse = rate.ProcessRate(rateRequest);
  265.  
  266. try
  267. {
  268.  
  269. // read values from Form
  270. //Fill total live freight cost from UPS in the freight form
  271. pVal.Form.Freeze(true);
  272. LinkedButton.GetFromUID(pVal.Form, "91").Click(SwissAddonFramework.UI.Components.Item.ClickType.Regular);
  273.  
  274. Form FreightForm = Form.GetFormFromUID(customize.B1Connector.GetB1Connector().Application.Forms.ActiveForm.UniqueID);
  275. Matrix FreightMtxRows = Matrix.GetFromUID(FreightForm, "3");
  276.  
  277. // row index holds the line in the freight window to fill with freight charges currently we default it to the first row.
  278. int rowIndex = 0;
  279. FreightMtxRows.SetValue("3", rowIndex, rateResponse.RatedShipment[0].TotalCharges.MonetaryValue);
  280.  
  281. // close freight form
  282. Button.GetFromUID(FreightForm, "1").Click(SwissAddonFramework.UI.Components.Item.ClickType.Regular);
  283. Button.GetFromUID(FreightForm, "1").Click(SwissAddonFramework.UI.Components.Item.ClickType.Regular);
  284. }
  285. catch (Exception ex)
  286. {
  287. SwissAddonFramework.Messaging.StatusBar.WriteError(ex.Message);
  288. }
  289. finally
  290. {
  291. pVal.Form.Freeze(false);
  292. }
  293.  
  294.  
  295. SwissAddonFramework.Messaging.StatusBar.WriteSucess("The transaction was a " + rateResponse.Response.ResponseStatus.Description);
  296. SwissAddonFramework.Messaging.StatusBar.WriteSucess("Total Shipment Charges " + rateResponse.RatedShipment[0].TotalCharges.MonetaryValue + rateResponse.RatedShipment[0].TotalCharges.CurrencyCode);
  297. }
  298. catch (System.Web.Services.Protocols.SoapException ex)
  299. {
  300. //SwissAddonFramework.Messaging.StatusBar.WriteError("---------Web Service returns error----------------");
  301. //SwissAddonFramework.Messaging.StatusBar.WriteError("---------\"Hard\" is user error \"Transient\" is system error----------------");
  302. //SwissAddonFramework.Messaging.StatusBar.WriteError("SoapException Message= " + ex.Message);
  303. SwissAddonFramework.Messaging.StatusBar.WriteError("SoapException Category:Code:Message= " + ex.Detail.LastChild.InnerText);
  304. //SwissAddonFramework.Messaging.StatusBar.WriteError("SoapException XML String for all= " + ex.Detail.LastChild.OuterXml);
  305. //SwissAddonFramework.Messaging.StatusBar.WriteError("SoapException StackTrace= " + ex.StackTrace);
  306. }
  307. catch (System.ServiceModel.CommunicationException ex)
  308. {
  309. SwissAddonFramework.Messaging.StatusBar.WriteError("CommunicationException= " + ex.Message);
  310. SwissAddonFramework.Messaging.StatusBar.WriteError("CommunicationException-StackTrace= " + ex.StackTrace);
  311.  
  312. }
  313. catch (Exception ex)
  314. {
  315. SwissAddonFramework.Messaging.StatusBar.WriteError(" Generaal Exception= " + ex.Message);
  316. SwissAddonFramework.Messaging.StatusBar.WriteError(" Generaal Exception-StackTrace= " + ex.StackTrace);
  317. }

Report this snippet  

You need to login to post a comment.