

  <?php
	include_once("../header.php");
  if($_GET['filtro']!=""){
        include_once("../php/config.php");
        $sql = mysql_query("select * from ventas where concepto='Prestamo' and cliente like '%".$_GET['filtro']."%'") or die(mysql_error());
      }
	?>
<script>
///....
	if(myLevel!="Master"){
		alert("No tiene acceso a este modulo del sitema")
		window.location.href="../menu/menu.php";
		
	}


     function filtrar(){
    var t = $("#txt_buscar").val();
    window.location.href="prestamos.php?filtro="+t;
   }

</script>


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


b{
    font-size: 18px;
  }

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

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

    <div class="container-fluid">



        
		<div class="row">
			<div class="col-md-12">

		  
<div class="panel panel-default">
       <div class="panel-heading">
        

      <div class="row">
        <div class="col-md-4 mt-2 mb-2">
              <button class="btn btn-success" type="button" onclick="modalPrestamo()"><i class="fa fa-w fa-file"></i> Nuevo prestamo</button>
        </div>
        
        <div class="col-md-3 mt-2 mb-2">
          <input type="search" id="txt_buscar" placeholder="Filtrar registros" class="form-control" value="<?php echo $_GET['filtro']?>">
        </div>
        
        
        <div class="col-md-3 mt-2 mb-2">
          <a class="btn btn-default" href="#" onClick="filtrar()"><i class="fa fa-search"></i><span style="font-weight:bolder;"> Filtrar</span></a>
        </div>
      </div>

	
			<script>
			function modalPrestamo() {
			$(".form-control").val("")	
            $("#modalPrestamo").modal("show");
            $("#monto").focus().select();
          }
		  </script>
				<a class="btn btn-default" href="../menu/menu.php"><i class="fa fa-home"></i> Salir</a>
        <br><br>
       </div>
       <div class="panel-body">
	<!--Body-->	
				<div class="table-responsive border p-3">
					<table class="table table-sm" data-page-length='8' style="width: 100%">
						<thead>
							<th>Folio</th>
							<th>Fecha</th>
              <th>Cliente</th>
             
              <th>Monto</th>
              <th>balance</th>
              <th>Editar</th>
						</thead>
						<tbody>
            <?php while($r=mysql_fetch_array($sql)){
              $folio = $r["folio"];
              $c++;
              $total+=$r["total"];
              $balance+=$r["balance"];
              $costo+=$r["costo"];
              $ganancia+=$r["total"] - $costo;
             

              $cliente=$r["cliente"];
              ?>
							<tr>
								<td><?php echo $r["folio"]?></td>
                <td><span class="text-primary"><?php echo fecha($r["fecha"])?></span></td>
								<td><?php echo $r["cliente"]?></td>
                 <td><?php echo decimal($r["total"])?></td>
                <td><?php echo decimal($r["balance"])?></td>
               
             
               <td><a class="" href="javascript:void(0)" onClick="modalPrestamo();ver(<?php echo $r['id_venta']?>)"><i class="fa fa-edit"></i></a></td>
							</tr>
                                                        <?php }?>
						</tbody>
            <tfoot>
              <th>
                Cant. ventas: <?php echo $c?>
              </th>
              <th class="text-success">
                Total: <?php echo decimal($total)?>
              </th>

              <th>
                
              </th>

              <th class="text-success">
                
              </th>

              <th class="text-success">
                Balance general: <?php echo decimal($balance)?>
              </th>

            </tfoot>
					</table>
				</div>
       </div>
			
</div>		
				
		
			</div>
		</div>
		
        </div>
      </div>
      


          
