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.SessionState;
using System.Web.Caching;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class SimpleCart:System.Web.UI.Page
{
private int _intTotalItem;
private double _dblNetPrice;
private System.Data.DataTable _dtCart;
///<summary>
///Author : Biswabed Dash
///Name : TotalItem
///Return Value : int
///Purpose : This field exposes Total number of items added by customer to the cart.
///Created Date : 25/10/2006
/// </summary>
public int TotalItem
{
get
{
return _intTotalItem;
}
set
{
_intTotalItem = value;
}
}
///<summary>
///Author : Biswabed Dash
///Name : NetPrice
///Return Value : double
///Purpose : This field exposes the Net Order Price for the customer.
///Created Date : 25/10/2006
/// </summary>
public double NetPrice
{
get
{
return _dblNetPrice;
}
set
{
_dblNetPrice = value;
}
}
///<summary>
///Author : Biswabed Dash
///Name : AddedProductList
///Return Value : DataTable
///Purpose : This field exposes the product list with details added by the customer to the cart.
///Created Date : 25/10/2006
/// </summary>
public System.Data.DataTable AddedProductList
{
get
{
return _dtCart;
}
set
{
_dtCart = value;
}
}
///<summary>
///Author : Biswabed Dash
///Name : MakeCart
///Return Value : void
///Purpose : MakeCart Function build the the cart stucture.
///Created Date : 25/10/2006
///</summary>
private void MakeCart()
{
TotalItem = 0;
NetPrice = 0.0;
_dtCart = new System.Data.DataTable();
_dtCart.Columns.Add(new DataColumn("ProductCode", typeof(string)));
_dtCart.Columns.Add(new DataColumn("ProductName", typeof(string)));
_dtCart.Columns.Add(new DataColumn("Quantity", typeof(int)));
_dtCart.Columns.Add(new DataColumn("UnitPrice", typeof(Double)));
_dtCart.Columns.Add(new DataColumn("Total", typeof(Double)));
}
///<summary>
///Author : Biswabed Dash
///Name : AddToCart
///Return Value : void
///Purpose : AddToCart Function enables Customer to add a Product to the Cart.
///Created Date : 25/10/2006
///</summary>
///<param name="pProductCode">Product Code that customer wants to add to the cart</param>
///<param name="pProductName">Product Name that customer wants to add to the cart</param>
///<param name="pQuantity">Numbers of items customer wants for the selected product</param>
///<param name="pUnitPrice">Unit Price for the selected Product </param>
public void AddToCart(string pProductCode, string pProductName, int pQuantity, double pUnitPrice)
{
if (Session["Cart"] != null)
{
SimpleCart obj = (SimpleCart)Session["Cart"];
DataRow[] rows = obj._dtCart.Select("ProductCode='" + pProductCode + "'");
if (rows.Length == 0)
{
System.Data.DataRow objDR = obj._dtCart.NewRow();
objDR["ProductCode"] = pProductCode;
objDR["ProductName"] = pProductName;
objDR["Quantity"] = pQuantity;
objDR["UnitPrice"] = pUnitPrice;
objDR["Total"] = pQuantity * pUnitPrice;
obj._dtCart.Rows.Add(objDR);
obj.TotalItem += (int)objDR["Quantity"];
obj.NetPrice += (double)objDR["Total"];
}
else
{
DataRow row = (DataRow)rows[0];
row["Quantity"] = Convert.ToInt32(row["Quantity"])+ pQuantity;
row["Total"] = Convert.ToInt32(row["Quantity"]) * Convert.ToDouble( row["UnitPrice"]);
row.AcceptChanges();
obj.TotalItem += pQuantity;
obj.NetPrice += pQuantity * pUnitPrice;
}
Session["Cart"] = obj;
}
else
{
MakeCart();
System.Data.DataRow objDR = _dtCart.NewRow();
objDR["ProductCode"] = pProductCode;
objDR["ProductName"] = pProductName;
objDR["Quantity"] = pQuantity;
objDR["UnitPrice"] = pUnitPrice;
objDR["Total"] = pQuantity * pUnitPrice;
_dtCart.Rows.Add(objDR);
TotalItem += (int)objDR["Quantity"];
NetPrice += (double)objDR["Total"];
Session["Cart"] = this;
}
}
///<summary>
///Author : Biswabed Dash
///Name : DeleteFromCart
///Return Value : void
///Purpose : DeleteFromCart Function removes a Product which Product Code provideed as Parameter from the Cart.
///Created Date : 25/10/2006
///</summary>
///<param name="pProductCode">Product Code that customer wants to remove from the cart</param>
public void DeleteFromCart(string pProductCode)
{
if (Session["Cart"] != null)
{
SimpleCartobj = (SimpleCart)Session["Cart"];
DataRow[] rows = obj._dtCart.Select("ProductCode='" + pProductCode + "'");
DataRow row = (DataRow)rows[0];
obj.TotalItem -= (int)row["Quantity"];
obj.NetPrice -= (double)row["Total"];
row.Delete();
Session["Cart"] = obj;
}
}
///<summary>
///Author : Biswabed Dash
///Name : UpdateCart
///Return Value : void
///Purpose : DeleteFromCart Function removes a Product which Product Code provideed as Parameter from the Cart.
///Created Date : 25/10/2006
///</summary>
///<param name="pProductCode">Product Code that customer wants to remove from the cart</param>
///<param name="pQuantity">Quantity need to be Updated by the customer for the Product whose code Passed as first Parameter.</param>
public void UpdateCart(string pProductCode,int pQuantity)
{
SimpleCart obj = (SimpleCart)Session["Cart"];
obj.NetPrice = 0.0;
obj.TotalItem = 0;
for (int rowcnt = 0; rowcnt <= obj._dtCart.Rows.Count - 1; rowcnt++)
{
string str = (string)obj._dtCart.Rows[rowcnt]["ProductCode"];
if (str == pProductCode)
{
obj._dtCart.Rows[rowcnt]["Quantity"] = pQuantity;
obj._dtCart.Rows[rowcnt]["Total"] = pQuantity * Convert.ToDouble(obj._dtCart.Rows[rowcnt]["UnitPrice"]);
obj._dtCart.Rows[rowcnt].AcceptChanges();
}
obj.TotalItem += (int)obj._dtCart.Rows[rowcnt]["Quantity"];
obj.NetPrice += (double)obj._dtCart.Rows[rowcnt]["Total"];
}
Session["Cart"] = obj;
}
}