-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_sale.php
More file actions
107 lines (93 loc) · 3.74 KB
/
add_sale.php
File metadata and controls
107 lines (93 loc) · 3.74 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
<?php
$page_title = 'Add Inactive';
require_once('includes/load.php');
page_require_level(2);
?>
<?php
if (isset($_POST['add_sale'])) {
$req_fields = array('s_id', 'quantity', 'comment', 'price');
validate_fields($req_fields);
if (empty($errors)) {
$p_id = $db->escape((int)$_POST['s_id']);
$s_qty = $db->escape((int)$_POST['quantity']);
$comment = $db->escape($_POST['comment']);
$repairable = $db->escape($_POST['repairable']);
$price = $db->escape((float)$_POST['price']); // Capture the price
$date = $_POST['date'];
if (empty($date)) {
$s_date = make_date();
} else {
$s_date = $db->escape($date);
}
// Call update_product_usage_qty for quantity deduction for inactives
$update_result = update_product_usage_qty($s_qty, $p_id);
if ($update_result === false) {
$session->msg('d', 'Insufficient quantity or product not found in product_usage.');
redirect('add_sale.php', false);
}
$price_cents = $db->escape($price); // Store the price without conversion
$sql = "INSERT INTO sales (";
$sql .= "product_id, qty, date, comment, repairable, price";
$sql .= ") VALUES (";
$sql .= "'{$p_id}','{$s_qty}','{$s_date}','{$comment}','{$repairable}','{$price_cents}'"; // Insert $price_cents
$sql .= ")";
if ($db->query($sql)) {
$session->msg('s', "Inactive item added. ");
redirect('sales.php', false);
} else {
$session->msg('d', ' Sorry failed to add!');
redirect('add_sale.php', false);
}
} else {
$session->msg("d", $errors);
redirect('add_sale.php', false);
}
}
?>
<?php include_once('layouts/header.php'); ?>
<div class="row">
<div class="col-md-6">
<?php echo display_msg($msg); ?>
<form method="post" action="add_sale.php">
<div class="form-group">
<label for="s_id">Select a product</label>
<select class="form-control" name="s_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="quantity">Quantity</label>
<input type="text" class="form-control" name="quantity">
</div>
<div class="form-group">
<label for="date">Date</label>
<input type="date" class="form-control datepicker" name="date">
</div>
<div class="form-group">
<label for="comment">Comment</label>
<input type="text" class="form-control" name="comment">
</div>
<div class="form-group">
<label for="price">Approximate Value</label>
<input type="number" class="form-control" name="price" step="0.01">
</div>
<div class="form-group">
<label for="repairable">Repairable</label>
<input type="radio" id="no" name="repairable" value="no" class="radio-repairable">
<label for="no" class="radio-repairable-label">No</label>
<input type="radio" id="unsure" name="repairable" value="unsure" class="radio-repairable" checked>
<label for="unsure" class="radio-repairable-label">Unsure</label>
<input type="radio" id="yes" name="repairable" value="yes" class="radio-repairable">
<label for="yes" class="radio-repairable-label">Yes</label>
</div>
<button type="submit" name="add_sale" class="btn btn-primary">Add Inactive</button>
</form>
</div>
</div>
<?php include_once('layouts/footer.php'); ?>