-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_usage.php
More file actions
86 lines (78 loc) · 3.17 KB
/
Copy pathadd_usage.php
File metadata and controls
86 lines (78 loc) · 3.17 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
<?php
$page_title = 'Add Usage';
require_once('includes/load.php');
page_require_level(2);
?>
<?php
if (isset($_POST['add_usage'])) {
$req_fields = array('product_id', 'used_qty', 'date');
validate_fields($req_fields);
$p_id = $db->escape((int)$_POST['product_id']);
$used_qty = $db->escape((int)$_POST['used_qty']);
$date = $db->escape($_POST['date']);
$u_date = date("Y-m-d", strtotime($date));
// Call update_product_qty for quantity deduction
$update_result = update_product_qty($used_qty, 0, $p_id, 'usage'); // Usage action
if ($update_result === false) {
$session->msg('d', 'Insufficient quantity or product not found.');
redirect('add_usage.php', false);
}
$sql = "INSERT INTO product_usage (product_id, date, quantity)";
$sql .= " VALUES ('{$p_id}', '{$u_date}', {$used_qty})";
$result = $db->query($sql);
if ($result && $db->affected_rows() === 1) {
$session->msg('s', "Usage added.");
redirect('usages.php', false);
} else {
$session->msg('d', 'Sorry, failed to add usage!');
redirect('usages.php', false);
}
}
?>
<?php include_once('layouts/header.php'); ?>
<div class="row">
<div class="col-md-12">
<?php echo display_msg($msg); ?>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading clearfix">
<strong>
<span class="glyphicon glyphicon-th"></span>
<span>Add Usage</span>
</strong>
<div class="pull-right">
<a href="usages.php" class="btn btn-primary">Show All Usages</a>
</div>
</div>
<div class="panel-body">
<form method="post" action="add_usage.php">
<div class="form-group">
<label for="product_id">Select a product</label>
<select class="form-control" name="product_id">
<option value="">Select a product</option>
<?php
$products = find_all('products');
foreach ($products as $product) :
?>
<option value="<?php echo $product['id']; ?>"><?php echo $product['name']; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="used_qty">Used Quantity</label>
<input type="text" class="form-control" name="used_qty">
</div>
<div class="form-group">
<label for="date">Date</label>
<input type="date" class="form-control datepicker" name="date" value="<?php echo date('Y-m-d'); ?>">
</div>
<button type="submit" name="add_usage" class="btn btn-primary">Add Usage</button>
</form>
</div>
</div>
</div>
</div>
<?php include_once('layouts/footer.php'); ?>