summaryrefslogtreecommitdiff
path: root/noncore/unsupported/qashmoney/transfer.cpp
blob: ae1b748758fd07b3a910c53eb27a82b854dd1811 (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
#include "transfer.h"
#include "account.h"
#include "transactiondisplay.h"
#include <stdlib.h>

extern Account *account;
extern Preferences *preferences;

Transfer::Transfer ()
  {
    db = sqlite_open ( "qmtransfers.db", 0, 0 );
  }

Transfer::~Transfer ()
  {
    sqlite_close ( db );
  }

void Transfer::addTransfer ( int fromaccount, int fromparent, int toaccount, int toparent, int day, int month, int year, float amount, int cleared )
  {
    int nextrowid = -1;
    char **results;
    sqlite_get_table ( db, "select count() from transfers;", &results, 0, 0, 0 );
    if ( atoi ( results [ 1 ] ) != 0 )
      {
        char **results;
        sqlite_get_table ( db, "select min ( rowid ) from transfers;", &results, 0, 0, 0 );
        nextrowid = ( atoi ( results [ 1 ] ) ) - 1;
      }
    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 );
  }

void Transfer::updateTransfer ( int fromaccount, int fromparent, int toaccount, int toparent, int day, int month, int year, float amount, int cleared, int transferid )
  {
    sqlite_exec_printf ( db, "update transfers set fromaccount = %i, fromparent = %i, toaccount = %i, toparent = %i, day = %i, month = %i, year = %i,"
     "amount = %.2f, cleared = %i where transferid = %i;", 0, 0, 0, fromaccount, fromparent, toaccount, toparent, day, month, year, amount, cleared, transferid );
  }

void Transfer::deleteTransfer ( int transferid )
  {
    sqlite_exec_printf ( db, "delete from transfers where transferid = %i;", 0, 0, 0, transferid );
  }

void Transfer::deleteAllTransfers ( int accountid )
  {
    sqlite_exec_printf ( db, "delete from transfers where fromaccount = %i;", 0, 0, 0, accountid );
    sqlite_exec_printf ( db, "delete from transfers where toaccount = %i;", 0, 0, 0, accountid );
  }

int Transfer::getNumberOfTransfers ()
  {
    char **results;
    sqlite_get_table ( db, "select count() from transfers;", &results, 0, 0, 0 );
    return atoi ( results [ 1 ] );
  }

int Transfer::getNumberOfTransfers ( int accountid )
  {
    char **results;
    sqlite_get_table_printf ( db, "select count() from transfers where fromaccount = %i;", &results, 0, 0, 0, accountid );
    int transfers = atoi ( results [ 1 ] );
    sqlite_get_table_printf ( db, "select count() from transfers where toaccount = %i;", &results, 0, 0, 0, accountid );
    transfers = transfers + atoi ( results [ 1 ] );
    return transfers;
  }

