00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "estadisticasview.h"
00018 #include <qlabel.h>
00019 #include <q3popupmenu.h>
00020 #include <qaction.h>
00021 #include <qobject.h>
00022 #include <qmessagebox.h>
00023
00024 #include <QPixmap>
00025
00026 int estadisticasview::inicializa(postgresiface2 *conexion, int tipo) {
00027 conexionbase = conexion;
00028 if (tipo ==0)
00029 presentar();
00030 else
00031 presentarbarras();
00032 return(0);
00033 }
00034
00035 estadisticasview::estadisticasview(QWidget *parent, const char *name ) : estadisticasdlg(parent,name) {}
00036
00037
00038 estadisticasview::~estadisticasview() {}
00039
00040
00041 void estadisticasview::presentar() {
00042 QString query;
00043 cursor2 *cursorapt;
00044 QString finicial = "01/01/2003";
00045 QString ffinal = "31/12/2005";
00046 QString cinicial = "10";
00047 QString cfinal = "99";
00048
00049
00050
00051 fprintf(stderr,"BALANCE: Empezamos a hacer la presentacion\n");
00052 conexionbase->begin();
00053 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() );
00054 conexionbase->ejecuta(query);
00055 query.sprintf("UPDATE BALANCE SET padre=0 WHERE padre ISNULL");
00056 conexionbase->ejecuta(query);
00057 query.sprintf("DELETE FROM balance WHERE debe=0 AND haber =0");
00058 conexionbase->ejecuta(query);
00059
00060
00061 query.sprintf("UPDATE BALANCE SET tsaldo=0 WHERE tsaldo ISNULL");
00062 conexionbase->ejecuta(query);
00063 query.sprintf("UPDATE BALANCE SET tdebe=0 WHERE tdebe ISNULL");
00064 conexionbase->ejecuta(query);
00065 query.sprintf("UPDATE BALANCE SET thaber=0 WHERE thaber ISNULL");
00066 conexionbase->ejecuta(query);
00067 query.sprintf("UPDATE BALANCE SET asaldo=0 WHERE asaldo ISNULL");
00068 conexionbase->ejecuta(query);
00069
00070
00071 query.sprintf("SELECT idcuenta FROM balance ORDER BY padre DESC");
00072 cursorapt = conexionbase->cargacursor(query,"Balance1view");
00073
00074 while (!cursorapt->eof()) {
00075 query.sprintf("SELECT * FROM balance WHERE idcuenta=%s",cursorapt->valor("idcuenta").ascii());
00076 cursor2 *mycur = conexionbase->cargacursor(query,"cursorrefresco");
00077
00078 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()));
00079
00080 conexionbase->ejecuta(query);
00081 delete mycur;
00082 cursorapt->siguienteregistro();
00083 }
00084 delete cursorapt;
00085
00086
00087
00088 query.sprintf("DELETE FROM balance where nivel(codigo)>%s","2");
00089 conexionbase->ejecuta(query);
00090
00091
00092 query.sprintf("DELETE FROM balance WHERE idcuenta IN (SELECT padre FROM balance)");
00093 conexionbase->ejecuta(query);
00094
00095
00096 query.sprintf("SELECT descripcion, abs(tsaldo)::integer AS tsaldoi FROM balance WHERE debe <> 0 OR haber <> 0 ORDER BY codigo");
00097 cursorapt = conexionbase->cargacursor(query,"mycursor");
00098
00099 QString valores="data=[";
00100 int init=0;
00101
00102
00103 while (!cursorapt->eof()) {
00104 if (init != 0) valores += ",";
00105
00106 valores+="(\\\""+cursorapt->valor("descripcion")+"\\\","+QString::number(cursorapt->valor("tsaldoi").toInt())+")";
00107 cursorapt->siguienteregistro();
00108 init=1;
00109 }
00110 valores+="]";
00111
00112
00113 delete cursorapt;
00114 query.sprintf("DROP TABLE balance");
00115 conexionbase->ejecuta(query);
00116 conexionbase->commit();
00117
00118
00119 fprintf(stderr,"Llamamos a sacapie\n");
00120 system("cp /usr/lib/bulmages/plugins/plotpie.py /tmp");
00121 QString cadena = " sed -e \"s&###MARCA###&"+valores+"&g\" /tmp/plotpie.py > /tmp/plotpie1.py";
00122 system (cadena.ascii());
00123 system("python /tmp/plotpie1.py --format=png > /tmp/pie.png");
00124
00125
00126
00127 fprintf(stderr,"Hemos terminado sacapie \n");
00128 QPixmap *imag= new QPixmap("/tmp/pie.png");
00129 fprintf(stderr,"Y ahora hemos creado la imagen\n");
00130 m_imagen->setPixmap(*imag);
00131 fprintf(stderr,"Y ahora la hemos mostrado\n");
00132
00133 delete imag;
00134
00135 }
00136
00137
00138
00139 void estadisticasview::presentarbarras() {
00140 QString query;
00141 cursor2 *cursorapt;
00142 QString finicial = "01/01/2003";
00143 QString ffinal = "31/12/2005";
00144 QString cinicial = "10";
00145 QString cfinal = "99";
00146
00147
00148
00149 fprintf(stderr,"BALANCE: Empezamos a hacer la presentacion\n");
00150 conexionbase->begin();
00151 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() );
00152 conexionbase->ejecuta(query);
00153 query.sprintf("UPDATE BALANCE SET padre=0 WHERE padre ISNULL");
00154 conexionbase->ejecuta(query);
00155 query.sprintf("DELETE FROM balance WHERE debe=0 AND haber =0");
00156 conexionbase->ejecuta(query);
00157
00158
00159 query.sprintf("UPDATE BALANCE SET tsaldo=0 WHERE tsaldo ISNULL");
00160 conexionbase->ejecuta(query);
00161 query.sprintf("UPDATE BALANCE SET tdebe=0 WHERE tdebe ISNULL");
00162 conexionbase->ejecuta(query);
00163 query.sprintf("UPDATE BALANCE SET thaber=0 WHERE thaber ISNULL");
00164 conexionbase->ejecuta(query);
00165 query.sprintf("UPDATE BALANCE SET asaldo=0 WHERE asaldo ISNULL");
00166 conexionbase->ejecuta(query);
00167
00168
00169 query.sprintf("SELECT idcuenta FROM balance ORDER BY padre DESC");
00170 cursorapt = conexionbase->cargacursor(query,"Balance1view");
00171
00172 while (!cursorapt->eof()) {
00173 query.sprintf("SELECT * FROM balance WHERE idcuenta=%s",cursorapt->valor("idcuenta").ascii());
00174 cursor2 *mycur = conexionbase->cargacursor(query,"cursorrefresco");
00175
00176 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()));
00177
00178 conexionbase->ejecuta(query);
00179 delete mycur;
00180 cursorapt->siguienteregistro();
00181 }
00182 delete cursorapt;
00183
00184
00185
00186 query.sprintf("DELETE FROM balance where nivel(codigo)>%s","2");
00187 conexionbase->ejecuta(query);
00188
00189
00190 query.sprintf("DELETE FROM balance WHERE idcuenta IN (SELECT padre FROM balance)");
00191 conexionbase->ejecuta(query);
00192
00193
00194 query.sprintf("SELECT descripcion, tsaldo::integer AS tsaldoi FROM balance WHERE debe <> 0 OR haber <> 0 ORDER BY codigo");
00195 cursorapt = conexionbase->cargacursor(query,"mycursor");
00196
00197 QString valores="data=[(";
00198 QString plot="";
00199 int init=0;
00200 int iplot=1;
00201
00202 while (!cursorapt->eof()) {
00203 if (init != 0) valores += ",";
00204
00205 valores+=QString::number(cursorapt->valor("tsaldoi").toInt());
00206
00207 plot += "plot"+QString::number(iplot)+"=bar_plot.T(label=\\\""+cursorapt->valor("descripcion")+"\\\", hcol="+QString::number(iplot-1)+", cluster=("+QString::number(iplot-1)+",3)) \\n";
00208 iplot++;
00209 cursorapt->siguienteregistro();
00210 init=1;
00211 }
00212 valores+=")]";
00213 plot+="ar.add_plot(plot1";
00214 for (int i=2;i<iplot;i++) {
00215 plot += ",plot"+QString::number(i);
00216 }
00217 plot+=")\\n";
00218
00219
00220
00221 delete cursorapt;
00222 query.sprintf("DROP TABLE balance");
00223 conexionbase->ejecuta(query);
00224 conexionbase->commit();
00225
00226
00227 fprintf(stderr,"Llamamos a sacapie\n");
00228 system("cp /usr/lib/bulmages/plugins/plotbars.py /tmp");
00229 QString cadena = " sed -e \"s&###DATA###&"+valores+"&g\" /tmp/plotbars.py > /tmp/plotbars1.py";
00230 system (cadena.ascii());
00231 cadena = " sed -e \"s&###PLOT###&"+plot+"&g\" /tmp/plotbars1.py > /tmp/plotbars2.py";
00232 fprintf(stderr,"%s\n",cadena.ascii());
00233 system (cadena.ascii());
00234 system("python /tmp/plotbars2.py --format=png > /tmp/pie.png");
00235
00236
00237
00238 fprintf(stderr,"Hemos terminado sacapie \n");
00239 QPixmap *imag= new QPixmap("/tmp/pie.png");
00240 fprintf(stderr,"Y ahora hemos creado la imagen\n");
00241 m_imagen->setPixmap(*imag);
00242 fprintf(stderr,"Y ahora la hemos mostrado\n");
00243
00244 delete imag;
00245
00246 }
00247
00248
00249
00250 myplugin:: myplugin() {}
00251 myplugin::~myplugin() {}
00252
00253
00254 void myplugin::BalanceGrafico() {
00255
00256 estadisticasview *est = new estadisticasview(0,0);
00257 est->inicializa(conexionbase,0);
00258 est->exec();
00259 delete est;
00260 }
00261
00262 void myplugin::BalanceBarras() {
00263
00264 estadisticasview *est = new estadisticasview(0,0);
00265 est->inicializa(conexionbase,1);
00266 est->exec();
00267 delete est;
00268 }
00269
00270