<?php
session_start();
include '../conexion.php';
require './Classes/mpdf/vendor/autoload.php';


$reporte = $_GET['reporte'] ?? null;
$fechaini = $_GET['fechaini'] ?? null;
$fechafin = $_GET['fechafin'] ?? null;
$usados = $_GET['usados'] ?? '99';
$detallado = $_GET['detallado'] ?? '99';
$datox= $_GET['datox'] ?? null;


$valores=null;

$mpdf = new \Mpdf\Mpdf([
    'mode' => 'utf-8',
    'format' => 'A4',
    'shrink_tables_to_fit' => 0 // Deshabilitar el ajuste automático de tamaño de las tablas
]);
$mpdf->SetDisplayMode('fullpage');

$mpdf->WriteHTML('<style>
body {
    font-family: Arial, sans-serif; /* Cambia el tipo de letra a Arial */
    font-size: 10pt; /* Cambia el tamaño de letra a 12 puntos */
}
.no-break {
    page-break-inside: avoid; /* Evita que la tabla se divida entre páginas */
    /*width: 100%; /* Asegura que la tabla use el ancho completo de la página */
}
thead {
    display: table-header-group; /* Asegura que el encabezado se repita en la nueva página si la tabla se divide */
}
th{
    font-family: Arial, sans-serif; /* Cambia el tipo de letra a Arial */
    font-size: 8pt; /* Cambia el tamaño de letra a 12 puntos */
    background:#C5CACF;
}
td{
    font-family: Arial, sans-serif; /* Cambia el tipo de letra a Arial */
    font-size: 8pt; /* Cambia el tamaño de letra a 12 puntos */
    text-align:right;
}
td#medida{
    text-align:left;
}
th#derecha{
    text-align:right;
}
tfoot {
    display: table-footer-group; /* Asegura que el pie de página se repita en la nueva página si la tabla se divide */
}
</style>');
$mpdf -> SetTitle('ENTREGA DE UNIFORME POR '.strtoupper($reporte).'');
    
$mpdf -> WriteHTML('<body>');
if($reporte=='lugar'){
    $reportex='lugar de trabajo';
}else{
    $reportex=$reporte;
}

if ($reporte === "empleado") {
    $query = "SELECT * FROM uni_empleados where num_emp=?";
    $stmt = $connection->prepare($query);
    $stmt->bind_param("s", $datox);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        while ($data = $result->fetch_assoc()) {
        $datotitulo=$data['num_emp']."-".$data['nombre'];
        }
    }
                    
}else if ($reporte === "supervisor") {
    $datotitulo="Supervisor: ".$datox;
}else if ($reporte === "lugar"){
    $datotitulo="Lugar de Trabajo: ".$datox;
}else if ($reporte === "cliente"){
    $query = "SELECT * FROM uni_clientes where iduniCliente=?";
    $stmt = $connection->prepare($query);
    $stmt->bind_param("s", $datox);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        while ($data = $result->fetch_assoc()) {
        $datotitulo="Cliente: ".$data['cliente'];
        }
    }
    
}else if ($reporte === "sucursal"){
    $datotitulo="Sucursal: ".$datox;
}else if ($reporte === "articulo"){
    $datotitulo="Articulo: ".$datox;
}


$mpdf -> WriteHTML('<div style="text-align: center;"><strong>ENTREGA DE UNIFORMES POR '.strtoupper($reportex).'</strong></div>');
$mpdf -> WriteHTML('<div style="text-align: center;">'.strtoupper($datotitulo).'</div>');
if($fechaini==$fechafin){
    $mpdf -> WriteHTML('<div style="text-align: center;">del dia: '.date('d/m/Y',strtotime($fechaini)).'</div>');
}else{
    $mpdf -> WriteHTML('<div style="text-align: center;">del dia: '.date('d/m/Y',strtotime($fechaini)).' al :'.date('d/m/Y',strtotime($fechafin)).'</div>');
}


