/
edit_budgets.php
306 lines (256 loc) · 8.27 KB
/
edit_budgets.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
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
<?php
$current_page = 'edit_budgets';
require('include.php');
function initBudgetDate() {
$budgetDate = new DateTime();
if (isset ($_POST['budget_date']))
{
// Expect date as MM/YYYY
$dateArr = explode('/', $_POST['budget_date']);
if (count($dateArr) != 2) {
$error = "Budget date must be in MM/YYYY format";
} elseif ($dateArr[0] < 1 || $dateArr[0] > 12) {
$error = "Budget date must be in MM/YYYY format";
} elseif ($dateArr[1] > 2100) {
$error = "Budget date year cannot go beyond 2100";
} else {
$budgetTime = mktime(0, 0, 0, $dateArr[0], 1, $dateArr[1]);
$budgetDate->setTimestamp($budgetTime);
}
} else {
// default date: first day of current month
$dateArr = getdate();
$budgetDate->setDate($dateArr['year'], $dateArr['mon'], 1);
}
return $budgetDate;
}
if (!isset ($_SESSION['login_id']))
{
// redirect to login if they have not logged in
header ("Location: login.php");
}
$error = '';
$message = '';
// set default vars
// Set budget date to first day of month
$budgetDate = initBudgetDate();
// default account comes from DB
$account_id = $_SESSION['default_summary2'];
if (isset ($_POST['account_id'])) {
$account_id = $_POST['account_id'];
}
if (isset ($_POST['prev_month']))
{
// go back one month
$budgetDate->sub(new DateInterval('P1M'));
}
elseif (isset ($_POST['next_month']))
{
// forward one month
$budgetDate->add(new DateInterval('P1M'));
} elseif (isset($_POST['saveBudget'])) {
// try saving
$newBudgets = $_POST['budgetAmounts'];
$accountIds = $_POST['accountIds'];
$budgetIds = $_POST['budgetIds'];
$defaultBudgets = $_POST['defaultBudgets'];
$budgetComments = $_POST['budgetComments'];
$pdo = db_connect_pdo();
$t1 = microtime(true);
$pdo->beginTransaction();
$t2 = microtime(true);
$ps = NULL;
for ($i = 0; $i < count($accountIds); $i++) {
$accountId = $accountIds[$i];
$newBudget = $newBudgets[$i];
$budgetId = $budgetIds[$i];
$defaultBudget = $defaultBudgets[$i];
$comments = $budgetComments[$i];
if (empty($budgetId)) {
// not editing, so set to -1
$budgetId = -1;
}
// update budget
$budget = new Budget();
$budget->Init_budget($accountId, $budgetDate, $newBudget, $comments, $budgetId);
$error = $budget->Save($pdo, $ps);
if ($error != '') {
break;
}
// update default budget
$account = new Account();
$account->Init_for_budget_update($accountId, $defaultBudget);
$error = $account->Update_budget_default($pdo, $ps);
if ($error != '') {
break;
}
}
if ($error == '') {
$t3 = microtime(true);
$pdo->commit();
$t4 = microtime(true);
$txTime += $t2 - $t1 + $t4 - $t3;
$message = 'Successfully saved ' . count($accountIds).
' budget record(s)';
} else {
// $pdo->rollBack();
}
$pdo = NULL;
$ps = NULL;
}
$budgetDateText = $budgetDate->format('m/Y');
// build account dropdowns: include inactive, and only show top 2 tiers
$account_list = Account::Get_account_list ($_SESSION['login_id'],
'', -1, false, false, true, true);
$account_dropdown = Build_dropdown ($account_list, 'account_id',
$account_id);
// Build main data list
$budget_list = array(); // pass by reference
if ($error == '') {
$error = Account::Get_account_budgets($budgetDate,
$account_id, $budget_list);
}
// Get income transactions
$income_list = array(); // pass by ref
if ($error == '') {
$error = Account::Get_income($budgetDate, $login_id, $income_list);
}
// Get total income
$totalIncome = 0.0;
foreach ($income_list as $income) {
$totalIncome += $income->amount;
}
?>
<html>
<head>
<title>Budget</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script language="javascript" type="text/javascript">
function bodyLoad()
{
document.forms[0].budget_date.focus();
}
</script>
</head>
<body onload="bodyLoad()">
<?= $navbar ?>
<h3>Budget</h3>
<span class="error"><?= $error ?></span>
<span class="message"><?= $message ?></span>
<form action="edit_budgets.php" method="post">
<table>
<tr>
<td>Budget Month (MM/YYYY): </td>
<td><input type="text" maxlength="7" name="budget_date" value="<?= $budgetDateText ?>" /></td>
<td> <input type="submit" value="Update" name="update_date"></td>
</tr>
<tr>
<td>Top Account: </td>
<td colspan="2"><?= $account_dropdown ?></td>
<!-- Month direction arrows -->
<td colspan="1"> <input type="submit" value="<" name="prev_month" />
<input type="submit" value=">" name="next_month" /></td>
</tr>
</table>
</form>
<form action="edit_budgets.php" method="post">
<!-- Copy upper form params here -->
<input type="hidden" name="budget_date" value="<?= $budgetDateText ?>" />
<input type="hidden" name="account_id" value="<?= $account_id ?>" />
<table class="budget-list" cellspacing="0" cellpadding="0">
<tr>
<th>Account</th>
<th style="text-align: center;">Default Budget</th>
<th style="text-align: center;">Curent</th>
<th style="text-align: center;">New Budget</th>
<th style="text-align: center;">Budget Comment</th>
</tr>
<?php
// First loop through data: calculate totals
$defaultTotal = 0.0;
$budgetTotal = 0.0;
// Need the abs_total to calculate row percentages
foreach ($budget_list as $account_data)
{
// account_list (account_name, account_total, account_id)
$defaultTotal += $account_data[1];
$budgetTotal += $account_data[2];
}
// Second loop: display data
// foreach ($account_list as $account_data)
foreach ($budget_list as $account_id => $budget_data)
{
$accountName = htmlspecialchars($budget_data[0]);
$defaultBudget = format_amount($budget_data[1]);
$budgetAmount = format_amount($budget_data[2]);
$budgetStyle = '';
if ($defaultBudget != $budgetAmount) {
// This month's budget is not the default
$budgetStyle = 'color: red;"';
}
$budgetId = $budget_data[3];
$accountDescr = htmlspecialchars($budget_data[4]);
$budgetComment = htmlspecialchars($budget_data[5]);
$newBudget = $budgetAmount;
if ($budgetAmount == null) {
// Apply default to budget when undefined
$newBudget = $defaultBudget;
}
echo " <tr> \n".
" <input type='hidden' name='accountIds[]' value='$account_id' />".
" <input type='hidden' name='budgetIds[]' value='$budgetId' />".
" <td title=\"$accountDescr\">$accountName</td> \n".
" <td><input type='text' name='defaultBudgets[]' ".
"maxlength='9' value='$defaultBudget' size='10' /></td> \n".
" <td style='text-align: right; $budgetStyle'>$budgetAmount</td> \n".
" <td><input type='text' name='budgetAmounts[]' ".
"maxlength='9' value='$newBudget' size='10' /></td> \n".
" <td><input style='text-align: left;' type='text' name='budgetComments[]' ".
"maxlength='100' size='50' value=\"$budgetComment\" /></td> \n".
" </tr> \n\n" ;
} // End budget loop
$defaultTotalString = format_currency($defaultTotal);
$budgetTotalString = format_currency($budgetTotal);
echo " <tr> \n".
" <td style='border-top: 1px solid black; border-bottom: 1px solid black;' ".
" colspan='5'> </td> \n".
" </tr> \n\n".
" <tr> \n".
" <td>Total</td> \n".
" <td style='text-align: right; font-weight: bold;'>$defaultTotalString</td> \n".
" <td style='text-align: right; font-weight: bold;'>$budgetTotalString</td> \n";
?>
<td colspan="1" style="text-align: right;">
<input style="margin-top: 5px; margin-bottom: 5px;" type="submit"
name="saveBudget" value="Save Budgets" />
</td>
</tr>
<tr>
<th colspan="2">Income Account</th>
<th class="numeric">Amount</th>
<th class="numeric">Total</th>
<th>Transaction Description</th>
</tr>
<?php
foreach ($income_list as $income) {
echo " <tr> <td>". $income->accountName . "</td><td></td> \n".
"<td class='numeric'>". format_currency($income->amount) . "</td> \n".
"<td></td> \n".
"<td>". $income->transDescr . "</td>\n".
"</tr> \n";
}
$totalUnbudgeted = $totalIncome - $budgetTotal;
echo "<tr><td style='font-weight: bold;'>Total Income</td> <td></td> <td></td> ".
"<td class='total'>" . format_currency($totalIncome) . "</td> \n".
"</tr>";
echo "<tr><td style='font-weight: bold;'>Unbudgeted Amount</td> <td></td> <td></td> ".
"<td class='total'>" . format_currency($totalUnbudgeted) . "</td> \n".
"</tr>";
?>
<tr>
<td colspan="5"><?php require('footer.php'); ?></td>
</tr>
</table>
</form>
</body>
</html>