summaryrefslogtreecommitdiff
path: root/noncore/unsupported/qashmoney/budget.cpp
blob: 2cec329fa3c5f2008bbc4d644aba4a0fc2df09f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "budget.h"
#include "transaction.h"
#include <stdlib.h>

extern Transaction *transaction;

Budget::Budget ()
  {
    bdb = sqlite_open ( "qmbudgets.db", 0, NULL );
  }

Budget::~Budget ()
  {
    sqlite_close ( bdb );
  }

int Budget::addBudget ( QString name, int type, QString description, QString currency, int startday, int startmonth, int startyear, int endday, int endmonth, int endyear, int defaultview )
  {
    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 );
    char **results;
    sqlite_get_table ( bdb, "select last_insert_rowid() from budgets;", &results, NULL, NULL, NULL );
    QString tablename = "table";
    tablename.append ( results [ 1 ] );
    sqlite_exec_printf ( bdb, "create table '%q' ( name, lineitemamount, type, lineitemid integer primary key );", 0, 0, 0, ( const char* ) tablename );
    return atoi ( results [ 1 ] );
  }

void Budget::updateBudget ( QString name, QString description, QString currency, int budgetid )
  {
    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 );
  }

void Budget::deleteBudget ( int budgetid )
  {
    if ( getNumberOfBudgets() != 0 )
      {
        QString tablename = "table";
        tablename.append ( QString::number ( budgetid ) );
        sqlite_exec_printf ( bdb, "delete from budgets where budgetid = %i;", 0, 0, 0, budgetid );
        sqlite_exec_printf ( bdb, "drop table '%q';", 0, 0, 0, ( const char* ) tablename );
      }
  }

int Budget::getNumberOfBudgets ()
  {
    char **results;
    sqlite_get_table ( bdb, "select count() from budgets;", &results, NULL, NULL, NULL );
    return atoi ( results [ 1 ] );
  }

int Budget::getNumberOfLineItems ( int budgetid )
  {
    QString tablename = "table";
    tablename.append ( QString::number ( budgetid ) );
    char **results;
    sqlite_get_table_printf ( bdb, "select count() from '%q';", &results, NULL, NULL, NULL, ( const char * ) tablename );
    return atoi ( results [ 1 ] );
  }

QStringList* Budget::getBudgetNames ()
  {
    QStringList *names = new QStringList ();
    char **results;
    int rows, counter;
    sqlite_get_table ( bdb, "select name from budgets;", &results, &rows, NULL, NULL );
    names->append ( "None" );
    for ( counter = 0; counter < rows; counter++ )
      names->append ( results [ counter+1 ] );
    return names;
  }

QString Budget::getBudgetName ( int budgetid )
  {
    char **results;
    sqlite_get_table_printf ( bdb, "select name from budgets where budgetid= %i;", &results, NULL, NULL, NULL, budgetid );
    return ( QString ) results [ 1 ];
  }

QString Budget::getBudgetDescription ( int budgetid )
  {
    char **results;
    sqlite_get_table_printf ( bdb, "select description from budgets where budgetid= %i;", &results, NULL, NULL, NULL, budgetid );
    return ( QString ) results [ 1 ];
  }

QString Budget::getCurrency ( int budgetid )
  {
    char **results;
    sqlite_get_table_printf ( bdb, "select currency from budgets where budgetid= %i;", &results, NULL, NULL, NULL, budgetid );
    return ( QString ) results [ 1 ];
  }

QStringList* Budget::getBudgetIDs ()
  {
    QStringList *ids = new QStringList ();
    char **results;
    int rows, counter;
    sqlite_get_table ( bdb, "select budgetid from budgets;", &results, &rows, NULL, NULL );
    for ( counter = 0; counter < rows; counter++ )
      ids->append ( results [ counter+1 ] );
    return ids;
  }

int Budget::addLineItem ( int budgetid, QString lineitemname, float lineitemamount, int lineitemtype )
  {
    QString tablename = "table";
    tablename.append ( QString::number ( budgetid ) );
    sqlite_exec_printf ( bdb, "insert into '%q' values ( '%q', %.2f, %i, NULL );", 0, 0, 0, ( const char* ) tablename, ( const char* ) lineitemname, lineitemamount, lineitemtype );
    char **results;
    sqlite_get_table_printf ( bdb, "select last_insert_rowid() from '%q';", &results, NULL, NULL, NULL, ( const char* ) tablename );
    return atoi ( results [ 1 ] );
  }

