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

$_SESSION['database'] = $_POST["database"];

$_SESSION['suc_id'] = $_POST["suc_id"];

$_SESSION['est_id'] = $_POST["est_id"];

$_SESSION['id_pack'] =$_POST["id_pack"];

 */

//$_SESSION['database'] = '1002_tc';

//$_SESSION['suc_id'] ='2';

//$_SESSION['est_id'] ='2';

//include_once ("../conn.php");
//include_once ("../funciones.php");

//$eje=empaca($connection);

function empaca($connection2)
{

    $montoesql3 = "select 	" . $_SESSION['database'] . ".operacion.op_id from  " . $_SESSION['database'] . ".operacion  where  " . $_SESSION['database'] . ".operacion.usermodify=1  and (op_tipo=2 or op_tipo=16) and op_fecha<=now() - INTERVAL 5 SECOND limit 1";

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

    $_SESSION['op_id'] = "NO";

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

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

        logs("nume entro " . $r['op_id']);

    }
    //   echo "nume ".$_SESSION['po_op_id'] ;

    logs("numeroooooooooooooooooo " . $_SESSION['op_id']);

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

        $SQLupdate2o = "UPDATE   " . $_SESSION['database'] . ".operacion
  set operacion.syncprocesing= 1     where " . $_SESSION['database'] . ".operacion.usermodify=1 and (op_tipo=2 or op_tipo=16) ' limit 5";

        if ($connection2->query($SQLupdate2o) === true) {
            //  echo "update op2";
        } else {
            logs("Error: " . $SQLupdate2o . "<br>" . $connection->error);
        }
        ;

//_SESSION['_id']='0';

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

            $getData = "select suc_corre_eop+1 as corre_e,suc_id FROM " . $_SESSION['database'] . ".sucursales  where   suc_id=" . $_SESSION['suc_id'];

        } else {

            $getData = "select e_corre_eop+1 as corre_e,e_id FROM " . $_SESSION['database'] . ".estaciones  where   e_id>1";
        }
        ;

        $qur = $connection2->query($getData);

        $_SESSION['corre'] = 0;

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

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

        logs("entro a la pelea: nro" . $_SESSION['corre']);

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

//echo "entro a la pelea";

            $SQLupdate2 = "  insert into  " . $_SESSION['database'] . "_tmp_pack.operacion_pack_v2 (id_pack,op_id)  select   " . $_SESSION['corre'] . "  as id,
			 	   operacion.op_id 	  from
				 " . $_SESSION['database'] . ".operacion where syncprocesing=1 ";

            logs($SQLupdate2);

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

                //echo "entro por aqui";

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

                logs('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' . $SQLupdate3);
                $qur = $connection2->query($SQLupdate3);

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

                    $empaco = $r['id_pack'];

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

//echo "si consiguio un registro  ".$empaco;

                }

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

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

                    $SQLupdate4 = "UPDATE " . $_SESSION['database'] . ".operacion   set operacion.syncprocesing= 0,operacion.usermodify=0 WHERE  operacion.syncprocesing=1  ";

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

                        if ($_SESSION['e_id'] == "0") {
                            $connection2->query("update " . $_SESSION['database'] . ".sucursales set suc_corre_eop=suc_corre_eop+1 ");
                        } else {

                            $connection2->query("update " . $_SESSION['database'] . ".estaciones set e_corre_eop=" . $empaco . "");

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

                        }

                    } else {
                        echo "Error: " . $SQLupdate4 . "<br>" . $connection2->error;
                    }

                } else {
                    echo "Error de empaque 88: " . $_SESSION['corre'] . " " . $empaco . "<br>";
                }

            } else {echo "Error: " . $SQLupdate2 . "<br>" . $connection2->error;
            }

        } else {

            echo "error en busca de corre99";

        }

    } else {
        echo '  Nada que empacar';
    }

}

@mysqli_close($connection2);

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

//empacando($_SESSION['database']);

?>
