<?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['op__id'] = $_POST['op__id'];
$_SESSION['op__tp'] = $_POST['op__tp'];
$_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'];


if (!empty($_POST['po_valid_exist']))
    $_SESSION['po_valid_exist'] = $_POST['po_valid_exist'];
else
    $_SESSION['po_valid_exist'] = 0;

if (!empty($_POST['po_valid_activ']))
    $_SESSION['po_valid_activ'] = $_POST['po_valid_activ'];
else
    $_SESSION['po_valid_activ'] = 0;


if (!empty($_POST['po_valid_price']))
    $_SESSION['po_valid_price'] = $_POST['po_valid_price'];
else
    $_SESSION['po_valid_price'] = 0;

$_SESSION['po_valid_exist'] = 0;

$cDb_Nam = $_SESSION['datab_orig'];
$cDb_NamD = $_SESSION['datab_dest'];
$cValidOp = $_SESSION['validat_op'];
$cSuc_id = $_SESSION['suc_id'];
$cOp__id = $_SESSION['op__id'];
$cOp__tp = $_SESSION['op__tp'];
$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");

$SaveLog =0;

if ($SaveLog == 1) {
    echo "" . chr(13);
    echo "++++++++++++++++++++++++++++++++++++++++++++++apt_get_files_fullv3.php++++++++++++++++++++++++++++++++++++++++++" . chr(13);
    echo "DB ORIGEN : $cDb_Nam" . chr(13);
    echo "DB DESTIN : $cDb_NamD" . chr(13);
    echo "VALIDA OP : $cValidOp" . chr(13);
    echo "SUCURSAL  : $cSuc_id" . chr(13);
    echo "OP_ID     : $cOp__id" . chr(13);
    echo "TIPO      : $cOp__tp" . chr(13);
    echo "BORRA TAB : $cDropTb" . chr(13);
    echo "CREAR TAB : $cCreaTb" . chr(13);
    echo "BORRA DATA: $cDelReg" . chr(13);
    echo "ONDUPLICAT: $cDupReg" . chr(13);
    echo "SAVE LOGIN: $SaveLog" . chr(13);
    echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" . chr(13);
    echo "";
}

