-
Notifications
You must be signed in to change notification settings - Fork 1
/
prototype.php
399 lines (341 loc) · 15.6 KB
/
prototype.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
<?php
include "connection.php";
include "admin_privilege.php";
$queryBahan = "SELECT * FROM masterbahan ORDER BY nama";
$resultBahan = $conn->query($queryBahan);
$stockQuantity = ""; // Default value, replace it with the actual stock quantity based on the selected item from the database
$newStockQuantity = "";
if (isset($_POST['quantity'])) {
$selectedItemId = $_POST['selectedItem'];
// Fetch the username from the POST data
$pengguna = isset($_SESSION['namaLengkap']) ? $_SESSION['namaLengkap'] : '';
// Fetch the stock quantity from the database based on the selected item
$query = "SELECT quantity FROM masterbahan WHERE stok_id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $selectedItemId);
$stmt->execute();
$stmt->bind_result($stockQuantity);
$stmt->fetch();
$stmt->close();
$submittedQuantity = $_POST['quantity'];
if ($submittedQuantity == "") {
echo json_encode(array('currentStock' => $stockQuantity, 'newStock' => $newStockQuantity));
exit();
} elseif ($submittedQuantity <= 0) {
echo "Kuantitas yang dimasukkan harus lebih besar dari 0";
exit();
}
// Update the database with the new stock quantity
$newStockQuantity = $stockQuantity - $submittedQuantity;
if ($newStockQuantity < 0) {
echo "Stok bahan tidak mencukupi untuk keperluan prototype.";
exit();
}
$updateQueryStock = "UPDATE masterbahan SET quantity = ? WHERE stok_id = ?";
$updateStmt = $conn->prepare($updateQueryStock);
$updateStmt->bind_param("ii", $newStockQuantity, $selectedItemId);
$updateStmt->execute();
$updateStmt->close();
$queryNamaBahan = "SELECT kelompok, nama FROM masterbahan WHERE stok_id = ?";
$stmtNamaBahan = $conn->prepare($queryNamaBahan);
$stmtNamaBahan->bind_param("i", $selectedItemId);
$stmtNamaBahan->execute();
$stmtNamaBahan->bind_result($kelompok, $namaBahan);
$stmtNamaBahan->fetch();
$stmtNamaBahan->close();
$kelompokNama = $kelompok . ' - ' . $namaBahan;
// Insert a new record into the 'historis' table
$insertQueryHistoris = "INSERT INTO historis (pengguna, nama_barang, waktu, quantity, activity, deskripsi) VALUES (?, ?, NOW(), ?, 'Prototype', ?)";
$insertStmt = $conn->prepare($insertQueryHistoris);
$insertStmt->bind_param("ssis", $pengguna, $kelompokNama, $submittedQuantity, $_POST['deskripsi']);
$insertStmt->execute();
$insertStmt->close();
// Return the updated stock quantity
echo json_encode(array('currentStock' => $stockQuantity, 'newStock' => $newStockQuantity));
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Prototype</title>
<link rel="icon" href="assets/adminlte/dist/img/OWLlogo.png" type="image/x-icon">
<!-- Google Font: Source Sans Pro -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<!-- Theme style -->
<link rel="stylesheet" href="assets/adminlte/dist/css/adminlte.min.css">
<!-- Sweetalert2 -->
<link rel="stylesheet" href="assets/adminlte/plugins/sweetalert2-theme-bootstrap-4/bootstrap-4.min.css">
<!-- Ionicons -->
<link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<!-- Select2 -->
<link rel="stylesheet" href="assets/adminlte/plugins/select2/css/select2.min.css">
<link rel="stylesheet" href="assets/adminlte/plugins/select2-bootstrap4-theme/select2-bootstrap4.min.css">
<style>
#successMessage {
display: none;
/* Hide the success message initially */
}
.gray-italic-text {
color: #808080;
font-style: italic;
}
</style>
</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">
<?php
$rootPath = $_SERVER['DOCUMENT_ROOT'];
include $rootPath . "/owl_inventory/includes/navbar.php";
include $rootPath . "/owl_inventory/includes/sidebar.php";
?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1>Prototype</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="homepage.php">Home</a></li>
<li class="breadcrumb-item active">Prototype</li>
</ol>
</div>
</div>
</div><!-- /.container-fluid -->
</section>
<!-- Main content -->
<section class="content">
<div class="container-fluid">
<!-- general form elements -->
<div class="card card-primary">
<div class="card-header">
<h3 class="card-title">Mengurangi Bahan Untuk Keperluan Prototype</h3>
</div>
<!-- /.card-header -->
<!-- form start -->
<form id="prototypeForm">
<div class="card-body">
<div class="form-group">
<label for="pilihBahanPrototype">Pilih Bahan <span style="color: red;">*</span></label>
<select class="form-control select2" id="pilihBahanPrototype" name="selectedItem">
<option value="">--- Pilih Bahan ---</option>
<?php
while ($row = $resultBahan->fetch_assoc()) {
echo '<option value="' . $row['stok_id'] . '">' . $row['kelompok'] . ' - ' . $row['nama'] . '</option>';
}
?>
</select>
</div>
<div class="form-group">
<label for="quantity">Kuantitas <span style="color: red;">*</span></label>
<div class="input-group">
<!-- Input untuk kuantitas -->
<input type="number" class="form-control" id="quantity" name="quantity" min="0" value="">
</div>
</div>
<p id="stockMessage">Stok Bahan Tersisa: <?php echo $stockQuantity; ?></p>
<p id="successMessage">Stok Bahan Terkini: <?php echo $newStockQuantity; ?></p>
<div class="form-group">
<label for="deksripsi">Deskripsi<span class="gray-italic-text"> (opsional)</span></label>
<textarea class="form-control" id="deskripsi" name="deskripsi" rows="3" placeholder="Masukkan keterangan penggunaan bahan ..."></textarea>
</div>
</div>
<!-- /.card-body -->
</form>
<div class="card-footer d-flex justify-content-end">
<button type="submit" id="submitButton" class="btn btn-primary" onclick="if(validateForm()) { validateSuccess();}">Submit</button>
</div>
</div>
<!-- general form elements -->
<!-- /.card -->
</div><!-- /.container-fluid -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<!-- Control Sidebar -->
<aside class="control-sidebar control-sidebar-dark">
<!-- Control sidebar content goes here -->
</aside>
<!-- /.control-sidebar -->
</div>
<!-- ./wrapper -->
<!-- jQuery -->
<script src="assets/adminlte/plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="assets/adminlte/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- bs-custom-file-input -->
<script src="assets/adminlte/plugins/bs-custom-file-input/bs-custom-file-input.min.js"></script>
<!-- AdminLTE App -->
<script src="assets/adminlte/dist/js/adminlte.min.js"></script>
<!-- SweetAlert2 Toast -->
<script src="assets/adminlte/plugins/sweetalert2/sweetalert2.min.js"></script>
<!-- Select2 -->
<script src="assets/adminlte/plugins/select2/js/select2.full.min.js"></script>
<!-- Page specific script -->
<script>
// Select2 Dropdown
$(document).on('select2:open', () => {
document.querySelector('.select2-search__field').focus();
});
$(document).ready(function() {
//Initialize Select2 Elements
$('.select2').select2({
theme: 'bootstrap4',
width: '100%',
containerCssClass: 'height-40px',
});
});
$(function() {
bsCustomFileInput.init();
// Add an event listener to the select element
$("#pilihBahanPrototype").change(function() {
validateCurrentStock();
});
});
function validateForm() {
var selectedItem = document.getElementById("pilihBahanPrototype").value;
var quantity = document.getElementById("quantity").value;
if (selectedItem === "" || quantity === "" || quantity <= 0) {
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Harap lengkapi semua formulir!',
showCancelButton: false,
confirmButtonColor: '#3085d6',
confirmButtonText: 'OK (enter)'
}).then((result) => {
if (result.isConfirmed) {
resetForm();
}
});
return false;
}
return true;
}
function updateStockMessage() {
var stockMessage = document.getElementById("stockMessage");
var selectedQuantity = parseInt(document.getElementById("quantity").value, 10);
// Update stock message dynamically based on the selected item's stock quantity
stockMessage.innerText = "Stok Bahan Tersisa: " + (<?php echo $stockQuantity; ?> - selectedQuantity);
}
function validateCurrentStock() {
// Get the form data
var formData = $("#prototypeForm").serialize();
// Use AJAX to submit the form data and fetch the updated stock quantity
$.ajax({
type: "POST",
url: "prototype.php",
data: formData,
dataType: "json",
success: function(response) {
// Hide the stock message
document.getElementById("successMessage").style.display = "none";
// Update the stock message with the fetched quantity
document.getElementById("stockMessage").innerText = "Stok Bahan Tersisa: " + response.currentStock;
},
error: function(error) {
alert("Error, refresh the page!");
}
});
}
function validateSuccess() {
// Get the form data
var formData = $("#prototypeForm").serialize();
// Use AJAX to submit the form data and fetch the updated stock quantity
$.ajax({
type: "POST",
url: "prototype.php",
data: formData,
dataType: "json",
success: function(response) {
// Hide the stock message
document.getElementById("stockMessage").innerText = "Stok Bahan Tersisa: ";
Swal.fire({
icon: 'success',
title: 'Stok berhasil diambil!',
text: 'Stok terbaru adalah ' + response.newStock + ' bahan',
showCancelButton: false,
confirmButtonColor: '#3085d6',
confirmButtonText: 'OK (enter)'
}).then((result) => {
if (result.isConfirmed) {
resetForm();
}
});
resetForm();
},
error: function(error) {
Swal.fire({
icon: 'error',
title: 'Stok Kurang',
text: 'Kurangi kuantitas yang diinput!',
showCancelButton: false,
confirmButtonColor: '#3085d6',
confirmButtonText: 'OK (enter)'
}).then((result) => {
if (result.isConfirmed) {
resetForm();
}
});
}
});
}
function resetForm() {
document.getElementById("prototypeForm").reset();
resetDropdown();
disableQuantityInput();
}
function resetDropdown() {
const dropdown = document.getElementById("pilihBahanPrototype");
dropdown.selectedIndex = 0; // reset ke pilihan pertama
// jika multiple selection
dropdown.querySelectorAll("option:checked").forEach(option => {
option.selected = false;
});
// memicu event change
dropdown.dispatchEvent(new Event('change'));
}
// Quantity input disabled to prevent bugs
document.addEventListener("DOMContentLoaded", function() {
disableQuantityInput();
});
function disableQuantityInput() {
const quantityInput = document.getElementById("quantity");
quantityInput.placeholder = "Pilih bahan terlebih dahulu";
quantityInput.disabled = true;
}
$("#pilihBahanPrototype").change(function() {
const quantityInput = document.getElementById("quantity");
quantityInput.placeholder = "Masukkan jumlah stok bahan yang ingin digunakan";
quantityInput.disabled = false;
});
// When user press enter on keyboard
var quantityInput = document.getElementById('quantity');
quantityInput.addEventListener('keydown', function(event) {
if (event.keyCode === 13) {
event.preventDefault();
submitForm();
}
});
var deksripsiInput = document.getElementById('deskripsi');
deksripsiInput.addEventListener('keydown', function(event) {
if (event.keyCode === 13) {
event.preventDefault();
submitForm();
}
});
function submitForm() {
document.getElementById('submitButton').click();
}
</script>
</body>
</html>