-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.sql
333 lines (274 loc) · 10.7 KB
/
functions.sql
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
-- Function freePlacesForConferenceDay
-- returns number of free places for a
-- given day of the conference
CREATE FUNCTION FUNCTION_freePlacesForConferenceDay
(@ConferenceDayID int) RETURNS int
AS BEGIN
DECLARE @totalPlaces int;
SET @totalPlaces = (SELECT ParticipantsLimit
FROM ConferenceDays
WHERE ConferenceDayID = @ConferenceDayID)
DECLARE @takenPlaces int;
SET @takenPlaces = (SELECT SUM(ParticipantsNumber)
FROM DayReservations
WHERE ConferenceDayID = @ConferenceDayID
AND IsCancelled = 0)
RETURN (@totalPlaces - @takenPlaces);
END
-- Function freePlacesForConferenceDayReservation
-- returns number of free places for a given
-- conference day reservation
CREATE FUNCTION FUNCTION_freePlacesForConferenceDayReservation
(@DayReservationID int) RETURNS int
AS BEGIN
DECLARE @totalPlaces int;
SET @totalPlaces = (SELECT ParticipantsNumber
FROM DayReservations
WHERE DayReservationID = @DayReservationID)
DECLARE @takenPlaces int;
SET @takenPlaces = (SELECT COUNT(*)
FROM DayRegistrations
WHERE DayReservationID = @DayReservationID)
RETURN (@totalPlaces - @takenPlaces);
END
-- Function freePlacesForWorkshop
-- return number of free places for a given workshop
CREATE FUNCTION FUNCTION_freePlacesForWorkshop
(@WorkshopID int) RETURNS int
AS BEGIN
DECLARE @totalPlaces int;
SET @totalPlaces = (SELECT ParticipantsLimit
FROM Workshops
WHERE WorkshopID = @WorkshopID)
DECLARE @takenPlaces int;
SET @takenPlaces = (SELECT SUM(ParticipantsNumber)
FROM WorkshopReservations
WHERE WorkshopID = @WorkshopID
AND IsCancelled = 0)
RETURN (@totalPlaces - @takenPlaces);
END
-- Function freePlacesForWorkshopReservation
-- returns number of free places for a given
-- workshop reservation
CREATE FUNCTION FUNCTION_freePlacesForWorkshopReservation
(@WorkshopReservationID int) RETURNS int
AS BEGIN
DECLARE @totalPlaces int;
SET @totalPlaces = (SELECT ParticipantsNumber
FROM WorkshopReservations
WHERE WorkshopReservationID = @WorkshopReservationID)
DECLARE @takenPlaces int;
SET @takenPlaces = (SELECT COUNT(*)
FROM WorkshopRegistrations
WHERE WorkshopReservationID = @WorkshopReservationID)
RETURN (@totalPlaces - @takenPlaces);
END
-- Function conferenceDayReservationPrice
-- returns price for a given conference day reservaton
CREATE FUNCTION FUNCTION_conferenceDayReservationPrice
(@DayReservationID int) RETURNS money
AS BEGIN
DECLARE @reservationDate datetime;
SET @reservationDate = (SELECT ReservationDate
FROM Reservations as r
JOIN DayReservations d
on r.ReservationID = d.ReservationID
WHERE d.DayReservationID = @DayReservationID)
DECLARE @studentDiscount real;
SET @studentDiscount = (SELECT StudentDiscount
FROM Conferences as c
JOIN ConferenceDays as cd
on c.ConferenceID = cd.ConferenceID
JOIN DayReservations dr
on dr.ConferenceDayID = cd.ConferenceDayID
WHERE dr.DayReservationID = @DayReservationID)
RETURN(
SELECT SUM(p.Price*StudentsNumber*@studentDiscount)
+ SUM(p.Price*(ParticipantsNumber-StudentsNumber))
FROM DayReservations as dr
JOIN Prices as p ON p.ConferenceDayID = dr.ConferenceDayID
AND p.untilDate = (
SELECT MIN(p2.untilDate) as soonestDate
FROM Prices as p2
WHERE p2.untilDate >= @reservationDate
AND p2.ConferenceDayID = dr.ConferenceDayID
)
WHERE dr.DayReservationID = @DayReservationID
)
END
-- Function reservedConferenceDaysPrice
-- returns price of given reservation for all
-- reserved days
CREATE FUNCTION FUNCTION_reservedConferenceDaysPrice
(@ReservationID int) RETURNS money
AS BEGIN
RETURN(
SELECT SUM(dbo.FUNCTION_conferenceDayReservationPrice(DayReservationID))
FROM DayReservations as dr
WHERE dr.ReservationID = @ReservationID
)
END
-- Function workshopReservationPrice
-- returns price of a given workshop reservation
CREATE FUNCTION FUNCTION_workshopReservationPrice
(@WorkshopReservationID int) RETURNS money
AS BEGIN
RETURN(
SELECT (w.Price*wr.ParticipantsNumber)
FROM WorkshopReservations as wr
JOIN Workshops w on wr.WorkshopID = w.WorkshopID
WHERE wr.WorkshopReservationID = @WorkshopReservationID
)
END
-- Function reservedWorkshopsPrice
-- returns price of all workshop reservations
-- for given reservation
CREATE FUNCTION FUNCTION_reservedWorkshopsPrice
(@ReservationID int) RETURNS money
AS BEGIN
RETURN(
SELECT SUM(dbo.FUNCTION_workshopReservationPrice(WorkshopReservationID))
FROM WorkshopReservations as wr
JOIN DayReservations dr on dr.DayReservationID = wr.DayReservationID
JOIN Reservations as r on dr.ReservationID = r.ReservationID
WHERE r.ReservationID = @ReservationID
)
END
-- Function conferenceReservationPrice
-- returns summary price for a given reservation
CREATE FUNCTION FUNCTION_conferenceReservationPrice
(@ReservationID int) RETURNS money
AS BEGIN
RETURN(dbo.FUNCTION_reservedConferenceDaysPrice(@ReservationID)
+ dbo.FUNCTION_reservedWorkshopsPrice(@ReservationID));
END
-- Function conferenceReservationBalance
-- returns balance of payments for a given reservation
CREATE FUNCTION FUNCTION_conferenceReservationBalance
(@ReservationID int) RETURNS money
AS BEGIN
RETURN(
ISNULL(ROUND((SELECT SUM(Amount)
FROM Payments
WHERE ReservationID = @ReservationID)
- dbo.FUNCTION_conferenceReservationPrice(@ReservationID), 2), 0)
);
END
-- Function conferenceDayParticipantList
-- returns participant list for a given conference day
CREATE FUNCTION FUNCTION_conferenceDayParticipantsList
(@ConferenceDayID int) RETURNS @DayParticipantsListTable TABLE
(
ParticipantID int,
Firstname varchar(50),
Lastname varchar(50)
)
AS BEGIN
INSERT @DayParticipantsListTable
SELECT DISTINCT P.ParticipantID, P.Firstname, P.Lastname
FROM DayReservations AS DRES
JOIN DayRegistrations AS DREG
ON DREG.DayReservationID = DRES.DayReservationID
JOIN Participants AS P
ON P.ParticipantID = DREG.ParticipantID
WHERE DRES.ConferenceDayID = @ConferenceDayID AND DRES.IsCancelled = 0
ORDER BY P.ParticipantID
RETURN
END
-- Function workshopParticipantList
-- returns participant list for a given workshop
CREATE FUNCTION FUNCTION_workshopParticipantsList
(@WorkshopID int) RETURNS @WorkshopParticipantListTable TABLE
(
ParticipantID int,
Firstname varchar(50),
Lastname varchar(50)
)
AS BEGIN
INSERT @WorkshopParticipantListTable
SELECT DISTINCT P.ParticipantID, P.Firstname, P.Lastname
FROM WorkshopReservations AS WRES
JOIN WorkshopRegistrations AS WREG
ON WREG.WorkshopReservationID = WRES.WorkshopReservationID
JOIN DayRegistrations AS DREG
ON DREG.DayRegistrationID = WREG.DayRegistrationID
JOIN Participants AS P ON P.ParticipantID = DREG.ParticipantID
WHERE WRES.WorkshopID = @WorkshopID AND WRES.isCancelled = 0
ORDER BY P.ParticipantID
RETURN
END
-- Function participantConferenceDayList
-- returns conference day list for a given participant
CREATE FUNCTION FUNCTION_participantConferenceDayList
(@ParticipantID int) RETURNS @ParticipantConferenceDayListTable TABLE
(ConferenceDayID int)
AS BEGIN
INSERT @ParticipantConferenceDayListTable
SELECT DISTINCT DRES.ConferenceDayID
FROM Participants AS P
JOIN DayRegistrations AS DREG ON
DREG.ParticipantID = P.ParticipantID
JOIN DayReservations AS DRES ON
DRES.DayReservationID = DREG.DayRegistrationID
WHERE P.ParticipantID = @ParticipantID
ORDER BY DRES.ConferenceDayID
RETURN
END
-- Function participantWorkshopList
-- returns workshop list for a given participant
CREATE FUNCTION FUNCTION_participantWorkshopList
(@ParticipantID int) RETURNS @ParticipantWorkshopListTable TABLE
(WorkshopID int)
AS BEGIN
INSERT @ParticipantWorkshopListTable
SELECT DISTINCT WRES.WorkshopID
FROM Participants AS P
JOIN DayRegistrations AS DREG ON DREG.ParticipantID = P.ParticipantID
JOIN WorkshopRegistrations AS WREG
ON WREG.DayRegistrationID = DREG.DayRegistrationID
JOIN WorkshopReservations AS WRES
ON WRES.WorkshopReservationID = WREG.WorkshopReservationID
WHERE P.ParticipantID = @ParticipantID
ORDER BY WRES.WorkshopID
RETURN
END
-- Function workshopCollision
-- returns true if two given workshop are provided at the same time
CREATE FUNCTION FUNCTION_workshopCollision
(@WorkshopID1 int, @WorkshopID2 int) RETURNS bit
AS BEGIN
DECLARE @StartTime1 TIME(7);
DECLARE @EndTime1 TIME(7);
DECLARE @Date1 DATE;
DECLARE @StartTime2 TIME(7);
DECLARE @EndTime2 TIME(7);
DECLARE @Date2 DATE;
SET @StartTime1 = (SELECT StartTime
FROM Workshops
WHERE WorkshopID = @WorkshopID1)
SET @EndTime1 = (SELECT EndTime
FROM Workshops
WHERE WorkshopID = @WorkshopID1)
SET @Date1 = (SELECT D.Date
FROM ConferenceDays AS D
JOIN Workshops AS W
ON W.ConferenceDayID = D.ConferenceDayID
WHERE W.WorkshopID = @WorkshopID1)
SET @StartTime2 = (SELECT StartTime
FROM Workshops
WHERE WorkshopID = @WorkshopID2)
SET @EndTime2 = (SELECT EndTime
FROM Workshops
WHERE WorkshopID = @WorkshopID2)
SET @Date2 = (SELECT D.Date
FROM ConferenceDays AS D
JOIN Workshops AS W
ON W.ConferenceDayID = D.ConferenceDayID
WHERE W.WorkshopID = @WorkshopID2)
DECLARE @Collision BIT;
IF(@StartTime1 > @EndTime2 OR @StartTime2 > @EndTime1 OR (@Date1 <> @Date2))
SET @Collision = 0
ELSE
SET @Collision = 1
RETURN @Collision
END