<?php
//ini_set('display_errors',1);

session_start();
unset($_SESSION['can_numdoc']);
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");
 
$contadorpro = 0;
$total_soles = 0;
$total_dolares = 0;
$dts1 = $oCanjeproveedor->mostrar_documentos($_POST['hdd_fil_pro_id'],$_POST['cmb_fil_empresa_id']);
$num_rows= mysqli_num_rows($dts1);
 
if($_POST['cmb_fil_mon_id']==1){
	$lbl_filmon = 'S/';
}else if($_POST['cmb_fil_mon_id']==2){
	$lbl_filmon = 'US$';
}
$dts = $oCanjeproveedor->mostrar_filtro(fecha_mysql($_POST['txt_fil_canjpro_fec1']),fecha_mysql($_POST['txt_fil_canjpro_fec2']),$_POST['hdd_fil_pro_id'],
$_POST['cmb_fil_empresa_id'],$_POST['cmb_fil_mon_id'],$_POST['txt_can_numdoc']);
$num_row= mysqli_num_rows($dts);
?>

<section class="content">
	<div class="container-fluid">
	<div class="row">
	    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
		    <div class="card">
				<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="padding:5px;">
	
					<div class="table-responsive">
		
		<table id="tabla_canje" class="table table-bordered table-striped">
           <thead>
			<tr >
				<th align="left">FECHA DE CANJE</th>
				<th align="left">NUMERO CANJE</th>	
				<th align="left">EMPRESA</th>	
				<th align="left">PROVEEDOR</th>	
				<th align="left">AVAL</th>		
				<th align="right">MONTO CANJEADO</th>
				<th></th>
			</tr>
		</thead>
         <tbody>
         <?php 
         if($num_row>0){
		while($dt = mysqli_fetch_array($dts))
		{ 
		?>
		<tr <?php if($dt['tb_canjeproveedor_est']=='2'){ echo 'class="even bg-yellow"'; }?> title="CANJE ANULADO" >
				<td  ><?php echo mostrarFecha($dt['tb_canjeproveedor_fec']); ?></td>	
				<td nowrap class="text-orange"><strong> <?php echo $dt['tb_canjeproveedor_numdoc']; ?></strong></td>					
				<td><?php echo $dt['tb_empresa_nomcom']; ?></td>			
				<td align="left" ><?php echo $dt['tb_proveedor_nom']; ?></td>
				<td align="left" ><?php echo $dt['tb_canjeproveedor_aval']; ?></td>
				<td align="left" ><?php echo $lbl_filmon.' '.formato_money($dt['tb_canjeproveedor_mon']); ?></td>
				<td>
					<span style="cursor:pointer" onClick="canje_form_vista('insertar',<?php echo $dt['tb_canjeproveedor_id']?>)" class="btn btn-primary btn-sm"  > <i class="fas fa-edit"></i></span>
					<?php 
					if($_SESSION['usuariogrupo_id']=='1' ){
					if($dt['tb_canjeproveedor_est']=='1'){
						?>
						<span style="cursor:pointer" onClick="canje_eliminar('anular',<?php echo $dt['tb_canjeproveedor_id']?>)" class="btn btn-warning btn-sm"  > <i class="fas fa-trash"></i></span>
						<?php 
					}
					}
					?>
								
				</td>
		</tr>
		<?php 
		}
		}else{
		?>
		<tr class="even">
				<td colspan="6" >CANJES NO ENCONTRADOS</td>		
		</tr>
		<?php
		}
         ?>
         </tbody>
     	</table>
		

		
          <table id="tabla_canje" class="table table-bordered table-striped">
           <thead>
			<tr >
				<th align="left">DESCRIPCIONES</th>	
				<th align="right">MONTOS TOTALES</th>
				<th align="right">SALDO SIN PAGAR</th>
			</tr>
		</thead>
         <tbody>	
		<?php
		if($num_rows>0){
		while($dt1 = mysqli_fetch_array($dts1))
		{ 
		$total_abonos = 0;
		$saldo = 0;
		$total_monto = 0;
	   $dts2=$oComprapago->filtrar($dt1['tb_compra_id']);
       while($dt2 = mysqli_fetch_array($dts2))
       {
        $monto = $dt2['tb_comprapago_mon']; 
        $abonos=0;
        $mod_id=4;
        $est=1;
        $dts3 = $oEgreso->mostrar_por_modulo($mod_id,$dt2['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;
		
		$total_soles += $dt1['tb_comprapago_mon'];
			$total_saldo += $saldo;
			$contadorpro ++; 
		} 
		mysqli_free_result($dts1); 
		}
			
		?>
		
			<tr class="even">	
				<td>TOTAL</td>			
				<td align="left" ><?php echo $lbl_filmon.' '.formato_money($total_soles); ?></td>
				<td align="left" ><?php echo $lbl_filmon.' <strong>'.formato_money($total_saldo).'</strong> '?></td>
			</tr>
	
		</tbody>
			
      </table>
     
   
				</div>
			</div>
		</div>
	</div>
	</div>
	</div>
</section>
<script type="text/javascript" src="../cotizacion/NumberFormat.js"></script>
  <script type="text/javascript">
	$(function(){
	
	});
/*var tot = 0;
function vertot(mon,id,numdoc){
	if( $("#chk_imprimir_"+id).prop('checked') ) {
	tot += mon*1;
	}else{
	tot -= mon*1;
	}
$("#hdd_totcanje").val(new NumberFormat(tot).toFormatted());
$("#lbl_montot").text(new NumberFormat(tot).toFormatted());
if(tot>0){
	$("#btn_canje").attr('disabled', false);
	$("#btn_canje").attr('readonly', false);
	$("#btn_canje").attr('onclick', 'canje_form()');
}else{
	$("#btn_canje").attr('disabled', true);
	$("#btn_canje").attr('readonly', true);
	$("#btn_canje").attr('onclick', '');
}
if($("#chk_imprimir_"+id).is(':checked')){
	$.ajax({
		type: "POST",
		url: "../canjeproveedor/canjeproveedor_reg.php",
		async:false,
		dataType: "html",                  
		data: ({
			action:	 'agregarnumdoc',
			can_numdoc: numdoc
		}),
		beforeSend: function() {
			
		},
		success: function(html){
			
		},
		complete: function(){			
			
		}
	});
}else{
	  $.ajax({
		type: "POST",
		url: "../canjeproveedor/canjeproveedor_reg.php",
		async:false,
		dataType: "html",                  
		data: ({
			action:	 'quitarnumdoc',
			can_numdoc: numdoc
		}),
		beforeSend: function() {
			
		},
		success: function(html){
			
		},
		complete: function(){			
			
		}
	});
}
	
	
}
	*/
	function canjeardocumentos(){
	$.ajax({
		type: "POST",
		url: "../canjeproveedor/canjeproveedor_reg.php",
		async:true,
		dataType: "html", 
		data: $("#for_canje").serialize(),                    
		beforeSend: function() {
			
		},
		success: function(html){
			
		},
		complete: function(){			
			
		}
	});     
		
	}
	  
	  </script>