<?php
$usuario = $_COOKIE["usuario"];
$nivel = $_COOKIE["nivel"];
if($nivel!="Master"){header("location: ../menu/menu.php");}

include_once("../header.php");
include_once("../php/config.php");
include_once("../php/formatos.php");
include_once("../php/fechas.php");

$sql = mysql_query("select * from pagos_credito") or die(mysql_error());

?>
 <style>
  .form-control{
    font-size: 20px;
  }


b{
    font-size: 18px;
  }

  th{
    font-size:18px;
    text-align: left;
  }

    td{
    font-size:18px;
  }
</style>

<div class="container">
    <!-- Breadcrumbs-->
          
   
               
   <form id="frm" name="frm" action="" method="post">           
    <div class="row">
       
        <div class="col-md-12">
            <h3 class="text-danger">Anular cobros</h3>
            <div class="row"> 
                <div class="col-md-1">
                    <label>Buscar cliente</label>
                </div>
                                
                <div class="col-md-2">
                    <input type="search" class="form-control" value="<?php echo $_POST['txt-buscar']?>" placeholder="Buscar.." id="txt-buscar" name="txt-buscar" onKeyUp="buscar(this.value)">
                </div>
                 <div class="col-md-5">
                    <button class="btn btn-success" name="btn-buscar"><i class="fa fa-search"></i> Buscar</button>
                
                    <button onSubmit="$('#lista').html('Eliminando cobro, espere...')" class="btn btn-default" type="submit" name="btn-anular" ><i class="fa fa-remove"></i>  Anular seleccionados</button>
                </div> 
                

            </div>
                                
            <div class="row">
                    <div class="col-md-10">
                       
                            <div id="lista">
                                <?php
                                
                                if(isset($_POST["btn-buscar"])){
                                    $n = $_POST["txt-buscar"];
									if($n==""){echo "Digite nombre del cliente.."; return;}
                                    $sql2 = mysql_query("select * from pagos_credito where cliente like '%".$n."%'  order by cliente desc") or die(mysql_error());
                                    while($r=mysql_fetch_array($sql2)){
                                        $id_cobro= $r["id"];
                                        echo "<input type='checkbox' name='ch[]' value='$id_cobro'>"." <i class='fa fa-money'></i>   <span class='titulos'><a href='#'>".$r["hora"]."".$r["id_cobro"]."-".$r["cliente"]."(<span class='text-info'>".decimal($r["monto"]).")</span></span>";
                                        echo "<br>";
                                        
                                    }
                                }
                                
                                if(isset($_POST["btn-anular"])){
                                    $cnt=array();
                                     $cnt=count($_POST['ch']);
                                    
                                     for($i=0;$i<$cnt;$i++)
                                      {
                                         $del_id=$_POST['ch'][$i]; 
                                         $sql_cobros = mysql_query("select * from pagos_credito where id='$del_id'") or die(mysql_error()); 
                                         $id_factura = @mysql_result($sql_cobros,0,"id_factura");
										 $folio = @mysql_result($sql_cobros,0,"folio");
                                         $folio_ingreso = @mysql_result($sql_cobros,0,"folio_ingreso");
                                        
                                       
                                         $monto = @mysql_result($sql_cobros,0,"monto");
                                         
                                         
                                         #UPDATE CUOTAS
                                         mysql_query("UPDATE ventas SET balance=balance+'$monto'
                                                                        
                                                                        where id_venta='$id_factura'") or die(mysql_error());   
                                         
                                         $query="delete from pagos_credito where id='$del_id'";
                                         mysql_query($query) or die(mysql_error());
                                         
                                         
                                      }
									  echo "<span class='text-success' id='mensaje'>Cobro anulado!</span>";
                                 }
                                ?>
                                </div>
                       
                    </div>
            </div>
        </div>
                    
    </div>
    </form>     
            
            
</div>
          


          
</div>
    </div>

	<!---Modal Cliente-->
	<div class="modal" id="modalPrestamos" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Seleccione prestamo</h5>
            <button class="close" type="button" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">×</span>
            </button>
          </div>
          <div class="modal-body">
		   
          	<div class="table-responsive" style="max-height: 450px;">
		  	<table class="table table-sm table-borderred compact">
						<thead>
							<th># Prestamo</th>
							<th>Nombre/Apell. </th>
							<th>Monto</th>
							<th>Balance</th>
							<th>Accion</th>
							
						</thead>
						<tbody>
                                                        <?php while($r=mysql_fetch_array($sql)){?>
							<tr>
								<td><?php echo $r["id_prestamo"]?></td>
								<td><?php echo $r["nombre"]?></td>
								<td><?php echo @decimal($r["monto_prestado"])?></td>
								<td><span class="badge badge-success"><?php echo @decimal($r["balance"])?></span></td>
								 <td><a class="btn btn-sm btn-default mb-2" onClick="get(<?php echo $r["id_prestamo"]?>)" data-dismiss="modal"><i class="fa fa-edit"></i></a></td>
							</tr>
                                                        <?php }?>
						</tbody>
					</table>
					</div>
		   </div>
          <div class="modal-footer">
          
           <button class="btn btn-default btn-sm"  data-dismiss="modal"><i class="fa fa-home"></i>Salir</button>
          </div>
        </div>
      </div>
    </div>

        	<!---Modal mora-->
	<div class="modal" id="modalMora" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Ajuste de mora manual</h5>
            <button class="close" type="button" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">×</span>
            </button>
          </div>
          <div class="modal-body">
              <div class="row">
                  <label for="monto_mora" class="col-sm-4 col-form-label" >Monto</label>
                  <div class="col-sm-8">
                      <input autocomplete="off" type="number"  class="form-control mb-2" id="monto_mora" name="monto_mora" value="">
                  </div>
              </div>
          </div>
          <div class="modal-footer">
          <button  onClick="ajuste_mora()" class="btn btn-warning"  data-dismiss="modal"><i class="fa fa-check"></i>Aceptar</button>
           <button class="btn btn-default"  data-dismiss="modal"><i class="fa fa-home"></i>Salir</button>
          </div>
        </div>
      </div>
    </div>
        
        
	<?php
	include_once("../footer.php");
	?>
<script>                
function copia(id){
window.open('copia_recibo.php?id_recibo='+id,'_blank', 'toolbar=no,scrollbars=no,resizable=no,top=500,left=500,width=800,height=700');

}
</script>