Skip to content

Commit e9e63ea

Browse files
committed
Upgrade budgetwarrior for date
1 parent 45d55c6 commit e9e63ea

File tree

7 files changed

+73
-74
lines changed

7 files changed

+73
-74
lines changed

include/pages/html_writer.hpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <regex>
1111

12+
#include "date.hpp"
1213
#include "writer.hpp"
1314

1415
namespace budget {
@@ -25,8 +26,9 @@ struct html_writer : writer {
2526
writer& operator<<(long value);
2627

2728
writer& operator<<(const budget::money& m) override;
29+
writer& operator<<(const budget::year& y) override;
2830
writer& operator<<(const budget::month& m) override;
29-
writer& operator<<(const budget::year& m) override;
31+
writer& operator<<(const budget::day& d) override;
3032

3133
writer& operator<<(const end_of_line_t& m) override;
3234
writer& operator<<(const p_begin_t& m) override;
@@ -66,11 +68,26 @@ struct html_writer : writer {
6668
bool need_module(const std::string& module);
6769
};
6870

69-
// Very small utility function necessary to convert regex matches (from httplib) to a number
71+
// Very small utility functions necessary to convert regex matches (from httplib) to a number
7072

7173
template <typename T, typename It>
7274
T to_number(const std::sub_match<It>& sm) {
7375
return to_number<T>(sm.str());
7476
}
7577

78+
template <typename It>
79+
year year_from_string(const std::sub_match<It>& sm) {
80+
return year_from_string(sm.str());
81+
}
82+
83+
template <typename It>
84+
month month_from_string(const std::sub_match<It>& sm) {
85+
return month_from_string(sm.str());
86+
}
87+
88+
template <typename It>
89+
day day_from_string(const std::sub_match<It>& sm) {
90+
return day_from_string(sm.str());
91+
}
92+
7693
} // end of namespace budget

src/pages/earnings_pages.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "data_cache.hpp"
1212

13+
#include "date.hpp"
1314
#include "pages/html_writer.hpp"
1415
#include "pages/earnings_pages.hpp"
1516
#include "http.hpp"
@@ -112,15 +113,11 @@ void budget::time_graph_income_page(html_writer& w) {
112113

113114
auto sy = start_year(w.cache);
114115

115-
for (unsigned short j = sy; j <= budget::local_day().year(); ++j) {
116-
budget::year const year = j;
117-
116+
for (budget::year year = sy; year <= budget::local_day().year(); ++year) {
118117
const auto sm = start_month(w.cache, year);
119118
const auto last = last_month(year);
120119

121-
for (unsigned short i = sm; i < last; ++i) {
122-
budget::month const month = i;
123-
120+
for (budget::month month = sm; month < last; ++month) {
124121
auto sum = get_base_income(w.cache, budget::date(year, month, 2)) + fold_left_auto(all_earnings_month(w.cache, year, month) | to_amount);
125122

126123
const std::string date = std::format("Date.UTC({},{},1)", year.value, month.value - 1);
@@ -159,16 +156,14 @@ void budget::time_graph_income_page(html_writer& w) {
159156

160157
auto sy = start_year(w.cache);
161158

162-
for (unsigned short j = sy; j <= budget::local_day().year(); ++j) {
163-
budget::year const year = j;
164-
159+
for (budget::year year = sy; year <= budget::local_day().year(); ++year) {
165160
const auto sm = start_month(w.cache, year);
166161
const auto last = last_month(year);
167162

168163
budget::money sum;
169164

170-
for (unsigned short i = sm; i < last; ++i) {
171-
sum += get_base_income(w.cache, budget::date(year, i, 2)) + fold_left_auto(all_earnings_month(w.cache, year, i) | to_amount);
165+
for (budget::month m = sm; m < last; ++m) {
166+
sum += get_base_income(w.cache, budget::date(year, m, 2)) + fold_left_auto(all_earnings_month(w.cache, year, m) | to_amount);
172167
}
173168

174169
const std::string date = std::format("Date.UTC({},1,1)", year.value);
@@ -203,15 +198,11 @@ void budget::time_graph_earnings_page(html_writer& w) {
203198

204199
auto sy = start_year(w.cache);
205200

206-
for (unsigned short j = sy; j <= budget::local_day().year(); ++j) {
207-
budget::year const year = j;
208-
201+
for (budget::year year = sy; year <= budget::local_day().year(); ++year) {
209202
const auto sm = start_month(w.cache, year);
210203
const auto last = last_month(year);
211204

212-
for (unsigned short i = sm; i < last; ++i) {
213-
budget::month const month = i;
214-
205+
for (budget::month month = sm; month < last; ++month) {
215206
const auto sum = fold_left_auto(all_earnings_month(w.cache, year, month) | to_amount);
216207

217208
ss << "[Date.UTC(" << year << "," << month.value - 1 << ", 1) ," << budget::money_to_string(sum) << "],";
@@ -314,7 +305,7 @@ void budget::edit_earnings_page(html_writer& w, const httplib::Request& req) {
314305

315306
void budget::earnings_page(html_writer& w, const httplib::Request& req) {
316307
if (req.matches.size() == 3) {
317-
show_earnings(to_number<size_t>(req.matches[2]), to_number<size_t>(req.matches[1]), w);
308+
show_earnings(month_from_string(req.matches[2]), year_from_string(req.matches[1]), w);
318309
} else {
319310
show_earnings(w);
320311
}

src/pages/expenses_pages.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <numeric>
99

10+
#include "date.hpp"
1011
#include "pages/html_writer.hpp"
1112
#include "pages/expenses_pages.hpp"
1213
#include "http.hpp"
@@ -201,7 +202,7 @@ void budget::month_breakdown_expenses_graph(
201202

202203
void budget::expenses_page(html_writer& w, const httplib::Request& req) {
203204
if (req.matches.size() == 3) {
204-
show_expenses(to_number<size_t>(req.matches[2]), to_number<size_t>(req.matches[1]), w);
205+
show_expenses(month_from_string(req.matches[2]), year_from_string(req.matches[1]), w);
205206
} else {
206207
show_expenses(w);
207208
}
@@ -242,15 +243,11 @@ void budget::time_graph_expenses_page(html_writer& w) {
242243

243244
auto sy = start_year(w.cache);
244245

245-
for (unsigned short j = sy; j <= budget::local_day().year(); ++j) {
246-
budget::year const year = j;
247-
246+
for (budget::year year = sy; year <= budget::local_day().year(); ++year) {
248247
const auto sm = start_month(w.cache, year);
249248
const auto last = last_month(year);
250249

251-
for (unsigned short i = sm; i < last; ++i) {
252-
budget::month const month = i;
253-
250+
for (budget::month month = sm; month < last; ++month) {
254251
budget::money const sum = fold_left_auto(all_expenses_month(w.cache, year, month) | to_amount);
255252

256253
const std::string date = std::format("Date.UTC({},{},1)", year.value, month.value - 1);
@@ -290,15 +287,11 @@ void budget::time_graph_expenses_page(html_writer& w) {
290287
std::vector<budget::money> serie;
291288
std::vector<std::string> dates;
292289

293-
for (unsigned short j = sy; j <= budget::local_day().year(); ++j) {
294-
budget::year const year = j;
295-
290+
for (budget::year year = sy; year <= budget::local_day().year(); ++year) {
296291
const auto sm = start_month(w.cache, year);
297292
const auto last = last_month(year);
298293

299-
for (unsigned short i = sm; i < last; ++i) {
300-
budget::month const month = i;
301-
294+
for (budget::month month = sm; month < last; ++month) {
302295
budget::money sum;
303296

304297
for (auto& expense : all_expenses_month(w.cache, year, month)) {
@@ -340,8 +333,8 @@ void budget::month_breakdown_expenses_page(html_writer& w, const httplib::Reques
340333
auto year = today.year();
341334

342335
if (req.matches.size() == 3) {
343-
year = to_number<size_t>(req.matches[1]);
344-
month = to_number<size_t>(req.matches[2]);
336+
year = year_from_string(req.matches[1]);
337+
month = month_from_string(req.matches[2]);
345338
}
346339

347340
w << title_begin << "Expenses Breakdown of " << month << " " << year << budget::year_month_selector{"expenses/breakdown/month", year, month} << title_end;
@@ -355,7 +348,7 @@ void budget::year_breakdown_expenses_page(html_writer& w, const httplib::Request
355348
auto year = today.year();
356349

357350
if (req.matches.size() == 2) {
358-
year = to_number<size_t>(req.matches[1]);
351+
year = year_from_string(req.matches[1]);
359352
}
360353

361354
w << title_begin << "Expense Categories Breakdown of " << year << budget::year_selector{"expenses/breakdown/year", year} << title_end;

src/pages/html_writer.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ budget::writer& budget::html_writer::operator<<(const budget::money& m) {
135135
return *this;
136136
}
137137

138+
budget::writer& budget::html_writer::operator<<(const budget::day& d) {
139+
os << d.value;
140+
141+
return *this;
142+
}
143+
138144
budget::writer& budget::html_writer::operator<<(const budget::month& m) {
139145
os << m.as_short_string();
140146

@@ -204,17 +210,17 @@ budget::writer& budget::html_writer::operator<<(const budget::year_month_selecto
204210

205211
if (m.current_month == 1) {
206212
previous_month = 12;
207-
previous_year = m.current_year - 1;
213+
previous_year = m.current_year - date_type(1);
208214
} else {
209-
previous_month = m.current_month - 1;
215+
previous_month = m.current_month - date_type(1);
210216
previous_year = m.current_year;
211217
}
212218

213219
if (m.current_month == 12) {
214220
next_month = 1;
215-
next_year = m.current_year + 1;
221+
next_year = m.current_year + date_type(1);
216222
} else {
217-
next_month = m.current_month + 1;
223+
next_month = m.current_month + date_type(1);
218224
next_year = m.current_year;
219225
}
220226

@@ -278,8 +284,8 @@ budget::writer& budget::html_writer::operator<<(const budget::year_selector& m)
278284

279285
os << R"======(<div class="col selector text-right">)======";
280286

281-
auto previous_year = m.current_year - 1;
282-
auto next_year = m.current_year + 1;
287+
auto previous_year = m.current_year - date_type(1);
288+
auto next_year = m.current_year + date_type(1);
283289

284290
os << R"(<a aria-label="Previous" href="/)" << m.page << "/" << previous_year << R"(/"><span class="oi oi-arrow-thick-left"></span></a>)";
285291
os << R"(<select aria-label="Year" id="year_selector">)";

src/pages/overview_pages.cpp

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ using namespace budget;
2020

2121
void budget::overview_page(html_writer& w, const httplib::Request& req) {
2222
if (req.matches.size() == 3) {
23-
display_month_overview(to_number<size_t>(req.matches[2]), to_number<size_t>(req.matches[1]), w);
23+
display_month_overview(month_from_string(req.matches[2]), year_from_string(req.matches[1]), w);
2424
} else {
2525
auto today = budget::local_day();
2626
display_month_overview(today.month(), today.year(), w);
@@ -43,7 +43,7 @@ void budget::overview_aggregate_year_page(html_writer& w, const httplib::Request
4343
std::string const separator = config_value("aggregate_separator", "/");
4444

4545
if (req.matches.size() == 2) {
46-
aggregate_year_overview(w, full, disable_groups, separator, to_number<size_t>(req.matches[1]));
46+
aggregate_year_overview(w, full, disable_groups, separator, year_from_string(req.matches[1]));
4747
} else {
4848
auto today = budget::local_day();
4949
aggregate_year_overview(w, full, disable_groups, separator, today.year());
@@ -57,7 +57,7 @@ void budget::overview_aggregate_year_fv_page(html_writer& w, const httplib::Requ
5757
std::string const separator = config_value("aggregate_separator", "/");
5858

5959
if (req.matches.size() == 2) {
60-
aggregate_year_fv_overview(w, full, disable_groups, separator, to_number<size_t>(req.matches[1]));
60+
aggregate_year_fv_overview(w, full, disable_groups, separator, year_from_string(req.matches[1]));
6161
} else {
6262
auto today = budget::local_day();
6363
aggregate_year_fv_overview(w, full, disable_groups, separator, today.year());
@@ -71,7 +71,7 @@ void budget::overview_aggregate_year_month_page(html_writer& w, const httplib::R
7171
std::string const separator = config_value("aggregate_separator", "/");
7272

7373
if (req.matches.size() == 2) {
74-
aggregate_year_month_overview(w, full, disable_groups, separator, to_number<size_t>(req.matches[1]));
74+
aggregate_year_month_overview(w, full, disable_groups, separator, year_from_string(req.matches[1]));
7575
} else {
7676
auto today = budget::local_day();
7777
aggregate_year_month_overview(w, full, disable_groups, separator, today.year());
@@ -85,7 +85,7 @@ void budget::overview_aggregate_month_page(html_writer& w, const httplib::Reques
8585
std::string const separator = config_value("aggregate_separator", "/");
8686

8787
if (req.matches.size() == 3) {
88-
aggregate_month_overview(w, full, disable_groups, separator, to_number<size_t>(req.matches[2]), to_number<size_t>(req.matches[1]));
88+
aggregate_month_overview(w, full, disable_groups, separator, month_from_string(req.matches[2]), year_from_string(req.matches[1]));
8989
} else {
9090
auto today = budget::local_day();
9191
aggregate_month_overview(w, full, disable_groups, separator, today.month(), today.year());
@@ -95,7 +95,7 @@ void budget::overview_aggregate_month_page(html_writer& w, const httplib::Reques
9595
void budget::overview_year_page(html_writer& w, const httplib::Request& req) {
9696
budget::year year = budget::local_day().year();
9797
if (req.matches.size() == 2) {
98-
year = to_number<size_t>(req.matches[1]);
98+
year = year_from_string(req.matches[1]);
9999
}
100100

101101
// Display the Summary Yearly Overview
@@ -126,14 +126,14 @@ void budget::overview_year_page(html_writer& w, const httplib::Request& req) {
126126

127127
ss << "]},";
128128

129-
if (year - 1 >= start_year(w.cache)) {
130-
ss << "{ name: '" << year - 1 << " Expenses',";
129+
if (year - date_type(1) >= start_year(w.cache)) {
130+
ss << "{ name: '" << year - date_type(1) << " Expenses',";
131131
ss << "data: [";
132132

133-
for (budget::month month = start_month(w.cache, year - 1); month < 13; ++month) {
134-
auto sum = fold_left_auto(all_expenses_month(w.cache, year - 1, month) | to_amount);
133+
for (budget::month month = start_month(w.cache, year - date_type(1)); month.is_valid(); ++month) {
134+
auto sum = fold_left_auto(all_expenses_month(w.cache, year - date_type(1), month) | to_amount);
135135

136-
const std::string date = std::format("Date.UTC({},{},1)", year.value, month.value - 1);
136+
const std::string date = std::format("Date.UTC({},{},1)", year.value, month.value - date_type(1));
137137
ss << "[" << date << "," << budget::money_to_string(sum) << "],";
138138
}
139139

@@ -168,15 +168,15 @@ void budget::overview_year_page(html_writer& w, const httplib::Request& req) {
168168

169169
ss << "]},";
170170

171-
if (year - 1 >= start_year(w.cache)) {
172-
ss << "{ name: '" << year - 1 << " Expenses',";
171+
if (year - date_type(1) >= start_year(w.cache)) {
172+
ss << "{ name: '" << year - date_type(1) << " Expenses',";
173173
ss << "data: [";
174174

175-
for (budget::month month = start_month(w.cache, year - 1); month < 13; ++month) {
175+
for (budget::month month = start_month(w.cache, year - date_type(1)); month.is_valid(); ++month) {
176176
auto sum =
177-
get_base_income(w.cache, budget::date(year - 1, month, 2)) + fold_left_auto(all_earnings_month(w.cache, year - 1, month) | to_amount);
177+
get_base_income(w.cache, budget::date(year - date_type(1), month, 2)) + fold_left_auto(all_earnings_month(w.cache, year - date_type(1), month) | to_amount);
178178

179-
const std::string date = std::format("Date.UTC({},{},1)", year.value, month.value - 1);
179+
const std::string date = std::format("Date.UTC({},{},1)", year.value, month.value - date_type(1));
180180
ss << "[" << date << "," << budget::money_to_string(sum) << "],";
181181
}
182182

@@ -209,15 +209,11 @@ void budget::time_graph_savings_rate_page(html_writer& w) {
209209

210210
auto sy = start_year(w.cache);
211211

212-
for (unsigned short j = sy; j <= budget::local_day().year(); ++j) {
213-
budget::year const year = j;
214-
212+
for (budget::year year = sy; year <= budget::local_day().year(); ++year) {
215213
const auto sm = start_month(w.cache, year);
216214
const auto last = last_month(year);
217215

218-
for (unsigned short i = sm; i < last; ++i) {
219-
budget::month const month = i;
220-
216+
for (budget::month month = sm; month < last; ++month) {
221217
auto status = budget::compute_month_status(w.cache, year, month);
222218

223219
auto savings = status.income - status.expenses;
@@ -286,15 +282,11 @@ void budget::time_graph_tax_rate_page(html_writer& w) {
286282

287283
double max = 1.0;
288284

289-
for (unsigned short j = sy; j <= budget::local_day().year(); ++j) {
290-
budget::year const year = j;
291-
285+
for (budget::year year = sy; year <= budget::local_day().year(); ++year) {
292286
const auto sm = start_month(w.cache, year);
293287
const auto last = last_month(year);
294288

295-
for (unsigned short i = sm; i < last; ++i) {
296-
budget::month const month = i;
297-
289+
for (budget::month month = sm; month < last; ++month) {
298290
auto status = budget::compute_month_status(w.cache, year, month);
299291

300292
double tax_rate = status.taxes / status.income;
@@ -428,7 +420,7 @@ void budget::side_overview_page(html_writer& w, const httplib::Request& req) {
428420
}
429421

430422
if (req.matches.size() == 3) {
431-
display_side_month_overview(to_number<size_t>(req.matches[2]), to_number<size_t>(req.matches[1]), w);
423+
display_side_month_overview(month_from_string(req.matches[2]), year_from_string(req.matches[1]), w);
432424
} else {
433425
auto today = budget::local_day();
434426
display_side_month_overview(today.month(), today.year(), w);

src/pages/server_pages.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,13 +1007,13 @@ void budget::add_date_picker(budget::writer& w, std::string_view default_value,
10071007
if (default_value.empty()) {
10081008
w << today.year() << "-";
10091009

1010-
if (today.month() < 10) {
1010+
if (today.month() < budget::month(10)) {
10111011
w << "0" << today.month().value << "-";
10121012
} else {
10131013
w << today.month().value << "-";
10141014
}
10151015

1016-
if (today.day() < 10) {
1016+
if (today.day() < budget::day(10)) {
10171017
w << "0" << today.day();
10181018
} else {
10191019
w << today.day();

0 commit comments

Comments
 (0)