<?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';");
}

session_start();

$_SESSION['datab_orig'] = $_POST['datab_orig'];
$_SESSION['datab_dest'] = $_POST['datab_dest'];
$_SESSION['suc_id'] = $_POST['suc_id'];
$_SESSION['op__id'] = $_POST['op__id'];
$_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'];

$cDb_Nam = $_SESSION['datab_orig'];
$cDb_NamD = $_SESSION['datab_dest'];
$cSuc_id = $_SESSION['suc_id'];
$cOp__id = $_SESSION['op__id'];
$cDropTb = $_SESSION['drop__tables'];
$cCreaTb = $_SESSION['creat_tables'];
$cDelReg = $_SESSION['delet_values'];
$cDupReg = $_SESSION['activ_ondupl'];
$SaveLog = $_SESSION['save_log_fle'];

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';
}

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

if ($SaveLog == 1) {
    logs2('', false);
    logs2('++++++++++++++++++++++++++++++++++++++++++++++insert_json_op_gen.php++++++++++++++++++++++++++++++++++++++++++');
    logs2('database : ' . $cDb_Nam);
    logs2('Sucursal : ' . $cSuc_id);
    logs2('Tbl_OpId : ' . $cOp__id);
    logs2('Cxc_OpId : ' . $cOp_cxc);
    logs2('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
    logs2('', false);
}

$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 = '';
}
$str_sql = $str_sql . $sValue1 . chr(10);

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

$cSql = 'SELECT * FROM ' . $cDb_Nam . '.operacion WHERE op_id="' . $cOp__id . '";';
$TablaOp = getRowAll($connection, $cSql, $recount, false);
// print_r( $TablaOp);

$sValue2 = jsonmysql_insert_result_str($TablaOp, $recount, 'operacion', $connection, $cDb_Nam, $cDb_NamD, $SaveLog, 'op_autoin', $cDupReg);
$str_sql = $str_sql . $sValue2 . chr(10);
unset($TablaOp);
/**
 * *********************************************************************************
 */

/**
 * *********************************************************************************
 */
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 = '';
}
$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 * FROM ' . $cDb_Nam . '.producto_op WHERE po_op_id="' . $cOp__id . '"';
$TablaPo = getRowAll($connection, $cSql, $recount, false);
// print_r( $TablaOp);

$sValue2 = jsonmysql_insert_result_str($TablaPo, $recount, 'producto_op', $connection, $cDb_Nam, $cDb_NamD, $SaveLog, 'po_id', $cDupReg);
$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 = '';
}
$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 * FROM ' . $cDb_Nam . '.operacion_det_pago WHERE op_id="' . $cOp__id . '"';
$TablaDp = getRowAll($connection, $cSql, $recount, false);
// print_r( $TablaOp);

$sValue2 = jsonmysql_insert_result_str($TablaDp, $recount, 'operacion_det_pago', $connection, $cDb_Nam, $cDb_NamD, $SaveLog, 'id', $cDupReg);
$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 = '';
}
$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 * FROM ' . $cDb_Nam . '.operacion_cxc WHERE op_rel="' . $cOp__id . '"';
$TablaCX = getRowAll($connection, $cSql, $recount, false);
// print_r( $TablaOp);

$sValue2 = jsonmysql_insert_result_str($TablaCX, $recount, 'operacion_cxc', $connection, $cDb_Nam, $cDb_NamD, $SaveLog, 'id_reg', $cDupReg);
$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 = '';
}
$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 ct.* 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 . '"';
$TablaCT = getRowAll($connection, $cSql, $recount, false);
// print_r( $TablaCT);

$sValue2 = jsonmysql_insert_result_str($TablaCT, $recount, 'clientes', $connection, $cDb_Nam, $cDb_NamD, $SaveLog, 'id_autoinc', $cDupReg);
$str_sql = $str_sql . $sValue2 . chr(10);
unset($TablaCT);
/**
 * *********************************************************************************
 */

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

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

@mysqli_close($connection);
?>