Reputation: 69
I am currently trying to form the logic where i place orders and if the order has exceeded the capacity when summed up with the current stock, the appropriate message will appear. Also, if there are incoming orders, when adding new orders they cannot exceed the capacity when summed up with pending incoming orders as well as the current stocks available. However, my problem is that no matter what amount i put, it keeps on showing my "Placed order stock amount has exceeded capacity." validation message. Is my logic around this feature wrong? Thanks.
if(isset($_POST['addBtn'])){
$brand = $_POST['brand'];
$modelName = $_POST['model_name'];
$session = $_SESSION['username'];
$station = 'At Factory';
$progress = 'Awaiting';
$status = 'Awaiting';
$handler = 'Awaiting';
$request = '-';
$stock = $_POST['stock'];
$incoming = '0';
$date = date('Y-m-j H:i:s', time());
$stock = $_POST['stock'];
$incoming = '0';
$sql2 = "SELECT o.stock,m.current,m.capacity FROM orders o
INNER JOIN models m ON o.model_id=m.model_id
WHERE o.incoming = 0";
$result2 = $conn->query($sql2);
if(!$result2) die($conn->error);
$rows = $result2->num_rows;
$stok = $rows['stock'];
$cu = $rows['current'];
$ca = $rows['capacity'];
if(($stock+$cu)>$ca){
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong id="alert2">Error! </strong>Placed order stock amount has exceeded capacity.
</div>
<?php
}elseif(($stok+$stock+$cu)>$ca){
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong id="alert2">Error! </strong>Pending incoming stocks has exceeded capacity.
</div>
<?php
}else{
$query = "INSERT INTO orders(brand,model_id,station,progress,status,editor,time_ordered,admin,request) VALUES ('".$brand."','".$modelName."','".$station."','".$progress."','".$status."','".$handler."','".$date."','".$session."','".$request."')";
}
}
Upvotes: 3
Views: 67
Reputation: 36
I believe you will need another WHERE clause in your SQL to select orders based on your model.
Example:
$sql2 = "SELECT SUM(o.stock) as stock_sum, m.current, m.capacity FROM orders o
INNER JOIN models m ON o.model_id=m.model_id
WHERE o.incoming = 0 AND m.model_id = $modelName";
But an error might occur if there are no orders. Such as $cu and $ca might be empty because you are getting those values by joining orders and models.
A great suggestion would be to have seperate SQL's to retrieve stock sum and anothe sql to retrieve model details.
Upvotes: 2