Exemple #1
0
    protected void AddToCartButton_Click(object sender, EventArgs e)
    {
        Products comboproduct = _productManager.GetProductById(Convert.ToInt32(Request.QueryString["PID"]));

        if (comboproduct != null)
        {
            try
            {
                Orders order = SessionUserOrder;
                if (order == null)
                {
                    order = new BusinessEntities.Orders();
                }

                if (order.OrderDetailsList == null)
                {
                    order.OrderDetailsList       = new List <BusinessEntities.OrderDetails>();
                    SessionOrderAdonList         = new List <List <BusinessEntities.OrderDetailAdOns> >();
                    SessionOrderDetailOptionList = new List <List <BusinessEntities.OrderDetailOptions> >();
                    order.OrderStatusID          = BusinessEntities.OrderStatus.NewOrder;
                }

                #region Order Detail

                BusinessEntities.OrderDetails orderdetail = new BusinessEntities.OrderDetails();
                orderdetail.Price          = comboproduct.DefaultBranchProductPrice;
                orderdetail.CategoryName   = comboproduct.CategoryName;
                orderdetail.ProductName    = comboproduct.Name;
                orderdetail.ProductID      = comboproduct.ProductID;
                orderdetail.ProductImage   = comboproduct.Image;
                orderdetail.Quantity       = 1;
                orderdetail.IsGroupProduct = true;

                orderdetail.OrderDetailSubProducts = new List <OrderDetailSubProduct>();

                #endregion

                Double price = 0;
                foreach (RadPageView pageView in RadMultiPage1.PageViews)
                {
                    price += ((MyUserControl)pageView.Controls[0]).GetPrice();
                    var orderDetailObj = ((MyUserControl)pageView.Controls[0]).GetOrderDetailSubProduct();
                    orderdetail.OrderDetailSubProducts.Add(orderDetailObj);
                    orderdetail.RecipientName = orderDetailObj.RecipientName;
                }


                #region Code Commit

                /*RepeaterItemCollection gridItems = rptProducts.Items;
                 * foreach (RepeaterItem productItem in gridItems)
                 * {
                 *  Double productPrice = 0, toppingPrice = 0;
                 *  bool CalculateAdonPrice = false;
                 *  HiddenField ProductID = productItem.FindControl("hdProductId") as HiddenField;
                 *  RadTextBox RecipientName = productItem.FindControl("txtRecipientName") as RadTextBox;
                 *  RadTextBox Instruction = productItem.FindControl("txtInstruction") as RadTextBox;
                 *  Products product = products.Where(p => p.ProductID == Convert.ToInt32(ProductID.Value)).First();
                 *
                 #region Order Detail Sub Product
                 *
                 *  OrderDetailSubProduct orderDetailSubProduct = new OrderDetailSubProduct();
                 *  orderDetailSubProduct.ProductId = product.ProductID;
                 *  orderDetailSubProduct.Quantity = 1;
                 *  orderDetailSubProduct.RecipientName = RecipientName.Text;
                 *  orderDetailSubProduct.Comments = Instruction.Text;
                 *
                 #endregion
                 *
                 *
                 *  Repeater rptOptions = productItem.FindControl("rptOptions") as Repeater;
                 *
                 *  foreach (RepeaterItem optionItem in rptOptions.Items)
                 *  {
                 #region Order Detail Sub Product Option
                 *
                 #endregion
                 *      HiddenField OptionTypeID = optionItem.FindControl("hdOptionTypeId") as HiddenField;
                 *      OptionTypesInProduct optionTypeInProduct = product.OptionTypesInProductList.Where(ot => ot.OptionTypeID == Convert.ToInt16(OptionTypeID.Value)).FirstOrDefault();
                 *      if (optionTypeInProduct.IsMultiSelect)
                 *      {
                 *          CheckBoxList chklist = optionItem.FindControl("CheckBoxList") as CheckBoxList;
                 *          if (chklist != null)
                 *          {
                 *              bool changePrice = true;
                 *              foreach (ListItem checkBox in chklist.Items)
                 *              {
                 *                  if (checkBox.Selected)
                 *                  {
                 *                      ProductOptions productOption = optionTypeInProduct.ProductOptionsList[0];
                 *                      if (optionTypeInProduct.IsSamePrice && optionTypeInProduct.IsProductPriceChangeType)
                 *                      {
                 *                          if (changePrice)
                 *                          {
                 *                              if (productPrice == 0)
                 *                                  productPrice = productOption.Price;
                 *                              else
                 *                                  productPrice += productOption.Price;
                 *                              changePrice = false;
                 *                          }
                 *                      }
                 *                      else if (optionTypeInProduct.IsProductPriceChangeType)
                 *                      {
                 *                          productPrice += productOption.Price;
                 *                      }
                 *                      OrderDetailSubProductOption orderDetailSubProductOption = new OrderDetailSubProductOption();
                 *                      orderDetailSubProductOption.ProductOptionId = productOption.OptionID;
                 *                      orderDetailSubProductOption.ProductOptionName = productOption.OptionName;
                 *                      orderDetailSubProductOption.Price = productOption.Price;
                 *                  }
                 *              }
                 *          }
                 *      }
                 *      else
                 *      {
                 *          RadioButtonList radiolist = optionItem.FindControl("RadioButtonList") as RadioButtonList;
                 *          if (radiolist != null && !String.IsNullOrEmpty(radiolist.SelectedValue))
                 *          {
                 *              CalculateAdonPrice = true;
                 *              if (optionTypeInProduct.IsSamePrice)
                 *              {
                 *                  ProductOptions productOption = optionTypeInProduct.ProductOptionsList[0];
                 *                  productPrice = productOption.Price;
                 *              }
                 *              else
                 *              {
                 *                  ProductOptions productOption = optionTypeInProduct.ProductOptionsList.Where(po => po.OptionID == Convert.ToInt32(radiolist.SelectedValue)).FirstOrDefault();
                 *                  productPrice = productOption.Price;
                 *                  toppingPrice = productOption.ToppingPrice;
                 *              }
                 *          }
                 *          else
                 *          {
                 *              productPrice = product.UnitPrice;
                 *          }
                 *      }
                 *      if (CalculateAdonPrice)
                 *      {
                 *          Repeater rptAdonsType = productItem.FindControl("rptAdonsType") as Repeater;
                 *          Int16 freeToppingCount = 0;
                 *          foreach (RepeaterItem adonTypeItem in rptAdonsType.Items)
                 *          {
                 *              HiddenField hdAdonTypeId = adonTypeItem.FindControl("hdAdonTypeId") as HiddenField;
                 *              AdOnTypeInProduct adonType = product.AdOnTypeInProductList.Where(at => at.AdonTypeID == Convert.ToInt16(hdAdonTypeId.Value)).FirstOrDefault();
                 *
                 *              Repeater rptAdons = adonTypeItem.FindControl("rptAdons") as Repeater;
                 *              foreach (RepeaterItem adonItem in rptAdons.Items)
                 *              {
                 *                  Double adonPrice = 0;
                 *                  HiddenField hdAdonId = adonItem.FindControl("hdAdonId") as HiddenField;
                 *                  Adon adon = adonType.Adons.Where(a => a.AdOnID == Convert.ToInt32(hdAdonId.Value)).FirstOrDefault();
                 *                  RadioButtonList txtAdonOptions = adonItem.FindControl("AdonOptions") as RadioButtonList;
                 *                  CheckBox txtDouble = adonItem.FindControl("txtDouble") as CheckBox;
                 *                  short selectedoption = short.Parse(txtAdonOptions.SelectedValue);
                 *                  OrderDetailSubProductAdon orderDetailSubAdon = new OrderDetailSubProductAdon();
                 *                  orderDetailSubAdon.AdOnId = adon.AdOnID;
                 *                  orderDetailSubAdon.AdonName = adon.AdOnName;
                 *                  orderDetailSubAdon.SelectedAdonOption = selectedoption;
                 *                  orderDetailSubAdon.IsDoubleSelected = txtDouble.Checked;
                 *                  if (adon.DefaultSelected == 0 && (selectedoption == 1 || selectedoption == 2 || selectedoption == 3))
                 *                  {       // When None was default selected and user has changed default selected option
                 *                      if (!adonType.IsFreeAdonType)
                 *                      {
                 *                          if (txtDouble.Checked)
                 *                          {
                 *                              if (freeToppingCount == product.NumberOfFreeTopping)
                 *                              {
                 *                                  if (toppingPrice > 0)
                 *                                      adonPrice += 2 * toppingPrice;
                 *                                  else
                 *                                      adonPrice += (2 * Convert.ToDouble(adonType.Price));
                 *                              }
                 *                              else
                 *                              {
                 *                                  if (toppingPrice > 0)
                 *                                      productPrice += toppingPrice;
                 *                                  else
                 *                                      productPrice += Convert.ToDouble(adonType.Price);
                 *                                  freeToppingCount++;
                 *                              }
                 *                          }
                 *                          else
                 *                          {
                 *                              if (freeToppingCount == product.NumberOfFreeTopping)
                 *                              {
                 *                                  if (toppingPrice > 0)
                 *                                      productPrice += toppingPrice;
                 *                                  else
                 *                                      productPrice += Convert.ToDouble(adonType.Price);
                 *                              }
                 *                              else
                 *                                  freeToppingCount++;
                 *                          }
                 *                      }
                 *                  }
                 *                  else  // When other than None is pre selected and user has also selected Double check box
                 *                  {
                 *                      if (txtDouble.Checked && selectedoption != 0)
                 *                      {
                 *                          if (toppingPrice > 0)
                 *                              adonPrice += toppingPrice;
                 *                          else
                 *                              adonPrice += Convert.ToDouble(adonType.Price);
                 *                      }
                 *                      else
                 *                      {
                 *                          txtDouble.Checked = false;
                 *                      }
                 *                  }
                 *                  orderDetailSubAdon.Price = adonPrice;
                 *                  productPrice += adonPrice;
                 *              }
                 *          }
                 *      } // CalculateAdonPrice
                 *  }
                 *
                 *  netProductPrice += productPrice;
                 *  orderDetailSubProduct.Price = productPrice;
                 * }*/
                #endregion

                orderdetail.ItemTotal = orderdetail.Price = price;
                order.OrderDetailsList.Add(orderdetail);


                //Add order details to log table when added to cart

                // Prepare data for log
                string sessionID     = HttpContext.Current.Session.SessionID;
                string recepientName = orderdetail.RecipientName == "" ? null : orderdetail.RecipientName;
                string productDetail = string.Empty;
                string adons         = string.Empty;

                foreach (OrderDetailSubProduct product in orderdetail.OrderDetailSubProducts)
                {
                    productDetail += product.ProductName + ": ";
                    foreach (var p in product.OrderDetailSubProductOptions)
                    {
                        productDetail += p.ProductOptionName + ",";
                    }
                    foreach (OrderDetailSubProductAdon adon in product.OrderDetailSubProductAdons)
                    {
                        adons += adon.AdOnId + ",";
                    }
                }

                //productDetail = productDetail[productDetail.Length - 1] == ','
                //                    ? productDetail.Substring(0, productDetail.Length - 2)
                //                    : productDetail;

                //adons = adons[adons.Length - 1] == ','
                //                    ? adons.Substring(0, adons.Length - 2)
                //                    : adons;

                productDetail = productDetail.Trim(',');
                adons         = adons.TrimEnd(',');

                productDetail = productDetail == "" ? null : productDetail;
                adons         = adons == "" ? null : adons;
                // get log message to pass to db
                string message = Common.AddToCartLogMessage(orderdetail.CategoryName, orderdetail.Price,
                                                            adons, productDetail, SessionUserFullName);

                // Log order details
                LogManager log = new LogManager();
                log.SaveLogData(sessionID, LogLevel.INFO.ToString(), Logger.AddToCart.ToString(), message, null);

                List <BusinessEntities.OrderDetailAdOns> list = new List <BusinessEntities.OrderDetailAdOns>();
                SessionOrderAdonList.Add(list);
                List <BusinessEntities.OrderDetailOptions> list1 = new List <BusinessEntities.OrderDetailOptions>();
                SessionOrderDetailOptionList.Add(list1);

                double preOrderPromoValue = 0.0d;

                //if (SessionPreOrderPromo!=null)
                //{
                //    preOrderPromoValue = SessionPreOrderPromo.PreOrderPromoValue;
                //}
                order.OrderTotal      = order.OrderTotal + price;
                SessionUserOrder      = order;
                SessionUserOrderTotal = order.OrderTotal;

                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "CloseWindow", "<script type='text/javascript'>CloseOnReload()</script>");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
    private decimal GetOrderOptions(BusinessEntities.OrderDetails orderdetail, decimal adonpricefromOptionType)
    {
        RadListViewDataItemCollection optioncollection  = txtOptions.Items;
        List <BusinessEntities.OrderDetailOptions> list = new List <BusinessEntities.OrderDetailOptions>();

        foreach (RadListViewDataItem item in optioncollection)
        {
            long id = (long)item.GetDataKeyValue("ProductsOptionTypeId");
            OptionTypesInProduct OTP = (from otp in entities.OptionTypesInProducts
                                        where otp.ProductsOptionTypeId == id
                                        select otp).FirstOrDefault();
            if (OTP.IsSamePrice && !OTP.IsMultiSelect)
            {
                RadioButtonList radioList = item.FindControl("RadioButtonList") as RadioButtonList;
                foreach (ListItem listitem in radioList.Items)
                {
                    if (listitem.Selected)
                    {
                        long POP_ID = long.Parse(listitem.Value); ////Product Option In Product ID
                        ProductOptionsInProduct POP_obj = (from pop in entities.ProductOptionsInProducts
                                                           where pop.ProductOptionsInProductID == POP_ID
                                                           select pop).FirstOrDefault();
                        if (OTP.IsProductPriceChangeType)
                        {
                            orderdetail.Price = Convert.ToDouble(POP_obj.Price);
                        }

                        if ((bool)OTP.IsProductPriceChangeType && OTP.IsAdonPriceVary)
                        {
                            if (POP_obj.ToppingPrice != null)
                            {
                                adonpricefromOptionType = (decimal)POP_obj.ToppingPrice;
                            }
                        }
                        BusinessEntities.OrderDetailOptions orderdetailoption = new BusinessEntities.OrderDetailOptions();
                        orderdetailoption.ProductOptionId       = POP_obj.ProductOptionID;
                        orderdetailoption.ProductOptionName     = POP_obj.ProductOption.OptionName;
                        orderdetailoption.ProductOptionTypeName =
                            POP_obj.OptionTypesInProduct.OptionType.OptionDisplayText;
                        if (OTP.IsProductPriceChangeType)
                        {
                            orderdetailoption.Price = Convert.ToDouble(POP_obj.Price.Value);
                        }
                        list.Add(orderdetailoption);
                    }
                }
            }
            else
            {
                if (OTP.IsMultiSelect)
                {
                    #region CheckBox List

                    CheckBoxList chkList = item.FindControl("CheckBoxList") as CheckBoxList;
                    foreach (ListItem listitem in chkList.Items)
                    {
                        if (listitem.Selected)
                        {
                            long POP_ID = long.Parse(listitem.Value); ////Product Option In Product ID
                            ProductOptionsInProduct POP_obj = (from pop in entities.ProductOptionsInProducts
                                                               where pop.ProductOptionsInProductID == POP_ID
                                                               select pop).FirstOrDefault();
                            orderdetail.Price += Convert.ToDouble(POP_obj.Price);

                            BusinessEntities.OrderDetailOptions orderdetailoption = new BusinessEntities.OrderDetailOptions();
                            orderdetailoption.ProductOptionId       = POP_obj.ProductOptionID;
                            orderdetailoption.ProductOptionName     = POP_obj.ProductOption.OptionName;
                            orderdetailoption.Price                 = Convert.ToDouble(POP_obj.Price.Value);
                            orderdetailoption.ProductOptionTypeName = POP_obj.OptionTypesInProduct.OptionType.OptionDisplayText;
                            list.Add(orderdetailoption);
                        }
                    }
                    #endregion
                }
                else
                {
                    RadioButtonList radioList = item.FindControl("RadioButtonList") as RadioButtonList;
                    foreach (ListItem listitem in radioList.Items)
                    {
                        #region

                        if (listitem.Selected)
                        {
                            long POP_ID = long.Parse(listitem.Value); ////Product Option In Product ID
                            ProductOptionsInProduct POP_obj = (from pop in entities.ProductOptionsInProducts
                                                               where pop.ProductOptionsInProductID == POP_ID
                                                               select pop).FirstOrDefault();
                            orderdetail.Price = Convert.ToDouble(POP_obj.Price);

                            if ((bool)OTP.IsProductPriceChangeType)
                            {
                                if (POP_obj.ToppingPrice != null)
                                {
                                    adonpricefromOptionType = (decimal)POP_obj.ToppingPrice;
                                }
                            }
                            BusinessEntities.OrderDetailOptions orderdetailoption = new BusinessEntities.OrderDetailOptions();
                            orderdetailoption.ProductOptionId       = POP_obj.ProductOptionID;
                            orderdetailoption.ProductOptionName     = POP_obj.ProductOption.OptionName;
                            orderdetailoption.ProductOptionTypeName =
                                POP_obj.OptionTypesInProduct.OptionType.OptionDisplayText;
                            orderdetailoption.Price = Convert.ToDouble(POP_obj.Price);
                            list.Add(orderdetailoption);
                        }
                        #endregion
                    }
                }
            }
        }
        SessionOrderDetailOptionList.Add(list);
        return(adonpricefromOptionType);
    }