<?php
//ini_set('display_errors',1);
require_once('../../libreriasphp/html2pdf/_tcpdf_5.9.206/tcpdf.php');

session_start();

require_once ("../../config/Cado.php");

require_once ("../../app/inventarioinicial/cInventarioinicial.php");
$oInventarioinicial = new cInventarioinicial();
require_once("../../app/producto/cProductolote.php");
$oProductolote = new cProductolote();
require_once ("../../app/empresa/cEmpresa.php");
$oEmpresa = new cEmpresa();
require_once ("../../app/stock/cStock.php");
$oStock = new cStock();
require_once ("../../app/formatos/numletras.php");
require_once ("../../app/formatos/formato.php");

date_default_timezone_set('America/Lima');
$now =  date('d-m-Y h:i:s a');

$dts=$oEmpresa->mostrarUno(1);
$dt = mysqli_fetch_array($dts);
	$emp_ruc=$dt['tb_empresa_ruc'];
	$emp_nomcom=$dt['tb_empresa_nomcom'];
	$emp_razsoc=$dt['tb_empresa_razsoc'];
	$emp_dir=$dt['tb_empresa_dir'];
	$emp_dir2=$dt['tb_empresa_dir2'];
	$emp_tel=$dt['tb_empresa_tel'];
	$emp_ema=$dt['tb_empresa_ema'];
	$emp_fir=$dt['tb_empresa_fir'];		
mysqli_free_result($dts);
	

	
$title = "INVENTARIO_".$now;
$nombre_archivo = $emp_ruc.'_INVENTARIO_'.$now.'.pdf';

// $file_name = 'FA-'.$serie.'-'.$numero.'-code.png';
// $path = 'temp/'.$file_name;
// $type = pathinfo($path, PATHINFO_EXTENSION);
// $data = file_get_contents($path);
// $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);


class MYPDF extends TCPDF
{

    public function Header() {	
      //$image_file = K_PATH_IMAGES.'logo.jpg';
      //$this->Image($image_file, 20, 10, 71, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
      // Set font
      //$this->SetFont('helvetica', 'B', 20);
      // Title
      //$this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
    }

    public function Footer() 
    {
      // $style = array(
      //   'position' => 'L',
      //   'align' => 'L',
      //   'stretch' => false,
      //   'fitwidth' => true,
      //   'cellfitalign' => '',
      //   'border' => false,
      //   'padding' => 0,
      //   'fgcolor' => array(0,0,0),
      //   'bgcolor' => false,
      //   'text' => false
      // //     'font' => 'helvetica',
      // //     'fontsize' => 8,
      // //     'stretchtext' => 4
      // );

      // $this -> SetY(-24);
      // // Page number
      // $this->SetFont('helvetica', '', 9);
      // //$this->SetTextColor(0,0,0);
      // $this->Cell(0, 0, 'Página '.$this->getAliasNumPage().' de '.$this->getAliasNbPages(), 'T', 1, 'R', 0, '', 0, false, 'T', 'M');
      
      // $codigo='CAV-'.str_pad($_GET['d1'], 4, "0", STR_PAD_LEFT);
      
      // $this->write1DBarcode($codigo, 'C128', '', 273, '', 6, 0.3, $style, 'N');
      // $this->Cell(0, 0, 'www.prestamosdelnortechiclayo.com', 0, 1, 'C', 0, '', 0, false, 'T', 'M');        
    }
}

// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('https://infosisperu.com.pe');
$pdf->SetTitle($title);

$pdf->SetSubject('https://infosisperu.com.pe');
$pdf->SetKeywords('https://infosisperu.com.pe');

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(12,15, 12);// left top right
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
//$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, 15);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// add a page
$pdf->AddPage('P', 'A4');

//font-family: Consolas, monaco, monospace;
//helvetica, monaco, monospace;

// Introducimos HTML de prueba
$html = '
<!DOCTYPE html>
<html lang="es">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
	 <link rel="shortcut icon" href="../../admin/bootstrap/images/favicon.png">
</head>
<style type="text/css">
    body {
        color: black;
        font-family: Verdana, Arial, Consolas;
        margin: 0px;
        padding-top: 0px;
        font-size: 7.5pt;
    }
    .header_row th {
		font-size:28px;
		background-color:#eee;
        border-bottom: 0.9px solid #010101;
        
        border-left: 0.9px solid #010101;
        border-top: 0.9px solid #010101;
    }
    .odd_row td {
        background-color: transparent;
        border-bottom: 0.9px solid #010101;
        padding-top: 5px;
        padding-bottom: 5px;
    }
    .even_row td {
        padding-top: 5px;
        padding-bottom: 5px;
       border-bottom: 0.9px solid #010101;
    }
    .row td{
        
        border-left: 0.9px solid #010101;
    }
</style>

<style media="print">
    .oculto{
        display: none;
    }

</style>
<body>




