-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
120 lines (100 loc) · 2.88 KB
/
functions.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
<?php
function db_connection(){
define("DB_HOST", "db_host");
define("DB_USER", "db_user");
define("DB_PASSWORD", "db_pass");
define("DB_DATABASE", "db_name");
$conn = new mysqli( DB_HOST , DB_USER, DB_PASSWORD, DB_DATABASE );
if( $conn->connect_error ){
die("Could not connect : " . $conn->connect_error );
}
return $conn;
}
function checkBalance($userId, $con){
$result = $con->query("Select Balance from user where ID={$userId}");
if($result->num_rows > 0){
$balance = $result->fetch_assoc()['Balance'];
return "Balance : ". $balance;
}
else{
return "ID is not found in database";
}
}
function payment($passenger_id, $driver_id, $fair, $con){
$result1 = $con->query("Select * from user where ID={$passenger_id}");
$result2 = $con->query("Select * from user where ID={$driver_id}");
if($result1 && $result2){
$psgr = $result1->fetch_assoc();
$drvr = $result2->fetch_assoc();
if( $psgr['Balance'] > 0 ){
$transaction = ($psgr['Balance'] >= $fair)? $fair: ($fair - $psgr['Balance']);
$result1 = $con->query("Update user Set Balance = Balance - {$transaction}, LastTransaction = {$transaction} WHERE Id={$passenger_id}");
$result2 = $con->query("Update user Set Balance = Balance + {$transaction}, LastTransaction = {$transaction} WHERE Id={$driver_id}");
if($result1 && $result2){
return "Amount ". $transaction . " is transferred to Driver. Your New Balance is ". ($psgr['Balance']-$transaction);
}
else{
return "Something Wrong in transaction";
}
}
else
{
return "You have no balance in your account";
}
}
elseif(!$result1){
return "Passenger ID is not found in database";
}
elseif(!$result1){
return "Driver ID is not found in database";
}
else{
return "Something Wrong with given ID";
}
}
function getFair($distance, $jam_time){
/*
* 1st 2km = 40 Tk
* then 12 Tk per km
* 3 Tk per min of jam
* Here every measurement in km.
*/
$fixed_distance = 2; /* Actually 2km */
$extra_distance = (float)($distance - $fixed_distance);
/* Fair */
$fixed_fair = 40.0;
$per_km_fair = 12.0;
$jam_fair = 0.0; /* actually (time * fair) */
if( $distance <= $fixed_distance ){
return (float)($fixed_fair + $jam_fair);
}
else{
return (float)($fixed_fair + $extra_distance * $per_km_fair + $jam_fair);
}
}
function displayAllUserInfo($con){
$result = $con->query("SELECT * FROM user");
if( $result->num_rows > 0 ){
echo "<h3>All User Info Table</h3>";
echo "<style> th, td{padding: 15px; text-align:center;} </style>";
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Amount</th>
<th>Last Transaction</th>
</tr>
";
while( $row = $result->fetch_assoc() ){
echo "<tr>
<td>{$row['Id']}</td>
<td>{$row['Name']}</td>
<td>{$row['Balance']}</td>
<td>{$row['LastTransaction']}</td>
</tr>
";
}
echo "</table>";
}
}
?>