<?php
//ini_set('display_errors',1);
session_start();
require_once ("../../config/Cado.php");
require_once ("../canjeproveedor/cCanjeproveedor.php");
$oCanjeproveedor = new cCanjeproveedor();
require_once ("../egreso/cEgreso.php");
$oEgreso = new cEgreso();
require_once ("../compra/cComprapago.php");
$oComprapago = new cComprapago();
require_once ("../formatos/formato.php");
if($_POST['monid']==1){
	$lblmon = 'S/';
}else if($_POST['monid']==2){
	$lblmon = 'US$';
}
?>
<div class="table-responsive">
<strong class="text-sm text-info" >DOCUMENTOS CANJEADOS</strong>
<table class="table table-bordered table-striped" >
				<thead>
				<tr >
				<th align="left">T.D.</th>	
				<th nowrap="" align="left">FECHA</th>	
				<th nowrap align="left">CORR.</th>	
				<th nowrap align="left">FECHA VEN.</th>	
				<th nowrap align="left">IMPORTE</th>
				<th nowrap align="left">SALDO</th>
				</tr>
				</thead>
				<tbody>
			<?php 
		       // echo $_POST['canid'];
	        $dts1 = $oCanjeproveedor->mostrar_filtro_documento($_POST['canid']);
			
			while($dt = mysqli_fetch_array($dts1))
			{
			$fec	= mostrarFecha($dt['tb_compra_fec']);
			$fecven	= mostrarFecha($dt['tb_comprapago_fecven']);
			$numdoc	= $dt['tb_compra_numdoc'];
			$docnom = $dt['tb_documento_nom'];
			$tot = $dt['tb_comprapago_mon'];
			
			$total_abonos = 0;
			$saldo = 0;
			$total_monto = 0;
	   		/* $dts2=$oComprapago->filtrar($dt['tb_compra_id']);
       
       		while($dt2 = mysqli_fetch_array($dts2))
       		{ */
        	$monto = $dt['tb_comprapago_mon']; 
        	$abonos=0;
        	$mod_id=4;
        	$est=1;
        	$dts3 = $oEgreso->mostrar_por_modulo($mod_id,$dt['tb_comprapago_id'],$est);
        	while($dt3 = mysqli_fetch_array($dts3))
        	{
          	$abonos+= $dt3['tb_egreso_imp'];
       		}	 
        	mysqli_free_result($dts3);
  
        	$total_abonos += $abonos; 
        	$total_monto += $monto; 
		
			//}
			$saldo = $total_monto - $total_abonos;
			?>
			
			<tr>
				<td nowrap><?php echo $docnom; ?></td>	
				<td nowrap ><?php echo $fec; ?></td>	
				<td nowrap><?php echo $numdoc; ?></td>	
				<td nowrap><?php echo $fecven; ?></td>	
				<td nowrap ><?php echo $lblmon.' '.formato_money($tot); ?></td>	
				<td nowrap><?php echo $lblmon.' '.formato_money($saldo); ?></td>	
			</tr>
			<?php 
			$total_saldo += $saldo; 
			$lblnumdoc .= $numdoc.'/';
			
		}
		
			?>
			<tr>
				<td>
					
				</td>
				<td>
					
				</td>
				<td colspan="3"> <strong class="text-info text-md"> IMPORTE CANJEADO : </strong></td>
				
				<td><strong class="text-info text-md"><?php echo $lblmon.' '. formato_money($total_saldo); ?></strong></td>
			</tr>
			</tbody>
			</table>
			</div>
 			
<script type="text/javascript">
	
$("#hdd_can_tot").val(<?php echo $total_saldo;?>);
$("#txt_canje_ref").val("<?php echo $lblnumdoc ;?>");
</script>
 			<div class="col-lg-12 col-sm-12 col-xs-12" style="height: 10px;"></div>