<?php

error_reporting( E_ERROR  | E_PARSE );

session_start();

$_SESSION['database'] = $_POST['database'];
$_SESSION['suc_id'] = $_POST['suc_id'];
$_SESSION['est_id'] = $_POST['est_id'];
$_SESSION['id_pack'] = $_POST['id_pack'];

include 'conn.php';
include 'funciones.php';

date_default_timezone_set('America/Panama');

$hoynow = getdate();
$hora = str_pad($hoynow['hours'], 2, '0', STR_PAD_LEFT);
$minutos = str_pad($hoynow['minutes'], 2, '0', STR_PAD_LEFT);
$mes = str_pad($hoynow['mon'], 2, '0', STR_PAD_LEFT);
$dia = str_pad($hoynow['mday'], 2, '0', STR_PAD_LEFT);
$timeStamp = $hoynow['year'] . '-' . $mes . '-' . $dia . ' ' . $hora . ':' . $minutos . ':' . $hoynow['seconds'];

$SQLupdate = 'update ' . $_SESSION['database'] . ".estaciones set e_ult_con='" . $timeStamp . "',e_pack_conf_est=" . $_SESSION['id_pack'] . "  where suc_id='" . $_SESSION['suc_id'] . "'  and e_id='" . $_SESSION['est_id'] . "' limit 1 ";

if ($connection->query($SQLupdate) === true) {
    //  echo 'update op';
} else {
    echo 'Error: ' . $SQLupdate . '<br>' . $conn->error;
}

$montoesql1 = "select
suc_id ,
est_id ,
ifnull(aviso_depositar,0) as aviso_depositar,
ifnull(aviso_monto_limite,0) as aviso_monto_limite ,
ifnull(aviso_efectivo_cheque,0) as aviso_efectivo_cheque,
ifnull(pedir_tipo_servicio,0) as pedir_tipo_servicio,
ifnull(solicita_detalle_devoluc,0) as solicita_detalle_devoluc,
ifnull(cierre_remoto,0) as cierre_remoto,
ifnull(saldo_disp_pos,0) as saldo_disp_pos,
a.trig_unic,

ifnull(arquear_todas_fp,0) as arquear_todas_fp,
ifnull(solicitar_cte_fide,0) as solicitar_cte_fide,
ifnull(nc_despues_de_x_minutos,0) as nc_despues_de_x_minutos,
ifnull(forzar_fact_con_z,0) as forzar_fact_con_z,
ifnull(forzar_fact_con_x_dias,0) as forzar_fact_con_x_dias,

suc_codigo_fe,est_codigo_fe,activo_fe,usuario_fe,clave_fe,no_carg_global,no_desc_global,forza_desc_glob_en_prod,coordenad_fe,a.trig_unic,muestra_tipo_precio_btn_pos,company_code,api_key,ocp_apim_subscription_key,ws_log_fe,
url_fe,pac_code_fe,pac_name_fe,load_from_ini_fe,pac_mess_fe,forzar_impresion_fe,razon_social_fe,banis_usa_terminal,banis_ip_terminal,banis_cierre_z,yap_active,yap_url,yap_api_key,yap_pas_key,yap_dev_id,yap_dev_name,yap_dev_user,
yap_dev_group_id,yap_log,yap_time_sec,cupon_descuento_activo,cupon_descuento_fecha_ini,cupon_descuento_fecha_fin,cupon_descuento_mensaje,cupon_descuento_link_web,cupon_descuento_cada_x_dolares,plux_active,plux_url,plux_api_key,plux_usr_key,plux_usr_secret,plux_cli_id,plux_cli_name,plux_merchant_id,plux_terminal_id,plux_site_id,plux_log,app_pio_active


from


" . $_SESSION['database'] . "_tmp_pack.est_global_conf_pack b

left outer join " . $_SESSION['database'] . ".estaciones_global_conf a on a.trig_unic=b.trig_unic


                   where  id_pack='" . $_SESSION['id_pack'] . "'  ";

