<?php
//include "clase.conexion.php";
class clsPreventa_Reporte extends clsConexion{
	public $varTitulo = "REPORTE DE PREVENTA DIARIO";
	public $varMyTabla  = "_sv_pedido";
	public $varMyID  	  = "id_pedido";
	function FormuBusqueda() {
		$sqlven = "call spQuery_Lista_Vendedor('','','');";
		$sqlrut = "call spQuery_Lista_Ruta('','%');";
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"620", "clase"=>"FormTitulo", "titulo"=>$this->varTitulo." - BUSQUEDA",
						"formu"=>array("id"=>"form", "name"=>"form", "metodo"=>"POST", "onsubmit"=>"return false;"),						
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$this->varTitulo),
									   array("tipo"=>"oculto", "id"=>"opt", "name"=>"opt", "valor"=>$Opcion)
									  ),
						"conte"=>array(array("titulo"=>"Vendedor :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo"=>array("tipo"=>"lista_doble", "filas"=>8, "ancho"=>"230", "sql"=>$sqlven,
											 				"id1"=>"ven", "name1"=>"ven", "id2"=>"vcod", "name2"=>"vcod",
															"dclick1"=>"mover_lista('right','ven','vcod')", "dclick2"=>"mover_lista('left','ven','vcod')"															
															)
											),									   
									   array("titulo"=>"Ruta :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo"=>array("tipo"=>"lista_doble", "filas"=>8, "ancho"=>"230", "sql"=>$sqlrut,
											 				"id1"=>"rut", "name1"=>"rut", "id2"=>"rcod", "name2"=>"rcod",
															"dclick1"=>"mover_lista('right','rut','rcod')", "dclick2"=>"mover_lista('left','rut','rcod')"															
															)
											),	
									   array("titulo"=>"Fecha Inicial :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"fini", "name"=>"fini", "alinear"=>"center",
																					 "ancho"=>"130", "valor"=>$varFINI)
																			   )
														 	)
											 ),
									   array("titulo"=>"Fecha Final :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"ffin", "name"=>"ffin", "alinear"=>"center", 
																					 "ancho"=>"130", "valor"=>$varFFIN)
																			   )
														 	)
											 )
									 ),
					  "boton"=>array(array("tipo"=>"submit", "id"=>"bcon", "name"=>"bcon", "ancho"=>"100", "texto"=>"Buscar",
					  					   "clase"=>"boton_buscar", "click"=>"javascript:xBuscarJS()"),
									 array("tipo"=>"button", "id"=>"bexp", "name"=>"bexp", "ancho"=>"100", "texto"=>"Exportar",
									 	   "clase"=>"boton_exportar", "click"=>"javascript:xExportarJS()"),								
									 array("tipo"=>"button", "id"=>"bimp", "name"=>"bimp", "ancho"=>"100", "texto"=>"Imprimir", 
										   "clase"=>"boton_imprimir", "click"=>"javascript:xImprimirJS('', 'NEW')")
									 )
					);
		return $this->setTablaForm($varTabla);
		}
 	
	function GridDatos($varVen	 = NULL, 
					   $varRut	 = NULL, 
					   $varIni	 = NULL,  
					   $varFin	 = NULL, 
					   $varField = NULL, 
					   $varOrder = NULL
					   ) {
		$varVen = ($varVen<>"")?"\"(".$varVen.")\"":"NULL";
		$varRut = ($varRut<>"")?"\"(".str_replace("\\","",$varRut).")\"":"NULL";
		$varIni = (isset($varIni) && $varIni<>'')? "'".FechaMySql($varIni)."'":"NULL";
		$varFin = (isset($varFin) && $varFin<>'')? "'".FechaMySql($varFin)."'":"NULL";
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";
		
		$sql = "call spQuery_Preventa_Reporte($varVen, $varRut, $varIni, $varFin, $varFie, $varOrd);";
		
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"900", "sql_cab"=>$sql, "pages"=>50,
						"conte"=>array(array("tamano"=>10, "titulo"=>"CVEN", "alinear"=>"center"),
									   array("tamano"=>100, "titulo"=>"VENDEDOR.", "fijo"=>"SI"),
									   array("tamano"=>10, "titulo"=>"CRUT", "alinear"=>"center"),
									   array("tamano"=>100, "titulo"=>"RUTA", "fijo"=>"SI"),
									   array("tamano"=>60, "titulo"=>"COBERTU", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>60, "titulo"=>"PUEBLOS", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>60, "titulo"=>"MERCADO", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>60, "titulo"=>"MAYORIS", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>60, "titulo"=>"OTROS", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>60, "titulo"=>"TOTAL", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>60, "titulo"=>"PEDIDOS", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>60, "titulo"=>"PDV", "alinear"=>"right", "funcion"=>array("autosuma")),
									   )
						);
		return $this->SetGridTabla2($varTabla);
		}
	
	function Imprimir ($varVen	 = NULL, 
					   $varRut	 = NULL, 
					   $varIni	 = NULL,  
					   $varFin	 = NULL
					   ) {
		
		// Listamos Vendedores seleccionados
		if ($varVen == "") {
			$varCadVen = "Todos - TODOS LOS VENDEDORES";
			}
		else {
			$varVend = "(".$varVen.")";
			$sql_ven = "select id_vendedor, nombre from _sv_vendedor where id_vendedor in ".$varVend;
			$this->AbrirConexion();
			$rs_ven = $this->Query($sql_ven);
			$this->CerrarConexion();
			while($fila_ven = $this->FetchArray($rs_ven)) {
				if ($varCadVen=="")	$varCadVen = $fila_ven[0]." - ".$fila_ven[1];
				else $varCadVen.= " / ".$fila_ven[0]." - ".$fila_ven[1];			
				}
			}
		
		// Listamos Rutas seleccionados
		if ($varRut == "") {
			$varCadRut = "Todos - TODAS LAS RUTAS";
			}
		else {
			$varRuta = "(".str_replace("\\","",$_GET["rcod"]).")";
			$sql_rut = "select id_ruta, nombre from _sv_ruta where id_ruta in ".$varRuta;
			$this->AbrirConexion();
			$rs_rut = $this->Query($sql_rut);
			$this->CerrarConexion();
			$varCadRut = "";
			while($fila_rut = $this->FetchArray($rs_rut)) {
				if ($varCadRut=="")	$varCadRut = $fila_rut[0]." - ".$fila_rut[1];
				else $varCadRut.= " / ".$fila_rut[0]." - ".$fila_rut[1];			
				}
			}
		$varCli = (isset($varCli) && $varCli<>'')? $varCli:"NULL";
		$varVen = ($varVen<>"")?"\"(".$varVen.")\"":"NULL";
		$varRut = ($varRut<>"")?"\"(".str_replace("\\","",$varRut).")\"":"NULL";
		$varTip = (isset($varTip) && $varTip<>'')? $varTip:"NULL";
		$varFecha = $varIni." - ".$varFin;
		$varIni = (isset($varIni) && $varIni<>'')? "'".FechaMySql($varIni)."'":"NULL";
		$varFin = (isset($varFin) && $varFin<>'')? "'".FechaMySql($varFin)."'":"NULL";
		
		$sql = "call spQuery_Preventa_Reporte($varVen, $varRut, $varIni, $varFin, 'CVEN, CRUT', NULL);";
		
		$html.='<table style="width: 95%; 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: 50%; vertical-align: middle; text-align:left;"><b>'.Config("SYSEMP").'</b></td>';
			$html.='<td style="width: 50%; vertical-align: middle; text-align:right;"><b>'.$this->varTitulo.'</b></td>';
			$html.='</tr>';
			$html.='<tr>';
			$html.='<td style="width: 50%; vertical-align: middle; text-align:left;"><b>Vendedor(es) :</b> '.$varCadVen.'</td>';
			$html.='<td style="width: 50%; vertical-align: middle; text-align:right;"><b>'.MostrarFechaActual().'</b></td>';
			$html.='</tr>';
			$html.='<tr>';
			$html.='<td style="width: 50%; vertical-align: middle; text-align:left;"><b>Ruta(s) :</b> '.$varCadRut.'</td>';
			$html.='<td style="width: 50%; vertical-align: middle; text-align:right;"><b></b></td>';
			$html.='</tr>';
			$html.='<tr>';
			$html.='<td style="width: 50%; vertical-align: middle; text-align:left;"><b>Fecha :</b> '.$varFecha.'</td>';
			$html.='<td style="width: 50%; vertical-align: middle; text-align:right;"><b></b></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">CVEN</td>';
			$html.='<td width="150">VENDEDOR</td>';
			$html.='<td width="30"" class="right">COBERT</td>';
			$html.='<td width="30"" class="right">PUEBLOS</td>';
			$html.='<td width="30"" class="right">MERCADO</td>';
			$html.='<td width="50"" class="right">MAYORIS</td>';
			$html.='<td width="50"" class="right">OTROS</td>';
			$html.='<td width="25" class="right">TOTAL</td>';
			$html.='<td width="25" class="right">PEDIDOS</td>';
			$html.='<td width="40" class="right">PDV</td>';
			$html.='</tr>';
			$html.='</thead>';
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			for($i=1;$i<=8;$i++) {
					$varTotal[$i] = 0;
					}
			while($fila = $this->FetchArray($rs))	{
				for($i=1;$i<=8;$i++) {
					$varTotal[$i] = $varTotal[$i] + $fila[$i+3];
					}
				$varTotal1 = $varTotal1 + $fila["TOTAL"];
				$varTotal2 = $varTotal2 + $fila["SALDO"];
				$varVenAct = $fila["CVEN"];
				$varNumDoc++;
				if($varVenAct <> $varVenAnt) {
					$varVenAnt = $fila["CVEN"];
					$html.='<tr class="list_row">';
					$html.='<td class="right"><b>'.$fila["CVEN"].'</b></td>';
					$html.='<td colspan="11"><b>'.$fila["VENDEDOR"].'</b></td>';
					$html.='</tr>';
					}
				$html.='<tr class="list_row">
						<td class="center">&nbsp;</td>
						<td class="center">'.$fila["CRUT"].'</td>
						<td NOWRAP>'.substr($fila["RUTA"],0,42).'</td>
						<td class="right">';
				$html.=($fila["PREV_COBER"]<>0)?number_format($fila["PREV_COBER"],"2",".",","):"";
				$html.='</td>';
				$html.='<td class="right">';
				$html.=($fila["PREV_PUEBL"]<>0)?number_format($fila["PREV_PUEBL"],"2",".",","):"";
				$html.='</td>';
				$html.='<td class="right">';
				$html.=($fila["PREV_MERCA"]<>0)?number_format($fila["PREV_MERCA"],"2",".",","):"";
				$html.='</td>';
				$html.='<td class="right">';
				$html.=($fila["PREV_MAYOR"]<>0)?number_format($fila["PREV_MAYOR"],"2",".",","):"";
				$html.='</td>';
				$html.='<td class="right">';
				$html.=($fila["PREV_OTRO"]<>0)?number_format($fila["PREV_OTRO"],"2",".",","):"";
				$html.='</td>';
				$html.='<td class="right">';
				$html.=($fila["PREV_TOTAL"]<>0)?number_format($fila["PREV_TOTAL"],"2",".",","):"";
				$html.='</td>';
				$html.='<td class="right">';
				$html.=($fila["PEDIDOS"]<>0)?number_format($fila["PEDIDOS"],"0",".",","):"";
				$html.='</td>';
				$html.='<td class="right">';
				$html.=($fila["PDV"]<>0)?number_format($fila["PDV"],"0",".",","):"";
				$html.='</td>';
				$html.='</tr>';
				}
			$html.='<tr class="head">';
			$html.='<td colspan="3" class="left"><strong>Totales :</strong></td>';
			for($i=1;$i<=8;$i++) {
					$html.='<td class="right">'.number_format($varTotal[$i],"2",".",",").'</td>';
					}
			$html.='</tr>';
	
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
				
		$html.='</table>';
		return $html;
		}
	}
	
?>