<?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);
$orden = $_GET['orden'];
$database = $_GET['database'];

include 'conn.php';
include 'funciones.php';

$jsonData = array();
$jsonTempData = array();

// $sql= "SELECT c_id FROM ".$database.".clientes
//               where c_idfiscal = '".$c_idfiscal."'";

$sql = "SELECT o.op_id_cliente,
       o.c_idfiscal2,
       o.op_nombre,
	   o.op_direc,
	   o.op_telf,
	   case when c.id_contribuyente is null then 0 else c.id_contribuyente end as id_contribuyente
	   FROM " . $database . ".tmp_operacion o left join " . $database . ".clientes c on o.c_idfiscal2 = c.c_idfiscal where o.op_id = '" . $orden . "'";

$qur = $connection->query($sql);
while ($r = mysqli_fetch_assoc($qur)) {

    $jsonTempData = array();
    $jsonTempData['op_id_cliente'] = utf8_encode($r['op_id_cliente']);
    $jsonTempData['c_idfiscal2'] = utf8_encode($r['c_idfiscal2']);
    $jsonTempData['op_nombre'] = utf8_encode($r['op_nombre']);
    $jsonTempData['op_direc'] = utf8_encode($r['op_direc']);
    $jsonTempData['op_telf'] = utf8_encode($r['op_telf']);
    $jsonTempData['id_contribuyente'] = $r['id_contribuyente'];
    $jsonData[] = $jsonTempData;
}

$json->jsonCliente = $jsonData;

$json->datos = "true";

$jsonTempData3 = array();
$c_id = "0";

while ($r = mysqli_fetch_assoc($qur)) {

    $c_id = $r['c_id'];
}

//print_r($sql);

print_r(json_encode($json));

$qur=null;
@mysqli_close($connection);
?>