<?php use_helper('Number','Date')?>
<?echo "hola". count($destinos)?>
<?php $destinoss    = ''?>
<?php foreach($destinos as $destino): ?>
  <?php $destinoss .= trim($destino->getMunicipios()->getMunicipio() .'<b> - '. DepartamentoPeer::getDescDepto($destino->getMunicipios()->getMunicipio()."-".$destino->getMunicipios()->getDepto()) .'</b>')?>
  <?php $destinoss .= $destino->getObservacion() ? ' ('.$destino->getObservacion().'), ' : ', '?>        
<?php endforeach?>
<?php 
	$gran_total = 0;
	$gran_total_dias = 0;
	$gran_total_manu = 0;
	$sumGastosPlanta = array();
	$sumGastosContrato = array();
?>       
<table class="show" style="width:100%;border:1">
<tr>

    
  <td rowspan="2" width="210"><?php echo image_tag('header')?></td>
  <td><strong>&Aacute;rea:</strong> <?php echo $comision->getAreas()->getDescripcion()?></td>
  <td><strong>Fecha:</strong> <?php echo ucfirst(format_date($comision->getFecha(),'MMMM yyyy','es'))?></td>

<td><strong>Tipo comisi&oacute;n:</strong> <?php if($comision->getTiposComision()->getDescomi()=='Ordinaria'||$comision->getTiposComision()->getDescomi()=='Extraordinaria'||$comision->getTiposComision()->getDescomi()=='Emergencia'):echo $comision->getTiposComision()->getDescomi(); else: echo $comision->getTiposComision()->getDescomi().' al plan de comision: '.$comision->getIdOriginal(); endif; ?> <?php if($comision->getInterrupcion()==1): echo 'CON INTERRUPCION'; else: ''; endif;?></td>

  <td><strong>Consecutivo:</strong> <?php echo $comision->getId()?></td>
</tr>
<tr>
  <td colspan="4"><strong>Proyecto:</strong> <?php echo $comision->getCodpro()?> - <?php echo $comision->getProyectos()->getDescripcion()?></td>
</tr>
</table>

<div id="toReport" style="display: block">
<!-- OBJETIVOS ETC -->
<hr size="5" color="black" />
<table width="100%">
  <tr>
    <td><b>Objetivos</b></td>
    <td><b>Actividades</b></td>
    <td><b>Destino(s)</b></td>
  </tr>  
  <tr>
    <td><?php echo $comision->getObjetivo()?></td>
    <td><?php echo $comision->getActividad()?></td>
    <td><?php echo $destinoss?></td>
  </tr>
</table>
<hr size="2" color="black" />
<br/>
<!-- END OBJETIVOS ETC -->

<!-- COMISIONADOS DE PLANTA -->


