CheckOut.aspx.cs :  » Template-Engines » netTiers » PetShop » UI » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Template Engines » netTiers 
netTiers » PetShop » UI » CheckOut.aspx.cs
using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using PetShop.Business;
using PetShop.Controls;
using PetShop.Services;
using PetShop.Services.CustomCode;

namespace PetShop.UI{
    public partial class CheckOut : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Profile profile = ProfileManager.Instance.GetCurrentUser(Page.User.Identity.Name);
            if(profile.IsAnonymous.Value)
            {
                Response.Redirect("~/SignIn.aspx");
            }

            if (billingForm.Address == null)
            {
                billingForm.Address = new Address(profile);
            }
        }

        /// <summary>
        /// Process the order
        /// </summary>
        protected void wzdCheckOut_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            var inventoryService = new InventoryService();
            Profile profile = ProfileManager.Instance.GetCurrentUser(Page.User.Identity.Name);
            if (profile.CartCollection.Count > 0)
            {
                // display ordered items
                CartListOrdered.Bind(profile.CartCollection);

                // display total and credit card information
                ltlTotalComplete.Text = ltlTotal.Text;
                ltlCreditCardComplete.Text = ltlCreditCard.Text;

                #region Create Order

                var order = new Order();

                order.UserId = profile.UniqueId.ToString();
                order.OrderDate = DateTime.Now;
                order.CreditCard = GetCreditCard();
                order.Courier = order.CreditCard.CardType;
                order.TotalPrice = profile.CartCollection.Sum(c => c.Total);
                order.AuthorizationNumber = 0;
                order.Locale = "en-us";

                #region Shipping Information

                order.ShipAddr1 = billingForm.Address.Address1;
                order.ShipAddr2 = billingForm.Address.Address2;
                order.ShipCity = billingForm.Address.City;
                order.ShipState = billingForm.Address.State;
                order.ShipZip = billingForm.Address.Zip;
                order.ShipCountry = billingForm.Address.Country;
                order.ShipToFirstName = billingForm.Address.FirstName;
                order.ShipToLastName = billingForm.Address.LastName;

                #endregion

                #region Billing Information

                order.BillAddr1 = shippingForm.Address.Address1;
                order.BillAddr2 = shippingForm.Address.Address2;
                order.BillCity = shippingForm.Address.City;
                order.BillState = shippingForm.Address.State;
                order.BillZip = shippingForm.Address.Zip;
                order.BillCountry = shippingForm.Address.Country;
                order.BillToFirstName = shippingForm.Address.FirstName;
                order.BillToLastName = shippingForm.Address.LastName;

                #endregion

                var orderService = new OrderService();
                orderService.DeepSave(order);

                #endregion

                int itemsOnBackOrder = 0;
                //Decrement and check the Inventory.
                foreach (Cart cart in profile.CartCollection)
                {
                    Inventory inventory = inventoryService.GetByItemId(cart.ItemId);
                    
                    if(cart.Quantity > inventory.Qty)
                    {
                        itemsOnBackOrder += cart.Quantity - inventory.Qty;
                    }

                    inventory.Qty -= cart.Quantity;

                    #region Reset the Inventory back to 10,000

                    if (inventory.Qty < 0)
                        inventory.Qty = 10000;

                    #endregion

                    inventoryService.DeepSave(inventory);
                }

                if(itemsOnBackOrder > 0)
                {
                    ItemsOnBackOrder.Text = string.Format("<br /><p style=\"color:red;\"><b>Backorder ALERT:</b> {0} items are on backorder.</p>", itemsOnBackOrder);   
                }

                var cartService = new CartService();
                cartService.SaveOrderLineItems(order.OrderId, profile.CartCollection);

                //profile.CartCollection.SaveOrderLineItems(order.OrderId);

                // destroy cart
                cartService.Delete(profile.CartCollection);
            }
            else
            {
                lblMsg.Text =
                    "<p><br>Can not process the order. Your cart is empty.</p><p class=SignUpLabel><a class=linkNewUser href=Default.aspx>Continue shopping</a></p>";
                wzdCheckOut.Visible = false;
            }
        }

        /// <summary>
        /// Create CreditCardInfo object from user input
        /// </summary>
        private CreditCard GetCreditCard()
        {
            string type = WebUtility.InputText(listCCType.SelectedValue, 40);
            string exp = WebUtility.InputText(txtExpDate.Text, 7);
            string number = WebUtility.InputText(txtCCNumber.Text, 20);
            return new CreditCard(type, number, exp);
        }

        /// <summary>
        /// Changing Wiszard steps
        /// </summary>
        protected void wzdCheckOut_ActiveStePChanged(object sender, EventArgs e)
        {
            if (wzdCheckOut.ActiveStepIndex == 3)
            {
                billingConfirm.Address = billingForm.Address;
                shippingConfirm.Address = shippingForm.Address;

                Profile profile = ProfileManager.Instance.GetCurrentUser(Page.User.Identity.Name);
                ltlTotal.Text = profile.CartCollection.Sum(c => c.Total).ToString("c");

                if (txtCCNumber.Text.Length > 4)
                    ltlCreditCard.Text = txtCCNumber.Text.Substring(txtCCNumber.Text.Length - 4, 4);
            }
        }

        /// <summary>
        /// Handler for "Ship to Billing Addredd" checkbox.
        /// Prefill/Clear shipping address form.
        /// </summary>
        protected void chkShipToBilling_CheckedChanged(object sender, EventArgs e)
        {
            if (chkShipToBilling.Checked)
                shippingForm.Address = billingForm.Address;
        }

        /// <summary>
        /// Custom validator to check CC expiration date
        /// </summary>
        protected void ServerValidate(object source, ServerValidateEventArgs value)
        {
            DateTime dt;
            if (DateTime.TryParse(value.Value, out dt))
                value.IsValid = (dt > DateTime.Now);
            else
                value.IsValid = false;
        }
    }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.