-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateBudget.php
55 lines (50 loc) · 1.74 KB
/
CreateBudget.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
<?php
session_start();
require __DIR__ . '/AuxFunctions.php';
$data = json_decode(file_get_contents('php://input'));
if(!isset($_SESSION['userID'])){
header("Location: ./Login.html");
}
$userID = $_SESSION['userID'];
$state = $data->{"StateID"};
$salary = $data->{"Salary"};
// get state tax bracket
$db = new mysqli("classroom.cs.unc.edu", "kjbass", "426password!", "kjbassdb");
$stmt = $db->prepare('SELECT StateBracketID
FROM ProjStateTax s
WHERE s.StateID=?
AND ? between s.MinIncome AND s.MaxIncome;');
$stmt->bind_param('si', $state, $salary);
$stmt->execute();
$result = $stmt->get_result();
$result = $result->fetch_assoc();
$stateBracketID = $result['StateBracketID'];
// get federal tax bracket
$stmt = $db->prepare('SELECT FedBracketID
FROM ProjFedTax f
WHERE ? between f.MinIncome AND f.MaxIncome;');
$stmt->bind_param('i', $salary);
$stmt->execute();
$result = $stmt->get_result();
$result = $result->fetch_assoc();
$fedBracketID = $result['FedBracketID'];
// Insert into Budget table
$stmt = $db->prepare('INSERT INTO ProjBudget(UserID, Salary, FedBracketID, StateBracketID) VALUES(?,?,?,?);
');
$stmt->bind_param('siii', $userID, $salary, $fedBracketID, $stateBracketID);
$stmt->execute();
// Insert default splits
$stmt = $db->prepare('SELECT BudgetID
FROM ProjBudget b
WHERE b.UserID = ?;
');
$stmt->bind_param('s', $userID);
$stmt->execute();
$result = $stmt->get_result();
$result = $result->fetch_assoc();
$budgetID = $result['BudgetID'];
insertSplit($budgetID, "Necessities", 50, $db);
insertSplit($budgetID, "Goals", 20, $db);
insertSplit($budgetID, "Wants", 30, $db);
echo json_encode(getBudget($budgetID, $userID, $db));
?>