<?php
// ini_set('display_errors',1);
session_start();
require_once ("../../config/Cado.php");
$fecha = date('Y-m-d'); 
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("../tipocambio/cTipocambio.php");
$oTipocambio = new ctipocambio();
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-md" >DOCUMENTOS A CAJEAR</strong>
<?php // echo $tipcamsun;?>
<input type="hidden" value="<?php echo $_POST['monid'];?>" />
<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">CAMBIO</th>	
				<th nowrap align="left">FECHA VEN.</th>	
				<th nowrap align="left">IMPORTE</th>	
			<!--	<th nowrap align="left">SALDO</th>
				-->
				</tr>
				</thead>
				<tbody>
			<?php 
			foreach($_SESSION['cancom_id'] as $compag_id){
	       
		  // echo $_POST['provid'].'-'.$_POST['empid'].'-'.$comid.'-'.$_POST['monid'];
			// echo $_POST['monid'];
	        $dts1 = $oCanjeproveedor->mostrar_documento_selec($_POST['provid'],$_POST['empid'],$compag_id);
			$dt = mysqli_fetch_array($dts1);
			$fec	= mostrarFecha($dt['tb_compra_fec']);
			$numdoc	= $dt['tb_compra_numdoc'];
			$docnom = $dt['tb_documento_nom'];
			$tot = $dt['tb_comprapago_mon'];
			$tipcam = $dt['tb_compra_tipcam'];
			$mon = $dt['tb_moneda_id'];
			$tipcam_can = formato_money3($dt['tb_compra_tipcamcan']);
			
			$fecven = mostrarFecha($dt['tb_comprapago_fecven']);
			
			mysqli_free_result($dts1);
			
			
			$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,$compag_id,$est);
        	while($dt3 = mysqli_fetch_array($dts3))
        	{
          	$abonos+= $dt3['tb_egreso_imp'];
       		}	 
        	mysqli_free_result($dts3);
  
        	$total_abonos += $abonos; 
        	$total_monto += $monto; 
		
			//}
			
			// echo '--'.$total_monto;
			
			$saldo = $total_monto - $total_abonos;
			
			if($_POST['monid']==2){
				
			if($mon==1){
					$saldo = $saldo / $tipcam_can;
					$tot = $tot / $tipcam_can;
			}else{
					$saldo = $saldo ;
					$tot = $tot ;
			}
			
				
			}else if($_POST['monid']==1){
				
					
					
			if($mon==1){
					$saldo = $saldo ;
					$tot = $tot ;
			}else{
					$saldo = $saldo *  $tipcam_can;
					$tot = $tot *  $tipcam_can;
			}
					
					
					
			}
			?>
			
			<tr>
				<td nowrap><?php echo $docnom; ?></td>	
				<td nowrap ><?php echo $fec; ?></td>	
				<td nowrap><?php echo $numdoc; ?></td>	
				<td nowrap><?php echo $tipcam_can; ?></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 nowrap >
					<a class="btn btn-danger btn-xs" title="Vaciar formas de pago" href="javascript:void(0)" onClick="agregar_documento('restablecer');"><i class="fa fa-trash"></i> Vaciar</a>
				</td>
				<td nowrap >
					<a class="btn btn-primary btn-xs" title="Agregar documento" href="javascript:void(0)" 
					onClick="ver_documentos('<?php echo $_POST['provid']?>','<?php echo $_POST['empid']?>','<?php echo $_POST['monid']?>');"><i class="fa fa-check"></i> Agregar</a>
				</td>
				<td colspan="3"> <span class="text-white-50 text-md"> IMPORTE A CANJEAR : </span></td>
				
				<td nowrap ><span class="text-white-50 text-md"><?php echo $lblmon.' '. formato_money($total_saldo); ?></span></td>
			</tr>
			</tbody>
			</table>
			</div>
 			</div>
<script type="text/javascript">
	
$("#hdd_can_tot").val(<?php echo $total_saldo;?>);
$("#txt_canje_ref").val("<?php echo $lblnumdoc ;?>");
<?php 
if($total_saldo>0){
?>
	$("#save_form").show(100);
	//$("#cmb_canje_mon_id").attr('disabled',true);
<?php 
}else{
?>
	$("#save_form").hide(100);
<?php 
}
?>
function ver_documentos(provid,empid,monid)
{
	if(monid!=""){
	$('#div_canjedocumentos_form_dialog').modal();
	$('body').css('overflow-y','hidden');
	$.ajax({
		type: "POST",
		url: "../canjeproveedor/canjeproveedor_verdocs.php",
		async:true,
		dataType: "html",  
		//data: //$("#for_fil_pro").serialize(),                    
		data: ({
			provid: provid,
			empid: empid,
			monid: monid
		}),
		beforeSend: function() {
				var div = "<center> <br> <span class='spinner-border spinner-border-lg mr-2'></span> <span style='font-size:1.5rem;'>PROCESANDO PETICIÓN</span></center>";	
				$('.div_canjedocumentos_form').html(div);
		},
		success: function(html){
			$('.div_canjedocumentos_form').html(html);
		},
		complete: function(){
		//	cuentasxpagar_tabla();
		}
	});
	}else{
		toastr["error"]('Seleccione una moneda.');
	}
}
</script>
 			<div class="col-lg-12 col-sm-12 col-xs-12" style="height: 10px;"></div>