using framework_business;
using framework_library;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class controls_module_products : System.Web.UI.UserControl
{
#region methods
///
/// Bind products
///
private void BindProductCategories()
{
ArrayList categories = new ArrayList();
try
{
int catId = GetActiveCategoryID();
categories = xData.GetTypedByCriteriaSpecific("recId", typeof(oProductCategory), "isActive,parentId", "1," + catId, "sequence");
ddCategories.DataSource = categories;
ddCategories.DataTextField = "category";
ddCategories.DataValueField = "recId";
ddCategories.DataBind();
ddCategories.Items.Insert(0, new ListItem("Select a type", "0"));
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("Error.aspx", false);
}
}
///
/// Bind products
///
private void BindProductBrands()
{
ArrayList brands = new ArrayList();
try
{
brands = xData.GetTypedByCriteriaSpecific("recId", typeof(oProductBrand), "isActive", "1", "sequence");
ddBrands.DataSource = brands;
ddBrands.DataTextField = "brand";
ddBrands.DataValueField = "recId";
ddBrands.DataBind();
ddBrands.Items.Insert(0, new ListItem("Select a brand", "0"));
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("Error.aspx", false);
}
}
///
/// Bind Products
///
private void BindProducts(bool filtered)
{
ArrayList products = new ArrayList();
try
{
if (filtered)
{
int catId = GetActiveCategoryID();
int typeId = int.Parse(ddCategories.SelectedValue);
int brandId = int.Parse(ddBrands.SelectedValue);
string keyword = txtKeyword.Value;
products = xData.SearchProducts(keyword, catId, typeId, brandId, "sequence");
}
else
{
int catId = GetActiveCategoryID();
products = xData.GetTypedByCriteriaSpecific("recId", typeof(oProduct), "isActive,catId", "1," + catId, "sequence");
}
rptProducts.DataSource = products;
rptProducts.DataBind();
}
catch (Exception ex)
{
exception.HandleException("products", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Product Display
///
private void HandleProductDisplay()
{
try
{
string prodName = handler.GetRoutedData("product-name");
if (prodName != String.Empty)
{
pnlProduct.Visible = true;
pnlProducts.Visible = false;
foreach (oProduct prod in xData.GetTypedByCriteriaSpecific("recId", typeof(oProduct), "name", prodName))
{
Session["product"] = prod;
PopulateProductValues(prod);
break;
}
}
else
{
pnlProduct.Visible = false;
pnlProducts.Visible = true;
}
}
catch (Exception ex)
{
exception.HandleException("Products:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("Error.aspx", false);
}
}
///
/// Product Specific Values
///
private void PopulateProductValues(oProduct prod)
{
try
{
if (prod.fileName != String.Empty)
imgProduct.ImageUrl = "~/upload/products/image/" + prod.fileName;
lblProductDisplay.Text = prod.display;
lblProductPrice.Text = "R " + utils.returnFormattedDecimal(prod.price.ToString());
foreach (oProductCategory cat in xData.GetTypedByCriteriaSpecific("recId", typeof(oProductCategory), "recId", prod.catId.ToString()))
{
lblCategory.Text = cat.category;
}
foreach (oProductBrand brnd in xData.GetTypedByCriteriaSpecific("recId", typeof(oProductBrand), "recId", prod.brandId.ToString()))
{
lblBrand.Text = brnd.brand;
}
lblSize.Text = prod.size;
lblDescription.Text = prod.description;
lblSpecs.Text = prod.specifications;
//build gallery
lblThumbnailGallery.Text = BuildGalleryData(prod.recId);
lblDocs.Text = BuildDocData(prod.recId);
}
catch (Exception ex)
{
exception.HandleException("products", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Build up document data
///
///
///
private string BuildDocData(int prodId)
{
string result = String.Empty;
try
{
ArrayList prodDocs = xData.GetTypedByCriteriaSpecific("recId", typeof(oAttachment), "moduleId,entityId,isActive,typeId", pNums.Module.Products.GetHashCode()
+ "," + prodId + ",1," + pNums.AttachmentType.Document.GetHashCode(), "sequence");
StringBuilder galBuilder = new StringBuilder();
if (prodDocs.Count > 0)
{
galBuilder.Append("
");
foreach (oAttachment attach in prodDocs)
{
galBuilder.Append("
" + attach.display + "");
}
galBuilder.Append("
");
}
result = galBuilder.ToString();
}
catch (Exception ex)
{
exception.HandleException("products", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
return result;
}
///
/// Build up gallery data
///
///
///
private string BuildGalleryData(int prodId)
{
string result = String.Empty;
try
{
ArrayList prodimages = xData.GetTypedByCriteriaSpecific("recId", typeof(oAttachment), "moduleId,entityId,isActive,typeId", pNums.Module.Products.GetHashCode()
+ "," + prodId + ",1," + pNums.AttachmentType.Image.GetHashCode(), "sequence");
StringBuilder galBuilder = new StringBuilder();
foreach (oAttachment attach in prodimages)
{
galBuilder.Append("");
}
result = galBuilder.ToString();
}
catch (Exception ex)
{
exception.HandleException("products", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
return result;
}
///
/// Get Active Category ID on page
///
///
private int GetActiveCategoryID()
{
int result = 0;
try
{
string prodName = handler.GetRoutedData("product-name");
string category = handler.GetRoutedData("product-category");
if (category != String.Empty)
{
foreach (oProductCategory cat in xData.GetTypedByCriteriaSpecific("recId", typeof(oProductCategory), "isActive,name", "1," + category))
{
result = cat.recId;
lblCatHeading.Text = cat.category;
break;
}
}
else if (prodName != String.Empty)
{
foreach (oProduct prod in xData.GetTypedByCriteriaSpecific("recId", typeof(oProduct), "isActive,name", "1," + prodName))
{
result = prod.catId;
break;
}
}
}
catch (Exception ex)
{
exception.HandleException("products", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
return result;
}
#endregion
#region events
///
/// Page Load Event
///
///
///
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!Page.IsPostBack)
{
BindProductCategories();
BindProductBrands();
//handle product display
HandleProductDisplay();
}
else {
if (utils.verifySession("product"))
{
oProduct prod = (oProduct)Session["product"];
PopulateProductValues(prod);
}
}
BindProducts(false);
}
catch (Exception ex)
{
exception.HandleException("products", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Filter Click
///
///
///
protected void btnFilter_Click(object sender, EventArgs e)
{
try
{
BindProducts(true);
}
catch (Exception ex)
{
exception.HandleException("products", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// View Product
///
///
///
protected void lnkViewProduct_Click(object sender, EventArgs e)
{
try
{
if (sender.GetType() == typeof(LinkButton))
{
int recId = int.Parse(((LinkButton)sender).CommandArgument);
foreach (oProduct prod in xData.GetTypedByCriteriaSpecific("recId", typeof(oProduct), "recId", recId.ToString()))
{
//change panel to product specific
Session["product"] = prod;
PopulateProductValues(prod);
pnlProduct.Visible = true;
pnlProducts.Visible = false;
break;
}
}
}
catch (Exception ex)
{
exception.HandleException("products", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Add To Cart
///
///
///
protected void lnkAddtoCart_Click(object sender, EventArgs e)
{
oUser user = new oUser();
bool canConfirm = false;
try
{
if (sender.GetType() == typeof(LinkButton))
{
int recId = int.Parse(((LinkButton)sender).CommandArgument);
foreach (oProduct prod in xData.GetTypedByCriteriaSpecific("recId", typeof(oProduct), "recId", recId.ToString()))
{
//add to cart
if (utils.verifySession("user"))
{
user = (oUser)Session["user"];
}
else
{
if (Request.AnonymousID != null)
{
oSetup _setup = handler.ReturnSetup();
user.cookieId = Request.AnonymousID;
user.customerCode = _setup.code;
foreach (oUser usr in xData.GetTypedByCriteriaSpecific("recId", typeof(oUser), "cookieId,customerCode", user.cookieId + "," + user.customerCode, "", "pal_",true))
{
user = usr;
Session["user"] = usr;
break;
}
}
if (user.recId == 0 && user.cookieId != String.Empty)
{
user.dateCreated = DateTime.Now;
user.isActive = true;
user.userType = pNums.UserType.WebsiteUser.GetHashCode();
user.recId = xData.SaveTyped("recId", typeof(oUser), user, "pal_", true);
}
}
if (user.recId > 0)
{
Session["user"] = user;
oOrder currentorder = new oOrder();
//get order for user if there is one
foreach (oOrder order in xData.GetTypedByCriteriaSpecific("recId", typeof(oOrder), "userId,orderSent", user.recId + ",0"))
{ currentorder = order; break; }
//create new order if not yet created
if (currentorder.recId == 0)
{
currentorder.userId = user.recId;
currentorder.recId = xData.SaveTyped("recId", typeof(oOrder), currentorder);
}
//verify we have an order
if (currentorder.recId > 0)
{
//add product to order items
oOrderItems orderItem = new oOrderItems();
//fetch existing order item if already exists for this product
foreach (oOrderItems prodItem in xData.GetTypedByCriteriaSpecific("recId", typeof(oOrderItems), "orderId,productId", currentorder.recId + "," + prod.recId))
{ orderItem = prodItem; break; }
orderItem.unitPrice = prod.price;
orderItem.orderId = currentorder.recId;
orderItem.productId = prod.recId;
//increase qty
orderItem.qty++;
if (orderItem.recId > 0)//update
{
if (xData.UpdateTyped("recId", orderItem.recId.ToString(), typeof(oOrderItems), orderItem))
canConfirm = true;
}
else//add new product to order
{
orderItem.recId = xData.SaveTyped("recId", typeof(oOrderItems), orderItem);
if (orderItem.recId > 0)
canConfirm = true;
}
if (canConfirm)
{
foreach (oOrder cOrder in xData.GetTypedByCriteriaSpecific("recId", typeof(oOrder), "recId", currentorder.recId.ToString()))
{
decimal orderTotal = 0;
//fetch all orderITems and calculate order Totla
foreach (oOrderItems prodItem in xData.GetTypedByCriteriaSpecific("recId", typeof(oOrderItems), "orderId", cOrder.recId.ToString()))
{
//increment total
orderTotal += (prodItem.unitPrice * prodItem.qty);
}
cOrder.orderTotal = orderTotal;
xData.UpdateTyped("recId", currentorder.recId.ToString(), typeof(oOrder), cOrder);
}
//Page.ClientScript.RegisterStartupScript(this.GetType(), "CartPop", "ShowCartModal();", true);
dynamic navigation1 = Page.Master.FindControl("navigation1");
navigation1.ReloadControl();
}
}
}
break;
}
}
}
catch (Exception ex)
{
exception.HandleException("products", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Add to item cart
///
///
///
protected void lnkAddItemToCart_Click(object sender, EventArgs e)
{
oUser user = new oUser();
bool canConfirm = false;
try
{
if (utils.verifySession("product"))
{
oProduct prod = (oProduct)Session["product"];
//add to cart
if (utils.verifySession("user"))
{
user = (oUser)Session["user"];
}
else
{
if (Request.AnonymousID != null)
{
oSetup _setup = handler.ReturnSetup();
user.customerCode = _setup.code;
user.cookieId = Request.AnonymousID;
foreach (oUser usr in xData.GetTypedByCriteriaSpecific("recId", typeof(oUser), "cookieId,customerCode", user.cookieId + "," + user.customerCode, "", "pal_",true))
{
user = usr;
break;
}
if (user.recId == 0 && user.cookieId != String.Empty)
{
user.dateCreated = DateTime.Now;
user.isActive = true;
user.userType = pNums.UserType.WebsiteUser.GetHashCode();
user.recId = xData.SaveTyped("recId", typeof(oUser), user, "pal_", true);
}
}
}
if (user.recId > 0)
{
Session["user"] = user;
oOrder currentorder = new oOrder();
//get order for user if there is one
foreach (oOrder order in xData.GetTypedByCriteriaSpecific("recId", typeof(oOrder), "userId,orderSent", user.recId + ",0"))
{ currentorder = order; break; }
//create new order if not yet created
if (currentorder.recId == 0)
{
currentorder.userId = user.recId;
currentorder.recId = xData.SaveTyped("recId", typeof(oOrder), currentorder);
}
//verify we have an order
if (currentorder.recId > 0)
{
//add product to order items
oOrderItems orderItem = new oOrderItems();
//fetch existing order item if already exists for this product
foreach (oOrderItems prodItem in xData.GetTypedByCriteriaSpecific("recId", typeof(oOrderItems), "orderId,productId", currentorder.recId + "," + prod.recId))
{ orderItem = prodItem; break; }
orderItem.unitPrice = prod.price;
orderItem.orderId = currentorder.recId;
orderItem.productId = prod.recId;
//increase qty
orderItem.qty++;
if (orderItem.recId > 0)//update
{
if (xData.UpdateTyped("recId", orderItem.recId.ToString(), typeof(oOrderItems), orderItem))
canConfirm = true;
}
else//add new product to order
{
orderItem.recId = xData.SaveTyped("recId", typeof(oOrderItems), orderItem);
if (orderItem.recId > 0)
canConfirm = true;
}
if (canConfirm)
{
foreach (oOrder cOrder in xData.GetTypedByCriteriaSpecific("recId", typeof(oOrder), "recId", currentorder.recId.ToString()))
{
decimal orderTotal = 0;
//fetch all orderITems and calculate order Totla
foreach (oOrderItems prodItem in xData.GetTypedByCriteriaSpecific("recId", typeof(oOrderItems), "orderId", cOrder.recId.ToString()))
{
//increment total
orderTotal += (prodItem.unitPrice * prodItem.qty);
}
cOrder.orderTotal = orderTotal;
xData.UpdateTyped("recId", currentorder.recId.ToString(), typeof(oOrder), cOrder);
}
//Page.ClientScript.RegisterStartupScript(this.GetType(), "CartPop", "ShowCartModal();", true);
dynamic navigation1 = Page.Master.FindControl("navigation1");
navigation1.ReloadControl();
}
}
}
}
}
catch (Exception ex)
{
exception.HandleException("products", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Go Back
///
///
///
protected void lnkBack_Click(object sender, EventArgs e)
{
try
{
Session["product"] = null;
pnlProducts.Visible = true;
pnlProduct.Visible = false;
}
catch (Exception ex)
{
exception.HandleException("products", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
///
/// Item Data Bound Event
///
///
///
protected void rptProducts_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
try
{
ScriptManager scriptMan = ScriptManager.GetCurrent(this.Page);
LinkButton btn = e.Item.FindControl("lnkAddtoCart") as LinkButton;
LinkButton lnkViewProduct = e.Item.FindControl("lnkViewProduct") as LinkButton;
if (btn != null)
{
btn.Click += lnkAddtoCart_Click;
scriptMan.RegisterAsyncPostBackControl(btn);
}
if (lnkViewProduct != null)
{
lnkViewProduct.Click += lnkViewProduct_Click;
scriptMan.RegisterPostBackControl(lnkViewProduct);
}
}
catch (Exception ex)
{
exception.HandleException("products", MethodBase.GetCurrentMethod().Name, ex, Session["user"]);
Response.Redirect("/error", false);
}
}
#endregion
}