<?php
// ini_set('display_errors',1);
date_default_timezone_set('America/Lima');
require_once ("../../config/Cado.php");
require_once ("cCuentacobrar.php");
$oCuentacobrar = new cCuentacobrar();
session_start();
require_once ("../ingreso/cIngreso.php");
$oIngreso = new cIngreso();
require_once ("../formatos/formato.php");
$contadorpro = 0;
$total_soles = 0;
$total_dolares = 0;

$dts1 = $oCuentacobrar->cuentas_por_cobrar($_POST['hdd_fil_cli_id'],$_POST['cmb_fil_empresa_id'],$_POST['cmb_fil_mon_id'],$_POST['cmb_fil_lim_id']);

//echo $_POST['cmb_fil_empresa_id'].'-'.$_POST['cmb_fil_mon_id'];

$num_rows= mysqli_num_rows($dts1);
?>
<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_resumen_todos" class="table table-bordered table-striped">
           <thead>
			<tr>				
				<th align="left">CLIENTE</th>
				<th align="left">DNI/RUC</th>	
				<th align="right">MONTO</th>
				<th align="right">ABONOS</th>
				<th align="right">SALDO</th>
				<th width="10%" class="header">&nbsp;</th>
			</tr>
		</thead>
        <tbody>	
		
		<?php
		if($num_rows>0){
		while($dt1 = mysqli_fetch_array($dts1))
		{ 
	//echo $dt1['tb_cliente_id'];
			$monto = $dt1['monto'];						
			$abonos = 0;
			$mone_id = $dt1['tb_moneda_id'];	
			$dts2 = $oCuentacobrar->cuentas_por_cobrar_por_cliente2($dt1['tb_cliente_id'],'1',$mone_id);
			$num_rows2= mysqli_num_rows($dts2);
			// echo '--'.$num_rows2;
			while($dt2 = mysqli_fetch_array($dts2))
			{
				//echo '-'.$dt2['tb_ventapagoingresos_id'];
				$mod_id=3;
				$est1=1;
				$est2=2;
				$dts3 = $oIngreso->mostrar_por_modulo($mod_id,$dt2['tb_ventapagoingresos_id'],$est1,$est2);
				$num_rows3= mysqli_num_rows($dts3);
				while($dt3 = mysqli_fetch_array($dts3))
				{
					$abonos += $dt3['tb_ingreso_imp'];
				} 
				mysqli_free_result($dts3);
			} 
			mysqli_free_result($dts2);
			$saldo = $monto-$abonos;
		
			//echo '-'.$abonos;
			
			if($mone_id==1){
					$lbl_mone = 'S/ ';
					$total_soles = $total_soles + $saldo;
			}else if($mone_id==2){
					$lbl_mone = 'US$ ';
					$total_dolares = $total_dolares + $saldo;
			}
			
		?>
			<tr class="even">
				<td align="left"><?php echo $dt1['tb_cliente_nom']?></td>
				<td align="left"><?php echo $dt1['tb_cliente_doc']?></td>		
				<td nowrap align="right"><?php echo $lbl_mone.'<strong>'.formato_money($monto).'</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>';?></td>
				<td nowrap >  
				<span style="cursor:pointer" onClick="cuentacobrar_tabladeudas('<?php echo $dt1['tb_cliente_id']?>','<?php echo $dt1['tb_empresa_id']?>','<?php echo $dt1['tb_moneda_id']?>')" id="verdetalles"  class="btn btn-primary" >DEUDAS</span>
				</td>              
			</tr>
		<?php 
		}
		mysqli_free_result($dts1);
		}else{
		?>
		<tr>
		<td colspan="7" align="center" >No hay cuentas por cobrar.</td>
		</tr>
		<?php 
		}
		?>
		
		
		</tbody>
			<tr class="even">
				<td align="left"><?php echo $num_rows.' registros'?></td>
				<td>TOTAL</td>
				<td></td>

				<td></td>
				<td  align="right" ><strong> <?php echo 'S/. <strong>'.formato_money($total_soles).'</strong> 
					<br />'.'US$ <strong>'.formato_money($total_dolares).'</strong>'; ?></strong></td>
				<td></td>
			</tr>
      </table>
    
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>