//echo $montoesql1 ;

$qur1 = $connection->query($montoesql1);

$i = 0;

while ($r1 = mysqli_fetch_assoc($qur1)) {

    $jsonTempData = array();
    $jsonTempData['campo1'] = utf8_encode($r1['suc_id']);
    $jsonTempData['campo2'] = utf8_encode($r1['est_id']);
    $jsonTempData['campo3'] = ($r1['aviso_depositar']);
    $jsonTempData['campo4'] = utf8_encode($r1['aviso_monto_limite']);
    $jsonTempData['campo5'] = utf8_encode($r1['aviso_efectivo_cheque']);
    $jsonTempData['campo6'] = utf8_encode($r1['pedir_tipo_servicio']);
    $jsonTempData['campo7'] = utf8_encode($r1['solicita_detalle_devoluc']);
    $jsonTempData['campo8'] = utf8_encode($r1['trig_unic']);
    $jsonTempData['campo9'] = utf8_encode($r1['cierre_remoto']);
    $jsonTempData['campo10'] = utf8_encode($r1['saldo_disp_pos']);

    $jsonTempData['campo11'] = utf8_encode($r1['arquear_todas_fp']);
    $jsonTempData['campo12'] = utf8_encode($r1['solicitar_cte_fide']);
    $jsonTempData['campo13'] = utf8_encode($r1['nc_despues_de_x_minutos']);
    $jsonTempData['campo14'] = utf8_encode($r1['forzar_fact_con_z']);
    $jsonTempData['campo15'] = utf8_encode($r1['forzar_fact_con_x_dias']);

    $jsonTempData['campo16'] = utf8_encode($r1['suc_codigo_fe']);
    $jsonTempData['campo17'] = utf8_encode($r1['est_codigo_fe']);
    $jsonTempData['campo18'] = utf8_encode($r1['activo_fe']);
    $jsonTempData['campo19'] = utf8_encode($r1['usuario_fe']);
    $jsonTempData['campo20'] = utf8_encode($r1['clave_fe']);
    $jsonTempData['campo21'] = utf8_encode($r1['no_carg_global']);
    $jsonTempData['campo22'] = utf8_encode($r1['no_desc_global']);
    $jsonTempData['campo23'] = utf8_encode($r1['coordenad_fe']);
    $jsonTempData['campo24'] = utf8_encode($r1['forza_desc_glob_en_prod']);
    $jsonTempData['campo25'] = utf8_encode($r1['muestra_tipo_precio_btn_pos']);
    $jsonTempData['campo26'] = utf8_encode($r1['company_code']);
    $jsonTempData['campo27'] = utf8_encode($r1['api_key']);
    $jsonTempData['campo28'] = utf8_encode($r1['ocp_apim_subscription_key']);
    $jsonTempData['campo29'] = utf8_encode($r1['ws_log_fe']);
    $jsonTempData['campo30'] = utf8_encode($r1['url_fe']);
    $jsonTempData['campo31'] = utf8_encode($r1['pac_code_fe']);
    $jsonTempData['campo32'] = utf8_encode($r1['pac_name_fe']);
    $jsonTempData['campo33'] = utf8_encode($r1['load_from_ini_fe']);
    $jsonTempData['campo34'] = utf8_encode($r1['pac_mess_fe']);
    $jsonTempData['campo35'] = utf8_encode($r1['forzar_impresion_fe']);
    $jsonTempData['campo36'] = utf8_encode($r1['razon_social_fe']);
    $jsonTempData['campo37'] = utf8_encode($r1['banis_usa_terminal']);
    $jsonTempData['campo38'] = utf8_encode($r1['banis_ip_terminal']);
    $jsonTempData['campo39'] = utf8_encode($r1['banis_cierre_z']);

    $jsonTempData['campo40'] = utf8_encode($r1['yap_active']);
    $jsonTempData['campo41'] = utf8_encode($r1['yap_url']);
    $jsonTempData['campo42'] = utf8_encode($r1['yap_api_key']);
    $jsonTempData['campo43'] = utf8_encode($r1['yap_pas_key']);
    $jsonTempData['campo44'] = utf8_encode($r1['yap_dev_id']);
    $jsonTempData['campo45'] = utf8_encode($r1['yap_dev_name']);
    $jsonTempData['campo46'] = utf8_encode($r1['yap_dev_user']);
    $jsonTempData['campo47'] = utf8_encode($r1['yap_dev_group_id']);
    $jsonTempData['campo48'] = utf8_encode($r1['yap_log']);
    $jsonTempData['campo49'] = utf8_encode($r1['yap_time_sec']);



    $jsonTempData['campo50'] = utf8_encode($r1['cupon_descuento_activo']);

    $jsonTempData['campo51'] = utf8_encode($r1['cupon_descuento_fecha_ini']);

    $jsonTempData['campo52'] = utf8_encode($r1['cupon_descuento_fecha_fin']);

    $jsonTempData['campo53'] = utf8_encode($r1['cupon_descuento_mensaje']);

    $jsonTempData['campo54'] = utf8_encode($r1['cupon_descuento_link_web']);

    $jsonTempData['campo55'] = utf8_encode($r1['cupon_descuento_cada_x_dolares']);

    $jsonTempData['campo56'] = utf8_encode($r1['plux_active']);
    $jsonTempData['campo57'] = utf8_encode($r1['plux_url']);
    $jsonTempData['campo58'] = utf8_encode($r1['plux_api_key']);
    $jsonTempData['campo59'] = utf8_encode($r1['plux_usr_key']);
    $jsonTempData['campo60'] = utf8_encode($r1['plux_usr_secret']);
    $jsonTempData['campo61'] = utf8_encode($r1['plux_cli_id']);
    $jsonTempData['campo62'] = utf8_encode($r1['plux_cli_name']);
    $jsonTempData['campo63'] = utf8_encode($r1['plux_merchant_id']);
    $jsonTempData['campo64'] = utf8_encode($r1['plux_terminal_id']);
    $jsonTempData['campo65'] = utf8_encode($r1['plux_site_id']);
    $jsonTempData['campo66'] = utf8_encode($r1['plux_log']);
    $jsonTempData['campo67'] = utf8_encode($r1['app_pio_active']);




    $jsonData[] = $jsonTempData;

    $i = $i + 1;
}

