问题
I'm doing an inventory project where you can create orders and product receptions. Each order can have multiple products. The database is like this:
CREATE TABLE `inventorio_orden` (
`inventorio_orden_id` int(11) NOT NULL,
`usuario_id` int(11) NOT NULL,
`inventorio_orden_fecha` date NOT NULL,
`inventorio_orden_destino` varchar(255) COLLATE ucs2_spanish_ci NOT NULL,
`inventorio_orden_nombre` varchar(255) COLLATE ucs2_spanish_ci NOT NULL,
`inventorio_orden_status` enum('active','inactive') COLLATE ucs2_spanish_ci NOT NULL,
`inventorio_orden_fecha_creada` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=ucs2 COLLATE=ucs2_spanish_ci;
CREATE TABLE `inventorio_orden_producto` (
`inventorio_orden_producto_id` int(11) NOT NULL,
`inventorio_orden_id` int(11) NOT NULL,
`producto_id` int(11) NOT NULL,
`cantidad` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=ucs2 COLLATE=ucs2_spanish_ci;
CREATE TABLE `productos` (
`producto_id` int(11) NOT NULL,
`categoria_id` int(11) NOT NULL,
`marca_id` int(11) NOT NULL,
`producto_nombre` text COLLATE ucs2_spanish_ci NOT NULL,
`producto_descripcion` text COLLATE ucs2_spanish_ci NOT NULL,
`producto_cantidad` int(11) NOT NULL,
`producto_unidad` varchar(125) COLLATE ucs2_spanish_ci NOT NULL,
`producto_ingresado_por` int(11) NOT NULL,
`producto_status` enum('active','inactive') COLLATE ucs2_spanish_ci NOT NULL,
`producto_fecha` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=ucs2 COLLATE=ucs2_spanish_ci;
As you can see there is a correlation table, one order can contain many products so this is why inventorio_orden_producto excist.
So I been trying to reduce the stock from table productos. This has to happen every time an order is created, to be more specific, each time the there is an insert in the table inventorio_orden_producto.
Here is the code
{
if($_POST['btn_action'] == 'Agregar')
{
$query = "
INSERT INTO inventorio_orden (usuario_id, inventorio_orden_fecha, inventorio_orden_destino, inventorio_orden_nombre,
inventorio_orden_status, inventorio_orden_fecha_creada)
VALUES (:usuario_id, :inventorio_orden_fecha, :inventorio_orden_destino, :inventorio_orden_nombre,
:inventorio_orden_status, :inventorio_orden_fecha_creada
)
";
$statement = $connect->prepare($query);
$statement->execute(
array(
':usuario_id' => $_SESSION["usuario_id"],
':inventorio_orden_fecha' => $_POST['inventorio_orden_fecha'],
':inventorio_orden_destino' => $_POST['inventorio_orden_destino'],
':inventorio_orden_nombre' => $_POST['inventorio_orden_nombre'],
':inventorio_orden_status' => 'active',
':inventorio_orden_fecha_creada' => date("Y-m-d"),
)
);
$result = $statement->fetchAll();
$statement = $connect->query("SELECT LAST_INSERT_ID()");
$inventorio_orden_id = $statement->fetchColumn();
if(isset($inventorio_orden_id))
{
for($count = 0; $count<count($_POST["producto_id"]); $count++)
{
$product_details = fetch_product_details($_POST["producto_id"][$count], $connect);
$sub_query = "
INSERT INTO inventorio_orden_producto (inventorio_orden_id, producto_id,cantidad) VALUES (:inventorio_orden_id, :producto_id, :cantidad)
";
$statement = $connect->prepare($sub_query);
$statement->execute(
array(
':inventorio_orden_id'=> $inventorio_orden_id,
':producto_id' => $_POST["producto_id"][$count],
':cantidad' => $_POST["cantidad"][$count]
)
);
$result = $statement->fetchAll();
$statement = $connect->query("SELECT LAST_INSERT_ID()");
$inventorio_orden_producto_id = $statement->fetchColumn();
if(isset($inventorio_orden_producto_id ))
{
$query0 ="SELECT inventorio_orden_producto.producto_id FROM inventorio_orden_producto
INNER JOIN inventorio_orden ON inventorio_orden.inventorio_orden_id = inventorio_orden_producto.inventorio_orden_id
WHERE inventorio_orden_producto.inventorio_orden_producto_id = ".$inventorio_orden_producto_id."
";
$statement0 = $connect->prepare($query);
$statement0->execute();
$producto_id = $statement0->fetchAll();
$producto_data = fetch_product_details($producto_id, $connect);
$query = "
SELECT inventorio_orden_producto.cantidad FROM inventorio_orden_producto
INNER JOIN inventorio_orden ON inventorio_orden.inventorio_orden_id = inventorio_orden_producto.inventorio_orden_id
WHERE inventorio_orden_producto.producto_id = ".$producto_id."";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$total = 0;
foreach($result as $row)
{
$total = $total + $row['cantidad'];
}
$query2="SELECT producto_cantidad * FROM productos WHERE producto_id = ".$producto_id."";
$statement2 = $connect->prepare($query2);
$statement2->execute();
$result2 = $statement2->fetchAll();
$totalstock = 0;
foreach($result2 as $row2)
{
$totalstock = $row['producto_cantidad'];
}
$available_quantity = intval($totalstock) - intval($total);
if(isset($available_quantity))
{
$update_query = "
UPDATE producto SET
producto_cantidad = ".$available_quantity."
WHERE producto_id = ".$producto_id."
";
$statement = $connect->prepare($update_query);
$statement->execute();
}
//return $available_quantity;
if(isset($result))
{
echo 'Orden Creada';
}
}
}
}
}
While the order is created and doesn't show any error it doesn't affect the stock from the productos table.
来源:https://stackoverflow.com/questions/60744039/reduce-stock-from-product-when-an-order-is-created