<?php if(count($plantas) > 0): ?>
<h2 align="center" style="color: black;" >Comisionados de Planta</h2>
<table width="100%" border="1" cellpadding="2">
  <tr style="font-weight: bold;" align="center" bgcolor="#B0C5EF">
    <th colspan="2" width="25%">Identificación</th><!-- 570 wide -->
    <th width="7%">Sue</th>
    <th width="6%">Cargo</th>
    <th width="4%">Sede</th>
    <th width="9%">Fecha salida</th>
    <th width="5%">D&iacute;as vi&aacute;ticos</th>
    <th width="7%">D&iacute;as manutenci&oacute;n</th>
    <th width="5%">Total d&iacute;as</th>
    <th width="8%">Transportes</th>
    <th width="15%">Objetos de Gasto</th>
    <th width="9%">Total comisi&oacute;n</th>
  </tr>
  <?php $suma_dias     = 0?>
  <?php $suma_manu = 0;?>
  <?php $suma_total    = 0?>  
  <?php foreach($plantas as $planta): ?>
  	<?php $suma_gastos = 0?>
    <?php $gastos      = ComisionadosObjsPeer::getAllObjetosGasto($planta->getNitocc(), $planta->getIdComision(), $comision->getVigencia())?>
    <tr align="center">
      <td width="8%"><?php echo $planta->getNitocc()?></td>
      <td width="17%"><?php echo $planta->getTerceros()->getRazsoc()?></td>
      <td width="7%" align="right"><?php echo format_number($planta->getSueldo())?></td>
      <td width="6%"><?php echo $planta->getTerceros()->getCargo()?></td>
      <td width="4%"><?php echo $planta->getCodreg()?></td>
      <td width="9%"><?php echo ucwords(format_date($planta->getFechaSalida(),'dd MMM yy','es'))?></td>
      <td width="5%"><?php echo $planta->getDias()?></td>
      <td width="7%"><?php echo $planta->getDiasManu()?></td>
      <td width="5%"><?php echo $planta->getDiasManu() + $planta->getDias()?></td>
      <td width="8%"><?php echo $planta->getTransportes()->getDescripcion()?></td>
      <td width="15%" align="right">
      	<table width="100%">
      	<?php foreach($gastos as $gasto): ?>
      		<?php 
      			if(key_exists($gasto->getObjetosGasto()->getDescripcion(), $sumGastosPlanta)):
      				$sumGastosPlanta[$gasto->getObjetosGasto()->getDescripcion()]['val'] +=  round($gasto->getValor());
      			else:
      				$sumGastosPlanta[$gasto->getObjetosGasto()->getDescripcion()]['desc'] = $gasto->getObjetosGasto()->getDescripcion();
      				$sumGastosPlanta[$gasto->getObjetosGasto()->getDescripcion()]['val'] =  round($gasto->getValor());
      			endif;
      		?>
      		<tr>
      			<td align="left" width="70%"><b><?php echo $gasto->getObjetosGasto()->getDescripcion()?></b></td>
      			<td align="right" width="30%"><?php echo format_number( round($gasto->getValor()))?></td>
      		</tr>
      		<?php $suma_gastos +=  round($gasto->getValor())?>
      	<?php endforeach;?>
      	</table>
      </td>
      <td width="9%" align="right" valign="bottom"><?php echo format_number($suma_gastos)?></td>
    </tr>
    <?php
    	$suma_dias			+= $planta->getDias();
    	$suma_manu			+= $planta->getDiasManu();
    	$suma_total			+= $suma_gastos;
    	$gran_total			+= $suma_gastos;
    	$gran_total_dias	+= $planta->getDias();
    	$gran_total_manu	+= $planta->getDiasManu();
    ?>
  <?php endforeach?>
  <tr>
    <td width="51%" colspan="6"><b>Suma total <?php echo count($plantas)?> comisionados de planta</b></td>
    <td width="5%" align="center"><b><?php echo number_format($suma_dias, 1)?></b></td>
    <td width="7%" align="center"><b><?php echo number_format($suma_manu, 1)?></b></td>
    <td width="5%" align="center"><b><?php echo number_format($suma_manu + $suma_dias, 1)?></b></td>
    <td width="8%"></td>
    <td width="15%" align="right"><b><?php echo format_number($suma_total)?></b></td>
    <td width="9%" align="right"><b><?php echo format_number($suma_total)?></b></td>
  </tr>
</table>
<br/>
<hr size="2" color="black" />
<?php endif ?>



<br />
<!-- END COMISIONADOS DE PLANTA -->


<!-- COMISIONADOS DE CONTRATO -->


