<?php
include("utiles.php");

date_default_timezone_set("America/Panama");
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);

if (! $connection = connectDb()) {
    die("err db");
} else {
    $connection->query("SET NAMES 'utf8';");
}
header('content-type: application/json');

session_start();
$_SESSION['db_Name'] = $_POST['db_Name'];
$_SESSION['cod_suc'] = $_POST['cod_suc'];
$_SESSION['cod_est'] = $_POST['cod_est'];
$_SESSION['cod_sta'] = $_POST['cod_sta'];
$_SESSION['shw_log'] = $_POST['shw_log'];

if (isset($_SESSION['shw_log']) == false)
    $iShw_Log = 0;
else
    $iShw_Log = $_SESSION['shw_log'];

$cDb_Name = $_SESSION['db_Name'];
$cCod_Suc = $_SESSION['cod_suc'];
$cCod_Est = $_SESSION['cod_est'];
$cCod_Sta = $_SESSION['cod_sta'];

$hoynow = getdate();
$timeStamp = $hoynow["year"] . "-" . $hoynow["mon"] . "-" . $hoynow["mday"];
$timeStamp = date("Y-m-d H:i:s");

$cSql_Ln = "
    SELECT 
        op.op_id,
        op.op_fecha,
        DATE_ADD(op.op_fecha, INTERVAL 15 MINUTE) AS op_fechaf,
        op.op_num_corre,
        op.op_id_cliente,
        op.op_nombre,
        op.op_dv,
        op.op_telf,
        ct.c_correo,
        op.op_total,
        op.op_app
    FROM `$cDb_Name`.operacion op
    LEFT JOIN `$cDb_Name`.clientes ct ON (op.op_id_cliente = ct.c_id)
    WHERE (op.op_tipo		=9)
    AND   (op.id_status	    =$cCod_Sta)
    AND   (op.suc_id		=$cCod_Suc)
    AND   ( TRIM(IFNULL(op.op_app,'')) <> '' ) 
    AND   ( (DATE(op.op_fecha)>=DATE_ADD(DATE(NOW()), INTERVAL -1 DAY) ) AND (DATE(op.op_fecha)<=DATE(NOW())) ) 
    ORDER BY op.op_fecha ASC 
    LIMIT 25;";
if ($iShw_Log==1)
    echo $cSql_Ln.chr(13);

$oQryData = mysqli_query($connection, $cSql_Ln);
$oJsnItems = array();
$oJsnItem = array();
$oJsnResp = array();
$iCnt = 0;
while ($oDataRow = mysqli_fetch_array($oQryData, MYSQLI_ASSOC)) {
    $oJsnItem['platform'] = 'APP-PIOPIO';
    $oJsnItem['number'] = $oDataRow['op_id'];
    $oJsnItem['cliente'] = $oDataRow['op_nombre'];
    $oJsnItem['op_app'] = $oDataRow['op_app'];
    $oJsnItem['status'] = 1;
    $oJsnItem['created_at'] = $oDataRow['op_fecha'];
    $oJsnItem['expire_at'] = $oDataRow['op_fechaf'];
    $oJsnItem['callback_accepted'] = '';
    $oJsnItem['callback_rejected'] = '';
    $oJsnItem['callback_prepared'] = '';
    $oJsnItem['callback_picked_up'] = '';
    $oJsnItems[] = $oJsnItem;
    $iCnt = $iCnt + 1;
}
$oJsnResp['pending'] = $iCnt;
$oJsnResp['orders'] = $oJsnItems;

header('Content-type: application/json');
print_r(json_encode($oJsnResp));

$oDataRow = NULL;
$oJsnItems = NULL;
$oJsnItem = NULL;
$oJsnResp = NULL;

mysqli_free_result($oQryData);
mysqli_close($connection);

session_unset();
session_destroy();
/*
JOHNN GUTIERREZ 2024-09-06
*/
