<?php
//include "clase.conexion.php";
class clsAsignacionVenta extends clsConexion{
	public $varTitulo = "LIQUIDACION DE DOCUMENTOS";
	function FormuBusqueda() {
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"430", "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"=>"Codigo :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"cod", "name"=>"cod", "alinear"=>"center", "ancho"=>"80"),
																			   array("tipo"=>"simple", "valor"=>"Nro. Asignacion"))
														 	)
											 ),									   
									   array("titulo"=>"Vehiculo :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"vcod", "name"=>"vcod", "valor"=>$_POST[vcod], 
																					 "ancho"=>"50", "alinear"=>"center", 
																					 "blur"=>"xajax_seleccionar_vehiculo(this.value)", 
																					 "dclick"=>"fn_cargar_listado_vehiculo('vcod', 'vnom')"),
																			   array("tipo"=>"text", "id"=>"vnom", "name"=>"vnom", "valor"=>NULL, 
																					 "ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
															)
											 ),	
									   array("titulo"=>"Conductor :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											"campo"=>array("tipo"=>"varios",
														   "contenido"=>array(array("tipo"=>"text", "id"=>"ccod", "name"=>"ccod", "valor"=>$_POST["ccod"], 
																					"ancho"=>"50",  "alinear"=>"center", 
																					"blur"=>"xajax_seleccionar_conductor(this.value)", 
																					"dclick"=>"fn_cargar_listado_conductor('ccod', 'cnom')"),
																			  array("tipo"=>"text", "id"=>"cnom", "name"=>"cnom", 
																					"ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																			  )
														   )
										   ),
									 array("titulo"=>"F.Inicial :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"fecha", "id"=>"fini", "name"=>"fini", "alinear"=>"center"))
														 	)
											 ),
									   array("titulo"=>"F.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()")
									 )
					);
		return $this->setTablaForm($varTabla);
		}
	function GridDatos($varForm  = NULL, 
					   $varField = NULL, 
					   $varOrder = NULL
					   )  {
						  
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";
		$varCod = (isset($varForm[cod]) && $varForm[cod]<>'')?$varForm[cod]:"NULL";
		$varVeh = (isset($varForm[vcod]) && $varForm[vcod]<>'')?$varForm[vcod]:"NULL";
		$varCon = (isset($varForm[ccod]) && $varForm[ccod]<>'')?$varForm[ccod]:"NULL";
		$varIni = (isset($varForm[fini]) && $varForm[fini]<>'')?"'".FechaMySql($varForm[fini])."'":"NULL";
		$varFin = (isset($varForm[ffin]) && $varForm[ffin]<>'')?"'".FechaMySql($varForm[ffin])."'":"NULL";
		
		$sql = "call spQuery_Asignacion($varCod, $varVeh, $varCon, $varIni, $varFin, $varFie, $varOrd)";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"1000", "sql_cab"=>$sql,	"pages"=>20,
					    "conte"=>array(array("titulo"=>"IDE",    "ancho"=>"30" , "alinear"=>"center"), 
									   array("titulo"=>"FECHA", "ancho"=>"80", "fijo"  =>"SI"),
									   array("titulo"=>"CVEH",  "ancho"=>"30", "alinear"=>"center"),
									   array("titulo"=>"PLACA", "ancho"=>"50"),
									   array("titulo"=>"VEHICULO", "ancho"=>"200", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("titulo"=>"CCON", "ancho"=>"30", "alinear"=>"center"),
									   array("titulo"=>"CONDUCTOR","ancho"=>"200", "fijo"=>"SI"),
									   array("titulo"=>"DOC'S", "ancho"=>"50", "alinear"=>"right", "decimal"=>"2"),
									   array("titulo"=>"MONTO", "ancho"=>"50", "alinear"=>"right", "decimal"=>"2", "funcion"=>array("autosuma")),
									   array("titulo"=>"FCREADO", "ancho"=>"100", "fijo"=>"SI"),
									   array("titulo"=>"FUPDATE", "ancho"=>"100", "oculto"=>"SI"),
									   array("titulo"=>"UCREADO", "ancho"=>"100", "alinear"=>"center", "fijo"=>"SI"),
									   array("titulo"=>"UUPDATE", "ancho"=>"100", "alinear"=>"center", "oculto"=>"SI")
									   ), 
						"boton"=>array(array("tipo"=>"img", "img"=>"images/b_table.png", 
												 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>VER EL DETALLE</b> DE LA ASIGNACION<br><b>Nro. Asignacion :</b> "),
																	 array("valor"=>0, "tipo"=>"INDEX"),
																	 array("valor"=>"<br><b>Fecha : </b>"),
																	 array("valor"=>1, "tipo"=>"INDEX"),
																	 array("valor"=>"<br><b>Monto : </b>"),
																	 array("valor"=>8, "tipo"=>"INDEX"),
																	),
												 "click"=>array("funcion"=>"xajax_xFormuDetalle",
																"param"=>array(array("valor"=>0, "tipo"=>"INDEX"))
																)
												 
												)
										)
							);
		return $this->SetGridTabla2($varTabla);
		}
	
	function FormuDetalle($varCod = NULL) 
		{
		$sql = "CALL spQuery_Asignacion_Detalle($varCod);";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		$fila = $this->FetchArray($rs);		
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"450", "clase"=>"FormTitulo", "titulo"=>$this->varTitulo." - SELECCIONE DOCUMENTOS A LIQUIDAR",
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"asig", "name"=>"asig", "valor"=>$fila["ASIG"])
									  ),
						"conte"=>array(array("titulo"=>"Nro Asig. :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"90",
											 "campo"=>array("tipo"=>"simple", "valor"=>$fila["ASIG"]),
											 ),
									   array("titulo"=>"F.Reparto :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"simple", "valor"=>$fila["FECHA"])
											 ),
									   array("titulo"=>"Vehiculo :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"simple", "ancho"=>"200",
																					 "valor"=>$fila["CVEH"]." - ".$fila["PLACA"]." : ".$fila["VEHICULO"]),
																			   array("tipo"=>"simple", "valor"=>"Costo S/. : ", "clase_in"=>"FormCampo1", "ancho"=>"70"),
																			   array("tipo"=>"simple", "valor"=>number_format($fila["MONTO_VEHICULO"],"2",".",","),
																			   		 "alinear"=>"right")
																			   )
															)
											 ),	
									   array("titulo"=>"Conductor :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											"campo"=>array("tipo"=>"varios",
														   "contenido"=>array(array("tipo"=>"simple",  "ancho"=>"200", 
														   							"valor"=>$fila["CCON"]." - ".$fila["CONDUCTOR"]),
																			   array("tipo"=>"simple", "valor"=>"Costo S/. : ", "clase_in"=>"FormCampo1", "ancho"=>"70"),
																			   array("tipo"=>"simple", "valor"=>number_format($fila["MONTO_CONDUCTOR"],"2",".",","),
																			   		 "alinear"=>"right")
																			   )
														   )
										   ),
									   array("titulo"=>"Cant.Doc. :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"simple", "valor"=>number_format($fila["NUM_DOC"],"2",".",","))
											 ),	
									   array("titulo"=>"Monto Asig. :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											"campo"=>array("tipo"=>"simple", "valor"=>number_format($fila["MON_DOC"],"2",".",","))
										   ),
									   array("titulo"=>"Fecha Creado :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											"campo"=>array("tipo"=>"simple", "valor"=>$fila["FECHA_CREADO"])
										   ),
									   array("titulo"=>"Usuario :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											"campo"=>array("tipo"=>"simple", "valor"=>$fila["USER_CREADO"])
										   ),
									   array("titulo"=>"F.Liquid. :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"fecha", "id"=>"fliq", "name"=>"fliq", "alinear"=>"center"))
														 	)
											 ),
									   ),
					  "boton"=>array(array("tipo"=>"button", "id"=>"bsav", "name"=>"bsav", "ancho"=>"160",
										   "clase"=>"boton_guardar", "texto"=>"Registrar Liquidacion", "click"=>"javascrip:xGuardarJS('NEW')"),
					  				 array("tipo"=>"button", "id"=>"breg", "name"=>"breg", "ancho"=>"100",
											 "clase"=>"boton_cancelar", "texto"=>"Regresar", "click"=>"javascrip:xajax_xRegresarPrincipal()")
									 )
					);
		
		$varReturn.= '<form id="form" name="form" method="post">';
		$varReturn.= $this->setTablaForm($varTabla);
		
		$sql = "CALL spQuery_Asignacion_Documentos_Detalle_Liquidar($varCod);";
		
		$varReturn.= "<br><a href=\"javascript:marcar_todo('form')\"><b>Seleccionar Todos</b></a> | ";
		$varReturn.= "<a href=\"javascript:marcar_ninguno('form')\"><b>Seleccionar Ninguno</b></a>";
		$varReturn.= "<table id=\"theTable\" style=\"padding: 0; margin-top:5px; margin-bottom:10px; border-collapse:collapse;\" ";
		$varReturn.= "width=\"900\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" >";
		$varReturn.= "<thead>";
		$varReturn.= "<tr>";
		$varReturn.= "<th height=\"35\" width=\"20\" class=\"sortable\">SEL</th>";
		$varReturn.= "<th width=\"20\"  class=\"sortable\">IDE</th>";
		$varReturn.= "<th width=\"50\"  class=\"sortable\">DOCUMENTO</th>";
		$varReturn.= "<th width=\"200\" class=\"sortable\">CLIENTE</th>";
		$varReturn.= "<th width=\"200\"  class=\"sortable\">DIRECCION</th>";
		$varReturn.= "<th width=\"200\"  class=\"sortable\">RUTA</th>";
		$varReturn.= "<th width=\"200\"  class=\"sortable\">VENDEDOR</th>";
		$varReturn.= "<th width=\"70\"  class=\"sortable\">MONTO</th>";
		$varReturn.= "<th width=\"70\"  class=\"sortable\">SALDO</th>";
		$varReturn.= "<th width=\"70\"  class=\"sortable\">LIQUIDAR</th>";
		$varReturn.= "</tr>";
		$varReturn.= "</thead>";
		$varReturn.= "<tbody id=cuerpoTabla>";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		while($fila = $this->FetchArray($rs)) {
			$j++;
			$varReturn.=($k%2==0)?"<tr class=\"par\">":"<tr class=\"impar\">";
			$varReturn.= "<td height=20 valign=middle align=ceenter class=\"interior\">";
			$varReturn.= "<input name=\"sel[".$fila["IDE"]."]\" id=\"sel\" type=\"checkbox\" value=\"".$fila["IDE"]."\" style=\"width:12px; height:12px\">";
			$varReturn.= "</td>";			
			$varReturn.= "<td valign=middle align=center  class=\"interior\"><b>".$fila["IDE"]."</b></td>";
			$varReturn.= "<td valign=middle class=\"interior\" align=\"center\">".$fila["NDOC"]."</td>";
			$varReturn.= "<td valign=middle class=\"interior\" nowrap>".$fila["CCLI"]." - ".$fila["CLIENTE"]."</td>";
			$varReturn.= "<td valign=middle class=\"interior\" nowrap>".$fila["DIRECCION"]."</td>";
			$varReturn.= "<td valign=middle class=\"interior\" nowrap>".$fila["RUTA"]."</td>";
			$varReturn.= "<td valign=middle class=\"interior\" nowrap>".$fila["VENDEDOR"]."</td>";
			$varReturn.= "<td height=20 valign=middle align=right  class=\"interior\">".number_format($fila["TOTAL"], 2,'.',',')."</td>";
			$varReturn.= "<td height=20 valign=middle align=right  class=\"interior\">".number_format($fila["SALDO"], 2,'.',',')."</td>";
			$varReturn.= "<td valign=middle align=center class=\"interior\">";
			$varReturn.= "<input type=\"text\" name=\"mliq[".$fila["IDE"]."]\" id=\"mliq[".$fila["IDE"]."]\" onkeypress=\"return SoloNumeros(event)\" ";
			$varReturn.= "style=\"width:50px;text-align:center\" class=\"inputLogro\" value=\"".$fila["SALDO"]."\" />";
			$varReturn.= "<input type=\"hidden\" name=\"ndoc[".$fila["IDE"]."]\" id=\"ndoc[".$fila["IDE"]."]\" value=\"".$fila["NDOC"]."\"/>";
			$varReturn.= "<input type=\"hidden\" name=\"msal[".$fila["IDE"]."]\" id=\"msal[".$fila["IDE"]."]\" value=\"".$fila["SALDO"]."\"/>";
			$varReturn.= "<input type=\"hidden\" name=\"ccli[".$fila["IDE"]."]\" id=\"ccli[".$fila["IDE"]."]\" value=\"".$fila["CCLI"]."\"/>";
			$varReturn.= "</td>";
			$varReturn.= "</tr>";
			}
		$varReturn.= "</tbody>";
		$varReturn.= "</table>";
		$varReturn.= "</form>";	
		return $varReturn;
		}
					
	function ProcesoGrabar($varForm = NULL) {		
		$varUsr = (isset($_SESSION["_CUSE"]))  ? $_SESSION["_CUSE"] : "1";
		
		foreach ($varForm['sel'] as $Codigo) {
			if($varForm[sel][$Codigo]) {
				$varVen = $Codigo;
				$varCli = $varForm[ccli][$Codigo];
				$varSal = $varForm[msal][$Codigo];
				$varMon = $varForm[mliq][$Codigo];
				$varDoc = "PLA-".$varForm[asig];
				$varObs = "LIQUIDACION PLANILLA ".$varForm[asig];
				$varFec = FechaMySql($varForm[fliq]);
				$sql.= "CALL spMante_Pago_CxC($varCli, $varVen, 1, '$varDoc', '$varFec', $varMon, $varSal,  '$varObs', $varUsr);";
				
				}
			}
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		return "OK";
		}
		
	}
	
?>