postgresiface2.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2003 by Tomeu Borras                                    *
00003  *   tborras@conetxia.com                                                  *
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 
00023 #include <QMessageBox>
00024 #include <QApplication>
00025 
00026 #include "postgresiface2.h"
00027 
00028 #include "msgerror.h"
00029 #include "funcaux.h"
00030 
00031 
00033 
00036 bool cursor2::error() {
00037     _depura("cursor2::error", 0);
00038     _depura("END cursor2::error", 0);
00039     return m_error;
00040 }
00041 
00042 
00044 
00047 QString cursor2::query() {
00048     _depura("cursor2::query", 0);
00049     _depura("END cursor2::query", 0);
00050     return m_query;
00051 }
00052 
00053 
00055 
00058 int cursor2::regactual() {
00059     _depura("cursor2::regactual", 0);
00060     _depura("END cursor2::regactual", 0);
00061     return registroactual;
00062 }
00063 
00064 
00073 cursor2::cursor2(QString nombre, PGconn *conn1, QString SQLQuery) {
00074     _depura("cursor2::cursor2", 0, SQLQuery);
00075     try {
00076         conn = conn1;
00077         m_error = FALSE;
00078         m_query = SQLQuery;
00079         nomcursor = nombre;
00080         nregistros = 0;
00081         registroactual = 0;
00082         _depura(SQLQuery, 0);
00083         result = PQexec(conn, SQLQuery.toAscii().data());
00084         switch (PQresultStatus(result)) {
00085         case PGRES_NONFATAL_ERROR:
00086         case PGRES_FATAL_ERROR:
00087         case NULL:
00088             m_error = TRUE;
00089             _depura(PQerrorMessage(conn));
00090             _depura("QUERY command failed [" + SQLQuery + "]", 10);
00091             if (confpr->valor(CONF_ALERTAS_DB) == "Yes") {
00092                 msgError(QString(QObject::tr("Error al hacer la consulta con la base de datos.")) + "\n:: " + QString(PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY)) + " ::", SQLQuery + QString("\n") + (QString) PQerrorMessage(conn));
00093             } // end if
00094             PQclear(result);
00095             throw -1;
00096             break;
00097         default:
00098             break;
00099         } // end switch
00100 
00101         nregistros = PQntuples(result);
00102         ncampos = PQnfields(result);
00103         registroactual = 0;
00104 
00106         m_campos.clear();
00107         for (int i = 0; i < ncampos; i++) {
00108             if (!m_campos.contains(nomcampo(i)))
00109                 m_campos[nomcampo(i)] = i;
00110         } // end for
00111 
00112         _depura("------------ RESULTADO DE LA CONSULTA -----------------");
00113         QString err;
00114         err.sprintf("Numero de registros: %d, Numero de campos: %d", nregistros, ncampos);
00115         _depura(err);
00116         _depura("--------- FIN RESULTADO DE LA CONSULTA ----------------");
00117     } catch (...) {
00118         _depura("cursor2::cursor2: Error en la consulta: " + SQLQuery, 2);
00119         throw -1;
00120     } // end try
00121     _depura("END cursor2::cursor2", 0, " Numero de registros: " + QString::number(nregistros) + ", Numero de campos: " + QString::number(ncampos));
00122 }
00123 
00124 
00126 
00128 cursor2::~cursor2() {
00129     _depura("cursor2::~cursor2", 0);
00130     cerrar();
00131     _depura("END cursor2::~cursor2", 0);
00132 }
00133 
00134 
00136 
00138 void cursor2::cerrar() {
00139     _depura("cursor2::cerrar", 0);
00140     PQclear(result);
00141     _depura("END cursor2::cerrar", 0);
00142 }
00143 
00144 
00146 
00149 int cursor2::numregistros() {
00150     _depura("cursor2::numregistros", 0);
00151     _depura("END cursor2::numregistros", 0);
00152     return nregistros;
00153 }
00154 
00155 
00158 
00161 int cursor2::numcampos() {
00162     _depura("cursor2::numcampos", 0);
00163     _depura("END cursor2::numcampos", 0);
00164     return ncampos;
00165 }
00166 
00167 
00170 
00173 QString cursor2::nomcampo(int campo) {
00174     _depura("cursor2::nomcampo", 0);
00175     _depura("END cursor2::nomcampo", 0);
00176     if (campo >= 0) {
00177         return ((QString)PQfname(result, campo));
00178     } else {
00179         return "--Campo no soportado--";
00180     } // end if
00181 }
00182 
00183 
00191 int cursor2::numcampo(const QString &campo) {
00192     _depura("cursor2::numcampo", 0);
00193     if (m_campos.contains(campo))
00194         return  m_campos.value(campo);
00195     _depura("END cursor2::numcampo", 0);
00196     return -1;
00197 }
00198 
00199 
00206 QString cursor2::valor(int posicion, int registro) {
00207     _depura("cursor2::valor", 0, QString::number(posicion) + QString::number(registro));
00208     if (registro == -1) {
00209         registro = registroactual;
00210     } // end if
00211     _depura("END cursor2::valor", 0);
00212     return (QString::fromUtf8(PQgetvalue(result, registro, posicion)));
00213 }
00214 
00215 
00222 QString cursor2::valor(const QString &campo, int registro) {
00223     _depura("cursor2::valor", 0, campo + " " + QString::number(registro));
00224     int i = 0;
00225     if (registro == -1) {
00226         registro = registroactual;
00227     } // end if
00228     i = numcampo(campo);
00229     if (i == -1)
00230         return "";
00231     _depura("END cursor2::valor ", 0, "campo:" + campo + " ----- Valor:" + PQgetvalue(result, registro, i));
00232     return (QString::fromUtf8(PQgetvalue(result, registro, i)));
00233 }
00234 
00235 
00237 
00240 int cursor2::siguienteregistro() {
00241     _depura("cursor2::siguienteregistro", 0, "Registro actual: " + QString::number(registroactual) + " Numero de registros: " + QString::number(nregistros));
00242     _depura("END cursor2::siguienteregistro", 0);
00243     return ++registroactual;
00244 }
00245 
00246 
00248 
00251 int cursor2::registroanterior() {
00252     _depura("cursor2::registroanterior", 0, "Registro actual: " + QString::number(registroactual) + " Numero de registros: " + QString::number(nregistros));
00253     return --registroactual;
00254 }
00255 
00256 
00258 
00261 int cursor2::primerregistro() {
00262     _depura("cursor2::primerregistro", 0, "Registro actual: " + QString::number(registroactual) + " Numero de registros: " + QString::number(nregistros));
00263     registroactual = 0;
00264     _depura("END cursor2::primerregistro", 0);
00265     return 0;
00266 }
00267 
00268 
00270 
00273 int cursor2::ultimoregistro() {
00274     _depura("cursor2::ultimoregistro", 0, "Registro actual: " + QString::number(registroactual)+" Numero de registros: " + QString::number(nregistros));
00275     registroactual = nregistros - 1;
00276     return registroactual;
00277 }
00278 
00279 
00281 
00284 bool cursor2::eof() {
00285     _depura("cursor2::eof", 0);
00286     if (nregistros == 0) {
00287         return (true);
00288     } // end if
00289     _depura("END cursor2::eof", 0);
00290     return (registroactual >= nregistros);
00291 }
00292 
00293 
00295 
00298 bool cursor2::bof() {
00299     _depura("cursor2::bof", 0);
00300     if (nregistros == 0) {
00301         return(true);
00302     } // end if
00303     _depura("END cursor2::bof", 0);
00304     return (registroactual < 0);
00305 }
00306 
00307 
00309 
00312 bool cursor2::esultimoregistro() {
00313     _depura("cursor2::esultimoregistro", 0);
00314     _depura("END cursor2::esultimoregistro", 0);
00315     return (registroactual == nregistros - 1);
00316 }
00317 
00318 
00320 
00323 bool cursor2::esprimerregistro() {
00324     _depura("cursor2::esprimerregistro", 0);
00325     _depura("END cursor2::esprimerregistro", 0);
00326     return (registroactual == 0);
00327 }
00328 
00329 
00331 
00332 
00334 
00337 QString postgresiface2::nameDB() {
00338     _depura("postgresiface2::nameDB", 0);
00339     _depura("END postgresiface2::nameDB", 0);
00340     return dbName;
00341 }
00342 
00343 
00345 
00347 postgresiface2::postgresiface2() {
00348     _depura("postgresiface2::postgresiface2", 0);
00349     m_transaccion = FALSE;
00350     _depura("END postgresiface2::postgresiface2", 0);
00351 }
00352 
00353 
00355 
00357 void postgresiface2::terminar() {
00358     _depura("postgresiface2::terminar", 0);
00359     PQfinish(conn);
00360     _depura("END postgresiface2::terminar", 0);
00361 }
00362 
00363 
00366 
00368 postgresiface2::~postgresiface2() {
00369     _depura("postgresiface2::~postgresiface2", 0);
00371     PQfinish(conn);
00372     _depura("END postgresiface2::~postgresiface2", 0);
00373 }
00374 
00375 
00383 int postgresiface2::inicializa(QString nomdb) {
00384     _depura("postgresiface2::inicializa", 0, nomdb);
00385     dbName = nomdb;
00386     pghost = confpr->valor(CONF_SERVIDOR); 
00387     pgport = confpr->valor(CONF_PUERTO); 
00388     pgoptions = ""; 
00389     pgtty = ""; 
00390     QString conexion;
00391 
00392     QString user = confpr->valor(CONF_LOGIN_USER);
00393     QString passwd = confpr->valor(CONF_PASSWORD_USER);
00396     if (pghost != "--")
00397         conexion = "host = " + pghost;
00398     conexion += " port = " + pgport;
00399     conexion += " dbname = " + dbName;
00400     if (user != "")
00401         conexion += " user = " + user;
00402     if (passwd != "")
00403         conexion += " password = " + passwd;
00404 
00405     _depura(conexion, 0);
00406     conn = PQconnectdb(conexion.toAscii().data());
00407     if (PQstatus(conn) == CONNECTION_BAD) {
00408         _depura("La conexion con la base de datos '" + dbName + "' ha fallado.\n", 0);
00409         if (passwd != "" && confpr->valor(CONF_ALERTAS_DB) == "Yes") {
00410             _depura(PQerrorMessage(conn), 2);
00411         } else {
00412             _depura(PQerrorMessage(conn), 0);
00413         } // end if
00414         return 1;
00415     } // end if
00416     _depura("La conexion con la base de datos ha ido bien, ahora vamos a por la fecha", 0);
00417     formatofecha();
00418     _depura("END postgresiface2::inicializa", 0, nomdb);
00419     return 0;
00420 }
00421 
00422 
00426 
00428 int postgresiface2::formatofecha() {
00429     _depura("postgresiface2::formatofecha", 0);
00430     QString query = "";
00431     PGresult *res;
00432     query = "SET DATESTYLE TO SQL, European";
00433     res = PQexec(conn, query.toAscii().data());
00434     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
00435         _depura( "Cambio del formato de fecha command failed");
00436     } // end if
00437     PQclear(res);
00438 
00442     query = "SET client_encoding = 'UTF8'";
00443     res = PQexec(conn, query.toAscii().data());
00444     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
00445         _depura( "Cambio del formato de codificacion");
00446     } // end if
00447     PQclear(res);
00448     _depura("END postgresiface2::formatofecha", 0);
00449     return 0;
00450 }
00451 
00452 
00457 int postgresiface2::begin() {
00458     _depura("postgresiface2::begin", 0);
00459     if (m_transaccion) {
00460         _depura("Ya estamos dentro de una transaccion", 0);
00461         return -1;
00462     } // end if
00463     PGresult *res;
00464     res = PQexec(conn, "BEGIN");
00465     if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
00466         _depura("BEGIN command failed");
00467         PQclear(res);
00468         return -1;
00469     } // end if
00470     PQclear(res);
00471     m_transaccion = TRUE;
00472     _depura("END postgresiface2::begin", 0);
00473     return(0);
00474 }
00475 
00476 
00480 
00483 void postgresiface2::commit() {
00484     _depura("postgresiface2::commit", 0);
00485     if (!m_transaccion) {
00486         _depura("No estamos en ninguna transaccion", 0);
00487         return;
00488     } // end if
00489     PGresult *res;
00490     res = PQexec(conn, "COMMIT");
00491     PQclear(res);
00492     m_transaccion = FALSE;
00493     _depura("postgresiface2::commit", 0);
00494 }
00495 
00496 
00500 
00503 void postgresiface2::rollback() {
00504     _depura("postgresiface2::rollback", 0);
00505     if (!m_transaccion) {
00506         _depura("No estamos en ninguna transaccion", 0);
00507         return;
00508     } // end if
00509     PGresult *res;
00510     res = PQexec(conn, "ROLLBACK");
00511     PQclear(res);
00512     m_transaccion = FALSE;
00513     _depura("END postgresiface2::rollback", 0);
00514 }
00515 
00516 
00521 
00525 cursor2 *postgresiface2::cargacursor(QString query, QString nomcursor, int limit, int offset) {
00526     _depura ("postgresiface2::cargacursor", 0, query);
00527     cursor2 *cur = NULL;
00528     try {
00530         if (limit != 0)
00531             query += " LIMIT " + QString::number(limit);
00532         if (offset != 0)
00533             query += " OFFSET " + QString::number(offset);
00534 
00535         cur = new cursor2(nomcursor, conn, query);
00536     } catch (...) {
00537         _depura("postgresiface2::cargacursor La base de datos genero un error: ", 0);
00538         delete cur;
00539 //        throw -1;
00540         return NULL;
00541     } // end try
00542     _depura ("END postgresiface2::cargacursor", 0, nomcursor);
00543     return cur;
00544 }
00545 
00546 
00552 
00553 #include <qtextcodec.h>
00554 
00556 
00559 int postgresiface2::ejecuta(QString Query) {
00560     _depura("postgresiface2::ejecuta", 0, Query);
00561     PGresult *result = NULL;
00562     try {
00564         if (confpr->valor(CONF_PRIVILEGIOS_USUARIO) != "1" && (Query.left(6) == "DELETE" || Query.left(6) == "UPDATE" || Query.left(6) == "INSERT"))
00565             throw 42501;
00567         result = PQexec(conn, (const char *) Query.toUtf8());
00568         if (!result || PQresultStatus(result) != PGRES_COMMAND_OK)
00569             throw -1;
00570         PQclear(result);
00571         _depura("postgresiface2::ejecuta", 0);
00572         return 0;
00573     } catch (int e) {
00574         if (e == 42501) {
00575             _depura("SQL command failed: " + Query);
00576             fprintf(stderr,"%s\n", PQerrorMessage(conn));
00577             QString mensaje = "No tiene permisos suficientes para ejecutar el comando SQL:\n";
00578             msgError(mensaje + (QString)PQerrorMessage(conn), Query + "\n" + (QString)PQerrorMessage(conn));
00579             PQclear(result);
00580             throw -1;
00581         } else {
00582             _depura("SQL command failed: " + Query);
00583             fprintf(stderr,"%s\n", PQerrorMessage(conn));
00584             QString mensaje = "Error al intentar modificar la base de datos:\n";
00585             msgError(mensaje + (QString)PQerrorMessage(conn), Query + "\n" + (QString)PQerrorMessage(conn));
00586             PQclear(result);
00587             throw -1;
00588         } // end if
00589     } catch (...) {
00590         throw -1;
00591     } // end try
00592     _depura("END postgresiface2::ejecuta", 0, Query);
00593 }
00594 
00595 
00599 
00603 QString postgresiface2::searchParent(QString cod) {
00604     _depura("postgresiface2::searchParent", 0);
00605     QString padre = "NULL"; 
00606     QString query;
00607     int i = 2;
00608     int fin = 0;
00609     while (!fin) {
00610         query = "SELECT * FROM cuenta WHERE codigo = '" + cod.left(i) + "'";
00611         begin();
00612         cursor2 *cur = cargacursor(query, "unquery");
00613         commit();
00614         if (!cur->eof()) {
00615             padre = cur->valor("codigo");
00616         } else {
00617             fin = 1;
00618         } // end if
00619         delete cur;
00620         i++;
00621     } // end while
00622     _depura("END postgresiface2::searchParent", 0);
00623     return padre;
00624 }
00625 
00626 
00628 
00642 int postgresiface2::nuevoborrador(int idcuenta, int idasiento, QString concepto, QString descripcion, float debe, float haber, QString fecha, int idcontrapartida, int idtipoiva, int idccoste, int idcanal) {
00643     _depura("postgresiface2::nuevoborrador", 0);
00644     QString query = "";
00645     QString textcuenta;
00646     QString textcontrapartida;
00647     QString textidccoste;
00648     QString textidcanal;
00649     if (idcuenta == 0) {
00650         textcuenta = "NULL";
00651     } else {
00652         textcuenta.sprintf("%d", idcuenta);
00653     } // end if
00654     if (idcontrapartida == 0) {
00655         textcontrapartida = "NULL";
00656     } else {
00657         textcontrapartida.sprintf("%d", idcontrapartida);
00658     } // end if
00659     if (idccoste == 0) {
00660         textidccoste = "NULL";
00661     } else {
00662         textidccoste.sprintf("%d", idccoste);
00663     } // end if
00664     if (idcanal == 0) {
00665         textidcanal = "NULL";
00666     } else {
00667         textidcanal.sprintf("%d", idcanal);
00668     } // end if
00669     query.sprintf("INSERT INTO borrador (idcuenta,idasiento,conceptocontable, descripcion, debe, haber, fecha, contrapartida, idtipoiva, idc_coste, idcanal) VALUES (%s, %d,'%s','%s', %2.2f, %2.2f,'%s', %s, %d, %s, %s)",
00670                   sanearCadena(textcuenta).toAscii().data(),
00671                   idasiento,
00672                   sanearCadena(concepto).toAscii().data(),
00673                   sanearCadena(descripcion).toAscii().data(),
00674                   debe,
00675                   haber,
00676                   sanearCadena(fecha).toAscii().data(),
00677                   sanearCadena(textcontrapartida).toAscii().data(),
00678                   idtipoiva,
00679                   sanearCadena(textidccoste).toAscii().data(),
00680                   sanearCadena(textidcanal).toAscii().data());
00681     _depura("END postgresiface2::nuevoborrador", 0);
00682     return (ejecuta(query));
00683 }
00684 
00685 
00688 
00701 int postgresiface2::modificaborrador(int idborrador, int idcuenta, float idebe, float ihaber, QString concepto, QString fecha, int contrapartida, int idtipoiva, int idccoste, int idcanal) {
00702     _depura("postgresiface2::modificaborrador", 0);
00703     QString query = "";
00704     QString textidccoste;
00705     QString textcontrapartida;
00706     QString textocanal;
00707     if (idccoste == 0) {
00708         textidccoste = "NULL";
00709     } else {
00710         textidccoste.sprintf("%d", idccoste);
00711     } // end if
00712     if (contrapartida == 0) {
00713         textcontrapartida = "NULL";
00714     } else {
00715         textcontrapartida.sprintf("%d", contrapartida);
00716     } // end if
00717     if (idcanal == 0) {
00718         textocanal = "NULL";
00719     } else {
00720         textocanal.sprintf("%d", idcanal);
00721     } // end if
00722 
00723     query.sprintf("UPDATE borrador SET idcuenta = %d, debe = %2.2f, haber = %2.2f, conceptocontable = '%s', fecha = '%s', contrapartida = %s, idtipoiva = %d, idc_coste = %s, idcanal = %s WHERE idborrador = %d", idcuenta, idebe, ihaber, concepto.toAscii().data(), fecha.toAscii().data(), textcontrapartida.toAscii().data(), idtipoiva, textidccoste.toAscii().data(), textocanal.toAscii().data(), idborrador);
00724     _depura(query);
00725     _depura("END postgresiface2::modificaborrador", 0);
00726     return(ejecuta(query));
00727 }
00728 
00729 
00733 
00738 cursor2 *postgresiface2::cargacuenta(int idcuenta, QString ccuenta) {
00739     _depura("postgresiface2::cargacuenta", 0);
00740     QString query = "";
00741     if ( idcuenta != 0) {
00742         query.sprintf("SELECT * FROM cuenta WHERE idcuenta = %d", idcuenta);
00743     }  else  {
00744         query.sprintf("SELECT * FROM cuenta WHERE codigo LIKE '%s' ORDER BY codigo", ccuenta.toAscii().data());
00745     } // end if
00746     cursor2 *cur = cargacursor(query, "cargacuenta");
00747     _depura("END postgresiface2::cargacuenta", 0);
00748     return cur;
00749 }
00750 
00751 
00753 
00757 cursor2 *postgresiface2::cargaasiento(int idasiento) {
00758     _depura("postgresiface2::cargaasiento", 0);
00759     QString query = "";
00760     query.sprintf("SELECT * FROM asiento WHERE idasiento = %d", idasiento);
00761     cursor2 *cur = cargacursor(query, "cargaasiento");
00762     _depura("END postgresiface2::cargaasiento", 0);
00763     return cur;
00764 }
00765 
00766 
00769 
00773 cursor2 *postgresiface2::cargaapuntes(int tidasiento) {
00774     _depura("postgresiface2::cargaapuntes", 0);
00775     QString query = "";
00776     query.sprintf("SELECT * FROM apunte where idasiento = %d ORDER BY idapunte", tidasiento);
00777     cursor2 *cur = cargacursor(query, "cargaapuntes");
00778     _depura("END postgresiface2::cargaapuntes", 0);
00779     return cur;
00780 }
00781 
00782 
00785 
00789 cursor2 *postgresiface2::cargaborradores(int tidasiento) {
00790     _depura("postgresiface2::cargaborradores", 0);
00791     QString query = "";
00792     query.sprintf("SELECT * FROM borrador where idasiento = %d ORDER BY idborrador", tidasiento);
00793     cursor2 *cur = cargacursor(query, "cargaborradores");
00794     _depura("END postgresiface2::cargaborradores", 0);
00795     return cur;
00796 }
00797 
00798 
00803 
00807 cursor2 *postgresiface2::cargacuentas(int padre) {
00808     _depura("postgresiface2::cargacuentas", 0);
00809     QString query = "";
00810     if (padre != 0 && padre != -1 && padre != -2) {
00811         query.sprintf("SELECT * FROM cuenta WHERE padre=%d ORDER BY padre", padre);
00812     } else if (padre == 0) {
00813         query.sprintf("SELECT * FROM cuenta WHERE padre isnull ORDER BY padre ");
00814     } else if (padre == -1) {
00815         query.sprintf("SELECT * FROM cuenta ORDER BY padre ");
00816     } else if (padre == -2) {
00817         query.sprintf("SELECT * FROM cuenta WHERE NOT padre isnull ORDER BY padre ");
00818     }// end if
00819     cursor2 *cur = cargacursor(query, "cargaborradores");
00820     _depura("END postgresiface2::cargacuentas", 0);
00821     return cur;
00822 }
00823 
00824 
00826 
00829 cursor2 *postgresiface2::cargagrupos() {
00830     _depura("postgresiface2::cargagrupos", 0);
00831     QString query = "SELECT * FROM grupo";
00832     cursor2 *cur = cargacursor(query, "cargagrupos");
00833     _depura("END postgresiface2::cargagrupos", 0);
00834     return cur;
00835 }
00836 
00837 
00840 
00846 cursor2 *postgresiface2::cargaapuntesctafecha(int tidcuenta, QString fechainicial, QString fechafinal) {
00847     _depura("ostgresiface2::cargaapuntesctafecha", 0);
00848     QString query = "";
00849     query.sprintf("SELECT * FROM apunte where idcuenta = %d AND fecha >= '%s' AND fecha <= '%s' ORDER BY fecha",tidcuenta, fechainicial.toAscii().data(), fechafinal.toAscii().data());
00850     cursor2 *cur = cargacursor(query, "cargasaldoscuentafecha");
00851     _depura("END ostgresiface2::cargaapuntesctafecha", 0);
00852     return cur;
00853 }
00854 
00855 
00858 
00863 cursor2 *postgresiface2::cargasaldoscuentafecha(int idcuenta, QString fecha) {
00864     _depura("postgresiface2::cargasaldoscuentafecha", 0);
00865     QString query = "";
00866     query.sprintf("SELECT sum(debe) as tdebe, sum(haber)as thaber FROM apunte WHERE idcuenta = %d AND fecha <'%s'",idcuenta, fecha.toAscii().data());
00867     cursor2 *cur = cargacursor(query, "cargasaldoscuentafecha");
00868     _depura("END postgresiface2::cargasaldoscuentafecha", 0);
00869     return(cur);
00870 }
00871 
00872 
00874 
00879 cursor2 *postgresiface2::cargaasientosfecha(QString fechini, QString fechfin) {
00880     _depura("postgresiface2::cargaasientosfecha", 0);
00881     QString query = "";
00882     query.sprintf("SELECT * FROM asiento WHERE fecha >= '%s' AND fecha <= '%s' ORDER BY fecha", fechini.toAscii().data(),fechfin.toAscii().data());
00883     cursor2 *cur=cargacursor(query, "cargaasientosfecha");
00884     _depura("END postgresiface2::cargaasientosfecha", 0);
00885     return cur;
00886 }
00887 
00888 
00895 
00901 cursor2 *postgresiface2::cargacuentascodigo(int padre, QString codigoinicial, QString codigofinal) {
00902     _depura("postgresiface2::cargacuentascodigo", 0);
00903     QString query = "";
00904     if (padre != 0 && padre != -1) {
00905         query.sprintf("SELECT * FROM cuenta WHERE padre = %d AND codigo >= '%s' AND codigo <= '%s' ORDER BY codigo", padre, codigoinicial.toAscii().data(), codigofinal.toAscii().data());
00906     } else if (padre== 0) {
00907         query.sprintf("SELECT * FROM cuenta WHERE padre isnull AND codigo >= '%s' AND codigo <= '%s' ORDER BY codigo", codigoinicial.toAscii().data(), codigofinal.toAscii().data());
00908     } else if (padre ==-1) {
00909         query.sprintf("SELECT * FROM cuenta WHERE codigo >= '%s' AND codigo <= '%s' ORDER BY codigo", codigoinicial.toAscii().data(), codigofinal.toAscii().data());
00910     } // end if
00911     cursor2 *cur = cargacursor(query, "cargasaldoscuentafecha");
00912     _depura("END postgresiface2::cargacuentascodigo", 0);
00913     return cur;
00914 }
00915 
00916 
00918 
00922 int postgresiface2::cierraasiento(int idasiento) {
00923     _depura("postgresiface2::cierraasiento", 0);
00924     QString query;
00925     query.sprintf("SELECT cierraasiento(%d)", idasiento);
00926     cursor2 *cur = cargacursor(query, "abreasientos");
00927     delete cur;
00928     _depura("END postgresiface2::cierraasiento", 0);
00929     return 1;
00930 }
00931 
00932 
00936 
00940 int postgresiface2::borrarasiento(int idasiento) {
00941     _depura("postgresiface2::borrarasiento", 0);
00942     QString query = "";
00943     query.sprintf("DELETE FROM asiento WHERE idasiento = %d", idasiento);
00944     _depura("END postgresiface2::borrarasiento", 0);
00945     return (ejecuta(query));
00946 }
00947 
00948 
00951 
00955 int postgresiface2::borrarborrador(int idborrador) {
00956     _depura("postgresiface2::borrarborrador", 0);
00957     QString query = "";
00958     query.sprintf("DELETE FROM borrador WHERE idborrador = %d", idborrador);
00959     _depura("END postgresiface2::borrarborrador", 0);
00960     return (ejecuta(query));
00961 }
00962 
00963 
00966 
00970 int postgresiface2::borrarcuenta(int idcuenta) {
00971     _depura("postgresiface2::borrarcuenta", 0);
00972     QString query = "";
00973     query.sprintf("DELETE FROM cuenta WHERE idcuenta = %d", idcuenta);
00974     _depura("END postgresiface2::borrarcuenta", 0);
00975     return (ejecuta(query));
00976 }
00977 
00978 
00981 
00985 int postgresiface2::abreasiento(int idasiento) {
00986     _depura("postgresiface2::abreasiento", 0);
00987     _depura("Funcion abreasiento\n");
00988     QString query = "";
00989     query.sprintf("SELECT abreasiento(%d)", idasiento);
00990     cursor2 *cur = cargacursor(query, "abreasientos");
00991     delete cur;
00992     _depura("END postgresiface2::abreasiento", 0);
00993     return 1;
00994 }
00995 
00997 
01019 int postgresiface2::modificacuenta(int idcuenta, QString desccuenta, QString codigo, bool cimputacion, bool cbloqueada, int idgrupo, bool cactivo, QString nombreent, QString cifent, QString dir, QString cp, QString tel, QString comm, QString banco, QString email, QString web, int tipocuenta, bool cnodebe, bool cnohaber) {
01020     _depura("postgresiface2::modificacuenta", 0);
01021     QString cadena;
01022     cadena.sprintf("%d", idcuenta);
01023     QString query = "";
01024     QString bloqueada = cbloqueada ? (char *) "TRUE" : (char *) "FALSE";
01025     QString activo = cactivo ?  "TRUE" :  "FALSE";
01026     QString imputacion = cimputacion ?  "TRUE" :  "FALSE";
01027     QString nodebe = cnodebe ? "TRUE" : "FALSE";
01028     QString nohaber = cnohaber ? "TRUE" : "FALSE";
01029     query.sprintf("UPDATE cuenta SET descripcion = '%s', codigo = '%s', imputacion = %s, bloqueada = %s, idgrupo = %d, activo = %s, nombreent_cuenta = '%s', cifent_cuenta = '%s', dirent_cuenta = '%s', cpent_cuenta = '%s', telent_cuenta = '%s', coment_cuenta = '%s', bancoent_cuenta = '%s', emailent_cuenta = '%s', webent_cuenta = '%s', tipocuenta = %d, nodebe = %s, nohaber = %s WHERE idcuenta = %d\n", desccuenta.toAscii().data(), codigo.toAscii().data(), imputacion.toAscii().data(), bloqueada.toAscii().data(),idgrupo, activo.toAscii().data(), nombreent.toAscii().data(), cifent.toAscii().data(), dir.toAscii().data(), cp.toAscii().data(), tel.toAscii().data(), comm.toAscii().data(), banco.toAscii().data(), email.toAscii().data(), web.toAscii().data(), tipocuenta,nodebe.toAscii().data(), nohaber.toAscii().data(), idcuenta);
01030     _depura(query);
01031     _depura("END postgresiface2::modificacuenta", 0);
01032     return (ejecuta(query));
01033 }
01034 
01035 
01037 
01056 int postgresiface2::nuevacuenta(QString desccuenta, QString codigo, int padre, int idgrupo, QString nombreent, QString cifent, QString dir, QString cp, QString tel, QString comm, QString banco, QString email, QString web, int tipocuenta, bool cnodebe, bool cnohaber) {
01057     _depura("postgresiface2::nuevacuenta", 0);
01058     QString query = "";
01059     QString tpadre;
01060     if (padre == 0) {
01061         tpadre = "NULL";
01062     } else {
01063         tpadre.sprintf("%d", padre);
01064     } // end if
01065     QString nodebe = cnodebe ? "TRUE" : "FALSE";
01066     QString nohaber = cnohaber ? "TRUE" : "FALSE";
01067 
01068     query.sprintf("INSERT INTO cuenta (descripcion, padre,codigo, idgrupo, nombreent_cuenta, cifent_cuenta, dirent_cuenta, cpent_cuenta, telent_cuenta, coment_cuenta, bancoent_cuenta, emailent_cuenta, webent_cuenta, tipocuenta, nodebe, nohaber) VALUES('%s',%s,'%s',%d, '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %s, %s)",
01069                   sanearCadena(desccuenta).toAscii().data(),
01070                   sanearCadena(tpadre).toAscii().data(),
01071                   sanearCadena(codigo).toAscii().data(),
01072                   idgrupo,
01073                   sanearCadena(nombreent).toAscii().data(),
01074                   sanearCadena(cifent).toAscii().data(),
01075                   sanearCadena(dir).toAscii().data(),
01076                   sanearCadena(cp).toAscii().data(),
01077                   sanearCadena(tel).toAscii().data() ,
01078                   sanearCadena(comm).toAscii().data(),
01079                   sanearCadena(banco).toAscii().data(),
01080                   sanearCadena(email).toAscii().data(),
01081                   sanearCadena(web).toAscii().data(),
01082                   tipocuenta,
01083                   sanearCadena(nodebe).toAscii().data(),
01084                   sanearCadena(nohaber).toAscii().data());
01085     _depura("END postgresiface2::nuevacuenta", 0);
01086     return (ejecuta(query));
01087 }
01088 
01089 
01091 
01094 cursor2 *postgresiface2::cargaempresas() {
01095     _depura("postgresiface2::cargaempresas", 0);
01096     QString query;
01097     query = "SELECT * FROM empresa";
01098     cursor2 *cur = cargacursor(query, "cargaempresas");
01099     _depura("END postgresiface2::cargaempresas", 0);
01100     return cur;
01101 }
01102 
01103 
01107 
01111 QString postgresiface2::sanearCadena(QString cadena) {
01112     _depura("postgresiface2::sanearCadena", 0);
01113     int longitud = 0;
01114     char *buffer = "";
01115     QString cadenaLimpia = "";
01116     longitud = cadena.size();
01119     buffer = (char *) malloc((sizeof(char) * longitud * 2) + 1);
01120     PQescapeString(buffer, cadena.toAscii().constData(), cadena.toAscii().size());
01121     cadenaLimpia = QString::fromAscii(buffer);
01122     free(buffer);
01123     _depura("END postgresiface2::sanearCadena", 0);
01124     return cadenaLimpia;
01125 }
01126 
01127 
01133 QString postgresiface2::propiedadempresa(QString nombre) {
01134     _depura("postgresiface2::propiedadempresa", 0);
01135     PGresult *result;
01136     QString value;
01137     int num;
01138     QString Query = "select * from configuracion where nombre = '" + nombre + "'";
01139     fprintf(stderr, "%s\n", Query.toAscii().data());
01140     result = PQexec(conn, Query.toAscii().data());
01141     if (!result || PQresultStatus(result) != PGRES_TUPLES_OK) {
01142         fprintf(stderr, "SQL command failed: %s\n", Query.toAscii().data());
01143         fprintf(stderr, "%s\n", PQerrorMessage(conn));
01144         PQclear(result);
01145         return "";
01146     } // end if
01147     num = PQntuples(result);
01148     if (num > 1) {
01149         fprintf(stderr, "Aviso: Hay %d valores para el campo %s en la tabla configuracion\n", num, nombre.toAscii().data());
01150     } // end if
01151     if (num == 0) {
01152         value = "";
01153     } else {
01154         value = PQgetvalue(result, 0, 2);
01155     } // end if
01156     PQclear(result);
01157     _depura("END postgresiface2::propiedadempresa", 0);
01158     return value;
01159 }
01160 
01161 
01167 bool postgresiface2::has_table_privilege(QString tabla, QString permiso) {
01168     _depura("postgresiface2::has_table_privilege", 0);
01170     cursor2 *cur = cargacursor("SELECT has_table_privilege('" + tabla + "', '" + permiso + "') AS pins");
01171     bool privileges = FALSE;
01172     if (cur) {
01173         if (cur->valor("pins") == "t") {
01174             privileges = TRUE;
01175         } // end if
01176         delete cur;
01177     } // end if
01178     _depura("END postgresiface2::has_table_privilege", 0);
01179     return privileges;
01180 }
01181 

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