summaryrefslogtreecommitdiff
path: root/noncore/unsupported/qashmoney/transaction.cpp
blob: d008a4f06b208368995bd225cedc5f6c97e10258 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// 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>

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();
  }

char ** Transaction::selectAllTransactions ( QDate fromdate, bool children, const char *limit, int id )
  {
    // initialize variables
    char **results;
    int showcleared = preferences->getPreference ( 3 );
    QDate today = QDate::currentDate();
    int fromyear = fromdate.year();
    int toyear = today.year();
    int frommonth = fromdate.month();
    int tomonth = today.month();
    int fromday = fromdate.day();

    // construct the first part of the string
    QString query = "select day, month, year, payee, amount, transid, accountid from transactions where";

    if ( frommonth == tomonth && fromyear == toyear ) // our dates cross neither a month nor a year
      {
        query.append ( " year = " );
        query.append ( QString::number ( toyear ) );
        query.append ( " and month = " );
        query.append ( QString::number ( tomonth ) );
        query.append ( " and day >= " );
        query.append ( QString::number ( fromday ) );
        query.append ( " and" );
      }
    else if ( frommonth != tomonth && fromyear == toyear ) // our dates cross a month within the same year
      {
        query.append ( " year = " );
        query.append ( QString::number ( toyear ) );
        query.append ( " and ( ( month <= " );
        query.append ( QString::number ( tomonth ) );
        query.append ( " and month > " );
        query.append ( QString::number ( frommonth ) );
        query.append ( " ) or ( month = " );
        query.append ( QString::number ( frommonth ) );
        query.append ( " and day >= " );
        query.append ( QString::number ( fromday ) );
        query.append ( " ) ) and " );
      }
    else if ( fromyear != toyear && fromyear != 1900 ) // here we are showing transactions from an entire year
      {
        // divide this taks into two parts - get the transactions from the prior and then the current year
        // current year part
        int tmpfrommonth = 1; // set temporary from months and days to Jan. 1
        int tmpfromday = 1;
        query.append ( " ( year >= " );
        query.append ( QString::number ( fromyear ) );
        query.append ( " and ( month <= " );
        query.append ( QString::number ( tomonth ) );
        query.append ( " and month > " );
        query.append ( QString::number ( tmpfrommonth ) );
        query.append ( " ) or ( month = " );
        query.append ( QString::number ( tmpfrommonth ) );
        query.append ( " and day >= " );
        query.append ( QString::number ( tmpfromday ) );
        query.append ( " ) ) or" );

        // prior year part
        int tmptomonth = 12;
        query.append ( " ( year = " );
        query.append ( QString::number ( fromyear ) );
        query.append ( " and ( ( month <= " );
        query.append ( QString::number ( tmptomonth ) );
        query.append ( " and month > " );
        query.append ( QString::number ( frommonth ) );
        query.append ( " ) or ( month = " );
        query.append ( QString::number ( frommonth ) );
        query.append ( " and day >= " );
        query.append ( QString::number ( fromday ) );
        query.append ( " ) ) ) and " );
      }

    if ( account->getParentAccountID ( id ) == -1 && children == TRUE )
      query.append ( " parentid = %i and payee like '%q';" );
    else
      query.append ( " accountid = %i and payee like '%q';" );

    sqlite_get_table_printf ( tdb, query, &results, &rows, &columns, NULL, id, limit );
    return results;
  }

char ** Transaction::selectNonClearedTransactions ( QDate fromdate, bool children, const char *limit, int id )
  {
    char **results;
    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 parentid = %i and payee like '%q';", &results, &rows, &columns, NULL, id, limit );
    else
      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 );
    return results;
  }

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

    char **results;
    if ( showcleared == 0 )
      results = selectNonClearedTransactions ( displaydate, children, limit, id );
    else
      results = selectAllTransactions ( displaydate, children, limit, id );

    // iterate through the result list and display each item
    int counter = 7;
    while ( counter < ( ( rows + 1 ) * columns ) )
      {
        //QDate testdate ( 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 );
  }