-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.php
More file actions
307 lines (253 loc) · 9.96 KB
/
function.php
File metadata and controls
307 lines (253 loc) · 9.96 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
304
305
306
307
<?php
session_start();
//Membuat koneksi ke database
$conn = mysqli_connect("localhost", "root", "", "InventoryBarang");
//Menambah barang ke database
if (isset($_POST['addnewbarang'])) {
$namabarang = $_POST['namabarang'];
$deskripsi = $_POST['deskripsi'];
$stock = $_POST['stock'];
$addtotable = mysqli_query($conn, "insert into stock (namabarang, deskripsi, stock, image) values ('$namabarang','$deskripsi','$stock','$image')");
if ($addtotable) {
header('location:stock.php');
} else {
echo 'Gagal';
header('location:stock.php');
}
}
//Menambah Barang Masuk
if (isset($_POST['barangmasuk'])) {
$barangnya = $_POST['barangnya'];
$keterangan = $_POST['keterangan'];
$qty = $_POST['qty'];
$cekstocksekarang = mysqli_query($conn, "select * from stock where idbarang='$barangnya'");
$ambildata = mysqli_fetch_array($cekstocksekarang);
$stocksekarang = $ambildata['stock'];
$totalstock = $stocksekarang + $qty;
$addtomasuk = mysqli_query($conn, "insert into barang_masuk (idbarang, keterangan, qty) values ('$barangnya','$keterangan','$qty')");
$updatestockmasuk = mysqli_query($conn, "update stock set stock='$totalstock' where idbarang='$barangnya'");
if ($addtomasuk && $updatestockmasuk) {
header('location:masuk.php');
} else {
echo 'Gagal';
header('location:masuk.php');
}
}
//Menambah Barang Keluar
if (isset($_POST['barangkeluar'])) {
$barangnya = $_POST['barangnya'];
$penerima = $_POST['penerima'];
$qty = $_POST['qty'];
$cekstocksekarang = mysqli_query($conn, "select * from stock where idbarang='$barangnya'");
$ambildata = mysqli_fetch_array($cekstocksekarang);
$stocksekarang = $ambildata['stock'];
if ($stocksekarang >= $qty) {
//jika barangnya cukup
$totalstock = $stocksekarang - $qty;
$addtomasuk = mysqli_query($conn, "insert into barang_keluar (idbarang, penerima, qty) values ('$barangnya','$penerima','$qty')");
$updatestockmasuk = mysqli_query($conn, "update stock set stock='$totalstock' where idbarang='$barangnya'");
if ($addtomasuk && $updatestockmasuk) {
header('location:keluar.php');
} else {
echo 'Gagal';
header('location:keluar.php');
}
} else {
//jika barangnya tidak cukup
echo '
<script>
alert("Stock saat ini tidak mencukupi!");
window.location.href="keluar.php";
</script>
';
}
}
//Update Info Barang
if (isset($_POST['updatebarang'])) {
$idb = $_POST['idb'];
$namabarang = $_POST['namabarang'];
$deskripsi = $_POST['deskripsi'];
//Soal Gambar
$allowed_extension = array('png', 'jpg', 'jpeg');
$nama = $_FILES['file']['name']; // Ngambil Nama File
$dot = explode('.', $nama);
$ekstensi = strtolower(end($dot)); //ngambil ekstensi
$ukuran = $_FILES['file']['size']; //ngambil size
$file_tmp = $_FILES['file']['tmp_name']; //lokasi file
//penamaan file
$image = md5(uniqid($nama, true) . time()) . '.' . $ekstensi; //menggabungkan nama file yang di enkripsi dengan ekstensi
if ($ukuran == 0) {
//jika tidak ingin upload
$update = mysqli_query($conn, "update stock set namabarang='$namabarang', deskripsi='$deskripsi' where idbarang='$idb'");
if ($update) {
header('location:stock.php');
} else {
echo 'Gagal';
header('location:stock.php');
}
} else {
//jika ingin upload
move_uploaded_file($file_tmp, 'images/' . $image);
$update = mysqli_query($conn, "update stock set namabarang='$namabarang', deskripsi='$deskripsi', image='$image' where idbarang='$idb'");
if ($update) {
header('location:stock.php');
} else {
echo 'Gagal';
header('location:stock.php');
}
}
}
//Menghapus Barang
if (isset($_POST['hapusbarang'])) {
$idb = $_POST['idb'];
$gambar = mysqli_query($conn, "select * from stock where idbarang='$idb'");
$get = mysqli_fetch_array($gambar);
$img = 'images/' . $get['image'];
unlink($img);
$hapus = mysqli_query($conn, "delete from stock where idbarang='$idb'");
if ($hapus) {
header('location:stock.php');
} else {
echo 'Gagal';
header('location:stock.php');
}
};
//Mengubah data barang masuk
if (isset($_POST['updatebarangmasuk'])) {
$idb = $_POST['idb'];
$idm = $_POST['idm'];
$deskripsi = $_POST['keterangan'];
$qty = $_POST['qty'];
$lihatstock = mysqli_query($conn, "select * from stock where idbarang='$idb'");
$stocknya = mysqli_fetch_array($lihatstock);
$stockskrg = $stocknya['stock'];
$qtyskrg = mysqli_query($conn, "select * from barang_masuk where idmasuk='$idm'");
$qtynya = mysqli_fetch_array($qtyskrg);
$qtysrkg = $qtynya['qty'];
if ($qty > $qtyskrg) {
$selisih = $qty - $qtyskrg;
$kurangin = $stockskrg + $selisih;
$kurangistocknya = mysqli_query($conn, "update stock set stock='$kurangin' where idbarang='$idb'");
$updatenya = mysqli_query($conn, "update barang_masuk set qty='$qty', keterangan='$deskripsi' where idmasuk='$idm'");
if ($kurangistocknya && $updatenya) {
header('location:masuk.php');
} else {
echo 'Gagal';
header('location:masuk.php');
}
} else {
$selisih = $qtyskrg - $qty;
$kurangin = $stockskrg - $selisih;
$kurangistocknya = mysqli_query($conn, "update stock set stock='$kurangin' where idbarang='$idb'");
$updatenya = mysqli_query($conn, "update barang_masuk set qty='$qty', keterangan='$deskripsi' where idmasuk='$idm'");
if ($kurangistocknya && $updatenya) {
header('location:masuk.php');
} else {
echo 'Gagal';
header('location:masuk.php');
}
}
}
//Menghapus barang masuk
if (isset($_POST['hapusbarangmasuk'])) {
$idb = $_POST['idb'];
$qty = $_POST['kty'];
$idm = $_POST['idm'];
$getdatastock = mysqli_query($conn, "SELECT * FROM stock WHERE idbarang='$idb'");
$data = mysqli_fetch_array($getdatastock);
$stok = $data['stock'];
$selisih = $stok - $qty;
$update = mysqli_query($conn, "UPDATE stock SET stock='$selisih' WHERE idbarang='$idb'");
$hapusdata = mysqli_query($conn, "DELETE FROM barang_masuk WHERE idmasuk='$idm'");
if ($update && $hapusdata) {
header('location:masuk.php');
} else {
header('location:masuk.php');
}
}
//Mengubah data barang keluar
if (isset($_POST['updatebarangkeluar'])) {
$idb = $_POST['idb'];
$idk = $_POST['idk'];
$penerima = $_POST['penerima'];
$qty = $_POST['qty'];
$lihatstock = mysqli_query($conn, "select * from stock where idbarang='$idb'");
$stocknya = mysqli_fetch_array($lihatstock);
$stockskrg = $stocknya['stock'];
$qtyskrg = mysqli_query($conn, "select * from barang_keluar where idkeluar='$idk'");
$qtynya = mysqli_fetch_array($qtyskrg);
$qtysrkg = $qtynya['qty'];
if ($qty > $qtyskrg) {
$selisih = $qty - $qtyskrg;
$kurangin = $stockskrg + $selisih;
$kurangistocknya = mysqli_query($conn, "update stock set stock='$kurangin' where idbarang='$idb'");
$updatenya = mysqli_query($conn, "update barang_keluar set qty='$qty', penerima='$penerima' where idkeluar='$idk'");
if ($kurangistocknya && $updatenya) {
header('location:masuk.php');
} else {
echo 'Gagal';
header('location:keluar.php');
}
} else {
$selisih = $qtyskrg - $qty;
$kurangin = $stockskrg - $selisih;
$kurangistocknya = mysqli_query($conn, "update stock set stock='$kurangin' where idbarang='$idb'");
$updatenya = mysqli_query($conn, "update barang_keluar set qty='$qty', penerima='$penerima' where idkeluar='$idk'");
if ($kurangistocknya && $updatenya) {
header('location:keluar.php');
} else {
echo 'Gagal';
header('location:keluar.php');
}
}
}
//Menghapus barang keluar
if (isset($_POST['hapusbarangkeluar'])) {
$idb = $_POST['idb'];
$qty = $_POST['kty'];
$idk = $_POST['idk'];
$getdatastock = mysqli_query($conn, "SELECT * FROM stock WHERE idbarang='$idb'");
$data = mysqli_fetch_array($getdatastock);
$stok = $data['stock'];
$selisih = $stok + $qty;
$update = mysqli_query($conn, "UPDATE stock SET stock='$selisih' WHERE idbarang='$idb'");
$hapusdata = mysqli_query($conn, "DELETE FROM barang_keluar WHERE idkeluar='$idk'");
if ($update && $hapusdata) {
header('location:keluar.php');
} else {
header('location:keluar.php');
}
}
//Menambah admin
if (isset($_POST['addadmin'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$queryinsert = mysqli_query($conn, "insert into login (email, password) values ('$email','$password')");
if ($queryinsert) {
header('location:admin.php');
} else {
header('location:admin.php');
}
}
//Edit admin
if (isset($_POST['updateadmin'])) {
$emailbaru = $_POST['emailadmin'];
$passwordbaru = $_POST['passwordbaru'];
$idnya = $_POST['id'];
$queryupdate = mysqli_query($conn, "update login set email='$emailbaru', password='$passwordbaru' where iduser='$idnya'");
if ($queryupdate) {
header('location:admin.php');
} else {
header('location:admin.php');
}
}
//Hapus admin
if (isset($_POST['hapusadmin'])) {
$id = $_POST['id'];
$querydelete = mysqli_query($conn, "delete from login where iduser='$id'");
if ($querydelete) {
header('location:admin.php');
} else {
header('location:admin.php');
}
}