<?php
//include "clase.conexion.php";
class clsCXC_ReportePagos extends clsConexion{
	public $varTitulo = "REPORTE DE PAGOS DE CUENTAS POR COBRAR";
	function FormuBusqueda() {
		$sqlven = "call spQuery_Lista_Vendedor('','','');";
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"600", "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),
									   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"=>$varPCOD, 
																					 "ancho"=>"50",  "alinear"=>"center", 
																					 "blur"=>"xajax_seleccionar_cliente(this.value)", 
																					 "dclick"=>"fn_cargar_listado_cliente('ccod', 'cnom')"),
																			   array("tipo"=>"text", "id"=>"cnom", "name"=>"cnom", "valor"=>$varPNOM, 
																			   		 "ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
														 )
										  	 ),
									   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"=>"Fecha Incial :", "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()")
									 )
					);
		return $this->setTablaForm($varTabla);
		}
 	
	function GridDatos($varCli	 = NULL, 
					   $varVen	 = NULL, 
					   $varIni	 = NULL,  
					   $varFin	 = NULL, 
					   $varField = NULL, 
					   $varOrder = NULL
					   ) {
		$varCli = (isset($varCli) && $varCli<>'')?$varCli:"NULL";
		$varVen = ($varVen<>"")?"\"(".$varVen.")\"":"NULL";
		$varIni = (isset($varIni) && $varIni<>'')? "'".FechaMySql($varIni)."'":"NULL";
		$varFin = (isset($varFin) && $varFin<>'')? "'".FechaMySql($varFin)."'":"NULL";
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";

		$sql = "call spQuery_Pagos_CxC($varCli, $varVen, $varIni, $varFin, $varFie, $varOrd);";

		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"900", "sql_cab"=>$sql, "pages"=>50,
						"conte"=>array(array("tamano"=>20, "titulo"=>"IDE", "alinear"=>"center"),
									   array("tamano"=>30, "titulo"=>"CCLI", "alinear"=>"center"),
									   array("tamano"=>250,"titulo"=>"CLIENTE", "fijo"=>"SI"),
									   array("tamano"=>30, "titulo"=>"FECHA", "alinear"=>"center"),
									   array("tamano"=>50, "titulo"=>"DOCUMENTO", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("tamano"=>30, "titulo"=>"EST", "alinear"=>"center"),
									   array("tamano"=>60, "titulo"=>"TOTAL", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>30, "titulo"=>"CFOR", "alinear"=>"center"),
									   array("tamano"=>250,"titulo"=>"FORMA PAGO", "oculto"=>"SI"),
									   array("tamano"=>250,"titulo"=>"FORMA PAGO", "fijo"=>"SI"),
									   array("tamano"=>30, "titulo"=>"NRO.PAGO", "alinear"=>"center"),
									   array("tamano"=>30, "titulo"=>"GLOSA", "fijo"=>"SI"),
									   array("tamano"=>30, "titulo"=>"FECHA", "alinear"=>"center"),									   
									   array("tamano"=>60, "titulo"=>"MONTO", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("titulo"=>"FCREADO",  "ancho"=>"100", "alinear"=>"center", "fijo"=>"SI"),
									   array("titulo"=>"FUPDATE",  "ancho"=>"100", "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"UCREADO",  "ancho"=>"50", "alinear"=>"center"),
									   array("titulo"=>"UUPDATE",  "ancho"=>"50", "alinear"=>"center", "oculto"=>"SI")
									   )
						);
		return $this->SetGridTabla2($varTabla);
		}
		
	function Imprimir ($varCli	 = NULL, 
					   $varVen	 = 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];			
				}
			}
		
		$varCli = (isset($varCli) && $varCli<>'')?$varCli:"NULL";
		$varVen = ($varVen<>"")?"\"(".$varVen.")\"":"NULL";
		$varIni = (isset($varIni) && $varIni<>'')? "'".FechaMySql($varIni)."'":"NULL";
		$varFin = (isset($varFin) && $varFin<>'')? "'".FechaMySql($varFin)."'":"NULL";
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";

		$sql = "call spQuery_Pagos_CxC($varCli, $varVen, $varIni, $varFin, 'P.fecha, NPAGO, 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>Vendedor(es) :</b> '.$varCadVen.'</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="2" class="center">&nbsp;</td>';
			$html.='<td width="2" class="center">CCLI</td>';
			$html.='<td width="150">CLIENTE</td>';
			$html.='<td width="30"" class="center">F.EMISION</td>';
			$html.='<td width="50"" class="left">DOC.VENTA</td>';
			$html.='<td width="10"" class="center">EST</td>';
			$html.='<td width="25" class="right" NOWRAP>MONTO</td>';
			$html.='<td width="50"" class="left">TIPO.PAGO</td>';
			$html.='<td width="50"" class="left">GLOSA</td>';
			$html.='<td width="50"" class="left">OPERACION</td>';
			$html.='<td width="30"" class="center">F.PAGO</td>';
			$html.='<td width="25" class="right" NOWRAP>PAGO</td>';
			$html.='<td width="50"" class="left">USUARIO</td>';
			$html.='</tr>';
			$html.='</thead>';
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$varTotal1 = 0;
			$varSubTot = 0;
			$varNumDoc = 0;
			$varVenAct = "";
			$varVenAnt = "";
			while($fila = $this->FetchArray($rs))	{
				$varTotal1 = $varTotal1 + $fila["PAGO"];
				$varVenAct = $fila["NPAGO"];
				$varNumDoc++;
				if($varVenAct <> $varVenAnt) {
					if($varVenAnt<>"") {
						$html.='<tr class="list_row">';
						$html.='<td align="right" colspan="11"><b>Total '.$varVenAnt.'</b></td>';
						$html.='<td align="right"><b>'.number_format($varSubTot,"2",".",",").'</b></td>';
						$html.='<td></td>';
						$html.='</tr>';
						$varSubTot = 0;
						}
					$varVenAnt = $fila["NPAGO"];
					$html.='<tr class="list_row">';
					$html.='<td colspan="12"><b>'.$fila["CTIP"].' - '.$fila["TIPO"].' - '.$fila["NPAGO"].'</b></td>';
					$html.='<td></td>';
					$html.='</tr>';
					}
				$varSubTot = $varSubTot + $fila["PAGO"];
				$html.='<tr class="list_row">
						<td align="center">&nbsp;</td>
						<td align="center">'.str_pad($fila["CCLI"], 5, "0", STR_PAD_LEFT).'</td>
						<td NOWRAP>'.$fila["CLIENTE"].'</td>
						<td NOWRAP align="left">'.$fila["FEMI"].'</td>
						<td NOWRAP align="left" NOWRAP>'.$fila["DOCUMENTO"].'</td>
						<td NOWRAP align="center" NOWRAP>'.$fila["ESTADO"].'</td>
						<td align="right">'.number_format($fila["MONTO"],"2",".",",").'</td>
						<td NOWRAP align="left">'.$fila["CTIP"].' - '.$fila["ABREV"].'</td>
						<td NOWRAP align="left">'.substr($fila["GLOSA"],0,30).'</td>
						<td NOWRAP align="left">'.$fila["NPAGO"].'</td>						
						<td NOWRAP align="left">'.$fila["FECHA"].'</td>
						<td align="right">'.number_format($fila["PAGO"],"2",".",",").'</td>
						<td NOWRAP align="left">'.$fila["USER_CREADO"].'</td>
						</tr>';
				}
			$html.='<tr class="list_row">';
			$html.='<td align="right" colspan="11"><b>Total '.$varVenAnt.'</b></td>';
			$html.='<td align="right"><b>'.number_format($varSubTot,"2",".",",").'</b></td>';
			$html.='<td></td>';
			$html.='</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",".",",").' Registros</td>';
			$html.='<td class="right">'.number_format($varTotal1,"2",".",",").'</td>';
			$html.='<td></td>';
			$html.='</tr>';
	
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
				
		$html.='</table>';
		return $html;
		}
	}
	
?>