</div>
    </div>

	<!---Modal prestamo-->
	<div class="modal" id="modalPrestamo" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-md" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h2 class="modal-title text-success" id="exampleModalLabel"><i class="fa  fa-edit"></i> Registrar nuevo prestamo</h2>
            <button class="close" type="button" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">×</span>
            </button>
          </div>
          <div class="modal-body">


		  <form name="frm" action="" method="POST" id="frm">
		  <input autocomplete="off" type="hidden"  class="form-control" id="id_venta" name="id_venta" value="">

		  <div class="row">
          <div class="col-md-3">
            <b>Cliente</b>
          </div>
          <div class="col-md-9">
            <select class="form-control  mb-2 btn-grabar" id="productor">

              <option value="" selected>Sel. productor</option>
              <?php $sql2 = mysql_query("select * from proveedores order by nombre asc") ?>

              <?php while ($r2 = mysql_fetch_array($sql2)) { ?>
                <option value="<?php echo $r2['nombre'] ?>">(<?php echo $r2['id_proveedor'] ?>)<?php echo $r2['nombre'] ?></option>
              <?php } ?>
            </select>
          </div>
        </div>	 

			  
		<div class="row">
          <label for="id" class="col-sm-3 col-form-label" >Fecha</label>
          <div class="col-sm-9">
            <input autocomplete="off" type="date"  class="form-control mb-2 font-weight-bolde" id="fecha" name="fecha" value="">
          </div>
        </div>

         <div class="row">
          <label for="codigo" class="col-sm-3 col-form-label" >Monto prestado</label>
          <div class="col-sm-3">
            <input autocomplete="off" type="number"  class="form-control mb-2" id="monto" name="monto" value="">
          </div>
        </div>
		
		 <div class="row">
          <label for="codigo" class="col-sm-3 col-form-label" >Cheque</label>
		   <div class="col-sm-3">
            <input autocomplete="off" type="number"  class="form-control mb-2" id="ncheque" name="ncheque" placeholder="No. cheque" value="">
          </div>
        </div>
         

		
	<div class="row">
          <label for="codigo" class="col-sm-3 col-form-label" >Detalles</label>
          <div class="col-sm-9">
            <input autocomplete="off" type="text"  class="form-control mb-2" id="nota" name="nota" value="">
          </div>

        </div>
		
		
		<hr>
		<div class="row">
          <label for="codigo" class="col-sm-3 col-form-label" >Total pagado</label>
          <div class="col-sm-3">
            <input autocomplete="off" type="number"  class="form-control mb-2" id="pagado" name="pagado" value="" disabled>
          </div>

        </div>
		
		<div class="row">
          <label for="codigo" class="col-sm-3 col-form-label" >Balance</label>
          <div class="col-sm-3">
            <input autocomplete="off" type="number"  class="form-control mb-2" id="balance" name="balance" value="" disabled>
          </div>

        </div>

			  
			  
		  </form>
		  
		  </div>
          <div class="modal-footer">
            <button class="btn btn-success" type="button" onclick="grabar()"><i class="fa fa-w fa-save"></i> Grabar</button>
			<button class="btn btn-danger" type="button" onclick="borrar()"><i class="fa fa-w fa-save"></i> Eliminar prestamo</button>


            <a class="btn btn-warning" data-dismiss="modal"> <i class="fa fa-w fa-home"></i> Salir</a>
          </div>
        </div>
      </div>
    </div>

        <?php
        include_once("../footer.php");

        function state($est) {

            if ($est == "1") {
                $b = "<span class='badge badge-success'>Activo</span>";
            } else {

                $b = "<span class='badge badge-danger'>Inactivo</span>";
            }

            return $b;
        }
        ?>
            
<script>
$("#delete").hide();
id_producto="";


