I have a table called item_movement
wherein I store number of inventory purchased, sold, returned by customer or returned to supplier:
------------------------------------------------------------------------
| trans_id | date | trans_type | inventory_id | quantity |
------------------------------------------------------------------------
| 1 | 2016-07-26 | Purchase | 1 | 10 |
------------------------------------------------------------------------
| 2 | 2016-07-26 | Purchase | 2 | 8 |
------------------------------------------------------------------------
| 3 | 2016-07-27 | Sale | 1 | 2 |
------------------------------------------------------------------------
| 4 | 2016-07-28 | Customer Return | 1 | 1 |
------------------------------------------------------------------------
| 5 | 2016-07-29 | Supplier Pullout | 2 | 5 |
------------------------------------------------------------------------
The inventory_id is the identification of each of the inventory.
My code is:
$conn = new mysqli('localhost', 'username', 'password', 'database');
$query = $_GET['id'];
$item_stock = $conn->query("SELECT * FROM item_movement WHERE `inventory_id` = '%".$query."%' ORDER BY date DESC") or die(mysql_error());
echo '<table class="stock-info-table">
<tr class="center">
<th>Date</th>
<th>Transaction Type</th>
<th>Quantity</th>
<th>Running Stock</th>
</tr>';
while($row = mysqli_fetch_array($item_stock)){
echo '<tr>
<td>'.$row['date'].'</td>
<td>'.$row['trans_type'].'</td>
<td>'.$row['quantity'].'</td>
<td>RUNNING STOCK HERE</td>
</tr>';
}
echo '</table>';
Problem: I haven't figured out how to show the running stock, as you can see in my code. The running stock column should show the number of stock movement per transaction like if the purchase quantity for inventory ID 1 is 10, then it should show 10. If there's a sale quantity of 2, then it should show 8 (10-2). Basically the formula for the stock is: Purchase - Sale + Customer Return - Supplier Pullout. The data should also show the recent transactions first (DESC), in which the sequence of the running stock should also be based.
Edit: Here's an example data I would like to show for an inventory_id:
-------------------------------------------------------------------
| Date | Transaction Type | Quantity | Running Stock |
-------------------------------------------------------------------
| 2016-07-29 | Purchase | 5 | 12 |
-------------------------------------------------------------------
| 2016-07-28 | Supplier Pullout | 2 | 7 |
-------------------------------------------------------------------
| 2016-07-27 | Customer Return | 1 | 9 |
-------------------------------------------------------------------
| 2016-07-26 | Sale | 2 | 8 |
-------------------------------------------------------------------
| 2016-07-25 | Purchase | 10 | 10 |
-------------------------------------------------------------------
Let me know if this is possible.