<?php

date_default_timezone_set('America/Lima');

require_once ("../../config/Cado.php");

require_once ("cCuentacobrar.php");

$oCuentacobrar = new cCuentacobrar();

require_once ("../cliente/cCliente.php");

$oCliente = new cCliente();

require_once ("../ingreso/cIngreso.php");

$oIngreso = new cIngreso();



require_once ("../formatos/formato.php");

require_once ("../formatos/fechas.php");



$dts3x = $oCliente->mostrarUno($_POST['cli_id']);

$dt3x = mysqli_fetch_array($dts3x);

$cliente = $dt3x['tb_cliente_nom'];

$numdoc = $dt3x['tb_cliente_doc'];

mysqli_free_result($dts3x);



$contadorpro2 = 0;

$total_soles = 0;

$total_dolares = 0;





$emp_id = $_SESSION['empresa_id'];

$dts2 = $oCuentacobrar->cuentas_por_cobrar_por_cliente($_POST['cli_id'],$emp_id);

$num_rows2= mysqli_num_rows($dts2);



?>







<div  class="col-lg-12 col-sm-12 col-xs-12" style="background-color:#fff;" >

	

	<div class="modal-header">

	<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>

	<h4 class="modal-title" id="titlemodal">Deudas del cliente</h4> 

</div>



	

		<div id="div_tabla_clientes" class="box-body" style="display: block;">

			<table id="tabla_resumen_cliente" class="table table-bordered table-striped">

				<thead>

					<tr>

					<th colspan="8">

					<div class="box-header with-border">

						<div class="user-block">

						<img class="img-circle" src="../../imagenes/users/user1.jpg" alt="User Image" title="">

						<span class="username"><a href="#"><?php echo "CLIENTE: ".$cliente;?></a></span>

						<span class="description">DNI - RUC : <?php echo $numdoc; ?></span>

						</div>

					</div>

					</th>

					</tr>

					<tr  >

						<th>DETALLE DE LA VENTA</th>				

						<th align="center">FECHA VENTA</th>

						<th align="center">FECHA VENCIMIENTO</th>

						<th align="left">ESTADO</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_rows2>0){

			while($dt2 = mysqli_fetch_array($dts2))

			{



				$monto = $dt2['tb_ventapago_mon'];						

				$abonos = 0;

				$mone_id = $dt2['tb_moneda_id'];	

				$mod_id=3;

				$est=1;

				$dts3 = $oIngreso->mostrar_por_modulo($mod_id,$dt2['tb_ventapago_id'],$est);

				$num_rows3= mysqli_num_rows($dts3);

				while($dt3 = mysqli_fetch_array($dts3))

				{

					$abonos+= $dt3['tb_ingreso_imp'];

				} 

				mysqli_free_result($dts3);



				$saldo = ($monto - $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 $dt2['tb_documento_nom'].' - '.$dt2['tb_venta_numdoc']?></td>

					<td align="center"><?php echo fecha_entexto($dt2['tb_venta_fec']);?></td>

					<td align="center"><?php echo fecha_entexto($dt2['tb_ventapago_fecven']);?></td>

					<?php $fechaven = comparar_fechavencimiento($dt2['tb_ventapago_fecven']);?> 



					<td align="left"><span class="label label-<?php echo $fechaven[0]?>"><?php echo $fechaven[1]?></span> </td>

					<td align="right"><?php echo $lbl_mone.'<strong>'.formato_money($dt2['tb_ventapago_mon']).'</strong>'; ?></td>

					<td align="right"><?php echo $lbl_mone.'<strong>'.formato_money($abonos).'</strong>'; ?></td>

					<td align="right"><?php echo $lbl_mone.'<strong>'.formato_money($saldo).'</strong>'; ?></td>

					<td nowroap>

						<span onClick="ingreso_form('insertar',<?php echo $dt2['tb_ventapago_id']?>)"

						class="btn btn-primary btn-sm" ><i class="fa fa-money"></i>  PAGAR</span>

					</td>              

				</tr>

				<?php 

				$contadorpro2 ++;

			} 

			mysqli_free_result($dts2); 

			}else{

				?>

				<tr>

				<td colspan="8" align="center" >ESTE CLIENTE NO TIENE DEUDAS</td>

				</tr>

				<?php 

			}

			?>

		</tbody>

		<tr class="even">		

			<td align="left"><?php echo $contadorpro2.' registros'?></td>

			<td colspan="6" align="right" > <?php echo 'S/. <strong>'.formato_money($total_soles).'</strong> 

					<br />'.'US$ <strong>'.formato_money($total_dolares).'</strong>'; ?></td>	

			<td colspan="2"> </td>

		</tr>

			</table>

			

		</div> 

	

	

	<div class="modal-footer pull-right">	 

	<button type="button" class="btn btn-default pull-left"  data-dismiss="modal">Cancelar</button>

	</div>

	

	

	

	

	

	</div>			

	











<script type="text/javascript">



function ingreso_form(act,id_vp){	

	

				$('#div_ingreso_form_dialog').modal(); 

		$.ajax({

			type: "POST",

			url: "../cuentacobrar/ingreso_cuentacobrar.php",

			async:true,

			dataType: "html",                      

			data: ({

				action: act,

				venpag_id : id_vp,

				vista: 'cuentacobrar_inicio'

			}),

			beforeSend: function() {

			var div = "<center><div class='overlay col-lg-12 col-sm-12 col-xs-12' style='background-color:#fff;padding:0%;font-size:3rem;' >  <i class='fa fa-refresh fa-spin ' ></i> </div>  </center>";				

			$('.div_ingreso_form .modal-content').html(div);



			},

			success: function(html){

				$('.div_ingreso_form').html(html);				

			},

			complete: function(html){

			}

		});

	}





</script>