void Transfer::displayTransfers ( QListView *listview, int accountid, bool children, QDate displaydate )
  {
    int showcleared = preferences->getPreference ( 3 );

    // select the from transfers to display
    char **results;
    int rows, columns;
    if ( account->getParentAccountID ( accountid ) == -1 && children == TRUE )
      {
        if ( showcleared == 0 )
          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 );
        else
          sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where toparent = %i;", &results, &rows, &columns, 0, accountid );
      }
    else
      {
        if ( showcleared == 0 )
          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 );
        else
          sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where toaccount = %i;", &results, &rows, &columns, 0, accountid );
      }

    // iterate through the list and display the from items
    int counter = 7;
    int position = 0;
    while ( counter < ( ( rows + 1 ) * columns ) )
      {
        // construct the date
	QString daystring = results [ counter ];
	int day = daystring.toInt ();
	QString monthstring = results [ counter + 1 ];
	int month = monthstring.toInt ();
	QString yearstring = results [ counter + 2 ];
	int year = yearstring.toInt ();
	QString date = preferences->getDate ( year, month, day );
        QDate testdate ( year, month, day );

        //construct the amount and id strings
        QString amount = results [ counter + 3 ];
        QString id = results [ counter + 4 ];

        // construct the transaction name
        QString transactionname = "FROM: ";
        QString temp1 = results [ counter + 5 ];
        transactionname.append ( account->getAccountName ( temp1.toInt() ) );

        QString toaccount = account->getAccountName ( atol ( results [ counter + 6 ] ) );

        if ( testdate >= displaydate || showcleared == 0 )
          {
            // display this transfer
            if ( account->getParentAccountID ( accountid ) == -1 )
              {
                if ( showcleared == 1 && getCleared ( id.toInt() ) == 1 )
                  ColorListItem *item = new ColorListItem ( listview, date, transactionname, amount, id, toaccount );
                else
                  QListViewItem *item = new QListViewItem ( listview, date, transactionname, amount, id, toaccount );
              }
            else
              {
                if ( showcleared == 1 && getCleared ( id.toInt() ) == 1 )
                  ColorListItem *item = new ColorListItem ( listview, date, transactionname, amount, id );
                else
                  QListViewItem *item = new QListViewItem ( listview, date, transactionname, amount, id );
              }
          }
        counter = counter + 7;
      }

    // select the to transfers to display
    char **toresults;
    rows = 0;
    columns = 0;
    if ( account->getParentAccountID ( accountid ) == -1 && children == TRUE )
      {
        if ( showcleared == 0 )
          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 );
        else
          sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where fromparent = %i;", &toresults, &rows, &columns, 0, accountid );
      }
    else
      {
        if ( showcleared == 0 )
          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 );
        else
          sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where fromaccount = %i;", &toresults, &rows, &columns, 0, accountid );
      }

    // iterate through the list and display the from items
    counter = 7;
    position = 0;
    while ( counter < ( ( rows + 1 ) * columns ) )
      {
        // construct the date
	QString daystring = toresults [ counter ];
	int day = daystring.toInt ();
	QString monthstring = toresults [ counter + 1 ];
	int month = monthstring.toInt ();
	QString yearstring = toresults [ counter + 2 ];
	int year = yearstring.toInt ();
	QString date = preferences->getDate ( year, month, day );
        QDate testdate ( year, month, day );

        //construct the amount and id strings
        QString amount = toresults [ counter + 3 ];
        amount.prepend ( "-" );
        QString id = toresults [ counter + 4 ];

        // construct the transaction name
        QString transactionname = "TO: ";
        QString temp1 = toresults [ counter + 6 ];
        transactionname.append ( account->getAccountName ( temp1.toInt() ) );

        QString fromaccount = account->getAccountName ( atol ( toresults [ counter + 5 ] ) );

         if ( testdate >= displaydate || showcleared == 0 )
           {
             // display this transfer
             if ( account->getParentAccountID ( accountid ) == -1 )
              {
                if ( showcleared == 1 && getCleared ( id.toInt() ) == 1 )
                  ColorListItem *item = new ColorListItem ( listview, date, transactionname, amount, id, fromaccount );
                else
                  QListViewItem *item = new QListViewItem ( listview, date, transactionname, amount, id, fromaccount );
              }
            else
              {
                if ( showcleared == 1 && getCleared ( id.toInt() ) == 1 )
                  ColorListItem *item = new ColorListItem ( listview, date, transactionname, amount, id );
                else
                  QListViewItem *item = new QListViewItem ( listview, date, transactionname, amount, id );
              }
          }

        counter = counter + 7;
      }
  }

int Transfer::getCleared ( int id )
  {
    char **results;
    sqlite_get_table_printf ( db, "select cleared from transfers where transferid= %i;", &results, 0, 0, 0, id );
    return atoi ( results [ 1 ] );
  }

void Transfer::setCleared ( int id, int cleared )
  {
    sqlite_exec_printf ( db, "update transfers set cleared = %i where transferid = %i;", 0, 0, 0, cleared, id );
  }

int Transfer::getFromAccountID ( int id )
  {
    char **results;
    sqlite_get_table_printf ( db, "select fromaccount from transfers where transferid= %i;", &results, 0, 0, 0, id );
    return atoi ( results [ 1 ] );
  }

int Transfer::getToAccountID ( int id )
  {
    char **results;
    sqlite_get_table_printf ( db, "select toaccount from transfers where transferid= %i;", &results, 0, 0, 0, id );
    return atoi ( results [ 1 ] );
  }

int Transfer::getDay ( int id )
  {
    char **results;
    sqlite_get_table_printf ( db, "select day from transfers where transferid= %i;", &results, 0, 0, 0, id );
    return atoi ( results [ 1 ] );
  }

int Transfer::getMonth ( int id )
  {
    char **results;
    sqlite_get_table_printf ( db, "select month from transfers where transferid= %i;", &results, 0, 0, 0, id );
    return atoi ( results [ 1 ] );
  }

int Transfer::getYear ( int id )
  {
    char **results;
    sqlite_get_table_printf ( db, "select year from transfers where transferid= %i;", &results, 0, 0, 0, id );
    return atoi ( results [ 1 ] );
  }

QString Transfer::getAmount ( int id )
  {
    char **results;
    sqlite_get_table_printf ( db, "select amount from transfers where transferid= %i;", &results, 0, 0, 0, id );
    return results [ 1 ];
  }