<?php
include "conn.php";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // Make MySQLi throw exceptions.

        try {
            // Try Connect to the DB with new MySqli object - Params {hostname, userid, password, dbname}
            $mysqli = new mysqli($host, $user, $pass, $base);

            // query
            $query = "SELECT a.fecha,b.codigo,b.descripcion,b.color,b.genero,b.linea,a.suc AS origen,m.destino
            ,b.talla,m.cant AS cant,0 AS dif,0 AS confirm,m.exist_orig,m.exist_dest,m.porc_orig,m.porc_dest 
            ,a.ultima_entrada,a.ultima_venta
            ,b.transito,a.rc
            FROM inv_res a INNER JOIN globales 
            INNER JOIN inv b ON b.tienda=a.suc AND a.rc=b.rc
            INNER JOIN movs_buenos m ON m.fecha=a.fecha AND m.origen=a.suc AND m.rc=a.rc AND m.talla=b.`talla`
            AND a.transito=0
            WHERE a.fecha=CURDATE()
            AND DATEDIFF(CURDATE(),ultima_entrada) > dias_venta_movs
            #AND DATEDIFF(CURDATE(),ultima_venta) > dias_venta_movs
            AND b.talla<>'UNITALLA'
            AND a.suc<>'00 CDS'
            ORDER BY origen,destino,codigo";

            if ($result = $mysqli->query($query)) {

                /* fetch associative array */
                while ($row = $result->fetch_assoc()) {
                    
                    $tabla = $tabla . '<tr>
                    <td>'.$row["codigo"].'</td>
                    <td>'.$row["descripcion"].'</td>
                    <td>'.$row["origen"].'</td>
                    <td>'.$row["destino"].'</td>
                    <td>'.$row["cant"].'</td>
                  </tr>';
                }

                // ******** TOTAL **************
                $query = "SELECT '' AS fecha,'' codigo,'TOTAL' AS descripcion,'' AS color,'' AS genero,'' AS linea,'' AS origen,'' AS destino
                ,'' AS talla,SUM(m.cant) AS cant,0 AS dif,0 AS confirm,SUM(m.exist_orig) AS exist_orig
                ,SUM(m.exist_dest) AS exist_dest,AVG(m.porc_orig) AS porc_orig,format(avg(m.porc_dest),1) AS porc_dest 
                ,MAX(a.ultima_entrada) AS ultima_entrada,MAX(a.ultima_venta) AS ultima_venta
                ,SUM(b.transito) AS transito,a.rc
                FROM inv_res a INNER JOIN globales 
            INNER JOIN inv b ON b.tienda=a.suc AND a.rc=b.rc
            INNER JOIN movs_buenos m ON m.fecha=a.fecha AND m.origen=a.suc AND m.rc=a.rc AND m.talla=b.`talla`
            AND a.transito=0
            WHERE a.fecha=CURDATE()
            AND DATEDIFF(CURDATE(),ultima_entrada) > dias_venta
            #AND DATEDIFF(CURDATE(),ultima_venta) > dias_venta
            AND b.talla<>'UNITALLA'
            AND a.suc<>'00 CDS'";
    
                if ($result = $mysqli->query($query)) {
    
                    /* fetch associative array */
                    while ($row = $result->fetch_assoc()) {
                        
                        $totales = $totales . '<tr>
                        <th>'.$row["codigo"].'</th>
                        <th>'.$row["descripcion"].'</th>
                        <th>'.$row["origen"].'</th>
                        <th>'.$row["destino"].'</th>
                        <th>'.$row["cant"].'</th>
                      </tr>';
                    }
                }
                //<td><a href="sucursal_detalle_lotifica.php?rc='.$row["rc"].'">'.$row["rc"].'</a></td>

                /* free the memory associated with this result set */
                $result->free();
            }
        } catch (mysqli_sql_exception $e) { // Failed to connect? Lets see the exception details..
            echo "MySQLi Error Code: " . $e->getCode() . "<br />";
            echo "Exception Msg: " . $e->getMessage();
            echo "<ht>".$query;
            exit; // exit and close connection.
        }

        $mysqli->close(); // finally, close the connection
        
 ?>  
 
<!DOCTYPE html>
<html>

<body>

<table>
<tr>
  <td>Codigo</td>
  <td>Descripcion</td>
  <td>Origen</td>
  <td>Destino</td>
  <td>Cant</td>
</tr>

<?php echo $tabla;?>
</table>

<script>
window.print();
</script>  

</body>

</html>