<?php
include '../comunes_php/cors.php';
include '../rdb/conn.php';
    $myfile = fopen("logFileGet.txt", "a") or die("Unable to open file!");
    $parametros = print_r($_REQUEST,TRUE);
    fwrite($myfile, "\n");
    fwrite($myfile, "---------------------------". date("Y-m-d H:i:s") ."--------------------------------------------------------");
    fwrite($myfile, "\n");    
    fwrite($myfile, "----------------------------nuevo registro log-------------------------------------------------------");
    fwrite($myfile, "\n");    
    fwrite($myfile, "-----------------------------------------------------------------------------------------------------");
    
    fwrite($myfile, "\n");
    
    fwrite($myfile, $parametros);
    fclose($myfile);

    $clientes= isset($_REQUEST['clientes']) ? $_REQUEST['clientes'] : '';
    $repartidores= isset($_REQUEST['repartidor']) ? $_REQUEST['repartidor'] : '';

    $zonas= isset($_REQUEST['zonas']) ? $_REQUEST['zonas'] : '';
    $empresas= isset($_REQUEST['empresas']) ? $_REQUEST['empresas'] : '';
    $incompletos= isset($_REQUEST['incompletos']) ? $_REQUEST['incompletos'] : '0';
    
    

    $idesde= isset($_REQUEST['idesde']) ? $_REQUEST['idesde'] : '';
    $ihasta= isset($_REQUEST['ihasta']) ? $_REQUEST['ihasta'] : '';

    $adesde= isset($_REQUEST['adesde']) ? $_REQUEST['adesde'] : '';
    $ahasta= isset($_REQUEST['ahasta']) ? $_REQUEST['ahasta'] : '';


    
    

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

    $where ="";
   
    if ($clientes <>'') {$where .= " and id in (select envio_id from envios_detalle ed ,contratos_cabecera cc where cc.id=ed.contrato_id and  cc.cliente_id in ($clientes)) "; }
    if ($incompletos =='true') {$where .= " and pendientes > 0 "; }
    if ($repartidores <>'') {$where .= " and repartidor_id in ($repartidores) "; }
      
    if ($empresas <>'') {$where .= " and id in (select envio_id from envios_detalle ed ,contratos_cabecera cc where cc.id=ed.contrato_id and  cc.empresa_id in ($empresas)) "; }
    if ($zonas <>'') {$where .= "and id in (select envio_id from envios_detalle ed ,contratos_cabecera cc where cc.id=ed.contrato_id and  cc.zona_id in ($zonas))  "; }
    if ($aliasml <>'') {$where .= "and id in (select envio_id from envios_detalle ed ,contratos_cabecera cc where cc.id=ed.contrato_id and  cc.alias_ml  like '%$aliasml%')     "; }
    
    if ($idesde<>'') {$where .= " and fecha_iniciado >= '$idesde' "; }
    if ($ihasta<>'') {$where .= " and fecha_iniciado <= '$ihasta' "; }   
    if ($adesde<>'') {$where .= " and fecha_aprobado >= '$adesde' "; }
    if ($ahasta<>'') {$where .= " and fecha_aprobado <= '$ahasta' "; }   
    

    
    if ($res_id <>'') {$where .= " and id in ($res_id) "; }
    
    $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_envios where 1 ";
$sql.= $where ;
    

    
    $rs = mysqli_query( $conn,$sql);
    $row = mysqli_fetch_row($rs);
    $result["total"] = $row[0];
    		    $myfile = fopen("logFileGet.txt", "a") or die("Unable to open file!");
      		    fwrite($myfile, "\n");  
      		    fwrite($myfile, date("Y-m-d H:i:s")."*****************MODIFICACION*****************");
		        fwrite($myfile, "\n");      		    
        	    fwrite($myfile, $sql);
        	    fwrite($myfile, "\n");
        	    fclose($myfile);	    
    if ($result["total"] === 0) {exit;}  
    
$sql = "select "
    . "*  "
    . "from "
    . "vw_envios where 1  ";
    
	$sql.=  $where;
	$sql.= " order by id desc"; //limit $offset,$rows ";	
	$rs = mysqli_query( $conn,$sql);
 		    $myfile = fopen("logFileGet.txt", "a") or die("Unable to open file!");
      		    fwrite($myfile, "\n");
      		    fwrite($myfile, $dhasta);
      		     fwrite($myfile, "\n");
      		    fwrite($myfile, $ddesde);
      		     fwrite($myfile, "\n");
      		    fwrite($myfile, $dporcentual);
      		     fwrite($myfile, "\n");
      		    
      		    fwrite($myfile, date("Y-m-d H:i:s")."*****************MODIFICACION*****************");
		        fwrite($myfile, "\n");      		    
        	    fwrite($myfile, $sql);
        	    fwrite($myfile, "\n");
        	    fclose($myfile);	    
    $items = array();
    while($row = mysqli_fetch_object($rs)){
        array_push($items, $row);
    }
    $result["rows"] = $items;
    $result["sql"] = $sql;


    echo json_encode($result);
    
  
    
?>