<?php
//ini_set("display_errors", 1);
date_default_timezone_set('America/Lima');
session_start();
require_once ("../../config/Cado.php");
require_once ("cCuentapagar.php");
$oCuentapagar = new cCuentapagar();
require_once ("../egreso/cEgreso.php");
$oEgreso = new cEgreso();
require_once ("../formatos/formato.php");
require_once ("../formatos/fechas.php");
$contadorpro2 = 0;
$total_soles = 0;
$total_dolares = 0;

	$dts2 = $oCuentapagar->cuentas_por_pagar_por_proveedor($_POST['pro_id'],$_POST['emp_id'],$_POST['doc_num'],$_POST['mon_id'],
	$_POST['doc_id'],$_POST['doc_est'],fecha_mysql($_POST['fec1']),fecha_mysql($_POST['fec2']));
	$num_rows2= mysqli_num_rows($dts2);

 // eCHO $_POST['doc_id'];

?>		
<div class="table-responsive">
			<table id="tabla_resumen_proveedor" class="table table-bordered table-striped">
				<thead>
					<tr>
					<th width="10%" class="header">OPCIONES</th>
						<th nowrap align="left">DOCUMENTO</th>	
						<th nowrap align="left">NRO DOC.</th>		
						<th align="center">FECHA EMISION</th>
						<th align="center">FECHA VENCIMIENTO</th>
						<th nowrap align="left">UBICACION</th>		
						<th nowrap align="left">NRO UNICO</th>		
						<th align="left">EMPRESA</th>
						<th align="left">ESTADO</th>
						<th align="right" nowrap >A RENOVAR</th>
						<th align="right">IMPORTE</th>
						<th align="right">ABONOS</th>
						<th align="right">SALDO</th>
						
					</tr>
				</thead>
				<tbody>	
					<?php	
					if($num_rows2>0){
					while($dt2 = mysqli_fetch_array($dts2))
					{					
							$monto = $dt2['tb_comprapago_mon'];					
						$abonos = 0;
						$mone_id = $dt2['tb_moneda_id'];
						$mod_id=4;
						$est=1;
						if($dt2['tb_comprapago_est']!=3){
						//echo $dt2['tb_comprapago_id']; 
						$dts3 = $oEgreso->mostrar_por_modulo($mod_id,$dt2['tb_comprapago_id'],$est);
						$num_rows3= mysqli_num_rows($dts3);
						while($dt3 = mysqli_fetch_array($dts3))
						{
							$abonos+= $dt3['tb_egreso_imp'];
						} 
						mysqli_free_result($dts3);
						$saldo = ($monto - $abonos);
}else{
	$saldo=0;
}
						if($mone_id==1){
						$lbl_mone = 'S/ ';
						$total_soles = $total_soles + $monto;
						}else if($mone_id==2){
						$lbl_mone = 'US$  ';	
						$total_dolares = $total_dolares +$monto;
						}
						?>
						<tr class="even" data-compagid = <?php echo $dt2['tb_comprapago_id']?> >
							<td NOWRAP >
							
							
							
							<?php
							if($dt2['tb_documento_abr']!='L'){
							 if($dt2['tb_comprapago_est']==2){
								?>
								
							<span style="cursor:pointer" onClick="egreso_form('insertar',<?php echo $dt2['tb_comprapago_id']?>)" 
								class="btn btn-primary btn-sm"  > <i class="fas fa-money-check"></i> PAGAR</span>
								
								<span style="cursor:pointer" onClick="compra_form('editar',<?php echo $dt2['tb_compra_id']?>)" 
								class="btn btn-success btn-sm"  > <i class="fas fa-eye-slash"></i> VER DOC.</span>
								
							<?php 
							 if($abonos>0){
								?>
								<span  style="cursor:pointer;" class="badge badge-info" onclick="pagos_form('<?php echo $dt2['tb_comprapago_id']?>')" >VER PAGOS</span>
								
								<?php 
							 }
							
							
							
							}else if($dt2['tb_comprapago_est']==1){
								?>
								
								<span style="cursor:pointer" onClick="compra_form('editar',<?php echo $dt2['tb_compra_id']?>)" 
								class="btn btn-success btn-sm"  > <i class="fas fa-eye-slash"></i> VER DOC.</span>
								
								<span style="cursor:pointer" class="btn btn-sm btn-primary" title="Subir y ver documento relacionado" 
								onClick="ver_archivos('<?php echo $dt2['tb_comprapago_id']?>','<?php echo $dt1['tb_compra_numdoc'] ?>')">
								<i class="fas  fa-unlink"></i></span>
								
								<span  style="cursor:pointer;" class="badge badge-info" onclick="pagos_form('<?php echo $dt2['tb_comprapago_id']?>')" >VER PAGOS</span>
								
								
								
							<?php 
							}else if($dt2['tb_comprapago_est']==3){
								?>
														
								<span style="cursor:pointer" style="cursor:pointer;" class="badge badge-danger">CANJEADO / CANCELADO<span>
								
							<?php 
							}
						}else{
							
							 if($dt2['tb_comprapago_est']==2){
							 	 ?>
								 
								
							
								 <?php
								 
								 if($dt2['tb_comprapago_con']!=0){	 
								  if($dt2['tb_comprapago_liq']>0){
									  
								  }else{
								 ?>
								 
								<span style="cursor:pointer" onClick="renovar('insertar_ren',<?php echo $dt2['tb_comprapago_id']?>)" 
								class="btn btn-default btn-sm "  > <i class="fas fa-bacon"></i> RENOVAR</span>
								
								 <?php 
								 }
								 }
								 
								 if($dt2['tb_comprapago_con']==3){				 
							 	 ?>
			
							 	  <span style="cursor:pointer" onClick="egreso_form('insertar',<?php echo $dt2['tb_comprapago_id']?>)"  
								class="btn btn-primary btn-sm"  > <i class="fas fa-money-check"></i> PAGAR</span>
							 	 <?php 
							 	 }
							 	 else if($dt2['tb_comprapago_con']!= 0 && $dt2['tb_banco_id']!=0){
							 	 ?>
							 	 <span style="cursor:pointer" onClick="egresoletra_form('insertarpagoletra',<?php echo $dt2['tb_comprapago_id']?>)" 
								class="btn btn-primary btn-sm"  > <i class="fas fa-money-check"> </i> PAGAR</span>
							 	 <?php 
							 	 }
								?>	
								<span style="cursor:pointer" onClick="letra_form('editar',<?php echo $dt2['tb_comprapago_id']?>,'cuentapagar')" 
								class="btn btn-success btn-sm"  > <i class="fa fa-edit"></i></span>
							<!--
								<span onClick="eliminar_letra('eliminarletra',<?php echo $dt2['tb_comprapago_id']?>)" 
								class="btn btn-danger btn-sm"  > <i class="fa fa-trash"></i></span>							
							-->
							<?php }else if($dt2['tb_comprapago_est']==1){
								?>
								<span style="cursor:pointer" onClick="letra_form('editar',<?php echo $dt2['tb_comprapago_id']?>,'pagado')"
								class="btn btn-success btn-sm"  > <i class="fa fa-edit"></i>  </span>
							<?php 
							 if($dt2['tb_comprapago_con']==3){
							 	?>
							 	<span  style="cursor:pointer" class="btn btn-sm btn-primary" title="Subir y ver documento relacionado" 
								onClick="ver_archivos('<?php echo $dt2['tb_comprapago_id']?>','<?php echo $dt1['tb_compra_numdoc'] ?>')">
								<i class="fas fa-unlink"></i></span>
								
								<span style="cursor:pointer;" class="badge badge-info" onclick="pagos_form('<?php echo $dt2['tb_comprapago_id']?>')" >VER PAGOS<span>
								<?php 
							 }else{
							 	?>
								<span style="cursor:pointer" class="btn btn-sm btn-primary" title="Subir y ver documento relacionado" 
								onClick="ver_archivos('<?php echo $dt2['tb_comprapago_id']?>','<?php echo $dt1['tb_compra_numdoc'] ?>')">
								<i class="fas fa-unlink"></i></span>
							 	<span style="cursor:pointer;" class="badge badge-info" onClick="pagosletra_form(<?php echo $dt2['tb_comprapago_id']?>)" >VER PAGOS<span>
								
	
							 	<?php 
							 }
							}else if($dt2['tb_comprapago_est']==3){
								?>
														
								<span style="cursor:pointer;" class="badge badge-danger">CANJEADO / CANCELADO<span>
								
							<?php 
							}
						}
							?>
							
								
								
							</td>
							
							
							<td align="left"><?php echo $dt2['tb_documento_nom']?></td>
							<td nowrap align="left"><?php echo $dt2['tb_compra_numdoc']?></td>
							<td nowrap="" align="center"><?php echo mostrarFecha($dt2['tb_compra_fec']);?></td>
							<td align="center">
							
							<?php 
							//if($dt2['tb_formapago_id']==1){
								//echo '<span class="label label-warning">EMITIDA AL CONTADO</span>';
							//}else{}
							echo mostrarFecha($dt2['tb_comprapago_fecven']);	//$dt2['tb_comprapago_fecven'];
							
							
							?>
							</td>
							<td>
							<?php if($dt2['tb_comprapago_con']==1){
								echo 'COBRANZAS';
							}else if($dt2['tb_comprapago_con']==2){
								echo 'DESCUENTOS';
							}else if($dt2['tb_comprapago_con']==3){
								echo 'CARTERAS';
							}?>
							</td>
							<td align="left"><?php echo $dt2['tb_comprapago_numuni']?></td>
							<td nowrap align="center"><?php echo $dt2['tb_empresa_nomcom'];?></td>
							<?php
							if($dt2['tb_formapago_id']==1){
							
							if($dt2['tb_comprapago_est']==1){
								
								$lbl = '<span class="badge badge-success">CANCELADO</span>';
								
							}else if($dt2['tb_comprapago_est']==2){
								
								$lbl =  '<span class="badge badge-danger">POR CANCELAR</span>';
								
							}						
							}else{
								$fechaven = comparar_fechavencimiento($dt2['tb_comprapago_fecven']);
								$lbl = '<span class="badge badge-'.$fechaven[0].'" >'.$fechaven[1].'</span>';
							}
							
							?>
							<td align="left"> <?php echo $lbl; ?> </td>
							<td align="left"> <strong class="text-red"> <?php echo formato_money($dt2['tb_comprapago_liq']); ?></strong> </td>
							<td NOWRAP align="right"><?php echo $lbl_mone.'<strong>'.formato_money($dt2['tb_comprapago_mon']).'</strong>'; ?></td>
							<td NOWRAP align="right"><?php echo $lbl_mone.'<strong>'.formato_money($abonos).'</strong>'; ?></td>
							<td NOWRAP align="right"><?php echo $lbl_mone.'<strong>'.formato_money($saldo).'</strong>' ;
							/*if(moneda_mysql($abonos)==0){
								
							}else{ 
							if(moneda_mysql($abonos)!=moneda_mysql($dt2['tb_comprapago_mon'])){
								echo ' <br /> <strong class="label label-primary">Renovado</strong>' ;
							} 
							};*/ 
							?></td>
							
							
							
							
							
							
							
							
							
							<!--	
							<td >
							<?php if($dt2['tb_comprapago_monope']>0){?>
							<input type="text" id="txt_gast_adm_<?php echo $dt2['tb_comprapago_id']; ?>" style="text-align:right" size="10" value="<?php echo formato_money($dt2['tb_comprapago_monadm']) ?>">
							<script type="text/javascript">
							validar(<?php echo $dt2['tb_comprapago_id']; ?>);
							
							</script>
							<?php }else{
								?>
								
							<input type="text" id="txt_gast_adm_<?php echo $dt2['tb_comprapago_id']; ?>" style="text-align:right" size="10" value="<?php echo formato_money($dt2['tb_comprapago_monadm']) ?>">
							
								<?php 
							}?>
							</td>							
							-->
							
						</tr>
						<?php 
						$contadorpro2 ++; 
					} 
					mysqli_free_result($dts2); 
					}else{
					?>
					<tr class="even" >
					<td colspan="13" align="center" >CUENTAS NO ENCONTRADAS</td>
					</tr>
					<?php 
					}
					?>
				</tbody>
				<tr class="even">					
					<td align="left"><?php echo $contadorpro2.' registros'?></td>
					<td colspan="10"   align="right" ><?php echo 'S/ <strong>'.formato_money($total_soles).'</strong> 
					<br />'.'US$ <strong>'.formato_money($total_dolares).'</strong>'; ?></td>	
					<td colspan="3"> </td>				
				</tr>
			</table>
			</div>
			
			
			
			
			 <script type="text/javascript">
  $(function() {  

	 $('#tabla_resumen_proveedor').DataTable({
		
        "language": {
                "lengthMenu": "Mostrar _MENU_ registros",
                "zeroRecords": "No se encontraron resultados",
                "info": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
                "infoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
                "infoFiltered": "(filtrado de un total de _MAX_ registros)",
                "sSearch": "Buscar:",
                "oPaginate": {
                    "sFirst": "Primero",
                    "sLast":"Último",
                    "sNext":"Siguiente",
                    "sPrevious": "Anterior"
			     },
			     "sProcessing":"Procesando...",
            }
    });  
	
	
  }); 
</script> 
			
			
			
			
			
			
			
			
			
			