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

session_start();

require_once ("../../config/Cado.php");
require_once ("../../app/notacredito/cNotacredito.php");
$oNotacredito = new cNotacredito();
require_once ("../../app/venta/cVenta.php");
$oVenta = new cVenta();
require_once ("../../app/venta/cVentapago.php");
$oVentapago = new cVentapago();
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");

   $notcreid = $_GET['notid'];
    $dts = $oNotacredito->mostrarUno($notcreid);
    while($dt = mysqli_fetch_array($dts)){
    $venid = $dt["tb_venta_id"];
	$notcre_numDoc	=$dt['tb_notacredito_numdoc'];
	$notcre_fec= mostrarFecha($dt['tb_notacredito_fec']);
	$motnotcre= $dt['tb_notacredito_motivo'];
	$notcre_tip= '0'.$dt['tb_notacredito_tip'];
	$notcredigval=$dt['tb_notacredito_digval'];
	$notcretot=$dt['tb_notacredito_tot'];
	$notcreigvsub=$dt['tb_notacredito_igvsub'];
	$notcreigvigv=$dt['tb_notacredito_igvigv'];
    }
   

	$dtsven= $oVenta->mostrarUno_ticket($venid);
	$dtven= mysqli_fetch_array($dtsven);
	//VENTA
	$ven_numDoc	=$dtven['numdoc'];
	$ven_docNom=$dtven['tb_documento_nom'];
	$ven_docAbr=$dtven['tb_documento_abr'];
	$ven_punVen=$dtven['tb_puntoventa_nom'];	
	$ven_venNom=$dtven['tb_usuario_nom'].' '.$dtven['tb_usuario_ape'];
	$ven_fecReg=$dtven['tb_venta_reg'];
	$ven_fec= mostrarFecha($dtven['tb_venta_fec']);
	$ven_est=$dtven['tb_venta_est'];
	$ven_valven=$dtven['tb_venta_valven'];
	$ven_igv=$dtven['tb_venta_igv'];
	$ven_tot=$dtven['tb_venta_tot'];
	$ven_pisNom=$dtven['tb_piso_nom'];
	//CLIENTE
	$cliNom=$dtven['tb_cliente_nom'];
	$cliDoc=$dtven['tb_cliente_doc'];
	$cliDir=$dtven['tb_cliente_dir'];
	$vendigval=$dtven['tb_venta_digval'];
	$docabr=$dtven['tb_documento_abr'];
	$monid=$dtven['moneid'];
	$empid=$dtven['tb_empresa_id'];
	
	mysqli_free_result($dtsven);
	
	if($monid==1){
		$mon ="S/ ";
		$lblmon="SOLES";
	}else if($monid==2)
	{
		$lblmon="DOLARES AMERICANOS";
		$mon ="US$ ";
	}
	

		$tipodoc = 'NOTA DE CREDITO';
		$tipcom="07";
	
	
	
$ser =  substr($notcre_numDoc,0,4); 
$corr =  substr($notcre_numDoc,5,13); 
	
$cont = strlen($cliDoc);

if($cont==11){
	$tipdoc = '06';
}else if($cont==8){
	$tipdoc = '01';
}
	
	if($ven_docAbr=='B'){
		$nomdoc = "BOLETA";
	}else if($ven_docAbr=='F'){
		$nomdoc = "FACTURA";
	}
	
$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);
	
/* if($ven_est=='0'){
		$anular = '
		<tr>
		<td width="50%"></td>
	    <td width="13%"></td>
	    <td td width="37%" style="text-align: center"><strong style="color:red;font-size:15pt;" >ANULADO</strong></td>
		</tr>
		';
}else{
	$anular = '';
} */
$anular = '';
	
$title = "Representacion de ".$notcre_numDoc;
$nombre_archivo = $emp_ruc.'_'.$notcre_numDoc.'_'.$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;
        background-color: #f6f6f6;
        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="20%">
	<img src="../../imagenes/logo1.png" alt="" />
	</td>
        <td style="text-align:center;" width="50%">
		<br />
		<br />
            <strong style="font-size:35px">'.$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">'.$tipodoc.'<br>
            RUC: '.$emp_ruc.'<br>
            '.$notcre_numDoc.'</strong>
			  <br>
        </td>
	   
    </tr>

</table>

<br />
<br />

<table style="border-collapse: collapse;">
    <tr style="border:1px solid #010101" >
	
        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" width="12%">SEÑOR(ES)</td>
        <td style="text-align: left;border:1px solid #010101;padding:15%;font-size:30px" width="55%">'.$cliNom.'</td>

		<td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" width="15%">FECHA EMISION</td>
        <td style="text-align: left;border:1px solid #010101;padding:15%;font-size:30px" width="18%">'.$notcre_fec.'</td>
		
		
    </tr>
    <tr style="border:1px solid #010101" >
        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" width="12%">RUC</td>
        <td style="text-align: left;border:1px solid #010101;padding:15%;font-size:30px" width="55%">'.$cliDoc.'</td>
		
		<td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" width="15%">MONEDA</td>
        <td style="text-align: left;border:1px solid #010101;padding:15%;font-size:30px" width="18%">'.$lblmon.'</td>
        
    </tr>
    <tr>
        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;" width="12%">DIRECCIÓN</td>
        <td style="text-align: left;border:1px solid #010101;padding:15%;font-size:30px" width="88%">'.$cliDir.'</td>
    </tr> 
	<tr>
        <td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:40px;" width="12%">MOTIVO</td>
        <td style="text-align: left;border:1px solid #010101;padding:15%;font-size:40px" width="36%">'.$motnotcre.'</td>
		
		<td style="text-align: left;font-weight:bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:40px;" width="30%">'.$nomdoc.' ELECTRONICA</td>
        <td style="text-align: left;border:1px solid #010101;padding:15%;font-size:40px" width="22%">'.$ven_numDoc.'</td>		
    </tr>
