<?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]);
	$varTitu = "RECHAZO DE VENTAS POR CLIENTE";
	$varFIni = ($_GET["fini"]=="") ? "NULL" : "'".FechaMySql($_GET["fini"])."'";
	$varFFin = ($_GET["ffin"]=="") ? "NULL" : "'".FechaMySql($_GET["ffin"])."'";
			
	$varVenAnt = $varVenAct = $varRutAnt = $varRutAct = "";

	$html = '
		<!DOCTYPE html  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
		<head>
		<title>'.$varTitu.'</title>
		<link rel="STYLESHEET" href="../css/print_avance_ventas.css" type="text/css" />
		</HEAD>
		<body class="page">';
		
	$html.='<table style="width: 98%; padding: 0; margin-top:10px; margin-bottom:10px; border-collapse:collapse;" align="center">';
	$html.='<thead>';
	$html.='<tr>';
	$html.='<td>';
		$html.='<table style="width: 100%;">';
		$html.='<tr>';
		$html.='<td style="width: 9%; vertical-align: middle; text-align:left;"><b>Fecha</b></td>';
		$html.='<td style="width: 1%; vertical-align: middle; text-align:center;"><b>:</b></td>';
		$html.='<td style="width: 40%; vertical-align: middle; text-align:left;"><b>';
		$html.=($_GET[fini]=="")?"Todo":$_GET[fini];
		$html.=' - ';
		$html.=($_GET[ffin]=="")?"Todo":$_GET[ffin];
		$html.='</b></td>';
		$html.='<td style="width: 50%; vertical-align: middle; text-align:right;"><b>'.$varSysEmpresa.'</b></td>';
		$html.='</tr>';
		$html.='<tr>';
		$html.='<td style="vertical-align: middle; text-align:left;"><b>Vendedor</b></td>';
		$html.='<td style="vertical-align: middle; text-align:center;"><b>:</b></td>';
		$html.='<td style="vertical-align: middle; text-align:left;"><b>';
		if ($_GET[vcod]<>"") {
			$sqlv = "select id_vendedor, nombre from _sv_vendedor where id_vendedor=".$_GET[vcod];
			$obj->AbrirConexion();
			$rsv = $obj->Query($sqlv);
			$obj->CerrarConexion();
			$filav = $obj->FetchArray($rsv);
			$html.= $filav[0].' - '.$filav[1];
			}
		else {
			$html.= 'TODAS LOS VENDEDORES';
			}
		$html.'</b></td>';
		$html.='<td style="vertical-align: middle; text-align:right;"><b>'.$varTitu.'</b></td>';
		$html.='</tr>';
		$html.='<tr>';
		$html.='<td style="vertical-align: middle; text-align:left;"><b>Cliente</b></td>';
		$html.='<td style="vertical-align: middle; text-align:center;"><b>:</b></td>';
		$html.='<td style="width: 50%; vertical-align: middle; text-align:left;"><b>';
		if ($_GET[ccod]<>"") {
			$sqlp = "select id_cliente, nombre from _sv_cliente where id_cliente=".$_GET[ccod];
			$obj->AbrirConexion();
			$rsp = $obj->Query($sqlp);
			$obj->CerrarConexion();
			$filap = $obj->FetchArray($rsp);
			$html.= $filap[0].' - '.$filap[1];
			}
		else {
			$html.= 'TODOS LOS CLIENTES';
			}
		$html.='</b></td>';
		$html.='<td style="width: 50%; vertical-align: middle; text-align:right;">'.MostrarFechaActual().'</td>';
		$html.='</tr>';
		$html.='</table>';
	$html.='</td>';
	$html.='</tr>';
	$html.='</thead>';	
	$html.='<tr>';
	$html.='<td>';
		$html.='<table style="width: 100%;padding: 0; margin-top:0px; margin-bottom:0px; border-collapse:collapse;">';
		$html.='<thead>';
		$html.='<tr class="head">';
		$html.='<td width="2" class="center">&nbsp;</td>';
		$html.='<td width="2" class="center">CCLI</td>';
		$html.='<td width="150">CLIENTE</td>';
		$html.='<td width="100" class="center">RUTA</td>';
		$html.='<td width="60" class="center">FEMI</td>';
		$html.='<td width="60" class="center">DOCUMENTO</td>';
		$html.='<td width="60" class="center">TIPO</td>';
		$html.='<td width="60" class="center">MOTIVO</td>';
		$html.='<td width="60" class="right">TOTAL</td>';
		$html.='<td width="60" class="right">RECHAZO</td>';
		$html.='<td width="60" class="right">EFECTIVO</td>';
		$html.='</tr>';
		$html.='</thead>';
		$varFIni = (isset($_GET["fini"]))?FechaMySql($_GET["fini"]):NULL;
		$varFFin = (isset($_GET["ffin"]))?FechaMySql($_GET["ffin"]):NULL;
		
		$sql = "call spQuery_Avance_Rebotes_Vendedor('$varFIni','$varFFin','$_GET[vcod]', '$_GET[ccod]')";
		$obj->AbrirConexion();
		$rs = $obj->Query($sql);
		$obj->CerrarConexion();
		$varTotal_1 = 0;
		$varTotal_2 = 0;
		$varTotal_3 = 0;
		$varNumDoc = 0;
		while($fila = $obj->FetchArray($rs))	{
			$varTotal_1 = $varTotal_1 + $fila["TOTAL_GRAL"];
			$varTotal_2 = $varTotal_2 + $fila["RECHAZADO"];
			$varTotal_3 = $varTotal_3 + $fila["EFECTIVO"];
			$varNumDoc++;
			$html.='<tr class="list_row">';
			$html.='<td class="center">&nbsp;</td>';
			$html.='<td class="center">'.$fila["CCLI"].'</td>';
			$html.='<td NOWRAP>'.$fila["CLIENTE"].'</td>';
			$html.='<td NOWRAP>'.$fila["CRUT"].' - '.$fila["RUTA"].'</td>';
			$html.='<td class="center">'.$fila["FEMI"].'</td>';
			$html.='<td class="center" NOWRAP>'.$fila["CTIP"].' '.$fila["NDOC"].'</td>';
			$html.='<td NOWRAP>'.$fila["ESTADO"].'</td>';
			$html.='<td NOWRAP>'.$fila["CMOT"].' - '.$fila["MOTIVO"].'</td>';
			$html.='<td class="right">'.number_format($fila["TOTAL_GRAL"],"2",".",",").'</td>';
			$html.='<td class="right">'.number_format($fila["RECHAZADO"],"2",".",",").'</td>';
			$html.='<td class="right">'.number_format($fila["EFECTIVO"],"2",".",",").'</td>';
			$html.='</tr>';
			}
		$html.='<tr class="list_foot">';
		$html.='<td class="left"></td>';
		$html.='<td class="left"></td>';
		$html.='<td class="left" colspan="6">'.number_format($varNumDoc,"0",".",",").' Registros</td>';
		$html.='<td class="right">'.number_format($varTotal_1,"2",".",",").'</td>';
		$html.='<td class="right">'.number_format($varTotal_2,"2",".",",").'</td>';
		$html.='<td class="right">'.number_format($varTotal_3,"2",".",",").'</td>';
		$html.='</tr>';

		$html.='</table>';
	$html.='</td>';
	$html.='</tr>';
			
$html.='</table>		
		</body>
		</html>';
	echo $html;
?>