void Budget::updateLineItem ( QString lineitemname, float lineitemamount, int lineitemtype, int budgetid, int lineitemid )
  {
    QString tablename = "table";
    tablename.append ( QString::number ( budgetid ) );
    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 );
  }

void Budget::deleteLineItem ( int budgetid, int lineitemid )
  {
    QString tablename = "table";
    tablename.append ( QString::number ( budgetid ) );
   sqlite_exec_printf ( bdb, "delete from '%q' where lineitemid = %i;", 0, 0, 0, ( const char * ) tablename, lineitemid );
  }

void Budget::displayLineItems ( int budgetid, QListView *listview, int month, int year, int viewtype )
  {
    QString tablename = "table";
    tablename.append ( QString::number ( budgetid ) );
    char **results;
    int rows, columns, counter;
    sqlite_get_table_printf ( bdb, "select name, lineitemamount, lineitemid from '%q';", &results, &rows, &columns, NULL, ( const char * ) tablename );
    int total = ( ( rows + 1 ) * columns );
    for ( counter = 3; counter < total; counter = counter + 3 )
      {
        float amount = 0;
        if ( viewtype == 0 )
          {
            QString lineitemamount = results [ counter + 1 ];
            amount = lineitemamount.toFloat() / 12;
          }
        else
          {
            QString lineitemamount = results [ counter + 1 ];
            amount = lineitemamount.toFloat();
          }
        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 ] );
      }
  }

QStringList Budget::getLineItems ( int budgetid )
  {
    QString tablename = "table";
    tablename.append ( QString::number ( budgetid ) );
    QStringList lineitems;
    char **results;
    int rows, counter;
    sqlite_get_table_printf ( bdb, "select name from '%q';", &results, &rows, NULL, NULL, (const char*) tablename );
    for ( counter = 0; counter < rows; counter++ )
      lineitems.append ( results [ counter + 1 ] );
    return lineitems;
  }

QStringList Budget::getLineItemIDs ( int budgetid )
  {
    QString tablename = "table";
    tablename.append ( QString::number ( budgetid ) );
    QStringList lineitemids;
    char **results;
    int rows, counter;
    sqlite_get_table_printf ( bdb, "select lineitemid from '%q';", &results, &rows, NULL, NULL, (const char*) tablename );
    for ( counter = 0; counter < rows; counter++ )
      lineitemids.append ( results [ counter + 1 ] );
    return lineitemids;
  }

int Budget::getLineItemTime ( int budgetid, int lineitemid )
  {
    QString tablename = "table";
    tablename.append ( QString::number ( budgetid ) );
    char **results;
    sqlite_get_table_printf ( bdb, "select type from '%q' where lineitemid= %i;", &results, NULL, NULL, NULL, ( const char * ) tablename, lineitemid );
    return atoi ( results [ 1 ] );
  }

float Budget::getLineItemAmount ( int budgetid, int lineitemid )
  {
    QString tablename = "table";
    tablename.append ( QString::number ( budgetid ) );
    char **results;
    sqlite_get_table_printf ( bdb, "select lineitemamount from '%q' where lineitemid= %i;", &results, NULL, NULL, NULL, ( const char* ) tablename, lineitemid );
    return strtod ( results [ 1 ], 0 );
  }

QString Budget::getBudgetTotal ( int budgetid, int viewtype )
  {
    QString tablename = "table";
    tablename.append ( QString::number ( budgetid ) );
    // determine if we are viewing a years, months, or days budget
    // we have to pick a different sum for each
    char **results;
    sqlite_get_table_printf ( bdb, "select sum ( lineitemamount ) from '%q';", &results, NULL, NULL, NULL, ( const char * ) tablename );
    QString amount = results [ 1 ];
    float total  = amount.toFloat();
    if ( viewtype == 0 )
      total = total / 12;
    amount.setNum ( total, 'f', 2 );
    return amount;
  }

int Budget::getLastAdded ()
  {
    char **results;
    sqlite_get_table ( bdb, "select last_insert_rowid() from budgets;", &results, NULL, NULL, NULL );
    return atoi ( results [ 1 ] );
  }