forked from udsaxman/Sapphirev2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processOp.php
269 lines (213 loc) · 7.07 KB
/
processOp.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
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
<?php
include 'connection.php';
//ini_set('display_errors', 1);
//error_reporting(E_ALL | E_STRICT);
//echo "Are you receiving?";
//WE NEED TO VALIDATE INPUT SO WE DON'T HAVE STRINGS IN INTs
$isExsist = true;
//$pilotArray[0] = 0;
$pilotIDArray[0] = 0;
$pilotCount = 0;
$shareArray[0] = 0;
$processorName = "";
$processorID = 0;
$op_id = 0;
$opName = "";
$opDate = "";
$opValue = 0;
$opTax = 0;
$opTaxAmount = 0;
$totalShares = 0;
$shareValue = 0;
$itemArray[0] = "";
$itemAmountArray[0] = 0;
$itemTotalValue[0] = 0;
//Items are 1 based
$itemCount = 1;
while ($isExsist == true) {
//These need to be changed to $_POST
//Items were ordered by ItemId so they should be stored the same way too?
if (isset($_POST["itemName" . $itemCount])) {
$itemArray[$itemCount] = $_POST["itemName" . $itemCount];
$itemAmountArray[$itemCount] = $_POST["itemAmount" . $itemCount];
$itemCount++;
} else {
$isExsist = false;
}
}
$totalUserCount = 0;
$sql = "Select Count(user_id) as Count from Users";
$result = mysql_query($sql, $conn) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $name => $value) {
if ($name == "Count") {
$totalUserCount = $value;
}
}
}
for ($i = 0; $i < ($totalUserCount + 1); $i++) {
if (isset($_POST["selType" . $i])) {
if ($_POST["selType" . $i] != 0) {
$pilotIDArray[$pilotCount] = $_POST["selType" . $i];
$shareArray[$pilotCount] = $_POST["Shares" . $i];
$pilotCount++;
}
}
}
if (isset($_POST["name"])) {
$processorName = $_POST["name"];
} else {
$processorName = "Error_No_Name_Was_Found";
}
if (isset($_POST["opName"])) {
$opName = $_POST["opName"];
} else {
$opName = "Error_No_Op_Name_Found";
}
if (isset($_POST["opDate"])) {
$opDate = $_POST["opDate"];
} else {
$opDate = "Error_No_Op_Date_Found";
}
if (isset($_POST["opTax"])) {
$opTax = $_POST["opTax"];
} else {
$opTax = 0;
}
$sql = "select user_id from Users where user_name = '" . $processorName . "'";
$result = mysql_query($sql, $conn) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $name => $value) {
if ($name == "user_id") {
$processorID = $value;
}
}
}
$itemValue[0] = 0;
$useTax[0] = true;
$itemTax[0] = 0;
$itemTotalValue[0] = 0;
//Calculate Op Value
//Items are 1 based
for ($i = 1; $i < $itemCount; $i++) {
//This is bad, Should just select all from items and then after you store them, use php to search the array
$sql = "Select
item_iskValue,
category_taxOverride,
category_useOverride
From
Items
Left Join
Item_Category on category_id = item_type
Where item_name = '" . $itemArray[$i] . "'";
$result = mysql_query($sql, $conn) or die(mysql_error());
//echo("itemArray: ". $itemArray[$i]);
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $name => $value) {
if ($name == "item_iskValue") {
$itemAmountArray[$i] = str_replace(",", "", $itemAmountArray[$i]);
$itemValue[$i] = (float)$value * (float)$itemAmountArray[$i];
}
if ($name == "category_taxOverride") {
$itemTax[$i] = $value;
}
if ($name == "category_useOverride") {
$useTax[$i] = $value;
}
}
}
}
for ($i = 0; $i < $itemCount; $i++) {
if ($useTax[$i] == 0) {
$tempTax = (float)$itemValue[$i] * ((float)$opTax / 100.0);
(float)$itemTotalValue[$i] = (float)$itemValue[$i] - (float)$tempTax;
(float)$opTaxAmount += (float)$tempTax;
} else {
(float)$tempTax = (float)$itemValue[$i] * ((float)$itemTax[$i] / 100.0);
(float)$itemTotalValue[$i] = (float)$itemValue[$i] - (float)$tempTax;
(float)$opTaxAmount += (float)$tempTax;
}
}
for ($i = 0; $i < $itemCount; $i++) {
(float)$opValue += (float)$itemTotalValue[$i];
}
/*$opTaxAmount = (float)$opValue * ((float)$opTax/100.0);
$opValue -= (float)$opTaxAmount;*/
for ($i = 0; $i < $pilotCount; $i++) {
$shareArray[$i] = str_replace(",", "", $shareArray[$i]);
if ($shareArray[$i] < 0) {
$shareArray[$i] = 0;
}
$totalShares += (int)$shareArray[$i];
}
if ($totalShares > 0) {
$shareValue = (float)$opValue / (float)$totalShares;
} else {
$shareValue = 0.00;
}
//CreateNew Op
$strSearch = "'";
$pos = stripos($opName, $strSearch);
if ($pos === false && trim($opName) != "") {
} else {
$opName = "DefaultOpName";
}
if (trim($opTax) == "" || !is_numeric($opTax)) {
$opTax = 0.00;
}
$sql = "Call sp_AddNewOp('" . $opName . "', " . $processorID . ", '" . $opDate . "', " . $opTax . ", " . $opTaxAmount . ", " . $shareValue . ", " . $opValue . ")";
$result = mysql_query($sql, $conn) or die(mysql_error());
//$sql = "select op_id from Ops";
$sql = "select op_id from Ops Order by op_id desc limit 1";
$result = mysql_query($sql, $conn) or die(mysql_error());
//We get ALL the op_ids, but in the end we are only storing the last one
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $name => $value) {
if ($name == "op_id") {
$op_id = $value;
}
}
}
//Then Create Op_Attendance
for ($i = 0; $i < $pilotCount; $i++) {
if ($shareArray[$i] > 0 && trim($shareArray[$i]) != "" && is_numeric($shareArray[$i])) {
$sql = "Call sp_AddUserToOpAttendence(" . $op_id . ", " . $pilotIDArray[$i] . ", " . $shareArray[$i] . ")";
$result = mysql_query($sql, $conn) or die(mysql_error());
//Add Money to people's Wallets
$sql = "Select user_isk From Users Where user_id = " . $pilotIDArray[$i] . "";
$result = mysql_query($sql, $conn) or die(mysql_error());
//This all should be done at the beginning or something
//Just seems like I'm going to be flooding the Database with ALL the things
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $name => $value) {
if ($name == "user_isk") {
$tempIsk = $value;
}
}
}
$tempIsk += ($shareValue * $shareArray[$i]);
$sql = "Call sp_OpTransaction(" . $pilotIDArray[$i] . ", " . $op_id . ", " . ($shareValue * $shareArray[$i]) . ", '" . $opDate . "')";
$result = mysql_query($sql, $conn) or die(mysql_error());
$sql = "Call sp_UpdateUserIsk(" . $tempIsk . ", " . $pilotIDArray[$i] . ")";
$result = mysql_query($sql, $conn) or die(mysql_error());
}
}
//Then Create Op_Loot
for ($i = 1; $i < $itemCount; $i++) {
if ($itemAmountArray[$i] != 0 && trim($itemAmountArray[$i]) != "" && is_numeric($itemAmountArray[$i])) {
$sql = "Call sp_AddItemToOpLoot(" . $op_id . ", " . $i . ", " . $itemAmountArray[$i] . ")";
$result = mysql_query($sql, $conn) or die(mysql_error());
}
}
Success();
//echo("Op Value: ". $opValue);
function Fail($error)
{
header('Location: ./TDSInError.php?Error=' . $error . '');
}
function Success()
{
//Go to View Op
header('Location: ./TDSInAdminTools.php');
}
?>