hola amigos, espero puedan ayudarme, en el siguiente código tengo el resultado total de la sumas de:
cantidad de ventas, precio de compra, precio de venta y ganancia
pero no logro sumar el total de las cantidades de cada items.. probé con el código siguiente
pero el resultado es individual es decir me muestra de cada producto
mi prueba... solo obtengo el total de cada producto y no el total de todo.
Código PHP:
Ver original<?php
$ganancias = $total_s_price - $total_buy_price ;
?>
el total de todo seria ejemplo:
Cantidad total: 4
Precio compra total: 6.00
Precio venta total: 7.50
ganancia total: 1.50
sale_report.php
Código PHP:
Ver original<?php
$servername = "localhost";
$username = "username";
$password = "password";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; if(isset($_POST['submit'])){ // me podrian recomendar alguna idea como validar?
}
$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];
$sql = "SELECT s.sFecha,p.nameProducts,s.vClient,p.sale_price,p.buy_price,
COUNT(s.product_id) AS total_records,
SUM(s.gty_s) AS total_qty,
SUM(p.sale_price * s.gty_s) AS total_s_price,
SUM(p.buy_price * s.gty_s) AS total_buy_price
FROM sales s
LEFT JOIN products p ON s.product_id = p.product_id
WHERE s.sFecha BETWEEN '{$startdate}' AND '{$enddate}'
GROUP BY DATE(s.sFecha),p.nameProducts
ORDER BY DATE(s.sFecha) DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
Mostrar por dias:<br>
Cantidad de Ventas: <?php echo $total_qty = $row["total_qty"]; ?> <br> ejemplo: 1, 1, 2 etc..
Precio Venta Total: <?php echo $total_s_price = $row["total_s_price"]; ?> <br> ejemplo: 1.50, 2.50, 3.50 etc..
Precio Compra Total: <?php echo $total_buy_price = $row["total_buy_price"]; ?> <br> ejemplo: 1.00, 2.00, 3.00 etc..
EJEMPLO:
|| Producto || Ventas || Precio Compra || Precio Venta || Ganancia |||
|| Fresa || 1 || 1.00 || 1.50 || 0.50 |||
|| Manzana || 1 || 2.00 || 2.50 || 0.50 |||
|| Naranja || 2 || 3.00 || 3.50 || 0.50 |||
TOTAL 4 6.00 7.50 1.50
<?php
$ganancias = $total_s_price - $total_buy_price ;
?>
<?php
}
} else {
echo "0 result";
}
$conn->close();
?>
Aqui selecciono la fecha: inicio y final
Formulario
Código HTML:
Ver original<form method="post" action="sale_report.php"> <input type="date" name="startdate" id="startdate" data-date data-date-format="yyyy-mm-dd" value="<?php echo date('Y-m-').'01'; ?>" >
<input type="date" name="enddate" id="enddate" data-date data-date-format="yyyy-mm-dd" value="<?php echo date('Y-m-d'); ?>" >
<button type="submit" name="submit" class="btn btn-primary">Generar Reporte
</button>
Código HTML:
Ver originalCREATE TABLE `sales` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`product_id` int(11) unsigned NOT NULL,
`gty_s` int(11) NOT NULL,
`total_s` decimal(25,2) DEFAULT 0.00,
`moneymaking` decimal(25,2) DEFAULT 0.00,
`vClient` varchar(255) DEFAULT NULL,
`sFecha` datetime NOT NULL,
CREATE TABLE `products` (
`product_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`nameProducts` varchar(255) NOT NULL,
`sale_price` decimal(25,2) DEFAULT 0.00,
`buy_price` decimal(25,2) DEFAULT 0.00,
`barCode` varchar(60) NOT NULL,
`pFecha` datetime NOT NULL,