estadisticasview.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           estadisticasview.cpp  -  description
00003                              -------------------
00004     begin                : mar jul 15 2003
00005     copyright            : (C) 2003 by Tomeu Borrás Riera
00006     email                : tborras@conetxia.com
00007  ***************************************************************************/
00008 /***************************************************************************
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  ***************************************************************************/
00016 
00017 #include "estadisticasview.h"
00018 #include <qlabel.h>
00019 
00020 #ifdef GDCHART
00021 #include "gdc.h"
00022 #include "gdcpie.h"
00023 #include <gdchart.h>
00024 #endif
00025 
00026 int estadisticasview::inicializa(postgresiface2 *conexion) {
00027     conexionbase = conexion;
00028     presentar();
00029 #ifdef GDCHART
00030    delete m_pie;
00031 #endif
00032 
00033 #ifdef ESTADISTICAS
00034    delete m_imagen;
00035 #endif    
00036     
00037     
00038     return(0);
00039 }// end inicializa
00040 
00041 estadisticasview::estadisticasview(QWidget *parent, const char *name ) : estadisticasdlg(parent,name) {
00042 }
00043 
00044 
00045 estadisticasview::~estadisticasview(){
00046 }
00047 
00048 
00049 void estadisticasview::presentar() {
00050       int j,num1;
00051       QString query;
00052       cursor2 *cursorapt;
00053       QString finicial = "01/01/2003";
00054       QString ffinal = "31/12/2003";
00055       QString cinicial = "10";
00056       QString cfinal = "99";
00057 
00058       // Hacemos la consulta de los apuntes a listar en la base de datos.
00059  //     int idc_coste = ccostes[combocoste->currentItem()];
00060 
00061       // La consulta es compleja, requiere la creación de una tabla temporal y de cierta mandanga por lo que puede
00062                 // Causar problemas con el motor de base de datos.
00063                 fprintf(stderr,"BALANCE: Empezamos a hacer la presentacion\n");
00064       conexionbase->begin();
00065                 query.sprintf("CREATE TEMPORARY TABLE balance AS SELECT cuenta.idcuenta, codigo, nivel(codigo) AS nivel, cuenta.descripcion, padre, tipocuenta ,debe, haber, tdebe, thaber,(tdebe-thaber) AS tsaldo, (debe-haber) AS saldo, adebe, ahaber, (adebe-ahaber) AS asaldo FROM cuenta LEFT JOIN (SELECT idcuenta, sum(debe) AS tdebe, sum(haber) AS thaber FROM apunte WHERE fecha >= '%s' AND fecha<= '%s' GROUP BY idcuenta) AS t1 ON t1.idcuenta = cuenta.idcuenta LEFT JOIN (SELECT idcuenta, sum(debe) AS adebe, sum(haber) AS ahaber FROM apunte WHERE fecha < '%s' GROUP BY idcuenta) AS t2 ON t2.idcuenta = cuenta.idcuenta", finicial.ascii(), ffinal.ascii(), finicial.ascii() );
00066       conexionbase->ejecuta(query);
00067       query.sprintf("UPDATE BALANCE SET padre=0 WHERE padre ISNULL");
00068       conexionbase->ejecuta(query);
00069       query.sprintf("DELETE FROM balance WHERE debe=0 AND haber =0");
00070       conexionbase->ejecuta(query);
00071 
00072       // Para evitar problemas con los nulls hacemos algunos updates
00073       query.sprintf("UPDATE BALANCE SET tsaldo=0 WHERE tsaldo ISNULL");
00074       conexionbase->ejecuta(query);
00075       query.sprintf("UPDATE BALANCE SET tdebe=0 WHERE tdebe ISNULL");
00076       conexionbase->ejecuta(query);
00077       query.sprintf("UPDATE BALANCE SET thaber=0 WHERE thaber ISNULL");
00078       conexionbase->ejecuta(query);
00079       query.sprintf("UPDATE BALANCE SET asaldo=0 WHERE asaldo ISNULL");
00080       conexionbase->ejecuta(query);
00081 
00082 
00083                 query.sprintf("SELECT idcuenta FROM balance ORDER BY padre DESC");
00084                 cursorapt = conexionbase->cargacursor(query,"Balance1view");
00085 
00086       while (!cursorapt->eof())  {
00087          query.sprintf("SELECT * FROM balance WHERE idcuenta=%s",cursorapt->valor("idcuenta").ascii());
00088          cursor2 *mycur = conexionbase->cargacursor(query,"cursorrefresco");
00089 
00090          query.sprintf("UPDATE balance SET tsaldo = tsaldo + (%2.2f), tdebe = tdebe + (%2.2f), thaber = thaber +(%2.2f), asaldo= asaldo+(%2.2f) WHERE idcuenta = %d",atof(mycur->valor("tsaldo").ascii()), atof(mycur->valor("tdebe").ascii()), atof(mycur->valor("thaber").ascii()),atof(mycur->valor("asaldo").ascii()),  atoi(mycur->valor("padre").ascii()));
00091 //                      fprintf(stderr,"%s para el código\n",query, cursorapt->valor("codigo").c_str());
00092                         conexionbase->ejecuta(query);
00093          delete mycur;
00094                         cursorapt->siguienteregistro();
00095                 }// end while
00096                 delete cursorapt;
00097       
00098 
00099       // Borramos todo lo que no es de este nivel
00100       query.sprintf("DELETE FROM balance where nivel(codigo)>%s","2");
00101       conexionbase->ejecuta(query);
00102 
00103       //Borramos todo lo que tiene un hijo en el balance
00104       query.sprintf("DELETE FROM balance WHERE idcuenta IN (SELECT padre FROM balance)");
00105       conexionbase->ejecuta(query);
00106 
00107 
00108                 query.sprintf("SELECT * FROM balance WHERE debe <> 0  OR haber <> 0 ORDER BY codigo");
00109                 cursorapt = conexionbase->cargacursor(query,"mycursor");
00110 
00111 
00112       // Calculamos cuantos registros van a crearse y dimensionamos la tabla.
00113       num1 = cursorapt->numregistros();
00114       j=0;
00115 
00116 #ifdef GDCHART
00117       char *label[1000];
00118       float p[1000];
00119 #endif
00120       while (!cursorapt->eof()) {
00121          // Acumulamos los totales para al final poder escribirlos
00122          float valor =  atof(cursorapt->valor("tsaldo").ascii());
00123          if (valor > 0) {
00124 #ifdef ESTADISTICAS
00125                   m_pie->addValue(valor,cursorapt->valor("descripcion").mid(0,15).ascii());
00126 #endif
00127 #ifdef GDCHART
00128                   label[j]=new char[30];
00129                   strcpy(label[j], cursorapt->valor("descripcion").mid(0,15).ascii());
00130                   p[j]=valor;
00131                   fprintf(stderr,"%s %d", label[j], p[j]);
00132 #endif
00133                   
00134          } else {
00135 #ifdef ESTADISTICAS
00136                   m_pie->addValue(-valor,cursorapt->valor("descripcion").mid(0,15).ascii());
00137 #endif
00138 #ifdef GDCHART
00139                   label[j]=new char[30];
00140                   strcpy(label[j], cursorapt->valor("descripcion").mid(0,15).ascii());
00141                   p[j]=-valor;
00142                   fprintf(stderr,"%s %d", label[j], p[j]);
00143 #endif
00144          } // end if
00145 
00146          // Calculamos la siguiente cuenta registro y finalizamos el bucle
00147          cursorapt->siguienteregistro();
00148          j++;
00149       }// end while
00150 
00151       // Vaciamos el cursor de la base de datos.
00152       delete cursorapt;
00153       query.sprintf("DROP TABLE balance");
00154       conexionbase->ejecuta(query);
00155       conexionbase->commit();
00156 
00157         /* values to chart */
00158               
00159 #ifdef GDCHART                  
00160       fprintf(stderr,"Llamamos a sacapie\n");
00161 //      sacapie(p, label, j);
00162       sacapie(p, label, (j-1>10 )? 10:j-1);
00163 //      sacapie(p, label, 4);
00164       fprintf(stderr,"Hemos terminado sacapie \n");
00165       QPixmap *imag= new QPixmap("/tmp/pie.gif");
00166       fprintf(stderr,"Y ahora hemos creado la imagen\n");
00167       m_imagen->setPixmap(*imag);
00168       fprintf(stderr,"Y ahora la hemos mostrado\n");   
00169       //Destruimos la memoria utilizada
00170       for(int i=0;i<j;i++) delete label[i];
00171       delete imag;
00172 #endif
00173 }// end presentar
00174 
00175 
00176 #ifdef GDCHART
00177 void estadisticasview::sacapie(float *p1,char **lbl1, int numslices1) {
00178 
00179 fprintf(stderr,"Hemos terminado de compilar   \n");
00180         /* labels */
00181         FILE            *fp = fopen( "/tmp/pie.gif", "wb" );
00182         /* labels */
00183         char            *lbl[] = { "CPQ\n(DEC)",
00184                                                    "HP",
00185                                                    "SCO",
00186                                                    "IBM",
00187                                                    "SGI",
00188                                                    "SUN\nSPARC",
00189                                                    "other" }; 
00190         /* values to chart */
00191         float            p[] = { 12.5,
00192                                                  20.1,
00193                                                  2.0,
00194                                                  22.0,
00195                                                  5.0,
00196                                                  18.0,
00197                                                  13.0 };
00198 
00199         /* set which slices to explode, and by how much */
00200         int                             expl[] = { 0, 0, 0, 0, 0, 20, 0 };
00201 
00202         /* set missing slices */
00203         unsigned char   missing[] = { FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE };
00204 
00205         /* colors */
00206         unsigned long   clr[] = { 0xFF4040L, 0x80FF80L, 0x8080FFL, 0xFF80FFL, 0xFFFF80L, 0x80FFFFL, 0x0080FFL };
00207 
00208         /* set options  */
00209         /* a lot of options are set here for illustration */
00210         /* none need be - see gdcpie.h for defaults */
00211         /* GDCPIE_title = "Sample\nPIE"; */
00212         GDCPIE_label_line = TRUE;
00213         GDCPIE_label_dist = 15;                         /* dist. labels to slice edge */
00214                                                                                 /* can be negative */
00215         GDCPIE_LineColor = 0x000000L;
00216         GDCPIE_label_size = GDC_SMALL;
00217         GDCPIE_3d_depth  = 25;
00218         GDCPIE_3d_angle  = 180;                         /* 0 - 359 */
00219         GDCPIE_perspective = 70;                                /* 0 - 99 */
00220         GDCPIE_explode   = expl;                        /* default: NULL - no explosion */
00221         GDCPIE_Color     = clr;
00222         GDCPIE_BGColor   = 0xFFFFFFL;
00223 /*      GDCPIE_EdgeColor = 0x000000L;              default is GDCPIE_NOCOLOR */ 
00224                                                                                 /* for no edging */
00225         GDCPIE_missing   = missing;                     /* default: NULL - none missing */
00226 
00227                                                                                 /* add percentage to slice label */
00228                                                                                 /* below the slice label */
00229         GDCPIE_percent_labels = GDCPIE_PCT_BELOW;
00230         GDC_image_type     = GDC_PNG;
00231         /* call the lib */
00232         GDC_out_pie( 300,                       /* width */
00233                                  200,                   /* height */
00234                                  fp,                    /* open file pointer */
00235                                  GDC_3DPIE,             /* or GDC_2DPIE */
00236                                  7,                             /* number of slices */
00237                                  NULL,                  /* can be NULL */
00238                                  p );                   /* data array */
00239 
00240 fprintf(stderr,"Hemos terminado la imagen\n");
00241         fclose( fp );
00242 //      exit( 0 );
00243 }
00244 #endif
00245 
00246 

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