-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic.js
177 lines (140 loc) · 4.98 KB
/
magic.js
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
var version="0.02";
var afData = {};
var current = {today:0.0, week:0.0, month:0.0, balance_today:0.0, preview_daily: 0.0, preview_week:0.0, preview_month:0.0};
function calculate() {
// Grab total budget and take off spending other than today's.
var remaining_in_total = afData.monthly_budget
var now = new Date();
var d = now.getDate()
for (var i=0;i<d-1;i++) {
remaining_in_total -= afData.hist[i]
}
current.month = remaining_in_total
current.preview_month = remaining_in_total - afData.hist[d-1]
// Days left in the month
days_left = new Date(now.getFullYear(), now.getMonth()+1, 0).getDate()
// And how much if the budget is split by that many days?
current.today = remaining_in_total / parseFloat(days_left)
current.preview_daily = current.preview_month / parseFloat(days_left-1)
// How per week?
current.week = current.today * 7.0
current.preview_week = current.preview_daily * 7.0
// Now figure out today
var spent_today = parseFloat(afData.hist[d-1])
current.balance_today = current.today - spent_today
console.log(current)
}
function load() {
if (localStorage.getItem("afData") === null) {
console.log('load() - json not found, resetting.');
reset(false)
} else {
afData=JSON.parse(localStorage.afData)
console.log('load() - found json.')
console.log(afData)
running_check()
// Or are we possibly in a new month?
calculate()
draw();
}
}
function running_check() {
// Call it frequently, populates current day and resets on a new month
// Are we in a new month?
if (afData.this_month != new Date().getMonth()) {
reset(true)
}
// Does today exist?
var day = new Date().getDate()
if (afData.hist.length < day) afData.hist.length.push(0.00)
}
function save() {
localStorage.afData = JSON.stringify(afData);
}
function reset(keep_monthly_budget) {
console.log('resetting')
if (keep_monthly_budget) {
afData = {monthly_budget:afData.monthly_budget}
} else {
afData = {monthly_budget:100.00}
}
afData.hist = new Array()
afData.today_each = new Array()
afData.this_month = new Date().getMonth()
var n = new Date().getDate()
for (var i=0;i<n;i++) {
afData.hist.push(0.00)
}
console.log(afData)
save()
calculate()
draw()
if (!keep_monthly_budget) show_help()
}
function draw() {
document.getElementById("spent").innerHTML = "£" + current.balance_today.toFixed(2);
document.getElementById("balance").innerHTML = "£" + current.today.toFixed(2);
document.getElementById("week_budget").innerHTML = "£" + current.week.toFixed(2);
document.getElementById("month_budget").innerHTML = "£" + current.month.toFixed(2);
if (afData.today_each.length == 0) {
document.getElementById("spending_history_today").style.display = "none";
} else {
document.getElementById("spending_history_today").style.display = "block";
var html = "<ul>"
for (var i = 0; i < afData.today_each.length; i++) {
html = html + "<li><span onclick='remove_spend_today("+i+");'>£"+afData.today_each[i].toFixed(2)+"<span></li>"
}
html = html + "</ul>"
document.getElementById("spend_today_data").innerHTML = html
}
if (current.balance_today >= 0) {
document.getElementById("alert").style.display = "none";
} else {
document.getElementById("alert").style.display = "block";
document.getElementById("overspent_amount").innerHTML = "£" + Math.abs(current.balance_today).toFixed(2);
document.getElementById("overspent_preview").innerHTML = "£" + current.preview_daily.toFixed(2) + " each day, £" + current.preview_week.toFixed(2)+ " each week, and £" + current.preview_month.toFixed(2)+" for the month"
}
document.getElementById("about").innerHTML = "Afforder v"+version+", made by guntrip.co.uk"
}
function remove_spend_today(i) {
var v = afData.today_each[i]
if (confirm("Undo "+v.toFixed(2)+" spending?") == true) {
afData.today_each.splice(i, 1);
var day = new Date().getDate()
afData.hist[day-1] = afData.hist[day-1] - v;
calculate()
save()
draw()
}
}
function spend() {
var s = parseFloat(prompt("Enter spending..", ""))
if (s != null && (isFloat(s) || isInteger(s))) {
// Does today exist?
var day = new Date().getDate()
if (afData.hist.length < day) afData.hist.length.push(0.00)
// Add it
afData.hist[day-1] = afData.hist[day-1] + s;
afData.today_each.push(s)
calculate()
save()
draw()
}
}
function update_monthly_budget() {
var v = parseFloat(prompt("Update monthly budget", afData.monthly_budget));
if (v != null && (isFloat(v) || isInteger(v))) {
afData.monthly_budget = v
calculate()
save()
draw()
}
}
function show_help() {
document.getElementById("help").style.display = "block";
}
function hide_help() {
document.getElementById("help").style.display = "none";
}
function isInteger(x) { return typeof x === "number" && isFinite(x) && Math.floor(x) === x; }
function isFloat(x) {return !!(x % 1);}