<?php
/*
JOHNN GUTIERREZ, 
FECHA DE CREACION   2021-02-23
ULTIMA MODIFICASION 2021-02-23
*/
include '../conn_obj/utiles_pdo_v2.php';
//include '../conn_obj/repo_func.php';
date_default_timezone_set("America/Panama");
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
#error_reporting(E_ALL);
session_start();
if ($_POST) {
    $_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['cte_id'] = $_POST['cte_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'];
    $_SESSION['replac_blanks'] = $_POST['replac_blanks'];
    $_SESSION['include_ctes'] = $_POST['include_ctes'];

    $cDb_Nam = $_SESSION['datab_orig'];
    $cDb_NamD = $_SESSION['datab_dest'];
    $cValidOp = $_SESSION['validat_op'];
    $cSuc_id = $_SESSION['suc_id'];
    $cCte_id = $_SESSION['cte_id'];
    $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'];
    $cIncCte = $_SESSION['include_ctes'];

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

    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 'CTE_ID    : ' . $cCte_id . 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 '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  ';
        echo chr(13);
    }

    $cSql_Str = "UPDATE operacion_cxc SET 
                    op_estado ='Pagada', 
                    op_canc   ='false' 
                WHERE ( (op_debito+op_credito) >= op_abono) 
                AND   (c_id = '$cCte_id'); ";
   // $oCnn->query($cSql_Str);

    $cSql_Str = "UPDATE operacion_cxc SET 
                    op_estado ='Pendiente', 
                    op_canc   ='true' 
                WHERE (op_debito < op_abono) 
                AND   (c_id = '$cCte_id'); ";
    //$oCnn->query($cSql_Str);

    $str_result = 'null';
    if ($cValidOp == '1') {
        $cSql = "SELECT c_id FROM `$cDb_Nam`.`clientes` WHERE c_id='$cCte_id' LIMIT 1";
        $oQry_Cte = $oCnn->query($cSql)->fetchArray();
        $records = $oCnn->numRows();
        if (($oQry_Cte['c_id'] != '') && ($oQry_Cte['c_id'] != 'null') && ($oQry_Cte['c_id'] != 'NULL' > 0)) {
            $str_result = $oCnn->Doit_Sql_cxc_v2($oCnn, $cDb_Nam, $cDb_NamD, $cValidOp, $cSuc_id, $cCte_id, $cDropTb, $cCreaTb, $cDelReg, $cDupReg, $SaveLog, $Repl_Bl, $cIncCte);
        } else {
            $str_result = 'null';
        }
    } else {
        $str_result = $oCnn->Doit_Sql_cxc_v2($oCnn, $cDb_Nam, $cDb_NamD, $cValidOp, $cSuc_id, $cCte_id, $cDropTb, $cCreaTb, $cDelReg, $cDupReg, $SaveLog, $Repl_Bl, $cIncCte);
    }

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

    echo $str_result;

    mysqli_close($oCnn);
}
?>
