summaryrefslogtreecommitdiff
path: root/noncore/apps/qashmoney/transaction.cpp
blob: 5ecc7ed8fc18cd83441cfa180bb2b1eefc2d0b75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// RESERVEDONE COLUMN NAME REPRESENTS THE LINEITEMID AND SHOULD BE CHANGED IN
// FUTURE VERSIONS OF QASHMONEY

// RESERVEDTWO REPRESENTS THE TRANSACTION DESCRIPTION

#include "transaction.h"
#include "account.h"
#include "transactiondisplay.h"

#include <stdlib.h>
#include <iostream.h>

extern Account *account;
extern Preferences *preferences;

Transaction::Transaction ()
  {
    tdb = sqlite_open ( "qmtransactions.db", 0, NULL );
  }

Transaction::~Transaction ()
  {
    sqlite_close ( tdb );
  }

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 )
  {
    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,
    0, 0, 0, NULL );", 0, 0, 0, ( const char * ) payee, accountid, parentid, number, day, month, year, amount, cleared, budgetid, lineitemid, ( const char * ) description );
  }

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 )
  {
    sqlite_exec_printf ( tdb, "update transactions set reservedtwo = '%q', payee = '%q', number = %i, day = %i, month = %i, year = %i, amount = %.2f,
      cleared = %i, budgetid = %i, reservedone = %i where transid = %i;", 0, 0, 0, ( const char * ) description, ( const char * ) payee, number, day, month, year,
      amount, cleared, budgetid, lineitemid, transactionid );
  }

void Transaction::deleteTransaction ( int transid )
  {
    sqlite_exec_printf ( tdb, "delete from transactions where transid = %i;", 0, 0, 0, transid );
  }

void Transaction::deleteAllTransactions ( int accountid )
  {
    sqlite_exec_printf ( tdb, "delete from transactions where accountid = %i;", 0, 0, 0, accountid );
  }

