Rewrite some of the grossUnitPrice stuff

This commit is contained in:
2023-08-21 14:45:45 +02:00
parent 4715a758a3
commit f15a20589b
3 changed files with 161 additions and 124 deletions
+21 -11
View File
@@ -135,11 +135,11 @@ updateState msg globals state = case state of
case find (\tg -> tg.id == searchResult.taxGroupId) globals.taxGroups of
Nothing -> (state, Cmd.none)
Just taxGroup ->
( ItemEditor
( ItemEditor <| updateGrossUnitPrice
{ barcode = searchResult.barcode
, name = searchResult.name
, netUnitPrice = NumberInput.fromFloat searchResult.netUnitPrice
, grossUnitPrice = NumberInput.fromFloat <| calculateGarfieldPrice searchResult.netUnitPrice taxGroup.percentage
, grossUnitPrice = NumberInput.fromFloat 0
, salesUnits = NumberInput.fromInt searchResult.salesUnits
, group = Select.init (.id >> String.fromInt) (.name) { id = searchResult.groupId, name = searchResult.groupName } globals.groups
, location = Select.init (.id >> String.fromInt) (.name) { id = searchResult.locationId, name = searchResult.locationName } globals.locations
@@ -175,7 +175,7 @@ updateState msg globals state = case state of
updateGrossUnitPrice model =
{ model
| grossUnitPrice = NumberInput.fromFloat <| calculateGarfieldPrice (NumberInput.get model.netUnitPrice) (Select.get model.taxGroup).percentage
| grossUnitPrice = Maybe.withDefault model.grossUnitPrice <| Maybe.map NumberInput.fromFloat <| calculateGarfieldPrice model
}
-- View stuff
@@ -238,15 +238,23 @@ view { globals, state } = case state of
[ label [ for "snack-name" ] [ text "Name" ]
, input [ value model.name, disabled True, id "snack-name" ] []
]
, div [ class "form-input" ]
[ label [ for "gross-unit-price" ]
[ text <| "Stückpreis (Brutto), Vorschlag: " ++ String.fromFloat (calculateGarfieldPrice (NumberInput.get model.netUnitPrice) (Select.get model.taxGroup).percentage)
]
, input [ onInput SetGrossUnitPrice, value <| NumberInput.show model.grossUnitPrice, id "gross-unit-price", type_ "number", step "0.01" ] []
]
, viewGrossUnitPriceInput model
]
]
viewGrossUnitPriceInput model =
let
suggestedPriceStr = case calculateGarfieldPrice model of
Nothing -> "?"
Just suggestedPrice -> String.fromFloat suggestedPrice
in
div [ class "form-input" ]
[ label [ for "gross-unit-price" ]
[ text <| "Stückpreis (Brutto), Vorschlag: " ++ suggestedPriceStr
]
, input [ onInput SetGrossUnitPrice, value <| NumberInput.show model.grossUnitPrice, id "gross-unit-price", type_ "number", step "0.01" ] []
]
searchResultHeaders =
tr []
[ th [] [ text "Barcode" ]
@@ -275,8 +283,10 @@ viewSearchResult model =
]
]
calculateGarfieldPrice netUnitPrice taxPercentage =
roundTo 2 <| netUnitPrice * (1 + taxPercentage) + 0.01
calculateGarfieldPrice model =
NumberInput.get model.netUnitPrice |> Maybe.map (\netUnitPrice ->
roundTo 2 <| netUnitPrice * (1 + (Select.get model.taxGroup).percentage) + 0.01
)
roundTo places x = toFloat (round <| x * 10 ^ places) / 10 ^ places
+11 -7
View File
@@ -1,25 +1,29 @@
module NumberInput exposing (..)
type alias Model a =
{ value : a
{ value : Maybe a
, original : String
, convert : String -> Maybe a
}
fromFloat : Float -> Model Float
fromFloat x = Model x (String.fromFloat x) String.toFloat
fromFloat x = Model (Just x) (String.fromFloat x) String.toFloat
fromInt : Int -> Model Int
fromInt x = Model x (String.fromInt x) String.toInt
fromInt x = Model (Just x) (String.fromInt x) String.toInt
get : Model a -> a
get : Model a -> Maybe a
get = .value
withDefault : a -> Model a -> a
withDefault d = Maybe.withDefault d << get
isValid : Model a -> Bool
isValid model = model.value /= Nothing
update : String -> Model a -> Model a
update str model =
case model.convert str of
Nothing -> model
Just value -> { model | value = value, original = str }
{ model | value = model.convert str, original = str }
show : Model a -> String
show = .original