summaryrefslogtreecommitdiff
authorallenforsythe <allenforsythe>2003-05-14 16:19:15 (UTC)
committer allenforsythe <allenforsythe>2003-05-14 16:19:15 (UTC)
commitb818d340ce6a9ffe00136f2278192f46c1163ba1 (patch) (unidiff)
tree419934d82f859d464c5325d0e73600f831fa4551
parent8c1e0e4eaf9bf4d1b6c4c6eb341e5e508341cd98 (diff)
downloadopie-b818d340ce6a9ffe00136f2278192f46c1163ba1.zip
opie-b818d340ce6a9ffe00136f2278192f46c1163ba1.tar.gz
opie-b818d340ce6a9ffe00136f2278192f46c1163ba1.tar.bz2
Initial revision
Diffstat (more/less context) (ignore whitespace changes)
-rwxr-xr-xnoncore/apps/qashmoney/account.cpp372
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
10extern Preferences *preferences;
11
12Account::Account ()
13 {
14 adb = sqlite_open ( "qmaccounts.db", 0, NULL );
15 }
16
17Account::~Account ()
18 {
19 sqlite_close ( adb );
20 }
21
22void 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
30void 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
35void Account::deleteAccount ( int accountid )
36 {
37 sqlite_exec_printf ( adb, "delete from accounts2 where accountid = %i;", 0, 0, 0, accountid );
38 }
39
40void 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
45int 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
55int 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
62int 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
69void 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
101void 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
110int 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
117int 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
124void 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
230int 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
253int 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
260int 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
267int 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
274int 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
281QString 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
288QString 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
295QString 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
302QString 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
309float 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
316float 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
323GreyBackgroundItem::GreyBackgroundItem ( QListView *parent )
324 : QListViewItem ( parent )
325 {
326 }
327
328GreyBackgroundItem::GreyBackgroundItem ( QListView *parent, QString label1, QString label2, QString label3 )
329 : QListViewItem ( parent, label1, label2, label3 )
330 {
331 }
332
333GreyBackgroundItem::GreyBackgroundItem ( QListView *parent, QString label1, QString label2, QString label3, QString label4 )
334 : QListViewItem ( parent, label1, label2, label3, label4 )
335 {
336 }
337
338GreyBackgroundItem::GreyBackgroundItem ( QListView *parent, QString label1, QString label2, QString label3, QString label4, QString label5 )
339 : QListViewItem ( parent, label1, label2, label3, label4, label5 )
340 {
341 }
342
343void 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
350QStringList 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
361QStringList 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