This commit branch has been muddled with changes that are out of scope and cover more than just the models. Specifically it includes completion of basic guest related models, database access, command line interface integration and preventing SQL injection. Total summary of changes: **IMPORTANT CHANGE: All queries are now parameterized to prevent SQL injections. Prior versions are subject to it since the strings were built. - HotelManager This class is used to provide database operations that are outside of scope from a given model. Furthermore if there is no model to work off of, such as creating a new model based off an existing entry, these functions live outside of the model since it makes no sense for models to contain functions where it would retrieve itself with no context. Realistically this could be placed in an empty constructor and address itself, however, for now this functionality has been moved off the model since it is a one use and is not guaranteed to generate a guest. Some functionality for the coming employee management functions are already in there since they share overlap with the guest module. Specifically GetBaseRate for checking and getting the base rate. - DatabaseManager This class has mostly migrated functionality out or switch to parameterized query generation to prevent SQL injections. - Guest, Reservation, and Transaction Classes Migrated functionality to create the database entry and a new entry of guest upon creation as well as update them. - Program (Terminal app) Fully integrated the guest module. Has not undergone extensive testing but does work as of this commit.
186 lines
8.2 KiB
C#
186 lines
8.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Ophelias.Managers;
|
|
using System.Data.SQLite;
|
|
|
|
namespace Ophelias.Models
|
|
{
|
|
internal class Reservation
|
|
{
|
|
internal int Id;
|
|
internal int? RoomNum;
|
|
internal Guest Guest;
|
|
internal Transaction Transaction;
|
|
|
|
internal bool IsNoShow;
|
|
|
|
internal ReservationType Type;
|
|
internal ReservationStatus Status;
|
|
|
|
internal DateTime CreationDate;
|
|
internal DateTime StartDate;
|
|
internal DateTime EndDate;
|
|
|
|
internal DateTime? CheckIn;
|
|
internal DateTime? CheckOut;
|
|
internal DateTime? DateChanged;
|
|
|
|
internal Reservation(Guest Guest, ReservationType Type,
|
|
DateTime CreationDate, DateTime StartDate, DateTime EndDate, ReservationStatus Status = ReservationStatus.Active,
|
|
bool IsNoShow = false, DateTime? CheckIn = null, DateTime? CheckOut = null, DateTime? DateChanged = null, int? RoomNum = null)
|
|
{
|
|
int Id; double Multiplier;
|
|
switch (Type)
|
|
{
|
|
case ReservationType.Conventional: Multiplier = TxFunctions.ConventionalFee; break;
|
|
case ReservationType.Prepaid: Multiplier = TxFunctions.PrepaidFee; break;
|
|
case ReservationType.Incentive: Multiplier = TxFunctions.IncentiveFee(StartDate, EndDate); break;
|
|
case ReservationType.SixtyDayAdvance: Multiplier = TxFunctions.SixtyDayFee; break;
|
|
default: throw new NotImplementedException();
|
|
}
|
|
double? BaseRate = HotelManager.GetBaseRate();
|
|
if (BaseRate == null) {
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
Transaction Transaction = new Transaction(
|
|
Rate: (double)BaseRate,
|
|
Owed: TxFunctions.CalculateOwed((double)BaseRate, (int)(EndDate.Date - StartDate.Date).TotalDays),
|
|
Multiplier: Multiplier,
|
|
PayBy: TxFunctions.GetPayByDate(Type, StartDate, EndDate)
|
|
);
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
if (RoomNum != null)
|
|
this.RoomNum = RoomNum;
|
|
cmd.CommandText =
|
|
"INSERT INTO reservations (RoomNum, GuestID, TransactionID, IsNoShow, Type, Status, CreationDate, StartDate, EndDate, CheckIn, CheckOut, DateChanged) " +
|
|
"VALUES (@RoomNum, @GuestID, @TransactionID, @IsNoShow, @Type, @Status, @CreationDate, @StartDate, @EndDate, @CheckIn, @CheckOut, @DateChanged);";
|
|
cmd.Parameters.AddWithValue("@RoomNum", RoomNum);
|
|
cmd.Parameters.AddWithValue("@GuestID", Guest.Id);
|
|
cmd.Parameters.AddWithValue("@TransactionID", Transaction.Id);
|
|
cmd.Parameters.AddWithValue("@IsNoShow", Convert.ToInt32(IsNoShow));
|
|
cmd.Parameters.AddWithValue("@Type", (int)Type);
|
|
cmd.Parameters.AddWithValue("@Status", (int)Status);
|
|
cmd.Parameters.AddWithValue("@CreationDate", CreationDate.ToString("yyyy-MM-dd"));
|
|
cmd.Parameters.AddWithValue("@StartDate", StartDate.ToString("yyyy-MM-dd"));
|
|
cmd.Parameters.AddWithValue("@EndDate", EndDate.ToString("yyyy-MM-dd"));
|
|
if (CheckIn.HasValue)
|
|
cmd.Parameters.AddWithValue("@CheckIn", CheckIn.Value.ToString("yyyy-MM-dd"));
|
|
else
|
|
cmd.Parameters.AddWithValue("@CheckIn", CheckIn);
|
|
if (CheckOut.HasValue)
|
|
cmd.Parameters.AddWithValue("@CheckOut", CheckOut.Value.ToString("yyyy-MM-dd"));
|
|
else
|
|
cmd.Parameters.AddWithValue("@CheckOut", CheckOut);
|
|
if (DateChanged.HasValue)
|
|
cmd.Parameters.AddWithValue("@DateChanged", DateChanged.Value.ToString("yyyy-MM-dd"));
|
|
else
|
|
cmd.Parameters.AddWithValue("@DateChanged", DateChanged);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
Id = (int)Manager.con.LastInsertRowId;
|
|
}
|
|
this.Id = Id;
|
|
this.RoomNum = RoomNum;
|
|
this.Guest = Guest;
|
|
this.Transaction = Transaction;
|
|
this.IsNoShow = IsNoShow;
|
|
this.Type = Type;
|
|
this.Status = Status;
|
|
this.CreationDate = CreationDate;
|
|
this.StartDate = StartDate;
|
|
this.EndDate = EndDate;
|
|
this.CheckIn = CheckIn;
|
|
this.CheckOut = CheckOut;
|
|
this.DateChanged = DateChanged;
|
|
}
|
|
internal Reservation(int Id, Guest Guest, Transaction Transaction, ReservationType Type, ReservationStatus Status,
|
|
DateTime CreationDate, DateTime StartDate, DateTime EndDate, bool IsNoShow = false, DateTime? CheckIn = null, DateTime? CheckOut = null, DateTime? DateChanged = null, int? RoomNum = null)
|
|
{
|
|
this.Id = Id;
|
|
this.RoomNum = RoomNum;
|
|
this.Guest = Guest;
|
|
this.Transaction = Transaction;
|
|
this.IsNoShow = IsNoShow;
|
|
this.Type = Type;
|
|
this.Status = Status;
|
|
this.CreationDate = CreationDate;
|
|
this.StartDate = StartDate;
|
|
this.EndDate = EndDate;
|
|
this.CheckIn = CheckIn;
|
|
this.CheckOut = CheckOut;
|
|
this.DateChanged = DateChanged;
|
|
}
|
|
internal void ChangeReservationDates(DateTime StartDate, DateTime EndDate)
|
|
{
|
|
this.StartDate = StartDate;
|
|
this.EndDate = EndDate;
|
|
DateChanged = DateTime.Now.Date;
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
string? query = QueryBuilder.UpdateReservation(Id: Id, Status: ReservationStatus.Changed, StartDate: this.StartDate, EndDate: this.EndDate, DateChanged: DateChanged);
|
|
|
|
if (query == null)
|
|
throw new Exception();
|
|
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@ID", Id);
|
|
cmd.Parameters.AddWithValue("@Status", Status);
|
|
cmd.Parameters.AddWithValue("@StartDate", StartDate.ToString("yyyy-MM-dd"));
|
|
cmd.Parameters.AddWithValue("@EndDate", EndDate.ToString("yyyy-MM-dd"));
|
|
cmd.Parameters.AddWithValue("@DateChanged", DateChanged.Value.ToString("yyyy-MM-dd"));
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
double? BaseRate = HotelManager.GetBaseRate();
|
|
if (BaseRate == null)
|
|
throw new ArgumentNullException(nameof(BaseRate));
|
|
Transaction.UpdateTransactionFees((double)BaseRate, TxFunctions.Changed, TxFunctions.GetPayByDate(Type, this.StartDate, this.EndDate));
|
|
}
|
|
internal void CancelReservation()
|
|
{
|
|
DateTime _DateChanged = DateTime.Now.Date;
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
string? query = QueryBuilder.UpdateReservation(Id: Id, Status: ReservationStatus.Cancelled, DateChanged: _DateChanged);
|
|
|
|
if (query == null)
|
|
throw new Exception();
|
|
|
|
Status = ReservationStatus.Cancelled;
|
|
DateChanged = _DateChanged;
|
|
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@ID", Id);
|
|
cmd.Parameters.AddWithValue("@Status", Status);
|
|
cmd.Parameters.AddWithValue("@DateChanged", DateChanged.Value.ToString("yyyy-MM-dd"));
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
internal enum ReservationStatus
|
|
{
|
|
Active,
|
|
Changed,
|
|
Cancelled,
|
|
Ended,
|
|
}
|
|
internal enum ReservationType
|
|
{
|
|
Conventional,
|
|
Prepaid,
|
|
Incentive,
|
|
SixtyDayAdvance,
|
|
}
|
|
}
|