-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
184 lines (162 loc) · 5.24 KB
/
main.go
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"reflect"
"strconv"
"strings"
"golang.org/x/net/html"
)
type Cadence int
const (
Monthly Cadence = iota
Yearly
)
func main() {
results := GetTaxDataRange(0, 100000, Yearly)
WriteTaxResults(results)
}
type TaxResult struct {
Brutto float64 `json:"brutto"`
GeldwerterVorteil float64 `json:"geldwertervorteil"`
Solidaritätszuschlag float64 `json:"solidaritätszuschlag"`
Kirchensteuer float64 `json:"kirchensteuer"`
Lohnsteuer float64 `json:"lohnsteuer"`
Steuern float64 `json:"steuern"`
Rentenversicherung float64 `json:"rentenversicherung"`
Arbeitslosenversicherung float64 `json:"arbeitslosenversicherung"`
Krankenversicherung float64 `json:"krankenversicherung"`
Pflegeversicherung float64 `json:"pflegeversicherung"`
SozialAbgaben float64 `json:"sozialabgaben"`
Netto float64 `json:"netto"`
}
// GetTaxDataRange gets tax data from the gross range start to stop in 1000€ increments.
func GetTaxDataRange(start int, stop int, cadence Cadence) []TaxResult {
var results []TaxResult
for gross := start; gross <= stop; gross += 1000 {
results = append(results, GetTaxData(gross, cadence))
}
return results
}
// TODO Input is hardcoded
// GetTaxData gets cadence-based tax data for gross income.
func GetTaxData(gross int, cadence Cadence) TaxResult {
log.Default().Printf("Getting tax data for %d", gross)
res, err := http.PostForm("https://www.brutto-netto-rechner.info/", url.Values{
"f_bruttolohn": {fmt.Sprintf("%d", gross)},
"f_abrechnungszeitraum": {"jahr"},
"f_geld_werter_vorteil": {"0"},
"f_abrechnungsjahr": {"2024"},
"f_steuerfreibetrag": {"0"},
"f_steuerklasse": {"1"},
"f_kirche": {"nein"},
"f_bundesland": {"bayern"},
"f_alter": {"27"},
"f_kinder": {"nein"},
"f_kinderfreibetrag": {"0"},
"f_krankenversicherung": {"pflichtversichert"},
"f_private_k": {""},
"f_arbeitgeberzuschuss_pkv": {"ja"},
"f_KVZ": {"1.2"},
"f_rentenversicherung": {"pflichtversichert"},
"f_arbeitslosenversicherung": {"pflichtversichert"},
"ok": {"1"},
})
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
doc, err := html.Parse(res.Body)
if err != nil {
log.Fatalln("Error parsing HTML:", err)
}
return GetTableData(doc, cadence)
}
// GetTableData parses doc and returns a cadence-based TaxResult.
func GetTableData(doc *html.Node, cadence Cadence) TaxResult {
regular_row := "right_column"
final_row := "right_column orange big"
if cadence == Monthly {
regular_row = fmt.Sprintf("%v %v", regular_row, "grey_bg")
final_row = fmt.Sprintf("%v %v", final_row, "grey_bg")
}
var results []float64
// Traverse the HTML tree to find and print the text content of <td> elements
var extractText func(*html.Node)
extractText = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "td" {
for _, attr := range n.Attr {
if attr.Key == "class" && attr.Val == regular_row || attr.Val == final_row {
var content string
if attr.Val == regular_row {
content = n.FirstChild.Data
} else if attr.Val == final_row {
// The 'Netto' amount in the final row is enclosed in another b tag.
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Data == "b" && c.FirstChild != nil {
content = c.FirstChild.Data
}
}
}
// TODO simplify this
content2 := strings.ReplaceAll(content, "\u00a0", "")
content2 = strings.ReplaceAll(content2, " ", "")
content2 = strings.ReplaceAll(content2, "€", "")
content2 = strings.ReplaceAll(content2, ".", "")
content2 = strings.ReplaceAll(content2, ",", ".")
value, err := strconv.ParseFloat(content2, 64)
if err != nil {
log.Fatal(err)
}
results = append(results, value)
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
extractText(c)
}
}
extractText(doc)
trType := reflect.TypeOf(TaxResult{})
if len(results) != trType.NumField() {
log.Fatal("Row amount mismatch!")
}
return TaxResult{
Brutto: results[0],
GeldwerterVorteil: results[1],
Solidaritätszuschlag: results[2],
Kirchensteuer: results[3],
Lohnsteuer: results[4],
Steuern: results[5],
Rentenversicherung: results[6],
Arbeitslosenversicherung: results[7],
Krankenversicherung: results[8],
Pflegeversicherung: results[9],
SozialAbgaben: results[10],
Netto: results[11],
}
}
// WriteTaxResults writes results to steuer.jsonl.
func WriteTaxResults(results []TaxResult) {
file, err := os.Create("steuer.jsonl")
if err != nil {
log.Fatalf("Error creating file: %s", err)
}
defer file.Close()
for _, m := range results {
line, err := json.Marshal(m)
if err != nil {
log.Printf("Error encoding JSON: %s", err)
return
}
line = append(line, '\n')
if _, err := file.Write(line); err != nil {
log.Printf("Error writing to file: %s", err)
return
}
}
}