-
Notifications
You must be signed in to change notification settings - Fork 1
/
MwstCalculator.elm
146 lines (108 loc) · 3.21 KB
/
MwstCalculator.elm
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
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (value)
import Html.Events exposing (onInput)
import String
main : Program Never Model Msg
main =
Html.beginnerProgram
{ model = initModel
, update = update
, view = view
}
-- MODEL
type alias Model =
{ brutto : Float
, netto : Float
, steuersatz : Float
, inputBrutto : String
, inputNetto : String
, inputSteuersatz : String
}
initModel : Model
initModel =
{ brutto = 0.0
, netto = 0.0
, steuersatz = 19.0
, inputBrutto = "0.0"
, inputNetto = "0.0"
, inputSteuersatz = "19"
}
-- UPDATE
type Msg
= ChangeNetto String
| ChangeBrutto String
| ChangeSteuersatz String
update : Msg -> Model -> Model
update msg model =
case msg of
ChangeNetto value ->
let
netto =
strToFloat value model.netto
brutto =
nettoToBrutto model.steuersatz netto
in
{ model | inputNetto = value, netto = netto, brutto = brutto, inputBrutto = toString brutto }
ChangeBrutto value ->
let
brutto =
strToFloat value model.brutto
netto =
bruttoToNetto model.steuersatz brutto
in
{ model | inputBrutto = value, netto = netto, brutto = brutto, inputNetto = toString netto }
ChangeSteuersatz value ->
let
steuersatz =
strToFloat value model.steuersatz
brutto =
nettoToBrutto steuersatz model.netto
in
{ model | inputSteuersatz = value, steuersatz = steuersatz, brutto = brutto, inputBrutto = toString brutto }
strToFloat : String -> Float -> Float
strToFloat str default =
case String.toFloat str of
Ok val ->
val
Err _ ->
default
roundFloat : Int -> Float -> Float
roundFloat digits num =
let
factor =
10 ^ (toFloat digits)
in
(toFloat (round (num * factor))) / factor
nettoToBrutto : Float -> Float -> Float
nettoToBrutto steuersatz netto =
netto * (1 + steuersatz / 100) |> roundFloat 2
bruttoToNetto : Float -> Float -> Float
bruttoToNetto steuersatz brutto =
brutto / (1 + steuersatz / 100) |> roundFloat 2
-- VIEW
view : Model -> Html Msg
view model =
div []
[ h1 []
[ text "Mehrwertsteuer-Rechner" ]
, formField "Netto" model.inputNetto ChangeNetto
, formField "Steuersatz" model.inputSteuersatz ChangeSteuersatz
, formField "Brutto" model.inputBrutto ChangeBrutto
]
formField : String -> String -> (String -> Msg) -> Html Msg
formField labelStr valueField changeMsg =
let
labelStyle =
Html.Attributes.style
[ ( "display", "inline-block" )
, ( "width", "7em" )
, ( "font-weight", "bold" )
, ( "margin", "5px" )
]
in
div
[]
[ label [ labelStyle ] [ text labelStr ]
, input [ onInput changeMsg, value valueField ] []
]