-
Notifications
You must be signed in to change notification settings - Fork 2
/
AddToCart.php
102 lines (93 loc) · 3.77 KB
/
AddToCart.php
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
<?php
require_once("Autoloader.php");
if(isset($_POST['eventId']) && isset($_POST['typeEvent']) && isset($_POST['amount']) && isset($_POST['childAmount']) && isset($_POST['adultAmount']) && isset($_POST['startTime']) && isset($_POST['date']) && isset($_POST['extraInfo'])) {
// assign vars
$session = new Session;
$OrderRepository = new OrderRepository();
$HistoricRepository = new HistoricRepository();
// assign vars from post data
$eventId = $_POST['eventId'];
$TypeEvent = $_POST['typeEvent'];
$amount = $_POST['amount'];
$childAmount = $_POST['childAmount'];
$adultAmount = $_POST['adultAmount'];
$isFoodReservation = false;
// create vars to be used
$maxAmount = 0;
$existingamount = 0;
$existingChildAmount = 0;
$existingAdultAmount = 0;
if (($childAmount != 0 || $adultAmount != 0) && $amount == 0) {
// food reservation is being added
$isFoodReservation = true;
}
if(isset($_SESSION["Tickets"])){
if ($isFoodReservation) {
// calculate existing food amounts in session (in 'cart')
foreach(EncryptionHelper::Decrypt($_SESSION["Tickets"]) as $item){
if ($item["EventId"] == $eventId) {
$existingChildAmount = $item["childAmount"];
$existingAdultAmount = $item["adultAmount"];
}
}
} else {
// calculate existing amounts in session (in 'cart')
foreach(EncryptionHelper::Decrypt($_SESSION["Tickets"]) as $item){
if($item["EventId"] == $eventId){
$existingamount = $item["Amount"];
}
}
}
}
// grab the 'max' amount allowed to be added (tickets left)
switch ($TypeEvent) {
case 1:
$maxAmount = $OrderRepository->GetTicketAmountFood($eventId);
break;
case 2:
$maxAmount = $OrderRepository->GetTicketAmountDance($eventId);
break;
case 3:
$historic = $OrderRepository->GetTicketAmountHistoric($eventId);
$maxAmount = $historic['Amount'];
$familyTourByReferenceId = $HistoricRepository->GetToursByReferenceId($eventId);
if ($historic['Type'] == 'Family') {
$amount = $amount * 4;
}
foreach(EncryptionHelper::Decrypt($_SESSION["Tickets"]) as $item) {
if ($familyTourByReferenceId == $item["EventId"]) {
$existingamount += $item['Amount'];
}
if ($historic['ReferenceId'] != 0 && $item["EventId"] == $historic['ReferenceId']) {
$existingamount += $item["Amount"];
}
}
break;
case 4:
$maxAmount = $OrderRepository->GetTicketAmountJazz($eventId);
break;
}
// calculate if the tickets being added + the tickets already in cart don't exceed or are equal to the remaining tickets. Act accordingly
if ($isFoodReservation) {
// we're adding a food reservation, call different method ...
if ($childAmount + $adultAmount + $existingChildAmount + $existingAdultAmount <= $maxAmount) {
// not exceeding or equal to, add reservation to cart
$session->AddToCartFood($eventId, $childAmount, $adultAmount, $_POST['startTime'], $_POST['date'], $_POST['extraInfo']);
print 1;
}
else {
print 0;
}
} else {
// adding normal tickets ...
if ($amount + $existingamount <= $maxAmount) {
// not exceeding or equal to, add tickets to cart
$session->AddToCart($eventId, $TypeEvent, $amount);
print 1;
}
else {
print 0;
}
}
}
?>