<?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]);
$sql = "select date_format(C.fecha_emision,'%d/%m/%Y') AS FECHA, 
		T.nombre as TIPO, C.nro_documento as NUMERO,
		CONCAT(C.id_cliente,' - ', UPPER(P.nombre)) as CLIENTE, upper(P.direccion) AS DIRECCION,
		cv.nombre AS CONDICION
		from _sv_venta C
		inner join _sv_Cliente P ON C.id_Cliente = P.id_Cliente
		inner join _sv_tipo_doc_pago T ON C.id_tipo_doc = T.id_tipo_doc
		inner join _sv_tipo_condicion CV ON C.id_condicion = CV.id_condicion
		where C._estado='A'
		AND C.id_venta=$_GET[ndoc]";
$obj->AbrirConexion();
$rs = $obj->Query($sql);
$obj->CerrarConexion();
$row = $obj->FetchArray($rs);
$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/css/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 width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="49%" valign="top">

<table style="width: 100%;" class="header">
<tr>
<td><h1 style="text-align: left">'.$varSysEmpresa.'</h1></td>
<td><h1 style="text-align: right; font-size: 10pt;">'.$row["NUMERO"].'</h1></td>
</tr>
</table>

<table style="width: 100%; font-size: 8pt;">
<tr>
<td>'.$$varSysRuc.'</td>
<td style="text-align: right">'.$row["TIPO"].'</td>
</tr>
</table>

<table style="width: 100%; font-size: 8pt;">
<tr>
<td><strong>Cliente</strong></td>
<td>:</td>
<td  style="width:230px; font-size: 8pt;">'.$row["CLIENTE"].'</td>
<td>&nbsp;</td>
<td><strong>F. Emisión</strong></td>
<td>: </td>
<td style="text-align: right">'.$row["FECHA"].'</td>
</tr>
<tr>
<td><strong>Dirección</strong></td>
<td>:</td>
<td>'.$row["DIRECCION"].'</td>
<td>&nbsp;</td>
<td><strong>Condicion</strong></td>
<td>: </td>
<td style="text-align: right">'.$row["CONDICION"].'</td>
</tr>
</table>
<table class="change_order_items">
<tbody>
<tr>
<th width="10" style="border: 1px solid black; font-size: 8pt;">Id</th>
<th width="100" style="border: 1px solid black; font-size: 8pt;">Descripcion</th>
<th width="20" style="border: 1px solid black; font-size: 8pt;">Precio</th>
<th width="20" style="border: 1px solid black; font-size: 8pt;">Doc</th>
<th width="20" style="border: 1px solid black; font-size: 8pt;">Uni</th>
<th width="20" style="border: 1px solid black; font-size: 8pt;">Monto</th>
</tr>';
$sql = "select D.id_producto AS CPRO, A.descripcion AS PRODUCTO, D.PRECIO, 
		TRUNCATE(D.cantidad/12,0) AS QCAN_DOC,
		MOD(D.cantidad,12) as QCAN_UNI,
		D.TOTAL
		from _sv_venta_detalle D
		inner join _sv_producto A ON D.id_producto = A.id_producto
		where D.id_venta=$_GET[ndoc]";
$obj->AbrirConexion();
$rs = $obj->Query($sql);
$obj->CerrarConexion();
$j=0;
$bgcolor1 = 'class="even_row" ';
$bgcolor2 = 'class="odd_row" ';
$varTotal = 0;
while($fila = $obj->FetchArray($rs))	{
	$varTotal = $varTotal + $fila["TOTAL"]; 
	$j++;
	$html.= ($j%2==0)?'<tr '.$bgcolor1.'>':'<tr '.$bgcolor2.'>';
	$html.='
		<td style="text-align:center;">'.$fila["CPRO"].'</td>
		<td>'.$fila["PRODUCTO"].'</td>
		<td style="text-align: right">';
	$html.= (!is_null($fila["PRECIO"]) && $fila["PRECIO"]<>0)?number_format($fila["PRECIO"],"2",".",","):"";
	$html.= '&nbsp;</td>
		<td style="text-align: center">';
	$html.= (!is_null($fila["QCAN_DOC"]) && $fila["QCAN_DOC"]<>0)?number_format($fila["QCAN_DOC"],"0",".",","):"";
	$html.= '</td>
		<td style="text-align: center">';
	$html.= (!is_null($fila["QCAN_UNI"]) && $fila["QCAN_UNI"]<>0)?number_format($fila["QCAN_UNI"],"0",".",","):"";
	$html.= '</td>
		<td style="text-align: right">';
	$html.= (!is_null($fila["TOTAL"]) && $fila["TOTAL"]<>0)?number_format($fila["TOTAL"],"2",".",","):"";
	$html.= '&nbsp;</td>
		</tr>';
	}
$html.='
</tbody>
<tr>
<td height="26" colspan="5" style="text-align: right;"><strong> TOTAL : </strong></td>
<td class="change_order_total_col"><strong>'.number_format($varTotal,"2",".",",").'</strong>&nbsp;</td></tr>
</table>
</td>


