<?php
include 'conn.php';
//Select data from database
error_reporting( E_ERROR  | E_PARSE );

include 'funciones.php';
date_default_timezone_set("America/Panama");

session_start();
$_SESSION['database'] = $_POST['dbname'];
$_SESSION['suc_id'] = $_POST['suc_id'];
$_SESSION['est_id'] = $_POST['est_id'];
$_SESSION['monto'] = $_POST['monto'];
$_SESSION['pack'] = $_POST['pack'];
$_SESSION['ip'] = $_POST['ip'];

$_SESSION["newsession"] = $_POST['dbname'];

$info = $_SESSION["newsession"];
/*session created*/
//echo $_SESSION["newsession"];

$hoynow = getdate();

if ($_SESSION['monto'] == "") {
    $_SESSION['monto'] = "0";

}

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

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

if ($_SESSION['est_id'] == "0") {
    $montoesql = "select ifnull(sum(op_subtotalneto),0) as monto from " . $_SESSION['database'] . ".operacion where (op_tipo=2 or op_tipo=16 or op_tipo=13 or op_tipo=14 or op_tipo=17 or op_tipo=18) and suc_id='" . $_SESSION['suc_id'] . "'  and op_fecha>='" . $timeStamp2 . "'";

} else {

    $montoesql = "select ifnull(sum(op_subtotalneto),0) as monto from " . $_SESSION['database'] . ".operacion where (op_tipo=2 or op_tipo=16 or op_tipo=13 or op_tipo=14 or op_tipo=17 or op_tipo=18) and suc_id='" . $_SESSION['suc_id'] . "' and op_estacion='" . $_SESSION['est_id'] . "' and op_fecha>='" . $timeStamp2 . "'";

    //$montoesql="select 1000 as 'monto' ";

}
;

logs($montoesql);

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

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

    $_SESSION['montoe'] = $r['monto'];
}

//echo "monor" .$_SESSION['monto']e;
if ($_SESSION['est_id'] == "0") {
    $SQLupdate = "update " . $_SESSION['database'] . ".sucursales set suc_ult_con=now(),suc_montor=" . $_SESSION['monto'] . ",suc_montoe=" . $_SESSION['montoe'] . ",suc_pack_venta=" . $_SESSION['pack'] . ", suc_sinc_host='" . $_SESSION['ip'] . "' where  suc_id='" . $_SESSION['suc_id'] . "' limit 1 ";

} else {
    $SQLupdate = "update " . $_SESSION['database'] . ".estaciones set e_ult_con=now(),e_montor=" . $_SESSION['monto'] . ",e_montoe=" . $_SESSION['montoe'] . ",e_pack_venta=" . $_SESSION['pack'] . ", e_sinc_host='" . $_SESSION['ip'] . "' 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") {

    logs("Entro por sucursales" . $_SESSION['est_id']);

    $getData = "select suc_ftp_rop as suc_ftp_rop FROM " . $_SESSION['database'] . ".sucursales where suc_id='" . $_SESSION['suc_id'] . "'";

} else {

    logs("Entro por estaciones" . $_SESSION['est_id']);

    $getData = "select e_ftp_rop as e_ftp_rop FROM " . $_SESSION['database'] . ".estaciones where suc_id='" . $_SESSION['suc_id'] . "' and e_id='" . $_SESSION['est_id'] . "' ";
}

logs($getData);

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

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

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

        $msg = array("corre" => $r['suc_ftp_rop'], "operaciones" => $r['suc_ftp_rop'], "info" => $info);

    } else {

        $msg = array("corre" => $r['e_ftp_rop'], "operaciones" => $r['e_ftp_rop'], "info" => $info);
    }

}
$json = $msg;

// $json = (object)$msg;

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

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