/* second constructor that show shop.ca order */ public DetailPage(ShopCaValues value) { InitializeComponent(); shopCaValues = value; ShowResult(value); // set flag to shop.ca CHANNEL = "Shop.ca"; }
/* third constructor that take ShopCaValues object as parameter */ public Package(ShopCaValues value) { // generate package detail -> weight and dimensions decimal[] skuDetail = { 0, 0, 0, 0 }; foreach (decimal[] detailList in value.Sku.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]; // service is set to default Service = "Expedited Parcel"; SelfLink = ""; LabelLink = ""; }
/* 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 ShopCaValues object */ private void ShowResult(ShopCaValues value) { // title bar set up logoPicturebox.Image = Properties.Resources.shopca; topOrderNumberTextbox.Text = value.OrderId; #region Order Summary // date orderDateTextbox.Text = value.OrderCreateDate.ToString("MM/dd/yyyy"); paidDateTextbox.Text = value.OrderCreateDate.ToString("MM/dd/yyyy"); shipByDateTextbox.Text = DateTime.Today.ToString("MM/dd/yyyy"); // unit price unitPriceTotalTextbox.Text = value.TotalPrice.ToString(CultureInfo.InvariantCulture); // GST and HST gsthstTextbox.Text = value.TotalTax.ToString(CultureInfo.InvariantCulture); // PST pstTextbox.Text = "0.00"; // other fee otherFeeTextbox.Text = (value.ItemShippingCost.Sum() - value.ItemDiscount.Sum()).ToString(CultureInfo.InvariantCulture); // total totalOrderTextbox.Text = value.GrandTotal.ToString(CultureInfo.InvariantCulture); #endregion #region Buyer / Recipient Info // sold to soldToTextbox.Text = value.BillTo.Name; soldToPhoneTextbox.Text = value.BillTo.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("Expedited Parcel"); serviceCombobox.Items.Add("Regular Parcel"); serviceCombobox.Items.Add("Xpresspost"); serviceCombobox.Items.Add("Priority"); serviceCombobox.SelectedIndex = 0; // adding items to reason combobox reasonCombobox.Items.Clear(); reasonCombobox.Items.Add("Select Reason"); reasonCombobox.Items.Add("Carrier cannot deliver"); reasonCombobox.Items.Add("Discontinued"); reasonCombobox.Items.Add("Incomplete Address"); reasonCombobox.Items.Add("Out of delivery area"); reasonCombobox.Items.Add("Out of Stock"); reasonCombobox.Items.Add("Other"); reasonCombobox.SelectedIndex = 0; // 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.OrderItemId.Count; i++) { // add item to list ListViewItem item = new ListViewItem(value.OrderItemId[i]); item.SubItems.Add(value.Title[i] + " SKU: " + value.Sku[i]); item.SubItems.Add("$ " + value.ItemPrice[i]); item.SubItems.Add(value.Quantity[i].ToString()); item.SubItems.Add("$ " + value.ExtendedItemPrice[i]); item.SubItems.Add(""); item.SubItems.Add(""); listview.Items.Add(item); // generate sku detail decimal[] detailList = Package.GetSkuDetail(value.Sku[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 shop.ca order to brightpearl on shop.ca account */ public void PostOrder(ShopCaValues value, int[] cancelList) { // check if the order is cancelled entirely -> if it is just return no need to post it if (cancelList.Length >= value.OrderItemId.Count) { return; } #region Posting Order to Shop.ca Account on BP // initialize order BPvalues object BPvalues orderValue = new BPvalues(value.ShipTo, value.OrderId, value.OrderCreateDate, 15, 1, null, null, 0, 0, 0, 0); // post order string orderId = post.PostOrderRequest("2897", orderValue); Status = "Getting order ID"; if (post.HasError) { Status = "Error occur during order post - Shop.ca"; do { Thread.Sleep(5000); orderId = post.PostOrderRequest("2897", orderValue); } while (post.HasError); } // calculate the total amount when excluding the cancelled items for (int i = 0; i < value.OrderItemId.Count; i++) { // the case if not cancel post it to brightpearl if (cancelList.Where(j => j == i).Any()) { continue; } // initialize BPvalues object BPvalues itemValue = new BPvalues(value.ShipTo, null, DateTime.Today, 15, 1, value.Sku[i], value.Title[i], value.Quantity[i], (double)value.ExtendedItemPrice[i], (double)value.ItemTax[i], 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 + " - Shop.ca"; 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 + " - Shop.ca"; do { Thread.Sleep(5000); post.PostReservationRequest(orderId, orderRowId, itemValue); } while (post.HasError); } #endregion }