<?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_dep=" . $_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 a.dep_id ,
dep_descripcion ,
dep_activo ,
suc_id ,
dep_abrev ,
ifnull(dep_orden ,0) as dep_orden,
ifnull(para_la_venta,0) as para_la_venta,
ifnull(transi_dep,0) as transi_dep,flag_borrar

FROM



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

left outer join " . $_SESSION['database'] . ".depositos a on a.dep_id=b.dep_id


                   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['dep_id']);
    $jsonTempData['campo2'] = ($r1['dep_descripcion']);
    $jsonTempData['campo3'] = ($r1['dep_activo']);
    $jsonTempData['campo4'] = utf8_encode($r1['suc_id']);
    $jsonTempData['campo5'] = utf8_encode($r1['dep_abrev']);
    $jsonTempData['campo6'] = utf8_encode($r1['dep_orden']);
    $jsonTempData['campo7'] = utf8_encode($r1['para_la_venta']);
    $jsonTempData['campo8'] = utf8_encode($r1['transi_dep']);
    $jsonTempData['campo9'] = utf8_encode($r1['flag_borrar']);

    $jsonData[] = $jsonTempData;

    $i = $i + 1;
}

$json->registros = $i;

$json->depositos = $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'] . '.depositos.dep_id  from ' . $_SESSION['database'] . '.depositos where usermodify=1  limit 1';

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

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

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

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

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

    }

    logs('listo dep' . $_SESSION['dep_id']);

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

        $SQLupdate = 'UPDATE   ' . $_SESSION['database'] . '.depositos 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_edep+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.depositos_pack  (id_pack,dep_id) select   ' . $_SESSION['corre'] . "  as id,
            depositos.dep_id


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


				where depositos.syncprocesing=1  ";

            // echo   $SQLupdate2;

            logs($SQLupdate2);

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

                $SQLupdate3 = 'select id_pack from ' . $_SESSION['database'] . '_tmp_pack.depositos_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'] . '.depositos 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.depositos_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_edep=' . $_SESSION['empaco'] . '');

                        logs('update ' . $_SESSION['database'] . '.estaciones set e_corre_edep=' . $_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);
?>
