<?php use_helper('UrlFunctions'); ?>
<script>

acutalizarEspecialidad = function(destino,elm){
  $(destino).options.length = 1;
  var url = '<?=buildUrl('ajax/request')?>';
  var pars = 'type=getEspecialidad&nombreElm='+destino+'&value='+elm.value;
  
	var myAjax = new Ajax.Request(
                            		url, 
                            		{
                            			method: 'get', 
                            			parameters: pars, 
                            			onComplete: actualizaJSON
                            		});
}

</script>
<script>
actualizaJSON = function(request, json){
  var numeroElementos = json.length;
  for (var i = 0; i < numeroElementos; i++){
    if($(json[i][0])){
  	  switch ($(json[i][0]).tagName){
  	    case 'TD'      : Element.update(json[i][0], json[i][1]); break;
  	    case 'INPUT'   : $(json[i][0]).value=json[i][1]; break;
  	    case 'SELECT'  : 
  	      var objDDL = $(json[i][0]);
  	      if(typeof(cont) == 'undefined'){
  	        var cont = 0;
  	        objDDL.options.length = 1;
  	      }
  	      var a_contentValues = json[i][1].split("_;_");
      		var value	= a_contentValues[0];
      		var text	= a_contentValues[1];
  	      var option	= new Option(text,value);
      		try{
      			objDDL.add(option,null);
      		}catch (e){
      			objDDL.add(option,-1);
      		}
  	      cont++;
  	    break;
  	  }
    }else{
      if(json[i][0].indexOf("function") > -1){
        eval("scriptEval = "+json[i][0]);
        scriptEval();
      }
    }
  }
}

actualizarMontoTardanza = function(destino,elm){
  var url = '<?=buildUrl('ajax/request')?>';
  var pars = 'type=getMontoTardanza&nombreElm='+destino+'&value='+elm.value;
  
	var myAjax = new Ajax.Request(
                            		url, 
                            		{
                            			method: 'get', 
                            			parameters: pars, 
                            			onComplete: actualizaJSON
                            		});
 
  destino = 'FechaTardanza';
  var url = '<?=buildUrl('ajax/request')?>';
  var pars = 'type=getFechaAbiertaAS&nombreElm='+destino+'&value='+elm.value;
  
	var myAjax = new Ajax.Request(
                            		url, 
                            		{
                            			method: 'get', 
                            			parameters: pars, 
                            			onComplete: actualizaJSON
                            		});
}
</script>
<?php
	 //Obtener Dia Abierto del balance diario
	

	 
	 
$o_ConceptosFN = new CrPlanillaConceptoPerFN();
$o_ConceptosFN->setPeriodo($periodo);
$o_ConceptosFN->setEstado('A');
$o_ConceptosFN->setVisualizarPlanilla('1');
$a_o_ConceptosFN = $o_ConceptosFN->select(); 
	

//Separar por ubicacion y ordenado por su numero de posicion

if(is_array($a_o_ConceptosFN)){
	foreach ($a_o_ConceptosFN as $key => $o_ConceptosFN){
		$a_Conceptos[$o_ConceptosFN->getIdConcepto()] = $o_ConceptosFN->getCodigo();
		switch($o_ConceptosFN->getUbicacion()){
			case "ING":
				$a_con_ingresos[$o_ConceptosFN->getPosicionPlanilla()] = $o_ConceptosFN;
				break;
			case "DES":
				$a_con_descuentos[$o_ConceptosFN->getPosicionPlanilla()] = $o_ConceptosFN;	
				break;
			case "APO":
				$a_con_aportes[$o_ConceptosFN->getPosicionPlanilla()] = $o_ConceptosFN;	
				break;
		}
	}
}
ksort($a_con_ingresos);
echo "<pre>";
//print_r($a_con_ingresos);	
echo "</pre>";	

ksort($a_con_descuentos);
echo "<pre>";
//print_r($a_con_descuentos);	
echo "</pre>";	



$o_planillaColaborador = new CrPlanillaFN();
$o_planillaColaborador->setPeriodo($periodo);
if( myUser::getUser()->getRoleId() == "sys.res") {
	
	$o_planillaColaborador->setIdPadreRes(myUser::getUser()->getIdColaborador());
}
$a_Colaborador = $o_planillaColaborador->getColaboradores();
$a_o_planillaColaborador = $o_planillaColaborador->select();
foreach ($a_o_planillaColaborador as $key => $o_planillaColaborador){
	$a_planilla_Col_valor[$o_planillaColaborador->getIdColaborador()][$a_Conceptos[$o_planillaColaborador->getIdConcepto()]] = $o_planillaColaborador->getValor();
}



