<?php
include('../clases/clase.conexion.php');
include('../funciones/variables.php');
include('../funciones/funciones.php');
$var = VarServer("MYSQLI");
$obj = new clsConexion($var[0],$var[1],$var[2],$var[3],$var[4]);
$varFINI = (isset($_GET["fini"]))?$_GET["fini"]:NULL;
$varFFIN = (isset($_GET["ffin"]))?$_GET["ffin"]:NULL;
$varCCLI = (isset($_GET["ccli"]))?$_GET["ccli"]:NULL;
$varFIni = FechaMySql($varFINI);
$varFFin = FechaMySql($varFFIN);

$sql = "call spQuery_Cuentas_Cobrar($varCCLI);";
$obj->AbrirConexion();
$rs = $obj->Query($sql);
$obj->CerrarConexion();
$fila = $obj->FetchArray($rs);
$FechaActual = MostrarFecha();
$html = '
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
	<link rel="STYLESHEET" href="clases/dompdf/www/test/print_static.css" type="text/css" />
	</head>
	
	<body>	
	<div id="body">	
	<div id="section_header">
	</div>	
	<div id="content">	  
	<div class="page" style="font-size: 7pt">
	
<table style="width: 100%;" class="header">
<tr>
<td><h1 style="text-align: left">'.$varSysEmpresa.'</td>
<td><h1 style="text-align: right">ESTADO DE CUENTA</td>
</tr>
</table>
		
<table style="width: 100%; font-size: 8pt;">
<tr>
<td width="40"><strong>Codigo</strong></td>
<td width="5"><strong>:</strong></td>
<td width="200" style="text-align: left">'.$fila["CCLI"].'</td>
<td width="80" style="text-align: right"><strong>Ultima Compra :</strong></td>
<td width="250" >'.$fila["ULT_COMPRA"].'</td>
</tr>
<tr>
<td><strong>Nombre</strong></td>
<td><strong>:</strong></td>
<td style="text-align: left">'.$fila["CLIENTE"].'</td>
<td style="text-align: right"><strong>Ultimo Pago :</strong></td>
<td>'.$fila["ULT_PAGO"].'</td>
</tr>
<tr>
<td><strong>Dirección</strong></td>
<td><strong>:</strong></td>
<td style="text-align: left">'.$fila["DIRECCION"].'</td>
<td style="text-align: right"><strong>Saldo Actual :</strong></td>
<td>'.number_format($fila["SALDO"],"2",".",",").'</td>
</tr>
<tr>
<td colspan="5">Registro de Movimientos del '.$_GET["fini"].' al '.$_GET["ffin"].'</td>
</tr>
</table>		
<table class="change_order_items">
<tbody>
<tr>
<th colspan="9">LISTADO DE MOVIMIENTOS</th>
</tr>
<tr>
<th width="40">Fecha</th>
<th width="80">Tipo Doc</th>
<th width="60">Documento</th>
<th width="180">Descripcion</th>
<th width="50" colspan="2">Cantidad</th>
<th width="30">Venta</th>
<th width="30">Pagos</th>
<th width="30">Saldo</th>
</tr>';
$sql = "call spQuery_Estado_Cuenta($varCCLI,'$varFIni','$varFFin')";
$obj->AbrirConexion();
$rs = $obj->Query($sql);
$obj->CerrarConexion();
$j=0;
$bgcolor1 = 'class="even_row" ';
$bgcolor2 = 'class="odd_row" ';
$varSaldo = 0;
while($fila = $obj->FetchArray($rs))	{
	$j=$j+1;
	$varSaldo = $varSaldo + $fila["VENTA"] - $fila["PAGOS"];
	$html.= ($j%2==0)?'<tr '.$bgcolor1.'>':'<tr '.$bgcolor2.'>';
	$html.= '<td style="text-align:center; white-space: normal">'.$fila[0].'</td>
	<td>'.ucwords(strtolower($fila[1])).'</td>
	<td style="text-align: center">'.strtolower($fila[2]).'</td>
	<td>'.ucwords(strtolower($fila[3])).'</td>
	<td style="text-align: center">';
$html.= (!is_null($fila[4]) && $fila[4]<>0)?number_format($fila[4],"0",".",","):"";
$html.= '</td>
	<td style="text-align: center">';
$html.= (!is_null($fila[5]) && $fila[5]<>0)?number_format($fila[5],"0",".",","):"";
$html.= '</td>
	<td style="text-align: right">';
$html.= (!is_null($fila[6]) && $fila[6]<>0)?number_format($fila[6],"2",".",","):"";
$html.= '</td>
	<td style="text-align: right">';
$html.= (!is_null($fila[7]) && $fila[7]<>0)?number_format($fila[7],"2",".",","):"";
$html.= '</td>
	<td style="text-align: right">'.number_format($varSaldo,"2",".",",").'</td>
	</tr>';
	}
$html.= '
</tbody>
</table>
		
	
		</div>
		
		</div>
		</div>
		
		<script type="text/php">
		
		if ( isset($pdf) ) {
		
		  $font = Font_Metrics::get_font("verdana");;
		  $size = 7;
		  $color = array(0,0,0);
		  $text_height = Font_Metrics::get_font_height($font, $size);
		
		  $foot = $pdf->open_object();
		  
		  $w = $pdf->get_width();
		  $h = $pdf->get_height();
		
		  // Draw a line along the bottom
		  $y = $h - 2 * $text_height - 24;
		  $pdf->line(16, $y, $w - 16, $y, $color, 1);
		
		  $y += $text_height;
		
		  $text = "by KenCitoxD";
		  $pdf->text(16, $y, $text, $font, $size, $color);
		
		  $pdf->close_object();
		  $pdf->add_object($foot, "all");
		
		  global $initials;
		  $initials = $pdf->open_object();
		  
		  // Add an initals box
		  $text = "'.$FechaActual.'";
		  $width = Font_Metrics::get_text_width($text, $font, $size);
		  $pdf->text($w - 16 - $width, $y, $text, $font, $size, $color);
			
		
		  $pdf->close_object();
		  $pdf->add_object($initials);
		
		  $text = "Pagina {PAGE_NUM} de {PAGE_COUNT}";  
		
		  // Center the text
		  $width = Font_Metrics::get_text_width("Pagina 1 de 2", $font, $size);
		  $pdf->page_text($w / 2 - $width / 2, $y, $text, $font, $size, $color);
		  
		}
		</script>
		
		</body>
		</html>';
require_once("../clases/dompdf/dompdf_config.inc.php");

$dompdf = new DOMPDF();
$dompdf->load_html($html);
//$dompdf->set_paper("a4", "landscape");
$dompdf->set_paper("a4", "portrait");
$dompdf->render();
$dompdf->stream("Cuenta Corriente");

?>