summaryrefslogtreecommitdiff
path: root/noncore/unsupported/qashmoney/budget.cpp
Unidiff
Diffstat (limited to 'noncore/unsupported/qashmoney/budget.cpp') (more/less context) (ignore whitespace changes)
-rwxr-xr-xnoncore/unsupported/qashmoney/budget.cpp221
1 files changed, 221 insertions, 0 deletions
diff --git a/noncore/unsupported/qashmoney/budget.cpp b/noncore/unsupported/qashmoney/budget.cpp
new file mode 100755
index 0000000..2cec329
--- a/dev/null
+++ b/noncore/unsupported/qashmoney/budget.cpp
@@ -0,0 +1,221 @@
1#include "budget.h"
2#include "transaction.h"
3#include <stdlib.h>
4
5extern Transaction *transaction;
6
7Budget::Budget ()
8 {
9 bdb = sqlite_open ( "qmbudgets.db", 0, NULL );
10 }
11
12Budget::~Budget ()
13 {
14 sqlite_close ( bdb );
15 }
16
17int Budget::addBudget ( QString name, int type, QString description, QString currency, int startday, int startmonth, int startyear, int endday, int endmonth, int endyear, int defaultview )
18 {
19 sqlite_exec_printf ( bdb, "insert into budgets values ( '%q', %i, '%q', '%q', %i, %i, %i, %i, %i, %i, %i, NULL );", 0, 0, 0, ( const char * ) name, type, ( const char * ) description, ( const char * ) currency, startday, startmonth, startyear, endday, endmonth, endyear, defaultview );
20 char **results;
21 sqlite_get_table ( bdb, "select last_insert_rowid() from budgets;", &results, NULL, NULL, NULL );
22 QString tablename = "table";
23 tablename.append ( results [ 1 ] );
24 sqlite_exec_printf ( bdb, "create table '%q' ( name, lineitemamount, type, lineitemid integer primary key );", 0, 0, 0, ( const char* ) tablename );
25 return atoi ( results [ 1 ] );
26 }
27
28void Budget::updateBudget ( QString name, QString description, QString currency, int budgetid )
29 {
30 sqlite_exec_printf ( bdb, "update budgets set name = '%q', description = '%q', currency = '%q' where budgetid = %i;", 0, 0, 0, ( const char * ) name, ( const char * ) description, ( const char * ) currency, budgetid );
31 }
32
33void Budget::deleteBudget ( int budgetid )
34 {
35 if ( getNumberOfBudgets() != 0 )
36 {
37 QString tablename = "table";
38 tablename.append ( QString::number ( budgetid ) );
39 sqlite_exec_printf ( bdb, "delete from budgets where budgetid = %i;", 0, 0, 0, budgetid );
40 sqlite_exec_printf ( bdb, "drop table '%q';", 0, 0, 0, ( const char* ) tablename );
41 }
42 }
43
44int Budget::getNumberOfBudgets ()
45 {
46 char **results;
47 sqlite_get_table ( bdb, "select count() from budgets;", &results, NULL, NULL, NULL );
48 return atoi ( results [ 1 ] );
49 }
50
51int Budget::getNumberOfLineItems ( int budgetid )
52 {
53 QString tablename = "table";
54 tablename.append ( QString::number ( budgetid ) );
55 char **results;
56 sqlite_get_table_printf ( bdb, "select count() from '%q';", &results, NULL, NULL, NULL, ( const char * ) tablename );
57 return atoi ( results [ 1 ] );
58 }
59
60QStringList* Budget::getBudgetNames ()
61 {
62 QStringList *names = new QStringList ();
63 char **results;
64 int rows, counter;
65 sqlite_get_table ( bdb, "select name from budgets;", &results, &rows, NULL, NULL );
66 names->append ( "None" );
67 for ( counter = 0; counter < rows; counter++ )
68 names->append ( results [ counter+1 ] );
69 return names;
70 }
71
72QString Budget::getBudgetName ( int budgetid )
73 {
74 char **results;
75 sqlite_get_table_printf ( bdb, "select name from budgets where budgetid= %i;", &results, NULL, NULL, NULL, budgetid );
76 return ( QString ) results [ 1 ];
77 }
78
79QString Budget::getBudgetDescription ( int budgetid )
80 {
81 char **results;
82 sqlite_get_table_printf ( bdb, "select description from budgets where budgetid= %i;", &results, NULL, NULL, NULL, budgetid );
83 return ( QString ) results [ 1 ];
84 }
85
86QString Budget::getCurrency ( int budgetid )
87 {
88 char **results;
89 sqlite_get_table_printf ( bdb, "select currency from budgets where budgetid= %i;", &results, NULL, NULL, NULL, budgetid );
90 return ( QString ) results [ 1 ];
91 }
92
93QStringList* Budget::getBudgetIDs ()
94 {
95 QStringList *ids = new QStringList ();
96 char **results;
97 int rows, counter;
98 sqlite_get_table ( bdb, "select budgetid from budgets;", &results, &rows, NULL, NULL );
99 for ( counter = 0; counter < rows; counter++ )
100 ids->append ( results [ counter+1 ] );
101 return ids;
102 }
103
104int Budget::addLineItem ( int budgetid, QString lineitemname, float lineitemamount, int lineitemtype )
105 {
106 QString tablename = "table";
107 tablename.append ( QString::number ( budgetid ) );
108 sqlite_exec_printf ( bdb, "insert into '%q' values ( '%q', %.2f, %i, NULL );", 0, 0, 0, ( const char* ) tablename, ( const char* ) lineitemname, lineitemamount, lineitemtype );
109 char **results;
110 sqlite_get_table_printf ( bdb, "select last_insert_rowid() from '%q';", &results, NULL, NULL, NULL, ( const char* ) tablename );
111 return atoi ( results [ 1 ] );
112 }
113
114void Budget::updateLineItem ( QString lineitemname, float lineitemamount, int lineitemtype, int budgetid, int lineitemid )
115 {
116 QString tablename = "table";
117 tablename.append ( QString::number ( budgetid ) );
118 sqlite_exec_printf ( bdb, "update '%q' set name = '%q', lineitemamount = %f, type = %i where lineitemid = %i;", 0, 0, 0, ( const char* ) tablename, ( const char * ) lineitemname, lineitemamount, lineitemtype, lineitemid );
119 }
120
121void Budget::deleteLineItem ( int budgetid, int lineitemid )
122 {
123 QString tablename = "table";
124 tablename.append ( QString::number ( budgetid ) );
125 sqlite_exec_printf ( bdb, "delete from '%q' where lineitemid = %i;", 0, 0, 0, ( const char * ) tablename, lineitemid );
126 }
127
128void Budget::displayLineItems ( int budgetid, QListView *listview, int month, int year, int viewtype )
129 {
130 QString tablename = "table";
131 tablename.append ( QString::number ( budgetid ) );
132 char **results;
133 int rows, columns, counter;
134 sqlite_get_table_printf ( bdb, "select name, lineitemamount, lineitemid from '%q';", &results, &rows, &columns, NULL, ( const char * ) tablename );
135 int total = ( ( rows + 1 ) * columns );
136 for ( counter = 3; counter < total; counter = counter + 3 )
137 {
138 float amount = 0;
139 if ( viewtype == 0 )
140 {
141 QString lineitemamount = results [ counter + 1 ];
142 amount = lineitemamount.toFloat() / 12;
143 }
144 else
145 {
146 QString lineitemamount = results [ counter + 1 ];
147 amount = lineitemamount.toFloat();
148 }
149 QListViewItem *item = new QListViewItem ( listview, results [ counter ], QString::number ( amount, 'f', 2 ), transaction->getBudgetTotal ( budgetid, atoi ( results [ counter + 2 ] ), year, month, viewtype ), results [ counter + 2 ] );
150 }
151 }
152
153QStringList Budget::getLineItems ( int budgetid )
154 {
155 QString tablename = "table";
156 tablename.append ( QString::number ( budgetid ) );
157 QStringList lineitems;
158 char **results;
159 int rows, counter;
160 sqlite_get_table_printf ( bdb, "select name from '%q';", &results, &rows, NULL, NULL, (const char*) tablename );
161 for ( counter = 0; counter < rows; counter++ )
162 lineitems.append ( results [ counter + 1 ] );
163 return lineitems;
164 }
165
166QStringList Budget::getLineItemIDs ( int budgetid )
167 {
168 QString tablename = "table";
169 tablename.append ( QString::number ( budgetid ) );
170 QStringList lineitemids;
171 char **results;
172 int rows, counter;
173 sqlite_get_table_printf ( bdb, "select lineitemid from '%q';", &results, &rows, NULL, NULL, (const char*) tablename );
174 for ( counter = 0; counter < rows; counter++ )
175 lineitemids.append ( results [ counter + 1 ] );
176 return lineitemids;
177 }
178
179int Budget::getLineItemTime ( int budgetid, int lineitemid )
180 {
181 QString tablename = "table";
182 tablename.append ( QString::number ( budgetid ) );
183 char **results;
184 sqlite_get_table_printf ( bdb, "select type from '%q' where lineitemid= %i;", &results, NULL, NULL, NULL, ( const char * ) tablename, lineitemid );
185 return atoi ( results [ 1 ] );
186 }
187
188float Budget::getLineItemAmount ( int budgetid, int lineitemid )
189 {
190 QString tablename = "table";
191 tablename.append ( QString::number ( budgetid ) );
192 char **results;
193 sqlite_get_table_printf ( bdb, "select lineitemamount from '%q' where lineitemid= %i;", &results, NULL, NULL, NULL, ( const char* ) tablename, lineitemid );
194 return strtod ( results [ 1 ], 0 );
195 }
196
197QString Budget::getBudgetTotal ( int budgetid, int viewtype )
198 {
199 QString tablename = "table";
200 tablename.append ( QString::number ( budgetid ) );
201 // determine if we are viewing a years, months, or days budget
202 // we have to pick a different sum for each
203 char **results;
204 sqlite_get_table_printf ( bdb, "select sum ( lineitemamount ) from '%q';", &results, NULL, NULL, NULL, ( const char * ) tablename );
205 QString amount = results [ 1 ];
206 float total = amount.toFloat();
207 if ( viewtype == 0 )
208 total = total / 12;
209 amount.setNum ( total, 'f', 2 );
210 return amount;
211 }
212
213int Budget::getLastAdded ()
214 {
215 char **results;
216 sqlite_get_table ( bdb, "select last_insert_rowid() from budgets;", &results, NULL, NULL, NULL );
217 return atoi ( results [ 1 ] );
218 }
219
220
221