<?php
session_start();
ob_start();

if($_GET['sel'] == ""){
    echo "Favor de seleccionar registros";
    return;
}

$fecha = $_GET['fecha'];
$nombre = $_GET['nombre'];
$observacion_enc = $_GET['observacion_enc'];

$sel = $_GET['sel'];
$sel = str_replace("chk_", "", $sel);
$arr = explode("|",$sel);
$cond = "";
for($x = 0; $x < count($arr); $x++){
    $cond .= "'" . $arr[$x] . "',";
}
if($cond != "")
    $cond =substr($cond, 0, - 1);

//echo $cond;
//return;

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

$created_user = $_SESSION['id_user'];

//Get Max Folio + 1
$query = mysqli_query($mysqli, "SELECT IFNULL(MAX(folio),0) + 1 as folio FROM transaccion_medicamentos_enc")
                                or die('error'.mysqli_error($mysqli));
$rows  = mysqli_num_rows($query);
$data  = mysqli_fetch_assoc($query);
$folio = $data['folio'];

//Insert in table transaccion_medicamentos_enc
$query = mysqli_query($mysqli, "
    INSERT INTO transaccion_medicamentos_enc(folio,fecha,created_user,nombre,status,observacion_enc) 
    VALUES('$folio','$fecha','$created_user','$nombre','A','$observacion_enc')")
    or die('error '.mysqli_error($mysqli));

//Get Last Insert Id
$query = mysqli_query($mysqli, "SELECT MAX(id) as idenc FROM transaccion_medicamentos_enc")
                                or die('error'.mysqli_error($mysqli));
$rows  = mysqli_num_rows($query);
$data  = mysqli_fetch_assoc($query);
$idenc = $data['idenc'];

//Update idenc in table transaccion_medicamentos
for($x = 0; $x < count($arr); $x++) {
    $query = mysqli_query($mysqli, "
        UPDATE transaccion_medicamentos SET idenc = '$idenc' WHERE codigo_transaccion = '$arr[$x]'")
        or die('error: '.mysqli_error($mysqli));
}

//Query Encabezado
$queryEnc = mysqli_query($mysqli, "
    SELECT e.*
    FROM transaccion_medicamentos_enc as e
    WHERE e.id = '$idenc'")
        or die('Error '.mysqli_error($mysqli));
$rw_encabezado=mysqli_fetch_array($queryEnc);        

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

$no = 1;

//Query detalle
$querydet = mysqli_query($mysqli, "
    SELECT b.*
    FROM transaccion_medicamentos as b
    WHERE b.idenc = '$idenc'")
        or die('Error '.mysqli_error($mysqli));
$rw_detalle=mysqli_fetch_array($querydet);

$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.nombre_entrega
    FROM transaccion_medicamentos as a
    INNER JOIN medicamentos as b ON a.codigo=b.codigo and a.tipo_transaccion='Salida'
    INNER JOIN usuarios as c on a.created_user = c.id_user 
    WHERE a.codigo_transaccion in(" . $cond . ")
    ORDER BY codigo_transaccion DESC")
        or die('Error '.mysqli_error($mysqli));

$count  = mysqli_num_rows($query);






?>
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>SALIDAS DE ALMACEN TRADE MKT</title>
        <link rel="stylesheet" type="text/css" href="../../assets/css/laporan.css" />
       
    </head>
    <body>
        <div id="myHeader">
           CASA SAN MATIAS, DEPARTAEMNTO TRADE MARKETING		   
        </div>
        
        <hr><br>    
		
		  <div id="title">
           VALE DE SALIDA ALMACEN TRADE	   
             <br>  
             <br>
             <br>

             <?php
           
            
               
               $dias = array("Domingo","Lunes","Martes","Miercoles","Jueves","Viernes","Sábado");
               $meses = array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
               echo $dias[date('w')]." ".date('d')." de ".$meses[date('n')-1]. " del ".date('Y') ;
             
              ?>

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

        <div id="myHeader">
              


               <?php
               //echo  'Entrega:  ';
           

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

               echo "FOLIO:      ".$rw_encabezado['serie'].$rw_encabezado['folio'];            
               echo "<br>";
			   echo "<br>";
               echo "ENTREGA:      ".$rw_usuario['name_user'];
			   echo "<br>";
			   echo "<br>";
            
            echo "<br>";
               echo "<br>";
			   echo "RECIBE:       ".$rw_detalle['nombre_entrega'];
               echo "<br>";
			   echo "<br>";
               echo "<br>";
               echo "<br>";

              
               
              ?>

        </div>


        <div id="myHeader">
		
            <table width="100%" border="0.3" cellpadding="0" cellspacing="0">
                <thead style="background:#e8ecee">
                    <tr class="tr-title">
                        <th height="25" align="center" valign="middle"><small>NO.</small></th>
                        <th height="20" align="center" valign="middle"><small>TRANS</small></th>
                        <th height="20" align="center" valign="middle"><small>CODIGO</small></th>
                        <th height="20" align="center" valign="middle"><small>PRODUCTOS</small></th>
                        <th height="20" align="center" valign="middle"><small>CANTIDAD</small></th>
                      
                        <th height="20" align="center" valign="middle"><small>AUTORIZA</small></th>
                        <th height="20" align="center" valign="middle"><small>OBSERVACIONES</small></th>
						
                    </tr>
                </thead>
                <tbody>

                    <br>  
             <br>
             <br>


        <?php


      
        while ($data = mysqli_fetch_assoc($query)) {
        #    $precio_compra = format_rupiah($data['precio_compra']);
         #   $precio_venta = format_rupiah($data['precio_venta']);
          
            echo "  <tr>
                        <td width='40' height='13' align='center' valign='middle'>$no</td>
                        <td width='150' height='13' align='center' valign='middle'>$data[codigo_transaccion]</td>
                        
                        <td width='80' height='13' align='center' valign='middle'>$data[codigo]</td>
                        <td style='padding-left:5px;' width='180' height='13' valign='middle'>$data[nombre]</td>
                        <td style='padding-right:10px;' width='60' height='13' align='center' valign='middle'>$data[numero]</td>
                     
                        <td width='50' height='13' align='center' valign='middle'>$data[username]</td>
						<td width='350' height='13' align='center' valign='middle'>$data[observaciones]</td>
                    </tr>";
            $no++;
        }
        ?>  
                </tbody>
            </table>

            
        </div>
		
		
<?php

echo "<br>";    
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo  "<div align=\"center\">_________________________</div><br>"; 
echo "<br>";
echo  "<div align=\"center\">Recibe</div><br>";
echo  "<div align=\"center\">$rw_detalle[nombre_entrega]</div><br>"; 


             
?>








    </body>
</html>

<?php
$filename="SALIDAS DE ALMACEN TRADE MKT.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('L','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; }
?>