$str_result = 'null';
if ($cValidOp == '1') {
    $cSql = 'SELECT op_id FROM ' . $cDb_Nam . '.operacion WHERE op_id="' . $cOp__id . '" AND op_tipo="' . $cOp__tp . '" LIMIT 1;';
    if ($SaveLog == 1) {
        echo $cSql . chr(13);
    }
    $result = getRow($connection, $cSql, $records, false);
    if (($result['op_id'] != '') && ($result['op_id'] != 'null') && ($result['op_id'] != 'NULL' > 0)) {
        $str_result = Doit_Sql($connection, $cDb_Nam, $cDb_NamD, $cValidOp, $cSuc_id, $cOp__id, $cOp__tp, $cDropTb, $cCreaTb, $cDelReg, $cDupReg, $SaveLog, $Repl_Bl, $PoValAct, $PoValpri, $PoValExi);
    } else {
        $str_result = 'null';
    }
} else {
    $str_result = Doit_Sql($connection, $cDb_Nam, $cDb_NamD, $cValidOp, $cSuc_id, $cOp__id, $cOp__tp, $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, $cOp__id, $pOp__tp, $cDropTb, $cCreaTb, $cDelReg, $cDupReg, $SaveLog, $Repl_Bl)
{
    if (($pOp__tp == '') or ($pOp__tp == 'NULL') or ($pOp__tp == 'null') or ($pOp__tp == '0')) {
        $pOp__tp = '2';
    }

    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 . '`.`operacion`;' . chr(10);
    }
     if ($cCreaTb == '1') {
        $cSql = "SHOW CREATE TABLE $cDb_Nam.operacion";
        $data = getRow($connection, $cSql, $records, false);

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

    if ($cDelReg == '1') {
        $str_sql = $str_sql . "DELETE FROM $cDb_NamD.`operacion` WHERE (op_id='$cOp__id') AND (op_tipo='$pOp__tp');" . chr(10);
    }
    $cSql = "SELECT $cDb_Nam.`Get_AllFields_ExeptV5_Not_Null`('operacion','op_autoin','') AS str_fields";
    $result = getRow($connection, $cSql, $records, false);
    $cList_Flds = $result['str_fields'];

    $cSql = "SELECT $cList_Flds FROM $cDb_Nam.operacion WHERE (op_id='$cOp__id') AND (op_tipo='$pOp__tp');";
    $recount = 0;
    $TablaOp = getRowAll($connection, $cSql, $recount, false);
    $str_sql = $str_sql . $sValue2 . chr(10);
    if ($recount > 0) {
        $sValue2 = jsonmysql_insert_result_str_v2($TablaOp, 'operacion', $connection, $cDb_Nam, $cDb_NamD, 'op_autoin', 1, 1, 1000, 0);
        $str_sql = $str_sql . $sValue2 . chr(10);
        unset($TablaOp);
    }
    /*----------------------------------------------------------------------------------------------------------------------------*/
    //$str_sql = $str_sql . $cSql;
    /*****************************************************************************************************************************/
    if ($cDropTb == '1') {
        $str_sql = $str_sql . "DROP TABLE IF EXISTS `$cDb_NamD`.`producto_op`;" . chr(10);
    }

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

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

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

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

    $cSql = "SELECT $cDb_Nam.Get_AllFields_ExeptV5_Alias_Not_Null('producto_op','po_id','','po') AS str_fields";
    $data = getRow($connection, $cSql, $records, false);
    $sCamposX = $data['str_fields'];

    $cSql = "
    SELECT 
        $sCamposX, 
        po.po_relac_unic AS po_relac_unic2  
    FROM `$cDb_Nam`.`producto_op` po ";

    $cSql = $cSql .  "WHERE (po.po_op_id='$cOp__id') ";

    //echo '/*'.$cSql.'*/';
    if ($SaveLog == 1) {
        // echo $cSql;
    }

    // ECHO "/* $cSql */";

    $recount = 0;
    $TablaPo = getRowAll($connection, $cSql, $recount, false);
    $str_sql = $str_sql . '' . chr(10);
    if ($recount > 0) {
        $sValue2 = jsonmysql_insert_result_str_v2($TablaPo, 'producto_op', $connection, $cDb_Nam, $cDb_NamD, 'po_id', 1, 1, 1000, 0);
        $str_sql = $str_sql . $sValue2 . chr(10);
        unset($TablaPo);
    }
    /*----------------------------------------------------------------------------------------------------------------------------*/

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

    if ($cCreaTb == '1') {
        $cSql = "SHOW CREATE TABLE $cDb_Nam.operacion_det_pago";
        $data = getRow($connection, $cSql, $records, false);

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

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

    $cSql = "SELECT $cDb_Nam.Get_AllFields_ExeptV5_Not_Null('operacion_det_pago','id','') AS str_fields";
    $result = getRow($connection, $cSql, $records, false);
    $sCamposX = $result['str_fields'];
    $cSql = "SELECT $sCamposX FROM $cDb_Nam.operacion_det_pago WHERE (op_id='$cOp__id')";

    $recount = 0;
    $TablaDp = getRowAll($connection, $cSql, $recount, false);
    $str_sql = $str_sql . '' . chr(10);
    if ($recount > 0) {
        $sValue2 = jsonmysql_insert_result_str_v2($TablaDp, 'operacion_det_pago', $connection, $cDb_Nam, $cDb_NamD, 'id', 1, 1, 1000, 0);
        $str_sql = $str_sql . $sValue2 . chr(10);
        unset($TablaDp);
    }
    /*----------------------------------------------------------------------------------------------------------------------------*/

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

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

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

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

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

    $cSql = "SELECT $cDb_Nam.Get_AllFields_ExeptV5_Not_Null('operacion_cxc','id_reg','') AS str_fields";
    $result = getRow($connection, $cSql, $records, false);
    $sCamposX = $result['str_fields'];

    $recount = 0;
    $cSql = "SELECT $sCamposX FROM $cDb_Nam.operacion_cxc WHERE (op_rel='$cOp__id')";
    $TablaCX = getRowAll($connection, $cSql, $recount, false);
    $str_sql = $str_sql . '' . chr(10);
    if ($recount > 0) {
        $sValue2 = jsonmysql_insert_result_str_v2($TablaCX, 'operacion_cxc', $connection, $cDb_Nam, $cDb_NamD, 'id_reg', 1, 1, 1000, 0);
        $str_sql = $str_sql . $sValue2 . chr(10);
        unset($TablaCX);
    }
    /*----------------------------------------------------------------------------------------------------------------------------*/

    /*****************************************************************************************************************************/
    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 = '';
    }
    if ($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);
    }

    $cSql = "SELECT $cDb_Nam.Get_AllFields_ExeptV5_Alias_Not_Null('clientes','id_autoinc','','ct') AS str_fields";
    $result = getRow($connection, $cSql, $records, false);
    $sCamposX = $result['str_fields'];

    $cSql = "SELECT $sCamposX FROM $cDb_Nam.clientes ct INNER JOIN $cDb_Nam.operacion op ON (op.op_id_cliente = ct.c_id) WHERE (op.op_id='$cOp__id')";
    $recount = 0;
    $TablaCT = getRowAll($connection, $cSql, $recount, false);
    $str_sql = $str_sql . '' . chr(10);
    if ($recount > 0) {
        $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);
    }
    /*----------------------------------------------------------------------------------------------------------------------------*/

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

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

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

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

    if ($cDelReg == '1') {
    }

    $cSql = "SELECT $cDb_Nam.Get_AllFields_ExeptV5_Alias_Not_Null('categorias','cat_cod','','ca') AS str_fields";
    $result = getRow($connection, $cSql, $records, false);
    $sCamposX = $result['str_fields'];

    $cSql = "
    SELECT $sCamposX 
    FROM $cDb_Nam.categorias ca
    INNER JOIN $cDb_Nam.clientes  ct ON (ca.cat_id = ct.cat_id)  
    INNER JOIN $cDb_Nam.operacion op ON (ct.c_id   = op.op_id_cliente)
    WHERE (op.op_id='$cOp__id') ";
    $recount = 0;
    $TablaCA = getRowAll($connection, $cSql, $recount, false);
    $str_sql = $str_sql . '' . chr(10);
    if ($recount > 0) {
        $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);
    }
    /*----------------------------------------------------------------------------------------------------------------------------*/

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

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

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

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

    if ($cDelReg == '1') {
    }

    $cSql = "SELECT $cDb_Nam.Get_AllFields_ExeptV5_Not_Null('puntos_conf','p_autoin','') AS str_fields";
    $result = getRow($connection, $cSql, $records, false);
    $sCamposX = $result['str_fields'];

    $cSql = "SELECT * FROM $cDb_Nam.puntos_conf WHERE (1=1) ";
    $recount = 0;
    $TablaCF = getRowAll($connection, $cSql, $recount, false);
    $str_sql = $str_sql . '' . chr(10);
    if ($recount > 0) {
        $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);
    }
    /*----------------------------------------------------------------------------------------------------------------------------*/

    if ($SaveLog == 1) {
        echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" . chr(13);
        echo "SQL : $str_sql" . chr(13);
        echo "++++++++++++++++++++++++++++++++++++++++++++++insert_json_op_gen.php++++++++++++++++++++++++++++++++++++++++++" . chr(13);
        echo "" . chr(13);
    }

    return $str_sql;
}
/*
JOHNN GUTIERREZ
FECHA DE CREACION    : 2023-07-13
ULTIMA MODIFICASION  : 2023-07-13
*/