<?php
ob_start();
ini_set('session.gc_maxlifetime', 14400);
session_set_cookie_params(14400);
session_start();

if (!isset($_SESSION["nombre"]))
{
    header("Location: ../../login/login.php");
}
else
{
    require '../header.php';
    require_once('../../config/Conexion.php');
if($_SESSION['almacen']==1){

?>

            <div class="content-wrapper">
                <!-- Breadcrumb Section -->
                <section class="content-header">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-sm-12">
                                <nav aria-label="breadcrumb">
                                    <ol class="breadcrumb">
                                        <li class="breadcrumb-item">
                                            <a href="#"><i class="fas fa-home"></i> Panel</a>
                                        </li>
                                        <li class="breadcrumb-item active" aria-current="page">
                                            <a href="list.php">
                                                <i class="module-icon"></i> Inventario Producto Malogrado
                                            </a>
                                        </li>
                                    </ol>
                                </nav>
                            </div>
                          
                            <div class="col-sm-12" style="margin-top: 20px;">
                                <div class="card  ">
                                    <div class="card-header bg-primary">
                                        <h3 class="card-title">
                                            <i class="fas fa-search"></i>
                                            Listado de Inventario Productos Malogrados
                                        </h3>
                                    </div>
                                    <div class="card-body" style="display: block;">
                                        <div class="container-fluid">
                                            <a target="_blank" href="excelmalogrado.php"<button class="btn btn-success btn-flat" >EXCEL   <i class="fa fa-table" aria-hidden="true"> </button></i></a>
                                            <br>
                                            <br>
                                            <table style="width:100%;" class="table table-bordered" id="producto">
                                                <thead>
                                                <tr>
                                                <th width="20%">Nombre Producto</th>
                                                <th>Cod Madre</th>
                                                <th>Codigo</th>
                                                <th>Estado Material</th>
                                                <th>Categoria</th>        
                                                <th>Unidad Medida</th>         
                                                <th>Precio Venta</th>                 
                                                <th>Cantidad Malogrado</th>
                                                <th>Verificar Garantia</th>
                                                </tr>
                                                </thead>
                                                <?php 
               
                                $query = "SELECT d.idarticulo,a.nombre FROM inventario_retorno d join articulo a on d.idarticulo=a.idarticulo  WHERE  d.estadomaterial=4 GROUP BY d.idarticulo ORDER BY a.idcategoria desc";
                                $adicional = mysqli_query($conexion,$query);
                                while ($row = mysqli_fetch_array($adicional)){
         
                             $nombre=$row['nombre'];
                             $idarti=$row['idarticulo'];
                        
                           ?>
         
                          <tr style="font-size:14px; text-align:center; background-color:#CCCCCC"> 
                          <th><?php echo "$nombre"; ?></th>
                          <th></th>
                          <th></th>
                          <th></th>
                          <th></th>
                          <th></th>
                          <th></th>
                          <th></th>
                          <th></th>
                         
                          
          
                      <?php
                      $sumacant=0.00;
                      $estado=0;
                    $query = "SELECT c.codigo as madre,d.codigo,d.estadomaterial,d.cantidad,a.unidad_medida,d.precio_venta,d.idarticulo,a.nombre,c.nombre as categoria,g.estado_garantia 
                              FROM inventario_retorno d 
                              JOIN articulo a ON d.idarticulo=a.idarticulo 
                              JOIN categoria c ON a.idcategoria=c.idcategoria 
                              LEFT JOIN garantia g ON g.codigo=d.codigo
                              WHERE d.idarticulo='$idarti' and d.estadomaterial=4 ORDER BY d.idinventario_retorno desc";
                           $adicional3 = mysqli_query($conexion,$query);
                        $cont=0;
                        while ($row5 = mysqli_fetch_array($adicional3)){
         
                         if($row5['estadomaterial']==1){
                           $row5['estadomaterial']="Nuevo";
                         }else if($row5['estadomaterial']==2){
                           $row5['estadomaterial']="Usado";
                         }else if($row5['estadomaterial']==3){
                           $row5['estadomaterial']="Reparado";
                         }else if($row5['estadomaterial']==4){
                           $row5['estadomaterial']="Malogrado";
                         }

                         if($row5['estado_garantia'] == 1){
                            $row5['estado_garantia']='<button class="btn btn-success btn-xs btn-flat">Proceso Garantia</button>';
                         }else if($row5['estado_garantia']==2){
                            $row5['estado_garantia']='<button class="btn btn-warning btn-xs btn-flat">Enviado para Garantia</button>';
                          }else if($row5['estado_garantia']==3){
                            $row5['estado_garantia']='<button class="btn btn-primary btn-xs btn-flat">Procedio Garantia</button>';
                          }else if($row5['estado_garantia']==4){
                            $row5['estado_garantia']='<button class="btn btn-danger btn-xs btn-flat">No procedio Garantia</button>';
                          }
                           
                       $ma =$row5['madre']; 
                       $co = $row5['codigo'];
                       $es = $row5['estadomaterial'];
                       $st = $row5['cantidad'];
                       $uni = $row5['unidad_medida'];
                       $prev = $row5['precio_venta'];
                       $ca = $row5['categoria'];     
                       $sumacant=$sumacant+$st;
                       $estado_garantia=$row5['estado_garantia'];

                       if($row5['estado_garantia'] == null ){
                        $estado="<a target='_blank' href='../garantia/create.php?numero_mac=$co'><button class='btn btn-warning btn-flat btn-xs'><i class='fa fa-eye'></i></button> </a></button>";
                       }else{
                          $estado=$estado_garantia;
                       }


         
                    ?>
                    <tr style="font-size:14px; text-align:center"> 
                    <th></th>
                    <th><?php echo "$ma"; ?> </th>
                    <th><?php echo "$co"; ?> </th>
                    <th><?php echo "$es"; ?></th>
                    <th><?php echo "$ca"; ?></th>           
                    <th><?php echo "$uni"; ?></th>
                    <th><?php echo number_format($prev,2); ?></th>
                    <th><?php echo "$st"; ?></th>
                    <th><?php echo " $estado"; ?></th>
                    </tr>
             <?php
                $cont++;
                 }
                   
                ?>
                <tr style="font-size:16px; text-align:center;background-color:#dc3545"> 
                <th colspan="6"></th>
                <th style="font-size:16px; text-align:right; color:#FFFFFF">TOTAL STOCK:</th>
               <th style="font-size:16px;  color:#FFFFFF"><?php echo $sumacant; ?></th>
               <th style="font-size:16px;  color:#FFFFFF"></th>
               </tr>
         <?php 
         
         
         }
         ?>
                      
                                                <tbody>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </section>                
            </div>

           
               
<?php
}
require '../footer.php';
?>

<?php 
}
ob_end_flush();
?>