<?php
include '../comunes_php/cors.php';
include '../rdb/conn.php';
include "../comunes_php/Log.class.php"; 
    
    $log = new Log("app_trace", "../");
    $parametros = print_r($_REQUEST,TRUE); 
    $log->insert($parametros, false, false, false);


   

    $desde= isset($_REQUEST['desde']) ? $_REQUEST['desde'] : '';
    $hasta= isset($_REQUEST['hasta']) ? $_REQUEST['hasta'] : '';


    if ($desde <>'') {$where .= " and saldo >= $desde "; }
    if ($hasta <>'') {$where .= " and saldo <= $hasta "; }

    $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
    $offset = ($page-1)*$rows;
    $result = array();


$sql = "select "
    . "count(*) "
    . "from"
    . " vw_proveedores where 1 ";
    
    $sql.= $where ;
    
    $log->insert($sql, false, false, false);

    
    $rs = mysqli_query( $conn,$sql);
    $row = mysqli_fetch_row($rs);
    $result["total"] = $row[0];
    $log->insert($sql, false, false, false);
	    
    if ($result["total"] === 0) {exit;}  
    
    $sql = "SELECT id,nombre,cuit,direccion,email,limite,saldo,telefono  FROM vw_proveedores where 1  ";
    
	$sql.=  $where;
	$sql.= " order by id desc limit $offset,$rows ";	
	$rs = mysqli_query( $conn,$sql);
    $log->insert($sql, false, false, false);
	    
    $items = array();
    while($row = mysqli_fetch_object($rs)){
        array_push($items, $row);
    }
    $result["rows"] = $items;
    $result["sql"] = $sql;


    echo json_encode($result);
    
  
    
?>