<?php
session_start();
if( $_SESSION["user"] == "" || $_SESSION["passwd"] == "" ) {
//   header("Location: index.php");
}

$Cuenta = isset($_POST['key']) ? $_POST["key"]  : ""; 

include_once('db_config.php');
ini_set('max_execution_time', 120);

   if( strlen($Cuenta) > 0 ) {

      $first = true;
      $sql = "select sql_no_cache id, cuenta, denominacion, sumariza, columna, columna_sumassaldos, cuenta_sumarizacion, asientos, salto from cuentas where cuenta = '" . $Cuenta . "' and borrado=0 limit 1";
//      echo $sql;
      $result = $conn->query($sql);
      if ($result->num_rows > 0) {
         if( $row = $result->fetch_assoc() ) {
//            echo "{\"id\":\"" . $row["id"] . "\",\"nombre\":\"" . $row["nombre"] . "\" }";

            echo "{\"id\":\"" . $row["id"] . "\",";
            echo  "\"cuenta\":\"" . $row["cuenta"] . "\",";
            echo  "\"denominacion\":\"" . $row["denominacion"] . "\",";
            echo  "\"sumariza\":\"" . $row["sumariza"] . "\",";
            echo  "\"columna\":\"" . $row["columna"] . "\",";
            echo  "\"columna_sumassaldos\":\"" . $row["columna_sumassaldos"] . "\",";
            echo  "\"cuenta_sumarizacion\":\"" . $row["cuenta_sumarizacion"] . "\",";
            echo  "\"asientos\":\"" . $row["asientos"] . "\",";
            echo  "\"salto\":\"" . $row["salto"] . "\",";
//            echo  "\"observa\":\"" . $row["observa"] . "\"}";
            echo  "\"observa\":\"" . $row["observa"] . "\"";

            $Cuenta = $row["cuenta_sumarizacion"];

            mysql_free_result( $result ); 
            $sql = "select sql_no_cache id, cuenta, denominacion from cuentas where cuenta = '" . $Cuenta . "' limit 1";
            $result = $conn->query($sql);
            if ($result->num_rows > 0) {
               if( $row = $result->fetch_assoc() ) {
                  echo  ",\"descripcion\":\"" . $row["denominacion"] . "\"}";
               }
               else echo  "}";
            }
            else echo  "}";
         }
      }
      else echo "[]";
      mysql_free_result( $result ); 
   }
   mysqli_close($conn);
?>

