<?php
//ini_set('display_erros',1);
require '../../libreriasphp/vendor/autoload.php';
use Spipu\Html2Pdf\Html2Pdf;

session_start();

require_once ("../../config/Cado.php");
require_once ("../../app/compra/cCompra.php");
$oCompra = new cCompra();
require_once ("../../app/empresa/cEmpresa.php");
$oEmpresa = new cEmpresa();
require_once ("../../app/compra/cComprapago.php");
$oComprapago = new cComprapago();
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= $oCompra->mostrarUno($_GET['com_id']);
	$dt = mysqli_fetch_array($dts);
	
	$fec	=mostrarFecha($dt['tb_compra_fec']);

	$doc_nom=$dt['tb_documento_nom'];
	$doc_id	=$dt['tb_documento_id'];
	$numdoc	=$dt['tb_compra_numdoc'];
	$usu_nom	=$dt['tb_usuario_use'];

	$mon_id	=$dt['tb_moneda_id'];
	$tipcam	=$dt['tb_compra_tipcam'];
	$tipcam_can	=$dt['tb_compra_tipcamcan'];
	
	$pro_id	=$dt['tb_proveedor_id'];
	$pro_nom	=$dt['tb_proveedor_nom'];
	$pro_dir	=$dt['tb_proveedor_dir'];
	$pro_doc	=$dt['tb_proveedor_doc'];
	$pro_id	=$dt['tb_proveedor_id'];
	
	$subtot	=$dt['tb_compra_subtot'];
	$des	=$dt['tb_compra_des'];
	$descal	=$dt['tb_compra_descal'];
	$fle	=$dt['tb_compra_fle'];
	$tipfle	=$dt['tb_compra_tipfle'];
	$ajupos	=$dt['tb_compra_ajupos'];
	$ajuneg	=$dt['tb_compra_ajuneg'];
	$com_valven	=$dt['tb_compra_valven'];
	$com_igv	=$dt['tb_compra_igv'];
	$com_tot	=$dt['tb_compra_tot'];
	$m	=$dt['tb_compra_permes'];
	$y	=$dt['tb_compra_peranio'];

	$com_igvval	=$dt['tb_compra_igvval'];		
	$per	=$dt['tb_compra_per'];		
	$tippro	=$dt['tb_compra_tippro'];
	$com_igvigv =$dt['tb_compra_igvigv'];
	$ord_com	=$dt['tb_compra_nrordcom'];
	$nroped	=$dt['tb_compra_nroped'];		
	$nroord	=$dt['tb_compra_nroped'];		
	$nroguia_e=$dt['tb_compra_nroguia_emi'];		
	$nroguia_t=$dt['tb_compra_nroguia_tra'];		
	$ref=$dt['tb_compra_ref'];		
	$fecguia=mostrarFecha($dt['tb_compra_fecguia']);		

	$alm_id	=$dt['tb_almacen_id'];
	$est	=$dt['tb_compra_est'];
	$emp_id	=$dt['tb_empresa_id'];
	mysqli_free_result($dts);
	
	//$emp_id	= 1;
	
//	echo '-----'.$emp_id;
	
	
	if($mon_id==1){
		$mon ="S/ ";
		$lblmon="SOLES";
	}else if($mon_id==2)
	{
		$lblmon="DOLARES AMERICANOS";
		$mon ="US$ ";
	}
	
$dts=$oEmpresa->mostrarUno($emp_id);
$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 ".$numdoc;
$nombre_archivo = $emp_ruc.'_'.$numDoc.'.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-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 width="100%" style="" >
<tr>
		<td width="50%"></td>
	    <td width="50%" style="text-align: right">
		<strong style="color:red;font-size:10pt;" >INGRESO DE COMPRA</strong> 
		</td>
		</tr>
		
<tr>
	<td width="25%">
	<img src="https://boticanomasalud.tec.ovh/imagenes/logo.png" alt="" />
		<br />
		<br />
        <strong style="font-size:20px">'.$emp_razsoc.'</strong>
		<br>
        '.$emp_dir.'<br/>  
        Telf.: '.$emp_tel.'  E-mail.: '.$emp_ema.'
		<br />
	</td>
        <td style="text-align:center;" width="15%">
	      
		</td>   
	   <td width="" style="text-align:center;">
         <br/>
         <br/>
         <br/>
        <div style="background-color:#EEEEEE;border-radius:20%;border:1px solid #E0E0E0;">
       <strong style="font-size: 20px">RUC: '.$emp_ruc.'<br>
       '.$doc_nom.'<br>
            '.$numdoc.'</strong>
			  <br>
			  </div>
        </td>
    </tr>
</table>

<br />

