<?php

// ini_set('display_errors',1);

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

session_start();
date_default_timezone_set('America/Lima');
require_once ("../../config/Cado.php");
require_once ("../../app/cuentapagar/cCuentapagar.php");
$oCuentapagar = new cCuentapagar();
require_once ("../../app/proveedor/cProveedor.php");
$oProveedor = new cProveedor();
require_once ("../../app/empresa/cEmpresa.php");
$oEmpresa = new cEmpresa();
require_once ("../../app/puntoventa/cPuntoventa.php");
$oPuntoventa = new cPuntoventa();
require_once ("../../app/cajageneral_cst/cCajageneral_cst.php");
$oCajageneral_cst = new cCajageneral_cst();

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

$dtsid=$oEmpresa->mostrarUno(1);
$dtid = mysqli_fetch_array($dtsid);
	$emp_rucid=$dtid['tb_empresa_ruc'];
	$emp_nomcom=$dtid['tb_empresa_nomcom'];
	$emp_razsoc=$dtid['tb_empresa_razsoc'];
	$emp_dirid=$dtid['tb_empresa_dir'];
	$emp_dir2id=$dtid['tb_empresa_dir2'];
	$emp_telid=$dtid['tb_empresa_tel'];
	$emp_emaid=$dtid['tb_empresa_ema'];
	$emp_firid=$dtid['tb_empresa_fir'];		
mysqli_free_result($dtsid);

$punven_id = $_GET['punven_id'];

$dts=$oPuntoventa->mostrarUno($_GET['punven_id']);
$dt = mysqli_fetch_array($dts);
$punven_nom = $dt['tb_puntoventa_nom'];
$punven_cod = $dt['tb_puntoventa_cod'];
$alm_id = $dt['tb_almacen_id'];
$caj_id = $dt['tb_caja_id'];
$imp_id = $dt['tb_impresora_id'];
$emp_id = $dt['tb_empresa_id'];
mysqli_free_result($dts);


$fec=date('d-m-Y');
	
$title = "RENDICION CAJA - ".$fec;
$nombre_archivo = 'RENDICION CAJA  '.$fec.'.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(-12);
      // 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->Cell(0, 0, 'www.radiadorespenarrieta.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');
$pdf->SetTitle($title);

$pdf->SetSubject('www.infosisperu.com');
$pdf->SetKeywords('www.infosisperu.com');

// 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(5, 2, 5);// 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;
        font-size: 7.5pt;
    }
    .header_row td{
		font-size:28px;
		background-color:#9E9E9E;
        border-bottom: 0.9px solid #010101;
        border-top: 0.9px solid #010101;
    } 
	
	.header_row2 td{
		font-size:10pt;
		
        border-top: 0.9px solid #010101;
		padding-top: 5px;
        padding-bottom: 5px;
    }
    .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;
    }
	
	
	
	div#div_tabla {  }
	div#div_tabla table { border-collapse: collapse; width: 100%; }
	div#div_tabla table td, div#div_tabla table th { border: 1px solid #eee;font-family:arial; font-size:28px; }
	div#div_tabla table th { height:18px;font-size:35px; }
	div#div_tabla table td { height:17px;font-size:35px; }
	
</style>

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

</style>
<body>


<table style="width: 100%; margin-bottom: 10px">
	<tr>
		<td width="15%">
			<img src="../../imagenes/logo.png" alt="" />
		</td>
        <td style="text-align:center;" width="85%">
         
            <strong style="font-size:35px">'.$emp_razsocid.'</strong>
			<br>
            '.$emp_dirid.'<br />   <strong>Telf.: '.$emp_telid.'  E-mail.: '.$emp_emaid.'</strong> 
        
		</td>
    </tr>
</table>

<br />
<br />



<table id="tabla_resumen_todos" width="100%" class="">
        <tbody>	
		
		<tr width="100%" class="" >		
					<td width="20%" style="font-size:10pt;" align="left" >DESDE</td>													
					<td width="20%" style="font-size:10pt;" align="left" >HASTA</td>																								
					<td width="60%" style="font-size:10pt;" align="left" >PUNTO VENTA</td>																								
		</tr>
		
		<tr width="100%" class="" >		
					<td width="20%" style="font-size:10pt;" align="left" >'.$_GET['fec1'].'</td>																								
					<td width="20%" style="font-size:10pt;" align="left" >'.$_GET['fec2'].'</td>																								
					<td width="60%" style="font-size:10pt;" align="left" >'.$emp_nomcom.' - '.$punven_nom.'</td>																								
		</tr>
		
		</tbody>		
