-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBConnector.php
446 lines (439 loc) · 14.7 KB
/
DBConnector.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
$host_db = 'localhost';
// MySQL username
$username_db = 'root';
// MySQL password
$password_db = '';
// Database name
$name_db = 'si';
$db = mysqli_connect($host_db, $username_db, $password_db, $name_db);
if (!$db)
die("Connection failed: " . mysqli_connect_error());
////////////////////////////////////////////// CRUD Penjualan //////////////////////////
function getAllPenjualan() {
global $db;
$qry = "SELECT * FROM penjualan ORDER BY tanggal DESC";
$res = mysqli_query($db, $qry);
$jual = $res->fetch_assoc();
$penjualan = array();
while(!is_null($jual)) {
$produk = getProduk($jual["id_produk"]);
$jual["nama_produk"] = $produk["nama"];
array_push($penjualan, $jual);
$jual = $res->fetch_assoc();
}
return $penjualan;
}
function addPenjualan($username, $id_produk, $tanggal, $harga){
global $db;
$qry = "SELECT * FROM produk WHERE id='$id_produk'";
$res = mysqli_query($db, $qry);
if(is_null($res->fetch_row()))
return "Produk tidak tersedia";
$qry = "INSERT INTO penjualan(username, id_produk, tanggal, harga_terjual) VALUES ('$username', '$id_produk', '$tanggal', '$harga')";
$res = mysqli_query($db, $qry);
if(!$res)
return "Penambahan penjualan baru gagal";
$komposisi = getKomposisi($id_produk);
$bahan = getAllBahan();
foreach ($komposisi as $key => $value) {
$jumlah = $bahan[$key]['tersedia'] - $value;
$qry = "UPDATE bahan_baku SET tersedia=$jumlah WHERE id=$key";
$res = mysqli_query($db, $qry);
}
return null;
}
function deletePenjualan($id, $username) {
global $db;
$qry = "SELECT * FROM penjualan WHERE id='$id'";
$res = mysqli_query($db, $qry);
$penjualan = $res->fetch_assoc();
if(is_null($penjualan))
return "Penjualan yang akan dihapus tidak tersedia";
if($penjualan["username"] != $username)
return "Pengguna ".$username." tidak berhak menghapus penjualan dengan ID ".$id;
$qry = "DELETE FROM penjualan WHERE id='$id'";
$res = mysqli_query($db, $qry);
if(!$res)
return "Penghapusan penjualan gagal";
$komposisi = getKomposisi($penjualan['id_produk']);
$bahan = getAllBahan();
foreach ($komposisi as $key => $value) {
$jumlah = $bahan[$key]['tersedia'] + $value;
$qry = "UPDATE bahan_baku SET tersedia=$jumlah WHERE id=$key";
$res = mysqli_query($db, $qry);
}
return null;
}
function editPenjualan($id, $username, $id_produk, $tanggal, $harga){
global $db;
$qry = "SELECT * FROM produk WHERE id='$id_produk'";
$res = mysqli_query($db, $qry);
if(is_null($res->fetch_row()))
return "Produk tidak tersedia";
$qry = "SELECT * FROM penjualan WHERE id='$id'";
$res = mysqli_query($db, $qry);
$penjualan = $res->fetch_assoc();
if(is_null($penjualan))
return "Penjualan yang akan diedit tidak tersedia";
if($penjualan["username"] != $username)
return "Pengguna ".$username." tidak berhak mengubah penjualan dengan ID ".$id;
$qry = "UPDATE penjualan SET username='$username', id_produk='$id_produk', tanggal='$tanggal', harga_terjual='$harga' WHERE id='$id'";
$res = mysqli_query($db, $qry);
if(!$res)
return "Pengeditan penjualan gagal";
$komposisi = getKomposisi($penjualan['id_produk']);
$bahan = getAllBahan();
foreach ($komposisi as $key => $value) {
$jumlah = $bahan[$key]['tersedia'] + $value;
$qry = "UPDATE bahan_baku SET tersedia=$jumlah WHERE id=$key";
$bahan[$key]['tersedia'] = $jumlah;
$res = mysqli_query($db, $qry);
}
$komposisi = getKomposisi($id_produk);
$bahan = getAllBahan();
foreach ($komposisi as $key => $value) {
$jumlah = $bahan[$key]['tersedia'] - $value;
$qry = "UPDATE bahan_baku SET tersedia=$jumlah WHERE id=$key";
$res = mysqli_query($db, $qry);
}
return null;
}
/////////////////////////////////////// Pegawai //////////////////////////////////////////
function getPegawai($username) {
global $db;
$qry = "SELECT * FROM pegawai_operasional WHERE username='$username'";
$res = mysqli_query($db, $qry);
$pegawai = $res->fetch_assoc();
if(!is_null($pegawai)) {
$pegawai["jenis"] = "operasional";
return $pegawai;
}
$qry = "SELECT * FROM pegawai_pengadaan WHERE username='$username'";
$res = mysqli_query($db, $qry);
$pegawai = $res->fetch_assoc();
if(!is_null($pegawai)) {
$pegawai["jenis"] = "pengadaan";
return $pegawai;
}
return null;
}
////////////////////////////////////// CRUD Bahan //////////////////////////////////////////
function getAllBahan() {
global $db;
$qry = "SELECT * FROM bahan_baku ORDER BY id ASC";
$res = mysqli_query($db, $qry);
$ret = array();
$bahan = $res->fetch_assoc();
while($bahan) {
$ret[$bahan['id']] = $bahan;
$bahan = $res->fetch_assoc();
}
return $ret;
}
function addBahan($nama, $tersedia, $batas_minimum){
global $db;
$qry = "INSERT INTO bahan_baku(nama, tersedia, batas_minimum) VALUES ('$nama', $tersedia, $batas_minimum)";
$res = mysqli_query($db, $qry);
if($res)
return null;
else
return "Penambahan bahan baku baru gagal";
}
function deleteBahan($id) {
global $db;
$qry = "SELECT * FROM bahan_baku WHERE id='$id'";
$res = mysqli_query($db, $qry);
$bahan = $res->fetch_assoc();
if(is_null($bahan))
return "Bahan baku yang akan dihapus tidak tersedia";
$qry = "DELETE FROM bahan_baku WHERE id='$id'";
$res = mysqli_query($db, $qry);
if($res)
return null;
else
return "Penghapusan bahan baku gagal";
}
function editBahan($id, $nama, $tersedia, $batas_minimum){
global $db;
$qry = "SELECT * FROM bahan_baku WHERE id='$id'";
$res = mysqli_query($db, $qry);
$bahan = $res->fetch_assoc();
if(is_null($bahan))
return "Bahan baku yang akan diedit tidak tersedia";
$qry = "UPDATE bahan_baku SET nama='$nama', tersedia=$tersedia, batas_minimum=$batas_minimum WHERE id='$id'";
$res = mysqli_query($db, $qry);
if($res)
return null;
else
return "Pengeditan bahan baku gagal";
}
/////////////////////////////// PESANAN BAHAN ////////////////////////////
function addPesananBahan($username, $tanggal, $porsi) {
global $db;
$status = 0;
$pegawai = getPegawai($username);
if(is_null($pegawai))
return "Pegawai tidak ditemukan";
$qry = "INSERT INTO pesanan_bahan(username, tanggal, status) VALUES ('$username', '$tanggal', $status)";
if(mysqli_query($db, $qry))
$id = mysqli_insert_id($db);
else
return "Gagal";
$fail = false;
foreach ($porsi as $item) {
$itemid = $item['id_bahan'];
$itemjumlah = $item['jumlah'];
$qry = "INSERT INTO porsi_pesan(id_pesan, id_bahan, jumlah) VALUES ($id, $itemid, $itemjumlah)";
$res = mysqli_query($db, $qry);
if(!$res)
$fail = true;
}
if($fail)
return "Terdapat kegagalan menambahkan porsi pesanan";
return null;
}
function getPesananBahan() {
global $db;
$qry = "SELECT * FROM pesanan_bahan WHERE status = 0";
$res = mysqli_query($db, $qry);
$pesanan = $res->fetch_assoc();
return $pesanan;
}
function verifyPesanan($id) {
global $db;
$qry = "UPDATE pesanan_bahan SET status=1 WHERE id=$id";
$res = mysqli_query($db, $qry);
$porsi = getPorsiPesan($id);
$bahan = getAllBahan();
$jumlah = array();
foreach ($bahan as $item) {
$jumlah[$item['id']] = $item['tersedia'];
}
$fail = false;
foreach($porsi as $item) {
if($item["dipesan"] > 0) {
$hasil = $jumlah[$item['id']] + $item['dipesan'];
$id_bahan = $item['id'];
$qry = "UPDATE bahan_baku SET tersedia=$hasil WHERE id=$id_bahan";
$res = mysqli_query($db, $qry);
if(!$res)
$fail = true;
}
}
if($fail)
return "Terdapat kesalahan";
return null;
}
function getPorsiPesan($id) {
global $db;
$qry = "SELECT * FROM porsi_pesan WHERE id_pesan = $id";
$res = mysqli_query($db, $qry);
$porsi = $res->fetch_assoc();
$jumlah = array();
while($porsi) {
$jumlah[$porsi['id_bahan']] = $porsi['jumlah'];
$porsi = $res->fetch_assoc();
}
$ret = array();
$bahan = getAllBahan();
foreach($bahan as $item) {
if(array_key_exists($item['id'],$jumlah))
$item['dipesan'] = $jumlah[$item['id']];
else
$item['dipesan'] = 0;
array_push($ret, $item);
}
return $ret;
}
function calculatePorsiPesan() {
$bahan = getAllBahan();
$ret = array();
foreach ($bahan as $key => $item) {
if($item['tersedia'] >= $item['batas_minimum'])
$item['dipesan'] = 0;
else
$item['dipesan'] = $item['batas_minimum'] - $item['tersedia'];
$ret[$key] = $item;
}
return $ret;
}
function editPorsiPesan($id, $porsi) {
global $db;
foreach($porsi as $item) {
$jumlah = $item['jumlah'];
$id_bahan = $item['id_bahan'];
$qry = "SELECT * FROM porsi_pesan WHERE id_bahan=$id_bahan AND id_pesan=$id";
$res = mysqli_query($db, $qry);
if(is_null($res->fetch_row())) {
$qry = "INSERT INTO porsi_pesan(id_bahan, id_pesan, jumlah) VALUES ($id_bahan, $id, $jumlah)";
$res = mysqli_query($db, $qry);
}
else {
$qry = "UPDATE porsi_pesan SET jumlah=$jumlah WHERE id_bahan=$id_bahan AND id_pesan=$id";
$res = mysqli_query($db, $qry);
}
}
}
/////////////////////////////////////// Produk /////////////////////////////////////////////
function getProduk($id) {
global $db;
$qry = "SELECT * FROM produk WHERE id=$id";
$res = mysqli_query($db, $qry);
return $res->fetch_assoc();
}
function getAllProduk() {
global $db;
$qry = "SELECT * FROM produk";
$res = mysqli_query($db, $qry);
$ret = array();
$now = $res->fetch_assoc();
while(!is_null($now)) {
$ret[$now['id']] = $now;
$now = $res->fetch_assoc();
}
return $ret;
}
function addProduk($nama, $jenis, $harga, $komposisi) {
global $db;
$qry = "INSERT INTO produk(nama, jenis, harga) VALUES ('$nama', '$jenis', $harga)";
$res = mysqli_query($db, $qry);
if(!$res)
return "Penambahan produk gagal";
$id = mysqli_insert_id($db);
$fail = false;
foreach ($komposisi as $key => $value) {
if($value > 0) {
$qry = "INSERT INTO komposisi(id_produk, id_bahan, jumlah) VALUES ($id, $key, $value)";
$res = mysqli_query($db, $qry);
if(!$res)
$fail = true;
}
}
if($fail)
return "Pengisian komposisi bahan gagal";
return null;
}
function editProduk($id, $nama, $jenis, $harga, $komposisi) {
global $db;
$qry = "SELECT * FROM produk WHERE id=$id";
$res = mysqli_query($db, $qry);
if(is_null($res->fetch_row()))
return "Produk tidak ditemukan";
$qry = "UPDATE produk SET nama='$nama', jenis='$jenis', harga=$harga WHERE id=$id";
$res = mysqli_query($db, $qry);
if(!$res)
return "Pengeditan produk gagal";
$before = getKomposisi($id);
$fail = false;
foreach ($komposisi as $key => $value) {
if($value > 0) {
if(isset($before[$key]))
$qry = "UPDATE komposisi SET jumlah=$value WHERE id_produk=$id AND id_bahan=$key";
else
$qry = "INSERT INTO komposisi(id_produk, id_bahan, jumlah) VALUES ($id, $key, $value)";
$res = mysqli_query($db, $qry);
if(!$res)
$fail = true;
}
elseif (isset($before[$key])) {
$qry = "DELETE FROM komposisi WHERE id_produk=$id AND id_bahan=$key";
$res = mysqli_query($db, $qry);
if(!$res)
$fail = true;
}
}
if($fail)
return "Pengisian komposisi bahan gagal";
return null;
}
function deleteProduk($id) {
global $db;
$qry = "DELETE FROM komposisi WHERE id_produk=$id";
$res = mysqli_query($db, $qry);
if(!$res)
return "Penghapusan komposisi gagal";
$qry = "DELETE FROM produk WHERE id=$id";
$res = mysqli_query($db, $qry);
if(!$res)
return "Penghapusan komposisi gagal";
return null;
}
function getKomposisi($id_produk) {
global $db;
$qry = "SELECT * FROM komposisi WHERE id_produk=$id_produk";
$res = mysqli_query($db, $qry);
$now = $res->fetch_assoc();
$ret = array();
while(!is_null($now)) {
$ret[$now['id_bahan']] = $now['jumlah'];
$now = $res->fetch_assoc();
}
return $ret;
}
function addKomposisi($id_produk, $id_bahan, $jumlah) {
global $db;
$qry = "SELECT * FROM produk WHERE id=$id_produk";
$res = mysqli_query($db, $qry);
if(is_null($res->fetch_assoc()))
return "Produk tidak tersedia";
$qry = "INSERT INTO komposisi(id_produk, id_bahan, jumlah) VALUES ($id_produk, $id_bahan, $jumlah)";
$res = mysqli_query($db, $qry);
if(!$res)
return "Penambahan komposisi gagal";
return null;
}
function editKomposisi($id_produk, $id_bahan, $jumlah) {
global $db;
$qry = "UPDATE komposisi SET jumlah=$jumlah WHERE id_produk=$id_produk AND id_bahan=$id_bahan";
$res = mysqli_query($db, $qry);
if(!$res)
return "Pengeditan komposisi gagal";
return null;
}
function deleteKomposisi($id_produk, $id_bahan) {
global $db;
$qry = "DELETE FROM komposisi WHERE id_produk=$id_produk AND id_bahan=$id_bahan";
$res = mysqli_query($db, $qry);
if(!$res)
return "Penghapusan komposisi gagal";
return null;
}
////////////////////////////////// Statistik //////////////////////////////////
function getJenisProduk() {
global $db;
$qry = "SELECT DISTINCT jenis FROM produk";
$res = mysqli_query($db, $qry);
$ret = array();
$jenis = $res->fetch_assoc();
while(!is_null($jenis)) {
array_push($ret, $jenis['jenis']);
$jenis = $res->fetch_assoc();
}
return $ret;
}
function getStat($kategori, $awal, $akhir) {
global $db;
if($kategori == "semua")
$qry = "SELECT tanggal, count(*) as banyak FROM penjualan GROUP BY tanggal HAVING tanggal >= '$awal' AND tanggal <= '$akhir'";
else
$qry = "SELECT tanggal, count(*) as banyak FROM penjualan INNER JOIN (SELECT * FROM produk WHERE jenis='$kategori') AS prod ON penjualan.id_produk=prod.id GROUP BY tanggal HAVING tanggal >= '$awal' AND tanggal<='$akhir'";
$res = mysqli_query($db, $qry);
$stat = array();
$now = $res->fetch_assoc();
while(!is_null($now)) {
array_push($stat, $now);
$now = $res->fetch_assoc();
}
$ret = array();
foreach ($stat as $item) {
$date = strtotime($item['tanggal']);
$ret[Date("F Y", $date)] = 0;
}
foreach ($stat as $item) {
$date = strtotime($item['tanggal']);
$ret[Date("F Y", $date)] += $item['banyak'];
}
return $ret;
}
?>