-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexpense_handler.php
116 lines (98 loc) · 4.37 KB
/
expense_handler.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
<?php
include_once('lock.php');
include_once('config.db.php');
include_once('date_handler.php');
include_once('plugins/KLogger.php');
$crntYear = getYear();
$crntMonth = getMonth();
$crntDay = getDay();
$log = new KLogger ("/var/www/extracker/logs/", KLogger::DEBUG);
function deleteRecord($db, $recId)
{
$log = new KLogger ("/var/www/extracker/logs/", KLogger::DEBUG);
$log->logDebug("Delete expense record id [" . $recId . "]");
$delquery = "DELETE FROM expences WHERE id='$recId'";
mysqli_query($db, $delquery);
}
function updateDayaccount($db, $amount, $year, $month, $day)
{
$log = new KLogger ("/var/www/extracker/logs/", KLogger::DEBUG);
$log->logDebug("Updating Daily summary tables");
$selectDay = "SELECT total FROM summary_day WHERE year='$year' AND month='$month' AND day='$day'";
$dayTot = mysqli_query($db, $selectDay);
$row = mysqli_fetch_array($dayTot);
$day_total = $row["total"] + $amount;
if (mysqli_num_rows($dayTot) == 0) {
//$fulld = $year . '-' . $month . '-' . $day;
$insertDay = "INSERT INTO summary_day VALUES ($year, $month, $day, 0)";
mysqli_query($db, $insertDay);
}
$updateDay = "UPDATE summary_day SET total='$day_total' WHERE year='$year' AND month='$month' AND day='$day'";
mysqli_query($db, $updateDay);
}
function updateMonthaccount($db, $amount, $year, $month)
{
$log = new KLogger ("/var/www/extracker/logs/", KLogger::DEBUG);
$log->logDebug("updating monthly summary tables");
$selectMonth = "SELECT total FROM summary_month WHERE year='$year' AND month='$month'";
$monthTot = mysqli_query($db, $selectMonth);
$row = mysqli_fetch_array($monthTot);
$month_total = $row["total"] + $amount;
if (mysqli_num_rows($monthTot) == 0) {
$insertMonth = "INSERT INTO summary_month VALUES ($year, $month, 0)";
mysqli_query($db, $insertMonth);
}
$updateMonth = "UPDATE summary_month SET total='$month_total' WHERE year='$year' AND month='$month'";
mysqli_query($db, $updateMonth);
}
function updateYearaccount($db, $amount, $year)
{
$log = new KLogger ("/var/www/extracker/logs/", KLogger::DEBUG);
$log->logDebug("updating yearly summary tables");
$selectYear = "SELECT total FROM summary_year WHERE year='$year'";
$yearTot = mysqli_query($db, $selectYear);
$row = mysqli_fetch_array($yearTot);
$year_total = $row["total"] + $amount;
if (mysqli_num_rows($yearTot) == 0) {
$insertYear = "INSERT INTO summary_year VALUES ($year, 0)";
mysqli_query($db, $insertYear);
}
$updateYear = "UPDATE summary_year SET total='$year_total' WHERE year='$year'";
mysqli_query($db, $updateYear);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$date = $_REQUEST['datepicker_send'];
// break day down
$eday = substr($date, 3, 2);
$emonth = substr($date, 0, 2);
$eyear = substr($date, 6, 4);
$desc = addslashes($_POST['desc']);
$catgry = addslashes($_POST['cat']);
$amnt = addslashes($_POST['aamount']);
// record delete
$delRec = addslashes($_POST['recId']);
$delAmnt = addslashes($_POST['amount']);
if ($date == "" || $desc == "" || $catgry == "" || $amnt == "") {
if ($delRec == "") {
$error = "please enter all the details";
header("Location: expenses.php?error=" . $error);
} elseif ($delRec != "" && $delAmnt != "") {
$delRec = addslashes($_POST['recId']);
$delAmnt = str_replace(",", "", addslashes($_POST['amount']));
deleteRecord($db, $delRec);
updateDayaccount($db, -$delAmnt, $crntYear, $crntMonth, $crntDay);
updateMonthaccount($db, -$delAmnt, $crntYear, $crntMonth);
updateYearaccount($db, -$delAmnt, $crntYear);
$success = "Record deleted successfully";
header("Location: expenses.php?success=" . $success);
}
} else {
$insertq = "INSERT INTO expences(`id`, `year`, `month`, `day`, `category_id`, `amount`, `userid`, `description`) VALUES (NULL, '$eyear', '$emonth', '$eday', '$catgry', '$amnt', '$login_id', '$desc')";
mysqli_query($db, $insertq);
updateDayaccount($db, $amnt, $eyear, $emonth, $eday);
updateMonthaccount($db, $amnt, $eyear, $emonth);
updateYearaccount($db, $amnt, $eyear);
$success = "Record added successfully";
header("Location: expenses.php?success=" . $success);
}
}