</table>

<br />
<br />

<table 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:5%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >ITEM</th>
            <th style="text-align:center; width:47%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >DESCRIPCION</th>
            <th style="text-align:center; width:18%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >MARCA</th>
            <th style="text-align:center; width:15%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >UNIDAD</th>
            <th style="text-align:center; width:5%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >CAN</th>
            <th style="text-align:center; width:10%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >PRE. UNI.</th>
        </tr>
';
if($notcre_tip=='01' || $notcre_tip=='06' || $notcre_tip=='07'){
$dtsnotdet = $oNotacredito->mostrar_detalle($notcreid);
$num_rows= mysqli_num_rows($dtsnotdet);
if($num_rows>=1){
$i =1;
	while($dt1 = mysqli_fetch_array($dtsnotdet))
	{
		
		$can = $dt1["tb_notacreditodetalle_can"] * 1;
		$can1 = $dt1['tb_notacreditodetalle_can']*1;
		
		$dts2=$oCatalogo->mostrarUno($dt1['tb_catalogo_id'],$dt1['tb_marca_id']);
		$dt2 = mysqli_fetch_array($dts2);
			
		$arttip_nom =  $dt2['tb_articulotipo_nom'];
		$emp_fac =  $dt2['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;
			}
			
		
		
		
		
		
		
		
	$html.='
		<tr style="border:1px solid #010101;padding:10%;font-size:30px;">
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong>'.$i.'</strong> </td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong>'.$dt1["tb_producto_nom"].'</strong> </td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong>'.$dt1["tb_marca_nom"].'</strong> </td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong>'.$dt1["tb_unidad_abr"].'</strong> </td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong>'.$can.'</strong> </td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong>'.formato_money($dt1["tb_notacreditodetalle_preuni"]) .'</strong> </td>
	</tr>
		';
	$i ++;
	}
	
	$tot += ($dt1["tb_notacreditodetalle_valuni"] * $notcreigvsub) * $dt1["tb_notacreditodetalle_can"];
}
}else if($notcre_tip=='04'){
	
	$valuni = $notcretot;
	
	$html.='
		<tr style="border:1px solid #010101;padding:10%;font-size:30px;">
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong>1</strong> </td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong>DESCUENTO GLOBAL</strong> </td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong></strong> </td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong></strong> </td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong></strong> </td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong>'.formato_money($valuni).'</strong> </td>
	</tr>
		';

}else if($notcre_tip=='05'){
	
	$valuni = $notcretot;
	
	$html.='
		<tr style="border:1px solid #010101;padding:10%;font-size:30px;">
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong>1</strong> </td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong>DESCUENTO POR ITEM</strong> </td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong></strong> </td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong></strong> </td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong></strong> </td>
		<td style="border-right: 0.9px solid #010101;border-left: 0.9px solid #010101;" > <strong>'.formato_money($valuni).'</strong> </td>
	</tr>
		';

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

<table style="margin-right:15px;" width="100%;" style="border-collapse: collapse;"  >
    <tbody >
        <tr class="" style="font-weight:bold;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;font-size:30px;">
            <th width="33.3%"  style="text-align:center; width:20%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >VALOR TOTAL</th>
            <th width="33.3%"  style="text-align:center; width:20%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >TOTAL IGV (18%)</th>
            <th width="33.3%"  style="text-align:center; width:20%;bold;border:1px solid #010101;background-color:#bdbdbd;padding:10%;" >TOTAL DE VENTA</th>
        </tr>
		
		<tr style="border:1px solid #010101;padding:10%;font-size:30px;">		
		<td style="border:1px solid #010101;padding:10%;text-align:right;" ><strong>'.$mon.' '.formato_money($notcretot/$notcreigvsub).'</strong></td>
		<td style="border:1px solid #010101;padding:10%;text-align:right;" ><strong>'.$mon.' '.formato_money(($notcretot/$notcreigvsub)*$notcreigvigv).'</strong></td>
		<td style="border:1px solid #010101;padding:10%;text-align:right;" ><strong>'.$mon.' '.formato_money($notcretot).'</strong></td>		
		</tr>
		
		<tr style="font-size:35px" >
		<td colspan="3" style="border:1px solid #010101;padding:10%;text-align:left;" >SON '.numtoletras($notcretot).' '.$lblmon.' 
		</td>
		</tr>
	
	</tbody>
</table>


<br />
<br />

<br/>

<table>
<tr>
<td width="75%;">
<br />
<br />
<br />
<p style="font-size:10pt">
Código de Seguridad (Hash): '.$notcredigval.'<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>
<td>
';
$style = array(
	'border' => 2,
	'vpadding' => 'auto',
	'hpadding' => 'auto',
	'fgcolor' => array(0,0,0),
	'bgcolor' => false, //array(255,255,255)
	'module_width' => 1, // width of a single module in points
	'module_height' => 1 // height of a single module in points
);

$params = $pdf->serializeTCPDFtagParameters(array($emp_ruc.'|'.$tipcom.'|'.$ser.'|'.$corr.'|'.$ven_igv.'|'.$ven_tot.'|'.$notcre_fec.'|'.$tipdoc.'|'.$cliDoc.'|', 'QRCODE,Q', '', '', 40, 40, $style, 'N'));
$html .= '<tcpdf method="write2DBarcode" params="'.$params.'" />
</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, 'I');
