-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectTransferUser.php
235 lines (223 loc) · 7.87 KB
/
selectTransferUser.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
<?php
session_start();
require_once 'db.php';
require 'loginControl.php';
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Transaction</title>
<link rel="icon" type="image/x-icon" href="./Public/img/favicon.png">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
</head>
<body>
<?php
require 'navbar.php';
?>
<!-- Transfer geçmişi -->
<div class="container">
<div class="row row justify-content-center">
<div class='row justify-content-center text-center'>
<div class="col-sm-4 col-md-6 col-lg-8">
<h1 class='alert alert-primary mt-2'>Transaction</h1>
</div>
</div>
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Company Id</th>
<th>Company Name</th>
<th>User Name</th>
<th>Company İban</th>
<th>Company Balance</th>
</tr>
</thead>
<tbody>
</div>
<?php
//!Parayı alacak hesap
//!Parayı alacak kişinin idsini get ile aldım
//? Username için inner join kullanmak zorunda kaldım. Çünkü company tablosunda username yok.
$sql = "SELECT companys.companyid,companys.companyname,companys.companyiban,companys.companybalance,users.username
FROM users
INNER JOIN companys
ON companys.userid=users.userid where companys.companyid = :idCompany";
$SORGU = $DB->prepare($sql);
$SORGU->bindParam(':idCompany', $_GET['idCompany']);
$SORGU->execute();
$takeUsers = $SORGU->fetchAll(PDO::FETCH_ASSOC);
//! Giriş yapan kullanıcı ile şirket sahibi aynı değilse yetkilendirme hatası
if ($_GET['idCompany'] != $takeUsers[0]['companyid']) {
//!Yetkilendirme hatası durumunda bir hata sayfasına yönlendir veya bir hata mesajı göster
header("Location: authorizationControl.php");
exit();
}
foreach ($takeUsers as $takeUser) {
echo "
<tr>
<th>{$takeUser['companyid']}</th>
<td>{$takeUser['companyname']}</td>
<td>{$takeUser['username']}</td>
<td>{$takeUser['companyiban']}</td>
<td>{$takeUser['companybalance']}₺</td>
</tr>
";
}
?>
</tbody>
</table>
</div>
</div>
<div class="container mt-3">
<div class="row">
<div class="col-6">
<form method="POST">
<div class="mb-3">
<?php
//!Parayı gönderecek hesap
//!Giriş yapan kullanıcının idsini sesiondan aldım
$sql = "SELECT * FROM companys WHERE userid = :idUser";
$SORGU = $DB->prepare($sql);
$SORGU->bindParam(':idUser', $_SESSION['id']);
$SORGU->execute();
$giveUsers = $SORGU->fetchAll(PDO::FETCH_ASSOC);
/* echo '<pre>';
print_r($giveUsers);
die(); */
//! Giriş yapan kullanıcı ile şirket sahibi aynı değilse yetkilendirme hatası
if ($_GET['idCompany'] == $giveUsers[0]['companyid']) {
//!Yetkilendirme hatası durumunda bir hata sayfasına yönlendir veya bir hata mesajı göster
header("Location: authorizationControl.php");
exit();
}
?>
<select class="form-select" name="form_selectedaccount"required>
<option disabled selected value="">Select Account</option>
<option value="<?php echo $giveUsers[0]['userid']; ?>">
<?php echo $giveUsers[0]['companyname']; ?> (Balance:
<?php echo $giveUsers[0]['companybalance']; ?> )
</option>
</select>
</div>
</div>
<div class="col-6">
<form method="POST">
<div class="input-group mb-3">
<input type="number" name="amount" placeholder="Write Amount" required class="form-control">
<span class="input-group-text">₺</span>
</div>
</div>
</div>
<div class="d-grid col-md-2 mx-auto">
<button type="submit" class="btn btn-success">Send Money</button>
</div>
</form>
</div>
</div>
</div>
<?php
//?Para alma ve gönderme işlemleri
//!sql1=Alıcı hesap
//!sql2=Gönderici hesap
if (isset($_POST['amount'])) {
$amount = $_POST['amount'];
/* SELECT companys.*,users.*
FROM users
INNER JOIN companys
ON companys.userid=users.userid WHERE users.userid = :idUser */
//!Alıcı hesap
//!sql1=Alıcı hesap
$sql = "SELECT * FROM companys WHERE companyid = :idCompany";
$SORGU = $DB->prepare($sql);
$SORGU->bindParam(':idCompany', $_GET['idCompany']);
$SORGU->execute();
$sql1 = $SORGU->fetchAll(PDO::FETCH_ASSOC);
/* echo '<pre>'; print_r($sql1);
die(); */
/*
SELECT companys.*,users.*
FROM users
INNER JOIN companys
ON companys.userid=users.userid WHERE users.userid = :form_selectedaccount */
//!Gönderici hesap
//!sql2=Gönderici hesap
$sql = "SELECT * FROM companys WHERE userid = :form_selectedaccount";
$SORGU = $DB->prepare($sql);
$SORGU->bindParam(':form_selectedaccount', $_POST['form_selectedaccount']);
$SORGU->execute();
$sql2 = $SORGU->fetchAll(PDO::FETCH_ASSOC);
/* echo '<pre>'; print_r($sql2);
die(); */
//!Girilen para değeri negatifse
if (($amount) < 0) {
echo '<script type="text/javascript">';
echo ' alert("Oops! Negative values cannot be transferred")'; // showing an alert box.
echo '</script>';
}
//!Para kontrolü sonradan düşünülecek
/* // constraint to check insufficient balance.
else if($amount > $sql2[0]['balance'])
{
echo '<script type="text/javascript">';
echo ' alert("Sorry, Insufficient Balance")'; // showing an alert box.
echo '</script>';
} */
//!Girilen para değeri 0 ise
else if ($amount == 0) {
echo "<script type='text/javascript'>";
echo "alert('Oops! Zero value cannot be transferred')";
echo "</script>";
} else {
//!Gönderici hesaptan para azaltma ve günceleme işlemi
//!Gönderici hesaptan parayı azalt ve veritabanını güncelle
$newbalance = $sql2[0]['companybalance'] - $amount;
$sql = "UPDATE companys set companybalance=$newbalance where userid=:form_selectedaccount";
$SORGU = $DB->prepare($sql);
$SORGU->bindParam(':form_selectedaccount', $_POST['form_selectedaccount']);
$SORGU->execute();
//!Alıcı hesaptan para arttırma ve günceleme işlemi
//!Gönderici hesaptan parayı al ve veritabanını güncelle
$newbalance = $sql1[0]['companybalance'] + $amount;
$sql = "UPDATE companys set companybalance=$newbalance where companyid=:idCompany";
$SORGU = $DB->prepare($sql);
$SORGU->bindParam(':idCompany', $_GET['idCompany']);
$SORGU->execute();
//!İşlem geçmişi için
//?sql1=Alıcı hesap
//?sql2=Gönderici hesap
//!Alıcı hesap bilgileri
$sender = $sql2[0]['companyname'];
$senderid = $sql2[0]['userid'];
//!Gönderici hesap bilgileri
$receiver = $sql1[0]['companyname'];
$receiverid = $sql1[0]['userid'];
//!İşlem geçmişi için veritabanına ekleme işlemi
//?İşlem geçmişinde tutar bilgisi düzenle
$sql = "INSERT INTO transactions (sender,senderid,reciver,reciverid,companybalance) VALUES ('$sender','$senderid','$receiver','$receiverid','$amount')";
$SORGU = $DB->prepare($sql);
$SORGU->execute();
$sql3 = "SELECT * FROM transactions";
$SORGU = $DB->prepare($sql3);
$SORGU->execute();
$CEVAP = $SORGU->fetchAll(PDO::FETCH_ASSOC);
//!Eğer işlem başarılıysa
if (count($CEVAP) > 0) {
echo "<script> alert('Transaction Completed');
window.location='selectTransferUser.php?idCompany={$sql1[0]['companyid']}';
</script>";
} else {
echo '<script type="text/javascript">';
echo ' alert("Transaction Not Completed")';
echo '</script>';
}
$newbalance = 0;
$amount = 0;
}
}
?>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</body>
</html>