Mini Kabibi Habibi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using System.Collections;
using DevExpress.Web.ASPxClasses;
public partial class Order : System.Web.UI.Page {
Hotel hotel = null;
protected Hotel Hotel {
get {
if(hotel == null)
hotel = Data.Instance.Hotels.Find(h => h.Title == ParseHotelTitle());
if(hotel == null)
Response.Redirect("Default.aspx");
return hotel;
}
}
protected Hashtable RoomsQuantityState { get; set; }
protected void Page_Init(object sender, EventArgs e) {
gvRoomsOrder.DataSource = Hotel.Rooms;
rptHotelService.DataSource = Hotel.HotelServices;
rptRoomServices.DataSource = Hotel.RoomServices;
descContainer.InnerHtml = Hotel.Description;
}
protected void Page_Load(object sender, EventArgs e) {
if(!IsPostBack) {
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();
SearchQuery query = Data.Instance.CurrentQuery;
if(query != null) {
deCheckIn.Value = query.FromDate;
deCheckOut.Value = query.ToDate;
}
}
if(IsPostBack)
SaveRoomsQuantityState();
deCheckOut.ClientEnabled = deCheckIn.Value != null;
if(!IsCallback)
UpdateRating();
lblTotalPrice.Text = GetTotalPrice();
}
protected string GetTotalPrice() {
int totalValue = 0;
for(int i = 0; i < gvRoomsOrder.VisibleRowCount; i++) {
if(gvRoomsOrder.Selection.IsRowSelected(i)) {
int price = Convert.ToInt32(gvRoomsOrder.GetRowValues(i, "Price"));
ASPxSpinEdit seQuantity = (ASPxSpinEdit)gvRoomsOrder.FindRowCellTemplateControl(i,
(GridViewDataColumn)gvRoomsOrder.Columns["Quantity"], "seQuantity");
totalValue += price * (int)seQuantity.Number;
}
}
if(deCheckIn.Value != null && deCheckOut.Value != null) {
TimeSpan span = (DateTime)deCheckOut.Value - (DateTime)deCheckIn.Value;
int days = span.Ticks > 0 && span.Days == 0 ? 1 : span.Days;
totalValue *= days;
}
return totalValue > 0 ? totalValue.ToString() : "0";
}
protected string GetChildrenText(GridViewDataItemTemplateContainer container) {
Room room = container.Grid.GetRow(container.VisibleIndex) as Room;
return room.Children > 0 ? string.Format("0-{0}", room.Children) : "0";
}
protected void UpdateRating() {
rcHotelRating.Value = Hotel.Rating;
lblHotelRating.Text = Hotel.Rating.ToString("F1");
lblHotelVotes.Text = Hotel.VoteCounter.ToString();
}
protected void ASPxButton1_Click(object sender, EventArgs e) {
Response.Redirect("Default.aspx");
}
protected void cpHotelRating_Callback(object sender, CallbackEventArgsBase e) {
if(Hotel.VoteAdresses.Keys.Contains(Request.UserHostAddress)) {
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++;
}
Hotel.Rating = (Hotel.Rating * (Hotel.VoteCounter - 1) + rcHotelRating.Value) / (Hotel.VoteCounter);
UpdateRating();
}
protected void deCheckIn_Validation(object sender, ValidationEventArgs e) {
Utils.ValidateCheckInDate(e);
}
protected void deCheckOut_Validation(object sender, ValidationEventArgs e) {
Utils.ValidateCheckOutDate((DateTime)deCheckIn.Value, e);
}
protected void seQuantity_Load(object sender, EventArgs e) {
if(RoomsQuantityState != null)
LoadRoomsQuantityState(sender as ASPxSpinEdit);
}
void SaveRoomsQuantityState() {
RoomsQuantityState = new Hashtable();
for(int i = 0; i < gvRoomsOrder.VisibleRowCount; i++) {
ASPxSpinEdit se = gvRoomsOrder.FindRowCellTemplateControl(i, gvRoomsOrder.Columns["Quantity"] as GridViewDataColumn, "seQuantity") as ASPxSpinEdit;
RoomsQuantityState[gvRoomsOrder.GetRowValues(i, "Id")] = se.Value;
}
}
void LoadRoomsQuantityState(ASPxSpinEdit se) {
object roomKey = ((GridViewDataItemTemplateContainer)se.NamingContainer).KeyValue;
se.Value = RoomsQuantityState[roomKey];
}
string ParseHotelTitle() {
string urlTitle = Request.QueryString["h"];
return string.IsNullOrEmpty(urlTitle) ? string.Empty : urlTitle.Replace("_", " ").Replace("AMP", "&");
}
}