-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpense_distribution.php
More file actions
303 lines (273 loc) · 12.7 KB
/
expense_distribution.php
File metadata and controls
303 lines (273 loc) · 12.7 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
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
<?php
require 'utilities/common.php';
if (!isset($_SESSION['email']))
{ header("location: index.php"); }
// Extracting the plan_no which was passed via url by view.php page
$plan_no = $_GET['plan'];
// Query to fetch the plan from the plans table:
$search_query = "SELECT * FROM plans WHERE id = '$plan_no'";
$result_search_query = mysqli_query($connect, $search_query) or die(mysqli_error($connect));
$row = mysqli_fetch_array($result_search_query);
// Query to fetch the expenses related to the plans:
$query = "SELECT * FROM expense WHERE p_id = $plan_no";
$result_query = mysqli_query($connect, $query) or die(mysqli_error($connect));
/*/ Query to fetch the expenses related to the plans:
I have again executed the same query because I will use the same query over two places
and I can't use $row over the two places. SO I have executed the same query so that I can
use them via $row and $row1 respectively*/
$result_query1 = mysqli_query($connect, $query) or die(mysqli_error($connect));
// Array to store the amount spent by the person:
$total = array();
// This array will store the each expense in a plan.
// Dynamically adds the amount spent in each expense:
while ($rows = mysqli_fetch_array($result_query))
{
// Check the value of the amount spent by the person
$f = 1;
$sum = 0;
// Say this plan has 2 people in it, so the loop will run 2 times
for ($f; $f <= $row['people']; $f++) {
/* As in every expense, only one of the person would have spent,
So the loop stops when there is a numeric value present and it
continues of there is NULL value */
if ($rows['Person' . $f] != NULL) {
$sum += $rows['Person' . $f];
break;
}
}
// Using the array_push method to put all the amount of expense in the total() array.
array_push($total, $sum);
}
/* As after this loop there will be an array having the each expenses, so I have used the
foreach loop to sum the values together. */
$tot = 0;
foreach ($total as $t)
{
// This is the total expenditure
$tot += $t;
}
/*As per the functioning of the project, in the expense distribution page, each person
will be stated by First, Second, Third and so on. So by this person I can find the alphabetical
equivalent of the person in numbers.Say if Person1 spent 20 Rs, then in the UI it will be First: 20.
This function will do that conversion*/
/*For the sale of simplicity I have done the conversion of 5 peoples.*/
function findPerson($a)
{
if ($a == 1)
return "First";
elseif ($a == 2)
return "Second";
elseif ($a == 3)
return "Third";
elseif ($a == 4)
return "Fourth";
else
return "Fifth";
}
// Function to sum the array elements:
function arraySum($arr)
{
$arrSum = 0;
foreach ($arr as $a) {
$arrSum += $a;
}
return $arrSum;
}
/*This function is used at the line 188, where I have to print statements according
to the different conditions */
function text($n)
{
if ($n > 0) {
echo "<p> <font color=#32de84>Gets back ₹ $n</font> </p>";
} elseif ($n < 0) {
$n = abs($n);
echo "<p> <font color=red>Owes ₹ $n</font> </p>";
} else {
echo "<p> <font color=black>All Settled Up..</font> </p>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Control Budget | Expense Distribution</title>
<script src="https://kit.fontawesome.com/d5d0f39f2f.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
include 'utilities/navbar.php';
?>
<?php
// Arrays to store the individual amount sum:
$First = array();
$Second = array();
$Third = array();
$Fourth = array();
$Fifth = array();
/* Appraoch to insert amount values to the correct person in case
the person has more than one expenses*/
while ($rows1 = mysqli_fetch_array($result_query1)) {
$d = 1;
while ($d <= $row['people']) {
if ($rows1['Person' . $d] != NULL) {
if ($d == 1) {
array_push($First, $rows1['Person' . $d]);
} elseif ($d == 2) {
array_push($Second, $rows1['Person' . $d]);
} elseif ($d == 3) {
array_push($Third, $rows1['Person' . $d]);
} elseif ($d == 4) {
array_push($Fourth, $rows1['Person' . $d]);
}
}
$d++;
}
}
?>
<!-- Expense Distribution -->
<section id="login">
<div class="container">
<div class="row">
<div class="col-md-6 mx-auto mb-5">
<div class="card mb-5">
<div class="card-header text-center" style="background: #c5d5c5">
<div class="row">
<div class="col-sm-9">
<h4><?php echo $row['title'] ?></h4>
</div>
<div class="col-sm-3 text-right" style="font-size: 20px">
<i class="fas fa-user"></i> <?php echo $row['people'] ?>
</div>
</div>
</div>
<div class="card-body py-4">
<div class="row">
<div class="col-sm-9">
<p>Initial Budget</p>
</div>
<div class="col-sm-3 pl-5 font-weight-bold text-right">
₹ <?php echo $row['budget'] ?>
</div>
</div>
<?php
$c = 1;
while ($c <= $row['people']) {
?>
<div class="row">
<div class="col-sm-9">
<p><?php echo findPerson($c) ?></p>
</div>
<div class="col-sm-3 pl-5 text-right"> ₹
<!--Logic to print the amount spent by the person: -->
<?php
if ($c == 1) {
echo arraySum($First);
} elseif ($c == 2) {
echo arraySum($Second);
} elseif ($c == 3) {
echo arraySum($Third);
} elseif ($c == 4) {
echo arraySum($Fourth);
} else {
echo arraySum($Fiifth);
}
?>
</div>
</div>
<?php
$c++;
}
?>
<div class="row">
<div class="col-sm-9">
<p>Total Amount Spent</p>
</div>
<div class="col-sm-3 pl-5 font-weight-bold text-right">
₹ <?php echo $tot ?>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p>Remaining Amount</p>
</div>
<div class="col-sm-6 pl-5 text-right font-weight-bold">
<?php
$r = ($row['budget'] - $tot);
$a = abs($r);
if ($r < 0) {
echo "<p> <font color=red>Overspent by ₹ $a</font> </p>";
} elseif ($r > 0) {
echo "<p> <font color=#32de84>₹ $r</font> </p>";
} else {
echo "<p> <font color=black>₹ $r</font> </p>";
}
?>
</div>
</div>
<div class="row">
<div class="col-sm-9">
<p>Individual Shares</p>
</div>
<div class="col-sm-3 pl-5 text-right">
₹ <?php echo round($tot / $row['people']) ?>
<!-- Used the round function to avoid fractions -->
</div>
</div>
<?php
$c = 1;
while ($c <= $row['people']) {
?>
<div class="row">
<div class="col-sm-7">
<p><?php echo findPerson($c) ?></p>
</div>
<div class="col-sm-5 pl-5 text-right font-weight-bold" id="color">
<?php
// Used round() as to neglect the negative numbers
$a = round($tot / $row['people']);
if ($c == 1) {
$b = arraySum($First);
} elseif ($c == 2) {
$b = arraySum($Second);
} elseif ($c == 3) {
$b = arraySum($Third);
} elseif ($c == 4) {
$b = arraySum($Fourth);
} else {
$b = arraySum($Fiifth);
}
$m = ($b - $a);
echo text($m);
?>
</div>
</div>
<?php
$c++;
}
?>
</div>
<a href="view.php?plan=<?php echo $plan_no ?>" class="btn btn-outline-primary" style="border: 3px solid; border-radius: 8px">
<i class="fas fa-arrow-circle-left"></i> Go Back
</a>
</div>
</div>
</div>
</div>
</section>
<!-- Footer -->
<?php
require 'utilities/footer.php';
?>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<script>
// Get the current year for the copyright
$('#year').text(new Date().getFullYear());
</script>
</body>
</html>