$json->registros = $i;

$json->estaciones_global_conf = $jsonData;

$hoynow = getdate();

$timeStamp = $hoynow['year'] . '-' . $hoynow['mon'] . '-' . $hoynow['mday'] . ' ' . $hoynow['hours'] . ':' . $hoynow['minutes'] . ':' . $hoynow['seconds'];

$timeStamp2 = $hoynow['year'] . '-' . $hoynow['mon'] . '-01 00:00:01';
//echo $timeStamp;

$SQLflags = 'select idflagprom,timestampdiff(minute ,idflagpromdate,now()) as dif from ' . $_SESSION['database'] . '.flag  limit 1';

//    logs( $SQLflags );

$qur = $connection->query($SQLflags);

$_SESSION['flags'] = '9';
$_SESSION['dif'] = '1';
while ($r = mysqli_fetch_assoc($qur)) {

    $_SESSION['flags'] = $r['idflagprom'];
    $_SESSION['dif'] = $r['dif'];

    logs('Esta es la diferencias ' . $_SESSION['dif']);

}

if (1 == 1) {

    $montoesql3 = 'select  ' . $_SESSION['database'] . '.estaciones_global_conf.trig_unic  from ' . $_SESSION['database'] . '.estaciones_global_conf where usermodify=1  limit 1';

    //echo  $montoesql;
    logs($montoesql3);

    $qur = $connection->query($montoesql3);

    $_SESSION['trig_unic'] = 'NO';

    while ($r = mysqli_fetch_assoc($qur)) {

        $_SESSION['trig_unic'] = $r['trig_unic'];

    }

    logs('listo estaciones_global_conf' . $_SESSION['trig_unic']);

    if ($_SESSION['trig_unic'] != 'NO') {

        $SQLupdate = 'UPDATE   ' . $_SESSION['database'] . '.estaciones_global_conf SET syncprocesing=1,usermodify=0 WHERE  usermodify=1 limit 100';

        //echo $SQLupdate;

        logs('uuuuuuuuuuuuuuuuuuuuuuuuuuu' . $SQLupdate);

        if ($connection->query($SQLupdate) === true) {
            //  echo 'update op';
        } else {
            echo 'Error: ' . $SQLupdate . '<br>' . $conn->error;
        }

        $getData = 'select e_corre_econf_est+1 as corre_e,e_id FROM ' . $_SESSION['database'] . '.estaciones  where   e_id=1 limit 1';

        logs('ccccccccccccccccccccccccccccccc' . $getData);

        //$getData = 'select * from user';
        $qur = $connection->query($getData);

        $_SESSION['corre'] = 0;

        while ($r = mysqli_fetch_assoc($qur)) {

            $_SESSION['corre'] = $r['corre_e'];
        }

        if ($_SESSION['corre'] > 0) {

            $SQLupdate2 = '  insert into  ' . $_SESSION['database'] . '_tmp_pack.est_global_conf_pack  (id_pack,trig_unic) select   ' . $_SESSION['corre'] . "  as id,
            estaciones_global_conf.trig_unic


				  from
				 " . $_SESSION['database'] . ".estaciones_global_conf


				where estaciones_global_conf.syncprocesing=1  ";

            //echo   $SQLupdate2;

            logs($SQLupdate2);

            if ($connection->query($SQLupdate2) === true) {

                $SQLupdate3 = 'select id_pack from ' . $_SESSION['database'] . '_tmp_pack.est_global_conf_pack where  id_pack=' . $_SESSION['corre'] . ' limit 1';

                logs('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' . $SQLupdate3);

                $qur = $connection->query($SQLupdate3);

                $empaco = 'NO';
                while ($r = mysqli_fetch_assoc($qur)) {

                    $empaco = $r['id_pack'];

                    logs('si consiguio un registro  ' . $empaco);

                }

                if ($_SESSION['corre'] == $empaco) {

                    logs('   empaco' . $empaco . ' corre  ' . $_SESSION['corre']);

                    $SQLupdate4 = 'UPDATE ' . $_SESSION['database'] . '.estaciones_global_conf SET syncprocesing=0,usermodify=0 WHERE  syncprocesing=1';

                    logs('   update prom xxxxxxxxxxxxxxxxxxxxxx ' . $SQLupdate4);

                    if ($connection->query($SQLupdate4) === true) {

                        $sqlmax = 'select max(id_pack) as id_pack from ' . $_SESSION['database'] . '_tmp_pack.est_global_conf_pack limit 1';

                        $qur12 = $connection->query($sqlmax);

                        $_SESSION['empaco'] = 0;

                        while ($r = mysqli_fetch_assoc($qur12)) {

                            $_SESSION['empaco'] = $r['id_pack'];
                        }

                        $connection->query('update ' . $_SESSION['database'] . '.estaciones set e_corre_econf_est=' . $_SESSION['empaco'] . '');

                        logs('update ' . $_SESSION['database'] . '.estaciones set e_corre_econf_est=' . $_SESSION['empaco'] . '');

                    } else {
                        echo 'Error: ' . $SQLupdate4 . '<br>' . $conn->error;
                    }

                } else {
                    echo 'Errorde empaque: ' . $_SESSION['corre'] . ' ' . $empaco . '<br>';
                }

            }

        } else {

            //echo 'error en busca de corre';

        }

    }

} else {

    logs('Esta ocupado por la estacionnnnnnnn ' . $_SESSION['est_id'] . ' Bandera ' . $_SESSION['flags']);

}

header('content-type: application/json');
print_r(json_encode($json));

$qur=null;
@mysqli_close($connection);
?>