<?php
//include "clase.conexion.php";
class clsAvanceProductos extends clsConexion{
	public $varTitulo 	= "AVANCE DE VENTAS POR PRODUCTO";
	function FormuBusqueda() {
		$sqlven = "call spQuery_Lista_Vendedor('','','');";
		$sqlrut = "call spQuery_Lista_Ruta('','%');";
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"650", "clase"=>"Formtitulo", "titulo"=>$this->varTitulo,
						"formu"=>array("id"=>"form", "name"=>"form", "metodo"=>"POST", "onsubmit"=>"return false;"),						
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$this->varTitulo)
									  ),
						"conte"=>array(array("titulo"=>"Vendedor :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>100,
											 "campo"=>array("tipo"=>"lista_doble", "filas"=>8, "ancho"=>"250", "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"=>"250", "sql"=>$sqlrut,
											 				"id1"=>"rut", "name1"=>"rut", "id2"=>"rcod", "name2"=>"rcod",
															"dclick1"=>"mover_lista('right','rut','rcod')", "dclick2"=>"mover_lista('left','rut','rcod')"															
															)
											),
									   array("titulo"=>"Proveedor :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"pcod", "name"=>"pcod", 
																					 "ancho"=>"50", "alinear"=>"center", 
																					 "keypress"=>"return SoloEnteros(event)",
																					 "blur"=>"xajax_seleccionar_proveedor(this.value)", 
																					 "dclick"=>"fn_cargar_listado_proveedor('pcod', 'pnom')"),
																			   array("tipo"=>"text", "id"=>"pnom", "name"=>"pnom", 
																					 "ancho"=>"350", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
														 	)
											 ),
									   array("titulo"=>"Producto :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"acod", "name"=>"acod", 
																					 "ancho"=>"50", "alinear"=>"center", 
																					 "keypress"=>"return SoloEnteros(event)",
																					 "blur"=>"xajax_seleccionar_articulo(this.value)", 
																					 "dclick"=>"fn_cargar_listado_producto('acod', 'anom')"),
																			   array("tipo"=>"text", "id"=>"anom", "name"=>"anom", 
																					 "ancho"=>"350", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
														 	)
											 ),
									   array("titulo"=>"Fecha Inicio :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"fecha", "id"=>"fini", "name"=>"fini", "alinear"=>"center"))
														 	)
											 ),
									   array("titulo"=>"Fecha Final :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"fecha", "id"=>"ffin", "name"=>"ffin", "alinear"=>"center"))
														 	)
											 ),
									   ),
						"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,
					   $varPro = NULL, 
					   $varArt = NULL, 
					   $varIni = NULL, 
					   $varFin = NULL
					   )  {
						  
		$varVen = ($varVen<>"")?"\"(".$varVen.")\"":"NULL";
		$varRut = ($varRut<>"")?"\"(".str_replace("\\","",$varRut).")\"":"NULL";
		$varPro = (isset($varPro) && $varPro<>"")?$varPro:"NULL";
		$varArt = (isset($varArt) && $varArt<>"")?"'".$varArt."'":"NULL";
		$varIni = (isset($varIni) && $varIni<>"")?"'".FechaMySql($varIni)."'":"NULL";
		$varFin = (isset($varFin) && $varFin<>"")?"'".FechaMySql($varFin)."'":"NULL";
		
		
		$sql = "call spQuery_Avance_Producto($varVen, $varRut, $varPro, $varArt, $varIni, $varFin)";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"900", "sql_cab"=>$sql,	"pages"=>100,
						"conte"=>array(array("tamano"=>20, "titulo"=>"CVEN", "alinear"=>"center"),
									   array("tamano"=>200,"titulo"=>"VENDEDOR", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("tamano"=>20, "titulo"=>"CPRO", "alinear"=>"center"),
									   array("tamano"=>200,"titulo"=>"PRODUCTO", "fijo"=>"SI"),
									   array("tamano"=>70, "titulo"=>"UNID", "alinear"=>"center"),
									   array("tamano"=>70, "titulo"=>"CANTIDAD", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>70, "titulo"=>"SUBTOTAL", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>70, "titulo"=>"IGV", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>70, "titulo"=>"TOTAL", "alinear"=>"right", "funcion"=>array("autosuma"))
									   )
						);
		return $this->SetGridTabla($varTabla);
		}
	function Imprimir ($varVen = NULL, 
					   $varRut = NULL,
					   $varPro = NULL, 
					   $varArt = NULL, 
					   $varIni = NULL, 
					   $varFin = NULL
					   )  {
						  
		$varVenC = ($varVen<>"")?"\"(".$varVen.")\"":"NULL";
		$varRutC = ($varRut<>"")?"\"(".str_replace("\\","",$varRut).")\"":"NULL";
		$varProC = (isset($varPro) && $varPro<>"")?$varPro:"NULL";
		$varArt  = (isset($varArt) && $varArt<>"")?"'".$varArt."'":"NULL";
		$varIniC = (isset($varIni) && $varIni<>"")?"'".FechaMySql($varIni)."'":"NULL";
		$varFinC = (isset($varFin) && $varFin<>"")?"'".FechaMySql($varFin)."'":"NULL";
		
		
		$sql = "call spQuery_Avance_Producto($varVenC, $varRutC, $varProC, $varArt, $varIniC, $varFinC)";
		
		$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>De '.$varIni.' Hasta '.$varFin.' </b></td>';
			$html.='<td style="width: 50%; vertical-align: middle; text-align:right;"><b>'.MostrarFechaActual().'</b></td>';
			$html.='</tr>';
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
		$html.='<tr>';
		$html.='<td>';
			$html.='<table style="width: 100%;">';
			$html.='<tr>';
			$html.='<td style="width: 10%; vertical-align: middle; text-align:left;" nowrap="nowrap"><b>Vendedor(es)</b></td>';
			$html.='<td style="width: 90%; vertical-align: middle; text-align:left;"><b> : </b>';
			if($varVen=="") {
				$html.= "Todos - TODOS LOS VENDEDORES";
				}
			else {
				$varVend  = ($varVen<>"")?"(".$varVen.")":"NULL";
				$sql1 = "select id_vendedor, nombre from _sv_vendedor where id_vendedor in ".$varVend;
				$this->AbrirConexion();
				$rs_1 = $this->Query($sql1);
				$this->CerrarConexion();
				$varTemp = "";
				while($fila_1 = $this->FetchArray($rs_1)) {
					if($varTemp=="")
						$varTemp = $fila_1[0]." - ".$fila_1[1];
					else	
						$varTemp.= " / ".$fila_1[0]." - ".$fila_1[1];
					}
				}
			$html.=$varTemp;
			$html.='</td>';
			$html.='</tr>';
			$html.='<tr>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b>Ruta(as)</b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b> : </b>';
			if($varRut=="") {
				$html.= "Todos - TODOS LAS RUTAS";
				}
			else {
				$varRuta  = ($_GET["rcod"]<>"")?"(".str_replace("\\","",$_GET["rcod"]).")":"NULL";
				$sql1 = "select id_ruta, nombre from _sv_ruta where id_ruta in ".$varRuta;
				$this->AbrirConexion();
				$rs_1 = $this->Query($sql1);
				$this->CerrarConexion();
				$varTemp = "";
				while($fila_1 = $this->FetchArray($rs_1)) {
					if($varTemp=="")
						$varTemp = $fila_1[0]." - ".$fila_1[1];
					else	
						$varTemp.= " / ".$fila_1[0]." - ".$fila_1[1];
					}
				}
			$html.=$varTemp;
			$html.='</td>';
			$html.='</tr>';
			$html.='<tr>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b>Proveedor</b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b> : </b>';
			if($varPro=="") {
				$html.= "Todos - TODOS LOS PROVEEDORES";
				}
			else {
				$sql1 = "select id_proveedor, nombre from _sv_proveedor where id_proveedor = ".$varPro;
				$this->AbrirConexion();
				$rs_1 = $this->Query($sql1);
				$this->CerrarConexion();
				$fila_1 = $this->FetchArray($rs_1);						
				$html.= $fila_1[0]." - ".$fila_1[1];
				}
			$html.='</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">CPRO</td>';
			$html.='<td width="150">PRODUCTO</td>';
			$html.='<td width="50">U.M.</td>';
			$html.='<td width="25" class="right" nowrap="nowrap">CANT.</td>';
			$html.='<td width="25" class="right" nowrap="nowrap">SUBTOTAL</td>';
			$html.='<td width="25" class="right" nowrap="nowrap">IGV</td>';
			$html.='<td width="25" class="right" nowrap="nowrap">TOTAL</td>';
			$html.='</tr>';
			$html.='</thead>';
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$varVend_Ant = "";
			$varVend_Act = "";
			$varTotal1 = 0;
			$varTotal2 = 0;
			$varTotal3 = 0;
			$varNumDoc = 0;
			while($fila = $this->FetchArray($rs))	{
				$varVend_Act = $fila["CVEN"];
				$varTotal1 = $varTotal1 + $fila["SUBTOTAL"];
				$varTotal2 = $varTotal2 + $fila["IGV"];
				$varTotal3 = $varTotal3 + $fila["TOTAL"];
				$varNumDoc++;
				if($varVend_Act <> $varVend_Ant) {
					$varVend_Ant = $fila["CVEN"];
					$html.='<tr class="head">';
					$html.='<td class="left" colspan="8"><b>'.$fila["CVEN"].' - '.$fila["VENDEDOR"].'</b></td>';
					$html.='</tr>';
					}
				$html.='<tr class="list_row">
						<td class="center">&nbsp;</td>
						<td class="center">'.$fila["CPRO"].'</td>
						<td NOWRAP>'.$fila["PRODUCTO"].'</td>
						<td NOWRAP>'.$fila["UM"].'</td>
						<td class="right">'.number_format($fila["CANT"],"2",".",",").'</td>
						<td class="right">'.number_format($fila["SUBTOTAL"],"2",".",",").'</td>
						<td class="right">'.number_format($fila["IGV"],"2",".",",").'</td>
						<td class="right">'.number_format($fila["TOTAL"],"2",".",",").'</td>
						</tr>';
				}
			$html.='<tr class="head">';
			$html.='<td colspan="2" class="left"><strong>Totales :</strong></td>';
			$html.='<td class="left">'.number_format($varNumDoc,"0",".",",").' Productos</td>';
			$html.='<td class="right"></td>';
			$html.='<td class="right"></td>';
			$html.='<td class="right">'.number_format($varTotal1,"2",".",",").'</td>';
			$html.='<td class="right">'.number_format($varTotal2,"2",".",",").'</td>';
			$html.='<td class="right">'.number_format($varTotal3,"2",".",",").'</td>';
			$html.='</tr>';
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
				
		$html.='</table>		
				</body>
				</html>';
		return $html;
		}
	}
?>