<table width="100%" >
    
    <tr>
    
	<td width="65%">
	
	<div style="border-radius:20%;border:1px solid #E0E0E0;padding:10%;">
	
    <table  >
        <tr  style="border:1px solid #010101" >
            <td style="text-align: left;font-weight:bold;padding:10%;font-size:12px;" width="25%"><strong></strong></td>
            <td style="text-align: left;padding:15%;font-size:10px" width="75%"></td>
        </tr>
        
        <tr  style="border:1px solid #010101" >
            <td style="text-align: left;font-weight:bold;padding:10%;font-size:12px;" width="25%"><strong>SEÑOR(ES)</strong></td>
            <td style="text-align: left;padding:15%;font-size:10px" width="75%">'.$pro_nom.'</td>
        </tr>
        
        <tr  style="border:1px solid #010101" >
            <td style="text-align: left;font-weight:bold;padding:10%;font-size:12px;" width="25%">RUC</td>
            <td style="text-align: left;font-size:10px" width="75%">'.$pro_doc.'</td>
        </tr>
        
        <tr>
            <td style="text-align: left;font-weight:bold;padding:10%;font-size:12px;" width="25%">DIRECCIÓN</td>
            <td style="text-align: left;font-size:10px" width="75%">'.$pro_dir.'</td>
		</tr>
    </table>
    </div>
    
	</td>
    <td width="5%">
	      
	</td>
	
	<td width="30%" style="border-radius:20%;border:1px solid #E0E0E0;" >
        

	
    <table  >
       
        
        <tr  style="border:1px solid #010101" >
            <td style="text-align: left;font-weight:bold;padding:10%;font-size:12px;" width="45%"><strong>USUARIO</strong></td>
            <td style="text-align: left;padding:15%;font-size:10px" width="55%">'.$usu_nom.'</td>
        </tr>
        
        <tr  style="border:1px solid #010101" >
            <td style="text-align: left;font-weight:bold;padding:10%;font-size:12px;" width="45%">MONEDA</td>
            <td style="text-align: left;font-size:10px" width="55%">'.$lblmon.'</td>
        </tr>
    </table>
   
    
        
        
    </td>
    
    </tr>
    
</table>

<br />

<div style="border-radius:20%;border:1px solid #E0E0E0;">
<table width="100%;" style="border-collapse: collapse;"  >
    <tbody >
    
        <tr  style="border:1px solid #010101" >
            <td style="text-align: left;font-weight:bold;padding:10%;font-size:12px;" width="25%"><strong></strong></td>
            <td style="text-align: left;padding:15%;font-size:10px" width="75%"></td>
        </tr>
        
        <tr class="" style="font-weight:bold;padding:10%;font-size:10px;">
            <th style="text-align:center; width:20%;padding:10%;" >FECHA EMISION</th>
            <th style="text-align:center; width:20%;padding:10%;" >ORDEN COMPRA</th>
        </tr>
		
		<tr style="padding:10%;font-size:10px;">		
		<td style="padding:10%;text-align:center;" >'.$fec.'</td>
		<td style="padding:10%;text-align:center;" >'.$ord_com.'</td>	
		</tr>
	
	</tbody>
</table>

</div>

<br />

<table width="100%;" style="border-collapse: collapse;height:400px;"  >
    <tbody >
        <tr class="" style="font-weight:bold;border:1px solid #010101;background-color:#EEEEEE;padding:15%;font-size:11px;">
            <th style="text-align:center; width:10%;background-color:#EEEEEE;padding:10%;" >CODIGO</th>
            <th style="text-align:center; width:37%;background-color:#EEEEEE;padding:10%;" >DESCRIPCION</th>
            <th style="text-align:center; width:10%;background-color:#EEEEEE;padding:10%;" >LOTE</th>
            <th style="text-align:center; width:8%;background-color:#EEEEEE;padding:10%;" >CAN</th>
            <th style="text-align:right; width:9%;background-color:#EEEEEE;padding:10%;" >V/U</th>		
            <th style="text-align:right; width:9%;background-color:#EEEEEE;padding:10%;" >P/U</th>			
            <th style="text-align:right; width:9%;background-color:#EEEEEE;padding:10%;" >V/T</th>
            <th style="text-align:right; width:9%;background-color:#EEEEEE;padding:10%;" >P/T</th>
        </tr>
';

$dts1=$oCompra->mostrar_compra_detalle($_GET['com_id']);
$num_rows= mysqli_num_rows($dts1);

while($dt1 = mysqli_fetch_array($dts1))
	{
	    $val_uni=$dt1['tb_compradetalle_preuni'];
        $can = $dt1['tb_compradetalle_can'];
        
        
        $emp_fac =  $dt1['tb_empaque_fac'];
			
			if($can>=$emp_fac){
				$can = $can/$emp_fac;
			}else{
				$can = $can;
			}
		
			
			//$can = $detven_can/$emp_fac;
		
			list($cane,$canf) = explode('.',$can);
			$canf = $canf * 1;
			
			//echo $cane.'--'.$canf ;
			//echo $canf.'-' ;


			if($cane!=="" && $canf==""){ 
				$can = $cane;
			}else if($cane=="" && $canf!==""){ 
				$can = $canf;
			}
			else if($cane!=="" && $canf!==""){ 
			
				$can = $can1;
			}
		
        
        
        
        
		$igv_uni = $val_uni * $com_igvigv;
		$pre_uni=$val_uni+$igv_uni;
			
		$val_tot = $val_uni * $can;
		$des_tot = $des_uni * $can;
		$igv_tot = $igv_uni * $can;
		$pre_tot = $pre_uni * $can;
		
		
		$html.='
		<tr style="margin:15%;font-size:10px;border-top:1px solid #E0E0E0;">
		<td style="" >'.$dt1['tb_producto_codint'].'</td>
		<td style="" >'.$dt1["tb_producto_nom"].'</td>
		<td style="" >'.$dt1['tb_compradetalle_lote'].'</td>
		<td style="text-align:right;" >'.$can.'</td>
		<td style="text-align:right;" >'.formato_money($val_uni).'</td>
		<td style="text-align:right;" >'.formato_money($pre_uni).'</td>
		<td style="text-align:right;" >'.formato_money($val_tot).'</td>
		<td style="text-align:right;" >'.formato_money($pre_tot).'</td>
		
		</tr>
		
		';				
		
		$cont++;
	}
	mysqli_free_result($dts1);	




