diff --git a/frontend/Entry.elm b/frontend/Entry.elm index 34358b2..b410caa 100644 --- a/frontend/Entry.elm +++ b/frontend/Entry.elm @@ -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 diff --git a/frontend/NumberInput.elm b/frontend/NumberInput.elm index 6ffab83..f209dec 100644 --- a/frontend/NumberInput.elm +++ b/frontend/NumberInput.elm @@ -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 diff --git a/jon/static/entry.js b/jon/static/entry.js index 696f436..16b7f6b 100644 --- a/jon/static/entry.js +++ b/jon/static/entry.js @@ -10745,17 +10745,6 @@ var $author$project$Entry$ItemEditor = function (a) { var $author$project$Entry$ReceiveSearchResults = function (a) { return {$: 'ReceiveSearchResults', a: a}; }; -var $elm$core$Basics$pow = _Basics_pow; -var $elm$core$Basics$round = _Basics_round; -var $author$project$Entry$roundTo = F2( - function (places, x) { - return $elm$core$Basics$round( - x * A2($elm$core$Basics$pow, 10, places)) / A2($elm$core$Basics$pow, 10, places); - }); -var $author$project$Entry$calculateGarfieldPrice = F2( - function (netUnitPrice, taxPercentage) { - return A2($author$project$Entry$roundTo, 2, (netUnitPrice * (1 + taxPercentage)) + 0.01); - }); var $elm$http$Http$BadStatus_ = F2( function (a, b) { return {$: 'BadStatus_', a: a, b: b}; @@ -10882,14 +10871,14 @@ var $elm$core$String$toFloat = _String_toFloat; var $author$project$NumberInput$fromFloat = function (x) { return A3( $author$project$NumberInput$Model, - x, + $elm$core$Maybe$Just(x), $elm$core$String$fromFloat(x), $elm$core$String$toFloat); }; var $author$project$NumberInput$fromInt = function (x) { return A3( $author$project$NumberInput$Model, - x, + $elm$core$Maybe$Just(x), $elm$core$String$fromInt(x), $elm$core$String$toInt); }; @@ -11132,15 +11121,12 @@ var $author$project$Entry$searchResultDecoder = A3( $elm$json$Json$Decode$succeed($author$project$Entry$SearchResult)))))))))))); var $author$project$NumberInput$update = F2( function (str, model) { - var _v0 = model.convert(str); - if (_v0.$ === 'Nothing') { - return model; - } else { - var value = _v0.a; - return _Utils_update( - model, - {original: str, value: value}); - } + return _Utils_update( + model, + { + original: str, + value: model.convert(str) + }); }); var $author$project$Select$find = F2( function (pred, xs) { @@ -11172,15 +11158,45 @@ var $author$project$NumberInput$get = function ($) { var $author$project$Select$get = function ($) { return $.selected; }; +var $elm$core$Maybe$map = F2( + function (f, maybe) { + if (maybe.$ === 'Just') { + var value = maybe.a; + return $elm$core$Maybe$Just( + f(value)); + } else { + return $elm$core$Maybe$Nothing; + } + }); +var $elm$core$Basics$pow = _Basics_pow; +var $elm$core$Basics$round = _Basics_round; +var $author$project$Entry$roundTo = F2( + function (places, x) { + return $elm$core$Basics$round( + x * A2($elm$core$Basics$pow, 10, places)) / A2($elm$core$Basics$pow, 10, places); + }); +var $author$project$Entry$calculateGarfieldPrice = function (model) { + return A2( + $elm$core$Maybe$map, + function (netUnitPrice) { + return A2( + $author$project$Entry$roundTo, + 2, + (netUnitPrice * (1 + $author$project$Select$get(model.taxGroup).percentage)) + 0.01); + }, + $author$project$NumberInput$get(model.netUnitPrice)); +}; var $author$project$Entry$updateGrossUnitPrice = function (model) { return _Utils_update( model, { - grossUnitPrice: $author$project$NumberInput$fromFloat( + grossUnitPrice: A2( + $elm$core$Maybe$withDefault, + model.grossUnitPrice, A2( - $author$project$Entry$calculateGarfieldPrice, - $author$project$NumberInput$get(model.netUnitPrice), - $author$project$Select$get(model.taxGroup).percentage)) + $elm$core$Maybe$map, + $author$project$NumberInput$fromFloat, + $author$project$Entry$calculateGarfieldPrice(model))) }); }; var $author$project$Entry$updateState = F3( @@ -11235,53 +11251,53 @@ var $author$project$Entry$updateState = F3( var taxGroup = _v2.a; return _Utils_Tuple2( $author$project$Entry$ItemEditor( - { - barcode: searchResult.barcode, - grossUnitPrice: $author$project$NumberInput$fromFloat( - A2($author$project$Entry$calculateGarfieldPrice, searchResult.netUnitPrice, taxGroup.percentage)), - group: A4( - $author$project$Select$init, - A2( - $elm$core$Basics$composeR, + $author$project$Entry$updateGrossUnitPrice( + { + barcode: searchResult.barcode, + grossUnitPrice: $author$project$NumberInput$fromFloat(0), + group: A4( + $author$project$Select$init, + A2( + $elm$core$Basics$composeR, + function ($) { + return $.id; + }, + $elm$core$String$fromInt), function ($) { - return $.id; + return $.name; }, - $elm$core$String$fromInt), - function ($) { - return $.name; - }, - {id: searchResult.groupId, name: searchResult.groupName}, - globals.groups), - location: A4( - $author$project$Select$init, - A2( - $elm$core$Basics$composeR, + {id: searchResult.groupId, name: searchResult.groupName}, + globals.groups), + location: A4( + $author$project$Select$init, + A2( + $elm$core$Basics$composeR, + function ($) { + return $.id; + }, + $elm$core$String$fromInt), function ($) { - return $.id; + return $.name; }, - $elm$core$String$fromInt), - function ($) { - return $.name; - }, - {id: searchResult.locationId, name: searchResult.locationName}, - globals.locations), - name: searchResult.name, - netUnitPrice: $author$project$NumberInput$fromFloat(searchResult.netUnitPrice), - salesUnits: $author$project$NumberInput$fromInt(searchResult.salesUnits), - taxGroup: A4( - $author$project$Select$init, - A2( - $elm$core$Basics$composeR, + {id: searchResult.locationId, name: searchResult.locationName}, + globals.locations), + name: searchResult.name, + netUnitPrice: $author$project$NumberInput$fromFloat(searchResult.netUnitPrice), + salesUnits: $author$project$NumberInput$fromInt(searchResult.salesUnits), + taxGroup: A4( + $author$project$Select$init, + A2( + $elm$core$Basics$composeR, + function ($) { + return $.id; + }, + $elm$core$String$fromInt), function ($) { - return $.id; + return $.description; }, - $elm$core$String$fromInt), - function ($) { - return $.description; - }, - taxGroup, - globals.taxGroups) - }), + taxGroup, + globals.taxGroups) + })), $elm$core$Platform$Cmd$none); } default: @@ -11389,9 +11405,6 @@ var $author$project$Entry$update = F2( var $author$project$Entry$SetBarcode = function (a) { return {$: 'SetBarcode', a: a}; }; -var $author$project$Entry$SetGrossUnitPrice = function (a) { - return {$: 'SetGrossUnitPrice', a: a}; -}; var $author$project$Entry$SetGroup = function (a) { return {$: 'SetGroup', a: a}; }; @@ -11553,6 +11566,51 @@ var $author$project$Select$view = F2( ]), A2($elm$core$List$map, viewOption, model.options)); }); +var $author$project$Entry$SetGrossUnitPrice = function (a) { + return {$: 'SetGrossUnitPrice', a: a}; +}; +var $author$project$Entry$viewGrossUnitPriceInput = function (model) { + var suggestedPriceStr = function () { + var _v0 = $author$project$Entry$calculateGarfieldPrice(model); + if (_v0.$ === 'Nothing') { + return '?'; + } else { + var suggestedPrice = _v0.a; + return $elm$core$String$fromFloat(suggestedPrice); + } + }(); + return A2( + $elm$html$Html$div, + _List_fromArray( + [ + $elm$html$Html$Attributes$class('form-input') + ]), + _List_fromArray( + [ + A2( + $elm$html$Html$label, + _List_fromArray( + [ + $elm$html$Html$Attributes$for('gross-unit-price') + ]), + _List_fromArray( + [ + $elm$html$Html$text('Stückpreis (Brutto), Vorschlag: ' + suggestedPriceStr) + ])), + A2( + $elm$html$Html$input, + _List_fromArray( + [ + $elm$html$Html$Events$onInput($author$project$Entry$SetGrossUnitPrice), + $elm$html$Html$Attributes$value( + $author$project$NumberInput$show(model.grossUnitPrice)), + $elm$html$Html$Attributes$id('gross-unit-price'), + $elm$html$Html$Attributes$type_('number'), + $elm$html$Html$Attributes$step('0.01') + ]), + _List_Nil) + ])); +}; var $author$project$Entry$GotoItemEditor = function (a) { return {$: 'GotoItemEditor', a: a}; }; @@ -11955,42 +12013,7 @@ var $author$project$Entry$view = function (_v0) { ]), _List_Nil) ])), - A2( - $elm$html$Html$div, - _List_fromArray( - [ - $elm$html$Html$Attributes$class('form-input') - ]), - _List_fromArray( - [ - A2( - $elm$html$Html$label, - _List_fromArray( - [ - $elm$html$Html$Attributes$for('gross-unit-price') - ]), - _List_fromArray( - [ - $elm$html$Html$text( - 'Stückpreis (Brutto), Vorschlag: ' + $elm$core$String$fromFloat( - A2( - $author$project$Entry$calculateGarfieldPrice, - $author$project$NumberInput$get(model.netUnitPrice), - $author$project$Select$get(model.taxGroup).percentage))) - ])), - A2( - $elm$html$Html$input, - _List_fromArray( - [ - $elm$html$Html$Events$onInput($author$project$Entry$SetGrossUnitPrice), - $elm$html$Html$Attributes$value( - $author$project$NumberInput$show(model.grossUnitPrice)), - $elm$html$Html$Attributes$id('gross-unit-price'), - $elm$html$Html$Attributes$type_('number'), - $elm$html$Html$Attributes$step('0.01') - ]), - _List_Nil) - ])) + $author$project$Entry$viewGrossUnitPriceInput(model) ])) ])); }