00001 #include "transfer.h"
00002 #include "account.h"
00003 #include "transactiondisplay.h"
00004 #include <stdlib.h>
00005
00006 extern Account *account;
00007 extern Preferences *preferences;
00008
00009 Transfer::Transfer ()
00010 {
00011 db = sqlite_open ( "qmtransfers.db", 0, 0 );
00012 }
00013
00014 Transfer::~Transfer ()
00015 {
00016 sqlite_close ( db );
00017 }
00018
00019 void Transfer::addTransfer ( int fromaccount, int fromparent, int toaccount, int toparent, int day, int month, int year, float amount, int cleared )
00020 {
00021 int nextrowid = -1;
00022 char **results;
00023 sqlite_get_table ( db, "select count() from transfers;", &results, 0, 0, 0 );
00024 if ( atoi ( results [ 1 ] ) != 0 )
00025 {
00026 char **results;
00027 sqlite_get_table ( db, "select min ( rowid ) from transfers;", &results, 0, 0, 0 );
00028 nextrowid = ( atoi ( results [ 1 ] ) ) - 1;
00029 }
00030 sqlite_exec_printf ( db, "insert into transfers values ( %i, %i, %i, %i, %i, %i, %i, 0, 0, %.2f, %i, 0, 0, 0, 0, 0, %i );", 0, 0, 0, fromaccount, fromparent, toaccount, toparent, day, month, year, amount, cleared, nextrowid );
00031 }
00032
00033 void Transfer::updateTransfer ( int fromaccount, int fromparent, int toaccount, int toparent, int day, int month, int year, float amount, int cleared, int transferid )
00034 {
00035 sqlite_exec_printf ( db, "update transfers set fromaccount = %i, fromparent = %i, toaccount = %i, toparent = %i, day = %i, month = %i, year = %i,"
00036 "amount = %.2f, cleared = %i where transferid = %i;", 0, 0, 0, fromaccount, fromparent, toaccount, toparent, day, month, year, amount, cleared, transferid );
00037 }
00038
00039 void Transfer::deleteTransfer ( int transferid )
00040 {
00041 sqlite_exec_printf ( db, "delete from transfers where transferid = %i;", 0, 0, 0, transferid );
00042 }
00043
00044 void Transfer::deleteAllTransfers ( int accountid )
00045 {
00046 sqlite_exec_printf ( db, "delete from transfers where fromaccount = %i;", 0, 0, 0, accountid );
00047 sqlite_exec_printf ( db, "delete from transfers where toaccount = %i;", 0, 0, 0, accountid );
00048 }
00049
00050 int Transfer::getNumberOfTransfers ()
00051 {
00052 char **results;
00053 sqlite_get_table ( db, "select count() from transfers;", &results, 0, 0, 0 );
00054 return atoi ( results [ 1 ] );
00055 }
00056
00057 int Transfer::getNumberOfTransfers ( int accountid )
00058 {
00059 char **results;
00060 sqlite_get_table_printf ( db, "select count() from transfers where fromaccount = %i;", &results, 0, 0, 0, accountid );
00061 int transfers = atoi ( results [ 1 ] );
00062 sqlite_get_table_printf ( db, "select count() from transfers where toaccount = %i;", &results, 0, 0, 0, accountid );
00063 transfers = transfers + atoi ( results [ 1 ] );
00064 return transfers;
00065 }
00066
00067 void Transfer::displayTransfers ( QListView *listview, int accountid, bool children, QDate displaydate )
00068 {
00069 int showcleared = preferences->getPreference ( 3 );
00070
00071
00072 char **results;
00073 int rows, columns;
00074 if ( account->getParentAccountID ( accountid ) == -1 && children == TRUE )
00075 {
00076 if ( showcleared == 0 )
00077 sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where cleared = 0 and toparent = %i;", &results, &rows, &columns, 0, accountid );
00078 else
00079 sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where toparent = %i;", &results, &rows, &columns, 0, accountid );
00080 }
00081 else
00082 {
00083 if ( showcleared == 0 )
00084 sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where cleared = 0 and toaccount = %i;", &results, &rows, &columns, 0, accountid );
00085 else
00086 sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where toaccount = %i;", &results, &rows, &columns, 0, accountid );
00087 }
00088
00089
00090 int counter = 7;
00091 int position = 0;
00092 while ( counter < ( ( rows + 1 ) * columns ) )
00093 {
00094
00095 QString daystring = results [ counter ];
00096 int day = daystring.toInt ();
00097 QString monthstring = results [ counter + 1 ];
00098 int month = monthstring.toInt ();
00099 QString yearstring = results [ counter + 2 ];
00100 int year = yearstring.toInt ();
00101 QString date = preferences->getDate ( year, month, day );
00102 QDate testdate ( year, month, day );
00103
00104
00105 QString amount = results [ counter + 3 ];
00106 QString id = results [ counter + 4 ];
00107
00108
00109 QString transactionname = "FROM: ";
00110 QString temp1 = results [ counter + 5 ];
00111 transactionname.append ( account->getAccountName ( temp1.toInt() ) );
00112
00113 QString toaccount = account->getAccountName ( atol ( results [ counter + 6 ] ) );
00114
00115 if ( testdate >= displaydate || showcleared == 0 )
00116 {
00117
00118 if ( account->getParentAccountID ( accountid ) == -1 )
00119 {
00120 if ( showcleared == 1 && getCleared ( id.toInt() ) == 1 )
00121 ColorListItem *item = new ColorListItem ( listview, date, transactionname, amount, id, toaccount );
00122 else
00123 QListViewItem *item = new QListViewItem ( listview, date, transactionname, amount, id, toaccount );
00124 }
00125 else
00126 {
00127 if ( showcleared == 1 && getCleared ( id.toInt() ) == 1 )
00128 ColorListItem *item = new ColorListItem ( listview, date, transactionname, amount, id );
00129 else
00130 QListViewItem *item = new QListViewItem ( listview, date, transactionname, amount, id );
00131 }
00132 }
00133 counter = counter + 7;
00134 }
00135
00136
00137 char **toresults;
00138 rows = 0;
00139 columns = 0;
00140 if ( account->getParentAccountID ( accountid ) == -1 && children == TRUE )
00141 {
00142 if ( showcleared == 0 )
00143 sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where cleared = 0 and fromparent = %i;", &toresults, &rows, &columns, 0, accountid );
00144 else
00145 sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where fromparent = %i;", &toresults, &rows, &columns, 0, accountid );
00146 }
00147 else
00148 {
00149 if ( showcleared == 0 )
00150 sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where cleared = 0 and fromaccount = %i;", &toresults, &rows, &columns, 0, accountid );
00151 else
00152 sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where fromaccount = %i;", &toresults, &rows, &columns, 0, accountid );
00153 }
00154
00155
00156 counter = 7;
00157 position = 0;
00158 while ( counter < ( ( rows + 1 ) * columns ) )
00159 {
00160
00161 QString daystring = toresults [ counter ];
00162 int day = daystring.toInt ();
00163 QString monthstring = toresults [ counter + 1 ];
00164 int month = monthstring.toInt ();
00165 QString yearstring = toresults [ counter + 2 ];
00166 int year = yearstring.toInt ();
00167 QString date = preferences->getDate ( year, month, day );
00168 QDate testdate ( year, month, day );
00169
00170
00171 QString amount = toresults [ counter + 3 ];
00172 amount.prepend ( "-" );
00173 QString id = toresults [ counter + 4 ];
00174
00175
00176 QString transactionname = "TO: ";
00177 QString temp1 = toresults [ counter + 6 ];
00178 transactionname.append ( account->getAccountName ( temp1.toInt() ) );
00179
00180 QString fromaccount = account->getAccountName ( atol ( toresults [ counter + 5 ] ) );
00181
00182 if ( testdate >= displaydate || showcleared == 0 )
00183 {
00184
00185 if ( account->getParentAccountID ( accountid ) == -1 )
00186 {
00187 if ( showcleared == 1 && getCleared ( id.toInt() ) == 1 )
00188 ColorListItem *item = new ColorListItem ( listview, date, transactionname, amount, id, fromaccount );
00189 else
00190 QListViewItem *item = new QListViewItem ( listview, date, transactionname, amount, id, fromaccount );
00191 }
00192 else
00193 {
00194 if ( showcleared == 1 && getCleared ( id.toInt() ) == 1 )
00195 ColorListItem *item = new ColorListItem ( listview, date, transactionname, amount, id );
00196 else
00197 QListViewItem *item = new QListViewItem ( listview, date, transactionname, amount, id );
00198 }
00199 }
00200
00201 counter = counter + 7;
00202 }
00203 }
00204
00205 int Transfer::getCleared ( int id )
00206 {
00207 char **results;
00208 sqlite_get_table_printf ( db, "select cleared from transfers where transferid= %i;", &results, 0, 0, 0, id );
00209 return atoi ( results [ 1 ] );
00210 }
00211
00212 void Transfer::setCleared ( int id, int cleared )
00213 {
00214 sqlite_exec_printf ( db, "update transfers set cleared = %i where transferid = %i;", 0, 0, 0, cleared, id );
00215 }
00216
00217 int Transfer::getFromAccountID ( int id )
00218 {
00219 char **results;
00220 sqlite_get_table_printf ( db, "select fromaccount from transfers where transferid= %i;", &results, 0, 0, 0, id );
00221 return atoi ( results [ 1 ] );
00222 }
00223
00224 int Transfer::getToAccountID ( int id )
00225 {
00226 char **results;
00227 sqlite_get_table_printf ( db, "select toaccount from transfers where transferid= %i;", &results, 0, 0, 0, id );
00228 return atoi ( results [ 1 ] );
00229 }
00230
00231 int Transfer::getDay ( int id )
00232 {
00233 char **results;
00234 sqlite_get_table_printf ( db, "select day from transfers where transferid= %i;", &results, 0, 0, 0, id );
00235 return atoi ( results [ 1 ] );
00236 }
00237
00238 int Transfer::getMonth ( int id )
00239 {
00240 char **results;
00241 sqlite_get_table_printf ( db, "select month from transfers where transferid= %i;", &results, 0, 0, 0, id );
00242 return atoi ( results [ 1 ] );
00243 }
00244
00245 int Transfer::getYear ( int id )
00246 {
00247 char **results;
00248 sqlite_get_table_printf ( db, "select year from transfers where transferid= %i;", &results, 0, 0, 0, id );
00249 return atoi ( results [ 1 ] );
00250 }
00251
00252 QString Transfer::getAmount ( int id )
00253 {
00254 char **results;
00255 sqlite_get_table_printf ( db, "select amount from transfers where transferid= %i;", &results, 0, 0, 0, id );
00256 return results [ 1 ];
00257 }
00258
00259