MYSQL库存管理设计

时间:2016-03-24 20:16:21

标签: php mysql

我不知道数据库的设计是错误的还是我的问题处理方法是错误的,但我在库存/货运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方法,但我不知道该怎么做

2 个答案:

答案 0 :(得分:1)

使用更新查询,您的问题很容易解决,但我想这并不能回答您的问题。

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'那样的通用ID。如果您有唯一的ID,则可以将它们放回库存中。

根据您的业务逻辑,这可能是一个非常复杂的系统: - )

答案 1 :(得分:1)

<强>解决方案:

<?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();
?>