$html.='</tbody>
</table>
<br />
<br />
<table width="100%;"   >
    <tbody >
    
        <tr  style="border:1px solid #010101" >
            <td style="text-align: left;font-weight:bold;padding:10%;font-size:12px;" width="70%"></td>
            <td style="text-align: left;font-weight:bold;padding:10%;font-size:12px;" width="15%"><strong>OP. GRAVADA</strong></td>
            <td style="text-align: right;padding:15%;font-size:12px" width="15%"><strong>'.$mon.' '.formato_money($com_valven).'</strong></td>
        </tr>  
        
         <tr  style="border:1px solid #010101" >
            <td style="text-align: left;font-weight:bold;padding:10%;font-size:12px;" width="70%"></td>
            <td style="text-align: left;font-weight:bold;padding:10%;font-size:12px;" width="15%"><strong>IGV</strong></td>
            <td style="text-align: right;padding:15%;font-size:12px" width="15%"><strong>'.$mon.' '.formato_money($com_igv).'</strong></td>
        </tr>  
        
         <tr  style="border:1px solid #010101" >
            <td style="text-align: left;font-weight:bold;padding:10%;font-size:12px;" width="70%"></td>
            <td style="text-align: left;font-weight:bold;padding:10%;font-size:12px;" width="15%"><strong>TOTAL</strong></td>
            <td style="text-align: right;padding:15%;font-size:12px" width="15%"><strong>'.$mon.' '.formato_money($com_tot).'</strong></td>
        </tr>  
        
	</tbody>
</table>

<br />
<br />

<div style="border-radius:20%;border:1px solid #E0E0E0;">


';

$dts6=$oComprapago->filtrar($_GET['com_id']);
$num_rows6= mysqli_num_rows($dts6);

if($num_rows6>0){
$html.='
<table width="50%;"  >
    <tbody >
       <tr  style="" >
            <td style="text-align: left;font-weight:bold;padding:10%;font-size:12px;" width="100%"></td>
        </tr> 
        
		
		<tr style="font-size:12px" >
		    <td style="padding:10%;text-align:left;" colspan="2" > INFORMACION DE PAGOS </td>
		</tr>
		
		
		<tr class="" style="font-weight:bold;border:1px solid #010101;background-color:#EEEEEE;padding:15%;font-size:11px;">
            <th style="text-align:right; width:20%;background-color:#EEEEEE;padding:10%;" >FORMA</th>
            <th style="text-align:right; width:60%;background-color:#EEEEEE;padding:10%;" >DETALLE</th>
            <th style="text-align:right; width:20%;background-color:#EEEEEE;padding:10%;" >MONTO</th>
        </tr>
        
        ';
    
    while($dt6 = mysqli_fetch_array($dts6))
	{
	    $i++;
	    
	                if($dt6['tb_formapago_id']==1) $forpag = 'CONTADO ';
					if($dt6['tb_formapago_id']==2) $forpag = 'CREDITO';
					
					
			if($dt6['tb_formapago_id']==2){
				$det = 'A '.$dt6['tb_comprapago_numdia'].' días | Vencimiento: '.mostrarFecha($dt6['tb_comprapago_fecven']);
				
			}
		 if($dt6['tb_formapago_id']==1){
			  if($dt6['tb_modopago_id']==1) $det = 'EFECTIVO';
			  if($dt6['tb_modopago_id']==2) $det = 'DEPOSITO';
			  if($dt6['tb_modopago_id']==3) $det = 'TRANSFERENCIA';
			  if($dt6['tb_modopago_id']==4) $det = 'CHEQUE';
			  if($dt6['tb_modopago_id']==5) $det = 'TARJETA';
			
		}
					
					
        $html.='
        
        <tr style="margin:15%;font-size:10px;border-top:1px solid #E0E0E0;">
		<td style="text-align:right;" >'.$forpag.'</td>
		<td style="text-align:right;" >'.$det.'</td>
		<td style="text-align:right;" >'.formato_money($dt6['tb_comprapago_mon']).'</td>
		
		</tr>
		
        
        ';
	}
		
	
		
		
	$html.='	
		
	</tbody>
</table>
';
}



$html.='

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