function ver(id){
    $("#delete").show();
    dstry();
      $("#frm").trigger('reset');
      setTimeout(function(){
       // $("#nombre").focus();
        //$("#nombre").select();
    },1000);

            var sql="select * from ventas where id_venta='"+id+"'"
            _enviar("../php/traer_datos.php",{"sql":sql})
            .done(function( data ){
                var rs = JSON.parse(data);
       
                for( var i in rs.data){
                    id_prestamo= rs.data[i].id_venta;             
                   $("#fecha").val(rs.data[i].fecha);
                   $("#monto").val(rs.data[i].total);
                   $("#id_venta").val(rs.data[i].id_venta); 
                    $("#pagado").val(rs.data[i].pagado);
                   $("#balance").val(rs.data[i].balance);
				    $("#nota").val(rs.data[i].nota);
					$("#ncheque").val(rs.data[i].numero_cheque);
                    //$("#costo").val(rs.data[i].costo);
                   // $("#stock").val(rs.data[i].stock);
				   $(".form-control").prop("disabled",false)
                    $("#productor option[value='"+rs.data[i].cliente+"']").prop('selected', true);
                }
                
            })
        }
  
  function grabar(){
	 var id_venta = $("#id_venta").val() 
     if(!confirm("Seguro(a) que desea registrar este prestamo?")){return;}                   
    
     var nombre = $("#productor").val();
     var monto = $("#monto").val();
     var fecha = $("#fecha").val();
     
     var ncheque = $("#ncheque").val();
	   var nota = $("#nota").val();
	   var balance = $("#balance").val();
   





     if(nombre=="" || monto=="" ||  fecha==""){alert('Faltan datos'); return}
    var sql="";                            
        if(id_venta==""){
            sql="INSERT INTO ventas set ";
            sql+="cliente='"+nombre+"'";
            sql+=", total='"+monto+"'";
            sql+=", fecha='"+fecha+"'";
           
            sql+=", numero_cheque='"+ncheque+"'"; 
			sql+=", concepto='Prestamo'"; 
			sql+=", nota='"+nota+"'"; 
			sql+=", balance='"+monto+"'"; 

        }else{
            sql="UPDATE ventas set ";
            sql+="cliente='"+nombre+"'";
            sql+=", total='"+monto+"'";
            sql+=", fecha='"+fecha+"'";
           
           
            sql+=", numero_cheque='"+ncheque+"'"; 
			sql+=", nota='"+nota+"'"; 
			sql+=", balance='"+balance+"'"; 
			sql+=" where id_venta='"+id_venta+"'"; 
           
            
        }
        
    _enviar("../php/update.php",{"sql":sql})
    .done(function( data ){
      //var rs = JSON.parse(data);
      if(data=="1"){
       
        noti("success","Registro grabado con exito","Registro grabado");
        
         $(".modal").modal('hide');
        setTimeout(function(){window.location.reload()},2000)

      }else{
          alert(data);
          
      }
       
    })
}
  

 function borrar(){
	 var id_venta = $("#id_venta").val() 
     if(id_venta==""){msg('Seleccione un registro'); return}
     if(!confirm("Seguro(a) que desea eliminar este registro?")){return;}
    var sql="";                            
    sql="delete from ventas where id_venta='"+id_venta+"'";
        
    _enviar("../php/update.php",{"sql":sql})
    .done(function( data ){
      //var rs = JSON.parse(data);
      if(data=="1"){
        $(".modal").modal('hide');
        noti("success","Registro eliminado con exito","Registro grabado");
        $("#delete").hide();
       
        setTimeout(function(){window.location.reload()},1000)

      }else{
          msg(data);
          
      }
        //$("#loading").modal('hide');
    })
	sql="delete from detalle_venta where id_venta='"+id_venta+"'";
	 _enviar("../php/update.php",{"sql":sql})
    .done(function( data ){
      //var rs = JSON.parse(data);
      if(data=="1"){
        $(".modal").modal('hide');
        noti("success","Registro eliminado con exito","Registro grabado");
        $("#delete").hide();
       
        setTimeout(function(){window.location.reload()},1000)

      }else{
          msg(data);
          
      }
        //$("#loading").modal('hide');
    })
	
}



function pago(f){
  window.location.href='pago_factura.php?cliente='
  
}

$("table").DataTable({
   "language": {
            "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Spanish.json"
        }
});
         
        </script>