if(isset($control)){
	
	
	$bResult = 1;

 	if($bResult){
	    ?>
	    <script language="JavaScript">
	    parent.objMainTab.reqBody('<?=buildUrl('planilla/list')?>','&block=002&insert=1');
	    parent.objMainTab.windowsCloseAll();
	    </script>
	    <?
	}else{
    	$url = buildUrl('planilla/add', "block=".$block."&error=1");
  	}
  	exit;
}
 
 
 

 ?>
 
<img src="/images/common/spacer.gif" width="10">
<table class="cuerpo" width="98%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td height="0">
      <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td class="titleBody">
            <li>Planilla</li>
            <span class="subtitle"><I>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:::&nbsp;Cierre de Planilla</I></span></td>
          <td align="right" valign="baseline">&nbsp;</td>
        </tr>
      </table>
    </td>
  </tr>
     
  <tr>
    <td valign="top">
      <form id="form1" name="form1" method="POST">
      <input type="hidden" name="control" value="1">
        <table hspace="20px" class="marco" width="99%">
          <tr>
            <td valign="top">
              <table width="100%" border="0" cellpadding="5" cellspacing="0" style="background: #E3E9E2">
          		<tr>
    				  <td height="0">
    				  <fieldset>
    					<legend class="fielsettex"><I>Datos Generales</I></legend>
    					 <table width="100%" border="0" cellspacing="6" cellpadding="0">
							  <tr>
                  					<td nowrap="nowrap" width="10%" class="cabecera"><label class="label req" for="txtAdministrador" >Periodo</label></td>
  								 	<td nowrap="nowrap"><label class="label req" for="txtAdministrador" ><?=$periodo?></label></td>
					          </tr>
						</table>
						
						<table width="100%" border="1" cellspacing="0" cellpadding="2">
						
							  <tr>
                  					<td nowrap="nowrap" colspan="2" class="cabecera" width="30%"></td>
  								 	<td nowrap="nowrap" align="center" colspan="<?=count($a_con_ingresos)?>"><label class="label" >Ingresos (S/.)</label></td>
  								 	<td nowrap="nowrap" align="center" colspan="<?=count($a_con_descuentos)?>"><label class="label" >Descuentos (S/.)</label></td>
  								 	<td></td>
  								 	<td nowrap="nowrap" align="center" colspan="<?=count($a_con_aportes)?>"><label class="label" >Aportes (S/.)</label></td>
  								 	
					          </tr>
					          <tr>
                  					<td nowrap="nowrap" colspan="2" class="cabecera"></td>
							 		<?php
							 		reset($a_con_ingresos);
							 		foreach ($a_con_ingresos as $key =>$o_con_ingresos){?>
							 			<td nowrap="nowrap" align="center"><label class="label" ><?=$o_con_ingresos->getDescripcionPlanilla()?></label></td>
							 		<?}?>
							 		<?foreach ($a_con_descuentos as $key =>$o_con_descuentos){?>
							 			<td nowrap="nowrap" align="center"><label class="label" ><?=$o_con_descuentos->getDescripcionPlanilla()?></label></td>
							 		<?}?>
  								 	<td nowrap="nowrap" align="center"><label class="label" >Neto</label></td>
  								 	<?foreach ($a_con_aportes as $key =>$o_con_aportes){?>
							 			<td nowrap="nowrap" align="center"><label class="label" ><?=$o_con_aportes->getDescripcionPlanilla()?></label></td>
							 		<?}?>
  								 	<td nowrap="nowrap" align="center"><label class="label" >Boleta</label></td>
					          </tr>
					          <?php
					          reset($a_Colaborador);
					          $ii=1;
					          foreach ($a_Colaborador as $key =>$o_Colaborador){
					          	$a_idcolaboradores[] = $o_Colaborador['IdColaborador'];?>
					           <tr>
  								 	<td nowrap="nowrap"><label class="label" ><?=$ii?></label></td>
  								 	<td nowrap="nowrap"><label class="label" ><?=$o_Colaborador['NombreCompleto']?></label></td>
  								 	<?php
  								 	$montoSubtotal = 0;
  								 	foreach ($a_con_ingresos as $key =>$o_con_ingresos){?>
							 			<td nowrap="nowrap" align="center"><label class="label" ><?php 
							 			$monto = $a_planilla_Col_valor[$o_Colaborador['IdColaborador']][$o_con_ingresos->getCodigo()];
							 			echo number_format($monto,2); 
							 			$montoSubtotal +=$monto;
							 			$a_montoISubtotal[$o_con_ingresos->getCodigo()] +=$monto;
							 			?>
							 			</label>
							 			</td>
							 		<?php }?>
							 		<?php foreach ($a_con_descuentos as $key =>$o_con_descuentos){?>
							 			<td nowrap="nowrap" align="center"><label class="label" ><?php 
							 			$monto = $a_planilla_Col_valor[$o_Colaborador['IdColaborador']][$o_con_descuentos->getCodigo()];
							 			echo number_format($monto,2);
							 			$montoSubtotal -=$monto;
							 			$a_montoSSubtotal[$o_con_descuentos->getCodigo()] +=$monto;
							 			?></label></td>
							 		<?php }?>
							 		<td  align="right"><label class="label Req" ><?php 
							 		echo number_format($montoSubtotal,2);
							 		$montoTotalV += $montoSubtotal;
							 		?></label></td>
							 		<?php foreach ($a_con_aportes as $key =>$o_con_aportes){?>
							 			<td nowrap="nowrap" align="center"><label class="label" ><?php 
							 			$monto = $a_planilla_Col_valor[$o_Colaborador['IdColaborador']][$o_con_aportes->getCodigo()];
							 			echo number_format($monto,2);
							 			//$montoSubtotal -=$monto;
							 			$a_montoASubtotal[$o_con_aportes->getCodigo()] +=$monto;
							 			?></label></td>
							 		<?php }?>
							 		<td align="center"><img src="/images/common/buttons/btnPdf.gif" title="Exportar a PDF" onclick="parent.objMainTab.modalDialog('<?=buildUrl('planilla/pdf_boleta/periodo/'.$periodo.'/idColaborador/'.$o_Colaborador['IdColaborador'],"block=002");?>',{title:'',width:710,height:440})" style="cursor:pointer" /></td>
					          </tr>
					          <?php
					          $ii++;
					          }
					          
					          
							  $cadena_colaboradores = implode("-",$a_idcolaboradores);?>
					          
					           <tr>
                  					<td nowrap="nowrap" colspan="2" class="cabecera" width="30%" align="right">Totales</td>
  								 	<?php
							 		reset($a_con_ingresos);
							 		foreach ($a_con_ingresos as $key =>$o_con_ingresos){?>
							 			<td nowrap="nowrap" align="center"><label class="label" ><?=number_format($a_montoISubtotal[$o_con_ingresos->getCodigo()],2)?></label></td>
							 		<?}?>
							 		<?foreach ($a_con_descuentos as $key =>$o_con_descuentos){?>
							 			<td nowrap="nowrap" align="center"><label class="label" ><?=number_format($a_montoSSubtotal[$o_con_descuentos->getCodigo()],2)?></label></td>
							 		<?}?>
  								 	<td nowrap="nowrap"  align="right"><label class="label req"><?=number_format($montoTotalV,2)?></label></td>
  								 	<?foreach ($a_con_aportes as $key =>$o_con_aportes){?>
							 			<td nowrap="nowrap" align="center"><label class="label" ><?=number_format($a_montoASubtotal[$o_con_aportes->getCodigo()],2)?></label></td>
							 		<?}?>
					          </tr>
						</table>
						<table width="100%" border="0" cellspacing="0" cellpadding="0">
						  <tr>
							 <td><img src="/images/common/spacer.gif" width="10"></td>
						  </tr>
						  <tr>
							<td align="center">
							  <input type="hidden" name="typeSubmit" id="typeSubmit">
							  <input class="button" type="button" value="Generar boletas" onclick="parent.objMainTab.modalDialog('<?=buildUrl('planilla/pdf_boleta/periodo/'.$periodo.'/idColaborador/'.$cadena_colaboradores,'&block=002');?>',{title:'',width:710,height:440})"/>
							 
							  <input class="button" type="button" value="Cancelar" onclick="location.href='<?=buildUrl('planilla/list','&block=002')?>'"/>
							</td>
						  </tr>
						
						</table>
						
    					</fieldset>				
    				</td>
                </tr>
      </table>
      </form>
			<script type="text/javascript">
				var valid = new Validation('form1', {immediate : true});
			</script>
    </td>
  </tr>
</table>
