//TODO: do we need to do this??? OR should it be in the other screen??? protected void SaveChanges() { //since addresses might have changed, we need to make sure the tax/shipping prices got updated Mediachase.Commerce.Orders.Cart cart = this.CheckoutCart; NWTD.Orders.Cart.AssignTotals(ref cart); this.CheckoutCart.AcceptChanges(); }
/// <summary> /// Event handler for carts getting bound to the carts grid. /// During this process, bind the line item to the sub grid that contains some controls for editing the cart /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void rptrMyCarts_ItemDataBound(object sender, RepeaterItemEventArgs e) { GridView gvMyCart = e.Item.FindControl("gvMyCart") as GridView; Mediachase.Commerce.Orders.Cart cart = e.Item.DataItem as Mediachase.Commerce.Orders.Cart; LinkButton makeActive = e.Item.FindControl("lbMakeActive") as LinkButton; if (cart.Name == NWTD.Profile.ActiveCart) { HyperLink viewOrder = e.Item.FindControl("linkViewOrder") as HyperLink; HtmlGenericControl header = e.Item.FindControl("CartHeading") as HtmlGenericControl; header.Attributes["class"] = "selected"; viewOrder.Visible = true; makeActive.Visible = false; } else { //makeActive.Click += new EventHandler(makeActive_Click); } CartHelper helper = new CartHelper(cart); gvMyCart.DataSource = helper.LineItems; gvMyCart.DataBind(); }
/// <summary> /// Generates an Order number from the cart following NWTDs custom conventions /// </summary> /// <param name="cart"></param> /// <returns></returns> public static string GenerateOrderNumber(Mediachase.Commerce.Orders.Cart cart) { //string num = new Random().Next(100, 999).ToString(); //return String.Format("{0}{1}{2}", NWTD.Profile.CustomerDepository.ToString(), cart.OrderGroupId, num); //Replaced above two lines with the following logic for shorter WebConfirmation Number (Heath Gardner 08/19/13) //Get current customer's depository (e.g. NWTD or MSSD) string custDepo = NWTD.Profile.CustomerDepository.ToString(); //Convert customer's depository into two character string (e.g. NW, MS, or ZZ for unknowns) if (custDepo == "NWTD") { custDepo = "NW"; } else if (custDepo == "MSSD") { custDepo = "MS"; } else { custDepo = "ZZ"; } //Build and return the WebConfirmation Number return(String.Format("{0}{1}", custDepo, cart.OrderGroupId)); }
/// <summary> /// Assigns totals to a cart. /// </summary> /// <param name="Cart"></param> public static void AssignTotals(ref Mediachase.Commerce.Orders.Cart Cart) { Cart.SubTotal = CartTotal(Cart, false, false); Cart.ShippingTotal = CartShippingCharge(Cart); Cart.TaxTotal = CartTax(Cart); Cart.Total = Cart.SubTotal + Cart.ShippingTotal + Cart.TaxTotal; }
/// <summary> /// Calculate the shipping charge based on the cart's owner's organization /// </summary> /// <param name="cart">The cart being examined</param> /// <returns></returns> public static decimal CartShippingCharge(Mediachase.Commerce.Orders.Cart cart) { Account account = Mediachase.Commerce.Profile.Account.LoadByPrincipalId(cart.CustomerId); decimal shipRate = 0m; decimal shipmentMinimumCharge = 0m; decimal shipmentFlatRate = 0m; if (account.Organization != null) { //error check just in case the meta fields don't exist try { decimal.TryParse(account.Organization["ShipRate"].ToString(), out shipRate); decimal.TryParse(account.Organization["ShipMinCharge"].ToString(), out shipmentMinimumCharge); decimal.TryParse(account.Organization["ShipFlatCharge"].ToString(), out shipmentFlatRate); } catch (Exception ex) { return(0m); } } if (shipmentFlatRate > 0) { return(shipmentFlatRate * CartTotal(cart)); } decimal total = 0m; total = shipRate * CartTotal(cart); if (total < shipmentMinimumCharge) { total = shipmentMinimumCharge; } return(total); }
/// <summary> /// Finds out if the name is available for a new cart /// </summary> /// <param name="UserId">The ID of the user for whom the cart would be created</param> /// <param name="CartName">The name of the cart to check</param> /// <returns>True if the name is availalbe, False if it is not.</returns> public static bool CartNameIsDuplicate(Guid UserId, string CartName) { Mediachase.Commerce.Orders.Cart test = Mediachase.Commerce.Orders.Cart.LoadByCustomerAndName(UserId, CartName); if (test != null) { //uh oh, there's already a cart by this name! return(true); } return(false); }
/// <summary> /// Copy notes from cart to purchse order /// </summary> /// <param name="purchaseOrder"></param> /// <param name="cart"></param> private void CopyNotesFromCartToPurchaseOrder(PurchaseOrder purchaseOrder, Mediachase.Commerce.Orders.Cart cart) { foreach (var note in cart.OrderNotes.OrderByDescending(n => n.Created)) { OrderNote on = purchaseOrder.OrderNotes.AddNew(); on.Detail = note.Detail; on.Title = note.Title; on.Type = OrderNoteTypes.System.ToString(); on.Created = note.Created; on.CustomerId = note.CustomerId; } purchaseOrder.AcceptChanges(); }
/// <summary> /// Searches a cart's line items for a shipping address. Returns the first address found. /// </summary> /// <param name="cart">The cart being examined</param> /// <returns></returns> public static OrderAddress FindCartShippingAddress(Mediachase.Commerce.Orders.Cart cart) { CartHelper helper = new CartHelper(cart); foreach (LineItem lineItem in helper.LineItems) { if (!String.IsNullOrEmpty(lineItem.ShippingAddressId)) { OrderAddress address = helper.FindAddressByName(lineItem.ShippingAddressId); if (address != null) { return(address); } } } return(null); }
/// <summary> /// Creates a new cart for the supplied customer with the supplied name. /// This method does some additional things beyond ECF's cart creation code, /// so it's very importand that NWTD carts are created this way. /// </summary> /// <param name="Account"></param> /// <param name="CartName"></param> /// <returns></returns> public static Mediachase.Commerce.Orders.Cart CreateCart(Account Account, String CartName) { if (Mediachase.Commerce.Orders.Cart.LoadByCustomerAndName(Account.PrincipalId, CartName) != null) { throw new Exception("A cart for this customer with this name already exists"); } //create the cart Mediachase.Commerce.Orders.Cart cartToAdd = Mediachase.Commerce.Orders.OrderContext.Current.GetCart(CartName, Account.PrincipalId); cartToAdd.CustomerName = Mediachase.Commerce.Profile.Account.LoadByPrincipalId(Account.PrincipalId).Name; cartToAdd.OrderForms.Add(new Mediachase.Commerce.Orders.OrderForm() { Name = CartName }); //We need to give it a name. So we can call the GetOrderForm mehod on the ECF's CartHelper class and actually return somethiung cartToAdd.Status = NWTD.Orders.Cart.CART_STATUS.OPEN.ToString(); cartToAdd.AcceptChanges(); return(cartToAdd); }
/// <summary> /// Calculates the total of a cart, including and gratis /// </summary> /// <param name="cart">The cart for which the total is being calculated</param> /// <param name="IncludeTax">Whether to include tax in the caculation</param> /// <param name="IncludeShipping">Whether to include shipping in the calculation</param> /// <returns></returns> public static decimal CartTotal(Mediachase.Commerce.Orders.Cart cart, bool IncludeTax, bool IncludeShipping) { decimal total = 0m; CartHelper helper = new CartHelper(cart); foreach (LineItem lineItem in helper.LineItems) { total += Cart.LineItemTotal(lineItem); } if (IncludeShipping) { total += CartShippingCharge(cart); } if (IncludeTax) { total += (CartTax(cart)); } return(total); }
/// <summary> /// Calculates the estimated tax of the cart, based on the first line item found that has a shipping address /// </summary> /// <param name="cart">The cart for which the tax is being estimated</param> /// <returns></returns> public static decimal CartTax(Mediachase.Commerce.Orders.Cart cart) { CartHelper helper = new CartHelper(cart); OrderAddress shippingAddress = FindCartShippingAddress(cart); decimal taxRate = 0m; bool isFreightTaxable = true; Account account = Mediachase.Commerce.Profile.Account.LoadByPrincipalId(helper.Cart.CustomerId); if (account.Organization.GetBool("IsTaxExempt")) { return(0m); } //error check just in case the meta fields don't exist try { decimal.TryParse(shippingAddress["TaxRate"].ToString(), out taxRate); bool.TryParse(shippingAddress["IsFreightTaxable"].ToString(), out isFreightTaxable); } catch (Exception ex) { return(0m); } return(CartTotal(cart, false, isFreightTaxable) * (taxRate / 100)); }
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)] //the entire parameter must be serialized to a string (no individual raw POST values) public CartResponse Update(string cartName, string newName) { Guid userId = ProfileContext.Current.UserId; var response = new CartResponse(); Mediachase.Commerce.Orders.Cart test = Mediachase.Commerce.Orders.Cart.LoadByCustomerAndName(userId, newName); if (!CartNameIsValid(newName)) { response.Message = "Invalid Wish List name! The Wish List name may only contain letters, numbers, spaces, and underscores"; response.Status = CartResponseStatus.ERROR; } else if (CartNameIsDuplicate(userId, newName)) { //uh oh, there's already a cart by this name! response.Status = CartResponseStatus.ERROR; response.Message = string.Format("A Wish List with the name {0} already exists", newName); } else { CartHelper helper = new CartHelper(cartName, userId); helper.Cart.Name = newName; if (helper.Cart.OrderForms[cartName] != null) { OrderForm childForm = helper.Cart.OrderForms[cartName]; childForm.Name = newName; childForm.AcceptChanges(); } helper.Cart.AcceptChanges(); if (NWTD.Profile.ActiveCart.Equals(cartName)) { NWTD.Profile.ActiveCart = newName; //if it's the active cart, we need to update that information in state } response.Message = "Successfully updated the Wish List."; } return(response); }
public static LineItem GetLineItem(this Mediachase.Commerce.Orders.Cart cart, string code) { return(cart.GetAllLineItems().FirstOrDefault(x => x.Code == code)); }
public static IReadOnlyCollection <LineItem> GetAllLineItems(this Mediachase.Commerce.Orders.Cart cart) { return(cart.OrderForms.Any() ? cart.OrderForms.First().LineItems.ToList() : new List <LineItem>()); }
/// <summary> /// Calculates the subtotal of a cart, including and gratis /// </summary> /// <param name="cart">The cart for which the total is being calculated</param> /// <returns></returns> public static decimal CartTotal(Mediachase.Commerce.Orders.Cart cart) { return(CartTotal(cart, false, false)); }
/// <summary> /// Finds out whether a cart can be edited (submitted carts can't). /// </summary> /// <param name="Cart">The cart to investigate</param> /// <returns></returns> public static bool CartCanBeEdited(Mediachase.Commerce.Orders.Cart Cart) { return(Cart.Status.ToString().Equals(NWTD.Orders.Cart.CART_STATUS.OPEN.ToString())); }
/// <summary> /// Saves the changes made to the cart being viewed /// </summary> protected bool SaveChanges() { CartHelper helper = this.SelectedCartHelper; int index = 0; foreach (LineItem item in helper.LineItems) { TextBox tbGratis = gvCart.Rows[index].FindControl("tbGratis") as TextBox; TextBox tbQuantityCharged = gvCart.Rows[index].FindControl("tbQuantityCharged") as TextBox; var gratisValidator = gvCart.Rows[index].FindControl("lblGratisValidator") as Label; var qtyValidator = gvCart.Rows[index].FindControl("lblQtyValidator") as Label; if (string.IsNullOrEmpty(tbGratis.Text.Trim())) { gratisValidator.Visible = true; return(false); } if (string.IsNullOrEmpty(tbQuantityCharged.Text.Trim())) { qtyValidator.Visible = true; return(false); } decimal newGratis = 0; decimal.TryParse(tbGratis.Text.Trim(), out newGratis); decimal newQuantityCharged = 0; decimal.TryParse(tbQuantityCharged.Text.Trim(), out newQuantityCharged); decimal newQty = newGratis + newQuantityCharged; ///TODO: We should be ensuring that there is a "Gratis" meta field before casting it if ((decimal?)item["Gratis"] != newGratis) { item["Gratis"] = newGratis; } if (newQty <= 0) // remove //item.Delete(); { item.Quantity = newQty; } else if (newQty != item.Quantity) // update { item.Quantity = newQty; } index++; } helper.RunWorkflow("CartValidate"); Mediachase.Commerce.Orders.Cart cart = helper.Cart; NWTD.Orders.Cart.AssignTotals(ref cart); helper.Cart.AcceptChanges(); lblCartMessage.Text = "Your changes have been saved"; return(true); }
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)] //the entire parameter must be serialized to a string (no individual raw POST values) public CartResponse Copy(string cartName, string newName, bool activate) { var response = new CartResponse(); //the response for the operation if (string.IsNullOrEmpty(newName)) { newName = string.Format("Copy of {0}", cartName); } Guid userId = ProfileContext.Current.UserId; //make sure there isn't already a cart with the same name if (!CartNameIsValid(newName)) { response.Message = "Invalid Wish List name! The Wish List name may only contain letters, numbers, spaces, and underscores"; response.Status = CartResponseStatus.ERROR; } else if (CartNameIsDuplicate(userId, newName)) { response.Message = "Invalid Wish List name! There is already a Wish List with this name"; response.Status = CartResponseStatus.ERROR; } else { CartHelper originalCartHelper = new CartHelper(cartName, userId); //create the new cart Mediachase.Commerce.Orders.Cart cartToAdd = NWTD.Orders.Cart.CreateCart(ProfileContext.Current.Profile.Account, newName); //now, we'll need a CartHelper to start adding the lines CartHelper newCartHelper = new CartHelper(cartToAdd); //now add all the same line items to the new cart, copying any relevant metadata (gratis and quantity) foreach (LineItem lineItem in originalCartHelper.LineItems) { //get the entry Entry entry = CatalogContext.Current.GetCatalogEntry(lineItem.CatalogEntryId, new Mediachase.Commerce.Catalog.Managers.CatalogEntryResponseGroup(CatalogEntryResponseGroup.ResponseGroup.CatalogEntryFull)); if (entry != null) { newCartHelper.AddEntry(entry, lineItem.Quantity); } //get the item we just added and set its gratis LineItem addedItem = newCartHelper.LineItems.Single(li => li.CatalogEntryId == entry.ID); addedItem["Gratis"] = lineItem["Gratis"]; //addedItem.ShippingAddressId = lineItem.ShippingAddressId; newCartHelper.RunWorkflow("CartValidate"); cartToAdd.AcceptChanges(); } //save the changes cartToAdd.AcceptChanges(); if (activate) { NWTD.Profile.ActiveCart = cartToAdd.Name; response.Message = "Wish List succesfully copied and made active"; } else { response.Message = "Wish List succesfully copied"; } } return(response); }