query($query_producto); if ($result_producto && $result_producto->num_rows > 0) { $row = $result_producto->fetch_assoc(); $nombre_producto = $row['nombre']; $stock_inicial = [ 'tipo' => 'stock inicial', 'cantidad' => $row['stock_inicial'], 'unidad_medida' => $row['unidad_medida'], 'precio_unitario' => $row['precio_unitario'], 'fecha' => $row['fecha_creacion'] ]; $total_cantidad += $row['stock_inicial']; if ($row['precio_unitario'] > 0) { $total_precio += $row['precio_unitario']; $entradas_para_promedio++; } $total_lempiras += $row['stock_inicial'] * $row['precio_unitario']; } } // OBTENER MOVIMIENTOS $query = "SELECT m.*, p.nombre, p.unidad_medida, p.fecha_vencimiento FROM movimientos m INNER JOIN productos p ON m.producto_id = p.id"; if ($producto_id) { $query .= " WHERE m.producto_id = $producto_id"; } $query .= " ORDER BY m.fecha ASC"; $result = $conn->query($query); if ($result) { while ($fila = $result->fetch_assoc()) { $movimientos[] = $fila; if ($fila['tipo'] === 'entrada') { $total_cantidad += $fila['cantidad']; if ($fila['precio_unitario'] > 0) { $total_precio += $fila['precio_unitario']; $entradas_para_promedio++; $total_lempiras += $fila['cantidad'] * $fila['precio_unitario']; } } elseif ($fila['tipo'] === 'salida') { $total_cantidad -= $fila['cantidad']; $total_lempiras += $fila['cantidad'] * $fila['precio_unitario']; // Incluir también en el total general } } } ?>
# | Producto | Fecha Vencimiento | Tipo | Cantidad | Unidad | Precio Unitario (L) | Total (L) | Fecha | {$contador} | " . htmlspecialchars($nombre_producto) . " | - | stock inicial | {$stock_inicial['cantidad']} | {$stock_inicial['unidad_medida']} | L " . number_format($stock_inicial['precio_unitario'], 2) . " | L " . number_format($total, 2) . " | {$stock_inicial['fecha']} | "; $contador++; } foreach ($movimientos as $fila) { $total = ($fila['precio_unitario'] > 0) ? $fila['cantidad'] * $fila['precio_unitario'] : "-"; echo "
---|---|---|---|---|---|---|---|---|
{$contador} | " . htmlspecialchars($fila['nombre']) . " | " . htmlspecialchars($fila['fecha_vencimiento']) . " | {$fila['tipo']} | {$fila['cantidad']} | " . htmlspecialchars($fila['unidad_medida']) . " | " . ($fila['precio_unitario'] > 0 ? "L " . number_format($fila['precio_unitario'], 2) : "-") . " | " . ($total !== "-" ? "L " . number_format($total, 2) : "-") . " | {$fila['fecha']} |
{$contador} | " . htmlspecialchars($dato['nombre']) . " | - | stock actual | {$dato['stock']} | " . htmlspecialchars($dato['unidad']) . " | " . ($precio_promedio > 0 ? "L " . number_format($precio_promedio, 2) : "-") . " | " . ($total > 0 ? "L " . number_format($total, 2) : "-") . " | {$dato['ultima_fecha']} |
Totales | = $total_cantidad ?> | = $entradas_para_promedio > 0 ? "L " . number_format($total_precio / $entradas_para_promedio, 2) : "-" ?> | L = number_format($total_lempiras, 2) ?> |