Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/ASP.NET/VB/HotelBooking/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/ASP.NET/VB/HotelBooking/Order.aspx.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DevExpress.Web.ASPxGridView
Imports DevExpress.Web.ASPxEditors
Imports System.Collections
Imports DevExpress.Web.ASPxClasses

Partial Public Class Order
	Inherits System.Web.UI.Page
	Private hotel_Renamed As Hotel = Nothing

	Protected ReadOnly Property Hotel() As Hotel
		Get
			If hotel_Renamed Is Nothing Then
				hotel_Renamed = Data.Instance.Hotels.Find(Function(h) h.Title = ParseHotelTitle())
			End If
			If hotel_Renamed Is Nothing Then
				Response.Redirect("Default.aspx")
			End If
			Return hotel_Renamed
		End Get
	End Property
	Private privateRoomsQuantityState As Hashtable
	Protected Property RoomsQuantityState() As Hashtable
		Get
			Return privateRoomsQuantityState
		End Get
		Set(ByVal value As Hashtable)
			privateRoomsQuantityState = value
		End Set
	End Property

	Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
		gvRoomsOrder.DataSource = Hotel.Rooms
		rptHotelService.DataSource = Hotel.HotelServices
		rptRoomServices.DataSource = Hotel.RoomServices
		descContainer.InnerHtml = Hotel.Description
	End Sub
	Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
		If (Not IsPostBack) Then
			deCheckIn.MinDate = DateTime.Now
			deCheckOut.MinDate = DateTime.Now.AddDays(1)

			lblTitle.Text = Hotel.Title
			imgHotel.ImageUrl = Utils.GetImageUrl(Hotel.ImageUrl, Utils.OrderImageSize)
			lblDestination.Text = String.Format("{0}, {1}", Hotel.City, Hotel.Country.Name)
			lblStars.Text = Hotel.Stars.ToString()

			gvRoomsOrder.DataBind()
			rptHotelService.DataBind()
			rptRoomServices.DataBind()

			cmbCountry.DataSource = Data.Instance.Countries
			cmbCountry.DataBind()

			Dim query As SearchQuery = Data.Instance.CurrentQuery
			If query IsNot Nothing Then
				deCheckIn.Value = query.FromDate
				deCheckOut.Value = query.ToDate
			End If
		End If
		If IsPostBack Then
			SaveRoomsQuantityState()
		End If
		deCheckOut.ClientEnabled = deCheckIn.Value IsNot Nothing
		If (Not IsCallback) Then
			UpdateRating()
		End If
		lblTotalPrice.Text = GetTotalPrice()
	End Sub
	Protected Function GetTotalPrice() As String
		Dim totalValue As Integer = 0
		For i As Integer = 0 To gvRoomsOrder.VisibleRowCount - 1
			If gvRoomsOrder.Selection.IsRowSelected(i) Then
				Dim price As Integer = Convert.ToInt32(gvRoomsOrder.GetRowValues(i, "Price"))
				Dim seQuantity As ASPxSpinEdit = CType(gvRoomsOrder.FindRowCellTemplateControl(i, CType(gvRoomsOrder.Columns("Quantity"), GridViewDataColumn), "seQuantity"), ASPxSpinEdit)
				totalValue += price * CInt(Fix(seQuantity.Number))
			End If
		Next i
		If deCheckIn.Value IsNot Nothing AndAlso deCheckOut.Value IsNot Nothing Then
			Dim span As TimeSpan = CDate(deCheckOut.Value) - CDate(deCheckIn.Value)
			Dim days As Integer
			If span.Ticks > 0 AndAlso span.Days = 0 Then
				days = 1
			Else
				days = span.Days
			End If
			totalValue *= days
		End If
		If totalValue > 0 Then
			Return totalValue.ToString()
		Else
			Return "0"
		End If
	End Function
	Protected Function GetChildrenText(ByVal container As GridViewDataItemTemplateContainer) As String
		Dim room As Room = TryCast(container.Grid.GetRow(container.VisibleIndex), Room)
		If room.Children > 0 Then
			Return String.Format("0-{0}", room.Children)
		Else
			Return "0"
		End If
	End Function
	Protected Sub UpdateRating()
		rcHotelRating.Value = Hotel.Rating
		lblHotelRating.Text = Hotel.Rating.ToString("F1")
		lblHotelVotes.Text = Hotel.VoteCounter.ToString()
	End Sub
	Protected Sub ASPxButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
		Response.Redirect("Default.aspx")
	End Sub
	Protected Sub cpHotelRating_Callback(ByVal sender As Object, ByVal e As CallbackEventArgsBase)
		If Hotel.VoteAdresses.Keys.Contains(Request.UserHostAddress) Then
			Hotel.Rating = (Hotel.Rating * Hotel.VoteCounter - Hotel.VoteAdresses(Request.UserHostAddress)) / (Hotel.VoteCounter - 1)
			Hotel.VoteAdresses(Request.UserHostAddress) = rcHotelRating.Value
		Else
			Hotel.VoteAdresses.Add(Request.UserHostAddress, rcHotelRating.Value)
			Hotel.VoteCounter += 1
		End If
		Hotel.Rating = (Hotel.Rating * (Hotel.VoteCounter - 1) + rcHotelRating.Value) / (Hotel.VoteCounter)
		UpdateRating()
	End Sub
	Protected Sub deCheckIn_Validation(ByVal sender As Object, ByVal e As ValidationEventArgs)
		Utils.ValidateCheckInDate(e)
	End Sub
	Protected Sub deCheckOut_Validation(ByVal sender As Object, ByVal e As ValidationEventArgs)
		Utils.ValidateCheckOutDate(CDate(deCheckIn.Value), e)
	End Sub
	Protected Sub seQuantity_Load(ByVal sender As Object, ByVal e As EventArgs)
		If RoomsQuantityState IsNot Nothing Then
			LoadRoomsQuantityState(TryCast(sender, ASPxSpinEdit))
		End If
	End Sub
	Private Sub SaveRoomsQuantityState()
		RoomsQuantityState = New Hashtable()
		For i As Integer = 0 To gvRoomsOrder.VisibleRowCount - 1
			Dim se As ASPxSpinEdit = TryCast(gvRoomsOrder.FindRowCellTemplateControl(i, TryCast(gvRoomsOrder.Columns("Quantity"), GridViewDataColumn), "seQuantity"), ASPxSpinEdit)
			RoomsQuantityState(gvRoomsOrder.GetRowValues(i, "Id")) = se.Value
		Next i
	End Sub
	Private Sub LoadRoomsQuantityState(ByVal se As ASPxSpinEdit)
		Dim roomKey As Object = (CType(se.NamingContainer, GridViewDataItemTemplateContainer)).KeyValue
		se.Value = RoomsQuantityState(roomKey)
	End Sub
	Private Function ParseHotelTitle() As String
		Dim urlTitle As String = Request.QueryString("h")
		If String.IsNullOrEmpty(urlTitle) Then
			Return String.Empty
		Else
			Return urlTitle.Replace("_", " ").Replace("AMP", "&")
		End If
	End Function
End Class