int Transaction::getAccountID ( int id )
  {
    char **results;
    sqlite_get_table_printf ( tdb, "select accountid from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
    return atol ( results [ 1 ] );
  }

int Transaction::getNumberOfTransactions ()
  {
    char **results;
    sqlite_get_table ( tdb, "select count() from transactions;", &results, NULL, NULL, NULL );
    return atoi ( results [ 1 ] );
  }

int Transaction::getNumberOfTransactions ( int accountid )
  {
    char **results;
    sqlite_get_table_printf ( tdb, "select count() from transactions where accountid = %i;", &results, NULL, NULL, NULL, accountid );
    return atol ( results [ 1 ] );
  }

QString Transaction::getPayee ( int id )
  {
    char **results;
    sqlite_get_table_printf ( tdb, "select payee from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
    return results [ 1 ];
  }

QString Transaction::getTransactionDescription ( int id )
  {
    char **results;
    sqlite_get_table_printf ( tdb, "select reservedtwo from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
    return results [ 1 ];
  }

QString Transaction::getNumber ( int id )
  {
    char **results;
    sqlite_get_table_printf ( tdb, "select number from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
    return results [ 1 ];
  }

QString Transaction::getAmount ( int id )
  {
    char **results;
    sqlite_get_table_printf ( tdb, "select amount from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
    return results [ 1 ];
  }

QString Transaction::getAbsoluteAmount ( int id )
  {
    char **results;
    sqlite_get_table_printf ( tdb, "select abs ( amount ) from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
    return results [ 1 ];
  }

int Transaction::getCleared ( int id )
  {
    char **results;
    sqlite_get_table_printf ( tdb, "select cleared from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
    QString cleared = results [ 1 ];
    return cleared.toInt();
  }

void Transaction::setCleared ( int id, int cleared )
  {
    sqlite_exec_printf ( tdb, "update transactions set cleared = %i where transid = %i;", 0, 0, 0, cleared, id );
  }

int Transaction::getBudgetID ( int id )
  {
    char **results;
    sqlite_get_table_printf ( tdb, "select budgetid from transactions where transid = %i;", &results, NULL, NULL, NULL, id );
    QString budgetid = results [ 1 ];
    return budgetid.toInt();
  }

int Transaction::getLineItemID ( int id )
  {
    char **results;
    sqlite_get_table_printf ( tdb, "select reservedone from transactions where transid = %i;", &results, NULL, NULL, NULL, id );
    QString lineitemid = results [ 1 ];
    return lineitemid.toInt();
  }

int Transaction::getDay ( int id )
  {
    char **results;
    sqlite_get_table_printf ( tdb, "select day from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
    QString daystring = results [ 1 ];
    return daystring.toInt();
  }

int Transaction::getMonth ( int id )
  {
    char **results;
    sqlite_get_table_printf ( tdb, "select month from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
    QString monthstring = results [ 1 ];
    return monthstring.toInt();
  }

int Transaction::getYear ( int id )
  {
    char **results;
    sqlite_get_table_printf ( tdb, "select year from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
    QString yearstring = results [ 1 ];
    return yearstring.toInt();
  }

void Transaction::displayTransactions ( QListView *listview, int id, bool children, const char *limit, QDate displaydate )
  {
    int showcleared = preferences->getPreference ( 3 );
    int year = ( displaydate.year() ) - 1;

    // select the transactions to display
    // two different statements are used based on
    // whether we are showing cleared transactions
    char **results;
    int rows, columns;
    if ( showcleared == 0 )
      {
        if ( account->getParentAccountID ( id ) == -1 && children == TRUE )
          sqlite_get_table_printf ( tdb, "select day, month, year, payee, amount, transid, accountid from transactions where cleared = 0 and year >= %i parentid = %i and payee like '%q';", &results, &rows, &columns, NULL, year, id, limit );
        else
          sqlite_get_table_printf ( tdb, "select day, month, year, payee, amount, transid, accountid from transactions where cleared = 0 year >= %i accountid = %i and payee like '%q';", &results, &rows, &columns, NULL, year, id, limit );
      }
    else
      {
      if ( account->getParentAccountID ( id ) == -1 && children == TRUE )
          sqlite_get_table_printf ( tdb, "select day, month, year, payee, amount, transid, accountid from transactions where year >= %i and parentid = %i and payee like '%q';", &results, &rows, &columns, NULL, year, id, limit );
      else
        sqlite_get_table_printf ( tdb, "select day, month, year, payee, amount, transid, accountid from transactions where accountid = %i and payee like '%q';", &results, &rows, &columns, NULL, id, limit );
      }

    // iterate through the result list and display each item
    int counter = 7;
    while ( counter < ( ( rows + 1 ) * columns ) )
      {
        QDate displaydate ( atoi ( results [ counter + 2 ] ), atoi ( results [ counter + 1 ] ), atoi ( results [ counter ] ) );
        QString date = preferences->getDate ( atoi ( results [ counter + 2 ] ), atoi ( results [ counter + 1 ] ), atoi ( results [ counter ] ) );

	// construct transaction name, amount, id
	QString payee = results [ counter + 3 ];
	QString amount = results [ counter + 4 ];
	QString transferid = results [ counter + 5 ];

        //determine the account name of the child accounts that we're displaying
        QString accountname = account->getAccountName ( atoi ( results [ counter + 6 ] ) );

	// fill in values
        if ( account->getParentAccountID ( id ) != -1 ) // use these constructors if we're showing a child account
          {
	    if ( showcleared == 1 && getCleared ( transferid.toInt() ) == 1 )
	      ColorListItem *item = new ColorListItem ( listview, date, payee, amount, transferid);
	    else
	      QListViewItem *item = new QListViewItem ( listview, date, payee, amount, transferid );
          }
       else
         {
	    if ( showcleared == 1 && getCleared ( transferid.toInt() ) == 1 )
	      ColorListItem *item = new ColorListItem ( listview, date, payee, amount, transferid, accountname );
	    else
	      QListViewItem *item = new QListViewItem ( listview, date, payee, amount, transferid, accountname );
         }

	// advance counter
	counter = counter + 7;
      }
  }

QString Transaction::getBudgetTotal ( int budgetid, int lineitemid, int year, int month, int viewtype )
  {
    // determine if we are viewing a years, months, or days budget
    // we have to pick a different sum for each
    char **results;
    switch ( viewtype )
      {
        case 1: // we are viewing a year
        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 );
        break;

        case 0: // we are viewing a month
        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 );
        break;
      }
    QString amount = results [ 1 ];
    float total  = amount.toFloat();
    amount.setNum ( total, 'f', 2 );
    return amount;
  }

QString Transaction::getActualTotal ( int budgetid, int year, int month, int viewtype )
  {
    // determine if we are viewing a years, months, or days budget
    // we have to pick a different sum for each
    char **results;
    switch ( viewtype )
      {
        case 1: // we are viewing a year
        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 );
        break;

        case 0: // we are viewing a month
        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 );
        break;
      }
    QString amount = results [ 1 ];
    float total  = amount.toFloat();
    amount.setNum ( total, 'f', 2 );
    return amount;
  }

void Transaction::clearBudgetIDs ( int budgetid, int lineitemid )
  {
    sqlite_exec_printf ( tdb, "update transactions set budgetid = -1 where budgetid = %i and reservedone = %i;", 0, 0, 0, budgetid, lineitemid );
  }

void Transaction::clearBudgetIDs ( int budgetid )
  {
    sqlite_exec_printf ( tdb, "update transactions set budgetid = -1 where budgetid = %i;", 0, 0, 0, budgetid );
  }