<td width="2%" valign="top">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td width="50" height="342">&nbsp;</td></tr>
</table>
</td>

<td width="49%" valign="top">
<table style="width: 100%;" class="header">
<tr>
<td><h1 style="text-align: left">'.$varSysEmpresa.'</h1></td>
<td><h1 style="text-align: right; font-size: 10pt;">'.$row["NUMERO"].'</h1></td>
</tr>
</table>

<table style="width: 100%; font-size: 8pt;">
<tr>
<td>'.$$varSysRuc.'</td>
<td style="text-align: right">'.$row["TIPO"].'</td>
</tr>
</table>

<table style="width: 100%; font-size: 8pt;">
<tr>
<td><strong>Cliente</strong></td>
<td>:</td>
<td  style="width:230px; font-size: 8pt;">'.$row["CLIENTE"].'</td>
<td>&nbsp;</td>
<td><strong>F. Emisión</strong></td>
<td>: </td>
<td style="text-align: right">'.$row["FECHA"].'</td>
</tr>
<tr>
<td><strong>Dirección</strong></td>
<td>:</td>
<td>'.$row["DIRECCION"].'</td>
<td>&nbsp;</td>
<td><strong>Condicion</strong></td>
<td>: </td>
<td style="text-align: right">'.$row["CONDICION"].'</td>
</tr>
</table>
<table class="change_order_items">
<tbody>
<tr>
<th width="10" style="border: 1px solid black; font-size: 8pt;">Id</th>
<th width="100" style="border: 1px solid black; font-size: 8pt;">Descripcion</th>
<th width="20" style="border: 1px solid black; font-size: 8pt;">Precio</th>
<th width="20" style="border: 1px solid black; font-size: 8pt;">Doc</th>
<th width="20" style="border: 1px solid black; font-size: 8pt;">Uni</th>
<th width="20" style="border: 1px solid black; font-size: 8pt;">Monto</th>
</tr>';
$sql = "select D.id_producto AS CPRO, A.descripcion AS PRODUCTO, D.PRECIO, 
		TRUNCATE(D.cantidad/12,0) AS QCAN_DOC,
		MOD(D.cantidad,12) as QCAN_UNI,
		D.TOTAL
		from _sv_venta_detalle D
		inner join _sv_producto A ON D.id_producto = A.id_producto
		where D.id_venta=$_GET[ndoc]";
$obj->AbrirConexion();
$rs = $obj->Query($sql);
$obj->CerrarConexion();
$j=0;
$bgcolor1 = 'class="even_row" ';
$bgcolor2 = 'class="odd_row" ';
$varTotal = 0;
while($fila = $obj->FetchArray($rs))	{
	$varTotal = $varTotal + $fila["TOTAL"]; 
	$j++;
	$html.= ($j%2==0)?'<tr '.$bgcolor1.'>':'<tr '.$bgcolor2.'>';
	$html.='
		<td style="text-align:center;">'.$fila["CPRO"].'</td>
		<td>'.$fila["PRODUCTO"].'</td>
		<td style="text-align: right">';
	$html.= (!is_null($fila["PRECIO"]) && $fila["PRECIO"]<>0)?number_format($fila["PRECIO"],"2",".",","):"";
	$html.= '&nbsp;</td>
		<td style="text-align: center">';
	$html.= (!is_null($fila["QCAN_DOC"]) && $fila["QCAN_DOC"]<>0)?number_format($fila["QCAN_DOC"],"0",".",","):"";
	$html.= '</td>
		<td style="text-align: center">';
	$html.= (!is_null($fila["QCAN_UNI"]) && $fila["QCAN_UNI"]<>0)?number_format($fila["QCAN_UNI"],"0",".",","):"";
	$html.= '</td>
		<td style="text-align: right">';
	$html.= (!is_null($fila["TOTAL"]) && $fila["TOTAL"]<>0)?number_format($fila["TOTAL"],"2",".",","):"";
	$html.= '&nbsp;</td>
		</tr>';
	}
$html.='
</tbody>
<tr>
<td height="26" colspan="5" style="text-align: right;"><strong> TOTAL : </strong></td>
<td class="change_order_total_col"><strong>'.number_format($varTotal,"2",".",",").'</strong>&nbsp;</td></tr>
</table>
    </td>
  </tr>
</table>

</div>

</div>
</div>
</body>
</html>';		
require_once("clases/dompdf/dompdf_config.inc.php");
//ini_set("memory_limit", "256M");
  
$dompdf = new DOMPDF();
$dompdf->load_html($html);
//$dompdf->set_paper("a4", "landscape");
$dompdf->set_paper("a4", "landscape");
$dompdf->render();
$dompdf->stream("Documento");
if(isset($_GET[tipo])) {
	header("Location: index.php");
	echo "<script language='Javascript'>";
	//echo "var Ruta = 'print/print_doc.php?ndoc='+".$NumCompra;
	//echo "testwindow= window.open (Ruta, \"mywindow\", \"location=1,status=1,scrollbars=1,width=50,height=50\");";
	echo "window.location='index.php'";
	echo "</script>\n";
	}
?>