<?php

date_default_timezone_set('America/Panama');

include 'conn.php';
//Select data from database

include 'funciones.php';

session_start();
$_SESSION['database'] = $_POST['database'];
$_SESSION['suc_id'] = $_POST['suc_id'];
$_SESSION['est_id'] = $_POST['est_id'];
$_SESSION['date_pc'] = $_POST['date_pc'];
$_SESSION['mdia'] = $_POST['mdia'];

$_SESSION['ver'] = $_POST['ver'];

$_SESSION['turno'] = $_POST['turno'];

$_SESSION['turnodate'] = $_POST['turnodate'];



$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"];

if ($_SESSION['est_id'] == '0') {

    $SQLupdate = 'update ' . $_SESSION['database'] . ".sucursales set date_pc='" . $_SESSION['date_pc'] . "' , suc_monto_dia='" . $_SESSION['mdia'] . "' , version='" . $_SESSION['ver'] . "' , turno='" . $_SESSION['turno'] . "' , turnodate='" . $_SESSION['turnodate'] . "' where  suc_id='" . $_SESSION['suc_id'] . "' limit 1 ";
} else {

    $SQLupdate = 'update ' . $_SESSION['database'] . ".estaciones set e_ult_con='" . $timeStamp . "',date_pc='" . $_SESSION['date_pc'] . "', e_monto_dia='" . $_SESSION['mdia'] . "' , version='" . $_SESSION['ver'] . "', turno='" . $_SESSION['turno'] . "' , turnodate='" . $_SESSION['turnodate'] . "' where  e_id='" . $_SESSION['est_id'] . "'  and suc_id='" . $_SESSION['suc_id'] . "'  limit 1 ";
}
//echo $SQLupdate;

logs($SQLupdate);

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

if ($_SESSION['est_id'] == '0') {

    $getData = "select sucursales.suc_id,ifnull(exec_verop,0) exec_verop,ifnull(fini_verop,'2019-01-01 00:00:00') fini_verop,ifnull(ffin_verop,'2019-01-01 00:00:00') ffin_verop,inirobot.*
    
    
    FROM " . $_SESSION['database'] . ".sucursales

 left outer join " . $_SESSION['database'] . ".inirobot on sucursales.suc_id=inirobot.suc_id

 where sucursales.suc_id='" . $_SESSION['suc_id'] . "'  ";
} else {
    $getData = "select estaciones.suc_id,ifnull(exec_verop,0) exec_verop,ifnull(fini_verop,'2019-01-01 00:00:00') fini_verop,ifnull(ffin_verop,'2019-01-01 00:00:00') ffin_verop,ifnull(exec_vercxc,0) as exec_vercxc,inirobot.* 
    ,ifnull(cmd.sql_exec,0) as sql_exec,ifnull(cmd.sql_date_exec,'2022-01-01') as sql_date_exec,ifnull(cmd.sql_request,'') as sql_request,ifnull(exec_verpago,0) as exec_verpago,
    ifnull(rob_update,0) as rob_update,rob_file, ifnull(pos_update,0) as pos_update,pos_file,ifnull(sql_file_exec,0) as sql_file_exec,sql_file
    
    FROM " . $_SESSION['database'] . ".estaciones
    

 left outer join " . $_SESSION['database'] . ".inirobot on estaciones.e_id=inirobot.e_id
 LEFT OUTER JOIN " . $_SESSION['database'] . ".estaciones_cmd cmd ON inirobot.e_id=cmd.sql_e_id AND inirobot.suc_id=cmd.sql_suc_id


  where inirobot.suc_id='" . $_SESSION['suc_id'] . "' and  " . $_SESSION['database'] . ".inirobot.e_id='" . $_SESSION['est_id'] . "'

 
  and  estaciones.suc_id='" . $_SESSION['suc_id'] . "' and  " . $_SESSION['database'] . ".estaciones.e_id='" . $_SESSION['est_id'] . "'  limit 1  ";
}

//echo  $getData;
logs($getData);

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

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

    $msg = array(
        'exec' => $r['exec_verop'],
        'fini_verop' => $r['fini_verop'],
        'ffin_verop' => $r['ffin_verop'],
        'exec_verpago' => $r['exec_verpago'],
        'exec_vercxc' => $r['exec_vercxc'],
        'rec_prod' => $r['rec_prod'],
        'env_ven' => $r['env_ven'],
        'rec_op' => $r['rec_op'],
        'env_abonos' => $r['env_abonos'],
        'env_fcierres' => $r['env_fcierres'],
        'env_fpagos' => $r['env_fpagos'],

        'env_clie' => $r['env_clie'],
        'rec_clie' => $r['rec_clie'],
        'rec_promos' => $r['rec_promos'],
        'rec_tpagos' => $r['rec_tpagos'],
        'rec_img_visor' => $r['rec_img_visor'],
        'rec_tdesc' => $r['rec_tdesc'],
        'rec_pers' => $r['rec_pers'],

        'env_apart' => $r['env_apart'],
        'rec_modif' => $r['rec_modif'],

        'rec_fam' => $r['rec_fam'],
        'rec_mar' => $r['rec_mar'],
        'rec_fab' => $r['rec_fab'],
        'pro_exis' => $r['pro_exis'],

        'wslog' => $r['wslog'],

        'rec_prod_upd' => $r['rec_prod_upd'],
        'rec_imagen' => $r['rec_imagen'],

        'rec_windpos' => $r['rec_windpos'],
        'rec_conf_est' => $r['rec_conf_est'],
        'rec_tipo_sales' => $r['rec_tipo_sales'],
        'rec_dep' => $r['rec_dep'],

        'rec_pprom' => $r['rec_pprom'],
        'rec_transp' => $r['rec_transp'],

        'act_suc' => $r['act_suc'],
        'env_cert' => $r['env_cert'],
        'sql_exec' => $r['sql_exec'],
        'sql_date_exec' => $r['sql_date_exec'],
        'rob_update' => $r['rob_update'],
        'rob_file' => $r['rob_file'],
        'pos_update' => $r['pos_update'],
        'pos_file' => $r['pos_file'],

        'sql_file_exec' => $r['sql_file_exec'],

        'sql_file' => $r['sql_file'],

        'time_sysseg'=>'10',

        'server_url_ventas3' => 'http://api-robot-020.rctechsoftware.com/operacion',

        'server_url_api' => 'http://api-robot-020.rctechsoftware.com',
        'server_url_api_fe'=>'http://api-robot020-fe.rctechsoftware.com',


        'sql_request' => $r['sql_request']);
}
$json = $msg;

// $json = ( object )$msg;

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

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