using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Controls_CartControl : WebBlockUserControl
{
public string ErrorMessage = null;
public bool RefreshWholePage = false;
public bool IsEditable = true;
private bool ignoreInventory
{
get { return Configuration.Get("IgnoreInventory") == "Yes" ? true : false; }
}
protected void Page_Load(object sender, EventArgs e)
{
litInventoryCheckError.Text = string.Empty;
if (!Page.IsPostBack)
{
BindData();
if (!UpdateQuantities())
{
litInventoryCheckError.Text = ErrorMessage;
ForceRebind();
}
}
BindDiscounts();
// redirect user to cart if there's no items in there to prevent placing an order with 0 items
if (ShoppingCart.Count <= 0 && !Request.Url.ToString().Contains("Cart.aspx"))
{
Response.Redirect("Cart.aspx");
}
}
public void ForceRebind()
{
BindData();
BindDiscounts();
}
private void BindDiscounts()
{
if (ShoppingCart.DiscountList != null)
{
rptDiscounts.DataSource = ShoppingCart.DiscountList;
rptDiscounts.DataBind();
}
}
private void BindData()
{
rptShoppingCart.DataSource = ShoppingCart;
rptShoppingCart.DataBind();
}
protected void rptDiscounts_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "del")
{
string DiscountCode = (string)e.CommandArgument;
ShoppingCart.DiscountList.RemoveByDiscountCode(DiscountCode);
BindDiscounts();
}
}
protected void rptShoppingCart_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("Delete"))
{
ShoppingCart.Remove(Int32.Parse((string)e.CommandArgument));
BindData();
ShoppingCart.RunDiscountIfPresent();
}
}
public bool UpdateQuantities()
{
ErrorMessage = "";
for (int i = rptShoppingCart.Items.Count - 1; i >= 0; i--)
{
// need to go backwards to handle the last item first
RepeaterItem ri = rptShoppingCart.Items[i];
TextBox txtQuantity = (TextBox)ri.FindControl("txtQuantity");
int quantity = 0;
try { quantity = Int32.Parse(txtQuantity.Text); }
catch { quantity = 0; }
int CartItemID = Int32.Parse(((ImageButton)ri.FindControl("btnRemove")).CommandArgument);
if (quantity > 0)
{
// grab the shopping cart item
ShoppingCartItem sci = ShoppingCart.FindByCartItemID(CartItemID);
// now grab all the shipping cart items with this itemid (check for items with customizations)
ArrayList alItems = ShoppingCart.FindItemsWithItemID(sci.ItemID);
// grab the Item with this ItemID
DataRow dr = Database.Items_GetByItemIDAndQuantity(sci.ItemID, quantity);
int Inventory = (int)dr["Inventory"];
// there's another item in cart with the same itemID. We want to add these quantities together since they pull from the same Inventory
// I copied everything in this if statement because I'm not sure what'll break if I don't.
if (alItems.Count > 1)
{
int cartQuantity = 0;
foreach (ShoppingCartItem sci2 in alItems)
{
foreach (RepeaterItem rptItem in rptShoppingCart.Items)
{
if(Int32.Parse(((ImageButton)rptItem.FindControl("btnRemove")).CommandArgument) == sci2.CartItemID)
cartQuantity += Int32.Parse(((TextBox)rptItem.FindControl("txtQuantity")).Text);
}
}
// check if there any items in stock
if (ignoreInventory || Inventory == - 1)
{
// ignore incventory
// we can update the quantities now
// to get the price, we can just check to see if the "Price" column
// returned a non-null value, and use that
// otherwise, use the item price
decimal Price = (dr["VolumePrice"] == DBNull.Value ? (decimal)dr["ItemPrice"] : (decimal)dr["VolumePrice"]);
// finally, update the quantities
ShoppingCart.UpdateItemQuantity(sci, quantity, Price);
}
else if (Inventory <= 0)
{
ShoppingCart.Remove(sci);
ErrorMessage += sci.Name + " has gone out of stock while you were shopping. It has been removed from your cart.
";
}
// item is in stock but the user entered a quantity that was more than avialable
else if (Inventory > 0 && Inventory < cartQuantity)
{
txtQuantity.Text = Inventory.ToString();
ShoppingCart.Remove(sci);
ErrorMessage += "The quantity you have requested for " + sci.Name + (sci.CustomizationText == string.Empty ? "" : "
" + sci.CustomizationText) + " exceeds what is currently in stock.
";
}
else
{
// we can update the quantities now
// to get the price, we can just check to see if the "Price" column
// returned a non-null value, and use that
// otherwise, use the item price
decimal Price = (dr["VolumePrice"] == DBNull.Value ? (decimal)dr["ItemPrice"] : (decimal)dr["VolumePrice"]);
// finally, update the quantities
ShoppingCart.UpdateItemQuantity(sci, quantity, Price);
}
}
else
{
// check if there any items in stock
if (ignoreInventory || Inventory == -1)
{
// ignore inventoryconfir
// we can update the quantities now
// to get the price, we can just check to see if the "Price" column
// returned a non-null value, and use that
// otherwise, use the item price
decimal Price = (dr["VolumePrice"] == DBNull.Value ? (decimal)dr["ItemPrice"] : (decimal)dr["VolumePrice"]);
// finally, update the quantities
ShoppingCart.UpdateItemQuantity(sci, quantity, Price);
}
else if (Inventory <= 0)
{
ShoppingCart.Remove(sci);
ErrorMessage += sci.Name + " has gone out of stock while you were shopping. It has been removed from your cart.
";
}
// item is in stock but the user entered a quantity that was more than avialable
else if (Inventory > 0 && Inventory < quantity)
{
txtQuantity.Text = Inventory.ToString();
sci.Quantity = Inventory;
ErrorMessage += "The quantity you have requested for " + sci.Name + " exceeds what is currently in stock. The quantity box has been updated to reflect the maximum quantity available.
";
}
else
{
// we can update the quantities now
// to get the price, we can just check to see if the "Price" column
// returned a non-null value, and use that
// otherwise, use the item price
decimal Price = (dr["VolumePrice"] == DBNull.Value ? (decimal)dr["ItemPrice"] : (decimal)dr["VolumePrice"]);
// finally, update the quantities
ShoppingCart.UpdateItemQuantity(sci, quantity, Price);
}
}
}
}
if (ErrorMessage.Length > 0)
return false;
else
{
ErrorMessage = null;
return true;
}
}
protected void lbUpdate_Click(object sender, EventArgs e)
{
if (!UpdateQuantities())
litInventoryCheckError.Text = ErrorMessage;
ShoppingCart.RunDiscountIfPresent();
BindData();
if (RefreshWholePage)
Response.Redirect(Request.RawUrl);
}
}