<?php
//include "clase.conexion.php";
class clsPlanillaSubirConceptos extends clsConexion{
	public $varTitulo 	= "SUBIR CONCEPTOS EN LOTE";
	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", "tamano"=>"100",
										 	 "campo" =>array("tipo"=>"texto", "id"=>"cod", "name"=>"cod", "valor"=>$_POST["cod"], 
										 	 "alinear"=>"center", "ancho"=>"70")),								   
									   array("titulo"=>"Descripci&oacute;n :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo" =>array("tipo"=>"texto", "id"=>"nam", "name"=>"nam", "valor"=>$_POST["nam"], "ancho"=>"306")),									   									   ),
						"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
					   ) {
		$varCod = (isset($varForm["cod"]) && $varForm["cod"]<>"")?"'".$varForm["cod"]."'":"NULL";
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";		
		$sql = "call spQuery_PeriodoPago($varCod, '$varForm[nam]', 'A', $varFie, $varOrd)";
		
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"800", "sql_cab"=>$sql,	"pages"=>20,
					    "conte"=>array(array("titulo"=>"CPER",     "ancho"=>"40" , "alinear"=>"center"), 
									   array("titulo"=>"PERIODO",  "ancho"=>"250", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("titulo"=>"CTIP",     "ancho"=>"50",  "fijo"=>"SI", "alinear"=>"center"),
									   array("titulo"=>"TIPO",     "ancho"=>"100", "fijo"=>"SI"),
									   array("titulo"=>"INICIO",   "ancho"=>"50",  "fijo"=>"SI", "alinear"=>"center"),
									   array("titulo"=>"FIN",      "ancho"=>"50",  "fijo"=>"SI", "alinear"=>"center"),
									   array("titulo"=>"DIAS",     "ancho"=>"50",  "fijo"=>"SI", "alinear"=>"center"),
									   array("titulo"=>"LAB.",     "ancho"=>"50",  "fijo"=>"SI", "alinear"=>"center"),
									   array("titulo"=>"FER.",     "ancho"=>"50",  "fijo"=>"SI", "alinear"=>"center"),
									   array("titulo"=>"ESTADO",   "ancho"=>"50",  "fijo"=>"SI", "alinear"=>"center"),
									   array("titulo"=>"FCREADO",  "ancho"=>"100","oculto"=>"SI"),
									   array("titulo"=>"FUPDATE",  "ancho"=>"100","oculto"=>"SI"),
									   array("titulo"=>"UCREADO",  "ancho"=>"50", "oculto"=>"SI"),
									   array("titulo"=>"UUPDATE",  "ancho"=>"50", "oculto"=>"SI")
									   ), 
						"boton"=>array(array("tipo"=>"img", "id"=>"bbus", "name"=>"bbus", "img"=>"../images/16x16/inbox_download.png", 
												 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>SUBIR CONCEPTOS</b> EN EL PERIODO<br><b>"),
																	 array("valor"=>0, "tipo"=>"INDEX"),
																	 array("valor"=>" - "),
																	 array("valor"=>1, "tipo"=>"INDEX"),
																	 array("valor"=>"</b>")
																	),
												 "click"=>array("funcion"=>"FormuRegistroJS",
																"param"=>array(array("valor"=>0, 	  "tipo"=>"INDEX"),
																			   array("valor"=>"EDIT", "tipo"=>"TEXTO")
																			  )
																)
												 
												)
										)
							);
		return $this->SetGridTabla2($varTabla);
		}
	function FormuRegistro($varCod=NULL,$varOpt=NULL) {
		$sql = "select P.id_periodo_pago as CPER, P.nombre as PERIODO,
				PF.id_planilla as CPF, PF.nombre as FIJA,
				PH.id_planilla AS CPH, PH.nombre AS HONORARIOS
				from _srh_periodo_pago P
				left join _srh_planilla PF on P.id_periodo_pago = PF.id_periodo_pago and PF.id_tipo_planilla = 1
				left join _srh_planilla PH on P.id_periodo_pago = PH.id_periodo_pago and PH.id_tipo_planilla = 2
				WHERE P.id_periodo_pago = $varCod;";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		$fila = $this->FetchArray($rs);
		$sqltip = "call spQuery_Lista_Tipo_Concepto('', NULL,NULL);";
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"450", "clase"=>"FormTitulo", "titulo"=>$this->varTitulo,
						"formu"=>array("id"=>"form", "name"=>"form", "metodo"=>"POST"),						
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$this->varTitulo),
									   array("tipo"=>"oculto", "id"=>"opt", "name"=>"opt", "valor"=>$varOpt)
									  ),
						"conte"=>array(array("titulo"=>"Periodo :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"90",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"pcod", "name"=>"pcod", "clase"=>"inputDisable",
											 										 "valor"=>$fila["CPER"], "alinear"=>"center", "locked"=>"SI", 
																					 "ancho"=>"40"),
																			   array("tipo"=>"texto", "id"=>"pnom", "name"=>"pnom", "clase"=>"inputDisable",
											 										 "valor"=>$fila["PERIODO"], "locked"=>"SI", "ancho"=>"270")))
											 ),
									   array("titulo"=>"Planilla Fija :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"90",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"fcod", "name"=>"fcod", "clase"=>"inputDisable",
											 										 "valor"=>$fila["CPF"], "alinear"=>"center", "locked"=>"SI", 
																					 "ancho"=>"40"),
																			   array("tipo"=>"texto", "id"=>"fnom", "name"=>"fnom", "clase"=>"inputDisable",
											 										 "valor"=>$fila["FIJA"], "locked"=>"SI", "ancho"=>"270")))
											 ),
									   array("titulo"=>"Honorarios :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"90",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"hcod", "name"=>"hcod", "clase"=>"inputDisable",
											 										 "valor"=>$fila["CPH"], "alinear"=>"center", "locked"=>"SI", 
																					 "ancho"=>"40"),
																			   array("tipo"=>"texto", "id"=>"hnom", "name"=>"hnom", "clase"=>"inputDisable",
											 										 "valor"=>$fila["HONORARIOS"], "locked"=>"SI", "ancho"=>"270")))
											 ),
									   array("titulo"=>"Tipo Concepto :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"90",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"select", "id"=>"tip", "name"=>"tip", "sql"=>$sqltip, 
																					 "titulo"=>"--[[SELECCIONAR]]--", "ancho"=>"200",
																					 "reload"=>"xajax_comboConceptoRemunerativo(this.value, 
																					 											'divCon','con','314')")	 
																			   ))
											 ),
									   array("titulo"=>"Concepto :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"90",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"select", "id"=>"con", "name"=>"con", "div"=>"divCon",
																			   		 "titulo"=>"--[[SELECCIONAR]]--", "ancho"=>"314")	 
																			   ))
											 ),
									   array("titulo"=>"Archivo :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"90",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"arc", "name"=>"arc", "clase"=>"inputDisable",
											 										 "locked"=>"SI", "ancho"=>"295"),
																			   array("tipo"=>"img", "id"=>"upload_button", 
																			   		 "img"=>"../images/16x16/inbox_download.png")
																			  )
															)
											 ),
									   ),
						"boton"=>array(array("tipo"=>"button", "id"=>"bsav", "name"=>"bsav", "ancho"=>"100",
											 "clase"=>"boton_verificar", "texto"=>"Verificar", "click"=>"javascrip:xVerificarJS()"),			  
									   array("tipo"=>"button", "id"=>"breg", "name"=>"breg", "ancho"=>"100",
											 "clase"=>"boton_cancelar", "texto"=>"Cancelar", "click"=>"javascrip:xajax_xRegresar()"),
									)
						);
		return $this->setTablaForm($varTabla);	
		}
	
	function FormuVerificar($varForm = NULL) {
		$varFile = "file_conceptos/".$varForm["arc"];
		$objExcel = new Spreadsheet_Excel_Reader();
		$objExcel->setOutputEncoding('CP1251');
		$objExcel->read($varFile);
		$NumFils = $objExcel->sheets[0]['numRows'];
		$NumCols = $objExcel->sheets[0]['numCols'];
		$varTabla = "<form id=\"formVerif\" method=\"POST\" name=\"formVerif\">";
		$varTabla.= "<table id=\"tabVerif\"  name=\"tabVerif\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" ";
		$varTabla.= "style=\"padding: 0; margin-top:5px; margin-bottom:10px; border-collapse:collapse;\"  ";
		$varTabla.= "width=\"600\" class=\"matriz\" >";	
		$varTabla.= "<thead>";
		$varTabla.= "<tr>";
		$varTabla.= "<th align=\"center\" class=\"FormTitulo\" colspan=\"".($NumCols+9)."\" height=\"28\">";
		$varTabla.= "<input id=\"bsav\" class=\"boton_guardar\" type=\"button\" style=\"cursor:pointer; width:210px;\" ";
		$varTabla.= "onclick=\"javascrip:xGrabarJS()\" value=\"Registrar Conceptos en Lote\" name=\"bsav\">";
		$varTabla.= "</th>";
		$varTabla.= "</tr>";
		for($fil=1; $fil<=$NumFils; $fil++) {
			$varTra = $objExcel->sheets[0]['cells'][$fil][1];
			if ($fil%2==0) $varTabla.= "<tr class=\"par\">";
			else $varTabla.= "<tr class=\"impar\">";
			if ($fil==1) {
				$varTabla.= "<th align=\"center\" class=\"FormTitulo\" height=\"28\">OPT</th>";
				$varTabla.= "<th align=\"center\" class=\"FormTitulo\" height=\"28\">PLA</th>";
				$varTabla.= "<th align=\"center\" class=\"FormTitulo\">CON</th>";
				$varTabla.= "<th align=\"center\" class=\"FormTitulo\">CEMP</th>";
				$varTabla.= "<th align=\"center\" class=\"FormTitulo\">EMPRESA</th>";
				$varTabla.= "<th align=\"center\" class=\"FormTitulo\">CAGE</th>";
				$varTabla.= "<th align=\"center\" class=\"FormTitulo\">AGENCIA</th>";
				$varTabla.= "<th align=\"center\" class=\"FormTitulo\">CTIO</th>";
				$varTabla.= "<th align=\"center\" class=\"FormTitulo\">TIPOPLA</th>";
				}
			else {
				$sql = "select TE.id_trabajador as CTRA, CONCAT(T.apellido_paterno, ' ', T.apellido_materno, ' ', T.nombre) as TRABAJADOR,
						TE.id_empresa as CEMP, E.nombre as EMPRESA,
						TE.id_agencia as CAGE, A.nombre as AGENCIA,
						TE.id_tipo_planilla as CPLA, P.nombre as PLANILLA,
						TE.id_estado_trabajador as CEST, S.nombre AS ESTADO
						from _srh_trabajador_empresa TE
						inner join _sv_trabajador T ON TE.id_trabajador = T.id_trabajador
						inner join _sv_empresa E ON TE.id_empresa = E.id_empresa
						inner join _srh_agencia A ON TE.id_agencia = A.id_agencia
						inner join _srh_tipo_planilla P ON TE.id_tipo_planilla = P.id_tipo_planilla
						inner join _srh_estado_trabajador S ON TE.id_estado_trabajador = S.id_estado_trabajador
						where TE.id_trabajador = $varTra;";
				$fila = $this->GetFilas($sql);
				$varPla = ($fila["CPLA"]=="1")?$varForm["fcod"]:(($fila["CPLA"]=="2")?$varForm["hcod"]:"");
				
				$varTabla.= "<td class=\"interior\" nowrap=\"nowrap\" height=\"20\" align=\"center\">";
				$varTabla.= "<img style=\"cursor:pointer\" onmouseout=\"UnTip()\" onmouseover=\"MensajeTip('HAGA CLICK PARA <b>QUITAR EL TRABAJADOR</b>";
				$varTabla.= "<br>".$fila["CTRA"]." - ".$fila["TRABAJADOR"]."')\" ";
				$varTabla.= "onclick=\"javascript:xEliminarJS('".$fila["CTRA"]."','".$fila["TRABAJADOR"]."',this)\" ";
				$varTabla.= "src=\"../images/16x16/delete.png\" name=\"bdel\" id=\"bdel\">";
				$varTabla.= "</td>";
				$varTabla.= "<td class=\"interior\" nowrap=\"nowrap\" height=\"20\" align=\"center\">";
				$varTabla.= "<input id=\"pla[".$fila["CTRA"]."]\"  name=\"pla[".$fila["CTRA"]."]\" class=\"inputDisable\" type=\"text\" ";
				$varTabla.= "style=\"width:30px; height:18px; text-align:center\" readonly value=\"".$varPla."\" >";
				$varTabla.= "</td>";
				
				$varTabla.= "<td class=\"interior\" nowrap=\"nowrap\" align=\"center\">";
				$varTabla.= "<input id=\"con[".$fila["CTRA"]."]\"  name=\"con[".$fila["CTRA"]."]\" class=\"inputDisable\" type=\"text\" ";
				$varTabla.= "style=\"width:40px; height:18px; text-align:center\" readonly value=\"".$varForm["con"]."\" >";
				$varTabla.= "</td>";
				
				$varTabla.= "<td class=\"interior\" nowrap=\"nowrap\" align=\"center\">";
				$varTabla.= "<input id=\"emp[".$fila["CTRA"]."]\"  name=\"emp[".$fila["CTRA"]."]\" class=\"inputDisable\" type=\"text\" ";
				$varTabla.= "style=\"width:30px; height:18px; text-align:center\" readonly value=\"".$fila["CEMP"]."\" >";
				$varTabla.= "</td>";
				$varTabla.= "<td class=\"interior\" nowrap=\"nowrap\" align=\"center\">".$fila["EMPRESA"]."</td>";
				$varTabla.= "<td class=\"interior\" nowrap=\"nowrap\" align=\"center\">";
				$varTabla.= "<input id=\"age[".$fila["CTRA"]."]\"  name=\"age[".$fila["CTRA"]."]\" class=\"inputDisable\" type=\"text\" ";
				$varTabla.= "style=\"width:30px; height:18px; text-align:center\" readonly value=\"".$fila["CAGE"]."\" >";
				$varTabla.= "</td>";
				$varTabla.= "<td class=\"interior\" nowrap=\"nowrap\" align=\"center\">".$fila["AGENCIA"]."</td>";
				$varTabla.= "<td class=\"interior\" nowrap=\"nowrap\" align=\"center\">".$fila["CPLA"]."</td>";
				$varTabla.= "<td class=\"interior\" nowrap=\"nowrap\"  align=\"center\">".$fila["PLANILLA"]."</td>";
				}
			for($col=1; $col<=$NumCols; $col++) {
				$MiValor = $objExcel->sheets[0]['cells'][$fil][$col];
				if ($fil==1) $varTabla.= "<th align=\"center\" class=\"FormTitulo\">".$MiValor."</th>";
				else {
					if($fila["CTRA"]) { 
						$varClas = "interior"; 
						$varClasCaj = "inputDisable";
						$varError= $fila["TRABAJADOR"];
						$varEst = "S";
						}
					else {
						$varClas = "interiorError";
						$varClasCaj = "inputError";
						$varError= "NO EXISTE TRABAJADOR";
						$varEst = "N";
						}
					if ($col==1) {
						$varTabla.= "<td class=\"interior\" nowrap=\"nowrap\" align=\"center\">";
						$varTabla.= "<input id=\"tra[".$fila["CTRA"]."]\"  name=\"tra[".$fila["CTRA"]."]\" class=\"".$varClasCaj."\" type=\"text\" ";
						$varTabla.= "style=\"width:70px; height:18px; text-align:center\" readonly value=\"".$MiValor."\" >";
						$varTabla.= "<input id=\"est[".$fila["CTRA"]."]\"  name=\"est[".$fila["CTRA"]."]\" type=\"hidden\" value=\"".$varEst."\" >";
						$varTabla.= "</td>";
						}
					else if ($col==2) {
						$varTabla.= "<td class=\"$varClas\" nowrap=\"nowrap\">".$MiValor." | ".$varError."</td>";
						}
					else {
						$varTabla.= "<td class=\"interior\" nowrap=\"nowrap\" >";
						$varTabla.= "<input id=\"mon[".$fila["CTRA"]."]\"  name=\"mon[".$fila["CTRA"]."]\" class=\"inputDisable\" type=\"text\" ";
						$varTabla.= "style=\"width:60px; height:18px; text-align:right\" readonly value=\"".number_format($MiValor,"2",".",",")."\" >";
						$varTabla.= "</td>";
						}
					}
				}
			if ($fil==1) $varTabla.= "</thead>";
			$varTabla.= "</tr>";
			}
		$varTabla.= "</table>";
		$varTabla.= "</form>";
		return $varTabla;
		}
		
	function ProcesoGrabar($varForm = NULL) {		
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		foreach ($varForm["tra"] as $varIde=>$varTra) {
			$varPla = $varForm["pla"][$varIde];
			$varCon = $varForm["con"][$varIde];
			$varEmp = $varForm["emp"][$varIde];
			$varAge = $varForm["age"][$varIde];
			$varMon =str_replace(",","",$varForm["mon"][$varIde]);
			$sql = "call spMante_Planilla_Detalle('NEW', $varPla, $varTra, '$varCon', $varEmp, NULL, $varAge, $varMon, 'Cargado en Lote', $varUsr);";
			$this->AbrirConexion();
			$this->Query(utf8_decode($sql));
			$this->CerrarConexion();
			}
		return "OK";
		}

	}
	
?>