<?php
//include "clase.conexion.php";
class clsRegistroVentas extends clsConexion{
	public $varTitulo = "REPORTE DE REGISTRO DE VENTAS";
	public $varMyTabla  = "_sv_compra";
	public $varMyID  	  = "id_compra";
	function FormuBusqueda() {
		$varEst = array("AC"=>"ACTIVO", "PP"=>"PAGADO PARCIAL","CA"=>"CANCELADO","AN"=>"ANULADO");
		$varTip = array("S"=>"SI INCLUIR MONTO DEVOLUCION PARCIAL", "N"=>"NO INCLUIR MONTO DEVOLUCION PARCIAL");
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"450", "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"=>"Cliente :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"ccod", "name"=>"ccod", "valor"=>$_POST["ccod"],
																					 "ancho"=>"50", "alinear"=>"center", 
																					 "keypress"=>"return SoloEnteros(event)",
																					 "blur"=>"xajax_seleccionar_cliente(this.value)", 
																					 "dclick"=>"fn_cargar_listado_cliente('ccod', 'cnom')"),
																			   array("tipo"=>"text", "id"=>"cnom", "name"=>"cnom", "valor"=>$_POST["cnom"], 
																					 "ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
														 	)
											 ),
									   array("titulo"=>"Vendedor :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"vcod", "name"=>"vcod", "valor"=>$_POST["vcod"],
																					 "ancho"=>"50", "alinear"=>"center", 
																					 "keypress"=>"return SoloEnteros(event)",
																					 "blur"=>"xajax_seleccionar_vendedor(this.value)", 
																					 "dclick"=>"fn_cargar_listado_vendedor('vcod', 'vnom')"),
																			   array("tipo"=>"text", "id"=>"vnom", "name"=>"vnom", "valor"=>$_POST["vnom"], 
																					 "ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
														 	)
											 ),
									   array("titulo"=>"Ruta :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"rcod", "name"=>"rcod", "valor"=>NULL,
																					 "ancho"=>"50", "alinear"=>"center",
																					 "blur"=>"xajax_seleccionar_ruta(this.value)", 
																					 "dclick"=>"fn_cargar_listado_ruta('rcod', 'rnom')"),
																			   array("tipo"=>"text", "id"=>"rnom", "name"=>"rnom", "valor"=>NULL, 
																					 "ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
														 	)
											 ),
									   array("titulo"=>"Tipo Doc. :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"tcod", "name"=>"tcod", "valor"=>NULL,
																					 "ancho"=>"50", "alinear"=>"center", 
																					 "keypress"=>"return SoloEnteros(event)",
																					 "blur"=>"xajax_seleccionar_tipo_docventa(this.value)", 
																					 "dclick"=>"fn_cargar_listado_tipo_docventa('tcod', 'tnom')"),
																			   array("tipo"=>"text", "id"=>"tnom", "name"=>"tnom", "valor"=>NULL, 
																					 "ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
														 	)
											 ),
									   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)
																			   )
														 	)
											 ),
										array("titulo"=>"Estado :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"select_simple", "id"=>"est", "name"=>"est",
																					 "combo"=>array("sql"=>$varEst, "sincod"=>"SI", 
																					 "ancho"=>"140", "titulo"=>"--[[SELECCIONAR]]--"))
																			   )
														 	)
											 ),
									   array("titulo"=>"Tipo Dato :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"select_simple", "id"=>"tip", "name"=>"tip",
																					 "combo"=>array("sql"=>$varTip, "sincod"=>"SI", 
																					 "ancho"=>"270"))
																			   )
														 	)
											 )
									 ),
					  "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($varForm  = NULL, 
					   $varField = NULL, 
					   $varOrder = NULL
					   ) {
		$varCli = (isset($varForm["ccod"]) && $varForm["ccod"]<>'')?$varForm["ccod"]:"NULL";
		$varVen = (isset($varForm["vcod"]) && $varForm["vcod"]<>'')?$varForm["vcod"]:"NULL";
		$varRut = (isset($varForm["rcod"]) && $varForm["rcod"]<>'')?"'".$varForm["rcod"]."'":"NULL";
		$varTip = (isset($varForm["tcod"]) && $varForm["tcod"]<>'')?$varForm["tcod"]:"NULL";
		$varIni = (isset($varForm["fini"]) && $varForm["fini"]<>'')?"'".FechaMySql($varForm["fini"])."'":"NULL";
		$varFin = (isset($varForm["ffin"]) && $varForm["ffin"]<>'')?"'".FechaMySql($varForm["ffin"])."'":"NULL";
		$varEst = (isset($varForm["est"]) && $varForm["est"]<>'Ninguno')?"'".$varForm["est"]."'":"NULL";
		$varDat = (isset($varForm["tip"]) && $varForm["tip"]<>'Ninguno')?"'".$varForm["tip"]."'":"NULL";
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";
		
		$sql = "call spQuery_CXC_Registro_Ventas($varCli, $varVen, $varRut, $varTip, $varIni, $varFin, $varEst, $varDat, $varFie, $varOrd);";
		
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"1000", "sql_cab"=>$sql, "pages"=>50,
						"conte"=>array(array("tamano"=>20, "titulo"=>"IDE", "alinear"=>"center"),
									   array("tamano"=>50, "titulo"=>"FEMI", "alinear"=>"center"),
									   array("tamano"=>50, "titulo"=>"FVEN", "alinear"=>"center"),
									   array("tamano"=>10, "titulo"=>"CTIP", "alinear"=>"center"),
									   array("tamano"=>10, "titulo"=>"CTIP", "alinear"=>"center", "oculto"=>"SI"),
									   array("tamano"=>50, "titulo"=>"TIPO", "fijo"=>"SI"),
									   array("tamano"=>50, "titulo"=>"ABREV", "fijo"=>"SI", "oculto"=>"SI"),
									   array("tamano"=>90, "titulo"=>"DOCUMENTO", "alinear"=>"center"),
									   array("tamano"=>40, "titulo"=>"EST.", "alinear"=>"center"),										   
									   array("tamano"=>10, "titulo"=>"CTID", "alinear"=>"center"),
									   array("tamano"=>50, "titulo"=>"DOCIDEN", "fijo"=>"SI"),
									   array("tamano"=>10, "titulo"=>"CCLI", "alinear"=>"center"),
									   array("tamano"=>50, "titulo"=>"CLIENTE", "fijo"=>"SI"),
									   array("tamano"=>250,"titulo"=>"GLOSA", "oculto"=>"SI"),
									   array("tamano"=>60, "titulo"=>"VVENTA", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>60, "titulo"=>"IGV", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>60, "titulo"=>"TOTAL", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>60, "titulo"=>"SALDO", "alinear"=>"right", "funcion"=>array("autosuma"), "oculto"=>"SI"),
									   array("tamano"=>100, "titulo"=>"FCREADO", "alinear"=>"center", "oculto"=>"SI"),
									   array("tamano"=>100, "titulo"=>"FMODIFI", "alinear"=>"center", "oculto"=>"SI"),
									   array("tamano"=>100, "titulo"=>"UCREADO", "alinear"=>"center", "oculto"=>"SI"),
									   array("tamano"=>100, "titulo"=>"UMODIFI", "alinear"=>"center", "oculto"=>"SI")
									   )
							);
		return $this->SetGridTabla2($varTabla);
		//return $sql;
		}
	
	function Imprimir ($varCli = NULL,
					   $varVen = NULL, 
					   $varRut = NULL, 
					   $varTip = NULL, 
					   $varIni = NULL,
					   $varFin = NULL,
					   $varEst = NULL,
					   $varDat = NULL
					   )  {
		
		$varCli = (isset($varCli) && $varCli<>'')?$varCli:"NULL";
		$varVen = (isset($varVen) && $varVen<>'')?$varVen:"NULL";
		$varRut = (isset($varRut) && $varRut<>'')?"'".$varRut."'":"NULL";
		$varTip = (isset($varTip) && $varTip<>'')?$varTip:"NULL";
		$varIni = (isset($varIni) && $varIni<>'')?"'".FechaMySql($varIni)."'":"NULL";
		$varFin = (isset($varFin) && $varFin<>'')?"'".FechaMySql($varFin)."'":"NULL";
		$varEst = (isset($varEst) && $varEst<>'Ninguno')?"'".$varEst."'":"NULL";
		$varDat = (isset($varDat) && $varDat<>'Ninguno')?"'".$varDat."'":"NULL";
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";
		
		$sql = "call spQuery_CXC_Registro_Ventas($varCli, $varVen, $varRut, $varTip, $varIni, $varFin, $varEst, $varDat, 'FEMI, SUNAT, DOCUMENTO', $varOrd);";
		
		$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></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.='</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="50" align="center">IDE</td>';
			$html.='<td width="50" align="center">FEMI</td>';
			$html.='<td width="50" align="center">FVEN</td>';
			$html.='<td width="20">CTIP</td>';
			$html.='<td width="20">ABREV</td>';
			$html.='<td width="20" align="center">SERIE</td>';
			$html.='<td width="50" align="center">NUMERO</td>';
			$html.='<td width="20" align="center">EST</td>';
			$html.='<td width="20" class="center">CTID</td>';
			$html.='<td width="30" class="center">DOCIDEN</td>';
			$html.='<td width="100" align="center">CLIENTE</td>';
			$html.='<td width="25" class="right" nowrap>SUBTOTAL</td>';
			$html.='<td width="25" class="right">IGV</td>';
			$html.='<td width="25" class="right">TOTAL</td>';
			$html.='</tr>';
			$html.='</thead>';
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$varTotal1 = 0;
			$varTotal2 = 0;
			$varTotal3 = 0;
			$varTotal4 = 0;
			$varNumDoc = 0;
			while($fila = $this->FetchArray($rs))	{
				$varTot1 = $varTot1 + $fila["SUBTOTAL"];
				$varTot2 = $varTot2 + $fila["IGV"];
				$varTot3 = $varTot3 + $fila["TOTAL"];
				$varNumDoc++;
				$html.='<tr class="list_row">
						<td NOWRAP align="center">'.$fila["IDE"].'</td>
						<td NOWRAP align="center">'.$fila["FEMI"].'</td>
						<td NOWRAP align="center">'.$fila["FVEN"].'</td>
						<td NOWRAP>'.$fila["SUNAT"].'</td>
						<td NOWRAP>'.$fila["ABREV"].'</td>
						<td NOWRAP align="center">'.substr($fila["DOCUMENTO"],0,3).'</td>
						<td NOWRAP align="center">'.substr($fila["DOCUMENTO"],5,7).'</td>
						<td class="center">'.$fila["ESTADO"].'</td>
						<td class="center">'.$fila["CTID"].'</td>
						<td NOWRAP align="center">'.$fila["DOCIDEN"].'</td>
						<td NOWRAP>'.$fila["CLIENTE"].'</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="3" class="left"><strong>Totales :</strong></td>';
			$html.='<td colspan="8" class="left">'.number_format($varNumDoc,"0",".",",").' Documentos</td>';
			$html.='<td class="right">'.number_format($varTot1,"2",".",",").'</td>';
			$html.='<td class="right">'.number_format($varTot2,"2",".",",").'</td>';
			$html.='<td class="right">'.number_format($varTot3,"2",".",",").'</td>';
			$html.='</tr>';
	
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
				
		$html.='</table>';
		return $html;
		}
	}
	
?>