-
Notifications
You must be signed in to change notification settings - Fork 1
/
master_device.php
406 lines (340 loc) · 16 KB
/
master_device.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
<?php
include "connection.php";
include "admin_privilege.php";
if (isset($_GET["getDropdownOptions"])) {
$queryMasterBahan = "SELECT kelompok, nama FROM masterbahan ORDER BY nama";
$resultMasterBahan = $conn->query($queryMasterBahan);
$options = '<option value="" selected disabled>Pilih Bahan</option>';
if ($resultMasterBahan && $resultMasterBahan->num_rows > 0) {
while ($row = $resultMasterBahan->fetch_assoc()) {
$options .= '<option value="' . $row['nama'] . '">' . $row['kelompok'] . ' - ' . $row['nama'] . '</option>';
}
}
echo $options;
exit();
} elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
// Handle the POST request for submitting form data
$namaDevice = $_POST["namaDevice"];
$gambarProduk = $_POST["gambar"];
// Check if the 'nama_produk' already exists
$checkQuery = "SELECT COUNT(*) FROM produk WHERE nama_produk = ?";
$checkStmt = $conn->prepare($checkQuery);
$checkStmt->bind_param("s", $namaDevice);
$checkStmt->execute();
$checkStmt->bind_result($count);
$checkStmt->fetch();
$checkStmt->close();
// If 'nama_produk' doesn't exist, insert it into the 'produk' table
if ($count == 0) {
$queryProduk = "INSERT INTO produk (nama_produk, quantity, gambar_produk) VALUES (?, '0', ?)";
$stmtProduk = $conn->prepare($queryProduk);
$stmtProduk->bind_param("ss", $namaDevice, $gambarProduk);
$stmtProduk->execute();
$stmtProduk->close();
}
// Check if 'bahan' and 'quantity' arrays are set in POST
if (isset($_POST["pilihNamaBahan"]) && isset($_POST["quantity"])) {
$bahanArray = $_POST["pilihNamaBahan"];
$quantityArray = $_POST["quantity"];
foreach ($bahanArray as $key => $bahan) {
$quantity = $quantityArray[$key];
// Use prepared statements
$checkQuery = "SELECT COUNT(*) FROM bahan_produksi WHERE produk = ? AND nama_bahan = ?";
$checkStmt = $conn->prepare($checkQuery);
$checkStmt->bind_param("ss", $namaDevice, $bahan);
$checkStmt->execute();
$checkStmt->bind_result($count);
$checkStmt->fetch();
$checkStmt->close();
if ($count > 0) {
$updateQuery = "UPDATE bahan_produksi SET quantity = ? WHERE produk = ? AND nama_bahan = ?";
$updateStmt = $conn->prepare($updateQuery);
$updateStmt->bind_param("iss", $quantity, $namaDevice, $bahan);
$updateStmt->execute();
$updateStmt->close();
} else {
$queryHarga = "SELECT harga_bahan FROM masterbahan WHERE nama = ?";
$stmtHarga = $conn->prepare($queryHarga);
$stmtHarga->bind_param("s", $bahan);
$stmtHarga->execute();
$stmtHarga->bind_result($hargaBahan);
$stmtHarga->fetch();
$stmtHarga->close();
$query = "INSERT INTO bahan_produksi (produk, nama_bahan, quantity, harga_bahan) VALUES (?, ?, ?, ?)";
$stmt = $conn->prepare($query);
$stmt->bind_param("ssii", $namaDevice, $bahan, $quantity, $hargaBahan);
if ($stmt->execute()) {
echo "Data berhasil ditambahkan ke tabel bahan_produksi dan tabel masterbahan.";
} else {
echo "Error: " . $stmt->error;
}
$stmt->close();
}
}
} else {
echo "Error: Bahan and Quantity arrays are not set.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Master Device</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">
<!-- Ionicons -->
<link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<!-- Sweetalert2 -->
<link rel="stylesheet" href="assets/adminlte/plugins/sweetalert2-theme-bootstrap-4/bootstrap-4.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>
.input-group-append label {
margin-right: 24px;
}
.lebar-kolom1 {
width: 78%;
}
.lebar-kolom2 {
width: 12%;
}
.lebar-kolom3 {
width: 10%;
}
.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>Master Device</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">Master Device</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">Menambah Jenis Device untuk Diproduksi</h3>
</div>
<!-- /.card-header -->
<!-- form start -->
<form id="masterDeviceForm">
<div class="card-body">
<div class="form-group">
<div class="row">
<div class="col">
<label for="namaDevice">Nama Device <span style="color: red;">*</span></label>
<input type="text" class="form-control form-control-border border-width-2" id="namaDevice" name="namaDevice" placeholder="Masukkan nama device baru">
</div>
</div>
</div>
<div class="form-group">
<label for="picture">Gambar Device <span class="gray-italic-text"> (opsional)</span></label>
<div class="input-group mb-3">
<div class="input-group-prepend">
<button type="button" onclick="window.open('https://img.doerig.dev/', '_blank')" class="btn btn-primary">
<i class="fas fa-upload"></i> Upload
</button>
</div>
<input type="text" class="form-control" id="gambar" name="gambar" placeholder="Copy dan paste disini link imgur yang telah dibuat">
</div>
</div>
<div class="card-body p-0">
<div class="table-responsive">
<table id="myTable" class=" table order-list table-striped">
<thead>
<tr>
<td class="text-center lebar-kolom1"><b>Nama Bahan <span style="color: red;">*</span></b></td>
<td class="text-center lebar-kolom2"><b>Kuantitas <span style="color: red;">*</span></b></td>
<td class="text-center lebar-kolom3"><b>Aksi</b></td>
</tr>
</thead>
<tbody>
<tr>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5" style="text-align: left;">
<input type="button" class="btn btn-outline-info btn-block" id="addrow" value="+ Tambah Bahan" />
</td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
<!-- /.card-body -->
</form>
<div class="card-footer d-flex justify-content-end">
<button type="button" class="btn btn-primary" onclick="if(validateForm()) { validateSuccess(); resetForm(); }">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>
$(document).ready(function() {
$(document).on('select2:open', () => {
document.querySelector('.select2-search__field').focus();
});
var counter = 0;
$("#addrow").on("click", function() {
addRow();
});
$("table.order-list").on("click", ".ibtnDel", function(event) {
$(this).closest("tr").remove();
calculateGrandTotal();
});
function addRow() {
// Make an AJAX request to fetch dropdown options
$.ajax({
url: 'master_device.php?getDropdownOptions',
type: 'GET',
success: function(dropdownOptions) {
var newRow = $("<tr>");
var cols = "";
cols += '<td><select class="form-control select2 pilihNamaBahan" name="pilihNamaBahan[]" style="min-width:140px;">' + dropdownOptions + '</select></td>';
cols += '<td><input type="number" class="form-control quantity" name="quantity[]" min="0" value="" placeholder="0"/></td>';
cols += '<td><input type="button" class="ibtnDel btn btn-md btn-danger" value="Delete"></td>';
newRow.append(cols);
$("table.order-list").append(newRow);
counter++;
$('.select2').select2({
theme: 'bootstrap4',
width: '100%',
containerCssClass: 'height-40px',
});
},
error: function(error) {
console.log("Error fetching dropdown options: " + error);
}
});
}
});
function calculateRow(row) {
var price = +row.find('input[name^="price"]').val();
}
function calculateGrandTotal() {
var grandTotal = 0;
$("table.order-list").find('input[name^="price"]').each(function() {
grandTotal += +$(this).val();
});
$("#grandtotal").text(grandTotal.toFixed(2));
}
function validateForm() {
var selectedItem = document.getElementById("namaDevice").value;
// Use classes for dynamic elements
var namaElements = document.querySelectorAll(".pilihNamaBahan");
var quantityElements = document.querySelectorAll(".quantity");
// Check each row
for (var i = 0; i < namaElements.length; i++) {
var nama = namaElements[i].value;
var quantity = quantityElements[i].value;
if (selectedItem === "" || nama === "" || quantity === "" || quantity <= 0) {
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Harap lengkapi semua formulir!',
});
return false;
}
}
return true;
}
function validateSuccess() {
var formData = new FormData(document.getElementById("masterDeviceForm"));
$.ajax({
type: "POST",
url: "master_device.php",
data: formData,
processData: false,
contentType: false,
success: function(response) {
Swal.fire({
icon: 'success',
title: 'Device berhasil didaftarkan!',
}).then((result) => {
if (result.isConfirmed) {
location.reload();
}
});
},
error: function(error) {
// ... existing code ...
}
});
}
function resetForm() {
document.getElementById("masterDeviceForm").reset();
resetDropdown();
}
function resetDropdown() {
const dropdown = document.getElementById("pilihNamaBahan");
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'));
}
</script>
</body>
</html>