我不知道数据库的设计是否错误,或者我解决问题的方法是错误的,但我正在使用一个库存/货运php/mysql软件。
假设这张桌子是我的存货
| auto_inc | id_pro | name | qty | unit_price |
| | | | | |
| 1 | PA1 | Paper | 10 | 100 |
| 2 | PA1 | Paper | 10 | 200 |
| 3 | FO1 | Folder | 6 | 300 |
| 4 | FO1 | Folder | 6 | 400 | 假设我有这张装运的桌子
| auto_inc | where_to | id_pro | name | qty | cost |
| | | | | | |
| 1 | RGUA | CF1 | Paper | 12 | ??? |
| 2 | SANV | PA1 | Folder | 7 | ??? |问题:
1.-如果我需要发送12份文件,如何在库存表中将第一行更新为0,第二行更新为8。
2.-装运表中第一行的费用应为1400,我如何才能得到这个编号来储存它。
我试图在这里实现fifo方法,但我不知道如何实现
发布于 2016-03-24 21:42:04
解决方案:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$db = "inventory_db";
//connection to the database
$mysqli = mysqli_connect($hostname, $username, $password, $db);
//Input variables
$code = "PA1";
$name = "Paper";
$qtyreq = 12;
$where_to = "ABCD";
//Output variables
$totalcost = 0;
$qtyout = 0;
if ($result = $mysqli->query("SELECT `auto_inc`, `id_pro`, `name`, `qty`, `unit_price` FROM `inventory` where `id_pro` = '$code' order by unit_price asc"))
{
while ($row = mysqli_fetch_assoc($result))
{
if($qtyreq > 0)
{
$batchout = 0;
$rem = max($row['qty']-$qtyreq,0);
if($rem == 0)
$batchout = $row['qty']; //This means there are no items of this cost remaining
else
$batchout = $qtyreq; //This means there are items remaining and therefore our next loop (within the while) will check for the next expensive item
$totalcost += ($batchout * $row['unit_price']);
$qtyreq -= $batchout;
$qtyout += $batchout;
$sql = "Update inventory set qty = (qty - $batchout) where auto_inc = ".$row["auto_inc"];
echo $sql."</br>";
$mysqli->query($sql);
}
}
$sql = "Insert into shipments (`where_to`, `id_pro`, `name`, `qty`, `cost`) values ('$where_to','$code','$name',$qtyout,$totalcost)";
echo $sql;
$mysqli->query($sql);
$result->free();
}
$mysqli->close();
?>发布于 2016-03-24 20:47:52
通过更新查询,您的问题很容易解决,但我想这并不能回答您的问题。
UPDATE inventory SET qty = 0 WHERE auto_inc = 1;
UPDATE inventory SET qty = 8 WHERE auto_inc = 2;
UPDATE shipments SET cost = 1400 WHERE auto_inc = 1;如果是用PHP编写的,则需要创建一些函数。
根据库存的数量来确定价格。先按单价订购最便宜的(如果这是你的业务逻辑中想要的话)。如果你没有足够的库存,以最便宜的价格,找到下一个。把你要寄出的东西存起来。
然后用计算出的价格更新装运行中的价格。
然后更新库存中的库存。
但是,如果您正在编写更复杂的系统,例如,如果一批货物被取消了怎么办?如果你想要被取消的货物回到库存,那么你需要为每一个产品使用唯一的id,而不是像'PA1‘这样的通用产品。如果你有唯一的身份证,那么你可以把它们放回你的存货里。
根据您的业务逻辑,这可能是一个相当复杂的系统:-)
https://stackoverflow.com/questions/36209266
复制相似问题