00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <QTextStream>
00022 #include <QLineEdit>
00023 #include <QLocale>
00024
00025 #include "canualesprintview.h"
00026 #include "funcaux.h"
00027 #include "empresa.h"
00028 #include "fixed.h"
00029 #include "postgresiface2.h"
00030 #include "arbol.h"
00031 #include "asiento1view.h"
00032
00033 #ifndef WIN32
00034 #include <unistd.h>
00035 #include <sys/types.h>
00036 #include <sys/wait.h>
00037 #endif
00038
00039
00041
00045 CAnualesPrintView::CAnualesPrintView ( Empresa *emp, QWidget *parent )
00046 : QDialog ( parent ), PEmpresaBase(emp)
00047 {
00048 _depura ( "CAnualesPrintView::CAnualesPrintView", 0 );
00049 setupUi ( this );
00050 fichero = NULL;
00051 _depura ( "END CAnualesPrintView::CAnualesPrintView", 0 );
00052
00053 }
00054
00055
00057
00059 CAnualesPrintView::~CAnualesPrintView()
00060 {
00061 _depura ( "CAnualesPrintView::~CAnualesPrintView", 0 );
00062 _depura ( "END CAnualesPrintView::~CAnualesPrintView", 0 );
00063 }
00064
00065
00068
00081 void CAnualesPrintView::on_mui_aceptar_clicked()
00082 {
00083 _depura ( "CAnualesPrintView::on_mui_aceptar_clicked", 0 );
00084 QString finicial = mui_fechainicial->text();
00085 QString ffinal = mui_fechafinal->text();
00086 QString finicial1 = mui_fechainicial1->text();
00087 QString ffinal1 = mui_fechafinal1->text();
00088 if ( finicial1 == "" )
00089 {
00090 finicial1 = finicial;
00091 }
00092 if ( ffinal1 == "" )
00093 {
00094 ffinal1 = ffinal;
00095 }
00096 if ( finicial == "" || ffinal == "" )
00097 {
00098 return;
00099 }
00100
00101
00105
00111
00126
00127
00128
00129
00132 empresaBase()->begin();
00133 QString query = "SELECT *, nivel(codigo) AS nivel FROM cuenta ORDER BY codigo";
00134 cursor2 *ramas;
00135 ramas = empresaBase()->cargacursor ( query, "Ramas" );
00136 Arbol *arbolP1, *arbolP2;
00137 arbolP1 = new Arbol;
00138 arbolP2 = new Arbol;
00139 while ( !ramas->eof() )
00140 {
00141 if ( atoi ( ramas->valor ( "nivel" ).toAscii().constData() ) == 2 )
00142 {
00143 arbolP1->nuevarama ( ramas );
00144 arbolP2->nuevarama ( ramas );
00145 }
00146 ramas->siguienteregistro();
00147 }
00148 arbolP1->inicializa ( ramas );
00149 arbolP2->inicializa ( ramas );
00150 delete ramas;
00151 empresaBase()->commit();
00152
00153 QRegExp rx("^.*perdidas y ganancias.*$");
00154 rx.setCaseSensitivity(Qt::CaseInsensitive);
00155 QString asiento;
00157 if(rx.exactMatch(m_doc.elementsByTagName("TITULO").item(0).toElement().text()))
00159 asiento = "%Asiento de Regularizaci%";
00160 else
00161 asiento = "%Asiento de Cierre%";
00162
00164 Asiento1View *asientoReg;
00165 ((Empresa *)empresaBase())->regularizaempresa(finicial, ffinal);
00166 asientoReg = ((Empresa *)empresaBase())->intapuntsempresa2();
00167
00170 empresaBase()->begin();
00171 query = "SELECT cuenta.idcuenta, numapuntes, cuenta.codigo, saldoant, debe, haber, saldo, debeej, haberej, saldoej FROM (SELECT idcuenta, codigo FROM cuenta) AS cuenta NATURAL JOIN (SELECT idcuenta, count(idcuenta) AS numapuntes,sum(debe) AS debeej, sum(haber) AS haberej, (sum(debe)-sum(haber)) AS saldoej FROM apunte WHERE EXTRACT(year FROM fecha) = EXTRACT(year FROM timestamp '"+finicial+"') GROUP BY idcuenta) AS ejercicio LEFT OUTER JOIN (SELECT idcuenta,sum(debe) AS debe, sum(haber) AS haber, (sum(debe)-sum(haber)) AS saldo FROM apunte WHERE fecha >= '"+finicial+"' AND fecha <= '"+ffinal+"' AND conceptocontable NOT SIMILAR TO '"+asiento+"' GROUP BY idcuenta) AS periodo ON periodo.idcuenta=ejercicio.idcuenta LEFT OUTER JOIN (SELECT idcuenta, (sum(debe)-sum(haber)) AS saldoant FROM apunte WHERE fecha < '"+finicial+"' GROUP BY idcuenta) AS anterior ON cuenta.idcuenta=anterior.idcuenta ORDER BY codigo";
00172 cursor2 *hojas;
00173 hojas = empresaBase()->cargacursor ( query, "Periodo1" );
00175 while ( !hojas->eof() )
00176 {
00177 arbolP1->actualizahojas ( hojas );
00178 hojas->siguienteregistro();
00179 }
00180 empresaBase()->commit();
00181 asientoReg->on_mui_borrar_clicked(FALSE);
00182
00184 ((Empresa *)empresaBase())->regularizaempresa(finicial1, ffinal1);
00185 asientoReg = ((Empresa *)empresaBase())->intapuntsempresa2();
00186
00189 empresaBase()->begin();
00190 query = "SELECT cuenta.idcuenta, numapuntes, cuenta.codigo, saldoant, debe, haber, saldo, debeej, haberej, saldoej FROM (SELECT idcuenta, codigo FROM cuenta) AS cuenta NATURAL JOIN (SELECT idcuenta, count(idcuenta) AS numapuntes,sum(debe) AS debeej, sum(haber) AS haberej, (sum(debe)-sum(haber)) AS saldoej FROM apunte WHERE EXTRACT(year FROM fecha) = EXTRACT(year FROM timestamp '"+finicial1+"') GROUP BY idcuenta) AS ejercicio LEFT OUTER JOIN (SELECT idcuenta,sum(debe) AS debe, sum(haber) AS haber, (sum(debe)-sum(haber)) AS saldo FROM apunte WHERE fecha >= '"+finicial1+"' AND fecha <= '"+ffinal1+"' AND conceptocontable NOT SIMILAR TO '"+asiento+"' GROUP BY idcuenta) AS periodo ON periodo.idcuenta=ejercicio.idcuenta LEFT OUTER JOIN (SELECT idcuenta, (sum(debe)-sum(haber)) AS saldoant FROM apunte WHERE fecha < '"+finicial1+"' GROUP BY idcuenta) AS anterior ON cuenta.idcuenta=anterior.idcuenta ORDER BY codigo";
00191 hojas = empresaBase()->cargacursor ( query, "Periodo2" );
00193 while ( !hojas->eof() )
00194 {
00195 arbolP2->actualizahojas ( hojas );
00196 hojas->siguienteregistro();
00197 }
00198 delete hojas;
00199 empresaBase()->commit();
00200 asientoReg->on_mui_borrar_clicked(FALSE);
00201
00202 QDomNodeList lcuentas = m_doc.elementsByTagName ( "CUENTA" );
00203 for ( int i = 0; i < lcuentas.count(); i++ )
00204 {
00205 QDomNode cuenta = lcuentas.item ( i );
00206 QDomElement e1 = cuenta.toElement();
00207 QString valorP1, valorP2;
00208 Fixed valor = Fixed ( "0.00" );
00209 if ( !e1.isNull() )
00210 {
00211 if ( arbolP1->irHoja ( e1.text() ) )
00212 valor = Fixed ( arbolP1->hojaactual ( "saldo" ) );
00213 else
00214 valor = Fixed ( "0.00" );
00215 valorP1 = valor.toQString();
00216 if ( arbolP2->irHoja ( e1.text() ) )
00217 valor = Fixed ( arbolP2->hojaactual ( "saldo" ) );
00218 else
00219 valor = Fixed ( "0.00" );
00220 valorP2 = valor.toQString();
00221 QDomNode c = e1.parentNode();
00222 agregaValores ( c, valorP1, valorP2 );
00223 }
00224 }
00225
00227 delete arbolP1;
00228 delete arbolP2;
00229
00232
00233 bool terminado = FALSE;
00234 while ( !terminado )
00235 {
00236 terminado = TRUE;
00238 QDomNodeList litems = m_doc.elementsByTagName ( "FORMULA" );
00239 for ( int i = 0; i < litems.count(); i++ )
00240 {
00241 QDomNode item = litems.item ( i );
00242 QDomElement e1 = item.toElement();
00243 if ( !e1.isNull() )
00244 {
00245 terminado &= procesaFormula ( item );
00246 }
00247 }
00248 }
00249
00251 imprimir ( finicial, ffinal, finicial1, ffinal1 );
00252 _depura ( "END CAnualesPrintView::on_mui_aceptar_clicked", 0 );
00253 }
00254
00255
00257
00262 bool CAnualesPrintView::procesaFormula ( const QDomNode &formula )
00263 {
00264 _depura ( "CAnualesPrintView::procesaFormula", 0 );
00265 QDomElement valor = formula.firstChildElement ( "VALORACT" );
00266
00267 QString valors = valor.toElement().text();
00268 QString codigo = formula.parentNode().firstChildElement ( "CONCEPTO" ).toElement().text();
00269
00270 if ( !valor.isNull() )
00271 {
00272 return TRUE;
00273 }
00274 Fixed tvaloract = Fixed ( "0.0" );
00275 Fixed tvalorant = Fixed ( "0.0" );
00276 QDomElement formula3 = formula.toElement();
00277 QDomNodeList litems = formula3.elementsByTagName ( "OPERADOR" );
00278 for ( int i = 0; i < litems.count(); i++ )
00279 {
00280 QDomNode item = litems.item ( i );
00281 QDomElement e1 = item.toElement();
00282 if ( !e1.isNull() )
00283 {
00284 if ( !procesaOperador ( item ) )
00285 return FALSE;
00286 QString valoract, valorant;
00287 if ( valorItem ( item, valoract, valorant ) )
00288 {
00289 tvaloract = tvaloract + Fixed ( valoract );
00290 tvalorant = tvalorant + Fixed ( valorant );
00291 }
00292 else
00293 return FALSE;
00294 }
00295 }
00296 QString tvaloracts = tvaloract.toQString();
00297 QString tvalorants = tvalorant.toQString();
00298 agregaValores ( formula, tvaloracts, tvalorants );
00299 _depura ( "END CAnualesPrintView::procesaFormula", 0 );
00300 return TRUE;
00301 }
00302
00311 bool CAnualesPrintView::procesaOperador ( const QDomNode &operador )
00312 {
00313 _depura ( "CAnualesPrintView::procesaOperador", 0, operador.toElement().text() );
00314 QDomElement valor = operador.firstChildElement ( "VALORACT" );
00315 if ( !valor.isNull() )
00316 return TRUE;
00318 QDomElement lineaid = operador.firstChildElement ( "LINEAID" );
00319
00320 if ( !lineaid.isNull() )
00321 {
00322 QDomNodeList litems = m_doc.elementsByTagName ( "ID" );
00323 for ( int i = 0; i < litems.count(); i++ )
00324 {
00325 QDomNode item = litems.item ( i );
00326 QDomElement e1 = item.toElement();
00327 if ( !e1.isNull() )
00328 {
00329 if ( e1.text() == lineaid.text() )
00330 {
00332 QDomNode formula = item.parentNode().firstChildElement ( "FORMULA" );
00333 QString valoract, valorant;
00334 if ( valorItem ( formula, valoract, valorant ) )
00335 {
00336 agregaValores ( operador, valoract, valorant );
00337 return TRUE;
00338 }
00339 else
00340 {
00341 return FALSE;
00342 }
00343 }
00344 }
00345 }
00346 }
00347 _depura ( "CAnualesPrintView::procesaOperador", 0 );
00348 return FALSE;
00349 }
00350
00351
00353
00359 bool CAnualesPrintView::valorItem ( const QDomNode &formula, QString &valoract, QString &valorant )
00360 {
00361 _depura ( "CAnualesPrintView::valorItem", 0, formula.toElement().tagName() );
00362 QDomElement valor = formula.namedItem ( "VALORACT" ).toElement();
00363 if ( valor.isNull() )
00364 {
00365 return FALSE;
00366 }
00367 valoract = valor.text();
00368 valorant = formula.namedItem ( "VALORANT" ).toElement().text();
00369 _depura ( "END CAnualesPrintView::valorItem", 0, formula.toElement().text() + "--" + valoract );
00370 return TRUE;
00371 }
00372
00373
00375
00380 void CAnualesPrintView::agregaValores ( const QDomNode &nodo, const QString &valoract, const QString &valorant )
00381 {
00382 _depura ( "CAnualesPrintView::agregaValores", 0, nodo.toElement().tagName() + " " + valoract );
00383 QDomElement enodo = nodo.toElement();
00384 Fixed fvaloract ( valoract );
00385 Fixed fvalorant ( valorant );
00387 if ( nodo.nodeName() == QString ( "OPERADOR" ) )
00388 {
00389 QDomNodeList opcs = enodo.elementsByTagName ( "OPCIONES" );
00390 for ( int i = 0; i < opcs.count(); i++ )
00391 {
00392 QDomElement op = opcs.item ( i ).toElement();
00393 QString opciones = op.text();
00394 if ( opciones == "POSITIVO" )
00395 {
00396 if ( fvaloract < 0 )
00397 fvaloract = fvaloract * -1;
00398 if ( fvalorant < 0 )
00399 fvalorant = fvalorant * -1;
00400 }
00401 if ( opciones == "NEGATIVO" )
00402 {
00403 if ( fvaloract > 0 )
00404 fvaloract = fvaloract * -1;
00405 if ( fvalorant > 0 )
00406 fvalorant = fvalorant * -1;
00407 }
00408 if ( opciones == "RESTAR" )
00409 {
00410 fvaloract = fvaloract * -1;
00411 fvalorant = fvalorant * -1;
00412 }
00413 }
00414 }
00415 else
00416 {
00417 QDomNodeList opcs = nodo.parentNode().toElement().elementsByTagName ( "OPCIONES" );
00418 for ( int i = 0; i < opcs.count(); i++ )
00419 {
00420 QDomElement op = opcs.item ( i ).toElement();
00421 QString opciones = op.text();
00422 if ( opciones == "MAYORCERO" )
00423 {
00424 if ( fvaloract < 0 )
00425 fvaloract = 0;
00426 if ( fvalorant < 0 )
00427 fvalorant = 0;
00428 }
00429 if ( opciones == "MENORCERO" )
00430 {
00431 if ( fvaloract > 0 )
00432 fvaloract = 0;
00433 if ( fvalorant > 0 )
00434 fvalorant = 0;
00435 }
00436 }
00437 }
00438 QDomElement valoract1 = m_doc.createElement ( "VALORACT" );
00439 valoract1.setTagName ( "VALORACT" );
00440 QDomText etx = m_doc.createTextNode ( fvaloract.toQString() );
00441 valoract1.appendChild ( etx );
00442 QDomElement valorant1 = m_doc.createElement ( "VALORANT" );
00443 valorant1.setTagName ( "VALORANT" );
00444 QDomText etc = m_doc.createTextNode ( fvalorant.toQString() );
00445 valorant1.appendChild ( etc );
00446 QDomNode n = nodo;
00447 enodo.appendChild ( valoract1 );
00448 enodo.appendChild ( valorant1 );
00449 _depura ( "END CAnualesPrintView::agregaValores", 0 );
00450 }
00451
00452
00454
00460 void CAnualesPrintView::imprimir ( QString periodo1finicial, QString periodo1ffinal, QString periodo2finicial, QString periodo2ffinal )
00461 {
00462 _depura ( "CAnualesPrintView::imprimir", 0 );
00463 QString archivo = confpr->valor ( CONF_DIR_OPENREPORTS ) + "canuales.rml";
00464 QString archivod = confpr->valor ( CONF_DIR_USER ) + "canuales.rml";
00465 QString archivologo = confpr->valor ( CONF_DIR_OPENREPORTS ) + "logo.jpg";
00467 #ifdef WINDOWS
00468
00469 archivo = "copy " + archivo + " " + archivod;
00470 #else
00471
00472 archivo = "cp " + archivo + " " + archivod;
00473 #endif
00474
00475 system ( archivo.toAscii().constData() );
00477 #ifdef WINDOWS
00478
00479 archivologo = "copy " + archivologo + " " + confpr->valor ( CONF_DIR_USER ) + "logo.jpg";
00480 #else
00481
00482 archivologo = "cp " + archivologo + " " + confpr->valor ( CONF_DIR_USER ) + "logo.jpg";
00483 #endif
00484
00485 system ( archivologo.toAscii().constData() );
00486 QFile file;
00487 file.setFileName ( archivod );
00488 file.open ( QIODevice::ReadOnly );
00489 QTextStream stream ( &file );
00490 QString buff = stream.readAll();
00491 file.close();
00492 QString fitxersortidatxt;
00493 fitxersortidatxt = "";
00494
00496 QDomNodeList componentes = m_doc.elementsByTagName ( "COMPONENTE" );
00497 for ( int j = 0; j < componentes.count(); j++ )
00498 {
00499 QDomElement comp = componentes.item ( j ).toElement();
00501 QString debeohaber = comp.namedItem ( "SUBTITULO" ).toElement().text();
00502 fitxersortidatxt += "" + debeohaber + "\n";
00504 fitxersortidatxt += "<blockTable style=\"tabla\" repeatRows=\"1\" colWidths=\"12cm,3cm,3cm\">\n";
00505 fitxersortidatxt += "<tr><td><para style=\"debeohaber\">" + debeohaber + "</para></td>\n";
00506 fitxersortidatxt += "<td><para style=\"periodo\">Periodo:</para><para style=\"periodo\">" + periodo1finicial + " ~ " + periodo1ffinal + "</para></td>\n";
00507 fitxersortidatxt += "<td><para style=\"periodo\">Periodo:</para><para style=\"periodo\">" + periodo2finicial + " ~ " + periodo2ffinal + "</para></td></tr>\n";
00508
00509 QDomNodeList litems = comp.elementsByTagName("LBALANCE");
00510 QLocale::setDefault(QLocale(QLocale::Spanish, QLocale::Spain));
00511 QLocale spanish;
00512 for ( int i = 0; i < litems.count(); i++ )
00513 {
00514 QDomNode item = litems.item ( i );
00515 QDomElement e1 = item.toElement();
00516 if ( !e1.isNull() )
00517 {
00518 QDomNode formul = item.firstChildElement ( "FORMULA" );
00519 QString vact = formul.firstChildElement ( "VALORACT" ).toElement().text();
00520 vact = spanish.toString(vact.toDouble(), 'f', 2);
00521 QString vant = formul.firstChildElement ( "VALORANT" ).toElement().text();
00522 vant = spanish.toString(vant.toDouble(), 'f', 2);
00523 QString texto = item.firstChildElement ( "CONCEPTO" ).toElement().text();
00524 fitxersortidatxt += "<tr>\n";
00525 fitxersortidatxt += "<td>"+texto+"</td>\n";
00526 fitxersortidatxt += "<td>"+vact+"</td>\n";
00527 fitxersortidatxt += "<td>"+vant+"</td>\n";
00528 fitxersortidatxt += "</tr>\n";
00529 }
00530 }
00531 QLocale::setDefault(QLocale::C);
00532 fitxersortidatxt += "</blockTable>\n";
00533 fitxersortidatxt += "<nextFrame/>\n";
00534 }
00535
00536 buff.replace ( "[story]", fitxersortidatxt );
00537 QDomElement nodo = m_doc.namedItem ( "BALANCE" ).namedItem ( "TITULO" ).toElement();
00538 buff.replace ( "[titulo]", nodo.text() );
00539
00540 if ( file.open ( QIODevice::WriteOnly ) )
00541 {
00542 QTextStream stream ( &file );
00543 stream << buff;
00544 file.close();
00545 }
00546
00547 invocaPDF ( "canuales" );
00548 _depura ( "END CAnualesPrintView::imprimir", 0 );
00549 }
00550
00551
00553
00557 void CAnualesPrintView::setidbalance ( QString id )
00558 {
00559 _depura ( "CAnualesPrintView::setidbalance", 0 );
00560 QFile f ( id );
00561 if ( !f.open ( QIODevice::ReadOnly ) )
00562 return;
00563 if ( !m_doc.setContent ( &f ) )
00564 {
00565 f.close();
00566 return;
00567 }
00568 f.close();
00569 m_nomBalance->setText ( id );
00570 _depura ( "END CAnualesPrintView::setidbalance", 0 );
00571 }
00572