/* first constructor that show sears order */ public DetailPage(SearsValues value) { InitializeComponent(); searsValues = value; ShowResult(value); // set flag to sears CHANNEL = "Sears"; }
/* second constructor that take SearsValues object as parameter */ public Package(SearsValues value) { // generate package detail -> weight and dimensions decimal[] skuDetail = { 0, 0, 0, 0 }; foreach (decimal[] detailList in value.TrxVendorSku.Select(GetSkuDetail).Where(detailList => !detailList.Equals(null))) { for (int i = 0; i < 4; i++) { skuDetail[i] += detailList[i]; } } // allocate data Weight = skuDetail[0] / 1000; Length = skuDetail[1]; Width = skuDetail[2]; Height = skuDetail[3]; // those are set to default PackageType = "Customer Supplied Package"; Service = "UPS Standard"; }
/* the event for detail button click that show the detail page for the selected item */ private void detailButton_Click(object sender, EventArgs e) { // the case if the user does not select any thing or select more than one if (listview.CheckedItems.Count != 1) { MessageBox.Show("Please select one item to see more details", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } switch (listview.CheckedItems[0].SubItems[0].Text) { case "Sears": { // the case if it is sears order SearsValues value = sears.GenerateValue(listview.CheckedItems[0].SubItems[4].Text); new DetailPage(value).ShowDialog(this); } break; case "Shop.ca": { // the case if it is shopl.ca order ShopCaValues value = shopCa.GenerateValue(listview.CheckedItems[0].SubItems[4].Text); new DetailPage(value).ShowDialog(this); } break; case "Giant Tiger": { // the case if it is giant tiger order GiantTigerValues value = giantTiger.GenerateValue(listview.CheckedItems[0].SubItems[4].Text); new DetailPage(value).ShowDialog(this); } break; } }
/* background worker that processing each order from the orderList */ private void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { // initialize all carrier fields Ups ups = new Ups(); CanadaPost canadaPost = new CanadaPost(); // start processing orders foreach (Order order in orderList) { // for sears order switch (order.Source) { case "Sears": { #region Sears Order // first get the detail for the order SearsValues value = sears.GenerateValue(order.TransactionId); // check if the order has been shipped before -> if not, ship it now if (value.Package.TrackingNumber == "") { value.Package = new Package(value); // second ship it string[] digest = ups.PostShipmentConfirm(value); if (ups.Error) { MessageBox.Show(ups.ErrorMessage, "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string[] result = ups.PostShipmentAccept(digest[1]); if (ups.Error) { MessageBox.Show(ups.ErrorMessage, "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // get identification, tracking, label and shipment confirm with no cancellation of item value.Package.IdentificationNumber = digest[0]; value.Package.TrackingNumber = result[0]; ups.ExportLabel(result[1], value.TransactionId, false); } sears.GenerateXml(value, new System.Collections.Generic.Dictionary <int, string>()); // post order to brightpearl with no cancellation bp.PostOrder(value, new int[0]); #endregion } break; case "Shop.ca": { #region Shop.ca Order // first get the detail for the order ShopCaValues value = shopCa.GenerateValue(order.TransactionId); // check if the order has been shipped before -> if not, ship it now if (value.Package.TrackingNumber == "") { value.Package = new Package(value); // second ship it string[] links = canadaPost.CreateShipment(value.ShipTo, value.Package); if (canadaPost.Error) { MessageBox.Show(canadaPost.ErrorMessage, "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // get tracking, self link, label link and shipment confirm with no cancellation of item value.Package.TrackingNumber = links[0]; value.Package.SelfLink = links[1]; value.Package.LabelLink = links[2]; System.Threading.Thread.Sleep(5000); // get artifact and export it byte[] binary = canadaPost.GetArtifact(links[2]); canadaPost.ExportLabel(binary, value.OrderId, true, false); } shopCa.GenerateTxt(value, new System.Collections.Generic.Dictionary <int, string>()); // post order to brightpearl with no cancellation bp.PostOrder(value, new int[0]); #endregion } break; case "Giant Tiger": { #region Giant Tiger Order // first get the detail for the order GiantTigerValues value = giantTiger.GenerateValue(order.TransactionId); // check if the order has been shipped before -> if not, ship it now if (value.Package.TrackingNumber == "") { value.Package = new Package(value); // second ship it string[] links = canadaPost.CreateShipment(value.ShipTo, value.Package); if (canadaPost.Error) { MessageBox.Show(canadaPost.ErrorMessage, "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // get tracking, self link, label link and shipment confirm with no cancellation of item value.Package.TrackingNumber = links[0]; value.Package.SelfLink = links[1]; value.Package.LabelLink = links[2]; System.Threading.Thread.Sleep(5000); // get artifact and export it byte[] binary = canadaPost.GetArtifact(links[2]); canadaPost.ExportLabel(binary, value.PoNumber, true, false); } giantTiger.GenerateCsv(value, new System.Collections.Generic.Dictionary <int, string>()); // post order to brightpearl with no cancellation bp.PostOrder(value, new int[0]); #endregion } break; } } }
/* a method that show the information of the given SearsValues object */ private void ShowResult(SearsValues value) { // title bar set up logoPicturebox.Image = Properties.Resources.sears; topOrderNumberTextbox.Text = value.TransactionId; #region Order Summary // date orderDateTextbox.Text = value.CustOrderDate.ToString("MM/dd/yyyy"); paidDateTextbox.Text = value.CustOrderDate.ToString("MM/dd/yyyy"); shipByDateTextbox.Text = value.ExpectedShipDate[0].ToString("MM/dd/yyyy"); // unit price double price = 0; for (int i = 0; i < value.LineCount; i++) { price += value.UnitPrice[i] * value.TrxQty[i]; } unitPriceTotalTextbox.Text = price.ToString(CultureInfo.InvariantCulture); // GST and HST price = value.GstHstExtended.Sum() + value.GstHstTotal.Sum(); gsthstTextbox.Text = price.ToString(CultureInfo.InvariantCulture); // PST price = value.PstExtended.Sum() + value.PstTotal.Sum(); pstTextbox.Text = price.ToString(CultureInfo.InvariantCulture); // other fee price = value.LineShipping.Sum() + value.LineHandling.Sum(); otherFeeTextbox.Text = price.ToString(CultureInfo.InvariantCulture); // total totalOrderTextbox.Text = value.TrxBalanceDue.ToString(CultureInfo.InvariantCulture); #endregion #region Buyer / Recipient Info // sold to soldToTextbox.Text = value.Recipient.Name; soldToPhoneTextbox.Text = value.Recipient.DayPhone; // ship to shipToNameTextbox.Text = value.ShipTo.Name; shipToAddress1Textbox.Text = value.ShipTo.Address1; shipToAddress2Textbox.Text = value.ShipTo.Address2; shipToCombineTextbox.Text = value.ShipTo.City + ", " + value.ShipTo.State + ", " + value.ShipTo.PostalCode; shipToPhoneTextbox.Text = value.ShipTo.DayPhone; #endregion #region Listview and Shipping Info // adding items to service combobox serviceCombobox.Items.Clear(); serviceCombobox.Items.Add("UPS Standard"); serviceCombobox.Items.Add("UPS Express"); serviceCombobox.Items.Add("UPS 3 Day Select"); serviceCombobox.Items.Add("UPS Worldwide Express"); serviceCombobox.SelectedIndex = 0; // adding items to reason combobox reasonCombobox.Items.Clear(); reasonCombobox.Items.Add("Select Reason"); reasonCombobox.Items.Add("Incorrect Ship To Address"); reasonCombobox.Items.Add("Incorrect SKU"); reasonCombobox.Items.Add("Cancelled at Merchant's Request"); reasonCombobox.Items.Add("Cannot fulfill the order in time"); reasonCombobox.Items.Add("Cannot Ship as Ordered"); reasonCombobox.Items.Add("Invalid Item Cost"); reasonCombobox.Items.Add("Merchant detected fraud"); reasonCombobox.Items.Add("Order missing information"); reasonCombobox.Items.Add("Out of Stock"); reasonCombobox.Items.Add("Product Has Been Discontinued"); reasonCombobox.Items.Add("Other"); reasonCombobox.SelectedIndex = 0; // ups details switch (value.ServiceLevel) { case "UPSN_3D": serviceCombobox.SelectedIndex = 2; break; case "UPSN_IX": serviceCombobox.SelectedIndex = 3; break; case "UPS": serviceCombobox.SelectedIndex = 1; break; default: serviceCombobox.SelectedIndex = 0; break; } // initialize field for sku detail -> [0] weight, [1] length, [2] width, [3] height decimal[] skuDetail = { 0, 0, 0, 0 }; // adding list to listview and getting sku detail for (int i = 0; i < value.LineCount; i++) { // add item to list ListViewItem item = new ListViewItem(value.MerchantLineNumber[i].ToString()); item.SubItems.Add(value.Description[i] + " SKU: " + value.TrxVendorSku[i]); item.SubItems.Add("$ " + value.UnitPrice[i]); item.SubItems.Add(value.TrxQty[i].ToString()); item.SubItems.Add("$ " + value.LineBalanceDue[i]); item.SubItems.Add(""); item.SubItems.Add(""); listview.Items.Add(item); // generate sku detail decimal[] detailList = Package.GetSkuDetail(value.TrxVendorSku[i]); // the case if bad sku if (detailList == null) { item.BackColor = Color.FromArgb(254, 126, 116); } else { for (int j = 0; j < 4; j++) { skuDetail[j] += detailList[j]; } } } // show result to shipping info weightKgUpdown.Value = skuDetail[0] / 1000; weightLbUpdown.Value = skuDetail[0] / 453.592m; lengthUpdown.Value = skuDetail[1]; widthUpdown.Value = skuDetail[2]; heightUpdown.Value = skuDetail[3]; // shipment status -> the case if the order has already shipped if (value.Package.TrackingNumber == "") { return; } createLabelButton.Enabled = false; trackingNumberTextbox.Text = value.Package.TrackingNumber; voidShipmentButton.Visible = true; #endregion }
/* a method that post shipment confirm request and return shipment digest and identification number*/ public string[] PostShipmentConfirm(SearsValues value) { // set error to false Error = false; // const string shipmentConfirmUri = "https://wwwcie.ups.com/ups.app/xml/ShipConfirm"; const string shipmentConfirmUri = "https://onlinetools.ups.com/ups.app/xml/ShipConfirm"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(shipmentConfirmUri); request.Method = "POST"; request.ContentType = "application/xml"; string textXml = "<?xml version=\"1.0\"?>" + "<AccessRequest xml:lang=\"en-US\">" + "<AccessLicenseNumber>" + ACCESS_LISCENSE_NUMBER + "</AccessLicenseNumber>" + "<UserId>" + USER_ID + "</UserId>" + "<Password>" + PASSWORD + "</Password>" + "</AccessRequest>" + "<?xml version=\"1.0\"?>" + "<ShipmentConfirmRequest xml:lang=\"en-U\">" + "<Request>" + "<RequestAction>ShipConfirm</RequestAction>" + "<RequestOption>validate</RequestOption>" + "</Request>" + "<Shipment>" + "<Shipper>" + "<Name>Ashlin BPG Marketing Inc</Name>" + "<PhoneNumber>9058553027</PhoneNumber>" + "<ShipperNumber>" + ACCOUNT_NUMBER + "</ShipperNumber>" + "<Address>" + "<AddressLine1>2351 Royal Windsor Dr</AddressLine1>" + "<City>Mississauga</City>" + "<StateProvinceCode>ON</StateProvinceCode>" + "<PostalCode>L5J4S7</PostalCode>" + "<CountryCode>CA</CountryCode>" + "</Address>" + "</Shipper>" + "<ShipTo>" + "<CompanyName>" + value.Recipient.Name + "</CompanyName>" + "<PhoneNumber>" + value.ShipTo.DayPhone + "</PhoneNumber>" + "<Address>" + "<AddressLine1>" + value.ShipTo.Address1 + "</AddressLine1>"; if (value.ShipTo.Address2 != "") { textXml += "<AddressLine2>" + value.ShipTo.Address2 + "</AddressLine2>"; } textXml += "<City>" + value.ShipTo.City + "</City>" + "<StateProvinceCode>" + value.ShipTo.State + "</StateProvinceCode>" + "<PostalCode>" + value.ShipTo.PostalCode + "</PostalCode>" + "<CountryCode>CA</CountryCode>" + "</Address>" + "</ShipTo>" + "<PaymentInformation>" + "<BillThirdParty>" + "<BillThirdPartyShipper>" + "<AccountNumber>" + SEARS_ACCOUNT_NUMBER + "</AccountNumber>" + "<ThirdParty>" + "<Address>" + "<PostalCode>L5J4S7</PostalCode>" + "<CountryCode>CA</CountryCode>" + "</Address>" + "</ThirdParty>" + "</BillThirdPartyShipper>" + "</BillThirdParty>" + "</PaymentInformation>" + "<Service>"; string code; switch (value.Package.Service) { case "UPS Standard": code = "11"; break; case "UPS 3 Day Select": code = "12"; break; case "UPS Worldwide Express": code = "07"; break; default: code = "01"; break; } textXml += "<Code>" + code + "</Code>" + "</Service>" + "<Package>"; switch (value.Package.PackageType) { case "Letter": code = "01"; break; case "Express Box": code = "21"; break; case "First Class": code = "59"; break; default: code = "02"; break; } textXml += "<PackagingType>" + "<Code>" + code + "</Code>" + "</PackagingType>" + "<Dimensions>" + "<UnitOfMeasurement>" + "<Code>CM</Code>" + "</UnitOfMeasurement>" + "<Length>" + value.Package.Length + "</Length>" + "<Width>" + value.Package.Width + "</Width>" + "<Height>" + value.Package.Height + "</Height>" + "</Dimensions>" + "<PackageWeight>" + "<UnitOfMeasurement>" + "<Code>KGS</Code>" + "</UnitOfMeasurement>" + "<Weight>" + value.Package.Weight + "</Weight>" + "</PackageWeight>" + "</Package>" + "</Shipment>" + "<LabelSpecification>" + "<LabelPrintMethod>" + "<Code>GIF</Code>" + "</LabelPrintMethod>" + "<LabelImageFormat>" + "<Code>GIF</Code>" + "</LabelImageFormat>" + "</LabelSpecification>" + "</ShipmentConfirmRequest>"; // turn request string into a byte stream byte[] postBytes = Encoding.UTF8.GetBytes(textXml); // send request using (Stream requestStream = request.GetRequestStream()) requestStream.Write(postBytes, 0, postBytes.Length); // get the response from the server HttpWebResponse response; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; try { response = (HttpWebResponse)request.GetResponse(); } catch { // the case if server error -> set error indication Error = true; ErrorMessage = "Server Internal Error"; return(null); } string result; using (StreamReader streamReader = new StreamReader(response.GetResponseStream())) result = streamReader.ReadToEnd(); // read xml doc.LoadXml(result); // get status code XmlNode node = doc.SelectSingleNode("/ShipmentConfirmResponse/Response"); string responseStatus = node["ResponseStatusCode"].InnerText; // get identification number and shipment digest string[] returnString = new string[2]; if (responseStatus == "1") { node = doc.SelectSingleNode("/ShipmentConfirmResponse"); returnString[0] = node["ShipmentIdentificationNumber"].InnerText; returnString[1] = node["ShipmentDigest"].InnerText; } else { // the case if bad request -> set error indication Error = true; ErrorMessage = "Error: " + node["Error"]["ErrorDescription"].InnerText; return(null); } return(returnString); }
/* a method that post sears order to brightpearl on sears account */ public void PostOrder(SearsValues value, int[] cancelList) { // check if the order is cancelled entirely -> if it is just return no need to post it if (cancelList.Length >= value.LineCount) { return; } #region Posting Order to Sears Account on BP // initialize order BPvalues object BPvalues orderValue = new BPvalues(value.Recipient, value.CustOrderNumber, value.CustOrderDate, 1, 7, null, null, 0, 0, 0, 0); // post order string orderId = post.PostOrderRequest("2854", orderValue); Status = "Getting order ID - Sears"; if (post.HasError) { Status = "Error occur during order post - Sears"; do { Thread.Sleep(5000); orderId = post.PostOrderRequest("2854", orderValue); } while (post.HasError); } // calculate the total amount when excluding the cancelled items for (int i = 0; i < value.LineCount; i++) { // the case if not cancel post it to brightpearl if (cancelList.Where(j => j == i).Any()) { continue; } #region Tax Determination double tax; switch (value.ShipTo.State) { case "NB": tax = 0.13; break; case "NF": tax = 0.15; break; case "NL": tax = 0.15; break; case "NS": tax = 0.15; break; case "ON": tax = 0.13; break; case "PEI": tax = 0.14; break; case "BC": tax = 0.05; break; case "MAN": tax = 0.05; break; case "PQ": tax = 0.05; break; case "QC": tax = 0.05; break; case "SK": tax = 0.05; break; case "AB": tax = 0.05; break; case "NV": tax = 0.05; break; case "YK": tax = 0.05; break; default: tax = 0; break; } #endregion // initialize BPvalues object -> no need total paid ( this is unit cost & no recipt ) double totalUnitCost = value.TrxUnitCost[i] * value.TrxQty[i]; BPvalues itemValue = new BPvalues(value.Recipient, null, DateTime.Today, 1, 7, value.TrxVendorSku[i], value.Description[i], value.TrxQty[i], totalUnitCost, totalUnitCost * tax, 0); // post order row string orderRowId = post.PostOrderRowRequest(orderId, itemValue); Status = "Getting order row ID"; if (post.HasError) { Status = "Error occur during order row post " + i + " - Sears"; do { Thread.Sleep(5000); orderRowId = post.PostOrderRowRequest(orderId, itemValue); } while (post.HasError); } // post reservation post.PostReservationRequest(orderId, orderRowId, itemValue); Status = "Posting reservation request " + i; if (!post.HasError) { continue; } Status = "Error occur during reservation post " + i + " - Sears"; do { Thread.Sleep(5000); post.PostReservationRequest(orderId, orderRowId, itemValue); } while (post.HasError); } #endregion }