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

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;

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

logs($montoesql);

$_SESSION['montoe'] = "0";

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

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

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

//echo "monor" .$_SESSION['monto']e;

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

//echo $SQLupdate;

logs($SQLupdate);

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

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

logs($getData);

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

while ($r = mysqli_fetch_assoc($qur)) {
    logs("si hay datos");

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

// $json = (object)$msg;

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

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