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