summaryrefslogtreecommitdiff
path: root/noncore/apps/qashmoney/transfer.cpp
Unidiff
Diffstat (limited to 'noncore/apps/qashmoney/transfer.cpp') (more/less context) (ignore whitespace changes)
-rwxr-xr-xnoncore/apps/qashmoney/transfer.cpp253
1 files changed, 253 insertions, 0 deletions
diff --git a/noncore/apps/qashmoney/transfer.cpp b/noncore/apps/qashmoney/transfer.cpp
new file mode 100755
index 0000000..77cbb4e
--- a/dev/null
+++ b/noncore/apps/qashmoney/transfer.cpp
@@ -0,0 +1,253 @@
1#include "transfer.h"
2#include "account.h"
3#include "transactiondisplay.h"
4#include <stdlib.h>
5#include <iostream.h>
6
7extern Account *account;
8extern Preferences *preferences;
9
10Transfer::Transfer ()
11 {
12 db = sqlite_open ( "qmtransfers.db", 0, 0 );
13 }
14
15Transfer::~Transfer ()
16 {
17 sqlite_close ( db );
18 }
19
20void Transfer::addTransfer ( int fromaccount, int fromparent, int toaccount, int toparent, int day, int month, int year, float amount, int cleared )
21 {
22 int nextrowid = -1;
23 char **results;
24 sqlite_get_table ( db, "select count() from transfers;", &results, 0, 0, 0 );
25 if ( atoi ( results [ 1 ] ) != 0 )
26 {
27 char **results;
28 sqlite_get_table ( db, "select min ( rowid ) from transfers;", &results, 0, 0, 0 );
29 nextrowid = ( atoi ( results [ 1 ] ) ) - 1;
30 }
31 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 );
32 }
33
34void Transfer::updateTransfer ( int fromaccount, int fromparent, int toaccount, int toparent, int day, int month, int year, float amount, int cleared, int transferid )
35 {
36 sqlite_exec_printf ( db, "update transfers set fromaccount = %i, fromparent = %i, toaccount = %i, toparent = %i, day = %i, month = %i, year = %i,
37 amount = %.2f, cleared = %i where transferid = %i;", 0, 0, 0, fromaccount, fromparent, toaccount, toparent, day, month, year, amount, cleared, transferid );
38 }
39
40void Transfer::deleteTransfer ( int transferid )
41 {
42 sqlite_exec_printf ( db, "delete from transfers where transferid = %i;", 0, 0, 0, transferid );
43 }
44
45void Transfer::deleteAllTransfers ( int accountid )
46 {
47 sqlite_exec_printf ( db, "delete from transfers where fromaccount = %i;", 0, 0, 0, accountid );
48 sqlite_exec_printf ( db, "delete from transfers where toaccount = %i;", 0, 0, 0, accountid );
49 }
50
51int Transfer::getNumberOfTransfers ()
52 {
53 char **results;
54 sqlite_get_table ( db, "select count() from transfers;", &results, 0, 0, 0 );
55 return atoi ( results [ 1 ] );
56 }
57
58int Transfer::getNumberOfTransfers ( int accountid )
59 {
60 char **results;
61 sqlite_get_table_printf ( db, "select count() from transfers where fromaccount = %i;", &results, 0, 0, 0, accountid );
62 int transfers = atoi ( results [ 1 ] );
63 sqlite_get_table_printf ( db, "select count() from transfers where toaccount = %i;", &results, 0, 0, 0, accountid );
64 transfers = transfers + atoi ( results [ 1 ] );
65 return transfers;
66 }
67
68void Transfer::displayTransfers ( QListView *listview, int accountid, bool children )
69 {
70 int showcleared = preferences->getPreference ( 3 );
71
72 // select the from transfers to display
73 char **results;
74 int rows, columns;
75 if ( account->getParentAccountID ( accountid ) == -1 && children == TRUE )
76 {
77 if ( showcleared == 0 )
78 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 );
79 else
80 sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where toparent = %i;", &results, &rows, &columns, 0, accountid );
81 }
82 else
83 {
84 if ( showcleared == 0 )
85 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 );
86 else
87 sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where toaccount = %i;", &results, &rows, &columns, 0, accountid );
88 }
89
90 // iterate through the list and display the from items
91 int counter = 7;
92 int position = 0;
93 while ( counter < ( ( rows + 1 ) * columns ) )
94 {
95 // construct the date
96 QString daystring = results [ counter ];
97 int day = daystring.toInt ();
98 QString monthstring = results [ counter + 1 ];
99 int month = monthstring.toInt ();
100 QString yearstring = results [ counter + 2 ];
101 int year = yearstring.toInt ();
102 QString date = preferences->getDate ( year, month, day );
103
104 //construct the amount and id strings
105 QString amount = results [ counter + 3 ];
106 QString id = results [ counter + 4 ];
107
108 // construct the transaction name
109 QString transactionname = "FROM: ";
110 QString temp1 = results [ counter + 5 ];
111 transactionname.append ( account->getAccountName ( temp1.toInt() ) );
112
113 QString toaccount = account->getAccountName ( atol ( results [ counter + 6 ] ) );
114
115 // display this transfer
116 if ( account->getParentAccountID ( accountid ) == -1 )
117 {
118 if ( showcleared == 1 && getCleared ( id.toInt() ) == 1 )
119 ColorListItem *item = new ColorListItem ( listview, date, transactionname, amount, id, toaccount );
120 else
121 QListViewItem *item = new QListViewItem ( listview, date, transactionname, amount, id, toaccount );
122 }
123 else
124 {
125 if ( showcleared == 1 && getCleared ( id.toInt() ) == 1 )
126 ColorListItem *item = new ColorListItem ( listview, date, transactionname, amount, id );
127 else
128 QListViewItem *item = new QListViewItem ( listview, date, transactionname, amount, id );
129 }
130
131 counter = counter + 7;
132 }
133
134 // select the to transfers to display
135 char **toresults;
136 rows = 0;
137 columns = 0;
138 if ( account->getParentAccountID ( accountid ) == -1 && children == TRUE )
139 {
140 if ( showcleared == 0 )
141 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 );
142 else
143 sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where fromparent = %i;", &toresults, &rows, &columns, 0, accountid );
144 }
145 else
146 {
147 if ( showcleared == 0 )
148 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 );
149 else
150 sqlite_get_table_printf ( db, "select day, month, year, amount, transferid, fromaccount, toaccount from transfers where fromaccount = %i;", &toresults, &rows, &columns, 0, accountid );
151 }
152
153 // iterate through the list and display the from items
154 counter = 7;
155 position = 0;
156 while ( counter < ( ( rows + 1 ) * columns ) )
157 {
158 // construct the date
159 QString daystring = toresults [ counter ];
160 int day = daystring.toInt ();
161 QString monthstring = toresults [ counter + 1 ];
162 int month = monthstring.toInt ();
163 QString yearstring = toresults [ counter + 2 ];
164 int year = yearstring.toInt ();
165 QString date = preferences->getDate ( year, month, day );
166
167 //construct the amount and id strings
168 QString amount = toresults [ counter + 3 ];
169 amount.prepend ( "-" );
170 QString id = toresults [ counter + 4 ];
171
172 // construct the transaction name
173 QString transactionname = "TO: ";
174 QString temp1 = toresults [ counter + 6 ];
175 transactionname.append ( account->getAccountName ( temp1.toInt() ) );
176
177 QString fromaccount = account->getAccountName ( atol ( toresults [ counter + 5 ] ) );
178
179 // display this transfer
180 if ( account->getParentAccountID ( accountid ) == -1 )
181 {
182 if ( showcleared == 1 && getCleared ( id.toInt() ) == 1 )
183 ColorListItem *item = new ColorListItem ( listview, date, transactionname, amount, id, fromaccount );
184 else
185 QListViewItem *item = new QListViewItem ( listview, date, transactionname, amount, id, fromaccount );
186 }
187 else
188 {
189 if ( showcleared == 1 && getCleared ( id.toInt() ) == 1 )
190 ColorListItem *item = new ColorListItem ( listview, date, transactionname, amount, id );
191 else
192 QListViewItem *item = new QListViewItem ( listview, date, transactionname, amount, id );
193 }
194
195 counter = counter + 7;
196 }
197 }
198
199int Transfer::getCleared ( int id )
200 {
201 char **results;
202 sqlite_get_table_printf ( db, "select cleared from transfers where transferid= %i;", &results, 0, 0, 0, id );
203 return atoi ( results [ 1 ] );
204 }
205
206void Transfer::setCleared ( int id, int cleared )
207 {
208 sqlite_exec_printf ( db, "update transfers set cleared = %i where transferid = %i;", 0, 0, 0, cleared, id );
209 }
210
211int Transfer::getFromAccountID ( int id )
212 {
213 char **results;
214 sqlite_get_table_printf ( db, "select fromaccount from transfers where transferid= %i;", &results, 0, 0, 0, id );
215 return atoi ( results [ 1 ] );
216 }
217
218int Transfer::getToAccountID ( int id )
219 {
220 char **results;
221 sqlite_get_table_printf ( db, "select toaccount from transfers where transferid= %i;", &results, 0, 0, 0, id );
222 return atoi ( results [ 1 ] );
223 }
224
225int Transfer::getDay ( int id )
226 {
227 char **results;
228 sqlite_get_table_printf ( db, "select day from transfers where transferid= %i;", &results, 0, 0, 0, id );
229 return atoi ( results [ 1 ] );
230 }
231
232int Transfer::getMonth ( int id )
233 {
234 char **results;
235 sqlite_get_table_printf ( db, "select month from transfers where transferid= %i;", &results, 0, 0, 0, id );
236 return atoi ( results [ 1 ] );
237 }
238
239int Transfer::getYear ( int id )
240 {
241 char **results;
242 sqlite_get_table_printf ( db, "select year from transfers where transferid= %i;", &results, 0, 0, 0, id );
243 return atoi ( results [ 1 ] );
244 }
245
246QString Transfer::getAmount ( int id )
247 {
248 char **results;
249 sqlite_get_table_printf ( db, "select amount from transfers where transferid= %i;", &results, 0, 0, 0, id );
250 return results [ 1 ];
251 }
252
253