<?php if(count($contratos) > 0): ?>
<h2 align="center" style="color: black;">Comisionados de Contrato</h2>
<table width="100%" border="1" cellpadding="2">
  <tr style="font-weight: bold;" align="center" bgcolor="#B0C5EF">
    <th colspan="2" width="25%">Identificación</th><!-- 570 wide -->
    <th width="7%">Sueldo</th>
    <th width="6%">Cargo</th>
    <th width="4%">Sede</th>
    <th width="9%">Fecha salida</th>
    <th width="5%">D&iacute;as vi&aacute;ticos</th>
    <th width="7%">D&iacute;as manutenci&oacute;n</th>
    <th width="5%">Total d&iacute;as</th>
    <th width="8%">Transportes</th>
    <th width="15%">Objetos de Gasto</th>
    <th width="9%">Total comisi&oacute;n</th>
  </tr>
  <?php $suma_dias     = 0?>
  <?php $suma_manu     = 0?>
  <?php $suma_total    = 0?>  
  <?php foreach($contratos as $planta): ?>
  	<?php $suma_gastos = 0?>
    <?php $gastos      = ComisionadosObjsPeer::getAllObjetosGasto($planta->getNitocc(), $planta->getIdComision(), $comision->getVigencia())?>
    <tr align="center">
      <td width="8%"><?php echo $planta->getNitocc()?></td>
      <td width="17%"><?php echo $planta->getTerceros()->getRazsoc()?></td>
      <td width="7%" align="right"><?php echo format_number($planta->getTerceros()->getSueldo())?></td>
      <td width="6%"><?php echo $planta->getTerceros()->getCargo()?></td>
      <td width="4%"><?php echo $planta->getCodreg()?></td>
      <td width="9%"><?php echo ucwords(format_date($planta->getFechaSalida(),'dd MMM yy','es'))?></td>
      <td width="5%"><?php echo $planta->getDias()?></td>
      <td width="7%"><?php echo $planta->getDiasManu()?></td>
      <td width="5%"><?php echo $planta->getDiasManu() + $planta->getDias()?></td>
      <td width="8%"><?php echo $planta->getTransportes()->getDescripcion()?></td>
      <td width="15%" align="right">
      	<table width="100%">
      	<?php foreach($gastos as $gasto): ?>
      		<?php 
      			if(key_exists($gasto->getObjetosGasto()->getDescripcion(), $sumGastosContrato)):
      				$sumGastosContrato[$gasto->getObjetosGasto()->getDescripcion()]['val'] += round($gasto->getValor());
      			else:
      				$sumGastosContrato[$gasto->getObjetosGasto()->getDescripcion()]['desc'] = $gasto->getObjetosGasto()->getDescripcion();
      				$sumGastosContrato[$gasto->getObjetosGasto()->getDescripcion()]['val'] = round($gasto->getValor());
      			endif;
      		?>
      		<tr>
      			<td align="left" width="70%"><b><?php echo $gasto->getObjetosGasto()->getDescripcion()?></b></td>
      			<td align="right" width="30%"><?php echo format_number( round($gasto->getValor()))?></td>
      		</tr>
      		<?php $suma_gastos +=  round($gasto->getValor())?>
      	<?php endforeach;?>
      	</table>
      </td>
      <td width="9%" align="right" valign="bottom"><?php echo format_number($suma_gastos)?></td>
    </tr>
    <?php
    	$suma_dias			+= $planta->getDias();
    	$suma_manu			+= $planta->getDiasManu();
    	$suma_total			+= $suma_gastos;
    	$gran_total			+= $suma_gastos;
    	$gran_total_dias	+= $planta->getDias();
    	$gran_total_manu	+= $planta->getDiasManu();
    ?>
  <?php endforeach?>
  <tr>
    <td width="51%" colspan="6"><b>Suma total <?php echo count($contratos)?> comisionados de contrato</b></td>
    <td width="5%" align="center"><b><?php echo number_format($suma_dias, 1)?></b></td>
    <td width="7%" align="center"><b><?php echo number_format($suma_manu, 1)?></b></td>
    <td width="5%" align="center"><b><?php echo number_format($suma_manu + $suma_dias, 1)?></b></td>
    <td width="8%"></td>
    <td width="15%" align="right"><b><?php echo format_number($suma_total)?></b></td>
    <td width="9%" align="right"><b><?php echo format_number($suma_total)?></b></td>
  </tr>
</table>
<br/>
<hr size="2" color="black" />
<?php endif?>



<!-- END COMISIONADOS DE CONTRATO -->
<br/><br/>

