<?php
require_once('../../libreriasphp/html2pdf/_tcpdf_5.9.206/tcpdf.php');

//ini_set('display_errors',1);

session_start();

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

require_once("../../app/guiaremision/cGuiaremision.php");
$oGuiaremision = new cGuiaremision();

require_once ("../../app/empresa/cEmpresa.php");
$oEmpresa = new cEmpresa();

require_once ("../../app/producto/cCatalogo.php");
$oCatalogo = new cCatalogo();

require_once ("../../app/formatos/numletras.php");
require_once ("../../app/formatos/formato.php");

	$guiaid = $_POST['guiaid'];

	$dtsped= $oGuiaremision->mostrarUno($guiaid);
	$dtped= mysqli_fetch_array($dtsped);
	
	$guianumDoc	=$dtped['tb_guiaremision_numdoc'];
	$guiaobs	=$dtped['tb_guiaremision_obs'];
	$guiafec= mostrarFecha($dtped['tb_guiaremision_fec']);
	
	$connom=$dtped['tb_conductor_nom'];
	$conruc=$dtped['tb_conductor_doc'];
	$conlic=$dtped['tb_conductor_lic'];
	$concat=$dtped['tb_conductor_cat'];
	
	$tranom=$dtped['tb_transporte_razsoc'];
	$traruc=$dtped['tb_transporte_ruc'];
	$tradir=$dtped['tb_transporte_dir'];
	
	$vemar=$dtped['tb_vehiculo_mar'];
	$vepla=$dtped['tb_vehiculo_pla'];
	$guiamotid=$dtped['tb_motivo_id'];
	
	$vennumdoc=$dtped['docrel'];
	$venfec=$dtped['tb_venta_reg'];
	$monid=$dtped['tb_moneda_id'];
	$guiapar=$dtped['tb_guiaremision_par'];
	$guialle=$dtped['tb_guiaremision_lleg'];
	$ate = $dtped['tb_guiaremision_ate'];
	$guiadigval = $dtped['tb_guiaremision_digval'];
	
	$cliNom=$dtped['tb_receptor_nom'];
	$cliDoc=$dtped['tb_receptor_doc'];
	
	$docabr=$dtped['tb_documento_abr'];
	$docrel=$dtped['docabr'];
	$empid=$dtped['empid'];
	
	mysqli_free_result($dtsped);
	
	if($guiamotid==1){
		$guiamot = 'VENTA';
	}else if($guiamotid==2){
		$guiamot = 'COMPRA';
	}else if($guiamotid==4){
		$guiamot = 'TRASLADO ENTRE ESTABLECIMIENTOS DE LA MISMA EMPRESA';
	}else if($guiamotid==14){
		$guiamot = 'VENTA SUJETA A CONFIRMACION DEL COMPRADOR';
	}else if($guiamotid==13){
		$guiamot = 'OTROS';
	}
	
if($monid==1){
		$lblmon = 'SOLES';
	}else if($monid==2){
		$lblmon = 'DOLARES AMERICANOS';
	}
	
		
	$ser =  substr($guianumDoc,0,4); 
	$corr =  substr($guianumDoc,5,13); 
	
	$cont = strlen($cliDoc);

	if($cont==11){
	$tipdoc = '06';
	}else if($cont==8){
	$tipdoc = '01';
	}
	
	
	$tipodoc = 'GUIA DE REMISION ELECTRONICA';
	
$dts=$oEmpresa->mostrarUno($empid);
$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 = "Representacion de ".$guianumDoc;
$nombre_archivo = 'pdf/'.$emp_ruc.'_'.$guianumDoc.'_'.$cliDoc.'.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('www.infosisperu.com.pe');
$pdf->SetTitle($title);

$pdf->SetSubject('www.infosisperu.com.pe');
$pdf->SetKeywords('www.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-right: 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-right: 0.9px solid #010101;
        border-left: 0.9px solid #010101;
    }
</style>

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

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

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

<table style="border-collapse: collapse;" >
<tr>
		<td style="border:1px solid #010101;background-color:#bdbdbd;font-size:40px;font-weight:bold;" >
		INFORMACION GENERAL
		</td>
</tr>
<tr>
		<td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;font-size:35px" width="18%">SEÑOR(ES)</td>
        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="82%">'.$cliNom.'</td>
</tr>
<tr>
		<td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;font-size:35px" width="18%">RUC</td>
        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="32%">'.$cliDoc.'</td>
        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;font-size:35px" width="25%">FECHA EMISION</td>
        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="25%">'.$guiafec.'</td>
</tr>
<tr>
		<td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;font-size:35px" width="18%">DIRECCION</td>
        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="82%">'.$cliDir.'</td>
</tr>

<tr>	
        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;font-size:35px" width="18%">PUN. PARTIDA</td>
        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="82%">'.$guiapar.'</td>
</tr>
<tr>
		<td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;font-size:35px" width="18%">PUN. LLEGADA</td>
        <td style="text-align: left;border:1px solid #010101;font-size:30px" width="82%">'.$guialle.'</td>
</tr>
</table>

<br />
<br />

<table style="border-collapse: collapse;" >
	<tr>	
        <td style="text-align: left;font-weight:bold;font-size:12pt;border:1px solid #010101;background-color:#bdbdbd" width="12%">MOTIVO</td>
        <td style="text-align: left;font-size:12pt;border:1px solid #010101;" width="46%">'.$guiamot.'</td>


		<td style="text-align: left;font-weight:bold;font-size:12pt;border:1px solid #010101;background-color:#bdbdbd" width="22%">DOC. OPERACION</td>
        <td style="text-align: left;font-size:12pt;border:1px solid #010101;" width="20%">'.$vennumdoc.'</td>

		
    </tr>
