-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBazy_projekt_kod.sql
More file actions
2936 lines (2629 loc) · 84.5 KB
/
Copy pathBazy_projekt_kod.sql
File metadata and controls
2936 lines (2629 loc) · 84.5 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
create type OrderedFood as table
(
OrderedFoodID int,
MenuPositionID int,
Quantity int
)
go
create type People as table
(
PeopleID int,
firstName nvarchar(50),
lastName nvarchar(50)
)
go
create type ReservationsDetIDtoTableID as table
(
TableResId int,
ResIdTable int
)
go
create table Administrators
(
AdminID int not null,
FirstName varchar(50) not null,
LastName varchar(50) not null,
Email varchar(50) not null,
constraint Administrators_pk
primary key (AdminID)
)
go
alter table Administrators
add constraint CK_Administrators_Email
check ([Email] like '%@%')
go
grant delete, insert, select, update on Administrators to Administrator
go
create table Categories
(
CategoryID int not null,
CategoryName varchar(50) not null,
constraint Categories_pk
primary key (CategoryID),
unique (CategoryName)
)
go
grant delete, insert, select, update on Categories to Administrator
go
grant delete, insert, select, update on Categories to Manager
go
create table Countries
(
CountryID int not null,
CountryName varchar(50) not null,
constraint Countries_pk
primary key (CountryID),
unique (CountryName)
)
go
create table Cities
(
CityID int not null,
CityName varchar(50) not null,
CountryID int not null,
constraint Cities_pk
primary key (CityID),
unique (CityName),
constraint Cities_Countries
foreign key (CountryID) references Countries
)
go
grant delete, insert, select, update on Cities to Administrator
go
grant delete, insert, select, update on Countries to Administrator
go
create table Customers
(
CustomerID int not null,
CityID int not null,
constraint Customers_pk
primary key (CustomerID),
constraint Customer_Cities
foreign key (CityID) references Cities
)
go
create table Company
(
CustomerID int not null,
CompanyName nvarchar(50) not null,
NIP nvarchar(50) not null,
constraint Company_pk
primary key (CustomerID),
unique (CompanyName),
unique (NIP),
constraint Company_Customer
foreign key (CustomerID) references Customers
)
go
alter table Company
add constraint CK_Company_NIP
check (isnumeric([NIP]) = 1)
go
grant delete, insert, select, update on Company to Administrator
go
grant delete, insert, select, update on Company to Manager
go
grant delete, insert, select, update on Customers to Administrator
go
grant delete, insert, select, update on Customers to Manager
go
create table DiscountDict
(
DiscountParamID int not null,
DiscountParamName nvarchar(50) not null,
constraint DiscountDict_pk
primary key (DiscountParamID)
)
go
grant delete, insert, select, update on DiscountDict to Administrator
go
create table DiscountParamsHist
(
DiscountHistID int not null,
ParamID int not null,
ParamValue real not null,
ValidFrom datetime not null,
ValidTo datetime,
constraint DiscountParamsHist_pk
primary key (DiscountHistID),
constraint DiscountDict_DiscountParamsHist
foreign key (ParamID) references DiscountDict
)
go
alter table DiscountParamsHist
add constraint CK_DiscPH_ParamValue
check ([ParamValue] > 0 AND [ParamValue] < 1)
go
alter table DiscountParamsHist
add constraint CK_DiscPH_ValidTo
check ([ValidTo] IS NULL OR [ValidTo] > [ValidFrom])
go
grant delete, insert, select, update on DiscountParamsHist to Administrator
go
create table Dishes
(
DishID int not null,
DishName varchar(50) not null,
CategoryID int not null,
DishPrice money not null,
constraint Dishes_pk
primary key (DishID),
unique (DishName),
constraint Products_Categories
foreign key (CategoryID) references Categories
)
go
alter table Dishes
add constraint CK_Dishes_Price
check ([DishPrice] > 0)
go
grant delete, insert, select, update on Dishes to Administrator
go
grant delete, insert, select, update on Dishes to Manager
go
create table MenuPositions
(
MenuPositionID int not null,
DishID int not null,
DishPrice money not null,
InDate datetime not null,
OutDate datetime,
constraint MenuPositions_pk
primary key (MenuPositionID),
constraint Menu_Products
foreign key (DishID) references Dishes
)
go
alter table MenuPositions
add constraint CK_MenuPos_DPrice
check ([DishPrice] > 0)
go
alter table MenuPositions
add constraint CK_MenuPos_OutDate
check ([OutDate] IS NULL OR [OutDate] > [InDate])
go
alter trigger CheckMenuPositions
on MenuPositions
after insert, update
as
begin
set nocount on;
EXECUTE uspCheckMenu
end
go
grant delete, insert, select, update on MenuPositions to Administrator
go
grant delete, insert, select, update on MenuPositions to Manager
go
create table PaymentStatus
(
PaymentStatusID int not null,
PaymentStatusName nvarchar(50) not null,
constraint PaymentStatus_pk
primary key (PaymentStatusID),
unique (PaymentStatusName)
)
go
grant delete, insert, select, update on PaymentStatus to Administrator
go
create table Person
(
PersonID int not null,
LastName nvarchar(50) not null,
FirstName nvarchar(50) not null,
Phone nvarchar(50) not null,
constraint Person_pk
primary key (PersonID)
)
go
create table Employees
(
EmployeeID int not null,
PersonID int not null,
ReportsTo int,
constraint Employees_pk
primary key (EmployeeID),
constraint Employees_Employees
foreign key (ReportsTo) references Employees,
constraint Employees_Person
foreign key (PersonID) references Person
)
go
alter table Employees
add constraint CK_Employees_ReportsTo
check ([ReportsTo] <> [EmployeeID] OR [ReportsTo] IS NULL)
go
grant delete, insert, select, update on Employees to Administrator
go
create table IndividualCustomers
(
CustomerID int not null,
PersonID int not null,
constraint IndividualCustomers_pk
primary key (CustomerID),
constraint IndividualCustomer_Customer
foreign key (CustomerID) references Customers,
constraint Person_IndividualCustomer
foreign key (PersonID) references Person
)
go
create table ConstantDiscount
(
ConstantDiscountID int not null,
CustomerID int not null,
ValidFrom datetime not null,
DiscountValue real not null,
constraint ConstantDiscount_pk
primary key (ConstantDiscountID),
constraint IndividualCustomer_ConstantDiscount
foreign key (CustomerID) references IndividualCustomers
)
go
alter table ConstantDiscount
add constraint CK_CD_DiscValue
check ([DiscountValue] > 0 AND [DiscountValue] < 1)
go
alter table ConstantDiscount
add constraint CK_CD_ValidFrom
check ([ValidFrom] >= getdate())
go
grant delete, insert, select, update on ConstantDiscount to Administrator
go
grant delete, insert, select, update on ConstantDiscount to Manager
go
grant delete, insert, select, update on IndividualCustomers to Administrator
go
grant delete, insert, select, update on IndividualCustomers to Manager
go
create unique index Person_Ind
on Person (LastName, FirstName, Phone)
go
alter table Person
add constraint CK_Person_Phone
check (isnumeric([Phone]) = 1)
go
grant delete, insert, select, update on Person to Administrator
go
create table ReservationsConditions
(
ReservationConditionID int not null,
ReservationConditionName nvarchar(50) not null,
ConditionValue int not null,
constraint ReservationsConditions_pk
primary key (ReservationConditionID),
unique (ReservationConditionName)
)
go
alter table ReservationsConditions
add constraint CK_ResConditions_ConditionValue
check ([ConditionValue] >= 0)
go
grant delete, insert, select, update on ReservationsConditions to Administrator
go
grant delete, insert, select, update on ReservationsConditions to Manager
go
create table ReservationsStatus
(
ReservationStatusID int not null,
ReservationStatusName nvarchar(50) not null,
constraint ReservationsStatus_pk
primary key (ReservationStatusID),
unique (ReservationStatusName)
)
go
create table Reservations
(
ReservationID int not null,
CustomerID int not null,
StartDate datetime not null,
EndDate datetime not null,
ReservationDate datetime not null,
ReservationStatus int not null,
constraint Reservations_pk
primary key (ReservationID),
constraint Reservation_ReservationStatus
foreign key (ReservationStatus) references ReservationsStatus
)
go
create table CompanyReservations
(
ReservationID int not null,
CustomerID int not null,
numberOfPeople int not null,
constraint CompanyReservations_pk
primary key (ReservationID),
constraint CompanyReservation_Company
foreign key (CustomerID) references Company,
constraint Reservation_CompanyReservation
foreign key (ReservationID) references Reservations
)
go
alter table CompanyReservations
add constraint CK_CP_nOfPeople
check ([numberOfPeople] > 0)
go
grant delete, insert, select, update on CompanyReservations to Administrator
go
grant delete, insert, select, update on CompanyReservations to Manager
go
alter table Reservations
add constraint CK_Reservations_EndDate
check ([EndDate] > [StartDate] AND datepart(year, [EndDate]) = datepart(year, [StartDate]) AND
datepart(month, [EndDate]) = datepart(month, [StartDate]) AND
datepart(day, [EndDate]) = datepart(day, [StartDate]))
go
alter table Reservations
add constraint CK_Reservations_StartDate
check ([StartDate] >= [ReservationDate])
go
alter trigger DeclineOrCompleteReservation
on Reservations
after insert, update
as
begin
set nocount on;
update Reservations
SET ReservationStatus = 4
where ReservationStatus = 1 and EndDate < GETDATE()
update Reservations
SET ReservationStatus = 2
where ReservationStatus = 3 and EndDate < GETDATE()
end
go
alter TRIGGER DeleteDeclinedOrderDetails
ON Reservations
FOR DELETE
AS
BEGIN
SET NOCOUNT ON;
DELETE FROM OrderDetails
WHERE OrderID in (
select IndividualReservations.OrderID from IndividualReservations
inner join dbo.Reservations R2 on R2.ReservationID = IndividualReservations.ReservationID
where R2.ReservationStatus = 2
)
end
go
grant delete, insert, select, update on Reservations to Administrator
go
grant delete, insert, select, update on Reservations to Manager
go
grant delete, insert, select, update on ReservationsStatus to Administrator
go
grant delete, insert, select, update on ReservationsStatus to Manager
go
create table SingleDiscount
(
DiscountID int not null,
CustomerID int not null,
ValidFrom datetime not null,
constraint SingleDiscount_pk
primary key (DiscountID),
constraint SingleDiscount_IndividualCustomer
foreign key (CustomerID) references IndividualCustomers
)
go
grant delete, insert, select, update on SingleDiscount to Administrator
go
grant delete, insert, select, update on SingleDiscount to Manager
go
create table SingleDiscountParams
(
DiscountHistID int not null,
DiscountID int not null,
SingleDiscountParamsID int identity,
constraint SingleDiscountParams_pk_2
primary key (SingleDiscountParamsID),
constraint DiscountParams_DiscountParamsHist
foreign key (DiscountHistID) references DiscountParamsHist,
constraint DiscountParams_SingleDiscount
foreign key (DiscountID) references SingleDiscount
)
go
create unique index SingleDiscountParams_SingleDiscountParamsID_uindex
on SingleDiscountParams (SingleDiscountParamsID)
go
grant delete, insert, select, update on SingleDiscountParams to Administrator
go
create table Tables
(
TableID int not null,
Quantity int not null,
constraint Tables_pk
primary key (TableID)
)
go
create table ReservationsDetails
(
ReservationID int not null,
TableID int,
LastName nvarchar(50) not null,
FirstName nvarchar(50) not null,
ReservationsDetailsID int identity,
constraint ReservationsDetails_pk
primary key (ReservationsDetailsID),
constraint ReservationsDetails_CompanyReservations
foreign key (ReservationID) references CompanyReservations,
constraint Tables_ReservationsDetails
foreign key (TableID) references Tables
)
go
create unique index ReservationsDetails_ReservationsDetailsID_uindex
on ReservationsDetails (ReservationsDetailsID)
go
alter TRIGGER ConfirmReservationIfTableToRes
ON ReservationsDetails
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE Reservations
SET ReservationStatus = 1
WHERE ReservationID in(select ReservationID FROM ReservationsDetails
WHERE TableID is not null)
end
go
grant delete, insert, select, update on ReservationsDetails to Administrator
go
grant delete, insert, select, update on ReservationsDetails to Manager
go
create table TableReservations
(
TableReservationID int not null,
TableID int not null,
TableReservationStart datetime not null,
TableReservationEnd datetime not null,
constraint TableReservations_pk
primary key (TableReservationID),
constraint TableReservation_Tables
foreign key (TableID) references Tables
)
go
alter table TableReservations
add constraint CK_TableRes_TableResEnd
check ([TableReservationEnd] > [TableReservationStart] AND
datepart(year, [TableReservationEnd]) = datepart(year, [TableReservationStart]) AND
datepart(month, [TableReservationEnd]) = datepart(month, [TableReservationStart]) AND
datepart(day, [TableReservationEnd]) = datepart(day, [TableReservationStart]))
go
grant delete, insert, select, update on TableReservations to Administrator
go
grant delete, insert, select, update on TableReservations to Manager
go
alter table Tables
add constraint CK_Tables_Quantity
check ([Quantity] > 0)
go
alter trigger DeleteTableFromTableResIfModified
on Tables
for delete
as
begin
set nocount on
delete
from TableReservations
where TableID = (select TableID from deleted)
end
go
grant delete, insert, select, update on Tables to Administrator
go
grant delete, insert, select, update on Tables to Manager
go
create table TakeawayStatus
(
TakeawayStatusID int not null,
TakeawayStatusName nvarchar(50) not null,
constraint TakeawayStatus_pk
primary key (TakeawayStatusID),
unique (TakeawayStatusName)
)
go
create table Orders
(
OrderID int not null,
EmployeeID int not null,
CustomerID int not null,
OrderDate datetime not null,
OutDate datetime,
PaidDate datetime,
TakeawayStatus int not null,
PaymentStatus int not null,
constraint Orders_pk
primary key (OrderID),
constraint Orders_Customer
foreign key (CustomerID) references Customers,
constraint Orders_Employees
foreign key (EmployeeID) references Employees,
constraint Orders_PaymentStatus
foreign key (PaymentStatus) references PaymentStatus,
constraint Orders_TakeawayStatus
foreign key (TakeawayStatus) references TakeawayStatus
)
go
create table IndividualReservations
(
ReservationID int not null,
CustomerID int not null,
OrderID int not null,
numberOfPeople int not null,
TableID int,
constraint IndividualReservations_pk
primary key (ReservationID),
constraint IndividualReservation_IndividualCustomer
foreign key (CustomerID) references IndividualCustomers,
constraint IndividualReservation_Orders
foreign key (OrderID) references Orders,
constraint Reservation_IndividualReservation
foreign key (ReservationID) references Reservations,
constraint Tables_IndividualReservations
foreign key (TableID) references Tables
)
go
alter table IndividualReservations
add constraint CK_IndiRes_nOfPeople
check ([numberOfPeople] > 0)
go
alter TRIGGER ConfirmIRReservationAfterAddingTableToRes
ON IndividualReservations
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE Reservations SET ReservationStatus = 1
WHERE ReservationID in (
select ReservationID from IndividualReservations
where tableID is not null
)
end
exec uspDeclineReservation 38
go
grant delete, insert, select, update on IndividualReservations to Administrator
go
grant delete, insert, select, update on IndividualReservations to Manager
go
create table OrderDetails
(
OrderID int not null,
DishID int not null,
MenuPositionID int not null,
Quantity int not null,
OrderDetailsID int identity,
constraint OrderDetails_pk
primary key (OrderDetailsID),
constraint OrderDetails_MenuPosition
foreign key (MenuPositionID) references MenuPositions,
constraint OrderDetails_Orders
foreign key (OrderID) references Orders
)
go
create unique index OrderDetails_OrderDetailsID_uindex
on OrderDetails (OrderDetailsID)
go
alter table OrderDetails
add constraint CK_OrderDet_Quantity
check ([Quantity] > 0)
go
grant delete, insert, select, update on OrderDetails to Administrator
go
grant delete, insert, select, update on OrderDetails to Manager
go
alter table Orders
add constraint CK_Orders_OutDate
check ([OutDate] IS NULL OR [OutDate] > [OrderDate])
go
alter table Orders
add constraint CK_Orders_PaidDate
check ([PaidDate] IS NULL OR [PaidDate] >= [OrderDate])
go
alter TRIGGER SetPaidStatusIfOrderOut
ON Orders
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE Orders
SET PaidDate = GETDATE()
WHERE PaymentStatus = 2 and OutDate < GETDATE()
UPDATE Orders
SET PaymentStatus = 1
WHERE PaymentStatus = 2 and OutDate < GETDATE()
end
go
grant delete, insert, select, update on Orders to Administrator
go
grant delete, insert, select, update on Orders to Manager
go
grant delete, insert, select, update on TakeawayStatus to Administrator
go
create table sysdiagrams
(
name sysname not null,
principal_id int not null,
diagram_id int identity,
version int,
definition varbinary(max),
primary key (diagram_id),
constraint UK_principal_name
unique (principal_id, name)
)
go
alter VIEW AverageOrderPricesForCustomers AS
Select COS.CustomerID,
P.LastName + ' ' + P.FirstName as Customer,
Round(AVG(COS.sum),2) as AVG,
'Person' as [Company/Person]
FROM CustomersOrdersSum COS
INNER JOIN IndividualCustomers IC on COS.CustomerID = IC.CustomerID
INNER JOIN Person P on P.PersonID = IC.PersonID
GROUP BY COS.CustomerID, P.LastName, P.FirstName
UNION
Select COS.CustomerID,
C.CompanyName as Customer,
Round(AVG(COS.sum),2) as AVG,
'Company' as [Company/Person]
FROM CustomersOrdersSum COS
INNER JOIN Company C on COS.CustomerID = C.CustomerID
GROUP BY COS.CustomerID, C.CompanyName
go
grant delete, insert, select, update on AverageOrderPricesForCustomers to Employee
go
alter VIEW CustomersNumbersOfOrders AS
select C.CustomerID,
P.LastName + ' ' + P.FirstName as Customer,
count(O.OrderID) as [Number of Orders],
'Person' as [Company/Person]
FROM Customers C
INNER JOIN Orders O on C.CustomerID = O.CustomerID
INNER JOIN IndividualCustomers IC on C.CustomerID = IC.CustomerID
INNER JOIN Person P on P.PersonID = IC.PersonID
GROUP BY C.CustomerID, P.LastName + ' ' + P.FirstName
UNION
SELECT C.CustomerID,
C2.CompanyName as Customer,
count(O.OrderID) as [Number of Orders],
'Company' as [Company/Person]
FROM Customers C
INNER JOIN Orders O on C.CustomerID = O.CustomerID
INNER JOIN Company C2 on C.CustomerID = C2.CustomerID
GROUP BY C.CustomerID, C2.CompanyName
go
alter VIEW CustomersOrders AS
SELECT O.OrderID,
IC.CustomerID,
P.LastName + ' ' + P.FirstName AS [Customer],
ROUND(SUM(MP.DishPrice * OD.Quantity * (1 - isnull(dbo.udfGetDiscount(O.OrderID, C.CustomerID), 0))),
2) AS [Order price],
O.OrderDate,
'Person' as [Company/Person],
YEAR(O.OrderDate) as Year,
MONTH(O.OrderDate) as Month
FROM Orders AS O
INNER JOIN OrderDetails OD on O.OrderID = OD.OrderID
INNER JOIN MenuPositions MP on MP.MenuPositionID = OD.MenuPositionID
INNER JOIN Customers C on C.CustomerID = O.CustomerID
INNER JOIN IndividualCustomers IC on C.CustomerID = IC.CustomerID
INNER JOIN Person P on P.PersonID = IC.PersonID
GROUP BY O.OrderID, IC.CustomerID, P.LastName + ' ' + P.FirstName, O.OrderDate
UNION
SELECT O.OrderID,
C.CustomerID,
C2.CompanyName AS [Customer],
ROUND(SUM(MP.DishPrice * OD.Quantity), 2) AS [Order price],
O.OrderDate,
'Company' as [Company/Person],
YEAR(O.OrderDate) as Year,
MONTH(O.OrderDate) as Month
FROM Orders AS O
INNER JOIN OrderDetails OD on O.OrderID = OD.OrderID
INNER JOIN MenuPositions MP on MP.MenuPositionID = OD.MenuPositionID
INNER JOIN Customers C on C.CustomerID = O.CustomerID
INNER JOIN Company C2 on C.CustomerID = C2.CustomerID
GROUP BY O.OrderID, C.CustomerID, C2.CompanyName, O.OrderDate
go
grant delete, insert, select, update on CustomersOrders to Employee
go
alter VIEW CustomersOrdersSum AS
select C.CustomerID,
O.OrderID,
ROUND(SUM(MP.DishPrice * OD.Quantity * (1 - isnull(dbo.udfGetDiscount(O.OrderID, C.CustomerID), 0))), 2) as sum
FROM Customers C
INNER JOIN IndividualCustomers IC on C.CustomerID = IC.CustomerID
INNER JOIN Person P on P.PersonID = IC.PersonID
INNER JOIN Orders O on C.CustomerID = O.CustomerID
INNER JOIN OrderDetails OD on O.OrderID = OD.OrderID
INNER JOIN MenuPositions MP on MP.MenuPositionID = OD.MenuPositionID
GROUP BY C.CustomerID, O.OrderID
UNION
select C.CustomerID,
O.OrderID,
ROUND(SUM(MP.DishPrice * OD.Quantity), 2) as sum
FROM Customers C
INNER JOIN Company C2 on C.CustomerID = C2.CustomerID
INNER JOIN Orders O on C.CustomerID = O.CustomerID
INNER JOIN OrderDetails OD on O.OrderID = OD.OrderID
INNER JOIN MenuPositions MP on MP.MenuPositionID = OD.MenuPositionID
GROUP BY C.CustomerID, O.OrderID
go
grant delete, insert, select, update on CustomersOrdersSum to Employee
go
-- STATYSTYKI
alter VIEW CustomersStatistics AS
SELECT C.CustomerID,
cNOO.Customer,
cNoo.[Company/Person],
cNOO.[Number of Orders],
aOPFC.AVG as [Average Order Price]
FROM Customers C
INNER JOIN averageOrderPricesForCustomers aOPFC on C.CustomerID = aOPFC.CustomerID
INNER JOIN customersNumbersOfOrders cNOO on C.CustomerID = cNOO.CustomerID
go
grant delete, insert, select, update on CustomersStatistics to Employee
go
alter VIEW DishesInMenuQuant as
SELECT D.DishName, (select count(*) from Dishes D2
INNER JOIN MenuPositions MP on D2.DishID = MP.DishID
where D.DishID = D2.DishID) as DishInMenu from Dishes D
go
grant delete, insert, select, update on DishesInMenuQuant to Employee
go
alter VIEW QuantityOfOrderedDishes as
SELECT D.DishName, (select count(*) from Dishes D2
INNER JOIN MenuPositions MP on D2.DishID = MP.DishID
INNER JOIN OrderDetails OD on MP.MenuPositionID = OD.MenuPositionID
where D.DishID = D2.DishID) as DishQuant from Dishes D
go
alter VIEW allCompaniesReservations AS
SELECT R.ReservationID,
R.CustomerID,
C.CompanyName,
RS.ReservationStatusName,
R.StartDate,
R.EndDate,
YEAR(R.StartDate) as Year,
MONTH(R.StartDate) as Month
FROM Reservations AS R
INNER JOIN ReservationsStatus RS on RS.ReservationStatusID = R.ReservationStatus
INNER JOIN CompanyReservations CR on R.ReservationID = CR.ReservationID
INNER JOIN Company C on C.CustomerID = CR.CustomerID
go
grant delete, insert, select, update on allCompaniesReservations to Employee
go
alter VIEW allDishes AS
SELECT DISTINCT D.DishID,
D.DishName,
C.CategoryName
FROM Dishes D
INNER JOIN Categories C on C.CategoryID = D.CategoryID
go
grant delete, insert, select, update on allDishes to Employee
go
alter VIEW allIndividualReservations AS
SELECT R.ReservationID,
R.CustomerID,
RS.ReservationStatusName,
R.StartDate,
R.EndDate,
YEAR(R.StartDate) as Year,
MONTH(R.StartDate) as Month
FROM Reservations AS R
INNER JOIN IndividualReservations IR on R.ReservationID = IR.ReservationID
INNER JOIN ReservationsStatus RS on RS.ReservationStatusID = R.ReservationStatus
go
grant delete, insert, select, update on allIndividualReservations to Employee
go
alter VIEW allMenuPositions AS
SELECT DISTINCT MP.MenuPositionID,
MP.DishID,
D.DishName,
Mp.DishPrice,
Mp.InDate,
Mp.OutDate
FROM MenuPositions AS MP
INNER JOIN Dishes D on D.DishID = MP.DishID
go
grant delete, insert, select, update on allMenuPositions to Employee
go
alter VIEW allReservations AS
SELECT R.ReservationID,
R.CustomerID,
RS.ReservationStatusName,
R.StartDate,
R.EndDate,
'Individual' AS Customer,
Year(R.StartDate) as Year,
MONTH(R.StartDate) as Month
FROM Reservations AS R
INNER JOIN IndividualReservations IR on R.ReservationID = IR.ReservationID
INNER JOIN ReservationsStatus RS on RS.ReservationStatusID = R.ReservationStatus
UNION
SELECT R.ReservationID,
R.CustomerID,
RS.ReservationStatusName,
R.StartDate,
R.EndDate,
'Company' AS Customer,
Year(R.StartDate) as Year,
MONTH(R.StartDate) as Month
FROM Reservations AS R
INNER JOIN CompanyReservations CR on R.ReservationID = CR.ReservationID
INNER JOIN ReservationsStatus RS on RS.ReservationStatusID = R.ReservationStatus
go
grant delete, insert, select, update on allReservations to Employee
go
-- Wyświetla wszystkich uczestników danych rezerwacji firmowych
alter VIEW allReservationsParticipants AS
SELECT CR.ReservationID,
RD.TableID,
RD.LastName + ' ' + RD.FirstName AS [Participant name]
FROM CompanyReservations AS CR
INNER JOIN ReservationsDetails RD on CR.ReservationID = RD.ReservationID
INNER JOIN Reservations R on R.ReservationID = CR.ReservationID
INNER JOIN ReservationsStatus RS on RS.ReservationStatusID = R.ReservationStatus
WHERE RS.ReservationStatusName = 'confirmed' OR RS.ReservationStatusName = 'completed'
go
alter view allTableStatistics as
select T.TableID, T.Quantity ,(select count(*) from TableReservations TR