The HotelManager class now has been connected to some aspects of the command line interface and is functioning for a few cases such as logging in as a specific guest via email, chaging a guests information and creating a new guest account. This was implemented first over the reservation system to test and implement something on a smaller scale. Furthermore, the reservation depends on an existing guest account since a Guest ID needs to be linked to the reservation that is created. Other changes include redesigning, tweaking/ adjusting, and/ or fixing of other modules that have not yet been implemented or are partially implemented.
257 lines
11 KiB
C#
257 lines
11 KiB
C#
using Ophelias.Models;
|
|
using System.Data.SQLite;
|
|
|
|
namespace Ophelias.Managers
|
|
{
|
|
internal static class HotelManager
|
|
{
|
|
internal static int GetLastId(string tableName)
|
|
{
|
|
int LastId = 0;
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
cmd.CommandText = $"SELECT SEQ FROM sqlite_sequence WHERE name='{tableName}';";
|
|
cmd.ExecuteNonQuery();
|
|
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
reader.Read();
|
|
LastId = reader.GetInt32(0);
|
|
}
|
|
}
|
|
}
|
|
return LastId;
|
|
}
|
|
internal static List<int> DailyOccupancies(DateTime Date)
|
|
{
|
|
List<int> Occupancies = new List<int>();
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
cmd.CommandText = $@"SELECT COUNT(*)
|
|
FROM reservations
|
|
WHERE DATE('{Date.Date.ToString("yyyy-MM-dd")}')
|
|
BETWEEN StartDate AND EndDate;";
|
|
cmd.ExecuteNonQuery();
|
|
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
reader.Read();
|
|
Occupancies.Add(reader.GetInt32(0));
|
|
}
|
|
}
|
|
}
|
|
return Occupancies;
|
|
}
|
|
internal static int AvgOccupancySpan(DateTime Start, DateTime End)
|
|
{
|
|
int thirtyDayOcc = 0;
|
|
int days = (int)(Start.Date - End.Date).TotalDays;
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
for (int i = 0; i < days; i++)
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
cmd.CommandText = $@"SELECT COUNT(*)
|
|
FROM reservations
|
|
WHERE DATE('{Start.AddDays(i).Date.ToString("yyyy-MM-dd")}')
|
|
BETWEEN StartDate AND EndDate;";
|
|
cmd.ExecuteNonQuery();
|
|
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
reader.Read();
|
|
thirtyDayOcc += reader.GetInt32(0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return thirtyDayOcc / days;
|
|
}
|
|
internal static Reservation CreateReservation(Guest Guest, ReservationType Type,
|
|
DateTime CreationDate, DateTime StartDate, DateTime EndDate, ReservationStatus Status = ReservationStatus.Active)
|
|
{
|
|
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();
|
|
}
|
|
Transaction t = CreateTransaction(
|
|
Rate: GetBaseRate(),
|
|
Owed: TxFunctions.CalculateOwed(GetBaseRate(), (int)(StartDate.Date - EndDate.Date).TotalDays),
|
|
Multiplier: Multiplier,
|
|
PayBy: TxFunctions.GetPayByDate(Type, StartDate, EndDate)
|
|
);
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
cmd.CommandText = QueryBuilder.CreateReservation(Guest.Id, t.Id, Type,
|
|
Status, CreationDate, StartDate, EndDate);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
id = (int)Manager.con.LastInsertRowId;
|
|
}
|
|
|
|
return new Reservation(id, Guest, t, Type, Status, CreationDate, StartDate, EndDate);
|
|
}
|
|
internal static Transaction CreateTransaction(double Rate, double Owed,
|
|
double Multiplier, DateTime PayBy, DateTime? LastPaid = null,
|
|
DateTime? PaidOn = null, double Refund = 0, double Penalty = 0)
|
|
{
|
|
int Id;
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
cmd.CommandText = QueryBuilder.CreateTransaction(Rate, Owed, Multiplier, PayBy,Refund: Refund, Penalty: Penalty, LastPaid: LastPaid, PaidOn: PaidOn);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
Id = (int)Manager.con.LastInsertRowId;
|
|
}
|
|
return new Transaction(Id, Rate, Owed, Multiplier, Penalty: Penalty, RefundAmount: Refund, PayBy: PayBy, PaidOn: PaidOn);
|
|
}
|
|
internal static Guest CreateGuest(string FirstName, string LastName, string Email, string? CreditCard = null, string? Expiration = null, string? CCV = null)
|
|
{
|
|
int Id;
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
cmd.CommandText = QueryBuilder.CreateGuest(FirstName, LastName, Email, CreditCard, Expiration, CCV);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
Id = (int)Manager.con.LastInsertRowId;
|
|
}
|
|
|
|
if (CreditCard != null && Expiration != null && CCV != null)
|
|
return new Guest(Id, FirstName, LastName, Email, CreditCard, Expiration, CCV);
|
|
else
|
|
return new Guest(Id, FirstName, LastName, Email);
|
|
}
|
|
internal static void UpdateGuest(int Id, string? FirstName = null, string? LastName = null, string? Email = null, string? CreditCard = null, string? Expiration = null, string? CCV = null)
|
|
{
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
cmd.CommandText = QueryBuilder.UpdateGuest(Id, FirstName, LastName, Email, CreditCard, Expiration, CCV);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
internal static Guest? GetGuestByEmail(string Email)
|
|
{
|
|
Guest? g = null;
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
cmd.CommandText = $"SELECT * FROM guests WHERE email = '{Email}'";
|
|
cmd.ExecuteNonQuery();
|
|
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
reader.Read();
|
|
if (reader.HasRows)
|
|
{
|
|
string? CreditCard = null, Expiration = null, CCV = null;
|
|
if (reader[4].GetType() != typeof(DBNull))
|
|
CreditCard = reader[4].ToString();
|
|
if (reader[5].GetType() != typeof(DBNull))
|
|
Expiration = reader[5].ToString();
|
|
if (reader[6].GetType() != typeof(DBNull))
|
|
CCV = reader[6].ToString();
|
|
g = new Guest(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), CreditCard, Expiration, CCV);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return g;
|
|
}
|
|
internal static double GetBaseRate()
|
|
{
|
|
double rate;
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
string query = "SELECT Rate FROM rates WHERE DefaultRate = 1;";
|
|
cmd.CommandText = query;
|
|
cmd.ExecuteNonQuery();
|
|
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
reader.Read();
|
|
rate = reader.GetDouble(0);
|
|
}
|
|
}
|
|
}
|
|
return rate;
|
|
}
|
|
internal static void SetBaseRate(double Rate, DateTime DateSet)
|
|
{
|
|
double rate;
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
string query = $"INSERT INTO rates (Rate, DateSet) VALUES ({Rate}, {DateSet});";
|
|
cmd.CommandText = query;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
internal static void CheckBaseRate()
|
|
{
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
int? OldId;
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
string query = "SELECT Id FROM rates WHERE DefaultRate = 1;";
|
|
cmd.CommandText = query;
|
|
cmd.ExecuteNonQuery();
|
|
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
reader.Read();
|
|
OldId = reader.GetInt32(0);
|
|
}
|
|
}
|
|
int? Id;
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
string query = $"SELECT Id FROM rates WHERE DateSet = {DateTime.Now.Date.ToString("yyyy-MM-dd")};";
|
|
cmd.CommandText = query;
|
|
cmd.ExecuteNonQuery();
|
|
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
reader.Read();
|
|
Id = reader.GetInt32(0);
|
|
}
|
|
}
|
|
if (Id != null)
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
cmd.CommandText = $@"UPDATE rates SET DefaultRate = NULL WHERE Id = {OldId};
|
|
UPDATE rates SET DefaultRate = 1 WHERE Id = {Id}";
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
internal static int CheckInGuest(DateTime CheckIn)
|
|
{
|
|
return 1;
|
|
}
|
|
internal static int CheckOutGuest(DateTime CheckOut)
|
|
{
|
|
return 1;
|
|
}
|
|
}
|
|
}
|