<?php
/*
//  app/api/c2p/validate
    header("Content-Type:application/json");
    include("../../../settings/dbconn.php");
    include("../../../settings/utils.php");
    
    $out = new stdClass();
    
    // Obtenemos el ultimo deposito ingresado para obtener siu fecha
    $sql = "SELECT deposits.createddate AS createddate, deposits.* ".
    "       FROM deposits, accounts, transactions ".
    "       WHERE deposits.rejected IS NULL ".
    "       AND deposits.valid IS NULL ".
    "       AND deposits.filetype IS NULL ".
    "       AND deposits.transactionid = transactions.id".
    "       AND transactions.accountid = accounts.id ".
    "       ORDER BY deposits.createddate".
    "       LIMIT 1";
    if (!$rs=$db->query($sql))
        badEnd("500", array("msg"=>$db->error.$sql));
    
    $row = $rs->fetch_assoc();
    $out->row = $row;
    
    // Buscamos depositos por la API
    $nonce = generateNonce();
    
    // Llamamos a la api primerito
    $request = array("fechaInicio"=>date("Y-m-d", strtotime($row['createddate'])));
    
    $headers = array('Content-Type:application/json', 
        "api-key: $BP_C2P_KEY", 
        "api-signature: ".
        (
            generateApiSignature("/".$BP_VERSION."/pagos/c2p".$nonce, $BP_C2P_KEY_SECRET)
        ),
        "nonce: ".$nonce,
    );
    
    // obtenemos respuesta
    $rest = queryApiBP($BP_URL_QA, "pagos/c2p/J302522030", $BP_VERSION, "GET", $headers, $request);
    $out->rest = $rest;
    
    if($rest['httpres'] == 200 || $rest['httpres'] == 201){
        foreach($rest['pagos'] as $deposit){
            // Acomodamos la fecha esa fea
            $deposit['fecha'] = str_replace('/', '-', $deposit['fecha']);
        
            // Consultamos por el ref que exista el pago
            $sql = "SELECT COUNT(paymentref) AS qty FROM deposits ".
            "       WHERE paymentref = '".$deposit['referencia']."'".
            "       AND DATE(createddate) = DATE('".date("Y-m-d",strtotime($deposit['fecha']))."')";
            if (!$res=$db->query($sql))
                badEnd("500", array("msg"=>$db->error.$sql));
            
            $rowqty = $res->fetch_assoc();
            
            // Si es mayor que cero lo ejecutamos
            if($rowqty['qty'] > 0){
                // Obtenemos los detalles para su actualizacion
                $sql = "SELECT * FROM deposits ".
                "       WHERE paymentref = '".$deposit['referencia']."'".
                "       AND DATE(createddate) = DATE('".date("Y-m-d",strtotime($deposit['fecha']))."')";
                if (!$res=$db->query($sql))
                    badEnd("500", array("msg"=>$db->error.$sql));
                
                $depositdata = $res->fetch_assoc();
                
                if($deposit['identReverso'] == 'R'){
                    if($depositdata['rejected'] == null){
                        //Actualizamos que esta validada en la tabla
                        $sql = "UPDATE deposits SET".
                        "       rejected = '".date("Y-m-d", strtotime($deposit['fecha']))." ".date("H:i:s", strtotime($deposit['hora']))."' ".
                        "       WHERE paymentref = '".$deposit['referencia']."'".
                        "       AND rejected IS NULL ".
                        "       AND paymentaccountid = ".$depositdata['paymentaccountid'];
                        if (!$db->query($sql))
                            badEnd("500", array("msg"=>$db->error.$sql));
                                    
                        //Actualizamos la transaccion
                        $sql = "UPDATE transactions SET".
                        "       amountghost = amount + amountghost, ".
                        "       amount = 0 ".
                        "       WHERE txid = ".$depositdata['transactionid'];
                        if (!$db->query($sql))
                            badEnd("500", array("msg"=>$db->error.$sql));
                            
                    }
                }else{
                    if($depositdata['valid'] == null){
                        //Actualizamos que esta validada en la tabla
                        $sql = "UPDATE deposits SET".
                        "       valid = '".date("Y-m-d", strtotime($deposit['fecha']))." ".date("H:i:s", strtotime($deposit['hora']))."' ".
                        "       WHERE paymentref = '".$deposit['referencia']."'".
                        "       AND valid IS NULL ".
                        "       AND paymentaccountid = ".$depositdata['paymentaccountid'];
                        if (!$db->query($sql))
                            badEnd("500", array("msg"=>$db->error.$sql));
                                    
                        //Actualizamos la transaccion
                        $sql = "UPDATE transactions SET".
                        "       amount = amount + amountghost, ".
                        "       amountghost = 0 ".
                        "       WHERE txid = ".$depositdata['transactionid'];
                        if (!$db->query($sql))
                            badEnd("500", array("msg"=>$db->error.$sql));
                            
                    }
                }
            
            }
        }
    }
    
    header("HTTP/1.1 200");
    echo(json_encode($out));
    die();
?>