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

budget.cpp

Go to the documentation of this file.
00001 #include "budget.h"
00002 #include "transaction.h"
00003 #include <stdlib.h>
00004 
00005 extern Transaction *transaction;
00006 
00007 Budget::Budget ()
00008   {
00009     bdb = sqlite_open ( "qmbudgets.db", 0, NULL );
00010   }
00011 
00012 Budget::~Budget ()
00013   {
00014     sqlite_close ( bdb );
00015   }
00016 
00017 int Budget::addBudget ( QString name, int type, QString description, QString currency, int startday, int startmonth, int startyear, int endday, int endmonth, int endyear, int defaultview )
00018   {
00019     sqlite_exec_printf ( bdb, "insert into budgets values ( '%q', %i, '%q', '%q', %i, %i, %i, %i, %i, %i, %i, NULL );", 0, 0, 0, ( const char * ) name, type, ( const char * ) description, ( const char * ) currency, startday, startmonth, startyear, endday, endmonth, endyear, defaultview );
00020     char **results;
00021     sqlite_get_table ( bdb, "select last_insert_rowid() from budgets;", &results, NULL, NULL, NULL );
00022     QString tablename = "table";
00023     tablename.append ( results [ 1 ] );
00024     sqlite_exec_printf ( bdb, "create table '%q' ( name, lineitemamount, type, lineitemid integer primary key );", 0, 0, 0, ( const char* ) tablename );
00025     return atoi ( results [ 1 ] );
00026   }
00027 
00028 void Budget::updateBudget ( QString name, QString description, QString currency, int budgetid )
00029   {
00030     sqlite_exec_printf ( bdb, "update budgets set name = '%q', description = '%q', currency = '%q' where budgetid = %i;", 0, 0, 0, ( const char * ) name, ( const char * ) description, ( const char * ) currency, budgetid );
00031   }
00032 
00033 void Budget::deleteBudget ( int budgetid )
00034   {
00035     if ( getNumberOfBudgets() != 0 )
00036       {
00037         QString tablename = "table";
00038         tablename.append ( QString::number ( budgetid ) );
00039         sqlite_exec_printf ( bdb, "delete from budgets where budgetid = %i;", 0, 0, 0, budgetid );
00040         sqlite_exec_printf ( bdb, "drop table '%q';", 0, 0, 0, ( const char* ) tablename );
00041       }
00042   }
00043 
00044 int Budget::getNumberOfBudgets ()
00045   {
00046     char **results;
00047     sqlite_get_table ( bdb, "select count() from budgets;", &results, NULL, NULL, NULL );
00048     return atoi ( results [ 1 ] );
00049   }
00050 
00051 int Budget::getNumberOfLineItems ( int budgetid )
00052   {
00053     QString tablename = "table";
00054     tablename.append ( QString::number ( budgetid ) );
00055     char **results;
00056     sqlite_get_table_printf ( bdb, "select count() from '%q';", &results, NULL, NULL, NULL, ( const char * ) tablename );
00057     return atoi ( results [ 1 ] );
00058   }
00059 
00060 QStringList* Budget::getBudgetNames ()
00061   {
00062     QStringList *names = new QStringList ();
00063     char **results;
00064     int rows, counter;
00065     sqlite_get_table ( bdb, "select name from budgets;", &results, &rows, NULL, NULL );
00066     names->append ( "None" );
00067     for ( counter = 0; counter < rows; counter++ )
00068       names->append ( results [ counter+1 ] );
00069     return names;
00070   }
00071 
00072 QString Budget::getBudgetName ( int budgetid )
00073   {
00074     char **results;
00075     sqlite_get_table_printf ( bdb, "select name from budgets where budgetid= %i;", &results, NULL, NULL, NULL, budgetid );
00076     return ( QString ) results [ 1 ];
00077   }
00078 
00079 QString Budget::getBudgetDescription ( int budgetid )
00080   {
00081     char **results;
00082     sqlite_get_table_printf ( bdb, "select description from budgets where budgetid= %i;", &results, NULL, NULL, NULL, budgetid );
00083     return ( QString ) results [ 1 ];
00084   }
00085 
00086 QString Budget::getCurrency ( int budgetid )
00087   {
00088     char **results;
00089     sqlite_get_table_printf ( bdb, "select currency from budgets where budgetid= %i;", &results, NULL, NULL, NULL, budgetid );
00090     return ( QString ) results [ 1 ];
00091   }
00092 
00093 QStringList* Budget::getBudgetIDs ()
00094   {
00095     QStringList *ids = new QStringList ();
00096     char **results;
00097     int rows, counter;
00098     sqlite_get_table ( bdb, "select budgetid from budgets;", &results, &rows, NULL, NULL );
00099     for ( counter = 0; counter < rows; counter++ )
00100       ids->append ( results [ counter+1 ] );
00101     return ids;
00102   }
00103 
00104 int Budget::addLineItem ( int budgetid, QString lineitemname, float lineitemamount, int lineitemtype )
00105   {
00106     QString tablename = "table";
00107     tablename.append ( QString::number ( budgetid ) );
00108     sqlite_exec_printf ( bdb, "insert into '%q' values ( '%q', %.2f, %i, NULL );", 0, 0, 0, ( const char* ) tablename, ( const char* ) lineitemname, lineitemamount, lineitemtype );
00109     char **results;
00110     sqlite_get_table_printf ( bdb, "select last_insert_rowid() from '%q';", &results, NULL, NULL, NULL, ( const char* ) tablename );
00111     return atoi ( results [ 1 ] );
00112   }
00113 
00114 void Budget::updateLineItem ( QString lineitemname, float lineitemamount, int lineitemtype, int budgetid, int lineitemid )
00115   {
00116     QString tablename = "table";
00117     tablename.append ( QString::number ( budgetid ) );
00118     sqlite_exec_printf ( bdb, "update '%q' set name = '%q', lineitemamount = %f, type = %i where lineitemid = %i;", 0, 0, 0, ( const char* ) tablename, ( const char * ) lineitemname, lineitemamount, lineitemtype, lineitemid );
00119   }
00120 
00121 void Budget::deleteLineItem ( int budgetid, int lineitemid )
00122   {
00123     QString tablename = "table";
00124     tablename.append ( QString::number ( budgetid ) );
00125    sqlite_exec_printf ( bdb, "delete from '%q' where lineitemid = %i;", 0, 0, 0, ( const char * ) tablename, lineitemid );
00126   }
00127 
00128 void Budget::displayLineItems ( int budgetid, QListView *listview, int month, int year, int viewtype )
00129   {
00130     QString tablename = "table";
00131     tablename.append ( QString::number ( budgetid ) );
00132     char **results;
00133     int rows, columns, counter;
00134     sqlite_get_table_printf ( bdb, "select name, lineitemamount, lineitemid from '%q';", &results, &rows, &columns, NULL, ( const char * ) tablename );
00135     int total = ( ( rows + 1 ) * columns );
00136     for ( counter = 3; counter < total; counter = counter + 3 )
00137       {
00138         float amount = 0;
00139         if ( viewtype == 0 )
00140           {
00141             QString lineitemamount = results [ counter + 1 ];
00142             amount = lineitemamount.toFloat() / 12;
00143           }
00144         else
00145           {
00146             QString lineitemamount = results [ counter + 1 ];
00147             amount = lineitemamount.toFloat();
00148           }
00149         QListViewItem *item = new QListViewItem ( listview, results [ counter ], QString::number ( amount, 'f', 2 ), transaction->getBudgetTotal ( budgetid, atoi ( results [ counter + 2 ] ), year, month, viewtype ), results [ counter + 2 ] );
00150       }
00151   }
00152 
00153 QStringList Budget::getLineItems ( int budgetid )
00154   {
00155     QString tablename = "table";
00156     tablename.append ( QString::number ( budgetid ) );
00157     QStringList lineitems;
00158     char **results;
00159     int rows, counter;
00160     sqlite_get_table_printf ( bdb, "select name from '%q';", &results, &rows, NULL, NULL, (const char*) tablename );
00161     for ( counter = 0; counter < rows; counter++ )
00162       lineitems.append ( results [ counter + 1 ] );
00163     return lineitems;
00164   }
00165 
00166 QStringList Budget::getLineItemIDs ( int budgetid )
00167   {
00168     QString tablename = "table";
00169     tablename.append ( QString::number ( budgetid ) );
00170     QStringList lineitemids;
00171     char **results;
00172     int rows, counter;
00173     sqlite_get_table_printf ( bdb, "select lineitemid from '%q';", &results, &rows, NULL, NULL, (const char*) tablename );
00174     for ( counter = 0; counter < rows; counter++ )
00175       lineitemids.append ( results [ counter + 1 ] );
00176     return lineitemids;
00177   }
00178 
00179 int Budget::getLineItemTime ( int budgetid, int lineitemid )
00180   {
00181     QString tablename = "table";
00182     tablename.append ( QString::number ( budgetid ) );
00183     char **results;
00184     sqlite_get_table_printf ( bdb, "select type from '%q' where lineitemid= %i;", &results, NULL, NULL, NULL, ( const char * ) tablename, lineitemid );
00185     return atoi ( results [ 1 ] );
00186   }
00187 
00188 float Budget::getLineItemAmount ( int budgetid, int lineitemid )
00189   {
00190     QString tablename = "table";
00191     tablename.append ( QString::number ( budgetid ) );
00192     char **results;
00193     sqlite_get_table_printf ( bdb, "select lineitemamount from '%q' where lineitemid= %i;", &results, NULL, NULL, NULL, ( const char* ) tablename, lineitemid );
00194     return strtod ( results [ 1 ], 0 );
00195   }
00196 
00197 QString Budget::getBudgetTotal ( int budgetid, int viewtype )
00198   {
00199     QString tablename = "table";
00200     tablename.append ( QString::number ( budgetid ) );
00201     // determine if we are viewing a years, months, or days budget
00202     // we have to pick a different sum for each
00203     char **results;
00204     sqlite_get_table_printf ( bdb, "select sum ( lineitemamount ) from '%q';", &results, NULL, NULL, NULL, ( const char * ) tablename );
00205     QString amount = results [ 1 ];
00206     float total  = amount.toFloat();
00207     if ( viewtype == 0 )
00208       total = total / 12;
00209     amount.setNum ( total, 'f', 2 );
00210     return amount;
00211   }
00212 
00213 int Budget::getLastAdded ()
00214   {
00215     char **results;
00216     sqlite_get_table ( bdb, "select last_insert_rowid() from budgets;", &results, NULL, NULL, NULL );
00217     return atoi ( results [ 1 ] );
00218   }
00219 
00220 
00221 

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