module Calculator exposing (..) import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) import NumberInput import Select type Tax = Net | Gross ctShow ct = case ct of Gross -> "Brutto" Net -> "Netto" type alias Model = { tax : Select.Model Tax , bundlePrice : NumberInput.Model Float , bundleSize : NumberInput.Model Int } init bundlePrice = Model (Select.init ctShow ctShow Net [Net, Gross]) (NumberInput.fromFloat bundlePrice) (NumberInput.fromInt 1) getResult model taxGroup = case (NumberInput.get model.bundlePrice, NumberInput.get model.bundleSize) of (Just bundlePrice, Just bundleSize) -> Just <| roundTo 2 <| if Select.get model.tax == Gross then (bundlePrice / toFloat bundleSize) / (1 + taxGroup.percentage) else bundlePrice / toFloat bundleSize _ -> Nothing type Msg = SetTax String | SetBundlePrice String | SetBundleSize String update msg model = case msg of SetTax key -> { model | tax = Select.update key model.tax } SetBundlePrice str -> { model | bundlePrice = NumberInput.update str model.bundlePrice } SetBundleSize str -> { model | bundleSize = NumberInput.update str model.bundleSize } view model taxGroup = let mainPart = [ text "(" , input [ class "formula-input", placeholder "Gebindepreis" , value <| NumberInput.show model.bundlePrice , onInput SetBundlePrice ] [] , text " ÷ " , input [ class "formula-input", placeholder "Gebindegröße" , value <| NumberInput.show model.bundleSize , onInput SetBundleSize ] [] , text ") " ] taxPart = [ text " ÷ " , input [ class "formula-input" , disabled True , value <| String.fromFloat <| 1 + taxGroup.percentage ] [] ] resultPart = [ text " = " , input [ class "formula-input" , disabled True , value <| Maybe.withDefault "?" <| Maybe.map String.fromFloat <| getResult model taxGroup ] [] ] in fieldset [] [ legend [] [ text "Preisrechner" ] , div [] <| List.concat <| List.filterMap identity [ Just mainPart , if Select.get model.tax == Gross then Just taxPart else Nothing , Just resultPart ] , div [ class "form-input" ] [ label [ for "calculator-tax" ] [ text "Gebindepreis ist" ] , Select.view [] SetTax model.tax ] ] roundTo places x = toFloat (round <| x * 10 ^ places) / 10 ^ places