Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

account.cpp

Go to the documentation of this file.
00001 #include "account.h"
00002 #include "preferences.h"
00003 
00004 #include <qpixmap.h>
00005 #include <stdlib.h>
00006 
00007 extern Preferences *preferences;
00008 
00009 Account::Account ()
00010   {
00011     adb = sqlite_open ( "qmaccounts.db", 0, NULL );
00012   }
00013 
00014 Account::~Account ()
00015   {
00016     sqlite_close ( adb );
00017   }
00018 
00019 void Account::addAccount ( QString name, int parentid, float balance, int type, QString description, float creditlimit,
00020                                         int statementyear, int statementmonth, int statementday, float statementbalance, const char *currency  )
00021   {
00022     sqlite_exec_printf ( adb, "insert into accounts2 values ( '%q', %i, %.2f, %i, '%q', %.2f, %i, %i, %i, %.2f, '%q', 0, 0, 0, 0, 0, NULL );", 0, 0, 0,
00023       (const char *) name, parentid, balance, type, (const char *) description, creditlimit, statementyear, statementmonth, statementday, statementbalance, currency );
00024   }
00025 
00026 void Account::updateAccount ( QString name, QString description, QString currencycode, int accountid )
00027   {
00028     sqlite_exec_printf ( adb, "update accounts2 set name = '%q', description = '%q', currency = '%q' where accountid = %i;", 0, 0, 0, ( const char * ) name, ( const char * ) description, ( const char * ) currencycode, accountid );
00029   }
00030 
00031 void Account::deleteAccount ( int accountid )
00032   {
00033     sqlite_exec_printf ( adb, "delete from accounts2 where accountid = %i;", 0, 0, 0, accountid );
00034   }
00035 
00036 void Account::setAccountExpanded ( int expanded, int accountid )
00037   {
00038     sqlite_exec_printf ( adb, "update accounts2 set r1 = %i where accountid = %i;", 0, 0, 0, expanded, accountid );
00039   }
00040 
00041 int Account::getAccountExpanded ( int id )
00042   {
00043     char **results;
00044     sqlite_get_table_printf ( adb, "select r1 from accounts2 where accountid = %i;", &results, 0, 0, 0, id );
00045     if ( strlen ( results [1] ) == 0 )
00046       return 0;
00047     else
00048       return atoi ( results [ 1 ] );
00049   }
00050 
00051 int Account::getNumberOfAccounts ()
00052   {
00053     char **results;
00054     sqlite_get_table ( adb, "select count() from accounts2;", &results, NULL, NULL, NULL );
00055     return atoi ( results [ 1 ] );
00056   }
00057 
00058 int Account::getNumberOfChildAccounts ( int id )
00059   {
00060     char **results;
00061     sqlite_get_table_printf ( adb, "select count() from accounts2 where parent = %i;", &results, NULL, NULL, NULL, id );
00062     return atoi ( results [ 1 ] );
00063   }
00064 
00065 void Account::updateAccountBalance ( int accountid )
00066   {
00067     // Here, we'll get a balance for the transactions in an account
00068     sqlite *tdb = sqlite_open ( "qmtransactions.db", 0, NULL );
00069     int rows, columns;
00070     char **results;
00071     sqlite_get_table_printf ( tdb, "select sum (amount) from transactions where accountid= %i;", &results, &rows, &columns, NULL, accountid );
00072     float transactionsbalance = strtod ( results [ 1 ], 0 );
00073     sqlite_close ( tdb );
00074 
00075     // next, we'll get a balance for all the transfers from the account
00076     sqlite *trdb = sqlite_open ( "qmtransfers.db", 0, NULL );
00077     rows = 0;
00078     columns = 0;
00079     char **results2;
00080     sqlite_get_table_printf ( trdb, "select sum (amount) from transfers where fromaccount = %i;", &results2, &rows, &columns, NULL, accountid );
00081     float fromtransfersbalance = ( strtod ( results2 [ 1 ], 0 ) * -1 );
00082 
00083     // finally, we'll get a balance for all the transfers into the account
00084     rows = 0;
00085     columns= 0;
00086     char **results3;
00087     sqlite_get_table_printf ( trdb, "select sum (amount) from transfers where toaccount = %i;", &results3, &rows, &columns, NULL, accountid );
00088     float totransfersbalance = strtod ( results3 [ 1 ], 0 );
00089 
00090     sqlite_close ( trdb );
00091 
00092     // calculate and update new balance
00093     sqlite_exec_printf ( adb, "update accounts2 set balance = %.2f where accountid = %i;", 0, 0, 0,
00094       ( transactionsbalance + fromtransfersbalance + totransfersbalance + getStatementBalance ( accountid ) ), accountid );
00095   }
00096 
00097 void Account::changeParentAccountBalance ( int parentid )
00098   {
00099     // select all child balances that share the parent of the current child account
00100     char **results;
00101     int rows;
00102     sqlite_get_table_printf ( adb, "select sum ( balance ) from accounts2 where parent = %i;", &results, &rows, NULL, NULL, parentid );
00103     sqlite_exec_printf ( adb, "update accounts2 set balance = %.2f where accountid = %i;", 0, 0, 0, strtod ( results[ 1 ], NULL ), parentid );
00104   }
00105 
00106 int Account::getParentAccountID ( int id )
00107   {
00108     char **results;
00109     sqlite_get_table_printf ( adb, "select parent from accounts2 where accountid = %i;", &results, NULL, NULL, NULL, id );
00110     return atoi ( results [ 1 ] );
00111   }
00112 
00113 int Account::getParentAccountID ( QString accountname )
00114   {
00115     char **results;
00116     sqlite_get_table_printf ( adb, "select parent from accounts2 where name= '%q';", &results, NULL, NULL, NULL, ( const char * ) accountname );
00117     return atoi ( results [ 1 ] );
00118   }
00119 
00120 void Account::displayAccounts ( QListView *listview )
00121   {
00122     char **results;
00123     int rows, columns;
00124     sqlite_get_table ( adb, "select name, parent, balance, accountid, currency from accounts2;", &results, &rows, &columns, 0 );
00125 
00126     // determine if we are using currency support
00127     int currency = preferences->getPreference ( 4 );
00128 
00129     // remove all columns from the account display
00130     int counter;
00131     for ( counter = 0; counter <= columns; counter++ )
00132       listview->removeColumn ( 0 );
00133 
00134     // add columns to the account display
00135     listview->addColumn ( "Account", 0 );
00136     int columntoalign = 1;
00137     if ( preferences->getPreference ( 4 ) == 1 ) // add the currency column if the user wants it
00138       {
00139         listview->addColumn ( "C", 0 );
00140         columntoalign = 2;
00141       }
00142     listview->addColumn ( "Balance", 0 );
00143     listview->addColumn ( "", 0 );
00144     listview->setColumnAlignment ( columntoalign, Qt::AlignRight );
00145     counter = 5;
00146     int total = ( rows + 1 ) * columns;
00147     while ( counter < total )
00148       {
00149         int accountid = atoi ( results [ counter + 3 ] );
00150         if ( atoi ( results [ counter + 1 ]  ) == -1 )
00151           {
00152             QListViewItem *parent = new QListViewItem ( listview );
00153             parent->setText ( 0, results [ counter ] );
00154             if ( currency == 0 )
00155               {
00156                 parent->setText ( 1, results [ counter + 2 ] );
00157                 parent->setText ( 2, results [ counter + 3 ] );
00158               }
00159             else
00160               {
00161                 if ( getNumberOfChildAccounts ( accountid ) == 0 ) // add the currency flag if this is a parent with no children
00162                   {
00163                     // create the string we'll use to set the currency pixmap
00164                     QString filename = "/opt/QtPalmtop/pics/flags/";
00165                     QString flag = results [ counter + 4 ];
00166                     filename.append ( flag );
00167                     filename.append ( ".png" );
00168                     parent->setPixmap ( 1, QPixmap ( filename ) );
00169                     parent->setText ( 1, flag );
00170                   }
00171                 parent->setText ( 2, results [ counter + 2 ] );
00172                 parent->setText ( 3, results [ counter + 3 ] );
00173               }
00174 
00175             if ( getAccountExpanded ( accountid ) == 1 )
00176               parent->setOpen ( TRUE );
00177 
00178             //Start display child accounts for this parent
00179             int childcounter = 5;
00180             while ( childcounter < total )
00181               {
00182                 if ( atoi ( results [ childcounter + 1 ] ) == accountid )
00183                   {
00184                     if ( currency == 0 )
00185                       QListViewItem *child = new QListViewItem ( parent, results [ childcounter ], results [ childcounter + 2 ], results [ childcounter + 3 ] );
00186                     else
00187                       {
00188                         // create the string we'll use to set the currency pixmap
00189                         QString filename = "/opt/QtPalmtop/pics/flags/";
00190                         QString flag = results [ childcounter + 4 ];
00191                         filename.append ( flag );
00192                         filename.append ( ".png" );
00193                         QListViewItem *child = new QListViewItem ( parent, results [ childcounter ], "", results [ childcounter + 2 ], results [ childcounter + 3 ] );
00194                         child->setPixmap ( 1, QPixmap ( filename ) );
00195                         child->setText ( 1, flag );
00196                       }
00197                   }
00198                 childcounter = childcounter + 5;
00199               }
00200             //End display child accounts
00201           }
00202         counter = counter + 5;
00203       }
00204 
00205     // resize all columns appropriately
00206     if ( preferences->getPreference ( 4 ) == 0 )
00207       {
00208         listview->setColumnWidth ( 0, preferences->getColumnPreference ( 1 ) );
00209         listview->setColumnWidthMode ( 0, QListView::Manual );
00210         listview->setColumnWidth ( 1, preferences->getColumnPreference ( 2 ) );
00211         listview->setColumnWidthMode ( 1, QListView::Manual );
00212         listview->setColumnWidthMode ( 2, QListView::Manual );
00213       }
00214     else
00215       {
00216         listview->setColumnWidth ( 0, preferences->getColumnPreference ( 10 ) );
00217         listview->setColumnWidthMode ( 0, QListView::Manual );
00218         listview->setColumnWidth ( 1, preferences->getColumnPreference ( 11 ) );
00219         listview->setColumnWidthMode ( 1, QListView::Manual );
00220         listview->setColumnWidth ( 2, preferences->getColumnPreference ( 12 ) );
00221         listview->setColumnWidthMode ( 2, QListView::Manual );
00222         listview->setColumnWidthMode ( 3, QListView::Manual );
00223       }
00224 
00225     // Now reset the column sorting to user preference
00226     int column = 0;
00227     int direction = 0;
00228     preferences->getSortingPreference ( 1, &column, &direction );
00229     listview->setSorting ( column, direction );
00230   }
00231 
00232 int Account::displayParentAccountNames ( QComboBox *combobox, QString indexstring )
00233   {
00234     char **results;
00235     int rows, columns, index;
00236     index = 0;
00237     sqlite_get_table ( adb, "select name from accounts2 order by name asc;", &results, &rows, &columns, NULL );
00238     int counter = 1;
00239     int indexcounter = 1;
00240     int total = ( rows + 1 ) * columns;
00241     while ( counter < total )
00242       {
00243         if ( getParentAccountID ( results [ counter ] ) == -1 )
00244           {
00245             combobox->insertItem ( results [ counter ], -1 );
00246             if ( strcmp ( results [ counter ], indexstring ) == 0 )
00247               index = indexcounter;
00248             indexcounter++;
00249           }
00250         counter ++;
00251       }
00252     return index;
00253   }
00254 
00255 int Account::getAccountType ( int accountid )
00256   {
00257     char **results;
00258     sqlite_get_table_printf ( adb, "select type from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid );
00259     return atoi ( results [ 1 ] );
00260   }
00261 
00262 int Account::getStatementDay ( int accountid )
00263   {
00264     char **results;
00265     sqlite_get_table_printf ( adb, "select statementday from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid );
00266     return atoi ( results [ 1 ] );
00267   }
00268 
00269 int Account::getStatementMonth ( int accountid )
00270   {
00271     char **results;
00272     sqlite_get_table_printf ( adb, "select statementmonth from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid );
00273     return atoi ( results [ 1 ] );
00274   }
00275 
00276 int Account::getStatementYear ( int accountid )
00277   {
00278     char **results;
00279     sqlite_get_table_printf ( adb, "select statementyear from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid );
00280     return atoi ( results [ 1 ] );
00281   }
00282 
00283 QString Account::getAccountDescription ( int accountid )
00284   {
00285     char **results;
00286     sqlite_get_table_printf ( adb, "select description from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid );
00287     return ( QString ) results [ 1 ];
00288   }
00289 
00290 QString Account::getCurrencyCode ( int accountid )
00291   {
00292     char **results;
00293     sqlite_get_table_printf ( adb, "select currency from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid );
00294     return ( QString ) results [ 1 ];
00295   }
00296 
00297 QString Account::getAccountName ( int accountid )
00298   {
00299     char **results;
00300     sqlite_get_table_printf ( adb, "select name from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid );
00301     return ( QString ) results [ 1 ];
00302   }
00303 
00304 QString Account::getAccountBalance ( int accountid )
00305   {
00306     char **results;
00307     sqlite_get_table_printf ( adb, "select balance from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid );
00308     return ( QString ) results [ 1 ];
00309   }
00310 
00311 float Account::getAccountCreditLimit ( int accountid )
00312   {
00313     char **results;
00314     sqlite_get_table_printf ( adb, "select creditlimit from accounts2 where accountid = %i;", &results, NULL, NULL, NULL, accountid );
00315     return strtod ( results [ 1 ], NULL );
00316   }
00317 
00318 float Account::getStatementBalance ( int accountid )
00319   {
00320     char **results;
00321     sqlite_get_table_printf ( adb, "select statementbalance from accounts2 where accountid = %i;", &results, NULL, NULL, NULL, accountid );
00322     return strtod ( results [ 1 ], NULL );
00323   }
00324 
00325 GreyBackgroundItem::GreyBackgroundItem ( QListView *parent )
00326   :  QListViewItem ( parent )
00327   {
00328   }
00329 
00330 GreyBackgroundItem::GreyBackgroundItem ( QListView *parent, QString label1, QString label2, QString label3 )
00331   :  QListViewItem ( parent, label1, label2, label3 )
00332   {
00333   }
00334 
00335 GreyBackgroundItem::GreyBackgroundItem ( QListView *parent, QString label1, QString label2, QString label3, QString label4 )
00336   :  QListViewItem ( parent, label1, label2, label3, label4 )
00337   {
00338   }
00339 
00340 GreyBackgroundItem::GreyBackgroundItem ( QListView *parent, QString label1, QString label2, QString label3, QString label4, QString label5 )
00341   :  QListViewItem ( parent, label1, label2, label3, label4, label5 )
00342   {
00343   }
00344 
00345 void GreyBackgroundItem::paintCell ( QPainter *p, const QColorGroup &cg, int column, int width, int alignment )
00346   {
00347     QColorGroup _cg ( cg );
00348     _cg.setColor ( QColorGroup::Base, Qt::lightGray );
00349     QListViewItem::paintCell ( p, _cg, column, width, alignment );
00350   }
00351 
00352 QStringList Account::getAccountNames ()
00353   {
00354     QStringList accountnames;
00355     char **results;
00356     int rows, counter;
00357     sqlite_get_table ( adb, "select name from accounts2;", &results, &rows, 0, 0 );
00358     for ( counter = 0; counter < rows; counter++ )
00359       accountnames.append ( results [ counter+1 ] );
00360     return accountnames;
00361   }
00362 
00363 QStringList Account::getAccountIDs ()
00364   {
00365     QStringList accountids;
00366     char **results;
00367     int rows, counter;
00368     sqlite_get_table ( adb, "select accountid from accounts2;", &results, &rows, 0, 0 );
00369     for ( counter = 0; counter < rows; counter++ )
00370       accountids.append ( results [ counter+1 ] );
00371     return accountids;
00372   }
00373 
00374 

Generated on Sat Nov 5 16:18:10 2005 for OPIE by  doxygen 1.4.2