 <?php

if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400'); // cache for 1 day
}

error_reporting(E_ERROR | E_PARSE);
session_start();

//include_once ("conn.php");
include_once "funciones.php";

//$ip_caja=$_GET["ip"];
$_SESSION['suc_id'] = "2";

//$_SESSION['e_id'] =$_GET["est"];

//$_SESSION['e_id'] ='0';

$ip_caja = "localhost";

$connection = new mysqli($ip_caja, "remoto", "1234") or die(mysqli_error());

$connectionserver = new mysqli('localhost', "remoto", "1234") or die(mysqli_error());

$_SESSION['database'] = 'ocadb';

$_SESSION['databaseserver'] = 'ocadb';

$_SESSION['ip_server'] = 'localhost';

//http://186.5.140.50:4000/rest/v5/robot/verificaop.php?201.221.252.242&e_id=5

// $SQLupdate2o="delete from operacion where op_estacion=".$_SESSION['e_id']." and    op_id not in (select po_op_id from producto_op) and op_fecha>'2017-11-01';";

// $SQLupdate2o="delete from operacion where suc_id=".$_SESSION['e_id']." and    op_id not in (select po_op_id from producto_op) and op_fecha>'2017-11-10';";
/*        if ($_SESSION['est_id']=="0"){

$SQLupdate2o="delete from ".$_SESSION['databaseserver'].".operacion where suc_id=".$_SESSION['suc_id']." and    op_id not in (select po_op_id from producto_op where suc_id=".$_SESSION['suc_id']." ) and op_fecha>'2017-11-10';";

}else {

$SQLupdate2o="delete from ".$_SESSION['databaseserver'].".operacion where op_estacion=".$_SESSION['e_id']."  and    op_id not in (select po_op_id from producto_op where e_id=".$_SESSION['e_id'].") and op_fecha>'2017-11-01';";

};

if ($connectionserver->query($SQLupdate2o) === TRUE) {

//echo 'creando tabla tmp_pack_v2';

} else {
logs( "Error: " . $SQLupdate2o . "<br>" . $connection->error);
};

 */

$montoesql = "select op_id,op_fecha from " . $_SESSION['database'] . ".operacion where  op_fecha>'2017-11-25' and suc_id=" . $_SESSION['suc_id'] . " and  (op_tipo=2 ) order by op_fecha asc";

$qur = $connection->query($montoesql);

$i = 1;
while ($r = mysqli_fetch_assoc($qur)) {

    $_SESSION['op_id'] = $r['op_id'];

    $_SESSION['op_fecha'] = $r['op_fecha'];

    $ver = verificacorre($_SESSION['op_id'], $_SESSION['ip_server']);

    $ver = trim($ver);

//echo 'trajo '.$ver;

    $_SESSION['total'] = '0';

    if ($ver == 'false') {

        $SQLupdate = 'update ' . $_SESSION['database'] . '.operacion set usermodify=1,syncprocesing=0 where op_id=' . $_SESSION['op_id'];

        if ($connection->query($SQLupdate) === true) {

            // $msg = array("success" => "true", "detalle" => "Detalle ejemplo");

        } else {

//    $msg = array("success" => "false", "detalle" => $conn);

            // echo "Error: " . $SQLupdate . "<br>" . $conn->error;
        }

        echo $i . ' No lo tengo : ' . $_SESSION['op_id'] . '  Fecha: ' . $_SESSION['op_fecha'] . '  ';

//    logs($i.  ' No lo tengo : '.$_SESSION['op_id'].'  Fecha: '.$_SESSION['op_fecha']);

        $i = $i + 1;
        $_SESSION['total'] = $i;
    } else {

        echo ' Si Lo tengo : ' . $_SESSION['op_id'] . '   Fecha: ' . $_SESSION['op_fecha'] . ' ';

//logs( ' SI Lo tengo : '.$_SESSION['op_id'].'   Fecha: '.$_SESSION['op_fecha']);

    }
    ;

//echo 'total de No encrontrados: '. $_SESSION['total'];

    //echo  "<br>" ;

}

echo 'total de No encrontrados: ' . $_SESSION['total'];

function verificacorre($op_id, $ip_server)
{
    $url1 = "http://" . $ip_server . "/rest/veropsap.php?op_id=" . $op_id . "&database=ocadb";

//$url1="http://201.221.252.230:4000/rest/v3/verop.php?op_id=".$op_id."&database=ocadb";

    $data_string99 = '{"operacion":{"tipo":"2","nombre":"Juanita Sierra","ruc":"25","direccion":"Preparatoria","telefono":"2297596"},
"productos":[{"pr_id":"08989","descripcion":"soda pepsi","cantidad":"5.90","precio":"12345","desc":"0","iva":"7.00"},
{"pr_id":"111","descripcion":"soda coca cola","cantidad":"5.90","precio":"12345","desc":"0","iva":"7.00"}],
"cant_prod":2,"pagos":[{"detalle":"efectivo","monto":"500"}]}';

    $ch = curl_init($url1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    //      'Content-Type: application/json',
    //   'Content-Length: ' . strlen($data_string))
    //   );
    $result = curl_exec($ch);
    curl_close($ch);

    return $result;

}

@mysqli_close($connection);

?>

