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

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

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

$_SESSION['database'] = $_POST['database'];
$_SESSION['tipoc'] = $_POST['tipoc'];
$_SESSION['valor'] = $_POST['valor'];
$_SESSION['guardar'] = $_POST['guardar'];
$_SESSION['SaveLog'] = $_POST['SaveLog'];

$cDbNam = $_SESSION['database'];
$cTipoC = $_SESSION['tipoc'];
$cValue = TRIM($_SESSION['valor']);
$cGuard = $_SESSION['guardar'];
$SaveLog = $_SESSION['SaveLog'];

if ($SaveLog == 1) {
    echo '++++++++++++++++++++++++++++++++++++++++++++++insert_json_op_gen.php++++++++++++++++++++++++++++++++++++++++++'.chr(13);
    echo 'database : ' . $cDbNam.chr(13);
    echo 'Tipo Opt : ' . $cTipoC.chr(13);
    echo 'Valor Sec: ' . $cValue.chr(13);
    echo 'Guardar  : ' . $cGuard.chr(13);
    echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'.chr(13);
}
if (($cValue == '0') || ($cValue == '') || ($cValue == 'null') || ($cValue == 'NULL')) {
    $cValue = corre_mante($connection, $cDbNam, $cTipoC);
}
$iCorre = intval($cValue);
$iEncontro = exists_value($connection, $cDbNam, $cTipoC, $iCorre,$SaveLog);
if ($SaveLog == 1){
    echo 'secuencia2 : '.$iCorre .chr(13);
    echo 'encontro   : '. $iEncontro.chr(13);
}
$intentos=0;
do {
    $intentos=$intentos+1;
        $iCorre = $iCorre + 1;
        $iEncontro = exists_value($connection, $cDbNam, $cTipoC, $iCorre,$SaveLog);
        if ($SaveLog == 1)
            echo 'encontro: '. $iEncontro .chr(13);
   
    if ($intentos>1000)
    break;
     
} while ($iEncontro == 1);
$cValue = strval($iCorre);

if ($cGuard == '1') {
    $cmd = "UPDATE $cDbNam.clientes_corre SET correlativo=$cValue WHERE (tipo_corre=$cTipoC) ";
    $bSaved = update_Sql($connection, $cmd);
};
$bSaveCorr = ($cGuard == '1' ? $bSaved : 'null');
$cFre_Fijo = ($cTipoC == '1' ? '21' : '22');
if ($cTipoC == '1') {
    $cCod_Cte = $cFre_Fijo . str_pad($cValue, 8, "0", STR_PAD_LEFT);
} else {
    $cCod_Cte = $cFre_Fijo . str_pad($cValue, 8, "0", STR_PAD_LEFT);
}

$msg = array("value" => $cCod_Cte,"save"=>$bSaveCorr );

print_r(json_encode($msg));

@mysqli_close($connection);
exit();

function corre_mante($pconn, $pdb, $pTipoCorre)
{
    $iRecount = 0;
    $Sql = "SELECT  IFNULL(`correlativo`,0)+1 AS cValue  FROM $pdb.`clientes_corre` WHERE (`tipo_corre`=$pTipoCorre) LIMIT 1";
    $data = getRow($pconn, $Sql, $iRecount, false);
    $cCodigo = trim($data['cValue']);
    if ($pTipoCorre == '1') {
        $cResult = $cCodigo;
    } else {
        $cResult = $cCodigo;
    }
    return intval($cResult);
    $data->close();
};

function exists_value($pconn, $pdb, $pTipoCorre, $pValue,$SaveLog)
{
    if ($pTipoCorre == '1') {
        $cCodCte = '21' . str_pad($pValue, 8, "0", STR_PAD_LEFT);
    } else {
        $cCodCte = '22' . str_pad($pValue, 8, "0", STR_PAD_LEFT);
    }
    $cmd = "SELECT c_id FROM $pdb.`clientes` WHERE (`c_id`='$cCodCte') LIMIT 1";
    if ($SaveLog == 1){
        echo 'valor : '. $pValue .chr(13);
        echo 'codigo: '. $cCodCte .chr(13);
        echo 'sql   : '. $cmd .chr(13);
    }
    $iRecount2=0;
    $data = getRow($pconn, $cmd, $iRecount2, false);
    if (empty($data['c_id'])) 
        return 0;
    else {

        return 1;
    }
};
