Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MYSQL的库存管理设计_Php_Mysql - Fatal编程技术网

Php MYSQL的库存管理设计

Php MYSQL的库存管理设计,php,mysql,Php,Mysql,我不知道数据库的设计是否是错误的,或者我解决问题的方法是否是错误的,但我正在使用inventory/php/mysql软件 假设我有这张桌子作为库存 | auto_inc | id_pro | name | qty | unit_price | | | | | | | | 1 | PA1 | Paper | 10 | 100 | | 2 | PA

我不知道数据库的设计是否是错误的,或者我解决问题的方法是否是错误的,但我正在使用inventory/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   |    ???     |
问题:

<?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();
?>
1.-如果我需要发送12份文件,如何将库存表中的第一行更新为0,第二行更新为8

2.-装运表中第一行的成本应为1400,如何获取此数字以存储它


我试图在这里实现fifo方法,但我不知道如何实现它

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

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,那么您可以将其放回您的库存中


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

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

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,那么您可以将其放回您的库存中


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

解决方案:

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

解决方案:

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

谢谢你的投入,这对我来说真的很复杂:谢谢你的投入,这对我来说真的很复杂:很高兴,如果你有任何问题,请告诉我。它工作得很好!如果可以的话,我还有两个问题。如果我想装运多个产品,我想我必须添加一个装运id来对装运进行分组,但我不知道怎么做。使用此结构,可以取消发货并将产品返回库存,否则我必须更改方法。如果您希望能够在同一“发货”中“发货”多个产品,则需要稍微重新设计表格。“产品”的取消也需要一点重组,因此,如果客户返回6个PA1,例如,您的数据库设计需要知道它来自
库存
中的
id
,以便在取消/返回后可以将其重新分配到正确的
库存
条目。因此,我将再添加一个表,即
orders
。通过将
orders
中的
id
链接到
order\u id
中的
order\u id
查看要分配给
shippings
的订单作为装运包,非常荣幸,如果您有任何问题,请告诉我。它工作得非常好!如果可以的话,我还有两个问题。如果我想装运多个产品,我想我必须添加一个装运id来对装运进行分组,但我不知道怎么做。使用此结构,可以取消发货并将产品返回库存,否则我必须更改方法。如果您希望能够在同一“发货”中“发货”多个产品,则需要稍微重新设计表格。“产品”的取消也需要一点重组,因此,如果客户返回6个PA1,例如,您的数据库设计需要知道它来自
库存
中的
id
,以便在取消/返回后可以将其重新分配到正确的
库存
条目。因此,我将再添加一个表,即
orders
。通过将
orders
中的
id
链接到
shippings中的
order\u id
来查看要分配给
shippings
的订单即发货包