<!-- TOTALES COMISION -->
<table width="100%" border="1">
  <tr>
    <td width="51%"><b>TOTAL <?php echo count($plantas) + count($contratos)?> COMISIONADOS</b></td>
    <td width="5%" align="center"><b><?php echo format_number($gran_total_dias, 1)?></b></td>
    <td width="7%" align="center"><b><?php echo format_number($gran_total_manu, 1)?></b></td>
    <td width="5%" align="center"><b><?php echo format_number($gran_total_manu + $gran_total_dias, 1)?></b></td>
    <td width="23%">&nbsp;</td>
    <td align="right" width="9%"><b><?php echo format_number($gran_total)?></b></td>
  </tr>
</table>
<!-- END TOTALES COMISION -->
<br/><br/>

<!-- Observaciones -->
<div align="left">
	<b>Observaciones</b>
	<br/>
	<?php echo $comision->getObserva()?>
</div>
<br/>
<br/>

<!-- RESUMEN DE GASTOS -->
<br/>
<h2 align="center" style="color: black;">Resumen Objetos de Gasto</h2>
<table border="1" width="50%">
	<tr align="center" bgcolor="#B0C5EF">
		<th>Objeto de Gasto</th>
		<th>Com. de Planta</th>
		<th>Com. de Contrato</th>
		<th>Total</th>
	</tr>
	<?php 
		$sumPlanta = 0;
		$sumContrato = 0;
	?>
	<?php foreach($sumGastosPlanta as $item): ?>
	<tr align="right">
		<td align="left"><?php echo $item['desc']?></td>
		<td><?php echo number_format($item['val'])?></td>
		<td><?php echo number_format($valContrato = key_exists($item['desc'], $sumGastosContrato) ? $sumGastosContrato[$item['desc']]['val'] : 0) ?></td>
		<td><?php echo number_format($item['val'] + $valContrato)?></td>
	</tr>
	<?php 
		$sumPlanta += $item['val'];
		$sumContrato += $valContrato;
	?>
	<?php endforeach;?>
	
	<?php //Revisar si hay objetos de gasto en Contrato que no esten en planta?>
	<?php foreach($sumGastosContrato as $itemContrato): ?>
	<?php if(!key_exists($itemContrato['desc'], $sumGastosPlanta)): ?>
		<tr align="right">
			<td align="left"><?php echo $itemContrato['desc']?></td>
			<td>0</td>
			<td><?php echo number_format($itemContrato['val'])?></td>
			<td><?php echo number_format($itemContrato['val'])?></td>
		</tr>
		<?php 
			$sumContrato += $itemContrato['val'];
		?>
	<?php endif;?>	
	<?php endforeach;?>
	<tr align="right">
		<td align="left"><b>Total</b></td>
		<td><b><?php echo number_format($sumPlanta)?></b></td>
		<td><b><?php echo number_format($sumContrato)?></b></td>
		<td><b><?php echo number_format($sumPlanta + $sumContrato)?></b></td>
	</tr>
</table>


	
	
<!-- END RESUMEN DE GASTOS -->

<?php $username= $comision->getUsername();?>
	<?php $userName=UsuariosPeer::retrieveByPk($username)?>
	<br>
	<br>
	<hr>
	<h4> Programador : <?php echo $userName->getUserName()?></h4>

	<h4 align="right">Fecha de Creación	<?php echo ucfirst(format_date($comision->getFecha(),'MMM dd, yyyy','es'))?></h4>
	
	<h4 align="right">Fecha de Impresion 
	<?php $date=date("F j, Y, g:i a");?>
	<?php echo ucfirst($date)?></h4>

</div><!-- END REPORT -->
<br/>
<?php echo javascript_include_tag('report')?>
<?php
	echo form_tag('comisionar/pdf');
	echo input_hidden_tag('htmlContent', '-');
	echo input_hidden_tag('filename', 'planilla_de_comision_detallado');
	echo input_hidden_tag('id_comision', $comision->getId());
	echo input_hidden_tag('vigencia', $comision->getVigencia());
	echo '<table cellspacing="15"><tr><td>'.submit_image_tag('topdf', array('title' => 'Generar PDF', 'onClick' => 'selectElements()')).'</td>';
	echo '<td>'.link_to(image_tag('back', 'title=Volver'),'comisionar/filter_preview?id='.$comision->getId().'&vigencia='.$comision->getVigencia()).'</td></tr></table>'
?>


