<?php
//include "clase.conexion.php";
class clsCXP_Pagar extends clsConexion{
	public $varTitulo = "CUENTAS POR PAGAR";
	public $varMyTabla  = "_sv_compra";
	public $varMyID  	  = "id_compra";
	function FormuBusqueda() {
		$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"=>"Empresa :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"ecod", "name"=>"ecod", "valor"=>$_POST["ecod"], 
																					 "ancho"=>"50", "alinear"=>"center", 
																					 "blur"=>"xajax_seleccionar_empresa(this.value)", 
																					 "dclick"=>"fn_cargar_listado_empresa('ecod', 'enom')"),
																			   array("tipo"=>"text", "id"=>"enom", "name"=>"enom", "valor"=>$_POST["enom"], 
																					 "ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
															)
											 ),
									   array("titulo"=>"Proveedor :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>100,
										     "campo"=>array("tipo"=>"varios",
														    "contenido"=>array(array("tipo"=>"text", "id"=>"pcod", "name"=>"pcod", "valor"=>$varPCOD, 
																					 "ancho"=>"50",  "alinear"=>"center", 
																					 "blur"=>"xajax_seleccionar_proveedor(this.value)", 
																					 "dclick"=>"fn_cargar_listado_proveedor('pcod', 'pnom','PR')"),
																			   array("tipo"=>"text", "id"=>"pnom", "name"=>"pnom", "valor"=>$varPNOM, 
																			   		 "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"=>$_POST["tcod"],
																					 "ancho"=>"50", "alinear"=>"center", 
																					 "keypress"=>"return SoloEnteros(event)",
																					 "blur"=>"xajax_seleccionar_tipo_doc_compra(this.value)", 
																					 "dclick"=>"fn_cargar_listado_tipo_doc_compra('tcod', 'tnom')"),
																			   array("tipo"=>"text", "id"=>"tnom", "name"=>"tnom", "valor"=>$_POST["tnom"], 
																					 "ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
														 	)
											 ),
									   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('', 'NEW')")
									 )
					);
		return $this->setTablaForm($varTabla);
		}
 	
	function GridDatos($varForm  = NULL, 
					   $varField = NULL, 
					   $varOrder = NULL
					   ) {
		$varEmp = (isset($varForm["ecod"]) && $varForm["ecod"]<>'')? $varForm["ecod"]:"NULL";
		$varPro = (isset($varForm["pcod"]) && $varForm["pcod"]<>'')? $varForm["pcod"]:"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";
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";
		
		$sql = "call spQuery_Cuentas_Pagar($varEmp, $varPro,  $varTip, $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"=>10, "titulo"=>"CEMP", "alinear"=>"center", "oculto"=>"SI"),
									   array("tamano"=>50, "titulo"=>"EMPRESA", "fijo"=>"SI"),
									   array("tamano"=>50, "titulo"=>"EMISION", "alinear"=>"center"),									   
									   array("tamano"=>90, "titulo"=>"DOCUMENTO", "alinear"=>"center", "funcion"=>array("contar")),
									   array("tamano"=>10, "titulo"=>"CCON", "alinear"=>"center", "oculto"=>"SI"),
									   array("tamano"=>50, "titulo"=>"CONDICION", "fijo"=>"SI"),
									   array("tamano"=>10, "titulo"=>"CPRO", "alinear"=>"center"	),
									   array("tamano"=>50, "titulo"=>"PROVEEDOR", "fijo"=>"SI"),
									   array("tamano"=>250,"titulo"=>"DIRECCION", "oculto"=>"SI"), 
									   array("tamano"=>60, "titulo"=>"TOTAL", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>60, "titulo"=>"PAGOS", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>60, "titulo"=>"SALDO", "alinear"=>"right", "funcion"=>array("autosuma")),
									   array("tamano"=>120, "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")
									   ),
						"boton"=>array(array("cond"=>array(array(3,"<>","AN")),
											 "tipo"=>"img", "id"=>"banu", "name"=>"banu", "img"=>"../images/b_dolar_add.png", 
											 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>REALIZAR UN PAGO</b> DE LA COMPRA"),
											 				 array("valor"=>"<br><b>Empresa : </b>"),
															 array("valor"=>1, "tipo"=>"INDEX"),
															 array("valor"=>" - "),
															 array("valor"=>2, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Proveedor : </b>"),
															 array("valor"=>7, "tipo"=>"INDEX"),
															 array("valor"=>" - "),
															 array("valor"=>8, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Fecha Emision : </b>"),
															 array("valor"=>3, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Nro. Documento : </b>"),
															 array("valor"=>4, "tipo"=>"INDEX")
															 ),
											 "click"=>array("funcion"=>"xFormPagoJS",
															"param"=>array(array("valor"=>0, "tipo"=>"INDEX"))
															)												 
											)
									)
						);
		return $this->SetGridTabla2($varTabla);
		}
	
	function FormuPago($varCod=NULL) {
		$sql = "call spQuery_Registro_Compras_Detalle($varCod);";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		$fila = $this->FetchArray($rs);
	
		$sqlpag = "call spQuery_Combo_Tipo_Doc_Pago();";
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"450", "clase"=>"FormTitulo", "titulo"=>$this->varTitulo." - REALIZAR PAGO",
						"formu"=>array("id"=>"form", "name"=>"form", "metodo"=>"POST", "onsubmit"=>"return validaRegistroPago(this)"),						
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$this->varTitulo),
									   array("tipo"=>"oculto", "id"=>"txtide", "name"=>"txtide", "valor"=>$varCod),
									   array("tipo"=>"oculto", "id"=>"txtemi", "name"=>"txtemi", "valor"=>$fila["EMISION"]),
									   array("tipo"=>"oculto", "id"=>"txtsal", "name"=>"txtsal", "valor"=>round($fila["SALDO"],2))
									  ),
						"conte"=>array(array("titulo"=>"Fecha Emisi&oacute;n :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"100",
											 "campo" =>array("tipo"=>"simple", "valor"=>$fila[EMISION])),
									   array("titulo"=>"Fecha Movimiento :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"100",
											 "campo" =>array("tipo"=>"simple", "valor"=>$fila[MOVIMIENTO])),
									   array("titulo"=>"Tipo Doc. :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo" =>array("tipo"=>"simple", "valor"=>$fila[CTIP]." - ".$fila[TIPO])),
									   array("titulo"=>"Nro Doc. :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo" =>array("tipo"=>"simple", "valor"=>$fila[NDOC])),
									   array("titulo"=>"Proveedor :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo" =>array("tipo"=>"simple", "valor"=>$fila[CPRO]." - ".$fila[PROVEEDOR])),
									   array("titulo"=>"Cond. Venta :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo" =>array("tipo"=>"simple", "valor"=>$fila[CCON]." - ".$fila[CONDICION])),
									   array("titulo"=>"Monto :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo" =>array("tipo"=>"simple", "valor"=>number_format($fila["TOTAL"],"2",".",","))),	 
									   array("titulo"=>"Pagos :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo" =>array("tipo"=>"simple", "valor"=>number_format($fila["PAGOS"],"2",".",","))),	 
									   array("titulo"=>"Saldo Actual :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo" =>array("tipo"=>"simple", "valor"=>number_format($fila["SALDO"],"2",".",","))),	 
									   array("titulo"=>"Forma Pago :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"select", "id"=>"tipo", "name"=>"tipo", "valor"=>NULL,
															"sql"=>$sqlpag, "ancho"=>"250", "sincod"=>"SI")),
									   array("titulo"=>"Nro. Doc. Pago :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"texto", "id"=>"txtnum", "name"=>"txtnum", "valor"=>NULL, "ancho"=>"150"),
											 ),
									   array("titulo"=>"Fecha Pago :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
												 "campo"=>array("tipo"=>"fecha", "id"=>"femi", "name"=>"femi", "alinear"=>"center", 
																"valor"=>date( "d/m/Y",time()), "ancho"=>"90")),
									   array("titulo"=>"Monto Pagar :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"texto", "id"=>"txtpago", "name"=>"txtpago", "valor"=>NULL, 
											 "ancho"=>"100", "keypress"=>"return SoloNumeros(event)"),
											 ),
									   array("titulo"=>"Observacion :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
												 "campo"=>array("tipo"=>"area", "id"=>"txtobs", "name"=>"txtobs", "ancho"=>"300", "alto"=>"40"))
									   ),
						"boton"=>array(array("tipo"=>"button", "id"=>"bsav", "name"=>"bsav", "ancho"=>"120",
											 "clase"=>"boton_guardar", "texto"=>"Grabar Pago", "click"=>"javascrip:xGrabarJS()"),			  
									   array("tipo"=>"button", "id"=>"breg", "name"=>"breg", "ancho"=>"100",
											 "clase"=>"boton_cancelar", "texto"=>"Cancelar", "click"=>"javascrip:xajax_xRegresar()")
									)
						);
		$varReturn = $this->setTablaForm($varTabla);	
		
		$sql = "select D.id_producto AS CPRO, A.descripcion AS PRODUCTO, D.id_um as UM, D.precio_con_igv AS PRECIO, D.CANTIDAD, D.TOTAL
				from _sv_compra_detalle D
				inner join _sv_producto A ON D.id_producto = A.id_producto
				where D.id_compra = $varCod";
		
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"700", "sql_cab"=>$sql, "pages"=>50,
						"conte"=>array(array("tamano"=>20, "titulo"=>"CPRO", "alinear"=>"center"),
									   array("tamano"=>50, "titulo"=>"PRODUCTO","funcion"=>array("contar")),									   
									   array("tamano"=>90, "titulo"=>"UM", "alinear"=>"center"),
									   array("tamano"=>60, "titulo"=>"PRECIO", "alinear"=>"right"),
									   array("tamano"=>60, "titulo"=>"CANTIDAD", "alinear"=>"right"),
									   array("tamano"=>60, "titulo"=>"TOTAL", "alinear"=>"right", "funcion"=>array("autosuma"))
									   )
						);
		$varReturn.= $this->SetGridTabla($varTabla);
		return $varReturn;
		}
	
	function Imprimir ($varEmp	 = NULL, 
					   $varPro	 = NULL, 
					   $varTip   = NULL, 
					   $varIni	 = NULL,  
					   $varFin	 = NULL
					   ) {
		
		$sql_emp = "select concat(id_empresa, ' - ', nombre) as empresa from _sv_empresa where id_empresa=$varEmp;";
		$this->AbrirConexion();
		$rs_emp = $this->Query($sql_emp);
		$this->CerrarConexion();
		$fila_emp = $this->FetchArray($rs_emp);
		$varCadEmp = $fila_emp[0];
		
		if ($varPro == "") {
			$varCadPro = "Todos - TODOS LOS PROVEEDORES";
			}
		else {
			$sql_ven = "select id_proveedor, nombre from _sv_proveedor where id_proveedor = ".$varPro;
			$this->AbrirConexion();
			$rs_pro = $this->Query($sql_ven);
			$this->CerrarConexion();
			$fila_pro = $this->FetchArray($rs_pro);
			$varCadPro = $fila_pro[0]." - ".$fila_pro[1];
			}
			
		$varPro = (isset($varPro) && $varPro<>'')? $varPro:"NULL";
		$varTip = (isset($varTip) && $varTip<>'')? $varTip:"NULL";
		$varIni = (isset($varIni) && $varIni<>'')? "'".FechaMySql($varIni)."'":"NULL";
		$varFin = (isset($varFin) && $varFin<>'')? "'".FechaMySql($varFin)."'":"NULL";
		
		$sql = "call spQuery_Cuentas_Pagar($varEmp, $varPro, $varTip, $varIni, $varFin, NULL, 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%;" cellspacing="0" cellpadding="0">';
			$html.='<tr class="titu_sup" height="25px">';
			$html.='<td colspan="2"><b>'.$this->varTitulo.'</b></td>';
			$html.='</tr>';
			$html.='<tr class="titu_inf">';
			$html.='<td colspan="2">';
			$html.='<div align="left" style=" width:50%;float:left;">'.$_SESSION["_CABR"].' : '.$_SESSION["_NSYS"].'</div>';
    		$html.='<div align="right" style="width:50%;float:left">'.MostrarFechaActual().'</div>';
			$html.='</td>';
			$html.='</tr>';
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
		$html.='<tr>';
		$html.='<td>';
			$html.='<table style="width: 100%;" cellspacing="0" cellpadding="0">';
			$html.='<tr>';
			$html.='<td style="width: 8%; vertical-align: middle; text-align:left;"><b>Empresa </b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b>:</b></td>';
			$html.='<td style="width: 42%; vertical-align: middle; text-align:left;">'.$varCadEmp.'</td>';
			$html.='<td style="width: 50%; vertical-align: middle; text-align:right;"><b></b></td>';
			$html.='</tr>';
			$html.='<tr>';
			$html.='<td style="width: 10%; vertical-align: middle; text-align:left;"><b>Proveedor </b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b>:</b></td>';
			$html.='<td style="width: 40%; vertical-align: middle; text-align:left;">'.$varCadPro.'</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="titu_tabla">';
			$html.='<td width="2" align="center">&nbsp;</td>';
			$html.='<td width="2" align="center">CPRO</td>';
			$html.='<td width="150">PROVEEDOR</td>';
			$html.='<td width="150">DIRECCION</td>';
			$html.='<td width="30" align="center">FECHA</td>';
			$html.='<td width="50" align="center">DOC.VENTA</td>';
			$html.='<td width="30" align="center">CONPAG</td>';
			$html.='<td width="25" align="right" NOWRAP>MONTO</td>';
			$html.='<td width="25" align="right" NOWRAP>SALDO</td>';
			$html.='<td width="40" align="right" NOWRAP>PAGOS</td>';
			$html.='</tr>';
			$html.='</thead>';
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$varTotal1 = 0;
			$varTotal2 = 0;
			$varNumDoc = 0;
			while($fila = $this->FetchArray($rs))	{
				$varTotal1 = $varTotal1 + $fila["TOTAL"];
				$varTotal2 = $varTotal2 + $fila["SALDO"];
				$varNumDoc++;
				$html.='<tr class="cont_tabla">
						<td align="center">&nbsp;</td>
						<td align="center">'.$fila["CPRO"].'</td>
						<td NOWRAP>'.$fila["PROVEEDOR"].'</td>
						<td NOWRAP>'.substr($fila["DIRECCION"],0,50).'</td>
						<td align="center">'.$fila["EMISION"].'</td>
						<td align="center" NOWRAP>'.$fila["DOCUMENTO"].'</td>
						<td align="center">'.$fila["CONDICION"].'</td>
						<td align="right">'.number_format($fila["TOTAL"],"2",".",",").'</td>
						<td align="right">'.number_format($fila["SALDO"],"2",".",",").'</td>
						<td align="right"></td>
						</tr>';
				}
			$html.='<tr class="titu_tabla">';
			$html.='<td colspan="3" align="left"><strong>Totales :</strong></td>';
			$html.='<td colspan="3" align="left">'.number_format($varNumDoc,"0",".",",").' Documentos</td>';
			$html.='<td align="right"></td>';
			$html.='<td align="right">'.number_format($varTotal1,"2",".",",").'</td>';
			$html.='<td align="right">'.number_format($varTotal2,"2",".",",").'</td>';
			$html.='<td align="right"></td>';
			$html.='</tr>';
	
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
				
		$html.='</table>';
		return $html;
		}
			
	function ProcesoGrabar($varForm = NULL) {
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$varFec = FechaMySql($varForm[femi]);	
		$sql = "CALL spMante_Pago_CxP($varForm[txtide],  '$varForm[tipo]', '$varForm[txtnum]', '$varFec', $varForm[txtpago], '$varForm[txtobs]', $varUsr);";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		$Fila = $this->FetchArray($rs);
		return $Fila[0];
		}		
	}
	
?>