我有两个数据表stock_incomes、stock_outcomes和stock_outcomes_fifo(我插入的是预先计算的数据):
stock_incomes(存储剩余数据)
id| Levtv
-----------
7 | 100
8 | 250
9 | 350
stock_outcomes(这里是重点)
id| Quantity
--------------
1 | 150*
我对库存产出没有问题。数量小于100(来自stock_incomes的min(Id),请参阅下面的代码),但我不知道要写什么代码,如果结果大于100,我可以得到计算结果。在我的示例中,我使用了150,我希望下一个表中的数据如下:
stock_outcomes_fifo(我希望插入前两个表中预先计算的数据)
id| IncomeId| OutcomeId| OutcomePart| Leftv
---------------------------------------------
1 | 7 | 1 | 100 | 0
2 | 8 | 1 | 50 | 200
这是我的代码,里面有问题(见代码的最后一部分):
<?php
include_once("config.inc.php");
include_once("db.class.php");
// stock_outcomes
$db = new db($host, $database, $user, $passwd);
$sql = "SELECT * FROM stock_outcomes WHERE Id = '1'";
$mas = $db->get_array($sql);
if($mas) {
foreach ($mas as $k => $v) {
$OutcomeId = $mas[$k]['Id'];
$OutcomeQuantity = $mas[$k]['Quantity'];
}
}
// stock_incomes
$sql = "select * from stock_incomes where Id = (select min(Id) from stock_incomes where Leftv > 0)";
$mas = $db->get_array($sql);
if($mas) {
foreach ($mas as $k => $v) {
$IncomeId = $mas[$k]['Id'];
$IncomeLeftv = $mas[$k]['Leftv'];
}
}
// insert into stock_outcomes_fifo
if ($OutcomeQuantity <= $IncomeLeftv) {
$OutcomePart = $OutcomeQuantity;
$FifoLeftv = $IncomeLeftv - $OutcomeQuantity;
mysql_query("INSERT INTO `stock_outcomes_fifo` (IncomeId,OutcomeId,OutcomePart,Leftv) VALUES ($IncomeId, $OutcomeId, $OutcomePart, $FifoLeftv)");
}
if ($OutcomeQuantity > $IncomeLeftv) {
// I have no idea what php function to use in this case... please give me direction, thank you...
}
?>
2条答案
按热度按时间svmlkihl1#
问题已经解决,下面是最终的工作代码,以防有人需要:
1zmg4dgp2#