I've looked over a lot of posts on here, but cannot find a solution to my issue. I'm trying to develop a tracking for a small shop of inventory of the same product from different lots. I'm utilizing PHP and MySQL for the DB. I have tried information from the following posts without success:
How to minus from several MySQL fields a certain value?
Keep subtracting value in loop getting mysql result php
Keep the remaining value after subtract in php while loop
There was a few others, but I don't have them open but they all have the same idea. This is for a small family ran shop, and we're using PHPRunner to develop the tracking. I have the following table:
Inventory:
id | product | lot | produced | sold | remaining |
---|---|---|---|---|---|
1 | ABC | 20 | 165 | 0 | 165 |
2 | ABC | 29 | 290 | 0 | 290 |
3 | def | 37 | 320 | 0 | 320 |
4 | ABC | 63 | 145 | 0 | 145 |
5 | xyz | 77 | 900 | 0 | 900 |
Now when someone orders say 500 units of product ABC, I need to loop through the table and subtract 500 total from the rows, so the desired code to update each row that is affected
id | product | lot | produced | sold | remaining |
---|---|---|---|---|---|
1 | ABC | 20 | 165 | 165 | 0 |
2 | ABC | 29 | 290 | 290 | 0 |
3 | def | 37 | 320 | 0 | 320 |
4 | ABC | 63 | 145 | 45 | 100 |
5 | xyz | 77 | 900 | 0 | 900 |
The amount of sale (sold) would come from a form, where the product number is selected along with the amount. I've been racking my brain the last week trying numerous methods to get this to work, with no avail. When I need it to do is loop through each row of product ABC, subtract the max amount from each line until the sold amount is zero, and update each row with the amount sold from each lot and the amount remaining.
Thank you