<?php
session_start();
require_once ("../../config/Cado.php");
require_once ("cCuentapagar.php");
$oCuentapagar = new cCuentapagar();
require_once ("../formatos/formato.php");
 
$contadorpro = 0;
$total_soles = 0;
$total_dolares = 0;
$dts1 = $oCuentapagar->cuentas_por_pagar($_POST['hdd_fil_pro_id'],$_POST['cmb_fil_empresa_id'],$_POST['cmb_fil_mon_id']);
$num_rows= mysqli_num_rows($dts1);
 
 //echo $_POST['cmb_fil_mon_id'];
 
?>
<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">PROVEEDOR</th>	
				<th align="left">DNI/RUC</th>
				<th align="left">EMPRESA</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){
			$saldox = 0;
			$abonox = 0;
			$montox = 0;
		while($dt1 = mysqli_fetch_array($dts1))
		{ 
	
			$montox = 0;
			//echo $dt1['tb_proveedor_id'].'-'.$dt1['tb_empresa_id'].'-'.$dt1['tb_moneda_id'];
			
			/* $dts2 = $oCuentapagar->cuentas_por_pagar_por_proveedor1($dt1['tb_proveedor_id'],$dt1['tb_empresa_id'],$dt1['tb_moneda_id']);
			while($dt2 = mysqli_fetch_array($dts2))
			{
				//$montox += $dt2['tb_comprapago_mon'];	
				
				 if($dt2['tb_documento_id']=='22'){
						$coin = strpos($dt2['tb_compra_numdoc'],'-');
							
				 if($coin===false){
						 $montox += $dt2['tb_comprapago_mon'];		
				 }else{
					 $montox = 0;
				 }
							
				 }else{
					 $montox += $dt2['tb_comprapago_mon'];		
				}
			
			
			}
				mysqli_free_result($dts2); 		 */
	
			$montox = $dt1['monto'];
			$mone_id = $dt1['tb_moneda_id'];	
		
			$dts4 = $oCuentapagar->cuentas_por_pagar_por_proveedor_2($dt1['tb_proveedor_id'],$dt1['tb_empresa_id'],$mone_id);
			$num_rows4= mysqli_num_rows($dts4);
			if($num_rows4>=1){
				while($dt4 = mysqli_fetch_array($dts4)) {
					$abonox = $dt4['sumpagado'];
				} 
				mysqli_free_result($dts4);
			}
			
		
			$dts5 = $oCuentapagar->cuentas_por_pagar_por_proveedor1($dt1['tb_proveedor_id'],$dt1['tb_empresa_id'],$mone_id);
			$num_rows5= mysqli_num_rows($dts5);
			if($num_rows5>=1){
				while($dt5 = mysqli_fetch_array($dts5)) {
					$saldox = $dt5['saldo'];
				} 
				mysqli_free_result($dts5);
			}
			
			if($mone_id==1){
					$lbl_mone = 'S/ ';
					$total_soles = $total_soles + $saldox;
			}else if($mone_id==2){
					$lbl_mone = 'US$ ';
					$total_dolares = $total_dolares + $saldox;
			}
				
		?>
			<tr class="even" data-dat = "<?php echo $dt1['tb_proveedor_id'].'- E'.$dt1['tb_empresa_id']; ?>"  data-comid ="<?php echo $dt1['tb_compra_id']?>" >
				<td align="left"><?php echo $dt1['tb_proveedor_nom']?></td>
				<td align="left"><?php echo $dt1['tb_proveedor_doc']?></td>					
				<td align="left"><?php echo $dt1['tb_empresa_nomcom']?></td>					
				<td align="right"><?php echo $lbl_mone.'<strong>'.formato_money($montox).'</strong>';?></td>
				<td align="right"><?php echo $lbl_mone.'<strong>'.formato_money($abonox).'</strong>';?></td>
				<td align="right"><?php echo $lbl_mone.'<strong>'.formato_money($saldox).'</strong>'?></td>
				<td nowrap > 
				<button onClick="cuentapagar_tabladeudas('<?php echo $dt1['tb_proveedor_id']?>','<?php echo $mone_id ?>','<?php echo $dt1['tb_empresa_id']?>')" id="btn_ver_detalles"  class="btn btn-primary btn-sm" >DETALLES</button>
 
				</td>              
			</tr>
		<?php 
			$contadorpro ++; 
		} 
		mysqli_free_result($dts1); 
		
		}else{
			?>
			<tr>
			<td colspan="7" align="center" >No hay cuentas por pagar.</td>
			</tr>
			
			<?php 			
		}
		
	?>
		</tbody>
			<tr class="even">
			
				<td align="left"><?php echo $contadorpro.' registros'?></td>
				<td>TOTAL</td>
				<td><?php ?></td>
				<td><?php ?></td>
				<td><?php ?></td>
				<td  align="right" ><?php echo 'S/ <strong>'.formato_money($total_soles).'</strong> 
					<br />'.'US$ <strong>'.formato_money($total_dolares).'</strong>'; ?></td>
				<td> </td>
			
			</tr>
      </table>
    
					</div>
				</div>
			</div>
		</div>
	</div>
	</div>
</section>