</table>

	 
	 <br />
	 <br />

<table style=" border-collapse:collapse;"  style="border-collapse: collapse;border:0.9px solid #010101;height:400px;" >
    <tbody >
        <tr style="font-weight:bold;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;">
            <th style="text-align:center; width:10%;border:0.9px solid #010101;background-color:#bdbdbd;padding:10%;" >CODIGO</th>
            <th style="text-align:center; width:45%;border:0.9px solid #010101;background-color:#bdbdbd;padding:10%;" >DESCRIPCION</th>
            <th style="text-align:center; width:15%;border:0.9px solid #010101;background-color:#bdbdbd;padding:10%;" >MARCA</th>
            <th style="text-align:center; width:6%;border:0.9px solid #010101;background-color:#bdbdbd;padding:10%;" >U.M.</th>
            <th style="text-align:right; width:5%;border:0.9px solid #010101;background-color:#bdbdbd;padding:10%;" >CAN</th>
            <th style="text-align:right; width:9%;border:0.9px solid #010101;background-color:#bdbdbd;padding:10%;" >PESO</th>
            <th style="text-align:right; width:10%;border:0.9px solid #010101;background-color:#bdbdbd;padding:10%;" >PESO TOT.</th>
        </tr>
';


$dtsdetven=$oGuiaremision->mostrardetalle($guiaid);
$cont=1;
while($dt1 = mysqli_fetch_array($dtsdetven))
	{
		
		$dts1=$oCatalogo->mostrarUno($dt1['tb_catalogo_id'],$dt1['tb_marca_id']);
		$dt = mysqli_fetch_array($dts1);
		
		if($docrel == 'NV'){
			$pretot = $dt1["tb_guiaremisiondetalle_preuni"]*$dt1["tb_guiaremisiondetalle_can"];
		}else{
			$pretot = '0.00';
		}
		
		
		
		$html.='
		<tr style="border:0.9px solid #010101;padding:10%;font-size:30px;" >
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;">'.$dt["tb_productomarca_codbar"].'</td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;">'.$dt["tb_producto_nom"].'</td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" >'.$dt["tb_marca_nom"].'</td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" >'.$dt["tb_unidad_abr"].'</td>
		<td align="right" style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" >'.$dt1["tb_guiaremisiondetalle_can"] * 1 .'</td>
		<td align="right" style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" >'.formato_money3($dt1["tb_guiaremisiondetalle_pes"]).'</td>	
		<td align="right" style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" >'.formato_money3($dt1["tb_guiaremisiondetalle_can"]*$dt1["tb_guiaremisiondetalle_pes"]).'</td>
		</tr>
		
		';				
		
		$tot += $pretot;
		$pestot += $dt1["tb_guiaremisiondetalle_can"]*$dt1["tb_guiaremisiondetalle_pes"]; 
		$cont++;
	}
	mysqli_free_result($dtsdetven);	




$html.='
</tbody>
</table>

<br />
<br />

<table>
<tr>
<td colspan="4" style="font-size:12pt;font-weight:bold;">ATENCION* '.$ate.'</td>
</tr>
</table>

<br />
<br />

<table>
<tr><td>
<span style="font-weight:bold;font-size:35px;">NOTA U OBSERVACION* </span>
<span style="font-size:35px;">'.formato_money($tot).' '.$lblmon.' | PESO TOTAL '.formato_money3($pestot).' KG </span>
</td></tr>
</table>

<br />
<br/>

<!--
<table style="border-collapse: collapse;" >
<tr>
		<td style="border:1px solid #010101;background-color:#bdbdbd;font-size:40px;font-weight:bold;" >INFORMACION DEL VEHICULO Y OPERACION</td>
</tr>
<tr>
		<td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;font-size:35px;" width="15%">MARCA</td>
        <td style="text-align: left;border:1px solid #010101;font-size:35px;" width="35%">'.$vemar.'</td>
		
		<td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;font-size:35px;" width="15%">PLACA</td>
        <td style="text-align: left;border:1px solid #010101;font-size:35px;" width="35%">'.$vepla.'</td>
</tr>
</table>

<br />
<br />
-->
<table style="border-collapse: collapse;" width="100%;" >
<tr>
		<td style="border:1px solid #010101;background-color:#bdbdbd;font-size:40px;font-weight:bold;" >INFORMACION DEL TRANSPORTISTA</td>
</tr>
<tr>
        <td style="text-align: left;border:1px solid #010101;font-size:35px;" width="100%" height="60px;">
		
		</td>
</tr>
</table>

<br />
<br />

<table width="100%;" style="border-collapse: collapse;"  >
<tr>
<td width="75%;">
<br />
<p style="font-size:10pt">
Código de Seguridad (Hash): '.$guiadigval.'<br>
Representación Impresa de la '.$tipodoc.'.<br>
Esta puede ser consultada en: <strong>http://e-consulta.sunat.gob.pe/ol-ti-itconsvalicpe/ConsValiCpe.htm</strong><br>
Autorizado mediante Resolucion de Superintendencia SUNAT
</p>
</td>
</tr>
</table>

</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, 'F');

?>