-rwxr-xr-x | noncore/apps/qashmoney/account.cpp | 372 |
1 files changed, 372 insertions, 0 deletions
diff --git a/noncore/apps/qashmoney/account.cpp b/noncore/apps/qashmoney/account.cpp new file mode 100755 index 0000000..fc2f8c1 --- a/dev/null +++ b/noncore/apps/qashmoney/account.cpp | |||
@@ -0,0 +1,372 @@ | |||
1 | #include "account.h" | ||
2 | #include "transaction.h" | ||
3 | #include "transfer.h" | ||
4 | #include "preferences.h" | ||
5 | |||
6 | #include <qpixmap.h> | ||
7 | #include <stdlib.h> | ||
8 | #include <iostream.h> | ||
9 | |||
10 | extern Preferences *preferences; | ||
11 | |||
12 | Account::Account () | ||
13 | { | ||
14 | adb = sqlite_open ( "qmaccounts.db", 0, NULL ); | ||
15 | } | ||
16 | |||
17 | Account::~Account () | ||
18 | { | ||
19 | sqlite_close ( adb ); | ||
20 | } | ||
21 | |||
22 | void Account::addAccount ( QString name, int parentid, float balance, int type, QString description, float creditlimit, | ||
23 | int statementyear, int statementmonth, int statementday, float statementbalance, const char *currency ) | ||
24 | { | ||
25 | int r = sqlite_exec_printf ( adb, "insert into accounts2 values ( '%q', %i, %.2f, %i, '%q', %.2f, %i, %i, %i, %.2f, '%q', 0, 0, 0, 0, 0, NULL );", 0, 0, 0, | ||
26 | (const char *) name, parentid, balance, type, (const char *) description, creditlimit, statementyear, statementmonth, statementday, statementbalance, currency ); | ||
27 | cout << "Results = " << r << endl; | ||
28 | } | ||
29 | |||
30 | void Account::updateAccount ( QString name, QString description, QString currencycode, int accountid ) | ||
31 | { | ||
32 | sqlite_exec_printf ( adb, "update accounts2 set name = '%q', description = '%q', currency = '%q' where accountid = %i;", 0, 0, 0, ( const char * ) name, ( const char * ) description, ( const char * ) currencycode, accountid ); | ||
33 | } | ||
34 | |||
35 | void Account::deleteAccount ( int accountid ) | ||
36 | { | ||
37 | sqlite_exec_printf ( adb, "delete from accounts2 where accountid = %i;", 0, 0, 0, accountid ); | ||
38 | } | ||
39 | |||
40 | void Account::setAccountExpanded ( int expanded, int accountid ) | ||
41 | { | ||
42 | sqlite_exec_printf ( adb, "update accounts2 set r1 = %i where accountid = %i;", 0, 0, 0, expanded, accountid ); | ||
43 | } | ||
44 | |||
45 | int Account::getAccountExpanded ( int id ) | ||
46 | { | ||
47 | char **results; | ||
48 | sqlite_get_table_printf ( adb, "select r1 from accounts2 where accountid = %i;", &results, 0, 0, 0, id ); | ||
49 | if ( strlen ( results [1] ) == 0 ) | ||
50 | return 0; | ||
51 | else | ||
52 | return atoi ( results [ 1 ] ); | ||
53 | } | ||
54 | |||
55 | int Account::getNumberOfAccounts () | ||
56 | { | ||
57 | char **results; | ||
58 | sqlite_get_table ( adb, "select count() from accounts2;", &results, NULL, NULL, NULL ); | ||
59 | return atoi ( results [ 1 ] ); | ||
60 | } | ||
61 | |||
62 | int Account::getNumberOfChildAccounts ( int id ) | ||
63 | { | ||
64 | char **results; | ||
65 | sqlite_get_table_printf ( adb, "select count() from accounts2 where parent = %i;", &results, NULL, NULL, NULL, id ); | ||
66 | return atoi ( results [ 1 ] ); | ||
67 | } | ||
68 | |||
69 | void Account::updateAccountBalance ( int accountid ) | ||
70 | { | ||
71 | // Here, we'll get a balance for the transactions in an account | ||
72 | sqlite *tdb = sqlite_open ( "qmtransactions.db", 0, NULL ); | ||
73 | int rows, columns; | ||
74 | char **results; | ||
75 | sqlite_get_table_printf ( tdb, "select sum (amount) from transactions where accountid= %i;", &results, &rows, &columns, NULL, accountid ); | ||
76 | float transactionsbalance = strtod ( results [ 1 ], 0 ); | ||
77 | sqlite_close ( tdb ); | ||
78 | |||
79 | // next, we'll get a balance for all the transfers from the account | ||
80 | sqlite *trdb = sqlite_open ( "qmtransfers.db", 0, NULL ); | ||
81 | rows = 0; | ||
82 | columns = 0; | ||
83 | char **results2; | ||
84 | sqlite_get_table_printf ( trdb, "select sum (amount) from transfers where fromaccount = %i;", &results2, &rows, &columns, NULL, accountid ); | ||
85 | float fromtransfersbalance = ( strtod ( results2 [ 1 ], 0 ) * -1 ); | ||
86 | |||
87 | // finally, we'll get a balance for all the transfers into the account | ||
88 | rows = 0; | ||
89 | columns= 0; | ||
90 | char **results3; | ||
91 | sqlite_get_table_printf ( trdb, "select sum (amount) from transfers where toaccount = %i;", &results3, &rows, &columns, NULL, accountid ); | ||
92 | float totransfersbalance = strtod ( results3 [ 1 ], 0 ); | ||
93 | |||
94 | sqlite_close ( trdb ); | ||
95 | |||
96 | // calculate and update new balance | ||
97 | sqlite_exec_printf ( adb, "update accounts2 set balance = %.2f where accountid = %i;", 0, 0, 0, | ||
98 | ( transactionsbalance + fromtransfersbalance + totransfersbalance + getStatementBalance ( accountid ) ), accountid ); | ||
99 | } | ||
100 | |||
101 | void Account::changeParentAccountBalance ( int parentid ) | ||
102 | { | ||
103 | // select all child balances that share the parent of the current child account | ||
104 | char **results; | ||
105 | int rows; | ||
106 | sqlite_get_table_printf ( adb, "select sum ( balance ) from accounts2 where parent = %i;", &results, &rows, NULL, NULL, parentid ); | ||
107 | sqlite_exec_printf ( adb, "update accounts2 set balance = %.2f where accountid = %i;", 0, 0, 0, strtod ( results[ 1 ], NULL ), parentid ); | ||
108 | } | ||
109 | |||
110 | int Account::getParentAccountID ( int id ) | ||
111 | { | ||
112 | char **results; | ||
113 | sqlite_get_table_printf ( adb, "select parent from accounts2 where accountid = %i;", &results, NULL, NULL, NULL, id ); | ||
114 | return atoi ( results [ 1 ] ); | ||
115 | } | ||
116 | |||
117 | int Account::getParentAccountID ( QString accountname ) | ||
118 | { | ||
119 | char **results; | ||
120 | sqlite_get_table_printf ( adb, "select parent from accounts2 where name= '%q';", &results, NULL, NULL, NULL, ( const char * ) accountname ); | ||
121 | return atoi ( results [ 1 ] ); | ||
122 | } | ||
123 | |||
124 | void Account::displayAccounts ( QListView *listview ) | ||
125 | { | ||
126 | char **results; | ||
127 | int rows, columns; | ||
128 | sqlite_get_table ( adb, "select name, parent, balance, accountid, currency from accounts2;", &results, &rows, &columns, 0 ); | ||
129 | |||
130 | // determine if we are using currency support | ||
131 | int currency = preferences->getPreference ( 4 ); | ||
132 | |||
133 | // remove all columns from the account display | ||
134 | int counter; | ||
135 | for ( counter = 0; counter <= columns; counter++ ) | ||
136 | listview->removeColumn ( 0 ); | ||
137 | |||
138 | // add columns to the account display | ||
139 | listview->addColumn ( "Account", 0 ); | ||
140 | int columntoalign = 1; | ||
141 | if ( preferences->getPreference ( 4 ) == 1 ) // add the currency column if the user wants it | ||
142 | { | ||
143 | listview->addColumn ( "C", 0 ); | ||
144 | columntoalign = 2; | ||
145 | } | ||
146 | listview->addColumn ( "Balance", 0 ); | ||
147 | listview->addColumn ( "", 0 ); | ||
148 | listview->setColumnAlignment ( columntoalign, Qt::AlignRight ); | ||
149 | counter = 5; | ||
150 | int total = ( rows + 1 ) * columns; | ||
151 | while ( counter < total ) | ||
152 | { | ||
153 | int accountid = atoi ( results [ counter + 3 ] ); | ||
154 | if ( atoi ( results [ counter + 1 ] ) == -1 ) | ||
155 | { | ||
156 | QListViewItem *parent = new QListViewItem ( listview ); | ||
157 | parent->setText ( 0, results [ counter ] ); | ||
158 | if ( currency == 0 ) | ||
159 | { | ||
160 | parent->setText ( 1, results [ counter + 2 ] ); | ||
161 | parent->setText ( 2, results [ counter + 3 ] ); | ||
162 | } | ||
163 | else | ||
164 | { | ||
165 | if ( getNumberOfChildAccounts ( accountid ) == 0 ) // add the currency flag if this is a parent with no children | ||
166 | { | ||
167 | // create the string we'll use to set the currency pixmap | ||
168 | QString filename = "/opt/QtPalmtop/pics/flags/"; | ||
169 | QString flag = results [ counter + 4 ]; | ||
170 | filename.append ( flag ); | ||
171 | filename.append ( ".png" ); | ||
172 | parent->setPixmap ( 1, QPixmap ( filename ) ); | ||
173 | parent->setText ( 1, flag ); | ||
174 | } | ||
175 | parent->setText ( 2, results [ counter + 2 ] ); | ||
176 | parent->setText ( 3, results [ counter + 3 ] ); | ||
177 | } | ||
178 | |||
179 | if ( getAccountExpanded ( accountid ) == 1 ) | ||
180 | parent->setOpen ( TRUE ); | ||
181 | |||
182 | //Start display child accounts for this parent | ||
183 | int childcounter = 5; | ||
184 | while ( childcounter < total ) | ||
185 | { | ||
186 | if ( atoi ( results [ childcounter + 1 ] ) == accountid ) | ||
187 | { | ||
188 | if ( currency == 0 ) | ||
189 | QListViewItem *child = new QListViewItem ( parent, results [ childcounter ], results [ childcounter + 2 ], results [ childcounter + 3 ] ); | ||
190 | else | ||
191 | { | ||
192 | // create the string we'll use to set the currency pixmap | ||
193 | QString filename = "/opt/QtPalmtop/pics/flags/"; | ||
194 | QString flag = results [ childcounter + 4 ]; | ||
195 | filename.append ( flag ); | ||
196 | filename.append ( ".png" ); | ||
197 | QListViewItem *child = new QListViewItem ( parent, results [ childcounter ], "", results [ childcounter + 2 ], results [ childcounter + 3 ] ); | ||
198 | child->setPixmap ( 1, QPixmap ( filename ) ); | ||
199 | child->setText ( 1, flag ); | ||
200 | } | ||
201 | } | ||
202 | childcounter = childcounter + 5; | ||
203 | } | ||
204 | //End display child accounts | ||
205 | } | ||
206 | counter = counter + 5; | ||
207 | } | ||
208 | |||
209 | // resize all columns appropriately | ||
210 | if ( preferences->getPreference ( 4 ) == 0 ) | ||
211 | { | ||
212 | listview->setColumnWidth ( 0, preferences->getColumnPreference ( 1 ) ); | ||
213 | listview->setColumnWidthMode ( 0, QListView::Manual ); | ||
214 | listview->setColumnWidth ( 1, preferences->getColumnPreference ( 2 ) ); | ||
215 | listview->setColumnWidthMode ( 1, QListView::Manual ); | ||
216 | listview->setColumnWidthMode ( 2, QListView::Manual ); | ||
217 | } | ||
218 | else | ||
219 | { | ||
220 | listview->setColumnWidth ( 0, preferences->getColumnPreference ( 10 ) ); | ||
221 | listview->setColumnWidthMode ( 0, QListView::Manual ); | ||
222 | listview->setColumnWidth ( 1, preferences->getColumnPreference ( 11 ) ); | ||
223 | listview->setColumnWidthMode ( 1, QListView::Manual ); | ||
224 | listview->setColumnWidth ( 2, preferences->getColumnPreference ( 12 ) ); | ||
225 | listview->setColumnWidthMode ( 2, QListView::Manual ); | ||
226 | listview->setColumnWidthMode ( 3, QListView::Manual ); | ||
227 | } | ||
228 | } | ||
229 | |||
230 | int Account::displayParentAccountNames ( QComboBox *combobox, QString indexstring ) | ||
231 | { | ||
232 | char **results; | ||
233 | int rows, columns, index; | ||
234 | index = 0; | ||
235 | sqlite_get_table ( adb, "select name from accounts2 order by name asc;", &results, &rows, &columns, NULL ); | ||
236 | int counter = 1; | ||
237 | int indexcounter = 1; | ||
238 | int total = ( rows + 1 ) * columns; | ||
239 | while ( counter < total ) | ||
240 | { | ||
241 | if ( getParentAccountID ( results [ counter ] ) == -1 ) | ||
242 | { | ||
243 | combobox->insertItem ( results [ counter ], -1 ); | ||
244 | if ( strcmp ( results [ counter ], indexstring ) == 0 ) | ||
245 | index = indexcounter; | ||
246 | indexcounter++; | ||
247 | } | ||
248 | counter ++; | ||
249 | } | ||
250 | return index; | ||
251 | } | ||
252 | |||
253 | int Account::getAccountType ( int accountid ) | ||
254 | { | ||
255 | char **results; | ||
256 | sqlite_get_table_printf ( adb, "select type from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid ); | ||
257 | return atoi ( results [ 1 ] ); | ||
258 | } | ||
259 | |||
260 | int Account::getStatementDay ( int accountid ) | ||
261 | { | ||
262 | char **results; | ||
263 | sqlite_get_table_printf ( adb, "select statementday from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid ); | ||
264 | return atoi ( results [ 1 ] ); | ||
265 | } | ||
266 | |||
267 | int Account::getStatementMonth ( int accountid ) | ||
268 | { | ||
269 | char **results; | ||
270 | sqlite_get_table_printf ( adb, "select statementmonth from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid ); | ||
271 | return atoi ( results [ 1 ] ); | ||
272 | } | ||
273 | |||
274 | int Account::getStatementYear ( int accountid ) | ||
275 | { | ||
276 | char **results; | ||
277 | sqlite_get_table_printf ( adb, "select statementyear from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid ); | ||
278 | return atoi ( results [ 1 ] ); | ||
279 | } | ||
280 | |||
281 | QString Account::getAccountDescription ( int accountid ) | ||
282 | { | ||
283 | char **results; | ||
284 | sqlite_get_table_printf ( adb, "select description from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid ); | ||
285 | return ( QString ) results [ 1 ]; | ||
286 | } | ||
287 | |||
288 | QString Account::getCurrencyCode ( int accountid ) | ||
289 | { | ||
290 | char **results; | ||
291 | sqlite_get_table_printf ( adb, "select currency from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid ); | ||
292 | return ( QString ) results [ 1 ]; | ||
293 | } | ||
294 | |||
295 | QString Account::getAccountName ( int accountid ) | ||
296 | { | ||
297 | char **results; | ||
298 | sqlite_get_table_printf ( adb, "select name from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid ); | ||
299 | return ( QString ) results [ 1 ]; | ||
300 | } | ||
301 | |||
302 | QString Account::getAccountBalance ( int accountid ) | ||
303 | { | ||
304 | char **results; | ||
305 | sqlite_get_table_printf ( adb, "select balance from accounts2 where accountid= %i;", &results, NULL, NULL, NULL, accountid ); | ||
306 | return ( QString ) results [ 1 ]; | ||
307 | } | ||
308 | |||
309 | float Account::getAccountCreditLimit ( int accountid ) | ||
310 | { | ||
311 | char **results; | ||
312 | sqlite_get_table_printf ( adb, "select creditlimit from accounts2 where accountid = %i;", &results, NULL, NULL, NULL, accountid ); | ||
313 | return strtod ( results [ 1 ], NULL ); | ||
314 | } | ||
315 | |||
316 | float Account::getStatementBalance ( int accountid ) | ||
317 | { | ||
318 | char **results; | ||
319 | sqlite_get_table_printf ( adb, "select statementbalance from accounts2 where accountid = %i;", &results, NULL, NULL, NULL, accountid ); | ||
320 | return strtod ( results [ 1 ], NULL ); | ||
321 | } | ||
322 | |||
323 | GreyBackgroundItem::GreyBackgroundItem ( QListView *parent ) | ||
324 | : QListViewItem ( parent ) | ||
325 | { | ||
326 | } | ||
327 | |||
328 | GreyBackgroundItem::GreyBackgroundItem ( QListView *parent, QString label1, QString label2, QString label3 ) | ||
329 | : QListViewItem ( parent, label1, label2, label3 ) | ||
330 | { | ||
331 | } | ||
332 | |||
333 | GreyBackgroundItem::GreyBackgroundItem ( QListView *parent, QString label1, QString label2, QString label3, QString label4 ) | ||
334 | : QListViewItem ( parent, label1, label2, label3, label4 ) | ||
335 | { | ||
336 | } | ||
337 | |||
338 | GreyBackgroundItem::GreyBackgroundItem ( QListView *parent, QString label1, QString label2, QString label3, QString label4, QString label5 ) | ||
339 | : QListViewItem ( parent, label1, label2, label3, label4, label5 ) | ||
340 | { | ||
341 | } | ||
342 | |||
343 | void GreyBackgroundItem::paintCell ( QPainter *p, const QColorGroup &cg, int column, int width, int alignment ) | ||
344 | { | ||
345 | QColorGroup _cg ( cg ); | ||
346 | _cg.setColor ( QColorGroup::Base, Qt::lightGray ); | ||
347 | QListViewItem::paintCell ( p, _cg, column, width, alignment ); | ||
348 | } | ||
349 | |||
350 | QStringList Account::getAccountNames () | ||
351 | { | ||
352 | QStringList accountnames; | ||
353 | char **results; | ||
354 | int rows, counter; | ||
355 | sqlite_get_table ( adb, "select name from accounts2;", &results, &rows, 0, 0 ); | ||
356 | for ( counter = 0; counter < rows; counter++ ) | ||
357 | accountnames.append ( results [ counter+1 ] ); | ||
358 | return accountnames; | ||
359 | } | ||
360 | |||
361 | QStringList Account::getAccountIDs () | ||
362 | { | ||
363 | QStringList accountids; | ||
364 | char **results; | ||
365 | int rows, counter; | ||
366 | sqlite_get_table ( adb, "select accountid from accounts2;", &results, &rows, 0, 0 ); | ||
367 | for ( counter = 0; counter < rows; counter++ ) | ||
368 | accountids.append ( results [ counter+1 ] ); | ||
369 | return accountids; | ||
370 | } | ||
371 | |||
372 | |||