summaryrefslogtreecommitdiff
path: root/noncore/unsupported/qashmoney/transaction.cpp
Unidiff
Diffstat (limited to 'noncore/unsupported/qashmoney/transaction.cpp') (more/less context) (ignore whitespace changes)
-rwxr-xr-xnoncore/unsupported/qashmoney/transaction.cpp345
1 files changed, 345 insertions, 0 deletions
diff --git a/noncore/unsupported/qashmoney/transaction.cpp b/noncore/unsupported/qashmoney/transaction.cpp
new file mode 100755
index 0000000..d008a4f
--- a/dev/null
+++ b/noncore/unsupported/qashmoney/transaction.cpp
@@ -0,0 +1,345 @@
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
157char ** Transaction::selectAllTransactions ( QDate fromdate, bool children, const char *limit, int id )
158 {
159 // initialize variables
160 char **results;
161 int showcleared = preferences->getPreference ( 3 );
162 QDate today = QDate::currentDate();
163 int fromyear = fromdate.year();
164 int toyear = today.year();
165 int frommonth = fromdate.month();
166 int tomonth = today.month();
167 int fromday = fromdate.day();
168
169 // construct the first part of the string
170 QString query = "select day, month, year, payee, amount, transid, accountid from transactions where";
171
172 if ( frommonth == tomonth && fromyear == toyear ) // our dates cross neither a month nor a year
173 {
174 query.append ( " year = " );
175 query.append ( QString::number ( toyear ) );
176 query.append ( " and month = " );
177 query.append ( QString::number ( tomonth ) );
178 query.append ( " and day >= " );
179 query.append ( QString::number ( fromday ) );
180 query.append ( " and" );
181 }
182 else if ( frommonth != tomonth && fromyear == toyear ) // our dates cross a month within the same year
183 {
184 query.append ( " year = " );
185 query.append ( QString::number ( toyear ) );
186 query.append ( " and ( ( month <= " );
187 query.append ( QString::number ( tomonth ) );
188 query.append ( " and month > " );
189 query.append ( QString::number ( frommonth ) );
190 query.append ( " ) or ( month = " );
191 query.append ( QString::number ( frommonth ) );
192 query.append ( " and day >= " );
193 query.append ( QString::number ( fromday ) );
194 query.append ( " ) ) and " );
195 }
196 else if ( fromyear != toyear && fromyear != 1900 ) // here we are showing transactions from an entire year
197 {
198 // divide this taks into two parts - get the transactions from the prior and then the current year
199 // current year part
200 int tmpfrommonth = 1; // set temporary from months and days to Jan. 1
201 int tmpfromday = 1;
202 query.append ( " ( year >= " );
203 query.append ( QString::number ( fromyear ) );
204 query.append ( " and ( month <= " );
205 query.append ( QString::number ( tomonth ) );
206 query.append ( " and month > " );
207 query.append ( QString::number ( tmpfrommonth ) );
208 query.append ( " ) or ( month = " );
209 query.append ( QString::number ( tmpfrommonth ) );
210 query.append ( " and day >= " );
211 query.append ( QString::number ( tmpfromday ) );
212 query.append ( " ) ) or" );
213
214 // prior year part
215 int tmptomonth = 12;
216 query.append ( " ( year = " );
217 query.append ( QString::number ( fromyear ) );
218 query.append ( " and ( ( month <= " );
219 query.append ( QString::number ( tmptomonth ) );
220 query.append ( " and month > " );
221 query.append ( QString::number ( frommonth ) );
222 query.append ( " ) or ( month = " );
223 query.append ( QString::number ( frommonth ) );
224 query.append ( " and day >= " );
225 query.append ( QString::number ( fromday ) );
226 query.append ( " ) ) ) and " );
227 }
228
229 if ( account->getParentAccountID ( id ) == -1 && children == TRUE )
230 query.append ( " parentid = %i and payee like '%q';" );
231 else
232 query.append ( " accountid = %i and payee like '%q';" );
233
234 sqlite_get_table_printf ( tdb, query, &results, &rows, &columns, NULL, id, limit );
235 return results;
236 }
237
238char ** Transaction::selectNonClearedTransactions ( QDate fromdate, bool children, const char *limit, int id )
239 {
240 char **results;
241 if ( account->getParentAccountID ( id ) == -1 && children == TRUE )
242 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 );
243 else
244 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 );
245 return results;
246 }
247
248void Transaction::displayTransactions ( QListView *listview, int id, bool children, const char *limit, QDate displaydate )
249 {
250 int showcleared = preferences->getPreference ( 3 );
251
252 char **results;
253 if ( showcleared == 0 )
254 results = selectNonClearedTransactions ( displaydate, children, limit, id );
255 else
256 results = selectAllTransactions ( displaydate, children, limit, id );
257
258 // iterate through the result list and display each item
259 int counter = 7;
260 while ( counter < ( ( rows + 1 ) * columns ) )
261 {
262 //QDate testdate ( atoi ( results [ counter + 2 ] ), atoi ( results [ counter + 1 ] ), atoi ( results [ counter ] ) );
263 QString date = preferences->getDate ( atoi ( results [ counter + 2 ] ), atoi ( results [ counter + 1 ] ), atoi ( results [ counter ] ) );
264
265 // construct transaction name, amount, id
266 QString payee = results [ counter + 3 ];
267 QString amount = results [ counter + 4 ];
268 QString transferid = results [ counter + 5 ];
269
270 //determine the account name of the child accounts that we're displaying
271 QString accountname = account->getAccountName ( atoi ( results [ counter + 6 ] ) );
272
273 // fill in values
274 if ( account->getParentAccountID ( id ) != -1 ) // use these constructors if we're showing a child account
275 {
276 if ( showcleared == 1 && getCleared ( transferid.toInt() ) == 1 )
277 ColorListItem *item = new ColorListItem ( listview, date, payee, amount, transferid );
278 else
279 QListViewItem *item = new QListViewItem ( listview, date, payee, amount, transferid );
280 }
281 else
282 {
283 if ( showcleared == 1 && getCleared ( transferid.toInt() ) == 1 )
284 ColorListItem *item = new ColorListItem ( listview, date, payee, amount, transferid, accountname );
285 else
286 QListViewItem *item = new QListViewItem ( listview, date, payee, amount, transferid, accountname );
287 }
288
289 // advance counter
290 counter = counter + 7;
291 }
292 }
293
294QString Transaction::getBudgetTotal ( int budgetid, int lineitemid, int year, int month, int viewtype )
295 {
296 // determine if we are viewing a years, months, or days budget
297 // we have to pick a different sum for each
298 char **results;
299 switch ( viewtype )
300 {
301 case 1: // we are viewing a year
302 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 );
303 break;
304
305 case 0: // we are viewing a month
306 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 );
307 break;
308 }
309 QString amount = results [ 1 ];
310 float total = amount.toFloat();
311 amount.setNum ( total, 'f', 2 );
312 return amount;
313 }
314
315QString Transaction::getActualTotal ( int budgetid, int year, int month, int viewtype )
316 {
317 // determine if we are viewing a years, months, or days budget
318 // we have to pick a different sum for each
319 char **results;
320 switch ( viewtype )
321 {
322 case 1: // we are viewing a year
323 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 );
324 break;
325
326 case 0: // we are viewing a month
327 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 );
328 break;
329 }
330 QString amount = results [ 1 ];
331 float total = amount.toFloat();
332 amount.setNum ( total, 'f', 2 );
333 return amount;
334 }
335
336void Transaction::clearBudgetIDs ( int budgetid, int lineitemid )
337 {
338 sqlite_exec_printf ( tdb, "update transactions set budgetid = -1 where budgetid = %i and reservedone = %i;", 0, 0, 0, budgetid, lineitemid );
339 }
340
341void Transaction::clearBudgetIDs ( int budgetid )
342 {
343 sqlite_exec_printf ( tdb, "update transactions set budgetid = -1 where budgetid = %i;", 0, 0, 0, budgetid );
344 }
345