<?php
include_once("utiles.php");

date_default_timezone_set("America/Panama");
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING );

if (! $connection = connectDb()) { 
	die ( "err db" );
} else {
	$connection->query( "SET NAMES 'utf8';" );
}
header ( 'content-type: application/json' );
 
session_start();
$_SESSION['database'] = $_POST['database'];
$_SESSION['c_id'] = $_POST['c_id'];
$_SESSION['c_descripcion'] = $_POST['c_descripcion'];
$_SESSION['c_direccion']=$_POST['c_direccion'];
$_SESSION['c_idfiscal'] = $_POST['c_idfiscal'];
$_SESSION['pa_id'] = $_POST['pa_id'];
$_SESSION['ciu_id'] = $_POST['ciu_id'];
$_SESSION['c_contacto'] = $_POST['c_contacto'];
$_SESSION['c_telefono'] = $_POST['c_telefono'];
$_SESSION['c_credito'] = $_POST['c_credito'];
$_SESSION['c_dias_credito'] = $_POST['c_dias_credito'];
$_SESSION['c_limite_credito'] = $_POST['c_limite_credito'];
$_SESSION['c_vendedor'] = $_POST['c_vendedor'];
$_SESSION['id_contribuyente'] = $_POST['id_contribuyente'];
$_SESSION['c_correo'] = $_POST['c_correo'];
$_SESSION['c_exclusiv_cont'] = $_POST['c_exclusiv_cont'];
$_SESSION['c_apart'] = $_POST['c_apart'];
$_SESSION['c_club']=$_POST['c_club'];
$_SESSION['c_activo'] = $_POST['c_activo'];
$_SESSION['suc_id'] = $_POST['suc_id'];
$_SESSION['c_ref2'] = $_POST['c_ref2'];
$_SESSION['c_ref3'] = $_POST['c_ref3'];
$_SESSION['c_precio'] = $_POST['c_precio'];
$_SESSION['c_desc'] = $_POST['c_desc'];
$_SESSION['usuario_id'] = $_POST['usuario_id'];

$hoynow    = getdate();
$timeStamp = $hoynow["year"]."-".$hoynow["mon"]."-".$hoynow["mday"] ;

$connection->query("USE `" .$_SESSION['database']."`");
	
$SQL_Insert=
"INSERT INTO ".$_SESSION['database'].".clientes (
	c_id			, c_descripcion	, pa_id,
	ciu_id			, c_idfiscal	, c_contacto,
	c_telefono		, c_credito		, c_dias_credito,
	c_limite_credito, c_vendedor	, id_contribuyente	,
	c_correo		, c_exclusiv_cont,	c_creacion,
	c_apart			, c_activo		, suc_id,
	c_ref2			, usermodify	, useroption, 
	c_direccion		, c_club		, c_u_id_crea,
	c_precio,c_desc) VALUES (
	".$_SESSION['c_id'].",
	".$_SESSION['c_descripcion'].",
	".$_SESSION['pa_id'].",
	".$_SESSION['ciu_id'].",
	".$_SESSION['c_idfiscal'].",
	".$_SESSION['c_contacto'].",
	".$_SESSION['c_telefono'].",
	".$_SESSION['c_credito'].",
	".$_SESSION['c_dias_credito'].",
	".$_SESSION['c_limite_credito'].",
	".$_SESSION['c_vendedor'].",
	".$_SESSION['id_contribuyente'].",
	".$_SESSION['c_correo'].",
	".$_SESSION['c_exclusiv_cont'].",
	'".$timeStamp."',
	".$_SESSION['c_apart'].",
	".$_SESSION['c_activo'].",
	".$_SESSION['suc_id'].",
	".$_SESSION['c_ref2'].",1,1,
	".$_SESSION['c_direccion'].",
	".$_SESSION['c_club'].",
	".$_SESSION['usuario_id'].",
	".$_SESSION['c_precio'].",
	".$_SESSION['c_desc'].")
ON DUPLICATE KEY UPDATE
    c_descripcion	=VALUES(c_descripcion),
    c_nom_comercial	=VALUES(c_descripcion),
	c_direccion 	=VALUES(c_direccion),
    pa_id			=VALUES(pa_id),
    ciu_id			=VALUES(ciu_id),
    c_idfiscal		=VALUES(c_idfiscal),
    c_contacto		=VALUES(c_contacto),
    c_telefono		=VALUES(c_telefono),
    c_credito		=VALUES(c_credito),
    c_dias_credito	=VALUES(c_dias_credito),
    c_limite_credito=VALUES(c_limite_credito),
    c_vendedor		=VALUES(c_vendedor),
    id_contribuyente=VALUES(id_contribuyente),
    c_correo		=VALUES(c_correo),
    c_exclusiv_cont	=VALUES(c_exclusiv_cont),
    c_apart			=VALUES(c_apart),
    c_club			=VALUES(c_club),
    c_activo		=VALUES(c_activo),
    c_ref2			=VALUES(c_ref2),
    c_direccion		=VALUES(c_direccion),
    c_desc			=VALUES(c_desc),
    c_modificacion	='".$timeStamp."',
    c_u_id_mod		=".$_SESSION['usuario_id'].",
    usermodify      = '1',
    useroption      = '2';";
if($connection->query($SQL_Insert) == TRUE){
	$msg = array("success"=> "true","detalle"=> "OK");
}
else
{
	$msg = array("success"=> "false","detalle"=> $connection);
	echo "<br>Error: ".$SQL_Insert."<br>" . $conn->error;
}

print_r( json_encode($msg));

@mysqli_close($connection);
?>
