-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_usage.php
More file actions
150 lines (115 loc) · 4.59 KB
/
edit_usage.php
File metadata and controls
150 lines (115 loc) · 4.59 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
<?php
$page_title = 'Edit Usage';
require_once('includes/load.php');
// Check user's permission level
page_require_level(3);
$usage = find_by_id('product_usage', (int)$_GET['id']);
$product = find_by_id('products', $usage['product_id']);
if (!$usage) {
$session->msg("d", "Missing usage ID.");
redirect('usages.php');
}
if (isset($_POST['update_usage'])) {
$req_fields = array('date');
validate_fields($req_fields);
if (empty($errors)) {
$used_qty = (int)$usage['quantity'];
if (isset($_POST['quantity_tickbox']) && $_POST['quantity_tickbox'] == 'on') {
$used_qty = (int)$_POST['used_qty'];
$old_qty = (int)$usage['quantity'];
$diff_qty = $old_qty - $used_qty;
// Check if the new quantity would result in a negative product quantity
if (($product['quantity'] + $diff_qty) < 0) {
$session->msg("d", "Insufficient quantity. Cannot update to a negative value.");
redirect("edit_usage.php?id={$usage['id']}", false);
}
}
$u_date = $db->escape($_POST['date']);
$updated_date = date("Y-m-d", strtotime($u_date));
// Update usage record
$sql = "UPDATE product_usage SET quantity = {$used_qty}, date = '{$updated_date}' WHERE id = '{$usage['id']}'";
$result = $db->query($sql);
if ($result && $db->affected_rows() === 1) {
// Update product quantity
if (isset($diff_qty)) {
$new_product_qty = (int)$product['quantity'] + $diff_qty;
$update_product_qty_sql = "UPDATE products SET quantity = {$new_product_qty} WHERE id = '{$usage['product_id']}'";
$db->query($update_product_qty_sql);
}
$session->msg("s", "Usage updated.");
redirect('usages.php', false);
} else {
$session->msg("d", "Sorry, failed to update usage.");
redirect("edit_usage.php?id={$usage['id']}", false);
}
} else {
$session->msg("d", $errors);
redirect("edit_usage.php?id={$usage['id']}", false);
}
}
?>
<!-- <?php include_once('layouts/header.php'); ?> -->
<div class="row">
<div class="col-md-6">
<?php echo display_msg($msg); ?>
</div>
</div>
<!-- Popup - New -->
<div class="addNewgGroupPopUpWap">
<div class="newgroupPopHeadWrap">
<div class="iconHeadGroup">
<img src="libs/logos/table-logo/inventory.png" alt="logo">
<div class="addPopGroupTitle">Add Active</div>
<?php echo display_msg($msg); ?>
</div>
<div class="addNewGroupContent">
<div class="addNewForm">
<form method="post" action="edit_usage.php?id=<?php echo (int)$usage['id'];?>">
<div class="inputGrup">
<label for="name">Select a Product</label>
<input type="text" class="form-control" name="product_name" value="<?php echo $product['name']; ?>" readonly>
</div>
<div class="form-group" style="display:none">
<label for="quantity_tickbox">Modify Quantity</label>
<input type="checkbox" name="quantity_tickbox" id="quantity_tickbox" onclick="toggleQuantityField()" checked>
</div>
<div class="inputGrup">
<label for="used_qty">Used Quantity</label>
<input type="number" class="form-control" name="used_qty" id="used_qty" value="<?php echo (int)$usage['quantity']; ?>">
</div>
<div class="inputGrup">
<label for="date">Date</label>
<input type="date" class="form-control datepicker" name="date" value="<?php echo $usage['date']; ?>">
</div>
<div class="buttonGroup">
<div>
<div class="cancelBtn">
<button type="button" id="cancelButton">Cancel</button>
</div>
<div class="submitbtn">
<button type="submit" name="update_usage" class="btn btn-primary">Add Usage</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script>
document.getElementById('cancelButton').addEventListener('click', function() {
window.history.back();
});
</script>
<?php include_once('layouts/footer.php'); ?>
<script>
function toggleQuantityField() {
var quantityField = document.getElementById("used_qty");
var quantityTickbox = document.getElementById("quantity_tickbox");
if (quantityTickbox.checked) {
quantityField.readOnly = false;
} else {
quantityField.readOnly = true;
}
}
</script>