-
Notifications
You must be signed in to change notification settings - Fork 0
/
Page6.xaml.cs
144 lines (132 loc) · 5.5 KB
/
Page6.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using Question2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Main_Project;
using Main_Project.Models;
using Microsoft.EntityFrameworkCore;
namespace Question2
{
/// <summary>
/// Interaction logic for Page6.xaml
/// </summary>
public partial class Page6 : Page
{
private readonly BookingReservation bookingReservation;
private Input input = new Input();
public Page6(BookingReservation bookingReservation)
{
InitializeComponent();
this.bookingReservation = bookingReservation;
Load();
}
private void Load()
{
ContentControl.Content = "Reservation ID: " + bookingReservation.BookingReservationId;
var list = MainWindow.INSTANCE.context.BookingDetails.Where(x => x.BookingReservationId == bookingReservation.BookingReservationId).Include(x => x.Room).ToList();
BookingDetails.ItemsSource = list;
txtId.ItemsSource = MainWindow.INSTANCE.context.RoomInformations.ToList();
txtId.DisplayMemberPath = "RoomNumber";
}
private void Rooms_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var item = (sender as DataGrid).SelectedItem;
if (item != null)
{
var customer = item as BookingDetail;
txtId.SelectedItem = customer.Room;
txtPrice.Text = customer.ActualPrice.ToString();
dpStart.SelectedDate = DateTime.Parse(customer.StartDate.ToString());
dpEnd.SelectedDate = DateTime.Parse(customer.EndDate.ToString());
}
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
txtId.SelectedItem = null;
txtPrice.Text = "";
dpStart.SelectedDate = null;
dpEnd.SelectedDate = null;
}
private void ButtonBase_OnClickAdd(object sender, RoutedEventArgs e)
{
if (txtId.SelectedItem!=null&&dpStart.SelectedDate!=null&&dpEnd.SelectedDate!=null)
{
DateOnly startDate = DateOnly.FromDateTime(dpStart.SelectedDate.GetValueOrDefault());
DateOnly endDate = DateOnly.FromDateTime(dpEnd.SelectedDate.GetValueOrDefault());
var room = txtId.SelectedItem as RoomInformation;
if (!input.isDateValid(startDate, endDate))
{
return;
}
else if(!input.isDateAfterToday(startDate))
{
return;
}
DateTime startDateTime = startDate.ToDateTime(new TimeOnly(0, 0));
DateTime endDateTime = endDate.ToDateTime(new TimeOnly(0, 0));
// Calculate the total days between the two DateTime values
int totalDays = (endDateTime - startDateTime).Days;
// Calculate the actualPrice using the totalDays and room.RoomPricePerDay
decimal? actualPrice = totalDays * room.RoomPricePerDay;
var bookingDetail = new BookingDetail()
{
StartDate = startDate,
Room = room,
EndDate = endDate,
ActualPrice = actualPrice.GetValueOrDefault(),
BookingReservationId = bookingReservation.BookingReservationId,
BookingReservation = bookingReservation
};
if (input.isDateOverlap(bookingDetail))
{
return;
}
txtPrice.Text = actualPrice.ToString();
MainWindow.INSTANCE.context.BookingDetails.Add(bookingDetail);
MainWindow.INSTANCE.context.SaveChanges();
Load();
}
else
{
MessageBox.Show("Please fill all the blank", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void Delete(object sender, RoutedEventArgs e)
{
if (txtId.SelectedItem != null)
{
var room = txtId.SelectedItem as RoomInformation;
var booking = MainWindow.INSTANCE.context.BookingDetails.FirstOrDefault(x => x.Room == room && x.BookingReservationId == bookingReservation.BookingReservationId);
if (booking != null)
{
var result = MessageBox.Show("Do you want to delete this booking?", "Delete Booking", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
MainWindow.INSTANCE.context.BookingDetails.Remove(booking);
MainWindow.INSTANCE.context.SaveChanges();
Load();
}
}
else
{
MessageBox.Show("Booking not found", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else
{
MessageBox.Show("Please select a booking to delete", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}