<?php
session_start();
include '../../script/conexion.php';
require '../general/validacion.php';

$referencia = $_GET['referencia'];
$talla = $_GET['talla'];

$sqlpro = "SELECT * FROM producto WHERE referencia='$referencia'";
$querypro = $conn->query($sqlpro);
$rowpro = $querypro->fetch_assoc();
?> 
<!doctype html>
<html lang="en" class="light-theme">
    <head>
        <?php include '../general/libreria-css.php'; ?>



    </head>
    <body>
        <!--start wrapper-->
        <div class="wrapper">
            <?php echo include '../general/menu.php'; ?>

            <!-- start page content wrapper-->
            <div class="page-content-wrapper">
                <!-- start page content-->
                <div class="page-content">


                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item">
                                <a href="#"> 
                                    <ion-icon name="home-outline"></ion-icon>
                                </a>
                            </li>
                            <li class="breadcrumb-item">
                                <a href="kardex.php?referencia=<?php echo $referencia; ?>"> 
                                    Kardex             
                                </a>
                            </li>
                            <li class="breadcrumb-item active" aria-current="page">Historial de venta o procesos</li>
                        </ol>
                    </nav>

                    <div class="card card-table mb-3">
                        <div class="card-header">
                            <h4 ><span class="text-primary">Historial:</span> <strong class="text-titulo"><?php echo $referencia; ?></strong></h4>
                        </div>
                    </div>


                    <div class="card">
                        <div class="card-body">

                            <div class="row mb-2"> 
                                <div class="col-12 col-sm-4">

                                    <table class="table text-sm table-sm" >
                                        <thead>
                                            <tr>
                                                <th class=" p-2">Habilitado</th>
                                                <th class=" p-2"> 
                                                    <?php
                                                    if ($rowpro['habilitado'] == 'Si') {
                                                        ?>
                                                        <span class="badge alert-success"><?php echo $rowpro['habilitado']; ?></span>
                                                        <?php
                                                    } else {
                                                        ?>
                                                        <span class="badge alert-danger"><?php echo $rowpro['habilitado']; ?></span>

                                                        <?php
                                                    }
                                                    ?>
                                                </th>
                                            </tr>
                                            <tr class="bg-light-dark">
                                                <th class=" p-2">Segmento</th>
                                                <td class=""><b><?php echo $rowpro['referencia_segmento']; ?></b></td>
                                            </tr> 
                                            <tr>
                                                <th class=" p-2">Codigo</th>
                                                <td class=""><b><?php echo $rowpro['codigo']; ?></b></td>
                                            </tr> 
                                            <tr>
                                                <th class=" p-2">Color 1</th>
                                                <td class=""><?php echo $rowpro['color']; ?>

                                                    <?php
                                                    $sql5 = "SELECT * FROM color WHERE color='" . $rowpro['color'] . "'";
                                                    $query5 = $conn->query($sql5);
                                                    $row5 = $query5->fetch_assoc();
                                                    ?>
                                                    <i class="fas fa-circle fa-2x float-end" style="color: <?php echo $row5['rgb']; ?>"></i>
                                                </td>
                                            </tr> 
                                            <tr>
                                                <th class=" p-2">Color 2</th>
                                                <td class=""><?php echo $rowpro['color2']; ?>

                                                    <?php
                                                    $sql51 = "SELECT * FROM color WHERE color='" . $rowpro['color2'] . "'";
                                                    $query51 = $conn->query($sql51);
                                                    $row51 = $query51->fetch_assoc();
                                                    ?>
                                                    <i class="fas fa-circle fa-2x float-end" style="color: <?php echo $row51['rgb']; ?>"></i>
                                                </td>
                                            </tr>
                                            <tr>
                                                <th class=" p-2">Categoria</th>
                                                <td class=""><?php echo $rowpro['categoria']; ?></td>
                                            </tr>
                                            <tr>
                                                <th class=" p-2">Clase</th>
                                                <td class=""><?php echo $rowpro['clase']; ?></td>
                                            </tr>
                                            <tr>
                                                <th class=" p-2">Peso</th>
                                                <td class=""><?php echo $rowpro['peso']; ?>g</td>
                                            </tr>
                                            <tr>
                                                <th class=" p-2">Precio de venta</th>
                                                <td class=""> <b class="text-secondary">$</b> <?php echo number_format($rowpro['precio_venta']); ?></td>
                                            </tr>
                                        </thead>
                                    </table>
                                </div>

                                <div class="col-6 col-sm-8">
                                    <div class="card mb-5">

                                        <h5><b>TALLA <?php echo $talla; ?></b></h5>
                                        <hr>
                                        <style>
                                            #hover_enlace:hover{
                                                color: purple;
                                                border-radius: 10px;
                                                box-shadow: 0px 0px 10px 0px purple;
                                            }

                                        </style>
                                        <div class="table-responsive">
                                            <table class="table table-bordered table-striped table-hover " id="example2"    >
                                                <thead>
                                                    <tr class="bg-tianzi text-dark">
                                                        <th class="text-center p-1" >FECHA</th>
                                                        <th class="text-center p-1" >CONSECUTIVO</th>
                                                        <th class="text-center p-1" >TERCERO</th>
                                                        <th class="text-center p-1" >MOVIMIENTO</th>
                                                        <th class="text-center p-1" >CANTIDAD</th> 
                                                        <th class="text-center p-1" >VER</th>
                                                    </tr>
                                                </thead>
                                                <tbody>


                                                    <?php
                                                    $sql2 = "SELECT * FROM alm_venta_detalle WHERE referencia_producto='$referencia' AND talla='$talla'";
                                                    $query2 = $conn->query($sql2);
                                                    if ($query2->num_rows > 0) {
                                                        while ($row2 = $query2->fetch_assoc()) {

                                                            $sql3 = "SELECT * FROM alm_venta WHERE id='" . $row2['id_venta'] . "'";
                                                            $query3 = $conn->query($sql3);
                                                            $row3 = $query3->fetch_assoc()
                                                            ?>
                                                            <tr>
                                                                <td class="text-center">
                                                                    <b><?php echo $row3['fecha']; ?></b>
                                                                </td>
                                                                <td class="text-center">
                                                                    <b class="text-info"><?php echo $row3['numero_venta']; ?></b>
                                                                </td>
                                                                <td class=""><?php echo $row3['nombre_tercero']; ?></td>
                                                                <td class="text-center">Ventas</td>
                                                                <td class="text-center"><i class="fas fa-minus-circle text-secondary float-start"></i><?php echo $row2['cantidad']; ?></td>
                                                                <td class="text-center">
                                                                    <a href="../venta/venta-detalle.php?id_venta=<?php echo $row3['id']; ?>" target="_blank">
                                                                        Ver
                                                                    </a>
                                                                </td>
                                                            </tr>
                                                            <?php
                                                        }
                                                    }
                                                    ?>


                                                    <?php
                                                    $sql = "SELECT  d.fecha as fecha, d.numero_despacho, d.nombre_tercero, d.id  as id_p 
                                                                FROM despacho d, despacho_detalle dd  
                                                                WHERE d.id=dd.id_despacho AND 
                                                                dd.estado='Recibido' AND dd.referencia_producto='$referencia' AND 
                                                                dd.talla='$talla' AND d.nombre_tercero='INNOVASUELAS ALMACEN'";
                                                    $query = $conn->query($sql);
                                                    if ($query->num_rows > 0) {
                                                        while ($row = $query->fetch_assoc()) {

                                                            $sql1 = "SELECT SUM(cantidad) as cantidad_despacho FROM despacho_detalle WHERE id_despacho='" . $row['id_p'] . "'"
                                                                    . " AND referencia_producto='$referencia' AND talla='$talla'";
                                                            $query1 = $conn->query($sql1);
                                                            $row1 = $query1->fetch_assoc()
                                                            ?>
                                                            <tr>
                                                                <td class="text-center">
                                                                    <b><?php echo $row['fecha']; ?></b>
                                                                </td>
                                                                <td class="text-center">
                                                                    <b class="text-info"><?php echo $row['numero_despacho']; ?></b>
                                                                </td>
                                                                <td class=""><?php echo $row['nombre_tercero']; ?></td>
                                                                <td class="text-center">Fabrica</td>
                                                                <td class="text-center"><i class="fas fa-plus-circle text-success float-start"></i> <?php echo $row1['cantidad_despacho']; ?></td>
                                                                <td class="text-center">
                                                                    <a href="../empresa/despacho-detalle.php?id_despacho=<?php echo $row['id_p']; ?>" target="_blank">
                                                                        Ver
                                                                    </a>
                                                                </td>
                                                            </tr>
                                                            <?php
                                                        }
                                                    }
                                                    ?>



                                                    <?php
                                                    $sql4 = "SELECT * FROM alm_novedad WHERE referencia_producto='$referencia' AND talla='$talla' AND tipo_novedad='Novedad'";
                                                    $query4 = $conn->query($sql4);
                                                    if ($query4->num_rows > 0) {
                                                        while ($row4 = $query4->fetch_assoc()) {
                                                            ?>
                                                            <tr>
                                                                <td class="text-center">
                                                                    <b><?php echo $row4['fecha']; ?></b>
                                                                </td>
                                                                <td class=""><b><?php echo $row4['observacion']; ?></b></td>

                                                                <td class="">MOVIMIENTO</td>
                                                                <td class="text-center">

                                                                    <?php
                                                                    if ($row4['movimiento'] == 'Entrada') {
                                                                        ?>
                                                                        Entrada

                                                                        <?php
                                                                    } else {
                                                                        ?>
                                                                        Salida

                                                                        <?php
                                                                    }
                                                                    ?>

                                                                </td>
                                                                <td class="text-center">
                                                                    <?php
                                                                    if ($row4['movimiento'] == 'Entrada') {
                                                                        ?>
                                                                        <i class="fas fa-plus-circle text-success float-start"></i>

                                                                        <?php
                                                                    } else {
                                                                        ?>
                                                                        <i class="fas fa-minus-circle text-secondary float-start"></i>

                                                                        <?php
                                                                    }
                                                                    ?>
                                                                    <?php echo $row4['cantidad']; ?>
                                                                </td>
                                                                <td class="text-center">
                                                                    <a href="movimiento.php?referencia=<?php echo $referencia; ?>" target="_blank">
                                                                        Ver
                                                                    </a>
                                                                </td>
                                                            </tr>
                                                            <?php
                                                        }
                                                    }
                                                    ?>


                                                    <?php
                                                    $sql6 = "SELECT * FROM alm_novedad WHERE referencia_producto='$referencia' AND talla='$talla' AND tipo_novedad='Devolucion'";
                                                    $query6 = $conn->query($sql6);
                                                    if ($query6->num_rows > 0) {
                                                        while ($row6 = $query6->fetch_assoc()) {
                                                            ?>
                                                            <tr>
                                                                <td class="text-center">
                                                                    <b><?php echo $row6['fecha']; ?></b>
                                                                </td>
                                                                <td class=""><b>---</b></td>

                                                                <td class=""><?php echo $row6['nombre_tercero']; ?></td>
                                                                <td class="text-center">

                                                                    Devolucion
                                                                </td>
                                                                <td class="text-center">
                                                                    <?php
                                                                    if ($row6['movimiento'] == 'Entrada') {
                                                                        ?>
                                                                        <i class="fas fa-plus-circle text-success float-start"></i>

                                                                        <?php
                                                                    } else {
                                                                        ?>
                                                                        <i class="fas fa-minus-circle text-secondary float-start"></i>

                                                                        <?php
                                                                    }
                                                                    ?>
                                                                    <?php echo $row6['cantidad']; ?>
                                                                </td>
                                                                <td class="text-center">
                                                                    <a href="novedad.php?referencia=<?php echo $referencia; ?>" target="_blank">
                                                                        Ver
                                                                    </a>
                                                                </td>
                                                            </tr>
                                                            <?php
                                                        }
                                                    }
                                                    ?>



                                                    <?php
                                                    $sql7 = "SELECT * FROM alm_compra_detalle WHERE referencia_producto='$referencia' AND talla='$talla'";
                                                    $query7 = $conn->query($sql7);
                                                    if ($query7->num_rows > 0) {
                                                        while ($row7 = $query7->fetch_assoc()) {

                                                            $sql8 = "SELECT * FROM alm_compra WHERE id='" . $row7['id_compra'] . "'";
                                                            $query8 = $conn->query($sql8);
                                                            $row8 = $query8->fetch_assoc()
                                                            ?>
                                                            <tr>
                                                                <td class="text-center">
                                                                    <b><?php echo $row8['fecha']; ?></b>
                                                                </td>
                                                                <td class="text-center">
                                                                    <b class="text-info"><?php echo $row8['numero_compra']; ?></b>
                                                                </td>
                                                                <td class=""><?php echo $row8['nombre_tercero']; ?></td>
                                                                <td class="text-center">Compras</td>
                                                                <td class="text-center"><i class="fas fa-minus-circle text-secondary float-start"></i><?php echo $row7['cantidad']; ?></td>
                                                                <td class="text-center">
                                                                    <a href="../compra/compra-detalle.php?id_compra=<?php echo $row8['id']; ?>" target="_blank">
                                                                        Ver
                                                                    </a>
                                                                </td>
                                                            </tr>
                                                            <?php
                                                        }
                                                    }
                                                    ?>


                                                </tbody>
                                            </table>
                                        </div>







                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>



                </div>

                <?php echo include '../general/footer.php'; ?>
            </div>
        </div>
        <!--end wrapper-->
        <?php echo include '../general/libreria-js.php'; ?>

        <script src="../../assets/plugins/datatable/js/jquery.dataTables.min.js"></script>
        <script src="../../assets/plugins/datatable/js/dataTables.bootstrap5.min.js"></script>

        <script>
            //Tabla producto
            $(document).ready(function () {
                var table = $('#example2').DataTable({
                    lengthChange: false,
                    buttons: ['excel', 'pdf', 'print'],
                    "order": [[0, 'desc']],
                });

                table.buttons().container()
                        .appendTo('#example2_wrapper .col-md-6:eq(0)');
            });

        </script>

    </body>
</html>