Refactor calculator into its own module

This commit is contained in:
2023-08-21 14:45:45 +02:00
parent d5ca5812a1
commit 8cb62dbacb
3 changed files with 195 additions and 174 deletions
+103
View File
@@ -0,0 +1,103 @@
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
+7 -101
View File
@@ -10,6 +10,7 @@ import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Calculator
import NumberInput
import Select
@@ -90,35 +91,6 @@ type alias Globals =
, taxGroups : List TaxGroup
}
type CalculatorTax = Net | Gross
ctToInt ct = case ct of
Gross -> 0
Net -> 1
ctShow ct = case ct of
Gross -> "Brutto"
Net -> "Netto"
type alias CalculatorModel =
{ tax : Select.Model CalculatorTax
, bundlePrice : NumberInput.Model Float
, bundleSize : NumberInput.Model Int
}
type CalculatorMsg
= SetTax String
| SetBundlePrice String
| SetBundleSize String
updateCalculator 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 }
type State
= ItemSearch
{ searchTerm : String
@@ -128,7 +100,7 @@ type State
{ barcode : String
, name : String
, salesUnits : NumberInput.Model Int
, calculator : CalculatorModel
, calculator : Calculator.Model
, netUnitPrice : NumberInput.Model Float
, grossUnitPrice : NumberInput.Model Float
, group : Select.Model Group
@@ -144,7 +116,7 @@ type Msg
| SetBarcode String
| SetName String
| SetSalesUnits String
| CalculatorMsg CalculatorMsg
| CalculatorMsg Calculator.Msg
| SetNetUnitPrice String
| SetGrossUnitPrice String
| SetGroup String
@@ -179,11 +151,7 @@ updateState msg globals state = case state of
( ItemEditor
{ barcode = searchResult.barcode
, name = searchResult.name
, calculator =
{ tax = Select.init ctShow ctShow Net [Net, Gross]
, bundlePrice = NumberInput.fromFloat searchResult.netUnitPrice
, bundleSize = NumberInput.fromInt 1
}
, calculator = Calculator.init searchResult.netUnitPrice
, netUnitPrice = NumberInput.fromFloat searchResult.netUnitPrice
, grossUnitPrice = NumberInput.fromFloat
(suggestedGrossPrice searchResult.netUnitPrice taxGroup.percentage)
@@ -204,7 +172,7 @@ updateState msg globals state = case state of
SetSalesUnits str ->
(ItemEditor { model | salesUnits = NumberInput.update str model.salesUnits }, Cmd.none)
CalculatorMsg msg_ ->
(ItemEditor { model | calculator = updateCalculator msg_ model.calculator }, Cmd.none)
(ItemEditor { model | calculator = Calculator.update msg_ model.calculator }, Cmd.none)
SetNetUnitPrice str ->
( ItemEditor { model | netUnitPrice = NumberInput.update str model.netUnitPrice }
, Cmd.none
@@ -269,7 +237,7 @@ view { globals, state } = case state of
[ label [ for "tax-group" ] [ text "Steuergruppe" ]
, Select.view SetTaxGroup model.taxGroup
]
, Html.map CalculatorMsg <| viewCalculator model.calculator (Select.get model.taxGroup)
, Html.map CalculatorMsg <| Calculator.view model.calculator (Select.get model.taxGroup)
, div [ class "form-input" ]
[ label [ for "net-unit-price" ] [ text "Einkaufspreis (Netto)" ]
, input
@@ -297,70 +265,8 @@ view { globals, state } = case state of
]
]
viewCalculator 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 <| calculatorResult 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
]
]
calculatorResult 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
viewSetCalculatedPriceButton model =
case calculatorResult model.calculator (Select.get model.taxGroup) of
case Calculator.getResult model.calculator (Select.get model.taxGroup) of
Nothing ->
button [ disabled True ] [ text "Auf ? setzen" ]
Just calculatedPrice ->