00001
00002
00003
00004
00005
00006 #include "transaction.h"
00007 #include "account.h"
00008 #include "transactiondisplay.h"
00009
00010 #include <stdlib.h>
00011
00012 extern Account *account;
00013 extern Preferences *preferences;
00014
00015 Transaction::Transaction ()
00016 {
00017 tdb = sqlite_open ( "qmtransactions.db", 0, NULL );
00018 }
00019
00020 Transaction::~Transaction ()
00021 {
00022 sqlite_close ( tdb );
00023 }
00024
00025 void Transaction::addTransaction ( QString description, QString payee, int accountid, int parentid, int number, int day, int month, int year, float amount, int cleared, int budgetid, int lineitemid )
00026 {
00027 sqlite_exec_printf ( tdb, "insert into transactions values ( '%q', %i, %i, %i, %i, %i, %i, %.2f, %i, %i, 0, 0, 0, 0, 0, 0, %i, '%q', 0, "
00028 "0, 0, 0, NULL );", 0, 0, 0, ( const char * ) payee, accountid, parentid, number, day, month, year, amount, cleared, budgetid, lineitemid, ( const char * ) description );
00029 }
00030
00031 void Transaction::updateTransaction ( QString description, QString payee, int number, int day, int month, int year, float amount, int cleared, int budgetid, int lineitemid, int transactionid )
00032 {
00033 sqlite_exec_printf ( tdb, "update transactions set reservedtwo = '%q', payee = '%q', number = %i, day = %i, month = %i, year = %i, amount = %.2f,"
00034 "cleared = %i, budgetid = %i, reservedone = %i where transid = %i;", 0, 0, 0, ( const char * ) description, ( const char * ) payee, number, day, month, year,
00035 amount, cleared, budgetid, lineitemid, transactionid );
00036 }
00037
00038 void Transaction::deleteTransaction ( int transid )
00039 {
00040 sqlite_exec_printf ( tdb, "delete from transactions where transid = %i;", 0, 0, 0, transid );
00041 }
00042
00043 void Transaction::deleteAllTransactions ( int accountid )
00044 {
00045 sqlite_exec_printf ( tdb, "delete from transactions where accountid = %i;", 0, 0, 0, accountid );
00046 }
00047
00048 int Transaction::getAccountID ( int id )
00049 {
00050 char **results;
00051 sqlite_get_table_printf ( tdb, "select accountid from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
00052 return atol ( results [ 1 ] );
00053 }
00054
00055 int Transaction::getNumberOfTransactions ()
00056 {
00057 char **results;
00058 sqlite_get_table ( tdb, "select count() from transactions;", &results, NULL, NULL, NULL );
00059 return atoi ( results [ 1 ] );
00060 }
00061
00062 int Transaction::getNumberOfTransactions ( int accountid )
00063 {
00064 char **results;
00065 sqlite_get_table_printf ( tdb, "select count() from transactions where accountid = %i;", &results, NULL, NULL, NULL, accountid );
00066 return atol ( results [ 1 ] );
00067 }
00068
00069 QString Transaction::getPayee ( int id )
00070 {
00071 char **results;
00072 sqlite_get_table_printf ( tdb, "select payee from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
00073 return results [ 1 ];
00074 }
00075
00076 QString Transaction::getTransactionDescription ( int id )
00077 {
00078 char **results;
00079 sqlite_get_table_printf ( tdb, "select reservedtwo from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
00080 return results [ 1 ];
00081 }
00082
00083 QString Transaction::getNumber ( int id )
00084 {
00085 char **results;
00086 sqlite_get_table_printf ( tdb, "select number from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
00087 return results [ 1 ];
00088 }
00089
00090 QString Transaction::getAmount ( int id )
00091 {
00092 char **results;
00093 sqlite_get_table_printf ( tdb, "select amount from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
00094 return results [ 1 ];
00095 }
00096
00097 QString Transaction::getAbsoluteAmount ( int id )
00098 {
00099 char **results;
00100 sqlite_get_table_printf ( tdb, "select abs ( amount ) from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
00101 return results [ 1 ];
00102 }
00103
00104 int Transaction::getCleared ( int id )
00105 {
00106 char **results;
00107 sqlite_get_table_printf ( tdb, "select cleared from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
00108 QString cleared = results [ 1 ];
00109 return cleared.toInt();
00110 }
00111
00112 void Transaction::setCleared ( int id, int cleared )
00113 {
00114 sqlite_exec_printf ( tdb, "update transactions set cleared = %i where transid = %i;", 0, 0, 0, cleared, id );
00115 }
00116
00117 int Transaction::getBudgetID ( int id )
00118 {
00119 char **results;
00120 sqlite_get_table_printf ( tdb, "select budgetid from transactions where transid = %i;", &results, NULL, NULL, NULL, id );
00121 QString budgetid = results [ 1 ];
00122 return budgetid.toInt();
00123 }
00124
00125 int Transaction::getLineItemID ( int id )
00126 {
00127 char **results;
00128 sqlite_get_table_printf ( tdb, "select reservedone from transactions where transid = %i;", &results, NULL, NULL, NULL, id );
00129 QString lineitemid = results [ 1 ];
00130 return lineitemid.toInt();
00131 }
00132
00133 int Transaction::getDay ( int id )
00134 {
00135 char **results;
00136 sqlite_get_table_printf ( tdb, "select day from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
00137 QString daystring = results [ 1 ];
00138 return daystring.toInt();
00139 }
00140
00141 int Transaction::getMonth ( int id )
00142 {
00143 char **results;
00144 sqlite_get_table_printf ( tdb, "select month from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
00145 QString monthstring = results [ 1 ];
00146 return monthstring.toInt();
00147 }
00148
00149 int Transaction::getYear ( int id )
00150 {
00151 char **results;
00152 sqlite_get_table_printf ( tdb, "select year from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
00153 QString yearstring = results [ 1 ];
00154 return yearstring.toInt();
00155 }
00156
00157 char ** Transaction::selectAllTransactions ( QDate fromdate, bool children, const char *limit, int id )
00158 {
00159
00160 char **results;
00161 int showcleared = preferences->getPreference ( 3 );
00162 QDate today = QDate::currentDate();
00163 int fromyear = fromdate.year();
00164 int toyear = today.year();
00165 int frommonth = fromdate.month();
00166 int tomonth = today.month();
00167 int fromday = fromdate.day();
00168
00169
00170 QString query = "select day, month, year, payee, amount, transid, accountid from transactions where";
00171
00172 if ( frommonth == tomonth && fromyear == toyear )
00173 {
00174 query.append ( " year = " );
00175 query.append ( QString::number ( toyear ) );
00176 query.append ( " and month = " );
00177 query.append ( QString::number ( tomonth ) );
00178 query.append ( " and day >= " );
00179 query.append ( QString::number ( fromday ) );
00180 query.append ( " and" );
00181 }
00182 else if ( frommonth != tomonth && fromyear == toyear )
00183 {
00184 query.append ( " year = " );
00185 query.append ( QString::number ( toyear ) );
00186 query.append ( " and ( ( month <= " );
00187 query.append ( QString::number ( tomonth ) );
00188 query.append ( " and month > " );
00189 query.append ( QString::number ( frommonth ) );
00190 query.append ( " ) or ( month = " );
00191 query.append ( QString::number ( frommonth ) );
00192 query.append ( " and day >= " );
00193 query.append ( QString::number ( fromday ) );
00194 query.append ( " ) ) and " );
00195 }
00196 else if ( fromyear != toyear && fromyear != 1900 )
00197 {
00198
00199
00200 int tmpfrommonth = 1;
00201 int tmpfromday = 1;
00202 query.append ( " ( year >= " );
00203 query.append ( QString::number ( fromyear ) );
00204 query.append ( " and ( month <= " );
00205 query.append ( QString::number ( tomonth ) );
00206 query.append ( " and month > " );
00207 query.append ( QString::number ( tmpfrommonth ) );
00208 query.append ( " ) or ( month = " );
00209 query.append ( QString::number ( tmpfrommonth ) );
00210 query.append ( " and day >= " );
00211 query.append ( QString::number ( tmpfromday ) );
00212 query.append ( " ) ) or" );
00213
00214
00215 int tmptomonth = 12;
00216 query.append ( " ( year = " );
00217 query.append ( QString::number ( fromyear ) );
00218 query.append ( " and ( ( month <= " );
00219 query.append ( QString::number ( tmptomonth ) );
00220 query.append ( " and month > " );
00221 query.append ( QString::number ( frommonth ) );
00222 query.append ( " ) or ( month = " );
00223 query.append ( QString::number ( frommonth ) );
00224 query.append ( " and day >= " );
00225 query.append ( QString::number ( fromday ) );
00226 query.append ( " ) ) ) and " );
00227 }
00228
00229 if ( account->getParentAccountID ( id ) == -1 && children == TRUE )
00230 query.append ( " parentid = %i and payee like '%q';" );
00231 else
00232 query.append ( " accountid = %i and payee like '%q';" );
00233
00234 sqlite_get_table_printf ( tdb, query, &results, &rows, &columns, NULL, id, limit );
00235 return results;
00236 }
00237
00238 char ** Transaction::selectNonClearedTransactions ( QDate fromdate, bool children, const char *limit, int id )
00239 {
00240 char **results;
00241 if ( account->getParentAccountID ( id ) == -1 && children == TRUE )
00242 sqlite_get_table_printf ( tdb, "select day, month, year, payee, amount, transid, accountid from transactions where cleared = 0 and parentid = %i and payee like '%q';", &results, &rows, &columns, NULL, id, limit );
00243 else
00244 sqlite_get_table_printf ( tdb, "select day, month, year, payee, amount, transid, accountid from transactions where cleared = 0 and accountid = %i and payee like '%q';", &results, &rows, &columns, NULL, id, limit );
00245 return results;
00246 }
00247
00248 void Transaction::displayTransactions ( QListView *listview, int id, bool children, const char *limit, QDate displaydate )
00249 {
00250 int showcleared = preferences->getPreference ( 3 );
00251
00252 char **results;
00253 if ( showcleared == 0 )
00254 results = selectNonClearedTransactions ( displaydate, children, limit, id );
00255 else
00256 results = selectAllTransactions ( displaydate, children, limit, id );
00257
00258
00259 int counter = 7;
00260 while ( counter < ( ( rows + 1 ) * columns ) )
00261 {
00262
00263 QString date = preferences->getDate ( atoi ( results [ counter + 2 ] ), atoi ( results [ counter + 1 ] ), atoi ( results [ counter ] ) );
00264
00265
00266 QString payee = results [ counter + 3 ];
00267 QString amount = results [ counter + 4 ];
00268 QString transferid = results [ counter + 5 ];
00269
00270
00271 QString accountname = account->getAccountName ( atoi ( results [ counter + 6 ] ) );
00272
00273
00274 if ( account->getParentAccountID ( id ) != -1 )
00275 {
00276 if ( showcleared == 1 && getCleared ( transferid.toInt() ) == 1 )
00277 ColorListItem *item = new ColorListItem ( listview, date, payee, amount, transferid );
00278 else
00279 QListViewItem *item = new QListViewItem ( listview, date, payee, amount, transferid );
00280 }
00281 else
00282 {
00283 if ( showcleared == 1 && getCleared ( transferid.toInt() ) == 1 )
00284 ColorListItem *item = new ColorListItem ( listview, date, payee, amount, transferid, accountname );
00285 else
00286 QListViewItem *item = new QListViewItem ( listview, date, payee, amount, transferid, accountname );
00287 }
00288
00289
00290 counter = counter + 7;
00291 }
00292 }
00293
00294 QString Transaction::getBudgetTotal ( int budgetid, int lineitemid, int year, int month, int viewtype )
00295 {
00296
00297
00298 char **results;
00299 switch ( viewtype )
00300 {
00301 case 1:
00302 sqlite_get_table_printf ( tdb, "select abs ( sum ( amount ) ) from transactions where year = %i and amount < 0 and budgetid = %i and reservedone = %i;", &results, NULL, NULL, NULL, year, budgetid, lineitemid );
00303 break;
00304
00305 case 0:
00306 sqlite_get_table_printf ( tdb, "select abs ( sum ( amount ) ) from transactions where year = %i and month = %i and amount < 0 and budgetid = %i and reservedone = %i;", &results, NULL, NULL, NULL, year, month, budgetid, lineitemid );
00307 break;
00308 }
00309 QString amount = results [ 1 ];
00310 float total = amount.toFloat();
00311 amount.setNum ( total, 'f', 2 );
00312 return amount;
00313 }
00314
00315 QString Transaction::getActualTotal ( int budgetid, int year, int month, int viewtype )
00316 {
00317
00318
00319 char **results;
00320 switch ( viewtype )
00321 {
00322 case 1:
00323 sqlite_get_table_printf ( tdb, "select abs ( sum ( amount ) ) from transactions where year = %i and amount < 0 and budgetid = %i;", &results, NULL, NULL, NULL, year, budgetid );
00324 break;
00325
00326 case 0:
00327 sqlite_get_table_printf ( tdb, "select abs ( sum ( amount ) ) from transactions where year = %i and month = %i and amount < 0 and budgetid = %i;", &results, NULL, NULL, NULL, year, month, budgetid );
00328 break;
00329 }
00330 QString amount = results [ 1 ];
00331 float total = amount.toFloat();
00332 amount.setNum ( total, 'f', 2 );
00333 return amount;
00334 }
00335
00336 void Transaction::clearBudgetIDs ( int budgetid, int lineitemid )
00337 {
00338 sqlite_exec_printf ( tdb, "update transactions set budgetid = -1 where budgetid = %i and reservedone = %i;", 0, 0, 0, budgetid, lineitemid );
00339 }
00340
00341 void Transaction::clearBudgetIDs ( int budgetid )
00342 {
00343 sqlite_exec_printf ( tdb, "update transactions set budgetid = -1 where budgetid = %i;", 0, 0, 0, budgetid );
00344 }
00345