summaryrefslogtreecommitdiff
path: root/noncore/apps/qashmoney/transaction.cpp
Unidiff
Diffstat (limited to 'noncore/apps/qashmoney/transaction.cpp') (more/less context) (ignore whitespace changes)
-rwxr-xr-xnoncore/apps/qashmoney/transaction.cpp274
1 files changed, 274 insertions, 0 deletions
diff --git a/noncore/apps/qashmoney/transaction.cpp b/noncore/apps/qashmoney/transaction.cpp
new file mode 100755
index 0000000..af7d18f
--- a/dev/null
+++ b/noncore/apps/qashmoney/transaction.cpp
@@ -0,0 +1,274 @@
1// RESERVEDONE COLUMN NAME REPRESENTS THE LINEITEMID AND SHOULD BE CHANGED IN
2// FUTURE VERSIONS OF QASHMONEY
3
4// RESERVEDTWO REPRESENTS THE TRANSACTION DESCRIPTION
5
6#include "transaction.h"
7#include "account.h"
8#include "transactiondisplay.h"
9
10#include <stdlib.h>
11
12extern Account *account;
13extern Preferences *preferences;
14
15Transaction::Transaction ()
16 {
17 tdb = sqlite_open ( "qmtransactions.db", 0, NULL );
18 }
19
20Transaction::~Transaction ()
21 {
22 sqlite_close ( tdb );
23 }
24
25void 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 )
26 {
27 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,
28 0, 0, 0, NULL );", 0, 0, 0, ( const char * ) payee, accountid, parentid, number, day, month, year, amount, cleared, budgetid, lineitemid, ( const char * ) description );
29 }
30
31void Transaction::updateTransaction ( QString description, QString payee, int number, int day, int month, int year, float amount, int cleared, int budgetid, int lineitemid, int transactionid )
32 {
33 sqlite_exec_printf ( tdb, "update transactions set reservedtwo = '%q', payee = '%q', number = %i, day = %i, month = %i, year = %i, amount = %.2f,
34 cleared = %i, budgetid = %i, reservedone = %i where transid = %i;", 0, 0, 0, ( const char * ) description, ( const char * ) payee, number, day, month, year,
35 amount, cleared, budgetid, lineitemid, transactionid );
36 }
37
38void Transaction::deleteTransaction ( int transid )
39 {
40 sqlite_exec_printf ( tdb, "delete from transactions where transid = %i;", 0, 0, 0, transid );
41 }
42
43void Transaction::deleteAllTransactions ( int accountid )
44 {
45 sqlite_exec_printf ( tdb, "delete from transactions where accountid = %i;", 0, 0, 0, accountid );
46 }
47
48int Transaction::getAccountID ( int id )
49 {
50 char **results;
51 sqlite_get_table_printf ( tdb, "select accountid from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
52 return atol ( results [ 1 ] );
53 }
54
55int Transaction::getNumberOfTransactions ()
56 {
57 char **results;
58 sqlite_get_table ( tdb, "select count() from transactions;", &results, NULL, NULL, NULL );
59 return atoi ( results [ 1 ] );
60 }
61
62int Transaction::getNumberOfTransactions ( int accountid )
63 {
64 char **results;
65 sqlite_get_table_printf ( tdb, "select count() from transactions where accountid = %i;", &results, NULL, NULL, NULL, accountid );
66 return atol ( results [ 1 ] );
67 }
68
69QString Transaction::getPayee ( int id )
70 {
71 char **results;
72 sqlite_get_table_printf ( tdb, "select payee from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
73 return results [ 1 ];
74 }
75
76QString Transaction::getTransactionDescription ( int id )
77 {
78 char **results;
79 sqlite_get_table_printf ( tdb, "select reservedtwo from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
80 return results [ 1 ];
81 }
82
83QString Transaction::getNumber ( int id )
84 {
85 char **results;
86 sqlite_get_table_printf ( tdb, "select number from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
87 return results [ 1 ];
88 }
89
90QString Transaction::getAmount ( int id )
91 {
92 char **results;
93 sqlite_get_table_printf ( tdb, "select amount from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
94 return results [ 1 ];
95 }
96
97QString Transaction::getAbsoluteAmount ( int id )
98 {
99 char **results;
100 sqlite_get_table_printf ( tdb, "select abs ( amount ) from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
101 return results [ 1 ];
102 }
103
104int Transaction::getCleared ( int id )
105 {
106 char **results;
107 sqlite_get_table_printf ( tdb, "select cleared from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
108 QString cleared = results [ 1 ];
109 return cleared.toInt();
110 }
111
112void Transaction::setCleared ( int id, int cleared )
113 {
114 sqlite_exec_printf ( tdb, "update transactions set cleared = %i where transid = %i;", 0, 0, 0, cleared, id );
115 }
116
117int Transaction::getBudgetID ( int id )
118 {
119 char **results;
120 sqlite_get_table_printf ( tdb, "select budgetid from transactions where transid = %i;", &results, NULL, NULL, NULL, id );
121 QString budgetid = results [ 1 ];
122 return budgetid.toInt();
123 }
124
125int Transaction::getLineItemID ( int id )
126 {
127 char **results;
128 sqlite_get_table_printf ( tdb, "select reservedone from transactions where transid = %i;", &results, NULL, NULL, NULL, id );
129 QString lineitemid = results [ 1 ];
130 return lineitemid.toInt();
131 }
132
133int Transaction::getDay ( int id )
134 {
135 char **results;
136 sqlite_get_table_printf ( tdb, "select day from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
137 QString daystring = results [ 1 ];
138 return daystring.toInt();
139 }
140
141int Transaction::getMonth ( int id )
142 {
143 char **results;
144 sqlite_get_table_printf ( tdb, "select month from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
145 QString monthstring = results [ 1 ];
146 return monthstring.toInt();
147 }
148
149int Transaction::getYear ( int id )
150 {
151 char **results;
152 sqlite_get_table_printf ( tdb, "select year from transactions where transid= %i;", &results, NULL, NULL, NULL, id );
153 QString yearstring = results [ 1 ];
154 return yearstring.toInt();
155 }
156
157void Transaction::displayTransactions ( QListView *listview, int id, bool children, const char *limit )
158 {
159 int showcleared = preferences->getPreference ( 3 );
160
161 // select the transactions to display
162 // two different statements are used based on
163 // whether we are showing cleared transactions
164 char **results;
165 int rows, columns;
166 if ( showcleared == 0 )
167 {
168 if ( account->getParentAccountID ( id ) == -1 && children == TRUE )
169 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 );
170 else
171 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 );
172 }
173 else
174 {
175 if ( account->getParentAccountID ( id ) == -1 && children == TRUE )
176 sqlite_get_table_printf ( tdb, "select day, month, year, payee, amount, transid, accountid from transactions where parentid = %i and payee like '%q';", &results, &rows, &columns, NULL, id, limit );
177 else
178 sqlite_get_table_printf ( tdb, "select day, month, year, payee, amount, transid, accountid from transactions where accountid = %i and payee like '%q';", &results, &rows, &columns, NULL, id, limit );
179 }
180
181 // iterate through the result list and display each item
182 int counter = 7;
183 while ( counter < ( ( rows + 1 ) * columns ) )
184 {
185 // construct the date
186 //QString daystring = results [ counter ];
187 //int day = results [ counter ].toInt ();
188 //QString monthstring = results [ counter + 1 ];
189 //int month = results [ counter + 1 ].toInt ();
190 //QString yearstring = results [ counter + 2 ];
191 //int year = results [ counter + 2 ].toInt ();
192 QString date = preferences->getDate ( atoi ( results [ counter + 2 ] ), atoi ( results [ counter + 1 ] ), atoi ( results [ counter ] ) );
193
194 // construct transaction name, amount, id
195 QString payee = results [ counter + 3 ];
196 QString amount = results [ counter + 4 ];
197 QString transferid = results [ counter + 5 ];
198
199 //determine the account name of the child accounts that we're displaying
200 QString accountname = account->getAccountName ( atoi ( results [ counter + 6 ] ) );
201
202 // fill in values
203 if ( account->getParentAccountID ( id ) != -1 ) // use these constructors if we're showing a child account
204 {
205 if ( showcleared == 1 && getCleared ( transferid.toInt() ) == 1 )
206 ColorListItem *item = new ColorListItem ( listview, date, payee, amount, transferid);
207 else
208 QListViewItem *item = new QListViewItem ( listview, date, payee, amount, transferid );
209 }
210 else
211 {
212 if ( showcleared == 1 && getCleared ( transferid.toInt() ) == 1 )
213 ColorListItem *item = new ColorListItem ( listview, date, payee, amount, transferid, accountname );
214 else
215 QListViewItem *item = new QListViewItem ( listview, date, payee, amount, transferid, accountname );
216 }
217
218 // advance counter
219 counter = counter + 7;
220 }
221 }
222
223QString Transaction::getBudgetTotal ( int budgetid, int lineitemid, int year, int month, int viewtype )
224 {
225 // determine if we are viewing a years, months, or days budget
226 // we have to pick a different sum for each
227 char **results;
228 switch ( viewtype )
229 {
230 case 1: // we are viewing a year
231 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 );
232 break;
233
234 case 0: // we are viewing a month
235 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 );
236 break;
237 }
238 QString amount = results [ 1 ];
239 float total = amount.toFloat();
240 amount.setNum ( total, 'f', 2 );
241 return amount;
242 }
243
244QString Transaction::getActualTotal ( int budgetid, int year, int month, int viewtype )
245 {
246 // determine if we are viewing a years, months, or days budget
247 // we have to pick a different sum for each
248 char **results;
249 switch ( viewtype )
250 {
251 case 1: // we are viewing a year
252 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 );
253 break;
254
255 case 0: // we are viewing a month
256 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 );
257 break;
258 }
259 QString amount = results [ 1 ];
260 float total = amount.toFloat();
261 amount.setNum ( total, 'f', 2 );
262 return amount;
263 }
264
265void Transaction::clearBudgetIDs ( int budgetid, int lineitemid )
266 {
267 sqlite_exec_printf ( tdb, "update transactions set budgetid = -1 where budgetid = %i and reservedone = %i;", 0, 0, 0, budgetid, lineitemid );
268 }
269
270void Transaction::clearBudgetIDs ( int budgetid )
271 {
272 sqlite_exec_printf ( tdb, "update transactions set budgetid = -1 where budgetid = %i;", 0, 0, 0, budgetid );
273 }
274