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

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

if (!$connection = connectDb()) {
    die("err db");
} else {
    $connection->query("SET NAMES 'utf8';");
}
session_start();

$_SESSION["datab_orig"] = $_POST["datab_orig"];
$_SESSION["datab_dest"] = $_POST["datab_dest"];
$_SESSION["cod_suc"]    = $_POST["cod_suc"];
$_SESSION["cod_est"]    = $_POST["cod_est"];
$_SESSION["dele_tables"] = $_POST["dele_tables"];
$_SESSION["swsavelog"] = $_POST["swsavelog"];

$cDb_Ori = $_SESSION["datab_orig"];
$cDb_Des = $_SESSION["datab_dest"];
$cSuc_id = $_SESSION["cod_suc"];
$cE__id = $_SESSION["cod_est"];
$cDeleTb = $_SESSION["dele_tables"];
$SaveLog = $_SESSION["swsavelog"];
$Debuger =0;
$cTableN = "estaciones";

$Sql_ln="DROP TABLE IF EXISTS `".$cDb_Ori."`.`tmp_est_corre`;";
if (!mysqli_query($connection, $Sql_ln)){
    echo mysqli_error($connection).chr(13);
}

$Sql_ln="DROP TEMPORARY TABLE IF EXISTS `".$cDb_Ori."`.`tmp_est_corre`;";
if (!mysqli_query($connection, $Sql_ln)){
    echo mysqli_error($connection).chr(13);
}
if ($Debuger==1)
    print_r($Sql_ln);

