Ver Mensaje Individual
  #1 (permalink)  
Antiguo 26/03/2021, 03:13
pilucho
 
Fecha de Ingreso: noviembre-2004
Ubicación: NULL
Mensajes: 652
Antigüedad: 19 años, 5 meses
Puntos: 6
sumar el total de todo los items

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
  1. <?php
  2.             $ganancias  = $total_s_price - $total_buy_price ;
  3.             echo number_format($ganancias,2)."<br>";
  4.             ?>

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
  1. <?php
  2.             $year = date('Y');
  3.             $servername = "localhost";
  4.             $username = "username";
  5.             $password = "password";
  6.             $conn = new mysqli($servername, $username, $password);
  7.             if ($conn->connect_error) {  die("Connection failed: " . $conn->connect_error); } echo "Connected successfully";
  8.             if(isset($_POST['submit'])){
  9.             // me podrian recomendar alguna idea como validar?
  10.             }
  11.             $startdate = $_POST['startdate'];
  12.             $enddate = $_POST['enddate'];
  13.  
  14.             $sql  = "SELECT s.sFecha,p.nameProducts,s.vClient,p.sale_price,p.buy_price,
  15.            COUNT(s.product_id) AS total_records,
  16.            SUM(s.gty_s) AS total_qty,
  17.            SUM(p.sale_price * s.gty_s) AS total_s_price,
  18.            SUM(p.buy_price * s.gty_s) AS total_buy_price
  19.            FROM sales s
  20.            LEFT JOIN products p ON s.product_id = p.product_id
  21.            WHERE s.sFecha BETWEEN '{$startdate}' AND '{$enddate}'
  22.            GROUP BY DATE(s.sFecha),p.nameProducts
  23.            ORDER BY DATE(s.sFecha) DESC";
  24.  
  25.             $result = $conn->query($sql);
  26.             if ($result->num_rows > 0) {
  27.             while($row = $result->fetch_assoc()) {
  28.             ?>
  29.             Mostrar por dias:<br>
  30.             Cantidad de Ventas:  <?php  echo $total_qty = $row["total_qty"]; ?> <br> ejemplo: 1, 1, 2 etc..
  31.             Precio Venta Total:  <?php  echo $total_s_price = $row["total_s_price"]; ?> <br>  ejemplo: 1.50, 2.50, 3.50 etc..
  32.             Precio Compra Total:  <?php  echo $total_buy_price = $row["total_buy_price"]; ?> <br> ejemplo: 1.00, 2.00, 3.00 etc..
  33.  
  34.             EJEMPLO:
  35.             ||  Producto  ||  Ventas  ||  Precio Compra ||  Precio Venta  || Ganancia  |||
  36.             ||  Fresa     ||    1     ||  1.00          ||  1.50          ||  0.50     |||
  37.             ||  Manzana   ||    1     ||  2.00          ||  2.50          ||  0.50     |||
  38.             ||  Naranja   ||    2     ||  3.00          ||  3.50          ||  0.50     |||
  39.             TOTAL               4         6.00              7.50              1.50
  40.  
  41.            <?php
  42.             $ganancias  = $total_s_price - $total_buy_price ;
  43.             echo number_format($ganancias,2)."<br>";
  44.             ?>
  45.             <?php
  46.             }
  47.             } else {
  48.             echo "0 result";
  49.             }
  50.             $conn->close();
  51. ?>


Aqui selecciono la fecha: inicio y final
Formulario
Código HTML:
Ver original
  1. <form method="post" action="sale_report.php">
  2.                 <input type="date" name="startdate" id="startdate" data-date data-date-format="yyyy-mm-dd" value="<?php echo date('Y-m-').'01'; ?>" >
  3.                 <input type="date" name="enddate" id="enddate" data-date data-date-format="yyyy-mm-dd" value="<?php echo date('Y-m-d'); ?>" >
  4.               </div>
  5.           </div>
  6.           <div class="form-group">
  7.                <button type="submit" name="submit" class="btn btn-primary">Generar Reporte</button>
  8.           </div>
  9. </form>




Código HTML:
Ver original
  1. CREATE TABLE `sales` (
  2.   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  3.   `product_id` int(11) unsigned NOT NULL,
  4.   `gty_s` int(11) NOT NULL,
  5.   `total_s` decimal(25,2) DEFAULT 0.00,
  6.   `moneymaking` decimal(25,2) DEFAULT 0.00,
  7.   `vClient` varchar(255) DEFAULT NULL,
  8.   `sFecha` datetime NOT NULL,
  9.  
  10.   CREATE TABLE `products` (
  11.   `product_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  12.   `nameProducts` varchar(255) NOT NULL,
  13.   `sale_price` decimal(25,2) DEFAULT 0.00,    
  14.   `buy_price` decimal(25,2) DEFAULT 0.00,
  15.   `barCode` varchar(60) NOT NULL,
  16.   `pFecha` datetime NOT NULL,