- Obtener enlace
- X
- Correo electrónico
- Otras aplicaciones
Featured Post
- Obtener enlace
- X
- Correo electrónico
- Otras aplicaciones
Codigo fuente del index.php
<?php
include_once "funciones.php";
if(isset($_POST['buscarPorId'])){
if(empty($_POST['idCliente'])) header("location: index.php");
}
$idCliente= (isset($_POST['idCliente'])) ? $_POST['idCliente'] : null;
$ventas = obtenerVentas($idCliente);
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<title>Ventas</title>
</head>
<body>
<div class="container">
<br />
<div class="card" style="box-shadow:5px 5px 5px grey;">
<div class="card-header bg-success text-white">Buscar venta por Id de cliente</div>
<div class="card-body">
<div class="row mb-2">
<div class="col">
<form action="" method="post" class="row">
<div class="col-2">
<input class="form-control mb-2" type="text" name="idCliente" style="width: 180px;box-shadow:5px 5px 5px grey;" placeholder="Ingrese nro id">
</div>
<div class="col-10">
<input type="submit" name="buscarPorId" value="Buscar" class="btn btn-warning" style="box-shadow:5px 5px 5px grey;">
</div>
</form>
</div>
</div>
<hr style="border: 3px inset #eee; height: 1px;">
<?php if(count($ventas) > 0){?>
<table class="table" style="box-shadow:5px 5px 5px grey;">
<thead class="table-secondary">
<tr>
<th>#</th>
<th>Cliente</th>
<th>Fecha</th>
<th>Total</th>
<th>Detalle Venta</th>
</tr>
</thead>
<tbody>
<?php foreach($ventas as $venta) {?>
<tr>
<td><?= $venta->id;?></td>
<td><?= $venta->cliente;?></td>
<td><?= $venta->fecha;?></td>
<td>$<?= $venta->total;?></td>
<td>
<table class="table table-striped" style="box-shadow:3px 3px 3px 3px grey;">
<thead class="table-dark">
<tr>
<th>id</th>
<th>cantidad</th>
<th>nombre</th>
<th>precio</th>
<th>subtotal</th>
</tr>
</thead>
<tbody>
<?php foreach($venta->productos as $producto) {?>
<tr>
<td><?= $producto->id;?></td>
<td><?= $producto->cantidad;?></td>
<td><?= $producto->nombre;?></td>
<td><?= $producto->precio;?></td>
<th>$<?= $producto->cantidad * $producto->precio ;?></th>
</tr>
<?php }?>
</tbody>
</table>
</td>
</tr>
<?php }?>
</tbody>
</table>
<?php }?>
<?php if(count($ventas) < 1){?>
<div class="alert alert-warning mt-3" role="alert">
<h2>No se han encontrado ventas con ese id</h2>
</div>
<?php }?>
</div>
</div>
</div>
</body>
</html>
include_once "funciones.php";
if(isset($_POST['buscarPorId'])){
if(empty($_POST['idCliente'])) header("location: index.php");
}
$idCliente= (isset($_POST['idCliente'])) ? $_POST['idCliente'] : null;
$ventas = obtenerVentas($idCliente);
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<title>Ventas</title>
</head>
<body>
<div class="container">
<br />
<div class="card" style="box-shadow:5px 5px 5px grey;">
<div class="card-header bg-success text-white">Buscar venta por Id de cliente</div>
<div class="card-body">
<div class="row mb-2">
<div class="col">
<form action="" method="post" class="row">
<div class="col-2">
<input class="form-control mb-2" type="text" name="idCliente" style="width: 180px;box-shadow:5px 5px 5px grey;" placeholder="Ingrese nro id">
</div>
<div class="col-10">
<input type="submit" name="buscarPorId" value="Buscar" class="btn btn-warning" style="box-shadow:5px 5px 5px grey;">
</div>
</form>
</div>
</div>
<hr style="border: 3px inset #eee; height: 1px;">
<?php if(count($ventas) > 0){?>
<table class="table" style="box-shadow:5px 5px 5px grey;">
<thead class="table-secondary">
<tr>
<th>#</th>
<th>Cliente</th>
<th>Fecha</th>
<th>Total</th>
<th>Detalle Venta</th>
</tr>
</thead>
<tbody>
<?php foreach($ventas as $venta) {?>
<tr>
<td><?= $venta->id;?></td>
<td><?= $venta->cliente;?></td>
<td><?= $venta->fecha;?></td>
<td>$<?= $venta->total;?></td>
<td>
<table class="table table-striped" style="box-shadow:3px 3px 3px 3px grey;">
<thead class="table-dark">
<tr>
<th>id</th>
<th>cantidad</th>
<th>nombre</th>
<th>precio</th>
<th>subtotal</th>
</tr>
</thead>
<tbody>
<?php foreach($venta->productos as $producto) {?>
<tr>
<td><?= $producto->id;?></td>
<td><?= $producto->cantidad;?></td>
<td><?= $producto->nombre;?></td>
<td><?= $producto->precio;?></td>
<th>$<?= $producto->cantidad * $producto->precio ;?></th>
</tr>
<?php }?>
</tbody>
</table>
</td>
</tr>
<?php }?>
</tbody>
</table>
<?php }?>
<?php if(count($ventas) < 1){?>
<div class="alert alert-warning mt-3" role="alert">
<h2>No se han encontrado ventas con ese id</h2>
</div>
<?php }?>
</div>
</div>
</div>
</body>
</html>
Codigo fuente de funciones.php
<?php
function obtenerVentas($idCliente){
$parametros = [];
$sentencia = "SELECT ventas.* , IFNULL(clientes.nombre, 'CONS.FINAL') AS cliente FROM ventas
INNER JOIN clientes ON clientes.id = ventas.idCliente WHERE ventas.idCliente LIKE ?";
array_push($parametros,"%$idCliente%");
$ventas = select($sentencia,$parametros);
return agregarProductosVendidos($ventas);
}
///////////////////////////////////////////////////////////////
function agregarProductosVendidos($ventas){
foreach($ventas as $venta){
$venta->productos = obtenerProductosVendidos($venta->id);
}
return $ventas;
}
//////////////////////////////////////////////
function obtenerProductosVendidos($idVenta){
$sentencia = "SELECT productos_ventas.id,productos_ventas.cantidad, productos_ventas.precio, productos.nombre,productos.compra
FROM productos_ventas
INNER JOIN productos ON productos.id = productos_ventas.idProducto
WHERE idVenta = ? ";
return select($sentencia, [$idVenta]);
}
//////////////////////////////////////////////
function select($sentencia, $parametros = []){
$bd = conectarBaseDatos();
$respuesta = $bd->prepare($sentencia);
$respuesta->execute($parametros);
return $respuesta->fetchAll();
}
//////////////////////////////////////////////
function conectarBaseDatos() {
$host = "localhost";
$db = "ventas_php";
$user = "root";
$pass = "root";
$charset = 'utf8mb4';
$options = [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_OBJ,
\PDO::ATTR_EMULATE_PREPARES => false,
];
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
try {
$pdo = new \PDO($dsn, $user, $pass, $options);
return $pdo;
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
}
?>
Archivo Sql de la Base Datos ventas_php
--
-- Base de datos: `ventas_php`
--
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `clientes`
--
CREATE TABLE `clientes` (
`id` bigint(20) UNSIGNED NOT NULL,
`nombre` varchar(255) NOT NULL,
`telefono` varchar(25) NOT NULL,
`direccion` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Volcado de datos para la tabla `clientes`
--
INSERT INTO `clientes` (`id`, `nombre`, `telefono`, `direccion`) VALUES
(1, 'juan', '1234', 'dad'),
(2, 'pedro', '22', 'dasd 333');
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `productos`
--
CREATE TABLE `productos` (
`id` bigint(20) UNSIGNED NOT NULL,
`codigo` varchar(255) NOT NULL,
`nombre` varchar(255) NOT NULL,
`compra` decimal(8,2) NOT NULL,
`venta` decimal(8,2) NOT NULL,
`existencia` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Volcado de datos para la tabla `productos`
--
INSERT INTO `productos` (`id`, `codigo`, `nombre`, `compra`, `venta`, `existencia`) VALUES
(1, '123', 'ACEITE 1 LTRO', '700.00', '890.00', 38),
(2, '265', 'PAN 1KG', '222.00', '300.00', 20);
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `productos_ventas`
--
CREATE TABLE `productos_ventas` (
`id` bigint(20) UNSIGNED NOT NULL,
`cantidad` int(11) NOT NULL,
`precio` decimal(8,2) NOT NULL,
`idProducto` bigint(20) NOT NULL,
`idVenta` bigint(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Volcado de datos para la tabla `productos_ventas`
--
INSERT INTO `productos_ventas` (`id`, `cantidad`, `precio`, `idProducto`, `idVenta`) VALUES
(1, 1, '890.00', 1, 1),
(2, 1, '890.00', 1, 2),
(3, 2, '300.00', 2, 1),
(4, 2, '890.00', 1, 3),
(5, 2, '300.00', 2, 4);
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `ventas`
--
CREATE TABLE `ventas` (
`id` bigint(20) UNSIGNED NOT NULL,
`fecha` datetime NOT NULL,
`total` decimal(9,2) NOT NULL,
`idUsuario` bigint(20) NOT NULL,
`idCliente` bigint(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Volcado de datos para la tabla `ventas`
--
INSERT INTO `ventas` (`id`, `fecha`, `total`, `idUsuario`, `idCliente`) VALUES
(1, '2022-09-11 18:07:35', '1490.00', 1, 1),
(2, '2022-09-11 18:55:36', '890.00', 2, 2),
(3, '2022-09-13 20:08:00', '1780.00', 1, 1),
(4, '2022-10-28 19:43:27', '600.00', 1, 1);
--
-- Índices para tablas volcadas
--
--
-- Indices de la tabla `clientes`
--
ALTER TABLE `clientes`
ADD PRIMARY KEY (`id`);
--
-- Indices de la tabla `productos`
--
ALTER TABLE `productos`
ADD PRIMARY KEY (`id`);
--
-- Indices de la tabla `productos_ventas`
--
ALTER TABLE `productos_ventas`
ADD PRIMARY KEY (`id`);
--
-- Indices de la tabla `ventas`
--
ALTER TABLE `ventas`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT de las tablas volcadas
--
--
-- AUTO_INCREMENT de la tabla `clientes`
--
ALTER TABLE `clientes`
MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
--
-- AUTO_INCREMENT de la tabla `productos`
--
ALTER TABLE `productos`
MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
--
-- AUTO_INCREMENT de la tabla `productos_ventas`
--
ALTER TABLE `productos_ventas`
MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
--
-- AUTO_INCREMENT de la tabla `ventas`
--
ALTER TABLE `ventas`
MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
COMMIT;
-- Base de datos: `ventas_php`
--
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `clientes`
--
CREATE TABLE `clientes` (
`id` bigint(20) UNSIGNED NOT NULL,
`nombre` varchar(255) NOT NULL,
`telefono` varchar(25) NOT NULL,
`direccion` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Volcado de datos para la tabla `clientes`
--
INSERT INTO `clientes` (`id`, `nombre`, `telefono`, `direccion`) VALUES
(1, 'juan', '1234', 'dad'),
(2, 'pedro', '22', 'dasd 333');
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `productos`
--
CREATE TABLE `productos` (
`id` bigint(20) UNSIGNED NOT NULL,
`codigo` varchar(255) NOT NULL,
`nombre` varchar(255) NOT NULL,
`compra` decimal(8,2) NOT NULL,
`venta` decimal(8,2) NOT NULL,
`existencia` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Volcado de datos para la tabla `productos`
--
INSERT INTO `productos` (`id`, `codigo`, `nombre`, `compra`, `venta`, `existencia`) VALUES
(1, '123', 'ACEITE 1 LTRO', '700.00', '890.00', 38),
(2, '265', 'PAN 1KG', '222.00', '300.00', 20);
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `productos_ventas`
--
CREATE TABLE `productos_ventas` (
`id` bigint(20) UNSIGNED NOT NULL,
`cantidad` int(11) NOT NULL,
`precio` decimal(8,2) NOT NULL,
`idProducto` bigint(20) NOT NULL,
`idVenta` bigint(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Volcado de datos para la tabla `productos_ventas`
--
INSERT INTO `productos_ventas` (`id`, `cantidad`, `precio`, `idProducto`, `idVenta`) VALUES
(1, 1, '890.00', 1, 1),
(2, 1, '890.00', 1, 2),
(3, 2, '300.00', 2, 1),
(4, 2, '890.00', 1, 3),
(5, 2, '300.00', 2, 4);
-- --------------------------------------------------------
--
-- Estructura de tabla para la tabla `ventas`
--
CREATE TABLE `ventas` (
`id` bigint(20) UNSIGNED NOT NULL,
`fecha` datetime NOT NULL,
`total` decimal(9,2) NOT NULL,
`idUsuario` bigint(20) NOT NULL,
`idCliente` bigint(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Volcado de datos para la tabla `ventas`
--
INSERT INTO `ventas` (`id`, `fecha`, `total`, `idUsuario`, `idCliente`) VALUES
(1, '2022-09-11 18:07:35', '1490.00', 1, 1),
(2, '2022-09-11 18:55:36', '890.00', 2, 2),
(3, '2022-09-13 20:08:00', '1780.00', 1, 1),
(4, '2022-10-28 19:43:27', '600.00', 1, 1);
--
-- Índices para tablas volcadas
--
--
-- Indices de la tabla `clientes`
--
ALTER TABLE `clientes`
ADD PRIMARY KEY (`id`);
--
-- Indices de la tabla `productos`
--
ALTER TABLE `productos`
ADD PRIMARY KEY (`id`);
--
-- Indices de la tabla `productos_ventas`
--
ALTER TABLE `productos_ventas`
ADD PRIMARY KEY (`id`);
--
-- Indices de la tabla `ventas`
--
ALTER TABLE `ventas`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT de las tablas volcadas
--
--
-- AUTO_INCREMENT de la tabla `clientes`
--
ALTER TABLE `clientes`
MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
--
-- AUTO_INCREMENT de la tabla `productos`
--
ALTER TABLE `productos`
MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
--
-- AUTO_INCREMENT de la tabla `productos_ventas`
--
ALTER TABLE `productos_ventas`
MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
--
-- AUTO_INCREMENT de la tabla `ventas`
--
ALTER TABLE `ventas`
MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
COMMIT;
Post publicado por:
- Obtener enlace
- X
- Correo electrónico
- Otras aplicaciones

Comentarios
Publicar un comentario