/// <summary>
        /// Add the product to the cart(session Variable):
        /// -"freeze" information of the product in ProductSelection Object
        /// Check if the product/size is already in the cart : if true :just add the quantity
        /// </summary>
        protected void AddButton_Click(object sender, EventArgs e)
        {
            try
            {
                bool modified = false;
                // get id from URL segment
                var segments  = Request.GetFriendlyUrlSegments();
                int productID = Convert.ToInt32(segments[0]);

                //ProductSelectionCreation
                dtoProdSel.SetProduct(blProduct.GetProduct(productID));
                dtoProdSel.SetOrigPrice(Convert.ToDecimal(labelPrice.Text));
                dtoProdSel.SetOrigSize(Convert.ToInt32(unitDropDownList.SelectedValue));
                dtoProdSel.SetQuantity(Convert.ToInt32(quantityDropDownList.SelectedValue));


                //Add to Cart
                cart = (List <ProductSelectionDTO>)(this.Session["cart"]);

                //Test ifproduct already exist in Cart
                foreach (ProductSelectionDTO p in cart)
                {
                    //If the exact same product is already in the cart, quantities are merge
                    if ((p.GetProduct().GetId() == dtoProdSel.GetProduct().GetId()) && (p.GetOrigSize() == dtoProdSel.GetOrigSize()))
                    {
                        //If the new quantity is bigger thanthe stock, the newQuantity is the maximum(=stock)
                        if (p.GetQuantity() + dtoProdSel.GetQuantity() <= p.GetProduct().GetStock())
                        {
                            p.SetQuantity(p.GetQuantity() + dtoProdSel.GetQuantity());
                        }
                        else
                        {
                            p.SetQuantity(p.GetProduct().GetStock() - 1);
                            lblResult.Text = "Maximum stock reached but   ";
                        }

                        modified = true;
                    }
                }

                //If the product isn't in the Cart, It's added
                if (modified == false)
                {
                    cart.Insert(cart.Count, dtoProdSel);
                }

                this.Session["cart"] = cart;
                lblResult.Text      += "Added to cart with success!";
            }
            catch (Exception ex)
            {
                ex.GetBaseException();
                Debug.Write(ex.ToString());
                lblResult.Text = "Error during cart managment";
            }
        }
Example #2
0
 private static ProductSelectionDTO GenerateSelection(SqlDataReader reader, ProductDTO product, InvoiceDTO inv, ProductSelectionDTO selection)
 {
     product.SetId(Convert.ToInt32(reader["productID"]));
     selection.SetID(Convert.ToInt32(reader["selectionID"]));
     selection.SetProduct(product);
     inv.SetID(Convert.ToInt32(reader["invoiceID"]));
     selection.SetInvoice(inv);
     selection.SetOrigPrice(Convert.ToDecimal(reader["originalPrice"]));
     selection.SetOrigSize(Convert.ToInt32(reader["originalSize"]));
     selection.SetQuantity(Convert.ToInt32(reader["quantity"]));
     return(selection);
 }