</table> 
		

<table id="tabla_resumen_todos" width="100%" class="">
        <tbody>					
				<tr width="100%" class="" >		
					<td  style="font-size:10pt;" align="center" ></td>																											
				</tr> 
			
				
				<tr width="100%" class="" >		
					<td  style="font-size:10pt;" align="center" ></td>																											
				</tr> 
			
				<tr width="100%" class="" >		
					<td width="100%" style="font-size:13pt;border-right: 5px #FF1744;font-weight:bold; " align="center" >VENTAS</td>														
				</tr> 
				
				<tr width="100%" class="" >		
					<td  style="font-size:10pt;" align="center" ></td>																											
				</tr> 
				
				';
				
				
				$dts7=$oCajageneral_cst->mostrar_ventas_agrupformapago(fecha_mysql($_GET['fec1']),fecha_mysql($_GET['fec2']),$punven_id);
						$num_rows7= mysqli_num_rows($dts7);
						
					
						if($num_rows7>0){	
						while($dt7 = mysqli_fetch_array($dts7))
						{		
							
							 if($dt7['tb_formapago_id']==1){
								$forpag = 'CONTADO';
							}else if($dt7['tb_formapago_id']==2){
								$forpag = 'CREDITO';
							}
							
							$html.='<tr width="100%" style=" " >		
									<td width="80%" style="font-size:13pt;border-bottom:1px #000 solid;" align="center" >'.$forpag.' </td>																								
									<td width="20%" style="font-size:15pt;border-bottom:1px #000 solid;" align="right" >'.$dt7['tb_moneda_sim'].' '.formato_money($dt7['sumtotvenpv']).'</td>																																													
							</tr>';	
						
						
						$dts8=$oCajageneral_cst->mostrar_ventas_agrupadopormodo(fecha_mysql($_GET['fec1']),fecha_mysql($_GET['fec2']),$punven_id,$dt7['tb_formapago_id'],$dt7['tb_moneda_id']);
						while($dt8 = mysqli_fetch_array($dts8))
						{
							$modpag = '';
						 
							if($dt8['tb_modopago_id']==1){
							 $modpag = 'EFECTIVO ';
							}else if($dt8['tb_modopago_id']==2){
							 $modpag = 'DEPOSITO ';
							}else if($dt8['tb_modopago_id']==4){
							 $modpag = 'TRANSFERENCIA ';
							}else if($dt8['tb_modopago_id']==5){
							 $modpag = 'CHEQUE ';
							}else if($dt8['tb_modopago_id']==6){
							 $modpag = 'TARJETA ';
							} 
												
							$html.='<tr width="100%" class="" >		
									<td width="80%" style="font-size:12pt;padding-right:12pt;" align="right" > <span  >'.$modpag.'</span>  </td>																								
									<td width="20%" style="font-size:13pt;border-left:1px #000 solid;" align="right" >'.$dt7['tb_moneda_sim'].' '.formato_money($dt8['totalmonto']).'</td>																																													
							</tr>';
							
						$dts10=$oCajageneral_cst->mostrar_ventas_agrupadopordocumento_fecha(fecha_mysql($_GET['fec1']),fecha_mysql($_GET['fec2']),
						$punven_id,$dt7['tb_formapago_id'],$dt7['tb_moneda_id'],$dt8['tb_modopago_id']);
						while($dt10 = mysqli_fetch_array($dts10))
						{
							
							$html.='<tr width="100%" class="" >		
									<td width="80%" style="font-size:10pt;padding-right:12pt;" align="right" > <span style="font-weight:bold;" >'.$dt10['tb_documento_nom'].'   '.'</span>  </td>																								
									<td width="20%" style="font-size:10pt;font-weight:bold;" align="right" >'.$dt7['tb_moneda_sim'].' '.formato_money($dt10['totalmonto']).'</td>																																													
							</tr>';
							
						}
						
						
						}
						
						
						
												
						}
						
						}else{
							$html.='<tr width="100%" class="" >		
									<td  style="font-size:10pt;" align="center" >SIN DATOS</td>																								
								</tr>';	
						}
				
		
		
$html.='
	</tbody>		
</table> 
';


$html.='
<br />
<br />
<br />

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

