arbol.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Ricardo Diaz                                    *
00003  *   richard@galdi.es                                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "arbol.h"
00022 
00023 
00025 
00027 Arbol::Arbol() {
00028     _depura("Arbol::Arbol", 0);
00029     unsigned int i;
00031     raiz = new tipohoja*[70];
00033     for (i = 0; i < 70; i++) {
00034         raiz[i] = NULL;
00035     } // end for
00036     _depura("END Arbol::Arbol", 0);
00037 }
00038 
00039 
00041 
00043 Arbol::~Arbol() {
00044     _depura("Arbol::~Arbol", 0);
00045     unsigned int i;
00047     for (i = 0; i < 70; i++) {
00048         raiz[i] = NULL;
00049     }
00050     delete raiz;
00051     delete hoja;
00052     delete rama;
00053     _depura("END Arbol::~Arbol", 0);
00054 }
00055 
00056 
00058 
00061 void Arbol::nuevarama(cursor2 *ramas) {
00062     _depura("Arbol::nuevarama", 0);
00063     unsigned int i;
00065     hoja = new tipohoja;
00066     hoja->idcuenta = atoi(ramas->valor("idcuenta").toAscii().constData());
00067     hoja->codigo = QString(ramas->valor("codigo"));
00068     hoja->descripcion = QString(ramas->valor("descripcion"));
00069     hoja->saldoant = hoja->debe = hoja->haber = hoja->saldo = hoja->debeej = hoja->haberej = hoja->saldoej = Fixed("0.00");
00070     hoja->numapuntes = 0;
00071     hoja->ramas = NULL;
00073     for(i = 0; i < 70 && raiz[i]; i++) {}
00074     raiz[i] = hoja;
00075     _depura("END Arbol::nuevarama", 0);
00076 }
00077 
00078 
00080 
00083 void Arbol::inicializa(cursor2 *ramas) {
00084     _depura("Arbol::inicializa", 0);
00085     unsigned int i;
00086     QString padre;
00087     tiporama* guia = NULL;
00088     ramas->primerregistro();
00090     for(i = 0; raiz[i] && i < 70; i++) {
00091         padre = raiz[i]->codigo;
00094         while (!ramas->eof() && ramas->valor("codigo").startsWith(padre)) {
00095             if (ramas->valor("nivel").toInt() > 2) {
00097                 hoja = new tipohoja;
00098                 hoja->idcuenta = atoi(ramas->valor("idcuenta").toAscii().constData());
00099                 hoja->codigo = QString(ramas->valor("codigo"));
00100                 hoja->descripcion = QString(ramas->valor("descripcion"));
00101                 hoja->saldoant = hoja->debe = hoja->haber = hoja->saldo = hoja->debeej = hoja->haberej = hoja->saldoej = Fixed("0.00");
00102                 hoja->numapuntes = 0;
00103                 hoja->ramas = NULL;
00104                 rama = new tiporama;
00105                 rama->hoja = hoja;
00106                 rama->sgte = NULL;
00108                 if (!raiz[i]->ramas) {
00109                     raiz[i]->ramas = guia = rama;
00110                 } else { 
00111                     guia->sgte = rama;
00112                     guia = guia->sgte;
00113                 } // end if
00116                 SintetizarRamas(&ramas, &(hoja->ramas));
00117             } else {
00118                 ramas->siguienteregistro();
00119             } // end if
00120         } // end while
00121     } // end for
00122     _depura("END Arbol::inicializa", 0);
00123 }
00124 
00125 
00127 
00131 void Arbol::SintetizarRamas(cursor2 **cuentas, tiporama **ramas) {
00132     _depura("Arbol::SintetizarRamas", 0);
00133     tiporama *guia, *rama;
00134     tipohoja *hoja;
00135     int nivel;
00136     cursor2* ptrcuentas = *cuentas;
00137     nivel = atoi(ptrcuentas->valor("nivel").toAscii().constData());
00138     ptrcuentas->siguienteregistro();
00139     guia = NULL;
00140     while (!ptrcuentas->eof() && (atoi(ptrcuentas->valor("nivel").toAscii().constData()) > nivel)) {
00142         rama = new tiporama;
00143         if (!guia) {
00144             *ramas = guia = rama;
00145         } else {
00146             guia->sgte = rama;
00147             guia = guia->sgte;
00148         } // end if
00149         hoja = new tipohoja; 
00150         hoja->idcuenta = atoi(ptrcuentas->valor("idcuenta").toAscii().constData());
00151         hoja->codigo = ptrcuentas->valor("codigo");
00152         hoja->descripcion = ptrcuentas->valor("descripcion");
00153         hoja->saldoant = hoja->debe = hoja->haber = hoja->saldo = hoja->debeej = hoja->haberej = hoja->saldoej = Fixed("0.00");
00154         hoja->numapuntes = 0;
00155         hoja->ramas = NULL;
00156         guia->hoja = hoja;
00157         guia->sgte = NULL;
00160         SintetizarRamas(&ptrcuentas, &(hoja->ramas));
00161     } // end while
00162     _depura("END Arbol::SintetizarRamas", 0);
00163 }
00164 
00165 
00167 
00170 void Arbol::actualizahojas(cursor2 *cuenta) {
00171     _depura("Arbol::actualizahojas", 0);
00172     unsigned int i = 0;
00173     bool actualizado;
00174     tipohoja *hojaraiz;
00175 
00176     QString cuentapadre = cuenta->valor("codigo").left(2);
00177     do {
00178         hojaraiz = raiz[i];
00179         i++;
00180     } while (i < 70 && cuentapadre != hojaraiz->codigo);
00181     actualizado = false;
00182     if (hojaraiz->ramas) {
00183         ActualizarHoja(&(hojaraiz->ramas), cuenta, &actualizado);
00184         if (actualizado) {
00185             hojaraiz->saldoant = hojaraiz->saldoant + Fixed(cuenta->valor("saldoant"));
00186             hojaraiz->debe = hojaraiz->debe + Fixed(cuenta->valor("debe"));
00187             hojaraiz->haber = hojaraiz->haber + Fixed(cuenta->valor("haber"));
00188             hojaraiz->saldo = hojaraiz->saldo + Fixed(cuenta->valor("saldo"));
00189             hojaraiz->debeej = hojaraiz->debeej + Fixed(cuenta->valor("debeej"));
00190             hojaraiz->haberej = hojaraiz->haberej + Fixed(cuenta->valor("haberej"));
00191             hojaraiz->saldoej = hojaraiz->saldoej + Fixed(cuenta->valor("saldoej"));
00192             hojaraiz->numapuntes += cuenta->valor("numapuntes").toInt();
00193         } // end if
00194     } // end if
00195     _depura("END Arbol::actualizahojas", 0);
00196 }
00197 
00198 
00200 
00205 void Arbol::ActualizarHoja(tiporama** ramaraiz, cursor2* cuenta, bool* actualizado) {
00206     _depura("Arbol::ActualizarHoja", 0);
00207     tiporama* rama = *ramaraiz;
00209     while (rama && !(*actualizado)) {
00210         int idcuenta = atoi(cuenta->valor("idcuenta").toAscii().constData());
00211         if (rama->hoja->idcuenta == idcuenta) {
00213             rama->hoja->saldoant = Fixed(cuenta->valor("saldoant"));
00214             rama->hoja->debe = Fixed(cuenta->valor("debe"));
00215             rama->hoja->haber = Fixed(cuenta->valor("haber"));
00216             rama->hoja->saldo = Fixed(cuenta->valor("saldo"));
00217             rama->hoja->debeej = Fixed(cuenta->valor("debeej"));
00218             rama->hoja->haberej = Fixed(cuenta->valor("haberej"));
00219             rama->hoja->saldoej = Fixed(cuenta->valor("saldoej"));
00220             rama->hoja->numapuntes = cuenta->valor("numapuntes").toInt();
00221             *actualizado = true;
00222         } else {
00223             if (rama->hoja->ramas) {
00224                 ActualizarHoja(&(rama->hoja->ramas), cuenta, &(*actualizado));
00226                 if (*actualizado) {
00227                     rama->hoja->saldoant = rama->hoja->saldoant + Fixed(cuenta->valor("saldoant"));
00228                     rama->hoja->debe = rama->hoja->debe + Fixed(cuenta->valor("debe"));
00229                     rama->hoja->haber = rama->hoja->haber + Fixed(cuenta->valor("haber"));
00230                     rama->hoja->saldo = rama->hoja->saldo + Fixed(cuenta->valor("saldo"));
00231                     rama->hoja->debeej = rama->hoja->debeej + Fixed(cuenta->valor("debeej"));
00232                     rama->hoja->haberej = rama->hoja->haberej + Fixed(cuenta->valor("haberej"));
00233                     rama->hoja->saldoej = rama->hoja->saldoej + Fixed(cuenta->valor("saldoej"));
00234                     rama->hoja->numapuntes += cuenta->valor("numapuntes").toInt();
00235                 } // end if
00236             } // end if
00237         } // end if
00238         rama = rama->sgte;
00239     } // end while
00240     _depura("END Arbol::ActualizarHoja", 0);
00241 }
00242 
00243 
00245 
00247 void Arbol::inicia() {
00248     _depura("Arbol::inicia", 0);
00250     hojaactiva = QString("00");
00251     _depura("END Arbol::inicia", 0);
00252 }
00253 
00254 
00256 
00261 bool Arbol::deshoja(unsigned int nivel, bool superiores) {
00262     _depura("Arbol::deshoja", 0);
00263     unsigned int i;
00264     bool deshojada = false;
00265     if (nivel > 2) {
00268         QString raizcandidata = hojaactiva.left(2);
00269         if (hojaactiva == QString("00"))
00270             i = 0;
00271         else {
00272             for(i = 0; i < 70 && raiz[i]; i++) {
00273                 if (raiz[i]->codigo == raizcandidata)
00274                     break;
00275             } // end for
00276         } // end if
00278         while (!deshojada && raiz[i]) {
00281             if (superiores && raiz[i]->codigo > hojaactiva && raiz[i]->numapuntes > 0) {
00282                 hoja = raiz[i];
00285                 if (hoja->numapuntes > 0) {
00286                     hojaactiva = hoja->codigo;
00287                     deshojada = true;
00288                 } // end if
00289             } else {
00290                 if (raiz[i]->ramas)
00292                     Deshojar(raiz[i]->ramas, nivel, superiores, &deshojada);
00293             } // end if
00294             i++;
00295         } // end while
00296     } else {
00297         if (hojaactiva == QString("00")) {
00298             if (raiz[0]) {
00299                 hojaactiva = raiz[0]->codigo;
00300                 hoja = raiz[0];
00301                 deshojada = true;
00302             } // end if
00303         } else {
00304             for (i = 0; i < 70 && raiz[i]; i++) {
00305                 if(raiz[i]->codigo > hojaactiva) {
00306                     hoja = raiz[i];
00309                     if (hoja->numapuntes > 0) {
00310                         hojaactiva = hoja->codigo;
00311                         deshojada = true;
00312                         break;
00313                     }
00314                 } // end if
00315             } // end for
00316         } // end if
00317     } // end if
00318     return deshojada;
00319     _depura("END Arbol::deshoja", 0);
00320 }
00321 
00322 
00324 
00330 void Arbol::Deshojar(tiporama *rama, unsigned int nivel, bool superiores, bool *deshojada) {
00331     _depura("Arbol::Deshojar", 0);
00332     unsigned int nivelhoja = rama->hoja->codigo.length();
00333     if (hojaactiva >= rama->hoja->codigo) {
00334         if (rama->hoja->ramas && nivelhoja < nivel)
00335             Deshojar(rama->hoja->ramas, nivel, superiores, &(*deshojada));
00336         if (rama->sgte && !(*deshojada))
00337             Deshojar(rama->sgte, nivel, superiores, &(*deshojada));
00338     } else {
00340         if (rama->hoja->numapuntes == 0) {
00341             if (rama->sgte)
00342                 Deshojar(rama->sgte, nivel, superiores, &(*deshojada));
00343         } else { 
00344             if (superiores) {
00345                 if (nivelhoja <= nivel) {
00346                     hoja = rama->hoja;
00347                     hojaactiva = hoja->codigo;
00348                     *deshojada = true;
00349                 } else {
00350                     if (rama->sgte)
00357                         Deshojar(rama->sgte, nivel, superiores, &(*deshojada));
00358                 } // end if
00359             } else {
00360                 if (nivelhoja == nivel) {
00361                     hoja = rama->hoja;
00362                     hojaactiva = hoja->codigo;
00363                     *deshojada = true;
00364                 } else {
00365                     if (rama->hoja->ramas && nivelhoja < nivel)
00366                         Deshojar(rama->hoja->ramas, nivel, superiores, &(*deshojada));
00367                     if (rama->sgte && !(*deshojada))
00368                         Deshojar(rama->sgte, nivel, superiores, &(*deshojada));
00369                 } // end if
00370             } // end if
00371         } // end if
00372     } // end if
00373     _depura("END Arbol::Deshojar", 0);
00374 }
00375 
00376 
00378 
00382 QString Arbol::hojaactual(QString valor) {
00383     _depura("Arbol::hojaactual", 0);
00384     QString resultado;
00385     if (valor == "saldoant")
00386         resultado = hoja->saldoant.toQString();
00387     else if (valor == "debe" )
00388         resultado = hoja->debe.toQString();
00389     else if (valor == "haber" )
00390         resultado = hoja->haber.toQString();
00391     else if (valor == "saldo" )
00392         resultado = hoja->saldo.toQString();
00393     else if (valor == "debeej" )
00394         resultado = hoja->debeej.toQString();
00395     else if (valor == "haberej" )
00396         resultado = hoja->haberej.toQString();
00397     else if (valor == "saldoej" )
00398         resultado = hoja->saldoej.toQString();
00399     else if (valor == "codigo" )
00400         resultado = hoja->codigo;
00401     else if (valor == "descripcion" )
00402         resultado = hoja->descripcion;
00403     else if (valor == "idcuenta" )
00404         resultado.setNum(hoja->idcuenta);
00405     else
00406         _depura("Campo " + valor + " no encontrado en la hoja del arbol", 2);
00407     return resultado;
00408     _depura("END Arbol::hojaactual", 0);
00409 }
00410 
00411 
00413 
00418 bool Arbol::irHoja(QString codigo, unsigned int nivel) {
00419     _depura("Arbol::irHoja", 0);
00420     hojaactiva = QString("00");
00421     while (deshoja(nivel, true) && hojaactual("codigo") != codigo);
00422     if (hojaactual("codigo") == codigo) {
00423         return true;
00424     } else {
00425         return false;
00426     } // end if
00427     _depura("END Arbol::irHoja", 0);
00428 }
00429 

Generated on Sat Dec 15 00:01:02 2007 for BulmaGes by  doxygen 1.5.1