<?php
include_once("utiles.php");
//include_once ("apt_funciones.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';");
}

$SaveLog = 0;
session_start();

$_SESSION['datab_orig'] = $_POST['datab_orig'];
$_SESSION['datab_dest'] = $_POST['datab_dest'];
$_SESSION['validat_op'] = $_POST['validat_op'];
$_SESSION['suc_id'] = $_POST['suc_id'];
$_SESSION['cod_cte'] = $_POST['cod_cte'];
$_SESSION['drop__tables'] = $_POST['drop_tables'];
$_SESSION['creat_tables'] = $_POST['create_tables'];
$_SESSION['delet_values'] = $_POST['delete_self_data'];
$_SESSION['activ_ondupl'] = $_POST['activ_ondupl'];
$_SESSION['save_log_fle'] = $_POST['swsavelog'];
$_SESSION['replac_blanks'] = $_POST['replac_blanks'];

$cDb_Nam = $_SESSION['datab_orig'];
$cDb_NamD = $_SESSION['datab_dest'];
$cValidOp = $_SESSION['validat_op'];
$cSuc_id = $_SESSION['suc_id'];
$cCod_Ct = $_SESSION['cod_cte'];
$cDropTb = $_SESSION['drop__tables'];
$cCreaTb = $_SESSION['creat_tables'];
$cDelReg = $_SESSION['delet_values'];
$cDupReg = $_SESSION['activ_ondupl'];
$SaveLog = $_SESSION['save_log_fle'];
$Repl_Bl = $_SESSION['replac_blanks'];

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

if ($SaveLog == 1) {
    logs2('', false);
    logs2('++++++++++++++++++++++++++++++++++++++++++++++apt_get_files_fullv3.php++++++++++++++++++++++++++++++++++++++++++');
    logs2('DB ORIGEN : ' . $cDb_Nam);
    logs2('DB DESTIN : ' . $cDb_NamD);
    logs2('VALIDA OP : ' . $cValidOp);
    logs2('SUCURSAL  : ' . $cSuc_id);
    logs2('COD CTE   : ' . $cCod_Ct);
    logs2('BORRA TAB : ' . $cDropTb);
    logs2('CREAR TAB : ' . $cCreaTb);
    logs2('BORRA DATA: ' . $cDelReg);
    logs2('ONDUPLICAT: ' . $cDupReg);
    logs2('SAVE LOGIN: ' . $SaveLog);
    logs2('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
    logs2('', false);
}

$str_result = Doit_Sql($connection, $cDb_Nam, $cDb_NamD, $cValidOp, $cSuc_id, $cCod_Ct, $cDropTb, $cCreaTb, $cDelReg, $cDupReg, $SaveLog, $Repl_Bl);

header('content-type: text/plain; charset=UTF-8');

echo $str_result;

@mysqli_close($connection);

function Doit_Sql($connection, $cDb_Nam, $cDb_NamD, $cValidOp, $cSuc_id, $cCod__Ct,$cDropTb, $cCreaTb, $cDelReg, $cDupReg, $SaveLog, $Repl_Bl)
{
    if (($cDropTb == '') or ($cDropTb == 'NULL') or ($cDropTb == 'null')) {
        $cDropTb = '0';
    }

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

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

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

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

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

    /*****************************************************************************************************************************/
    if ($cDropTb == '1') {
        $str_sql = $str_sql . 'DROP TABLE IF EXISTS `' . $cDb_NamD . '`.`clientes`;' . chr(10);
    }

    if ($cCreaTb == '1') {
        $cSql = 'SHOW CREATE TABLE ' . $cDb_Nam . '.clientes';

        $data = getRow($connection, $cSql, $records, false);

        if ($records = 0) {
            $sValue1 = '';
        } else {
            $sValue1 = $data['Create Table'];
            $sValue1 = str_replace('`clientes`', '`' . $cDb_NamD . '`.`clientes`', $sValue1);
            $sValue1 = $sValue1 . ';' . chr(10);
        }
    } else {
        $sValue1 = '';
    }
    $str_sql = $str_sql . $sValue1 . chr(10);

    if ($cDelReg == '1') {
        // $str_sql = $str_sql . 'DELETE FROM `' . $cDb_NamD . '`.`clientes` WHERE c_id="' . $cOp__id . '";' . chr(10);
    }

    $recount = 0;
    $cSql = 'SELECT * FROM `' . $cDb_Nam . '`.`clientes` WHERE (c_id="' . $cCod__Ct . '")';
    $TablaCT = getRowAll($connection, $cSql, $recount, false);
 
    $sValue2 = jsonmysql_insert_result_str_v2($TablaCT, 'clientes', $connection, $cDb_Nam, $cDb_NamD, 'id_autoinc', 1, 1, 1000, 0);
    //$sValue2 = jsonmysql_insert_result_strV2($TablaCT, $recount, 'clientes', $connection, $cDb_Nam, $cDb_NamD, $SaveLog, 'id_autoinc', $cDupReg, $Repl_Bl);
    $str_sql = $str_sql . $sValue2 . chr(10);
    unset($TablaCT);
    /*----------------------------------------------------------------------------------------------------------------------------*/

    /*****************************************************************************************************************************/
    $recount = 0;
    $cSql = '
    SELECT ca.* 
    FROM `' . $cDb_Nam . '`.`categorias` ca
    INNER JOIN `' . $cDb_Nam . '`.`clientes`  ct ON ca.cat_id = ct.cat_id  
    WHERE (ct.c_id="' . $cCod__Ct . '") ';
    $TablaCA = getRowAll($connection, $cSql, $recount, false);
    $str_sql = $str_sql . ''. chr(10);
 
    $sValue2 = jsonmysql_insert_result_str_v2($TablaCA, 'categorias', $connection, $cDb_Nam, $cDb_NamD, 'cat_cod', 1, 1, 1000, 0);
    $str_sql = $str_sql . $sValue2 . chr(10);
    unset($TablaCA);
    /*----------------------------------------------------------------------------------------------------------------------------*/

    /*****************************************************************************************************************************/
    $recount = 0;
    $cSql = '
    SELECT cf.* 
    FROM `' . $cDb_Nam . '`.`puntos_conf` cf 
    INNER JOIN ' . $cDb_Nam . '.`clientes`  ct ON (cf.cod_cat = ct.cat_id)
    WHERE (ct.c_id="' . $cCod__Ct . '") ';
    $TablaCF = getRowAll($connection, $cSql, $recount, false);
    $str_sql = $str_sql . ''. chr(10);
  
    $sValue2 = jsonmysql_insert_result_str_v2($TablaCF, 'puntos_conf', $connection, $cDb_Nam, $cDb_NamD, 'p_autoin', 1, 1, 1000, 0);
    $str_sql = $str_sql . $sValue2 . chr(10);
    unset($TablaCF);
    /*----------------------------------------------------------------------------------------------------------------------------*/

    /*****************************************************************************************************************************/
    $recount = 0;
    $cSql = 'SELECT * FROM `' . $cDb_Nam . '`.`puntos_clientes` WHERE (p_cod_cte="' . $cCod__Ct . '") ';
    $TablaPC = getRowAll($connection, $cSql, $recount, false);
    $str_sql = $str_sql .  ''. chr(10);
 
    $sValue2 = jsonmysql_insert_result_str_v2($TablaPC, 'puntos_clientes', $connection, $cDb_Nam, $cDb_NamD, 'p_autoin', 1, 1, 1000, 0);
    $str_sql = $str_sql . $sValue2 . chr(10);
    unset($TablaPC);
    /*----------------------------------------------------------------------------------------------------------------------------*/

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

    return $str_sql;
}
