-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnewTrans.php
More file actions
180 lines (155 loc) · 7.2 KB
/
newTrans.php
File metadata and controls
180 lines (155 loc) · 7.2 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
<?php
session_start();
//Basic Info
$cust_no = filter_input(INPUT_POST, 'cust_no');
$name = filter_input(INPUT_POST, 'name');
$address = filter_input(INPUT_POST, 'address');
$phone = filter_input(INPUT_POST, 'phone');
//Transaction Details
$date = filter_input(INPUT_POST, 'date');
$rebate = filter_input(INPUT_POST, 'rebate');
$serial = filter_input(INPUT_POST, 'serial');
$amount = filter_input(INPUT_POST, 'amount');
$package_no = filter_input(INPUT_POST, 'package_no');
//Loan Information
$loan = filter_input(INPUT_POST, 'loan');
$start = filter_input(INPUT_POST, 'start');
$end = filter_input(INPUT_POST, 'end');
$months = filter_input(INPUT_POST, 'months');
$balance = filter_input(INPUT_POST, 'balance');
//Generate a random pin for customer number and deal numbers
$custNo = "C";
$dealNo = "D";
$empNo = $_SESSION["emp_no"];
//Assign random pin number deal number to all transactions and customer number to all new customers
if($cust_no == "new")
{
$i = 0; $digits = 5;
while($i < $digits)
{
//generate a random number between 0 and 9.
$custNo .= mt_rand(1, 9);
$dealNo .= mt_rand(1, 9);
$i++;
}
} else
{
$custNo = $cust_no;
$i = 0; $digits = 5;
while($i < $digits)
{
$dealNo .= mt_rand(1, 9);
$i++;
}
}
//Get necessary information from multiple tables
if(isset($_SESSION['salesrep1']))
{
$connect = mysqli_connect("127.0.0.1", "root", "", "dealer_one");
$query = "SELECT * FROM cars WHERE serial_no = '$serial'";
$query1 = "SELECT * FROM customer_d1 WHERE name='$name' AND address='$address' AND phone='$phone'";
$query2 = "SELECT model FROM rebate1 WHERE rebate_no = '$rebate'";
} else if(isset($_SESSION['salesrep2']))
{
$connect = mysqli_connect("127.0.0.1", "root", "", "dealer_two");
$query = "SELECT * FROM autos WHERE vehicle_no = '$serial'";
$query1 = "SELECT * FROM customer_d2 WHERE name='$name' AND address='$address' AND phone='$phone'";
$query2 = "SELECT model FROM rebate2 WHERE rebate_no = '$rebate'";
}
//Select from the vehicle tables and assign necessary values to variables
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($result))
{
$model = $row['model'];
$color = $row['color'];
$autotrans = $row['autotrans'];
$warehouse = $row['warehouse'];
}
//If the customer is not new check if inputed basic info matches the info of the selected $cust_no
if($cust_no != "new")
{
$result1 = mysqli_query($connect, $query1); //Select from customer tables
if(($result1->num_rows == 0))
{
//echo "Something went wrong" . "<br>" . $connect->error;
echo "<script>alert('Name, address, or phone does not match existing record');
window.location.href='addTrans.html';</script>";
exit();
}
}
//If a rebate has been selected Check to see if the model matches the model of the vehicle being bought
if($rebate != "")
{
$result2 = mysqli_query($connect, $query2);
while($row = mysqli_fetch_assoc($result2))
{
$rebateModel = $row['model'];
}
if($rebateModel != $model)
{
echo "<script>alert('Model of selected rebate does not match model of vehicle being purchased');
window.location.href='addTrans.html';</script>";
exit();
}
}
//Adding all of the posted information into the appropriate tables
if(isset($_SESSION['salesrep1']))
{
$connection = mysqli_connect("127.0.0.1", "root", "", "dealer_one");
//Insert the information into the appropriate tables
if($cust_no == "new")
{
//Only add to the customer table if the customer is new
$sql1 = "INSERT INTO customer_d1 (customer_no, name, address, phone)
VALUES ('$custNo', '$name', '$address', '$phone')";
}
$sql2 = "INSERT INTO purchased_cars (serial_no, model, color, autotrans, warehouse, amount)
VALUES ('$serial', '$model', '$color', '$autotrans', '$warehouse', '$amount')";
$sql3 = "INSERT INTO loan (serial_no, customer_no, amount, start_date, end_date, months, balance)
VALUES ('$serial', '$custNo', '$loan', '$start', '$end', '$months', '$balance')";
$sql4 = "INSERT INTO transaction (deal_no, rebate_no, package_no, rep_no, customer_no, serial_no, amount, fin_amt, date)
VALUES ('$dealNo', '$rebate', '$package_no', '$empNo', '$custNo', '$serial', '$amount', '$loan', '$date')";
$sql5 = "DELETE FROM cars where serial_no = '$serial'";
} else if(isset($_SESSION['salesrep2']))
{
$connection = mysqli_connect("127.0.0.1", "root", "", "dealer_two");
//Insert the information into the appropriate tables
if($cust_no == "new")
{
//Only add to the customer table if the customer is new
$sql1 = "INSERT INTO customer_d2 (buyer_no, name, address, phone)
VALUES ('$custNo', '$name', '$address', '$phone')";
}
$sql2 = "INSERT INTO purchased_autos (vehicle_no, model, color, autotrans, warehouse, amount)
VALUES ('$serial', '$model', '$color', '$autotrans', '$warehouse', '$amount')";
$sql3 = "INSERT INTO finance (vehicle_no, buyer_no, amount, start_date, end_date, months, balance)
VALUES ('$serial', '$custNo', '$loan', '$start', '$end', '$months', '$balance')";
$sql4 = "INSERT INTO deal (deal_no, rebate_no, package_no, sale_no, buyer_no, vehicle_no, amount, fin_amt, date)
VALUES ('$dealNo', '$rebate', '$package_no', '$empNo', '$custNo', '$serial', '$amount', '$loan', '$date')";
$sql5 = "DELETE FROM autos where vehicle_no = '$serial'";
}
//Redirect if successfully added or alert if something went wrong
if($cust_no == "new")
{
if ($connection->query($sql1) === TRUE && $connection->query($sql2) === TRUE && $connection->query($sql3) === TRUE
&& $connection->query($sql4) === TRUE && $connection->query($sql5) === TRUE)
{
echo "<script>alert('Transaction Successfully Made'); window.location.href='empAccount.html';</script>";
} else
{
echo "Something went wrong" . "<br>" . $connection->error;
//echo "<script>alert('Something went wrong'); window.location.href='addTrans.html';</script>";
}
} else
{
if ($connection->query($sql2) === TRUE && $connection->query($sql3) === TRUE
&& $connection->query($sql4) === TRUE && $connection->query($sql5) === TRUE)
{
echo "<script>alert('Transaction Successfully Made'); window.location.href='empAccount.html';</script>";
} else
{
echo "Something went wrong" . "<br>" . $connection->error;
//echo "<script>alert('Something went wrong'); window.location.href='addTrans.html';</script>";
}
}
?>