<?php
session_start();
require_once('conexion.php');
require_once('include/sesiones.php');
if (isset($_SESSION['usu_nombre']))
{
    $regis=mysqli_query($con,"SELECT * from permiso where modulo_mod_id='28' and usuario_usu_id='$usu_id'") or die(mysqli_error($con));
    $reg=mysqli_fetch_array($regis);
    if($reg['per_visualizar']=="Activo")
    {
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include ('head.php'); ?>
</head>
<body>
<?php
$inicio="";
$M_inventario="";
$M_contactos="";
$M_compras="";
$M_ventas="";
$M_pedidos="";
$M_pos="";
$R_reportes="active";

$ventas="";
$caja="";
$compra="";
$contactos="";
$inventario="active";
$utilidad="";
include ('menu.php');
?> 
    
<div class="main">
    <div class="col-md-2">
        <?php include ('include/menu_reportes.php');?>
    </div>
    <div class="col-md-10">
        <div class="widget stacked">
            <div class="widget-content">
                <div class="widget stacked">
                    <div class="widget-content">
                        <?php include ('php_reportes/menu_inventario.php');?>  
                    </div>
                </div>
                <div class="widget stacked widget-table action-table">
                <?php 
                    $desde=$_GET['desde'];
                    $articulo=$_GET['articulo'];
                    $hasta=date('Y-m_d'); 
                ?>    
                    <div class="widget-content">
                        <div class="alert alert-info alert-dismissable">
                            <strong>
                                <?php 

                                $sql11=mysqli_query($con,"SELECT *, a.art_id as art_id ,b.art_id as art_art_id  FROM articulos a, articulos_has_bodegas b
                                where a.art_id=b.articulos_art_id and b.art_id='$articulo' ");
                                $ar=mysqli_fetch_array($sql11);
                             echo   $art_art_id=$ar['art_art_id'];
                                $art_id=$ar['art_id'];
                                $art_referencia=$ar['art_referencia'];
                                $atributo1=$ar['atributo1'];

                                $atri1=mysqli_query($con,"SELECT * FROM atributos1 where atri_art_id='$art_id' and atri_id='$atributo1'");
                                $atri1 = mysqli_fetch_array($atri1);
                                if (isset($atri1['atri_nombre'])) { 
                                    $atri1_id=$atri1['atri_id']; 
                                    $atri1=$atri1['atri_nombre']; 
                                } 
                                else {  
                                    $atri1_id='0';
                                    $atri1='';
                                }

                                $atributo2=$ar['atributo2'];
                                $atri2=mysqli_query($con,"SELECT * FROM atributos2 where atri_art_id2='$art_id' and atri_id2='$atributo2'");
                                $atri2 = mysqli_fetch_array($atri2);
                                if (isset($atri2['atri_nombre2'])) { 
                                    $atri2_id=$atri2['atri_id2'];  
                                    $atri2=$atri2['atri_nombre2']; 
                                } else { 
                                    $atri2_id='0';
                                    $atri2='';
                                }
                                
                                echo $art_nombre=$ar['art_nombre']; echo '<span style="color: red;">';echo ' '; echo $atri1; echo '-'; echo $atri2; echo '</span';
                                ?>
                                    
                                </strong>
                        </div>

          
<div class="tabbable">
    <ul class="nav nav-tabs">
        <li class="active"> <a href="#ventas" data-toggle="tab">VENTAS</a></li>
        <li><a href="#compras" data-toggle="tab">COMPRAS</a></li>
        <li><a href="#aparatdo" data-toggle="tab">APARTADOS</a></li>
        <li><a href="#devo" data-toggle="tab">DEVOLUCION</a></li>
        <li><a href="#anulados" data-toggle="tab">ANULADOS</a></li>
        <li><a href="#traslados" data-toggle="tab">TRASLADOS</a></li>
        <li><a href="#ajustes" data-toggle="tab">AJUSTES DE INVENTARIO</a></li>
    </ul>
    <div class="tab-content">
        <div class="tab-pane active" id="ventas">
            <div class="table-responsive">
                <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                        <td style="width: 8%">FACT N°</td>
                            <td>CLIENTE</td>
                            <td style="width: 8%">FECHA</td>
                            <td>NOMBRE</td>
                            <td>CANTIDAD</td>
                            <td style="width: 10%">PRECIO VENTA</td>
                            <td style="width: 5%"></td>
                        </tr>
                    </thead>
                <?php
                    $canti=0;
                    $cantidad1=0;
                    $registros=mysqli_query($con,"SELECT * FROM ventas where ven_fecha BETWEEN '$desde' AND '$hasta' and empresa='$empresa' and ven_estado<>'anulada' and sucursal='$sucursal' order by ven_id desc") or die(mysqli_error($con));
                    while ($reg=mysqli_fetch_array($registros))
                    {
                        $ven_id=$reg['ven_id'];
                        $ven_fecha=$reg['ven_fecha'];
                    
                        $sql=mysqli_query($con,"SELECT * from venta_detalle where ventas_ven_id='$ven_id' and art_art_id='$art_art_id'");
                        while ($row=mysqli_fetch_array($sql))
                        {
                ?>  
                        <tr>
                            <td ><?php echo $reg['ven_n_venta']; ?></td>
                            <td ><?php echo $reg['ven_nombre']; ?></td>
                            <td ><?php echo $ven_fecha; ?></td>
                            <td ><?php echo $row['vend_nombre']; echo $row['vend_unidad'] ?> </td>
                            <td ><?php echo $row['vend_cantidad']; ?></BR></td>
                            <td ><span class="pull-right"><?php echo number_format($row['vend_precio_real'],0); ?></span></td>
                            <td > <?php include ('include/boton_venta2.php'); ?></td>
                        </tr>
                <?php 
                            $cantidad=$row['vend_cantidad'];
                            $cantidad1+=$cantidad;
                        } 
                    }
                    $canti+=$cantidad1;
                ?>
                        <tr>
                            <td></td>
                            <td></td>
                            <td></td>
                            <th>TOTAL</th>
                            <th><?php echo $canti; ?></th>
                            <td></td>
                            <td></td>
                        </tr> 
                
                </table> 
            </div> 
        </div>



        <div class="tab-pane " id="compras">
            <div class="table-responsive">
                <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <td style="width: 8%">COMPRA N°</td>
                            <td style="width: 8%">PROVEEDOR</td>
                            <td style="width: 8%">FECHA</td>
                            <td style="width: 10%">CANTIDAD</td>
                            <td style="width: 10%">PRECIO COMPRA</td>
                        </tr>
                    </thead>
                <?php
                    $ccanti=0;
                    $ccantidad1=0; 
                    $registros=mysqli_query($con,"SELECT * FROM compras where com_fecha BETWEEN '$desde' AND '$hasta' and sucursal='$sucursal' and empresa='$empresa' order by com_id desc") or die(mysqli_error($con));
                    while ($reg=mysqli_fetch_array($registros))
                    {
                        $com_id=$reg['com_id'];
                        $com_fecha=$reg['com_fecha'];
                        $sql=mysqli_query($con,"SELECT * from compra_detalle where compras_com_id='$com_id' AND comd_art_id='$art_id' and atributo1='$atri1_id' and atributo2='$atri2_id'");
                        while ($row=mysqli_fetch_array($sql))
                        {
                ?>  
                        <tr>
                            <td style="width: 10%"><?php echo $reg['com_n_compra']; ?></td>
                            <td style="width: 10%"><?php echo $reg['proveedor_nombre']; ?></td>
                            <td style="width: 8%"><?php echo $com_fecha; ?></td>
                            <td style="width: 10%"><?php echo $row['comd_cantidad']; ?></td>
                            <td style="width: 10%"><span class="pull-right"><?php echo number_format($row['comd_precio'],0); ?></span></td>
                        </tr>
                <?php  
                            $ccantidad=$row['comd_cantidad'];
                            $ccantidad1+=$ccantidad;
                        } 
                    }
                    $ccanti +=$ccantidad1;
                ?>  
                        <tr>
                            <td style="width: 10%"></td>
                            <td style="width: 10%"></td>
                            <th style="width: 8%">TOTAL</th>
                            <th style="width: 10%"><?php echo $ccanti; ?></th>
                            <td style="width: 10%"></td>
                        </tr>
                </table> 
            </div> 
        </div>

        <div class="tab-pane " id="aparatdo">
            <div class="table-responsive">
                <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <td style="width: 8%">APARTADO N°</td>
                            <td style="width: 8%">CLIENTE</td>
                            <td style="width: 8%">FECHA</td>
                            <td style="width: 10%">CANTIDAD</td>
                        </tr>
                    </thead>
                <?php
                    $veapar=mysqli_query($con,"SELECT * FROM ventas_apartadas where vena_fecha BETWEEN '$desde' AND '$hasta' and sucursal='$sucursal' and empresa='$empresa' and vena_estado='apartada' order by vena_id desc") or die(mysqli_error($con));
                    while ($venapa=mysqli_fetch_array($veapar))
                    {
                        $vena_id=$venapa['vena_id'];
                        $vena_fecha=$venapa['vena_fecha'];
                        $vena_n_venta=$venapa['vena_n_venta'];
                        $vena_nombre=$venapa['vena_nombre'];

                        $sqlap=mysqli_query($con,"SELECT * from venta_apartadas_detalle where ventas_apartadas_vena_id ='$vena_id' AND venad_art_id='$art_id' ");
                        while ($rowap=mysqli_fetch_array($sqlap))
                        {
                ?>  
                        <tr>
                            <td style="width: 10%"><?php echo $vena_n_venta; ?></td>
                            <td style="width: 10%"><?php echo $vena_nombre; ?></td>
                            <td style="width: 8%"><?php echo $vena_fecha; ?></td>
                            <td style="width: 10%"><span><?php echo $rowap['venad_cantidad']; ?></span></td>
                        </tr>
                <?php  
                           
                        } 
                    }
                   
                ?>  
                         
                </table> 
            </div> 
        </div>
     


        <div class="tab-pane" id="devo">
            <div class="table-responsive">
                <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <td style="width: 8%">FACTURA N°</td>
                            <td style="width: 8%">CLIENTE</td>
                            <td style="width: 8%">FECHA</td>
                            <td style="width: 10%">CANTIDAD</td>
                            <td style="width: 10%">PRECIO VENTA</td>
                        </tr>
                    </thead>
                <?php
                    $art_art_id;
                    $desde=$_GET['desde'];
                    $hasta=date('Y-m_d'); 
                    $dcanti=0;
                    $dcantidad1=0; 
                    $registros=mysqli_query($con,"SELECT * FROM devolucion where dev_fecha BETWEEN '$desde' AND '$hasta' and empresa='$empresa' and art_art_id='$art_art_id' order by dev_id desc") or die(mysqli_error($con));
                    while ($reg=mysqli_fetch_array($registros))
                    {
                        $ventas_ven_id=$reg['ventas_ven_id'];
                        $ve=mysqli_query($con,"SELECT * FROM ventas where ven_id='$ventas_ven_id'") or die(mysqli_error($con));
                        $ven=mysqli_fetch_array($ve);
                ?>  
                        <tr>
                            <td style="width: 10%"><?php echo $ven['ven_n_venta']; ?></td>
                            <td style="width: 10%"><?php echo $ven['ven_nombre']; ?></td>
                            <td style="width: 8%"><?php echo $reg['dev_fecha'];?></td>
                            <td style="width: 10%"><?php echo $reg['dev_cantidad']; ?></td>
                            <td style="width: 10%"><span class="pull-right"><?php echo number_format($reg['dev_precio'],0); ?></span></td>
                        </tr>
                <?php  
                        $dcantidad=$reg['dev_cantidad'];
                        $dcantidad1+=$dcantidad;
                    } 
                    $dcanti +=$dcantidad1;
                ?>  
                        <tr>
                            <td style="width: 10%"></td>
                            <td style="width: 10%"></td>
                            <th style="width: 8%">TOTAL</th>
                            <th style="width: 10%"><?php echo $dcanti; ?></th>
                            <td style="width: 10%"></td>
                        </tr>
                </table>  
            </div> 
        </div>
            


            <div class="tab-pane" id="anulados">
                <div class="table-responsive">
                    <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <td style="width: 8%">FACTURA N°</td>
                            <td style="width: 8%">CLIENTE</td>
                            <td style="width: 8%">FECHA</td>
                            <td style="width: 10%">CANTIDAD</td>
                            <td style="width: 10%">PRECIO VENTA</td>
                        </tr>
                    </thead>
                <?php
                    $desde=$_GET['desde'];
                    $hasta=date('Y-m_d'); 
                    $registros=mysqli_query($con,"SELECT * FROM ventas where ven_fecha BETWEEN '$desde' AND '$hasta' and sucursal='$sucursal' and empresa='$empresa' and ven_estado='anulada' order by ven_n_venta desc") or die(mysqli_error($con));
                    while ($reg=mysqli_fetch_array($registros))
                    {
                        $ven_id=$reg['ven_id'];
                        $ven_fecha=$reg['ven_fecha'];
                        $sql=mysqli_query($con,"SELECT * from venta_detalle where ventas_ven_id='$ven_id' AND  art_art_id='$art_art_id'");
                        while ($row=mysqli_fetch_array($sql))
                        {
                ?>  
                        <tr>
                            <td style="width: 10%"><?php echo $reg['ven_n_venta']; ?></td>
                            <td style="width: 10%"><?php echo $reg['ven_nombre']; ?></td>
                            <td style="width: 8%"><?php echo $ven_fecha; ?></td>
                            <td style="width: 10%"><?php echo $row['vend_cantidad']; ?></td>
                            <td style="width: 10%"><span class="pull-right"><?php echo number_format($row['vend_precio_real'],0); ?></span></td>
                        </tr>
                    <?php } } ?> 
                  </table> 
                </div> 
            </div>


            
            <div class="tab-pane " id="ajustes">
                <div class="table-responsive">
                    <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <td style="width: 8%">FECHA</td>
                            <td style="width: 8%">TIPO DE AJUSTE</td>
                            <td style="width: 10%">CANTIDAD</td>
                        </tr>
                    </thead>
                <?php
                    $desde=$_GET['desde'];
                    $hasta=date('Y-m_d'); 
                    $canti_ajuste=0;
                    $cantidad1_ajuste=0;

                    $registros=mysqli_query($con,"SELECT * FROM ajuste_inventario where aju_fecha BETWEEN '$desde' AND '$hasta' and sucursal='$sucursal' and  empresa='$empresa' order by aju_id desc") or die(mysqli_error($con));
                    while ($reg=mysqli_fetch_array($registros))
                    {
                        $aju_id=$reg['aju_id'];
                        $sql=mysqli_query($con,"SELECT * from ajuste_detalle where ajuste_inventario_aju_id='$aju_id' AND art_id_bod='$art_art_id'");
                        while ($row=mysqli_fetch_array($sql))
                        {
                ?>  
                        <tr>
                            <td style="width: 8%"><?php echo $reg['aju_fecha']; ?></td>
                            <td style="width: 8%"><?php echo $row['ajud_tipo']; ?></td>
                            <td style="width: 10%"><?php echo $row['ajud_cantidad']; ?></td>
                        </tr>
                <?php 
                            $cantidad=$row['ajud_cantidad'];
                            $cantidad1_ajuste+=$cantidad;

                        } } 
                          $canti_ajuste +=$cantidad1_ajuste;?> 
                    </table> 
                </div> 
            </div>


           



            
        <div class="tab-pane " id="traslados">
                  <div class="table-responsive">
                    <table class="table table-striped table-bordered">
                    <thead>
                      <tr>
                        <td style="width: 3%"># </td>
                        <td style="width: 5%">FECHA</td>
                        <td style="width: 2%">CANTIDAD</td>
                        <td style="width: 5%">PROCESO</td>
                        <td style="width: 10%">ORIGEN</td>
                        <td style="width: 10%">DESTINO</td>
                        <td style="width: 5%"></td>
                      </tr>
                    </thead>
                    <tbody  class="buscar">
                   <?php
                    $desde=$_GET['desde'];
                    $hasta=date('Y-m-d'); 
                    $registrost=mysqli_query($con,"SELECT * FROM  trasladoe where tra_fecha BETWEEN '$desde' AND '$hasta'  order by tra_id desc  ") or die(mysqli_error($con));
                      while ($regt=mysqli_fetch_array($registrost))
                    {
                    $tra_id=$regt['tra_id'];
                    $tra_empresa_o=$regt['tra_empresa_o'];
                   
                    $tra_empresa_d=$regt['tra_empresa_d'];
                    $sql1=mysqli_query($con, "SELECT * from trasladoe_detalle where trasladoe_tra_id='$tra_id' AND trad_referencia='$art_referencia'");
                     while ($rowt=mysqli_fetch_array($sql1))
                      {
                    ?>  
                      <tr>
                        <td><?php echo $regt['tra_n_traslado']; ?></td>
                        <td><?php echo $regt['tra_fecha']; ?></td>
                        <td><?php echo $rowt['trad_cantidad']; ?></td>
                        <?php if($tra_empresa_o == $empresa) { ?>
                        <td ><i class="btn-icon-only icon-long-arrow-right alert-danger fa-2x"></i>ENVIADO</td>

                        <?php  } else { ?>
                        <td><i class="btn-icon-only icon-long-arrow-left alert-success fa-2x"></i>RECIBIDO</td>
                        <?php  }  ?>
                        <td >
                            <?php  
                                $empre_o=mysqli_query($con,"SELECT * from empresa where emp_id='$tra_empresa_o' ");
                                $empre_o=mysqli_fetch_array($empre_o);
                                echo $empre_o['emp_nombre']; 
                            ?>
                        </td>
                        <td >
                            <?php  
                                $empre_d=mysqli_query($con,"SELECT * from empresa where emp_id='$tra_empresa_d' ");
                                $empre_d=mysqli_fetch_array($empre_d);
                                echo $empre_d['emp_nombre']; 
                            ?>
                            </td>

                         <td >
                            <a data-id="<?php echo $regt["tra_id"];?>" class="btn btn-tras btn-xs btn-info"  title="Ver Traslado"> <i class="btn-icon-only icon-eye-open"></i></a>
                             <a href="libraries/tcpdf/documentos/traslado_emp.php?tra_id=<?php echo $regt['tra_id'];?>" target="_blank" class="btn btn-success btn-xs" ><span class="btn-icon-only icon-print"></span></a>
                        </td>
                      </tr>
                     <?PHP }} ?> 

                     <script type="text/javascript">
//traslado
$(".btn-tras").click(function(){
      id = $(this).data("id");
      $.get("php_traslados_emp/form_detalle.php","id="+id,function(data){
        $("#form-tras").html(data);
      });
      $('#editModal').modal('show');
    });
</script>

<div class="modal fade" id="editModal" tabindex="-1" role="dialog"  >
   <div class="modal-dialog  modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Detalle del Traslado</h4>
            </div>
                <div class="modal-body">
                    <div id="form-tras"></div>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
</div><!-- /.modal -->
 
                    </tbody> 
                  </table> </div> 
              </div>


             <?php  $ccanti-$canti-$canti_ajuste; ?>


                            </div>
                        </div> <!-- /widget-content -->
                    </div>
                </div>
            </div> <!-- /widget -->
        </div></br></br>
    </div> <!-- /span6 -->
</div> <!-- /main -->
<?php
    include ('php_caja/caja_abrir.php');
    include ('script.php');
?>
<?php include ('php_reportes/form_inventario.php'); ?>
</body>
</html>
<?php
}
else
{
   echo '<script> setTimeout("history.back(1)", 0); </script>';
}
}
else
{
    echo '<script>alert("primero debes Iniciar Cesion para ver esta pagina")</script>';
    echo '<script>location.href = "index.php";</script>';
}
?>