<table width="100%" style="" > 
<tr>
	<td width="22%">
	<img src="../../imagenes/logo1.png" alt="" />
	</td>
        <td style="text-align:center;" width="50%">
		<br />
		<br />
            <strong style="font-size:40px">'.$emp_razsoc.'</strong>
			<br>
            '.$emp_dir.'<br />   <strong>Telf.: '.$emp_tel.'  E-mail.: '.$emp_ema.'</strong> 
			<br />       
		</td>   
	   <td width="" style="text-align:center;border:1px solid #010101;">
         <br>
         <br>
       <strong style="font-size: 40px">INVENTARIO<br>
            RUC: '.$emp_ruc.'<br>
            '.$now.'</strong>
			  <br>
        </td>
    </tr>
</table>

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


<table width="100%;" style="border-collapse: collapse;height:400px;"  >
    <tbody >
        <tr class="" style="font-weight:bold;padding:10%;font-size:30px;">
            <th style="text-align:center; width:10%;padding:10%;" >CODIGO</th>
            <th style="text-align:center; width:40%;padding:10%;" >NOMBRE</th>
            <th style="text-align:center; width:15%;;padding:10%;" >LABORATORIO</th>
            <th style="text-align:center; width:10%;padding:10%;" >LOTE</th>
            <th style="text-align:right; width:10%;padding:10%;" >F. VEN.</th>	
            <th style="text-align:right; width:10%;padding:10%;" >STOCK</th>
            <th style="text-align:right; width:10%;;padding:10%;" ></th>
        </tr>
';

$dts = $oInventarioinicial->filtrar($_GET['dat1'],$_GET['dat2'],$_GET['dat3'],
$_GET['dat4'],$_GET['limit'],$_GET['alm_id'],$_GET['lab'],$_GET['acc'],$_GET['letlab']);
$numrowsin = mysqli_num_rows($dts);
while($dt1 = mysqli_fetch_array($dts))
	{
		
		
		if($_GET['alm_id']>0){
		    $stock = mysqli_fetch_array($oStock->stock_por_presentacion($dt1['tb_catalogo_id'],$dt1['tb_marca_id'],$_GET['alm_id']))['tb_stock_num']*1; 
		}
							
		$emp_fac = $dt1['tb_empaque_fac'];
							$can = $stock / $emp_fac;
							$canf = 0;
							$cane = 0;
							
							if($stock<$emp_fac){
								$cane = $cane;
								$canf = $stock;
							}else{
								list($cane,$canf) = explode('.',$can);
							}
							
							// echo $cane.'--'.$canf;
							
							if($cane>0 && $canf>0){
								
								$canemp = $cane * $emp_fac;
								$canf = $stock - $canemp;
								$stock = $cane.'F'.$canf;
								
							}else if($cane>0 && $canf==""){
								$stock = $cane;
							}else if($cane=="" && $canf>0){
								
								$stock = 'F'.$canf;
							}else if($cane==0 && $canf>0){
								
								$stock = 'F'.$canf;
							}
							
							
					$dtspl = $oProductolote->mostrarUltimoLote($dt1['tb_catalogo_id']);		
					$numrowspl = mysqli_num_rows($dtspl);
					
					$dtp = mysqli_fetch_array($dtspl);
					$lote = $dtp['tb_productolote_nom'];
					$lot_id = $dtp['tb_productolote_id'];
					

		
		$html.='
		<tr style="border-bottom:1px solid #010101;padding:10%;font-size:30px;">
		<td style="border-bottom: 0.9px solid #010101;" >'.$dt1['tb_producto_codint'].'</td>
		<td style="border-bottom: 0.9px solid #010101;" >'.$dt1["tb_producto_nom"].'</td>		
		<td style="border-bottom: 0.9px solid #010101;" >'.$dt1['tb_marca_nom'].'</td>
		<td style="border-bottom: 0.9px solid #010101;" >'.$lote.'</td>
		<td style="border-bottom: 0.9px solid #010101;text-align:right;" >'.mostrarFecha($dt1['tb_productomarca_fecven']).'</td>
		<td style="border-bottom: 0.9px solid #010101;text-align:right;" >'.$stock.'</td>
		<td style="border-bottom: 0.9px solid #010101;text-align:right;" >______</td>
		
		</tr>
		
		';				
		
		$cont++;
	}
	mysqli_free_result($dts);	




$html.='


<tr style="border:1px solid #010101;padding:10%;font-size:30px;">
		<td style="" colspan="7" >'.$numrowsin.' Registros.</td>
</tr>

</tbody>

</table>

<br/>
<br/>

';

$html.='
</body>
</html>';

$pdf->writeHTML($html, true, 0, true, true);

//$pdf->write2DBarcode($ruc_empresa.'|'.$idcomprobante.'|'.$serie.'|'.$numero.'|'.$toigv.'|'.$importetotal.'|'.fecha_mysql($fecha).'|'.$idtipodni.'|'.$ruc.'|', 'QRCODE,Q', 157, 99, 40, 40, $style, 'N');

$pdf->Output($nombre_archivo, 'I');
