<?
session_start();
include("../conexion/class-sb-conexion.php");
$empresa_global = $_SESSION['empresa_global'];
$sucursal_global = $_SESSION['sucursal_global'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Libro Remuneracion</title>
<link href="../style/joda-estilo.css" rel="stylesheet" type="text/css" />
</head>
<style>
.linea{
font-size:12px;

}
</style>
<body>
<?
		$db = new sb_mysql();
		$db->proceso($empresa_global);
		$sql_empresa="
  select 
   	rut_empresa,
   	nombre,
	rut_representante,
	nombre_representante,
	ciudad,
	direccion
  from empresa
  where id='".$empresa_global."'
	  ";
		$consulta_empresa = $db->consulta_free($sql_empresa);
		$res_empresa = $db->sb_array($consulta_empresa);

		?>
<table width="100%" border="0" cellpadding="0" cellspacing="0"> 
		<tr class="titulos2">
	<td colspan="8">
		<span style="font-weight:bold">
		<?=$res_empresa['nombre']?>
        </span>
    </td>
    <td align="right">Fecha :<?=date("d/m/Y")?></td>
</tr>
<tr class="titulos2">
	<td colspan="8">
		<span style="font-weight:bold">
		<?=$res_empresa['direccion']?>
        </span>
    </td>

</tr>
<tr class="titulos2">
    <td >
    	R.U.T.&nbsp;:&nbsp;
        <span style="font-weight:bold">
		<?=$res_empresa['rut_empresa']?>
        </span>
    </td>

</tr>
</table>
<?




	  $sql="select 
	  		Rut_Func,
			concat(Apellido_Paterno_Func,' ',Apellido_Materno_Func,' ',Nombre_Func) as Nombre,
			Total_Liquido_Liq,
			Total_Imponible_Liq,
			Total_Haberes_Liq,
			Total_Descuentos_Liq,
			Ajuste_Liq,
			Total_Beneficios_Liq,
			Codigo_Liq,
			Codigo_Liq,
			Sueldo_Base_Liq
	  	FROM sb_contratos_lista
	  	INNER JOIN  sb_funcionarios_lista 
		  		on sb_contratos_lista.Codigo_Func_cont = sb_funcionarios_lista.Codigo_Func
		INNER JOIN  sb_datos_liquidacion 
		  		on sb_contratos_lista.Codigo_Cont = sb_datos_liquidacion.Codigo_Cont_Liq		
	   where sb_contratos_lista.Codigo_Sucu_Cont='".$sucursal_global."' and
	   		 sb_contratos_lista.Codigo_Empresa_Cont='".$empresa_global."' AND
		 sb_datos_liquidacion.Activo_Liq=1	 and Codigo_Proceso='".$db->id_proceso."'
	   order  by Apellido_Paterno_Func,Apellido_Materno_Func,Nombre_Func asc	 ";   
	
    
	$consulta = $db->consulta_free($sql);
	$num_reg=mysqli_num_rows($consulta);
	$registros=0;
		
		$sql_sucursal="
  select 
   	Codigo_Sucursal,
   	Nombre_Sucursal
  from sb_sucursal_lista
  where Codigo_Empresa_Sucursal='".$empresa_global."' AND Codigo_Sucursal='".$sucursal_global."'	  ";
		$consulta_sucursal = $db->consulta_free($sql_sucursal);
		$res_sucursal = $db->sb_array($consulta_sucursal);

	
	?>
    <table border="0" width="101%" style="border:1px solid #CCCCCC"  cellpadding="0" cellspacing="0">
<caption>Libro de Remuneracion  <br />
        Periodo : <?=$db->mes_actual." - ".$db->ano_actual?>
        Sucursal: <?=$res_sucursal['Nombre_Sucursal']?>
        </caption>
		<tr valign="top" class="linea" align="center">
        	<td width="2%" style="border:1px solid #CCCCCC">N&deg;</td>
          <td width="9%" style="border:1px solid #CCCCCC">Nombre</td>
          <td width="6%" style="border:1px solid #CCCCCC">Rut</td>       
          <td width="3%" style="border:1px solid #CCCCCC">Dias</td>
          <td width="4%" style="border:1px solid #CCCCCC">Sueldo<br/>
          Mes</td>
          <td width="3%" style="border:1px solid #CCCCCC">Extra</td>
          <td width="4%" style="border:1px solid #CCCCCC">Gratif.</td>
          <td width="4%" style="border:1px solid #CCCCCC">Otros Impo</td>
          <td width="3%" style="border:1px solid #CCCCCC">Total<br/>
          Imp</td>
          <td width="4%" style="border:1px solid #CCCCCC">N&deg;<br/>
          Cargas</td>
          <td width="3%" style="border:1px solid #CCCCCC">ASig.<br/>
          Fami.</td>
          <td width="5%" style="border:1px solid #CCCCCC">Otros.<br/>
          Haberes.</td>
         
          <td width="4%" style="border:1px solid #CCCCCC">Total<br />
          Haberes</td>
      <td width="3%" style="border:1px solid #CCCCCC">Desc.<br />
          Afp</td>
         
          
          <td width="3%" style="border:1px solid #CCCCCC">Desc<br />
          Salud</td>
          <td width="3%" style="border:1px solid #CCCCCC">Imp.<br />
          Unico</td>
           <td width="3%" style="border:1px solid #CCCCCC">Seg.<br />
           Cesan</td>
          <td width="7%" style="border:1px solid #CCCCCC">Otros<br />
          Descuentos</td>
          <td width="7%" style="border:1px solid #CCCCCC">Total<br />
          Desc.</td>
          <td width="6%" style="border:1px solid #CCCCCC">Anticipo</td>
          <td width="5%" style="border:1px solid #CCCCCC">Ajuste</td>
          <td width="9%" style="border:1px solid #CCCCCC">Total<br />
          Liquido</td> 
      </tr>
	<?
	while($Reg = $db->sb_array($consulta))
	{   
	$registros++;
	?>
	<tr class="linea" style="border:1px solid #CCCCCC"><td><?=$registros?></td><td><?=$Reg['Nombre']?></td>
	<?
	$sql="select 
		  	Codigo_HabDes_Plantilla,
		  	Nombre_HabDes,
		  	Valor_Plantilla
		  FROM sb_plantilla_haberes_descuentos
		  INNER JOIN  sb_haberes_descuentos_lista 
		  	on sb_plantilla_haberes_descuentos.Codigo_HabDes_Plantilla = sb_haberes_descuentos_lista.Codigo_HabDes
		  where Codigo_Liq_Plantilla='".$Reg['Codigo_Liq']."'
		  order  by Tipo_HabDes asc,Imponible_HabDes asc,Codigo_HabDes asc  ";   

	$consulta2 = $db->consulta_free($sql);
	while($resultados = $db->sb_array($consulta2))
	{   
	
		 $indice=$resultados['Codigo_HabDes_Plantilla'];
		 $campos[$indice]=$resultados['Valor_Plantilla'];
	
	}

	$c2=0;
	$c3=0;
	$c5=0;
	$c6=0;	
	$c9=0;
	$c10=0;
	$c18=0;
	$c19=0;
	$c24=0;	
	$c29=0;
	$c30=0;
	$c31=0;
	$c32=0;
	$TRI=0;
	$THA=0;
	$TDE=0;
	$Aju=0;
	$TLI=0;
	
	?><td><?=$Reg['Rut_Func']?></td><?
	?><td align="right"><?=number_format($campos['1'],0,0,'.')?></td><?
	?><td align="right"><?=number_format($campos['2'],0,0,'.')?></td><? $c2+=$campos['2'];
	?><td align="right"><?=number_format($campos['5'],0,0,'.')?></td><? $c5+=$campos['5'];
	?><td align="right"><?=number_format($campos['3'],0,0,'.')?></td><? $c3+=$campos['3'];
	
	?><td align="right"><?=number_format(($campos['6']+$campos['7']),0,0,'.')?></td><? $c6+=($campos['6']+$campos['7']);

	
	?><td align="right"><?=number_format($Reg['Total_Imponible_Liq'],0,0,'.')?></td><?  $TRI+=$Reg['Total_Imponible_Liq'];
	?><td align="right"><?=number_format($campos['8'],0,0,'.')?></td><?
	?><td align="right"><?=number_format($campos['9'],0,0,'.')?></td><?  $c9+=$campos['9'];

	?><td align="right"><?=number_format(($campos['10']+$campos['11']+$campos['12']+$campos['13']+$campos['14']+$campos['15']+$campos['16']),0,0,'.')?></td><? $c10+=($campos['10']+$campos['11']+$campos['12']+$campos['13']+$campos['14']+$campos['15']+$campos['16']);

	
	?><td align="right"><?=number_format($Reg['Total_Haberes_Liq'],0,0,'.')?></td><?  $THA+= $Reg['Total_Haberes_Liq'];
	
?><td align="right"><?=number_format($campos['29'],0,0,'.')?></td><? $c29+=$campos['29'];

	

		?><td align="right"><?=number_format(($campos['30']+$campos['31']),0,0,'.')?></td><? $c30+=$campos['30'];
		 $c31+=$campos['31'];
		
		?><td align="right"><?=number_format($campos['32'],0,0,'.')?></td><? $c32+=$campos['32'];
		?><td align="right"><?=number_format($campos['33'],0,0,'.')?></td><? $c19+=$campos['33'];
		?><td align="right"><?=number_format(($campos['19']+$campos['21']+$campos['22']+$campos['23']+$campos['25']+$campos['26']+$campos['20']+$campos['34']+$campos['27']+$campos['28']),0,0,'.')?></td><? $c24+=($campos['19']+$campos['21']+$campos['22']+$campos['23']+$campos['25']+$campos['26']+$campos['20']+$campos['34']+$campos['27']+$campos['28']);

		?><td align="right"><?=number_format(($Reg['Total_Descuentos_Liq']-$campos['18']),0,0,'.')?></td><? $TDE+= ($Reg['Total_Descuentos_Liq']-$campos['18']);
		?><td align="right"><?=number_format($campos['18'],0,0,'.')?></td><? $c18+=$campos['18'];
		?><td align="right"><?=number_format($Reg['Ajuste_Liq'],0,0,'.')?></td><?   $Aju+= $Reg['Ajuste_Liq'];
		?><td align="right"><?=number_format($Reg['Total_Liquido_Liq'],0,0,'.')?></td> <? $TLI+= $Reg['Total_Liquido_Liq']; 
	?></tr>
    
    <?
	
	}

   ?>
   <tr>
   <td height="19"></td>
   <td>Totales</td>
   <td></td>
   <?
   
	?><td align="right"></td><?
	?><td align="right"><?=number_format($c2,0,0,'.')?></td><? 
	?><td align="right"><?=number_format($c5,0,0,'.')?></td><?
	?><td align="right"><?=number_format($c3,0,0,'.')?></td><?
	?><td align="right"><?=number_format($c6,0,0,'.')?></td><?
	?><td align="right"><?=number_format($TRI,0,0,'.')?></td><?
	?><td align="right">&nbsp;</td><?
	?><td align="right"><?=number_format($c9,0,0,'.')?></td><?
	?><td align="right"><?=number_format($c10,0,0,'.')?></td><?
	
	?><td align="right"><?=number_format($THA,0,0,'.')?></td><?
	?><td align="right"><?=number_format($c29,0,0,'.')?></td><?

	
		
		?><td align="right"><?=number_format(($c30+$c31),0,0,'.')?></td><?
		
		?><td align="right"><?=number_format($c32,0,0,'.')?></td><?
		?><td align="right"><?=number_format($c19,0,0,'.')?></td><?
			?><td align="right"><?=number_format($c24,0,0,'.')?></td><?
				
		?><td align="right"><?=number_format($TDE,0,0,'.')?></td><?
		?><td align="right"><?=number_format($c18,0,0,'.')?></td><?
		?><td align="right"><?=number_format($Aju,0,0,'.')?></td><?
		?><td align="right"><?=number_format($TLI,0,0,'.')?></td>
	
 </tr>

   
   
    </table>
	


</body>
</html>
