protected void btnCheckout_Click(object sender, ImageClickEventArgs e)
        {
            ShoppingItem shopItem = GetItems()[0];//there will always be one item returned from GetItems.
            CompanyProduct compProd = null;

            int companyId = int.Parse(Request[WebConstants.Request.COMPANY_ID]);
            IEnumerable<CompanyProduct> existingCompProds = (from prods in DatabaseContext.CompanyProducts where prods.CompanyID == companyId select prods);
            foreach (CompanyProduct existingProd in existingCompProds){
                if (existingProd.ProductID == shopItem.ProductEntity.ProductID && existingProd.EndDate.CompareTo(DateTime.Now) > 0) {
                    SetErrorMessage("The Company has already subscribed for this product.");
                    return;
                }
                else if (existingProd.ProductID == shopItem.ProductEntity.ProductID && existingProd.EndDate.CompareTo(DateTime.Now) < 0) {
                    compProd = existingProd;
                    break;
                }
            }

            if (compProd == null)
            {
                compProd = new CompanyProduct();
                compProd.StartDate = DateTime.Now;
                DatabaseContext.AddToCompanyProducts(compProd);
            }
            compProd.CompanyID = companyId;
            if(shopItem.ProductEntity != null)
                compProd.ProductID = shopItem.ProductEntity.ProductID;
            if(shopItem.VersionEntity != null)
                compProd.VersionID = shopItem.VersionEntity.VersionID;
            if (shopItem.ProductDetailEntity != null)
                compProd.ProductDetailID = shopItem.ProductDetailEntity.ProductDetailID;
            if(compProd.StartDate == null)
                compProd.StartDate = DateTime.Now;

            TextBox licenseNo = rptItems.Items[0].FindControl("tbQuantity") as TextBox;
            if (licenseNo.Text.ToString().CompareTo("") != 0)
                compProd.NumOfLicenses = int.Parse(licenseNo.Text.ToString());
            compProd.EndDate = DateTime.Now.AddYears(1);

            DatabaseContext.SaveChanges();

            SetSuccessMessage("Product Subscribed for company "+ compProd.Company.Name +" for 1 year period starting from now.");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!verifyTransactionSignature(Request))
            {
                panelSignature.Visible = true;
            }
            else
            {
                string transactionUId = Request["orderNumber"];
                Transaction transaction = (from tr in DatabaseContext.Transactions where tr.TransactionUID == transactionUId select tr).FirstOrDefault();
                if (Request.Form.Get("decision") == "ACCEPT" || Request.Form.Get("decision") == "REVIEW")
                {

                    List<ShoppingItem> shoppingItems = (List<ShoppingItem>)Session[WebConstants.Session.TROLLEY];
                    shoppingItems.Clear();

                    lblAmountText.Text = GetAmountText();
                    panelSuccess.Visible = true;

                    transaction.GWDecision = Request.Form.Get("decision");
                    transaction.GatewayID = transactionUId;
                    transaction.CompletionTime = DateTime.Now;

                    DatabaseContext.SaveChanges();

                    EmailUtility.SendPaymentEmailtoClient(LoggedIsUser, Request.Form.Get("billTo_firstName"), Request.Form.Get("billTo_lastName"), Request.Form.Get("card_accountNumber"),
                    Request.Form.Get("card_expirationMonth"), Request.Form.Get("card_expirationYear"), Utility.GetCardType(Request.Form.Get("card_cardType")),
                    lblAmountText.Text, LoggedIsUser.Email);
                    EmailUtility.SendPaymentEmailtoAdmin(LoggedIsUser, Request.Form.Get("billTo_firstName"), Request.Form.Get("billTo_lastName"), Request.Form.Get("card_accountNumber"),
                    Request.Form.Get("card_expirationMonth"), Request.Form.Get("card_expirationYear"), Utility.GetCardType(Request.Form.Get("card_cardType")),
                    lblAmountText.Text, LoggedIsUser.Email);

                    bool healthAndSafetyPurchased = false;
                    if (LoggedIsUser.Company != null)
                    {
                        foreach (TransactionDetail detail in transaction.TransactionDetails)
                        {
                            CompanyProduct companyProduct = new CompanyProduct();
                            companyProduct.Company = LoggedIsUser.Company;
                            companyProduct.ProductID = detail.ProductID;
                            companyProduct.VersionID = detail.VersionID.Value;
                            if (detail.ProductDetailID > 0)
                                companyProduct.ProductDetailID = detail.ProductDetailID.Value;
                            companyProduct.NumOfLicenses = detail.Quantity;
                            companyProduct.StartDate = DateTime.Now;
                            companyProduct.EndDate = DateTime.Now.AddMonths(detail.Duration);
                            DatabaseContext.AddToCompanyProducts(companyProduct);

                            if (detail.ProductID == 2) healthAndSafetyPurchased = true;
                        }
                        DatabaseContext.SaveChanges();
                        //we need to mark Health And Safety as purchased if its already in trial mode
                        if (healthAndSafetyPurchased)
                        {
                            SqlConnection conn = new SqlConnection(AppSettings["HSDB"]);
                            try
                            {
                                conn.Open();
                                SqlCommand command = new SqlCommand(AppSettings["MarkHSPurchasedProcedure"], conn);
                                command.CommandType = System.Data.CommandType.StoredProcedure;
                                command.Parameters.AddWithValue("@simplicity_company_id", LoggedIsUser.Company.CompanyID);
                                command.Parameters.AddWithValue("@flg_trial", false);
                                command.ExecuteReader();
                            }
                            finally
                            {
                                if (conn != null) conn.Close();
                            }
                        }
                    }

                    ShoppingCart.ClearTrolley();
                    productsLogin.Visible = true;
                    //add to user products
                }
                else
                {
                    transaction.GWDecision = Request.Form.Get("decision");
                    transaction.GatewayID = transactionUId;
                    transaction.CompletionTime = DateTime.Now;
                    transaction.GWReasonCode = int.Parse(Request.Form.Get("reasonCode"));
                    DatabaseContext.SaveChanges();

                    if (Request.Form.Get("reasonCode") == "102")
                    {
                        HashSet<string> fields = new HashSet<string>();
                        for (int i = 0; i < 100; i++)
                        {
                            if (Request["InvalidField" + i] != null)
                                fields.Add(Request["InvalidField" + i]);
                            else
                                break;
                        }

                        for (int i = 0; i < 100; i++)
                        {
                            if (Request["MissingField" + i] != null)
                                fields.Add(Request["MissingField" + i]);
                            else
                                break;
                        }
                        string fieldValues = "";
                        foreach (string field in fields)
                        {
                            fieldValues += field + ",";
                        }
                        Response.Redirect("~/Admin/PaymentDetail.aspx?" + WebConstants.Request.TRANSACTION_UID + "=" + transactionUId
                            + "&" + WebConstants.Request.INVALID_FIELDS + "=" + fieldValues);
                    }
                    else
                    {
                        Response.Redirect("~/Admin/PaymentDetail.aspx?" + WebConstants.Request.TRANSACTION_UID + "=" + transactionUId
                            + "&" + WebConstants.Request.REASON_CODE + "=" + Request.Form.Get("reasonCode"));
                    }
                }
            }
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the CompanyProducts EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToCompanyProducts(CompanyProduct companyProduct)
 {
     base.AddObject("CompanyProducts", companyProduct);
 }
 /// <summary>
 /// Create a new CompanyProduct object.
 /// </summary>
 /// <param name="companyProductID">Initial value of the CompanyProductID property.</param>
 /// <param name="companyID">Initial value of the CompanyID property.</param>
 /// <param name="productID">Initial value of the ProductID property.</param>
 /// <param name="versionID">Initial value of the VersionID property.</param>
 /// <param name="startDate">Initial value of the StartDate property.</param>
 /// <param name="endDate">Initial value of the EndDate property.</param>
 /// <param name="numOfLicenses">Initial value of the NumOfLicenses property.</param>
 public static CompanyProduct CreateCompanyProduct(global::System.Int32 companyProductID, global::System.Int32 companyID, global::System.Int32 productID, global::System.Int32 versionID, global::System.DateTime startDate, global::System.DateTime endDate, global::System.Int32 numOfLicenses)
 {
     CompanyProduct companyProduct = new CompanyProduct();
     companyProduct.CompanyProductID = companyProductID;
     companyProduct.CompanyID = companyID;
     companyProduct.ProductID = productID;
     companyProduct.VersionID = versionID;
     companyProduct.StartDate = startDate;
     companyProduct.EndDate = endDate;
     companyProduct.NumOfLicenses = numOfLicenses;
     return companyProduct;
 }