$Sql_ln="
CREATE TEMPORARY TABLE `".$cDb_Ori."`.`tmp_est_corre` (
	`e_codigo` INT(11) NOT NULL DEFAULT '0',
	`suc_id` INT(11) NULL DEFAULT NULL,
	`e_id` INT(11) NULL DEFAULT '1',
	`e_descripcion` VARCHAR(100) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
	`e_activo` INT(1) NOT NULL DEFAULT '0',
    `e_corre_r` BIGINT(20) NOT NULL DEFAULT '0',
	`e_corre_rper` BIGINT(20) NOT NULL DEFAULT '0',
	`e_corre_rclie` BIGINT(20) NOT NULL DEFAULT '0',
	`e_corre_rprom` BIGINT(20) NOT NULL DEFAULT '0',
	`e_corre_rtpagos` BIGINT(20) NOT NULL DEFAULT '0',
	`e_corre_rtdesc` BIGINT(20) NOT NULL DEFAULT '0',
	`e_corre_rimagen` BIGINT(20) NOT NULL DEFAULT '0',
	`e_corre_rwindpos` BIGINT(20) NOT NULL DEFAULT '0',
	`e_corre_rconf_est` BIGINT(20) NOT NULL DEFAULT '0',
	`e_corre_rtipo_sales` BIGINT(20) NOT NULL DEFAULT '0',
	`e_corre_rpprom` BIGINT(20) NOT NULL DEFAULT '0',
	`e_corre_rdep` BIGINT(20) NOT NULL DEFAULT '0',
    `e_corre_rtransp` BIGINT(20) NOT NULL DEFAULT '0',
	PRIMARY KEY (`e_codigo`) USING BTREE,
	UNIQUE INDEX `suc_id_e_id` (`suc_id`, `e_id`) USING BTREE,
	INDEX `suc_id` (`suc_id`) USING BTREE,
	INDEX `e_id` (`e_id`) USING BTREE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=0;";
if (!mysqli_query($connection, $Sql_ln)){
    echo mysqli_error($connection).chr(13);
}
if ($Debuger==1)
    print_r($Sql_ln);

$Sql_ln='
INSERT INTO `'.$cDb_Ori.'`.`tmp_est_corre` (
    suc_id,    
    e_id,
    e_descripcion,  
    e_activo,
    e_corre_r,
	e_corre_rper,
	e_corre_rclie,
	e_corre_rprom,
	e_corre_rtpagos,
	e_corre_rtdesc,
	e_corre_rimagen,
	e_corre_rwindpos,
	e_corre_rconf_est,
	e_corre_rtipo_sales,
	e_corre_rpprom,
	e_corre_rdep,
    e_corre_rtransp)
SELECT 
    suc_id,    
    e_id,
    UPPER(TRIM(IFNULL(e_descripcion,"")))  AS e_descripcion,  
    IFNULL(e_activo,0) AS e_activo,
    IF(IFNULL(e_corre_e         ,0)	<=1,IFNULL(e_corre_e,0)				,IFNULL(e_corre_e,0)-1			)	as e_corre_r,
	IF(IFNULL(e_corre_eper      ,0)	<=1,IFNULL(e_corre_eper,0)			,IFNULL(e_corre_eper,0)-1		)	as e_corre_rper,
	IF(IFNULL(e_corre_eclie     ,0)	<=1,IFNULL(e_corre_eclie,0)		    ,IFNULL(e_corre_eclie,0)-1		) 	as e_corre_rclie,
	IF(IFNULL(e_corre_eprom     ,0)	<=1,IFNULL(e_corre_eprom,0)		    ,IFNULL(e_corre_eprom,0)-1 	    ) 	as e_corre_rprom,
	IF(IFNULL(e_corre_etpago    ,0)	<=1,IFNULL(e_corre_etpago,0)		,IFNULL(e_corre_etpago,0)-1	    ) 	as e_corre_rtpagos,
	IF(IFNULL(e_corre_etdesc    ,0)	<=1,IFNULL(e_corre_etdesc,0)		,IFNULL(e_corre_etdesc,0)-1	    ) 	as e_corre_rtdesc,
	IF(IFNULL(e_corre_eimagen   ,0)	<=1,IFNULL(e_corre_eimagen,0)		,IFNULL(e_corre_eimagen,0)-1	) 	as e_corre_rimagen,
	IF(IFNULL(e_corre_ewindpos  ,0)	<=1,IFNULL(e_corre_ewindpos,0)	    ,IFNULL(e_corre_ewindpos,0)-1	) 	as e_corre_rwindpos,
	IF(IFNULL(e_corre_econf_est ,0)	<=1,IFNULL(e_corre_econf_est,0)	    ,IFNULL(e_corre_econf_est,0)-1  ) 	as e_corre_rconf_est,
	IF(IFNULL(e_corre_etipo_sales,0)<=1,IFNULL(e_corre_etipo_sales,0)   ,IFNULL(e_corre_etipo_sales,0)  ) 	as e_corre_rtipo_sales,
	IF(IFNULL(e_corre_epprom    ,0)	<=1,IFNULL(e_corre_epprom,0)		,IFNULL(e_corre_epprom,0)-1	    ) 	AS e_corre_rpprom,
	IF(IFNULL(e_corre_edep	    ,0)	<=1,IFNULL(e_corre_edep	,0)		    ,IFNULL(e_corre_edep	,0)-1	) 	as e_corre_rdep,
	IF(IFNULL(e_corre_etransp   ,0)	<=1,IFNULL(e_corre_etransp	,0)		,IFNULL(e_corre_etransp	,0)-1	) 	as e_corre_rtransp
  FROM `'.$cDb_Ori.'`.`estaciones` WHERE (suc_id="'.$cSuc_id.'") AND (e_id="'. $cE__id . '")
  ON DUPLICATE KEY UPDATE
    suc_id          =VALUES(suc_id),    
    e_id            =VALUES(e_id),
    e_descripcion   =VALUES(e_descripcion),  
    e_activo        =VALUES(e_activo),
    e_corre_r       =VALUES(e_corre_r),
    e_corre_rper    =VALUES(e_corre_rper),
    e_corre_rclie   =VALUES(e_corre_rclie),
    e_corre_rprom   =VALUES(e_corre_rprom),
    e_corre_rtpagos =VALUES(e_corre_rtpagos),
    e_corre_rtdesc  =VALUES(e_corre_rtdesc),
    e_corre_rimagen =VALUES(e_corre_rimagen),
    e_corre_rwindpos=VALUES(e_corre_rwindpos),
    e_corre_rconf_est=VALUES(e_corre_rconf_est),
    e_corre_rtipo_sales=VALUES(e_corre_rtipo_sales),
    e_corre_rpprom  =VALUES(e_corre_rpprom),
    e_corre_rdep    =VALUES(e_corre_rdep),
    e_corre_rtransp =VALUES(e_corre_rtransp); ';
if (!mysqli_query($connection, $Sql_ln)){
    echo mysqli_error($connection).chr(13);
}
if ($Debuger==1)
    print_r($Sql_ln);
  
if (($cDeleTb == '') or ($cDeleTb == 'NULL') or ($cDeleTb == 'null')) {
    $cDeleTb = '0';
}

if (($SaveLog == '') or ($SaveLog == 'NULL') or ($SaveLog == 'null')) {
    $SaveLog = '0';
}

$hoynow = getdate();
$timeStamp = $hoynow["year"] . "-" . $hoynow["mon"] . "-" . $hoynow["mday"];
$timeStamp = date("Y-m-d H:i:s");

if ($SaveLog == 1) {
    logs2('', false);
    logs2('+++++++++++++++++++++++++++++++++++++++++++++++++gen_get_windpos.php++++++++++++++++++++++++++++++++++++++++++');
    logs2('Origen  : ' . $cDb_Ori);
    logs2('Destino : ' . $cDb_Des);
    logs2('Sucursal: ' . $cSuc_id);
    logs2('Estacion: ' . $cod_est);
    logs2('BorrData: ' . $cDeleTb);
    logs2('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
    logs2('', false);
}

$str_sql = '';

if ($cDeleTb == '1')
    $str_sql = $str_sql . 'DELETE FROM '.$cDb_Des.'.`'. $cTableN . '` WHERE  (suc_id="' . $cSuc_id . '") AND (e_id="' . $cE__id . '");' . chr(10);

$str_sql = $str_sql . $sValue1;
$recount = 0;
$Sql_ln = "SELECT * FROM `".$cDb_Ori."`.`tmp_est_corre`";

$TablaWp = getRowAll($connection, $Sql_ln, $recount, false);
if ($Debuger==1)
    print_r($TablaWp);

$Excl_Field = 'e_codigo';
$sValue2 = jsonmysql_insert_result_str($TablaWp, $recount,  $cTableN, $connection, $cDb_Ori, $cDb_Des, $SaveLog, $Excl_Field,1);
                                      //($tabla, $reg_p, $nombre, $connection1, $database, $database_dest, $pSaveLog, $Excl_Field, $On_Dlupl, $Replac_Blanks)
$str_sql = $str_sql . $sValue2;
unset($TablaWp);

if ($SaveLog == 1) {
    logs2('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
    logs2('SQL : ' . $str_sql);
    logs2('++++++++++++++++++++++++++++++++++++++++++++++insert_json_op_gen.php++++++++++++++++++++++++++++++++++++++++++');
    logs2('', false);
}

$Sql_ln="DROP TEMPORARY TABLE IF EXISTS `".$cDb_Ori."`.`tmp_est_corre`;";
mysqli_query($connection, $Sql_ln);
if ($Debuger==1)
print_r($Sql_ln);

header('content-type: application/json; charset=UTF-8');
echo $str_sql;

mysqli_close($connection);
//*creado por johnn gutierres 2021-05-31*/
?>
