<?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);
$jsoncliente = $_POST['jsoncliente'];

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

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

$obj = json_decode($jsoncliente);

//print_r($obj);

$database = $obj->{'database'};
$c_desc = $obj->{'c_desc'};
$c_dir = $obj->{'c_dir'};
$c_idfiscal = $obj->{'c_idfiscal'};
$c_activo = $obj->{'c_activo'};
$c_vendedor = $obj->{'c_vendedor'};
$c_u_id_crea = $obj->{'c_u_id_crea'};
$suc_id = $obj->{'suc_id'};
$c_telefono = $obj->{'c_telefono'};
$id_contribuyente = $obj->{'id_contribuyente'};

//print_r($database." ".$c_desc);

$jsonData3 = array();
$jsonTempData3 = array();

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

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

$json->datos = "true";

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

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

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

//print_r($sql);

if ($c_id == "0") {

    $sql = "insert into " . $database . ".clientes
		  (c_id,
		  c_idfiscal,
		  c_u_id_crea,
		  c_vendedor,
		  c_telefono,
		  c_direccion,
		  c_nom_comercial,
		  c_descripcion,suc_id,id_contribuyente,c_activo) values (
		  '" . $c_idfiscal . "',
		  '" . $c_idfiscal . "',
		  '" . $c_u_id_crea . "',
		  '" . $c_vendedor . "',
		  '" . $c_telefono . "',
		  '" . $c_dir . "',
		  '" . $c_desc . "',
		  '" . $c_desc . "',
		  " . $suc_id . ",
		  " . $id_contribuyente . ",
		  1)";
    $json->datos = "Crear";
} else {
    //$c_dir = "548#";
    $sql = "update " . $database . ".clientes
	set c_descripcion = '" . $c_desc . "',
	c_nom_comercial = '" . $c_desc . "',
	c_direccion = '" . $c_dir . "',
	c_telefono = '" . $c_telefono . "',
	c_vendedor = '" . $c_vendedor . "',
	c_u_id_crea = '" . $c_u_id_crea . "',
	id_contribuyente = " . $id_contribuyente . ",
	c_activo = 1
	where c_idfiscal = '" . $c_idfiscal . "'";
    $json->datos = "Actualizar";

}

//print_r($sql);
$qur = $connection->query($sql);
print_r(json_encode($json));
$qur = null;
@mysqli_close($connection);
?>
