<?php
// app/api/transactions/currencies

    header("Content-Type:application/json");
    include("../../../settings/dbconn.php");
    include("../../../settings/utils.php");
    
    // parametros obligatorios
    $parmsob = array("sessionid");
    if (!parametrosValidos($_GET, $parmsob)){
        badEnd("400", array("msg"=>"Parametros obligatorios " . implode(", ", $parmsob)));
    }
    
    //Declaracion de variables
    $out = new stdClass();
    $sessionid = $_GET["sessionid"];
    $currencyid = "";
    
    //Validamos la session
    isSessionValid($db, $sessionid);
    
    //Obtenemos el lenguaje del usuario
    $lang = updateLang($db,$sessionid);
    
    
    //Si no se filtra por nada solo devuelve monedas base
    $currencyid = " WHERE (pairs.refcurrencyid = currencies.id) AND pairs.status = 1 AND currencies.status = 1 ";
    
    // Si se envia el currencyid, se arma el query
    if(isset($_GET['currencyid']) && $_GET['currencyid'] != ''){
        // Si se envia una moneda base, buscaremos las cuentas por las monedas ref de los pares donde se encuentre
        
        // Consultaremos las monedas ref de los pares donde esta la moneda base
        $sql = "SELECT p.refcurrencyid, p.currencyid ".
        "       FROM pairs p ".
        "       INNER JOIN currencies c ON (p.refcurrencyid = c.id OR p.currencyid = c.id) AND c.status = 1 " .
        "       WHERE p.status=1 ".
        "       AND p.refcurrencyid = ".$_GET['currencyid'];//.
        //"       OR currencyid =".$_GET['currencyid'];
        if (!$rs=$db->query($sql))
            badEnd("500", array("msg"=>determinateDBError($db)));
            
        $count = 0;
        $currencyid = " WHERE (pairs.currencyid = currencies.id) AND currencies.status = 1 AND (";
        
        // Con las monedas recogidas armamos la condicion
        while($row = $rs->fetch_assoc()){
            
            if($count == 0){
                $currencyid .= " pairs.currencyid = ".$row['currencyid'];//($_GET['currencyid'] == $row['currencyid'] ? $row['refcurrencyid'] : $row['currencyid']);
                $count++;
            }else{
                $currencyid .= " OR pairs.currencyid = ".$row['currencyid'];//($_GET['currencyid'] == $row['currencyid'] ? $row['refcurrencyid'] : $row['currencyid']);
            }
            
        }
        
        $currencyid .= " ) ";
        /*
        // Si se envia una moneda base, buscaremos las cuentas por las monedas ref de los pares donde se encuentre
        
        // Consultaremos las monedas ref de los pares donde esta la moneda base
        $sql = "SELECT refcurrencyid, currencyid ".
        "       FROM pairs ".
        "       WHERE refcurrencyid = ".$_GET['currencyid'] ." ";
        //"       OR refcurrencyid =".$_GET['currencyid'];
        if (!$rs=$db->query($sql))
            badEnd("500", array("msg"=>determinateDBError($db)));
            
        $count = 0;
        $currencyid = " WHERE (currencyid = currencies.id) ";// OR refcurrencyid = currencies.id) ";
        
        // Con las monedas recogidas armamos la condicion
        while($row = $rs->fetch_assoc()){
            
            if($count == 0){
                $currencyid .= " AND ( (pairs.currencyid = ".($_GET['currencyid'] == $row['currencyid'] ? $row['refcurrencyid'] : $row['currencyid']) .") ";
                //"               OR pairs.refcurrencyid = ".($_GET['currencyid'] == $row['currencyid'] ? $row['refcurrencyid'] : $row['currencyid']).") ";
                $count++;
            }else{
                $currencyid .= //" OR (pairs.refcurrencyid = ".($_GET['currencyid'] == $row['currencyid'] ? $row['refcurrencyid'] : $row['currencyid']).
                "               OR (pairs.currencyid = ".($_GET['currencyid'] == $row['currencyid'] ? $row['refcurrencyid'] : $row['currencyid']).") ";
            }
            
        }
        
        $currencyid .= ") ";*/
    }
    
    //Consultamos la cantidad de monedas bases que retornaremos
    /*$sql = "SELECT COUNT(DISTINCT currencies.id) AS qty ".
    "       FROM pairs, currencies ".   
    "       WHERE (pairs.currencyid = currencies.id ".
    "       OR pairs.refcurrencyid = currencies.id) ".
    "       ".$currencyid;*/
    $sql = "SELECT COUNT(DISTINCT currencies.id) AS qty ".
    "       FROM pairs, currencies ".   
    //"       WHERE (pairs.refcurrencyid = currencies.id) ".
    "       ".$currencyid;
    if (!$rs=$db->query($sql))
        badEnd("500", array("msg"=>determinateDBError($db)));
        
    $row = $rs->fetch_assoc();
    $out->numofrecords = (int)$row['qty'];
    //$out->Zprobando = $sql;
    
    // Consultamos los datos de las monedas base
    /*$sql = "SELECT DISTINCT currencies.id AS currencyid, ".
    "       currencies.name AS currencyname, ".
    "       currencies.symbol AS currencysymbol, ".
    "       currencies.icon AS currencyicon, ".
    "       currencies.decimals AS decimals, ".
    "       currencies.interface AS interface, ".
    "       currencies.status AS status ".
    "       FROM pairs, currencies ".
    "       WHERE (pairs.currencyid = currencies.id ".
    "       OR pairs.refcurrencyid = currencies.id) ".
    "       ".$currencyid;
    */
    //SOLO DEVUELVE MONEDAS BASE
    $sql = "SELECT DISTINCT currencies.id AS currencyid, ".
    "       currencies.name AS currencyname, ".
    "       currencies.symbol AS currencysymbol, ".
    "       currencies.icon AS currencyicon, ".
    "       currencies.decimals AS decimals, ".
    "       currencies.interface AS interface, ".
    "       currencies.status AS status ".
    "       FROM pairs, currencies ".
    //"       WHERE (pairs.refcurrencyid = currencies.id)  ".
    "       ".$currencyid;
    //die($sql);
    if (!$rs=$db->query($sql))
        badEnd("500", array("msg"=>determinateDBError($db)));
        
    //Ordenamos los datos
    while($row = $rs->fetch_assoc()){
        $record = new stdClass();
        
        $record->id = (int)$row['currencyid'];
        $record->name = $row['currencyname'];
        $record->symbol = $row['currencysymbol'];
        $record->icon = $row['currencyicon'];
        $record->decimals = (int)$row['decimals'];
        $record->interface = (int)$row['interface'];
        
        // Estado de la moneda
        $record->status = new stdClass();
        $record->status->id = (int)$row['status'];
        $record->status->dsc = intval($row['status']) == 1 ? 'Activo' : 'Inactivo';
        
        $records [] = $record;
    }
    
    $out->records = $records;
    
    header("HTTP/1.1 200");
    echo (json_encode($out));
    die();
?>