scheduler tutorial

Upload: wilson-palacios

Post on 30-Oct-2015

420 views

Category:

Documents


0 download

TRANSCRIPT

Scheduler Tutorial: Hotel Room Booking (ASP.NET, C#, VB.NET)May 18, 2009 [updated October 15, 2012]

This tutorial shows how to useDayPilot Schedulerto build a hotel room booking application.Online Demo Tutorial DemoDownload Tutorial Source Code(TutorialHotel.20121015.zip, 1.19 MB) - ASP.NET Project (C# and VB.NET)IncludesDayPilot Pro for ASP.NET WebForms 7.1 SP2Trial (download the latesttrial version).Features Colorsindicating the reservation phase Rulesfor reservation updates (overlap forbidden, past reservations) Filtersfor visible rooms (based on drop-down) Modal dialogfor event creating and editing Flash notificationabout user action resultRequirements .NET Framework 4.0 or higher Visual Studio 2010 or higher (optional)Database setupIn the tutorial, we are using anembedded SQLite engine(withSQLite ADO.NET Provider) soyou don't have to set anything upin order to run the demo.The database contains two tables: event resourceThe[event]table stores the events (reservations).CREATE TABLE [event] ([id] varchar(30) UNIQUE NOT NULL,[name] varchar (50) NULL,[eventstart] datetime NULL,[eventend] datetime NULL,[resource_id] varchar(50) NULL,[status] INTEGER DEFAULT '0' NOT NULL);The[resource]table stores the room details.CREATE TABLE [resource] ([id] VARCHAR(50) UNIQUE NOT NULL,[name] VARCHAR(200) NULL,[beds] INTEGER NULL,[bath] VARCHAR(50) NULL);The database file (daypilot.sqlite) can be found inApp_Datadirectory. The database structure can be examined usingSQLite Administrator.

Marking the past daysTheScheduleroffers several ways to show the current date/time (e.g.Separators). We will useBeforeCellRenderto show past days in different color:

C# protected void DayPilotScheduler1_BeforeCellRender(object sender, DayPilot.Web.Ui.Events.BeforeCellRenderEventArgs e) { if (e.Start < DateTime.Today) { if (e.IsBusiness) { e.BackgroundColor = "#D5D5C0"; } else { e.BackgroundColor = "#D5CFB3"; } } }VB.NET Protected Sub DayPilotScheduler1_BeforeCellRender(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.BeforeCellRenderEventArgs) Handles DayPilotScheduler1.BeforeCellRender If e.Start < DateTime.Today Then If e.IsBusiness Then e.BackgroundColor = "#D5D5C0" Else e.BackgroundColor = "#D5CFB3" End If End If End SubEvent colors indicating the reservation phaseIn our application, we will use four reservation phases: New reservation(unconfirmed) Confirmed reservation Arrived(checked in) Checked outEach phase will use a specificDurationBarcolor.New reservation (Orange)

Confirmed reservation (Green)

Arrived(Blue)

Checked out (Gray)

Problem (Red)

TheRedcolor is used for reservation with a problem: New reservationsnot confirmed2 days before the start day Confirmed reservationsnot checked inbefore 6pm on the start day Staysnot checked outbefore 10 am on the end dayThe color is assigned usingBeforeEventRenderevent handler. Note that the"status" database fieldis used to determine the phase The status is loaded into the Tag collection usingDataTagFields="status"declaration In the event handlers, it's available ase.Tag["status"]in the event handlerC# protected void DayPilotScheduler1_BeforeEventRender(object sender, DayPilot.Web.Ui.Events.BeforeEventRenderEventArgs e) { e.InnerHTML = String.Format("{0} ({1:d} - {2:d})", e.Text, e.Start, e.End); int status = Convert.ToInt32(e.Tag["status"]); switch (status) { case 0: // new if (e.Start < DateTime.Today.AddDays(2)) // must be confirmed two day in advance { e.DurationBarColor = "red"; e.ToolTip = "Expired (not confirmed in time)"; } else { e.DurationBarColor = "orange"; e.ToolTip = "New"; } break; case 1: // confirmed if (e.Start < DateTime.Today || (e.Start == DateTime.Today && DateTime.Now.TimeOfDay.Hours > 18)) // must arrive before 6 pm { e.DurationBarColor = "red"; e.ToolTip = "Late arrival"; } else { e.DurationBarColor = "green"; e.ToolTip = "Confirmed"; } break; case 2: // arrived if (e.End < DateTime.Today || (e.End == DateTime.Today && DateTime.Now.TimeOfDay.Hours > 11)) // must checkout before 10 am { e.DurationBarColor = "red"; e.ToolTip = "Late checkout"; } else { e.DurationBarColor = "blue"; e.ToolTip = "Arrived"; } break; case 3: // checked out e.DurationBarColor = "gray"; e.ToolTip = "Checked out"; break; default: throw new ArgumentException("Unexpected status."); }VB.NET Protected Sub DayPilotScheduler1_BeforeEventRender(ByVal sender As Object, ByVal e As DayPilot.Web.Ui.Events.BeforeEventRenderEventArgs) Handles DayPilotScheduler1.BeforeEventRender e.InnerHTML = [String].Format("{0} ({1:d} - {2:d})", e.Text, e.Start, e.[End]) Dim status As Integer = Convert.ToInt32(e.Tag("status")) Select Case status Case 0 ' new If e.Start < DateTime.Today.AddDays(2) Then ' must be confirmed two day in advance e.DurationBarColor = "red" e.ToolTip = "Expired (not confirmed in time)" Else e.DurationBarColor = "orange" e.ToolTip = "New" End If Exit Select Case 1 ' confirmed If e.Start < DateTime.Today OrElse (e.Start = DateTime.Today AndAlso DateTime.Now.TimeOfDay.Hours > 18) Then ' must arrive before 6 pm e.DurationBarColor = "red" e.ToolTip = "Late arrival" Else e.DurationBarColor = "green" e.ToolTip = "Confirmed" End If Exit Select Case 2 ' arrived If e.[End] < DateTime.Today OrElse (e.[End] = DateTime.Today AndAlso DateTime.Now.TimeOfDay.Hours > 11) Then ' must checkout before 10 am e.DurationBarColor = "red" e.ToolTip = "Late checkout" Else e.DurationBarColor = "blue" e.ToolTip = "Arrived" End If Exit Select Case 3 ' checked out e.DurationBarColor = "gray" e.ToolTip = "Checked out" Exit Select Case Else Throw New ArgumentException("Unexpected status.") End Select e.InnerHTML = e.InnerHTML + [String].Format("
{0}", e.ToolTip) End SubReservation change rules

The booking logic imposes somerestrictions. We will implement the following rules: No room can be booked for two guests at the same time (no overlap allowed). The reservations that already checked in can't be moved to another room. The start of reservations that already checked in can't be changed. The end of reservations that already checked out can't be changed.TheSchedulerhandles the drag&drop user actions (move and resize) usingEventMoveandEventResizeevent handlers.EventMove Event HandlerC# protected void DayPilotScheduler1_EventMove(object sender, DayPilot.Web.Ui.Events.EventMoveEventArgs e) { string id = e.Value; DateTime start = e.NewStart; DateTime end = e.NewEnd; string resource = e.NewResource; string message = null; if (!dbIsFree(id, start, end, resource)) { message = "The reservation cannot overlap with an existing reservation)."; } else if (e.OldEnd