<?php
session_start();
ob_start();

require_once "../../../config/database.php";
include "../../../config/fungsi_tanggal.php";
include "../../../config/fungsi_rupiah.php";

if (!isset($_GET['id'])){
    echo "Favor seleccionar una remision";
    return;  
}
$cond = $_GET['id'];
$created_user = $_SESSION['id_user'];

$hari_ini = date("d-m-Y");

$no = 1;

$query = mysqli_query($mysqli, "
     SELECT a.tipo_transaccion, a.codigo_transaccion,a.fecha,a.codigo,a.numero,
					b.codigo,b.nombre,b.unidad,a.observaciones,a.created_user,c.username, 
					a.idenc,CONCAT(d.serie,d.id) as 'folio',(select nombre_entrega from  transaccion_medicamentos where idenc = (" . $cond . ") limit 1) as 'recibe',d.observacion_enc,d.txtMotivo
					FROM transaccion_medicamentos as a, 
					medicamentos as b,
					usuarios as c,
					transaccion_medicamentos_enc as d
					Where
						a.codigo=b.codigo
					and a.tipo_transaccion='Salida'
					and a.created_user = c.id_user 
					and d.id = a.idenc 
					and a.idenc in(" . $cond . ")
					ORDER BY codigo_transaccion DESC")
        or die('Error '.mysqli_error($mysqli));

$count  = mysqli_num_rows($query);

$sql_folio=mysqli_query($mysqli,"SELECT a.tipo_transaccion,a.status, a.codigo_transaccion,a.fecha,a.codigo,a.numero,
					b.codigo,b.nombre,b.unidad,a.observaciones,a.created_user,c.name_user, 
					a.idenc,CONCAT(d.serie,d.id) as 'folio', e.id as remision ,
                    (select nombre_entrega from  transaccion_medicamentos where idenc = (" . $cond . ") limit 1) as 'recibe',
					d.observacion_enc,d.txtMotivo,d.txtCancel
					FROM transaccion_medicamentos as a, 
					medicamentos as b,
					usuarios as c,
					transaccion_medicamentos_enc as d,
                    remision_encabezado as e
					Where
						a.codigo=b.codigo
                    and e.idSalida = d.folio
					and a.tipo_transaccion='Salida'
					and a.created_user = c.id_user 
					and d.id = a.idenc 
					and a.idenc in(" . $cond . ")
					ORDER BY codigo_transaccion DESC ");
$rw_folio=mysqli_fetch_array($sql_folio);
$status= $rw_folio['status'];

$sql_usuario=mysqli_query($mysqli,"select * from usuarios WHERE id_user='$_SESSION[id_user]' ");
$rw_usuario=mysqli_fetch_array($sql_usuario);

$queryEnc = mysqli_query($mysqli, "
    SELECT e.* FROM remision_encabezado as e WHERE e.idSalida = '$idenc'")
        or die('Error '.mysqli_error($mysqli));
$rw_encabezado_rem=mysqli_fetch_array($queryEnc);   
$foliorem = $rw_encabezado_rem['folio']; 

$queryperfil = mysqli_query($mysqli, "SELECT * FROM perfil")
                                or die('error'.mysqli_error($mysqli));
$rows  = mysqli_num_rows($queryperfil);
$data_perfil  = mysqli_fetch_assoc($queryperfil);
$folio_perfil = $data_perfil['folio'];
$folio_rem_perfil = $data_perfil['folio2'];
$nombre_perfil = $data_perfil['nombre_empresa'];
$direccion_perfil = $data_perfil['direccion'];
$direccion_perfil = $data_perfil['direccion'];
$email_perfil = $data_perfil['email'];
$almacen_perfil = $data_perfil['almacen'];
$almacenista_perfil = $data_perfil['almacenista'];
$imagen = $data_perfil['imagen'];



$queryrecibe = mysqli_query($mysqli, "
SELECT b.nombre FROM transaccion_medicamentos_enc as a,correos as b WHERE a.txtCorreo = b.tipo and a.id = '$cond' ")
or die('Error '.mysqli_error($mysqli));
$rows  = mysqli_num_rows($queryrecibe);
$data  = mysqli_fetch_assoc($queryrecibe);
$recibe = $data['nombre'];
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Informe de Salida</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f3f3f3;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        h1, h2, h3 {
            color: #333;
            text-align: center;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        table, th, td {
            border: 1px solid #ddd;
        }
        th, td {
            padding: 10px;
            text-align: left;
        }
        th {
            background-color: #005a6f;
            color: #fff;
        }
        .highlight {
            background-color: #ffd700;
        }
        .signature {
            width: 100%;
            border-top: 1px solid #ddd;
            padding-top: 20px;
            margin-top: 20px;
            text-align: center;
            font-style: italic;
        }
        .highlighted {
            background-color: #ffffcc;
        }
    </style>
</head>
<body>
    <div class="container" >
        <img src="http://<?=$_SERVER['HTTP_HOST']?>/images/<?= $data_perfil['imagen']?>/logo.png" alt="Logo" style="max-width: 60px; height: auto; margin: 0 auto; display: block; ">
        <h2>SALIDA DE ALMACEN  <?=$data_perfil['almacen']?> </h2>

     
            <tr style="margin-bottom: 10px;">
                <strong>Folio:</strong>
                <span style='color: #ff0000; font-size: 100%; font-weight: bold;'>    <?=$folio_perfil?> - <?=$rw_folio['idenc']?></span><br>
                <strong>Fecha:</strong>    <?=$rw_folio['fecha']?> <br> 
                <strong>Almacenista:</strong>   <?=$data_perfil['almacenista']?> <br>   
                <strong>Autoriza:</strong>     <?=$rw_folio['name_user']?> <br> 
                <strong>Motivo de Salida:</strong>    <?=$rw_folio['txtMotivo']?> <br> 
                <strong>Estatus:</strong>       
                     <?php 
                if($status == 'C'){  
                    echo "<span style='color:#ff0000; font-size:90%; font-weight:bold;'>CANCELADO</span>";
                } elseif($status == 'A'){  
                    echo "<span style='color:#14A1F5; font-size:90%; font-weight:bold;'>SALIDA SURTIDA</span>";
                }
                ?>
                
            
            </tr> <br> <br> 
      

        Detalle de la Salida <?=$rw_folio['folio']?> Relacionado con la Remision <?=$folio_rem_perfil?>-<?=$rw_folio['remision']?> <br><br>

        <table>
            <thead>
                <tr>
                    <th>PARTIDA</th>
                    <th>CANTIDAD</th>
                    <th>ARTICULO</th>
                    <th>CODIGO</th>
                    <th>TRANS</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $no = 1;
                while ($data = mysqli_fetch_assoc($query)) {
                    echo "<tr> 
                            <td>{$no}</td>
                            <td>{$data['numero']}</td>
                            <td>{$data['nombre']}</td>
                            <td>{$data['codigo']}</td>
                            <td>{$data['codigo_transaccion']}</td>
                        </tr>";
                    $no++;
                }
                ?>
            </tbody>
        </table>
        
        
        
            
                <h5><strong>OBSERVACIONES</strong><br></h5>
                <?=$rw_folio['observacion_enc']?><br><br><br>
                
                <!--<img src="http://<?=$_SERVER['HTTP_HOST']?>/images/surtido.png" alt="Logo" style="max-width: 60px; height: auto; margin: 0 auto; display: block; "><br>-->
                <div style="text-align: center;">
                <strong>FIRMA DE RECIBIDO</strong><br>
                <strong>_________________</strong>
                </div>

                <h5 style="text-align: center;"><?=$recibe?></h5>
                
            <?php
			  if($status == 'C'){    
				   echo "<div align='center'>
				  <img src='http://".$_SERVER['HTTP_HOST']."/images/cancelado.png' border='0' width='150' height='170' align=\'center\'> 
				   </div>
				   <br>
					<br>
				   
					<tr >
					
					<strong align='center'style='color:#ff0000;font-size:100%;font-weight:bold;'>MOTIVO DE CANCELACION:</strong>
					</tr>
					<tr >
					<strong align='center'style='color:#ff0000;font-size:100%;font-weight:bold;'>$rw_folio[txtCancel]</strong>
					</tr>
				   <br/>
				   ";
				} else{
				     echo "<div align='center'>
				  <img src='http://".$_SERVER['HTTP_HOST']."/images/surtido.png' border='0' width='150' height='170' align=\'center\'> 
				   </div>";
				}
				
			  ?>

        

    </div>
</body>
</html>
<?php
$filename = "$folio_perfil-$cond.pdf"; 

$content = ob_get_clean();
$content = '<page style="font-family: freeserif">' . $content . '</page>';

require_once('../../../assets/plugins/html2pdf_v4.03/html2pdf.class.php');
try {
    $html2pdf = new HTML2PDF('P','F4','en', false, 'ISO-8859-15', array(10, 10, 10, 10));
    $html2pdf->setDefaultFont('Arial');
    $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
    $html2pdf->Output($filename);
} catch(HTML2PDF_exception $e) {
    echo $e;
}
?>

