0

Desde que añadi la funcion error_reporting(E_ALL);

Me sale el siguiente error:

Notice: Undefined variable: acumulado in C:\AppServ\www\sisadmin\modulos\compras\reportes\reporte_articulos_abc_pc.php on line 144

el cual hace referencia a la variable $acumulado

define('PAGE_TITLE', 'Gestión de Compras');
define('MOD_TITLE', 'Gestión de Compras :: Reporte Artículos por Método ABC Por Precio/Cantidad');

if ($_POST['excel'] == 'SI') {
    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=REPORTE_ARTICULOS_POR_METODO_ABC_POR_PRECIO_CANTIDAD_" . date('d.m.Y') . "_" . date('h.i.s') . ".xls");
    header("Pragma: no-cache");
    header("Expires: 0");
}

$mysqli = new dbMysqli;

function Retornar_Total($sql) {
    global $mysqli;
    $total = 0;

    $res = $mysqli->query($sql);

    while ($dato = $res->fetch_object()) {
        $total += $dato->Precio * $dato->cantidad_existente;
    }

    return $total;
}

function Retornar_Nombre_Deposito($id) {
    global $mysqli;

    $sql = "SELECT nombre FROM obrasdeposito WHERE id = '$id'";
    $res = $mysqli->query($sql);
    $obj = $res->fetch_object();

    return $obj->nombre;
}
?>
<html>
    <head>
        <title><?php echo PAGE_TITLE ?></title>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
        <?php if ($_POST['excel'] == 'NO') { ?>
        <link type="text/css" rel="stylesheet" href="../../../estilo/styles.css" />
        <style type="text/css">
            .legend { width: 2%; border: 1px solid #999; text-align: center; padding: 2px; display: inline-block; }
            .right { text-align: right !important; }
        </style>
        <?php } else { ?>
        <style type="text/css"><?php require_once('../../../estilo/styles.css') ?></style>
        <?php } ?>
    </head>
    <body>
        <?php if ($_POST['excel'] == 'NO') { ?>
        <table class="table-title">
            <tr>
                <td><?php echo MOD_TITLE ?></td>
            </tr>
        </table>
        <table class="table-buttons">
            <tr>
                <td>
                    <input type="button" value="Cerrar" onClick="self.close()" class="btn btn-primary" />
                </td>
            </tr>
        </table>
        <table class="table-fields">
            <tr>
                <td width="10%"><strong>Dep&oacute;sito</strong></td>
                <td width="90%"><?php echo Retornar_Nombre_Deposito($_POST['deposito']) ?></td>
            </tr>
            <tr>
                <td width="10%"><strong>Legenda</strong></td>
                <td width="90%">
                    <span class="legend" style="background-color:#BEF781">A</span>
                    <span class="legend" style="background-color:#F3E2A9">B</span>
                    <span class="legend" style="background-color:#FA5882">C</span>
                </td>
            </tr>
        </table>
        <?php } ?>
        <table class="table-list">
            <thead>
                <tr> 
                    <td>C&oacute;digo Art&iacute;culo</td>
                    <td>Nombre Art&iacute;culo</td>
                    <td>Stock Actual * Precio</td>
                    <td>Unidad</td>
                    <td>%</td>
                    <td>Acumulado</td>
                    <td>Categor&iacute;a</td> 
                    <td>Stock Actual</td>
                    <td>Precio</td>
                </tr>
            </thead>
            <tbody>
                <?php
                $sql = "SELECT 
                            res.codigoArticulo, 
                            res.articulo, 
                            res.unidad, 
                            res.Precio, 
                            res.cantidad_existente 
                        FROM
                            (
                                SELECT 
                                    stp.cod_articulo as codigoArticulo,
                                    art.nombre as articulo,
                                    stp.stock as cantidad_existente,
                                    art.unidad as unidad,
                                    IFNULL(ROUND((
                                        SELECT dp.precio
                                        FROM detalle_pedidos as dp
                                        INNER JOIN pedidos p ON p.num_pedido = dp.num_pedido
                                        WHERE p.estado != 'ANULADA' AND dp.cod_articulo = stp.cod_articulo
                                        ORDER BY dp.num_pedido DESC
                                        LIMIT 1
                                    ), 2), 0) as Precio
                                FROM
                                    stock_tareaprogramada as stp
                                INNER JOIN 
                                    articulos as art ON stp.cod_articulo = art.cod_articulo 
                                WHERE
                                    stp.deposito_id = '{$_POST['deposito']}'
                                    AND stp.cod_articulo = art.cod_articulo
                                    AND stp.stock > 0
                                    AND art.estado != 'BAJA'
                                    AND art.stockeable = 'SI'
                            ) as res
                        ORDER BY res.Precio * res.cantidad_existente DESC";

                $res = $mysqli->query($sql);

                if ($res->num_rows > 0) {
                    $total = Retornar_Total($sql);

                    while ($dato = $res->fetch_object()) {
                        $cantidadPorPrecio = $dato->cantidad_existente * $dato->Precio;
                        $porcentaje = ($cantidadPorPrecio * 100) / $total;
                        $acumulado = $acumulado + $porcentaje;

                        if ($acumulado < 80 ) {
                            $categoria = "A";
                            $bgcolor = "#BEF781";
                        } elseif ($acumulado < 95 ) {
                            $categoria = "B";
                            $bgcolor = "#F3E2A9";
                        } else {
                            $categoria = "C";
                            $bgcolor = "#FA5882";
                        }
                        ?>
                        <tr>
                            <td><?php echo "[" . $dato->codigoArticulo. "]" ?></td>
                            <td><?php echo $dato->articulo ?></td>
                            <td class="right"><?php echo number_format($cantidadPorPrecio, 2, ",", ".") ?></td>
                            <td><?php echo $dato->unidad ?></td>
                            <td class="right"><?php echo "%" . number_format($porcentaje, 2, ',' , ".") ?></td>    
                            <td class="right"><?php echo "%" . number_format($acumulado, 2, ',' , '.') ?></td>
                            <td bgcolor="<?php echo $bgcolor ?>"><?php echo $categoria ?></td>
                            <td class="right"><?php echo number_format($dato->cantidad_existente, 2, ",", ".") ?></td>
                            <td class="right"><?php echo number_format($dato->Precio, 2, ",", ".") ?></td>
                        </tr>
                        <?
                    }
1
  • 1
    usa el boton editar y agrega toda la informacion en la pregunta, no en comentarios.
    – gbianchi
    el 7 ene. 2019 a las 15:48

1 respuesta 1

3

El problema es que, la primera vez que entra al while pides a tu variable $acumulado se sume con $porcentaje pero en el primer ciclo acumulado no existe.

while ($dato = $res->fetch_object()) {
   $cantidadPorPrecio = $dato->cantidad_existente * $dato->Precio;
   $porcentaje = ($cantidadPorPrecio * 100) / $total;
   $acumulado = $acumulado + $porcentaje;
   ...
 }

Lo puedes solucionar declarando $acumulado fuera del while

$acumulado = 0;
while ($dato = $res->fetch_object()) {
   $cantidadPorPrecio = $dato->cantidad_existente * $dato->Precio;
   $porcentaje = ($cantidadPorPrecio * 100) / $total;
   $acumulado = $acumulado + $porcentaje;
   ...
 }

El agregar error_reporting(E_ALL); te estará reportando todo lo que PHP encuentre mal en tu código, aunque este error de $acumulado es informativo y no merma la ejecución de la aplicación, es molesto ver el mensaje, peor aún si activaste la función en un ambiente de producción.

1
  • Muchas gracias, Alberto!
    – FerAvila
    el 7 ene. 2019 a las 17:27

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.