-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectives_pages.cpp
More file actions
250 lines (193 loc) · 7.77 KB
/
objectives_pages.cpp
File metadata and controls
250 lines (193 loc) · 7.77 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//=======================================================================
// Copyright (c) 2013-2020 Baptiste Wicht.
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
// http://opensource.org/licenses/MIT)
//=======================================================================
#include "pages/objectives_pages.hpp"
#include "pages/html_writer.hpp"
#include "http.hpp"
#include "config.hpp"
using namespace budget;
namespace {
void add_objective_operator_picker(budget::writer& w, std::string_view default_value = "") {
w << R"=====(
<div class="form-group">
<label for="input_operator">Operator</label>
<select class="form-control" id="input_operator" name="input_operator">
)=====";
if ("min" == default_value) {
w << "<option selected value=\"min\">Min</option>";
} else {
w << "<option value=\"min\">Min</option>";
}
if ("max" == default_value) {
w << "<option selected value=\"max\">Max</option>";
} else {
w << "<option value=\"max\">Max</option>";
}
w << R"=====(
</select>
</div>
)=====";
}
void add_objective_type_picker(budget::writer& w, std::string_view default_value = "") {
w << R"=====(
<div class="form-group">
<label for="input_type">Type</label>
<select class="form-control" id="input_type" name="input_type">
)=====";
if ("monthly" == default_value) {
w << "<option selected value=\"monthly\">Monthly</option>";
} else {
w << "<option value=\"monthly\">Monthly</option>";
}
if ("yearly" == default_value) {
w << "<option selected value=\"yearly\">Yearly</option>";
} else {
w << "<option value=\"yearly\">Yearly</option>";
}
w << R"=====(
</select>
</div>
)=====";
}
void add_objective_source_picker(budget::writer& w, std::string_view default_value = "") {
w << R"=====(
<div class="form-group">
<label for="input_source">Source</label>
<select class="form-control" id="input_source" name="input_source">
)=====";
if ("balance" == default_value) {
w << "<option selected value=\"balance\">Balance</option>";
} else {
w << "<option value=\"balance\">Balance</option>";
}
if ("earnings" == default_value) {
w << "<option selected value=\"earnings\">Earnings</option>";
} else {
w << "<option value=\"earnings\">Earnings</option>";
}
if ("income" == default_value) {
w << "<option selected value=\"income\">Income</option>";
} else {
w << "<option value=\"income\">Income</option>";
}
if ("expenses" == default_value) {
w << "<option selected value=\"expenses\">Expenses</option>";
} else {
w << "<option value=\"expenses\">Expenses</option>";
}
if ("expenses_no_taxes" == default_value) {
w << "<option selected value=\"expenses_no_taxes\">Expenses w/o taxes</option>";
} else {
w << "<option value=\"expenses_no_taxes\">Expenses w/o taxes</option>";
}
if ("savings_rate" == default_value) {
w << "<option selected value=\"savings_rate\">Savings Rate</option>";
} else {
w << "<option value=\"savings_rate\">Savings Rate</option>";
}
w << R"=====(
</select>
</div>
)=====";
}
} // namespace
void budget::objectives_card(budget::html_writer& w) {
// if the user does not use objectives, this card does not make sense
if (w.cache.objectives().empty()) {
return;
}
const auto today = budget::local_day();
const auto m = today.month();
const auto y = today.year();
// Compute the year/month status
auto year_status = budget::compute_year_status(w.cache);
auto month_status = budget::compute_month_status(w.cache, y, m);
w << R"=====(<div class="card">)=====";
w << R"=====(<div class="card-header card-header-primary">Goals</div>)=====";
w << R"=====(<div class="row card-body">)=====";
for (size_t i = 0; i < w.cache.objectives().size(); ++i) {
auto& objective = w.cache.objectives()[i];
w << R"=====(<div class="col-lg-2 col-md-3 col-sm-4 col-xs-6">)=====";
auto ss = start_chart_base(w, "solidgauge", "objective_gauge_" + budget::to_string(i), "height: 200px");
ss << R"=====(title: {style: {color: "rgb(124, 181, 236)", fontWeight: "bold" }, text: ')=====";
ss << objective.name;
ss << R"=====('},)=====";
ss << R"=====(tooltip: { enabled: false },)=====";
ss << R"=====(yAxis: { min: 0, max: 100, lineWidth: 0, tickPositions: [], },)=====";
std::string status;
int success_int = 0;
if (objective.type == "yearly") {
status = budget::get_status(year_status, objective);
success_int = budget::compute_success(year_status, objective);
} else if (objective.type == "monthly") {
status = budget::get_status(month_status, objective);
success_int = budget::compute_success(month_status, objective);
} else {
throw budget_exception("Invalid objective type", true);
}
ss << R"=====(plotOptions: {)=====";
ss << R"=====(solidgauge: {)=====";
ss << R"=====(dataLabels: {)=====";
ss << R"=====(enabled: true, verticalAlign: "middle", borderWidth: 0, useHTML: true, )=====";
ss << R"=====(format: '<div class="gauge-objective-title"><span class="lead""><strong>)=====";
ss << success_int;
ss << R"=====(%</strong></span> <br />)=====";
ss << status;
ss << R"=====(</div>')=====";
ss << R"=====(},)=====";
ss << R"=====(rounded: true)=====";
ss << R"=====(})=====";
ss << R"=====(},)=====";
ss << R"=====(series: [{)=====";
ss << "name: '" << objective.name << "',";
ss << R"=====(data: [{)=====";
ss << R"=====(radius: '112%',)=====";
ss << R"=====(innerRadius: '88%',)=====";
ss << "y: " << std::min(success_int, 100);
ss << R"=====(}])=====";
ss << R"=====(}])=====";
end_chart(w, ss);
w << R"=====(</div>)=====";
}
w << R"=====(</div>)=====";
w << R"=====(</div>)=====";
}
void budget::list_objectives_page(html_writer& w) {
budget::list_objectives(w);
make_tables_sortable(w);
}
void budget::status_objectives_page(html_writer& w) {
budget::status_objectives(w);
}
void budget::add_objectives_page(html_writer& w) {
w << title_begin << "New objective" << title_end;
form_begin(w, "/api/objectives/add/", "/objectives/add/");
add_name_picker(w);
add_objective_type_picker(w);
add_objective_source_picker(w);
add_objective_operator_picker(w);
add_amount_picker(w);
form_end(w);
}
void budget::edit_objectives_page(html_writer& w, const httplib::Request& req) {
if (!validate_parameters(w, req, {"input_id", "back_page"})) {
return;
}
auto input_id = req.get_param_value("input_id");
if (!objective_exists(budget::to_number<size_t>(input_id))) {
return display_error_message(w, "The objective {} does not exist", input_id);
}
auto back_page = req.get_param_value("back_page");
w << title_begin << "Edit objective " << input_id << title_end;
form_begin_edit(w, "/api/objectives/edit/", back_page, input_id);
auto objective = objective_get(budget::to_number<size_t>(input_id));
add_name_picker(w, objective.name);
add_objective_type_picker(w, objective.type);
add_objective_source_picker(w, objective.source);
add_objective_operator_picker(w, objective.op);
add_amount_picker(w, budget::money_to_string(objective.amount));
form_end(w);
}