<?
$r = "../";
require_once($r."inc/funciones.php");
validNotSession($r);
$iduser=$_SESSION['userid'];
$mysqli = connectDB($r);
$row1=$mysqli->query("SELECT * FROM z_productos");

$row10=$mysqli->query("SELECT idproducto,COUNT(*) FROM z_inventario GROUP BY idproducto");
?>
<!DOCTYPE html>
<html lang="en">
  <?php include $r."inc/head.php"; ?>
<body class="fixed-nav sidenav-toggled sticky-footer bg-dark" id="page-top">
  <?php include $r."inc/nav.php"; ?>
  <div class="content-wrapper">
    <div class="container-fluid">
      <div class="card mb-3">
        <div class="h3 card-header">
          <i class="fa fa-table"></i> INFORME EJECUTIVO INVENTARIO ACUMULADO</div>
        <div class="card-body">
          <div class="table-responsive">
            <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
              <thead>
                <tr>
                  <th>Producto</th>
                  <th>Inicial</th>
                  <th>Disponibles</th>
                  <th>Física</th>
                  <th>X Apr</th>
                  <th>X Ent</th>
                  <th>U X Cob</th>
                  <th>U Sal Bod</th>
                  <th>Dev</th>
                  <th>Bonos</th>
                  <th>x Cobrar</th>
                  <th>Cobrado</th>
                </tr>
              </thead>
        <? while($row2=$row1->fetch_assoc()){?>
        
                <tr>
                    
                  <td><? echo $row2['producto'];?></td>
                  <td><? $idproducto=$row2['id'];
            $row3=$mysqli->query("SELECT * FROM z_iinventario WHERE idproducto=$idproducto ORDER BY id DESC"); $row4=$row3->fetch_assoc(); echo $row4['cantidad']; echo '/'.round($row4['cantidad']/$row2['canastillas'], 1).' C'; ?>
                    </td>
            <td><? $idproducto=$row2['id'];
            $row3=$mysqli->query("SELECT * FROM z_inventario WHERE idproducto=$idproducto ORDER BY id DESC"); $row4=$row3->fetch_assoc(); echo $row4['cantidad'];  echo '/'.round($row4['cantidad']/$row2['canastillas'], 1).' C'; ?>
                    </td>
            <td><? $idproducto=$row2['id'];
            $row3=$mysqli->query("SELECT * FROM z_rinventario WHERE idproducto=$idproducto ORDER BY id DESC"); $row4=$row3->fetch_assoc(); echo $row4['cantidad']; echo '/'.round($row4['cantidad']/$row2['canastillas'], 1).' C'; ?>
                    </td>
            
                    
            <td><?$suma=0; $hoy = date("Y-m-d"); $row3=$mysqli->query("SELECT * FROM z_pedidos INNER JOIN z_pedido WHERE z_pedidos.codigopedido = z_pedido.codigopedido AND z_pedidos.estado='0'");
             while($row4 = $row3->fetch_assoc()) {$idd=$row4['idproducto'];if($idproducto==$idd){$suma=$suma+$row4['cantidad'];}} echo $suma;$sumafila5=$sumafila5+$suma; $suma=0; ?>
                    </td>
                    
            <td><?$suma=0; $hoy = date("Y-m-d"); $row3=$mysqli->query("SELECT * FROM z_pedidos INNER JOIN z_pedido WHERE z_pedidos.codigopedido = z_pedido.codigopedido AND z_pedidos.estado='1'");
             while($row4 = $row3->fetch_assoc()) {$idd=$row4['idproducto'];if($idproducto==$idd){$suma=$suma+$row4['cantidad']+$row4['bono'];}} echo $suma; $sumafila6=$sumafila6+$suma; $suma=0;?>
                    </td>
            <td><?$suma=0; $hoy = date("Y-m-d"); $row3=$mysqli->query("SELECT * FROM z_pedidos INNER JOIN z_pedido WHERE z_pedidos.codigopedido = z_pedido.codigopedido AND z_pedidos.estado='2'");
             while($row4 = $row3->fetch_assoc()) {$idd=$row4['idproducto'];if($idproducto==$idd){$suma=$suma+$row4['cantidad'];$debo1=$row4['devolucion'];$debo2=$debo2+$debo1;}}  if($suma1>0){?><span class="text-success"> <?} echo $suma-$debo2; $sumafila7=$sumafila7+$suma-$debo2; $suma=0;$debo2=0;?></td>
                    
                    
            <td><?$suma=0; $hoy = date("Y-m-d"); $row3=$mysqli->query("SELECT * FROM z_pedidos INNER JOIN z_pedido WHERE z_pedidos.fechasalida BETWEEN '$hoy 00:00:01' AND '$hoy 23:59:59' AND z_pedidos.codigopedido = z_pedido.codigopedido ");
             while($row4 = $row3->fetch_assoc()) {$idd=$row4['idproducto'];if($idproducto==$idd){$suma=$suma+$row4['cantidad'];$debo1=$row4['devolucion'];$debo2=$debo2+$debo1;$bono1=$row4['bono'];$bono2=$bono2+$bono1;}} echo $suma-$debo2+$bono2;$sumafila8=$sumafila8+$suma-$debo2+$bono2; $suma=0;$debo2=0;$bono2=0;?>
                    </td>
                    
                    
            <td><?$suma1=0; $hoy = date("Y-m-d"); $row3=$mysqli->query("SELECT * FROM z_pedidos INNER JOIN z_pedido WHERE z_pedidos.fechasalida BETWEEN '$hoy 00:00:01' AND '$hoy 23:59:59' AND z_pedidos.codigopedido = z_pedido.codigopedido ");
             while($row4 = $row3->fetch_assoc()) {$idd=$row4['idproducto'];if($idproducto==$idd){$suma1=$suma1+$row4['devolucion'];}} echo $suma1;$sumafila9=$sumafila9+$suma1; $suma1=0;?>
                    </td>
            <td><?$suma1=0; $hoy = date("Y-m-d"); $row3=$mysqli->query("SELECT * FROM z_pedidos INNER JOIN z_pedido WHERE z_pedidos.fechaaprovacion BETWEEN '$hoy 00:00:01' AND '$hoy 23:59:59' AND z_pedidos.codigopedido = z_pedido.codigopedido ");
             while($row4 = $row3->fetch_assoc()) {$idd=$row4['idproducto'];if($idproducto==$idd){$suma1=$suma1+$row4['bono'];}} echo $suma1;$sumafila10=$sumafila10+$suma1; $suma1=0;?>
                    </td>
            <td><?$suma1=0;$debo1=0;$debo2=0; $hoy = date("Y-m-d"); $row3=$mysqli->query("SELECT * FROM z_pedidos INNER JOIN z_pedido WHERE z_pedidos.fechaaprovacion BETWEEN '$hoy 00:00:01' AND '$hoy 23:59:59' AND z_pedidos.codigopedido = z_pedido.codigopedido  AND z_pedidos.pago='0' ");
             while($row4 = $row3->fetch_assoc()) {$idd=$row4['idproducto'];if($idproducto==$idd){$suma1=$suma1+$row4['total'];$debo1=$row4['devolucion']*$row4['precio'];$debo2=$debo2+$debo1;$abono=$row4['abono'];}} if($suma1>0){?><span class="text-danger"> <?} echo '$ '.intval($suma1-$debo2-$abono).' COP';$sumafila11=$sumafila11+$suma1-$abono-$debo2; $suma1=0;$debo1=0;$debo2=0;?>
                   </span> </td>
                    
                    
            <td><?$suma1=0;$debo1=0;$debo2=0; $hoy = date("Y-m-d"); $row3=$mysqli->query("SELECT * FROM z_pedidos INNER JOIN z_pedido WHERE z_pedidos.fechaaprovacion BETWEEN '$hoy 00:00:01' AND '$hoy 23:59:59' AND z_pedidos.codigopedido = z_pedido.codigopedido  AND z_pedidos.pago!='0' ");
             while($row4 = $row3->fetch_assoc()) {$idd=$row4['idproducto'];if($idproducto==$idd){$suma1=$suma1+$row4['total'];$debo1=$row4['devolucion']*$row4['precio'];$debo2=$debo2+$debo1;}}  if($suma1>0){?><span class="text-success"> <?}  echo '$ '.intval($suma1-$debo2+$abono).' COP'; $sumafila12=$sumafila12+$suma1-$abono-$debo2;$suma1=0;$debo1=0;$debo2=0;$abono=0;?>
                    </td>
                    
                    
                    
                </tr>
                <? } ?>
              <tbody>
        <tfoot>
            <tr>
                  <th></th>
                  <th></th>
                  <th></th>
                  <th></th>
                  <th><? echo $sumafila5 ?></th>
                  <th><? echo $sumafila6 ?></th>
                  <th><? echo $sumafila7 ?></th>
                  <th><? echo $sumafila8 ?></th>
                  <th><? echo $sumafila9 ?></th>
                  <th><? echo $sumafila10 ?></th>
                  <th>$ <? echo $sumafila11 ?> COP</th>
                  <th>$ <? echo $sumafila12 ?> COP</th>
            </tr>
        </tfoot>
            </table>
          </div>
        </div>
      </div>
      <input type="button" class="btn btn-primary float-left" value="Atras" name="Back2" onclick="history.back()" />
    </div>
  </div>
  <?php include $r."inc/foot.php"; ?>
<script>
$('#dataTable').DataTable( {
        "order": [[ 10, "asc" ]],
        "lengthMenu": [[100, 500, 1000, -1], [100, 500, 1000, "All"]]
    } );
  </script>
</body>
</html>   