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

//$mpdf = new \Mpdf\Mpdf();
$sucursal=$_GET['sucursal'];
$fecha1=$_GET['fecha1'];
$fecha2=$_GET['fecha2'];

$anio=date('Y',strtotime($fecha1));

$consultax=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');
$sqlx='select * from datos_uniformes where activo=1 group by u_descripcion order by ordena';

$consulta=mysqli_query($connection,$sqlx);
$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>');

$sucx=null;

switch($sucursal){
    case '1': $sucx="Tijuana"; break;
    case '2': $sucx="Ensenada";break;
    case '3': $sucx="Mexicali";break;
}

$mpdf -> SetTitle('Reporte de Inventario de Uniformes '.$sucx.'');
$mpdf -> WriteHTML('<body>');
$mpdf -> WriteHTML('<div style="text-align: center;"><strong>Reporte de Inventario de Uniformes '.$sucx.'</strong></div>');
$mpdf -> WriteHTML('<div style="text-align: center;">al dia '.date('d-M-Y').' y consumos del '.date('d-M-Y',strtotime($fecha1)).' al '.date('d-M-Y',strtotime($fecha2)).'</div>');
$mpdf -> WriteHTML('<br>');

//$mpdf -> WriteHTML($sql);
//$consulta=mysqli_query($connection,$sql);
$result = mysqli_num_rows($consulta);
//$mpdf -> WriteHTML('<table>');
if ($result > 0) {
    while ($data = mysqli_fetch_array($consulta)) {
        $etc=0;
        $etn=0;
        $etu=0;
        $ett=0;
        $ctn=0;
        $ctu=0;
        $ctt=0;
        $sql=null;

        if($sucursal==1){
            $sql="SELECT  
    a.orden,
    a.medida,
    a.u_descripcion,
    a.descrip,
    a.datoxy,
    SUM(CASE WHEN a.estado = 'NUEVO' THEN a.inv1 ELSE 0 END) AS Nuevo,
    SUM(CASE WHEN a.estado = 'USADO' THEN a.inv1 ELSE 0 END) AS Usado,
    b.CantN,
    b.CantU,
    c.compra,
    a.comentario
FROM 
    datos_uniformes_1 a
LEFT JOIN (
    SELECT 
        medida,
        descrip,
        u_descripcion,
        datoxy,
        SUM(CASE WHEN estado = 'NUEVO' THEN cant ELSE 0 END) AS CantN,
        SUM(CASE WHEN estado = 'USADO' THEN cant ELSE 0 END) AS CantU
    FROM 
        entregas_tijuana_1
    WHERE 
        fechaMov BETWEEN '".$fecha1."' AND '".$fecha2."' 
        AND uni_tipomov_idtipoMovUni = 4
    GROUP BY 
        datoxy
) b ON b.datoxy = a.datoxy
LEFT JOIN (
    SELECT 
        descrip,
        sum(cant) as compra,
        sucursal
        
    FROM 
        datos_compras_anio
    WHERE 
        anio='".$anio."' and sucursal='1'
    GROUP BY 
        descrip
) c ON c.descrip = a.descrip
WHERE 
    a.activo = 1 
    AND a.u_descripcion = '".$data['u_descripcion']."'
GROUP BY 
    a.datoxy
ORDER BY 
    a.ordena, 
    a.orden ASC";
            /*$sql="SELECT  
    a.orden,
    a.medida,
    a.u_descripcion,
    a.descrip,
    a.datoxy,
    SUM(CASE WHEN a.estado = 'NUEVO' THEN a.inv1 ELSE 0 END) AS Nuevo,
    SUM(CASE WHEN a.estado = 'USADO' THEN a.inv1 ELSE 0 END) AS Usado,
    b.CantN,
    b.CantU,
    a.comentario
FROM 
    datos_uniformes_1 a
LEFT JOIN (
    SELECT 
        medida,
        descrip,
        u_descripcion,
        datoxy,
        SUM(CASE WHEN estado = 'NUEVO' THEN cant ELSE 0 END) AS CantN,
        SUM(CASE WHEN estado = 'USADO' THEN cant ELSE 0 END) AS CantU
    FROM 
        entregas_tijuana_1
    WHERE 
        fechaMov BETWEEN '".$fecha1."' AND '".$fecha2."' 
        AND uni_tipomov_idtipoMovUni = 4
    GROUP BY 
        datoxy
) b ON b.datoxy = a.datoxy
WHERE 
    a.activo = 1 
    AND a.u_descripcion = '".$data['u_descripcion']."'
GROUP BY 
    a.datoxy
ORDER BY 
    a.ordena, 
    a.orden ASC;
";*/
        }else if($sucursal==2){
            /*$sql="SELECT  
    a.orden,
    a.medida,
    a.u_descripcion,
    a.descrip,
    a.datoxy,
    SUM(CASE WHEN a.estado = 'NUEVO' THEN a.inv2 ELSE 0 END) AS Nuevo,
    SUM(CASE WHEN a.estado = 'USADO' THEN a.inv2 ELSE 0 END) AS Usado,
    b.CantN,
    b.CantU,
    a.comentario
FROM 
    datos_uniformes_1 a
LEFT JOIN (
    SELECT 
        medida,
        descrip,
        u_descripcion,
        datoxy,
        SUM(CASE WHEN estado = 'NUEVO' THEN cant ELSE 0 END) AS CantN,
        SUM(CASE WHEN estado = 'USADO' THEN cant ELSE 0 END) AS CantU
    FROM 
        entregas_ensenada_1
    WHERE 
        fechaMov BETWEEN '".$fecha1."' AND '".$fecha2."' 
        AND uni_tipomov_idtipoMovUni = 4
    GROUP BY 
        datoxy
) b ON b.datoxy = a.datoxy
WHERE 
    a.activo = 1 
    AND a.u_descripcion = '".$data['u_descripcion']."'
GROUP BY 
    a.datoxy
ORDER BY 
    a.ordena, 
    a.orden ASC;
";*/
$sql="SELECT  
    a.orden,
    a.medida,
    a.u_descripcion,
    a.descrip,
    a.datoxy,
    SUM(CASE WHEN a.estado = 'NUEVO' THEN a.inv2 ELSE 0 END) AS Nuevo,
    SUM(CASE WHEN a.estado = 'USADO' THEN a.inv2 ELSE 0 END) AS Usado,
    b.CantN,
    b.CantU,
    c.compra,
    a.comentario
FROM 
    datos_uniformes_1 a
LEFT JOIN (
    SELECT 
        medida,
        descrip,
        u_descripcion,
        datoxy,
        SUM(CASE WHEN estado = 'NUEVO' THEN cant ELSE 0 END) AS CantN,
        SUM(CASE WHEN estado = 'USADO' THEN cant ELSE 0 END) AS CantU
    FROM 
        entregas_ensenada_1
    WHERE 
        fechaMov BETWEEN '".$fecha1."' AND '".$fecha2."' 
        AND uni_tipomov_idtipoMovUni = 4
    GROUP BY 
        datoxy
) b ON b.datoxy = a.datoxy
LEFT JOIN (
    SELECT 
        descrip,
        sum(cant) as compra,
        sucursal
        
    FROM 
        datos_compras_anio
    WHERE 
        anio='".$anio."'  and sucursal='2'
    GROUP BY 
        descrip
) c ON c.descrip = a.descrip
WHERE 
    a.activo = 1 
    AND a.u_descripcion = '".$data['u_descripcion']."'
GROUP BY 
    a.datoxy
ORDER BY 
    a.ordena, 
    a.orden ASC";
        }else if($sucursal==3){
            /*$sql="SELECT  
    a.orden,
    a.medida,
    a.u_descripcion,
    a.descrip,
    a.datoxy,
    SUM(CASE WHEN a.estado = 'NUEVO' THEN a.inv3 ELSE 0 END) AS Nuevo,
    SUM(CASE WHEN a.estado = 'USADO' THEN a.inv3 ELSE 0 END) AS Usado,
    b.CantN,
    b.CantU,
    a.comentario
FROM 
    datos_uniformes_1 a
LEFT JOIN (
    SELECT 
        medida,
        descrip,
        u_descripcion,
        datoxy,
        SUM(CASE WHEN estado = 'NUEVO' THEN cant ELSE 0 END) AS CantN,
        SUM(CASE WHEN estado = 'USADO' THEN cant ELSE 0 END) AS CantU
    FROM 
        entregas_mexicali_1
    WHERE 
        fechaMov BETWEEN '".$fecha1."' AND '".$fecha2."' 
        AND uni_tipomov_idtipoMovUni = 4
    GROUP BY 
        datoxy
) b ON b.datoxy = a.datoxy
WHERE 
    a.activo = 1 
    AND a.u_descripcion = '".$data['u_descripcion']."'
GROUP BY 
    a.datoxy
ORDER BY 
    a.ordena, 
    a.orden ASC;
";*/
$sql="SELECT  
    a.orden,
    a.medida,
    a.u_descripcion,
    a.descrip,
    a.datoxy,
    SUM(CASE WHEN a.estado = 'NUEVO' THEN a.inv3 ELSE 0 END) AS Nuevo,
    SUM(CASE WHEN a.estado = 'USADO' THEN a.inv3 ELSE 0 END) AS Usado,
    b.CantN,
    b.CantU,
    c.compra,
    a.comentario
FROM 
    datos_uniformes_1 a
LEFT JOIN (
    SELECT 
        medida,
        descrip,
        u_descripcion,
        datoxy,
        SUM(CASE WHEN estado = 'NUEVO' THEN cant ELSE 0 END) AS CantN,
        SUM(CASE WHEN estado = 'USADO' THEN cant ELSE 0 END) AS CantU
    FROM 
        entregas_mexicali_1
    WHERE 
        fechaMov BETWEEN '".$fecha1."' AND '".$fecha2."' 
        AND uni_tipomov_idtipoMovUni = 4
    GROUP BY 
        datoxy
) b ON b.datoxy = a.datoxy
LEFT JOIN (
    SELECT 
        descrip,
        sum(cant) as compra,
        sucursal
        
    FROM 
        datos_compras_anio
    WHERE 
        anio='".$anio."'  and sucursal='3'
    GROUP BY 
        descrip
) c ON c.descrip = a.descrip
WHERE 
    a.activo = 1 
    AND a.u_descripcion = '".$data['u_descripcion']."'
GROUP BY 
    a.datoxy
ORDER BY 
    a.ordena, 
    a.orden ASC";
        }

        

        $consultax=mysqli_query($connection,$sql);
        $resultx = mysqli_num_rows($consultax);
        $mpdf -> WriteHTML('<table border="1" class="no-break" >');
        $mpdf -> WriteHTML('<thead><tr><th colspan="10">'.$data['u_descripcion'].' '.$data['comentario'].'</th></tr>');
        $mpdf -> WriteHTML('<tr>
                            <th rowspan="2" style="text-align:center;width:80px;">Medida</th>                    
                            <th rowspan="2" style="text-align:center;width:60px;">Compra Anual global</th>
                            <th colspan="3" style="text-align:center;">Existencia</th>
                            <th colspan="3" style="text-align:center;">Consumos</th>
                            <th rowspan="2" style="text-align:center;width:60px;">Proceso</th>
                            <th rowspan="2" style="text-align:center;width:60px;">Sugerido</th>
                        </tr>
                        </thead>
                        <tr>
                            <th style="text-align:center;width:60px;">Nuevo</th>
                            <th style="text-align:center;width:60px;">Usado</th>
                            <th style="text-align:center;width:60px;">Total</th>
                            <th style="text-align:center;width:60px;">Nuevo</th>
                            <th style="text-align:center;width:60px;">Usado</th>
                            <th style="text-align:center;width:60px;">Total</th>
                        </tr><tbody>');
        if ($resultx > 0) {
            while ($datax = mysqli_fetch_array($consultax)) {
                $cantN = $datax['CantN'] === null ? '0' : $datax['CantN'];
                $cantU = $datax['CantU'] === null ? '0' : $datax['CantU'];
                $cantC =$datax['compra'] === null ? '0' : $datax['compra'];
                $mpdf -> WriteHTML('<tr>
                <td style="padding-left:10px;" id="medida">'.$datax['medida'].'</td>
                <td style="padding-right:10px;">'.$cantC.'</td>
                <td style="padding-right:10px;">'.$datax['Nuevo'].'</td>
                <td style="padding-right:10px;">'.$datax['Usado'].'</td>
                <td style="padding-right:10px;">'.$datax['Nuevo']+$datax['Usado'].'</td>
                <td style="padding-right:10px;">'.$cantN.'</td>
                <td style="padding-right:10px;">'.$cantU.'</td>
                <td style="padding-right:10px;">'.$datax['CantN']+$datax['CantU'].'</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
              </tr>');
                $etc+=$cantC;
                $etn+=$datax['Nuevo'];
                $etu+=$datax['Usado'];
                $ett+=$datax['Nuevo']+$datax['Usado'];
                $ctn+=$cantN;
                $ctu+=$cantU;
                $ctt+=$cantN+$cantU;
            }
        }
        $mpdf -> WriteHTML('</tbody><tfoot>
        <tr>
            <th style="text-align: right;">Totales</th>
            <th id="derecha" style="padding-right:10px;">'.$etc.'</th>
            <th id="derecha" style="padding-right:10px;">'.$etn.'</th>
            <th id="derecha" style="padding-right:10px;">'.$etu.'</th>
            <th id="derecha" style="padding-right:10px;">'.$ett.'</th>
            <th id="derecha" style="padding-right:10px;">'.$ctn.'</th>
            <th id="derecha" style="padding-right:10px;">'.$ctu.'</th>
            <th id="derecha" style="padding-right:10px;">'.$ctt.'</th>
            <th></th>
            <th></th>
        </tr>
    </tfoot>
        </table><br>');
    }
}
//$mpdf -> WriteHTML('</table>');

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