if ($reporte === "empleado") {
    $mpdf -> WriteHTML('
    <div style="text-align:center; margin: 0 auto; padding: 20px;">
        <table border="1" style="margin: 0 auto; width: 100%; font-size: 8pt; border-collapse: collapse;">
            <thead>
                <tr>

                                <th style="width:15%;">Fecha</th>
                                <th style="width:35%;">Descripcion</th>
                                <th style="width:10%;">Cantidad</th>
                                <th >Devuelto</th>
                                <th>Fecha Devolución</th>
                            </tr>
    
        <tbody>
                        ');

                        // Consulta principal
                        //$query = "SELECT * FROM datos_traspasos WHERE medida like ? OR tipoUniforme LIKE ? ORDER BY orden ASC LIMIT ?, ?";
                        $query = "SELECT * FROM datos_entregas where num_emp=? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ? ORDER BY fechaMov DESC,idmovimientosUni DESC";
                        $stmt = $connection->prepare($query);
                        $stmt->bind_param("sss", $datox,$fechaini,$fechafin);
                        $stmt->execute();
                        $result = $stmt->get_result();

                        if ($result->num_rows > 0) {
                            $sum=0;
                            while ($data = $result->fetch_assoc()) {
                                $dev=($data['dev'] == 1) ? "SI" : "NO";
                                $mpdf->WriteHTML( "<tr>

                                        <td style='text-align:left;padding-left:20px'>{$data['fechaMov']}</td>
                                        <td style='text-align:left;padding-left:20px'>" . htmlspecialchars($data['descrip'], ENT_QUOTES, 'UTF-8') . "</td>
                                        <td style='text-align:left;padding-left:20px'>{$data['cant']}</td>
                                        <td style='text-align:left;padding-left:20px'>" . $dev. "</td>
                                        <td style='text-align:left;padding-left:20px'>{$data['fechaDev']}</td>   
                                        
                                    </tr>");
                                    $sum+=$data['cant'];
                            }
                            $mpdf->WriteHTML( "<tr>

                                        <th style='text-align:right;padding-right:20px' colspan='2'>Total</th>
                                        <th style='text-align:left;padding-left:20px'>{$sum}</th>   
                                        <th style='text-align:left;padding-left:20px'colspan='2'></th> 
                                        
                                        
                                    </tr>");
                        }else{
                            $mpdf->WriteHTML( "<tr>
                                        <td colspan='6' style='text-align:center;padding-left:20px;color:red'>Sin datos que mostrar</td>
                                        </tr>");
                        }
                        $mpdf->WriteHTML('


                        </tbody>
                                </table>
                            </div>
    
                ');

   
        //$response['success'] = true;
        //$response['valores'] = $valores;

} else if ($reporte === "supervisor") {

    $mpdf -> WriteHTML('
    
                    <div style="text-align:center; margin: 0 auto; padding: 20px;">
        <table border="1" style="margin: 0 auto; width: 80%; font-size: 8pt; border-collapse: collapse;">
            <thead>
                <tr>
                                
                                <th style="width:45%;">Descripcion</th>
                                <th style="width:10%;">Cantidad</th>
                            </tr>
                        </thead>
                        <tbody>
                        ');



                        // Consulta principal
                        //$query = "SELECT * FROM datos_traspasos WHERE medida like ? OR tipoUniforme LIKE ? ORDER BY orden ASC LIMIT ?, ?";
                        $query = "SELECT * FROM datos_entregas where supervisor=? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ? ORDER BY descrip ASC,idmovimientosUni DESC";
                        $stmt = $connection->prepare($query);
                        $stmt->bind_param("sss", $datox,$fechaini,$fechafin);
                        $stmt->execute();
                        $result = $stmt->get_result();

                        if ($result->num_rows > 0) {
                            $sum=0;
                            while ($data = $result->fetch_assoc()) {
                                $dev=($data['dev'] == 1) ? "SI" : "NO";
                                $mpdf->WriteHTML( "<tr>
                                
                                        <td style='text-align:left;padding-left:20px'>" . htmlspecialchars($data['descrip'], ENT_QUOTES, 'UTF-8') . "</td>
                                        <td style='text-align:left;padding-left:20px'>{$data['cant']}</td>
                                        
                                    </tr>");
                                    $sum+=$data['cant'];
                            }
                            $mpdf->WriteHTML( "<tr>
                                        <th style='text-align:right;padding-right:20px'>TOTAL:</th>
                                        <th style='text-align:left;padding-left:20px'>{$sum}</th>
                                        
                                    </tr>");
                        }else{
                            $mpdf->WriteHTML( "<tr>
                                        <td colspan='6' style='text-align:center;padding-left:20px;color:red'>Sin datos que mostrar</td>
                                        </tr>");
                        }
                        $mpdf->WriteHTML('


                        </tbody>
                    </table>
    

                ');

   
        //$response['success'] = true;
        //$response['valores'] = $valores;
} else if ($reporte === "lugar") {
    $mpdf -> WriteHTML('
    <div style="text-align:center; margin: 0 auto; padding: 20px;">
        <table border="1" style="margin: 0 auto; width: 80%; font-size: 8pt; border-collapse: collapse;">
            <thead>
                <tr>

                                <th style="width:35%;">Descripcion</th>
                                <th style="width:10%;">Cantidad</th>
                        

                            </tr>
                        </thead>
                        <tbody>
                        ');




                        // Consulta principal
                        //$query = "SELECT * FROM datos_traspasos WHERE medida like ? OR tipoUniforme LIKE ? ORDER BY orden ASC LIMIT ?, ?";
                        $query = "SELECT * FROM datos_entregas where dato3=? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ? ORDER BY fechaMov DESC,idmovimientosUni DESC";
                        $stmt = $connection->prepare($query);
                        $stmt->bind_param("sss", $datox,$fechaini,$fechafin);
                        $stmt->execute();
                        $result = $stmt->get_result();

                        if ($result->num_rows > 0) {
                            $sum=0;
                            while ($data = $result->fetch_assoc()) {
                                $dev=($data['dev'] == 1) ? "SI" : "NO";
                                $mpdf->WriteHTML( "<tr>
                                        <td style='text-align:left;padding-left:20px'>" . htmlspecialchars($data['descrip'], ENT_QUOTES, 'UTF-8') . "</td>
                                        <td style='text-align:left;padding-left:20px'>{$data['cant']}</td>
                                        </td>
                                    </tr>");
                                    $sum+=$data['cant'];
                            }
                            $mpdf->WriteHTML( "<tr>
                            <tH style='text-align:right;padding-right:20px'>TOTALES</tH>
                            <tH style='text-align:left;padding-left:20px'>{$sum}</tH>
                            </td>
                        </tr>");
                        }else{
                            $mpdf->WriteHTML( "<tr>
                                        <td colspan='6' style='text-align:center;padding-left:20px;color:red'>Sin datos que mostrar</td>
                                        </tr>");
                        }
                        $mpdf->WriteHTML('


                        </tbody>
                    </table>
                </div>
           
                ');

   
        //$response['success'] = true;
        //$response['valores'] = $valores;

} else if ($reporte === "cliente") {
            $query = "SELECT * FROM uni_clientes WHERE iduniCliente = ?";
            $stmt = $connection->prepare($query);

            // Vincula el parámetro correctamente
            $stmt->bind_param("s", $datox);

            // Ejecuta la consulta
            $stmt->execute();

            // Obtiene los resultados
            $result = $stmt->get_result();
            $datoxx=null;
            // Verifica si hay filas y procesa los datos
            if ($result->num_rows > 0) {
                while ($data = $result->fetch_assoc()) {
                    $datoxx = $data['cliente'];
                }
            }
    
    

                        if($detallado=='99'){
                            $mpdf->WriteHTML('
    <div style="text-align:center; margin: 0 auto; padding: 20px;">
        <table border="1" style="margin: 0 auto; width: 80%; font-size: 8pt; border-collapse: collapse;">
            <thead>
                <tr>
                    <th style="width: 70%; text-align: left; padding: 5px;">Descripción</th>
                    <th style="width: 15%; text-align: left; padding: 5px;">Cantidad</th>
                </tr>
            </thead>
            <tbody>
');
                            // Consulta principal
                            //$query = "SELECT * FROM datos_traspasos WHERE medida like ? OR tipoUniforme LIKE ? ORDER BY orden ASC LIMIT ?, ?";
                            $query = "SELECT idmovimientosUni,fechaMov,sum(cant) as cantx ,descrip,dev,fechaDev FROM datos_entregas where cliente=? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ? group by descrip ORDER BY fechaMov DESC,idmovimientosUni DESC";
                            $stmt = $connection->prepare($query);
                            $stmt->bind_param("sss", $datoxx,$fechaini,$fechafin);
                            $stmt->execute();
                            $result = $stmt->get_result();

                            if ($result->num_rows > 0) {
                                $sum=0;
                                while ($data = $result->fetch_assoc()) {
                                    $dev=($data['dev'] == 1) ? "SI" : "NO";
                                    $mpdf->WriteHTML( "<tr>
                                            <td style='text-align:left;width:300px;'>" . htmlspecialchars($data['descrip'], ENT_QUOTES, 'UTF-8') . "</td>
                                            <td style='text-align:left;padding-left:20px'>{$data['cantx']}</td>                                            
                                        </tr>");
                                        $sum+=$data['cantx'];
                                }
                                $mpdf->WriteHTML( "<tr>
                                <th style='text-align:right;padding-right:20px'>TOTALES</th>
                                <th style='text-align:left;padding-left:20px'>{$sum}</th>                                            
                            </tr>");
                            }else{
                                $mpdf->WriteHTML( "<tr>
                                            <td colspan='6' style='text-align:center;padding-left:20px;color:red'>Sin datos que mostrar</td>
                                            </tr>");
                            }
                            $mpdf->WriteHTML('

    
                            </tbody>
                            </table>

                            </div>
                            ');
                        }
                        else{
                            $mpdf->WriteHTML('
    <div style="text-align:center; margin: 0 auto; padding: 20px;">
        <table border="1" style="margin: 0 auto; width: 100%; font-size: 8pt; border-collapse: collapse;">
            <thead>
                <tr>
                             <th >Descripcion</th>
                             <th style="width:10%">Cantidad</th>                                
                             <th>Empleado</th>                                
                                
                            </tr>
                        </thead>

                        <tbody>
                        ');

                                // Consulta principal
                                //$query = "SELECT * FROM datos_traspasos WHERE medida like ? OR tipoUniforme LIKE ? ORDER BY orden ASC LIMIT ?, ?";
                                $query = "SELECT * FROM datos_entregas where cliente=? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ? ORDER BY descrip ASC";
                                $stmt = $connection->prepare($query);
                                $stmt->bind_param("sss", $datoxx,$fechaini,$fechafin);
                                $stmt->execute();
                                $result = $stmt->get_result();

                                if ($result->num_rows > 0) {
                                    $sum=0;
                                    while ($data = $result->fetch_assoc()) {
                                        $dev=($data['dev'] == 1) ? "SI" : "NO";
                                        $mpdf->WriteHTML( "<tr>
                                                <td style='text-align:left;padding-left:20px'>" . htmlspecialchars($data['descrip'], ENT_QUOTES, 'UTF-8') . "</td>
                                                <td style='text-align:left;padding-left:20px'>{$data['cant']}</td>
                                                <td style='text-align:left;padding-left:20px'>{$data['dato1']}</td>
                                                </td>
                                            </tr>");
                                            $sum+=$data['cant'];
                                    }
                                    $mpdf->WriteHTML( "<tr>
                                                <th style='text-align:right;padding-right:20px;'>TOTALES</th>
                                                <th style='text-align:left;padding-left:20px'>{$sum}</th>
                                                <th style='text-align:left;padding-left:20px'></th>
                                                </td>
                                            </tr>");
                                }else{
                                    $mpdf->WriteHTML( "<tr>
                                                <td colspan='6' style='text-align:center;padding-left:20px;color:red'>Sin datos que mostrar</td>
                                                </tr>");
                                }
                                $mpdf->WriteHTML('

        
                                </tbody>
                            </table>
                        </div>
                        </div>
                        
                        ');

                        }
                        

   
        //$response['success'] = true;
        //$response['valores'] = $valores;


} else if ($reporte === "sucursal") {

                        if($detallado=='99'){
                            $mpdf -> WriteHTML('
                            <div style="text-align:center; margin: 0 auto; padding: 20px;">
        <table border="1" style="margin: 0 auto; width: 80%; font-size: 8pt; border-collapse: collapse;">
            <thead>
                <tr>

                                <th style="width:80%;">Descripcion</th>
                                <th>Cantidad</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                        ');

                            // Consulta principal
                            //$query = "SELECT * FROM datos_traspasos WHERE medida like ? OR tipoUniforme LIKE ? ORDER BY orden ASC LIMIT ?, ?";
                            $query = "SELECT idmovimientosUni,fechaMov,sum(cant) as cantx ,descrip,dev,fechaDev FROM datos_entregas where dato2 like ? and uni_tipomov_idtipoMovUni='4' and fechaMov between ? and ? group by descrip ORDER BY descrip ASC ";
                            $likeBusq = "%{$datox}%";
                            $stmt = $connection->prepare($query);
                            $stmt->bind_param("sss", $likeBusq,$fechaini,$fechafin);
                            $stmt->execute();
                            $result = $stmt->get_result();

                            if ($result->num_rows > 0) {
                                while ($data = $result->fetch_assoc()) {
                                    $dev=($data['dev'] == 1) ? "SI" : "NO";
                                    $mpdf->WriteHTML( "<tr>
                                            <td style='text-align:left;padding-left:20px'>" . htmlspecialchars($data['descrip'], ENT_QUOTES, 'UTF-8') . "</td>
                                            <td style='text-align:right;padding-right:20px'>{$data['cantx']}</td>                                            
                                            
                                        </tr>");
                                        $sum+=$data['cantx'];
                                }
                                $mpdf->WriteHTML("
<tr>
    <th style='text-align:right;padding-right:20px'>TOTALES</th>
    <th style='text-align:right;padding-right:20px'>{$sum}</th>
</tr>
");

                            }else{
                                $mpdf->WriteHTML( "<tr>
                                            <td colspan='6' style='text-align:center;padding-left:20px;color:red'>Sin datos que mostrar</td>
                                            </tr>");
                            }
                            $mpdf->WriteHTML('

    
                            </tbody>
                            </table>
                            </div>
                            </div>
                            ');
                        }
                        else{
                            $mpdf -> WriteHTML('
                                <div style="text-align:center; margin: 0 auto; padding: 20px;">
        <table border="1" style="margin: 0 auto; width: 100%; font-size: 8pt; border-collapse: collapse;">
            <thead>
                <tr>


                                                <th style="width:45%;">Descripción</th>
                                                <th style="width:10%;">Cantidad</th>
                                                <th>Empleado</th>
                                            </tr>
                                        </thead>

                                            <tbody>');

                                // Consulta principal
                                $likeBusq = "%{$datox}%";
                                $query = "SELECT * FROM datos_entregas 
                                        WHERE dato2 LIKE ? AND uni_tipomov_idtipoMovUni='4' 
                                        AND fechaMov BETWEEN ? AND ?
                                        ORDER BY descrip ASC";
                                $stmt = $connection->prepare($query);
                                $stmt->bind_param("sss", $likeBusq, $fechaini, $fechafin);
                                $stmt->execute();
                                $result = $stmt->get_result();

                                if ($result->num_rows > 0) {
                                    $sum=0;
                                    while ($data = $result->fetch_assoc()) {
                                        $dev = ($data['dev'] == 1) ? "SI" : "NO";
                                        $mpdf->WriteHTML( "<tr>
                                                        <td style='text-align:left; padding-left:20px;'>"
                                                            . htmlspecialchars($data['descrip'], ENT_QUOTES, 'UTF-8') . 
                                                        "</td>
                                                        <td style='text-align:right; padding-right:20px;'>{$data['cant']}</td>
                                                        <td style='text-align:left; padding-left:20px;'>". utf8_encode($data['dato1']) . 
                                                        "</td>>
                                                    </tr>");
                                                    $sum+=$data['cant'];
                                    }
                                    $mpdf->WriteHTML( "<tr>
                                                        <th style='text-align:right; padding-right:20px;'>TOTALES</th>
                                                        <th style='text-align:right; padding-right:20px;'>{$sum}</th>
                                                        <th style='text-align:left; padding-left:20px;'></th>>
                                                    </tr>");
                                } else {
                                    $mpdf->WriteHTML( "<tr>
                                                    <td colspan='7' style='text-align:center; padding:20px; color:red;'>Sin datos que mostrar</td>
                                                </tr>");
                                }

                                $mpdf -> WriteHTML('
                                            </tbody>
                                        </table>
                                    </div>
                                </div>');

                        }
                        

   
        //$response['success'] = true;
        //$response['valores'] = $valores;
        
} else if ($reporte === "articulo") {
    $x='';
    $z='';
    if($usados=='99'){
        $x="and estado='NUEVO'";
        $mpdf -> WriteHTML('<br>');
    }else{
        $mpdf -> WriteHTML('<div style="text-align:center; margin: 0 auto; padding: 20px;">'.$datox);
    }
    

    
        $mpdf -> WriteHTML('
                                
        <table border="1" style="margin: 0 auto; width: 70%; font-size: 8pt; border-collapse: collapse;">
            <thead>
                <tr>

                            <th style="width:80%">Descripción</th>
                            <th>Cantidad</th>

                        </tr>
                    </thead>

                        <tbody>');

          

            // Consulta principal
    
                $query = "SELECT idmovimientosUni,descrip,sum(cant) as cantx FROM datos_entregas 
                WHERE uni_tipomov_idtipoMovUni='4' ".$x."
                AND fechaMov BETWEEN ? AND ?
                group by descrip
                ORDER BY descrip ASC";
                $stmt = $connection->prepare($query);
                $stmt->bind_param("ss", $fechaini, $fechafin);
    
            
                    
            
            $stmt->execute();
            $result = $stmt->get_result();

            if ($result->num_rows > 0) {
                $sum=0;
                while ($data = $result->fetch_assoc()) {
                    
                    $mpdf->WriteHTML( "<tr>
                                    <td style='text-align:left; padding-left:20px; '>"
                                        . utf8_encode($data['descrip']) . 
                                    "</td>
                                    <td style='text-align:right; padding-right:20px; '>{$data['cantx']}</td>

                                </tr>");
                                $sum+=$data['cantx'];
                }
                $mpdf->WriteHTML( "<tr>
                                    <th style='text-align:right; padding-right:20px; '>TOTALES</th>
                                    <th style='text-align:right; padding-right:20px; '>{$sum}</th>

                                </tr>");
            } else {
                $mpdf->WriteHTML( "<tr>
                                <td colspan='7' style='text-align:center; padding:20px; color:red;'>Sin datos que mostrar</td>
                            </tr>");
            }

            $mpdf -> WriteHTML('
                        </tbody>
                    </table>
                </div>
            </div>');

    
    
    
    


//$response['success'] = true;
//$response['valores'] = $valores;

} 



//echo json_encode($response);

$mpdf -> WriteHTML('');
$mpdf -> WriteHTML('</body>');
$mpdf -> Output('./archivos/NombreDeTuArchivo.pdf', 'I');
exit;

?>

