Move around gross unit price input

This commit is contained in:
Paul Brinkmeier 2023-08-20 17:35:53 +02:00
parent f15a20589b
commit 89422aad2a
2 changed files with 164 additions and 195 deletions

View File

@ -69,6 +69,16 @@ type alias TaxGroup =
, percentage : Float
}
type alias NewItem =
{ barcode : String
, name : String
, salesUnits : Int
, group : Group
, location : Location
, netUnitPrice : Float
, taxGroup : TaxGroup
}
type alias Context =
{ globals : Globals
, state : State
@ -135,11 +145,12 @@ updateState msg globals state = case state of
case find (\tg -> tg.id == searchResult.taxGroupId) globals.taxGroups of
Nothing -> (state, Cmd.none)
Just taxGroup ->
( ItemEditor <| updateGrossUnitPrice
( ItemEditor
{ barcode = searchResult.barcode
, name = searchResult.name
, netUnitPrice = NumberInput.fromFloat searchResult.netUnitPrice
, grossUnitPrice = NumberInput.fromFloat 0
, grossUnitPrice = NumberInput.fromFloat
(suggestedGrossPrice searchResult.netUnitPrice taxGroup.percentage)
, 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
@ -157,26 +168,26 @@ updateState msg globals state = case state of
SetSalesUnits str ->
(ItemEditor { model | salesUnits = NumberInput.update str model.salesUnits }, Cmd.none)
SetNetUnitPrice str ->
( ItemEditor <| updateGrossUnitPrice { model | netUnitPrice = NumberInput.update str model.netUnitPrice }
( ItemEditor { model | netUnitPrice = NumberInput.update str model.netUnitPrice }
, Cmd.none
)
SetGrossUnitPrice str ->
(ItemEditor { model | grossUnitPrice = NumberInput.update str model.grossUnitPrice }, Cmd.none)
( ItemEditor { model | grossUnitPrice = NumberInput.update str model.grossUnitPrice }
, Cmd.none
)
SetGroup key ->
(ItemEditor { model | group = Select.update key model.group }, Cmd.none)
SetLocation key ->
(ItemEditor { model | location = Select.update key model.location }, Cmd.none)
SetTaxGroup key ->
( ItemEditor <| updateGrossUnitPrice { model | taxGroup = Select.update key model.taxGroup }
( ItemEditor { model | taxGroup = Select.update key model.taxGroup }
, Cmd.none
)
_ ->
(state, Cmd.none)
updateGrossUnitPrice model =
{ model
| grossUnitPrice = Maybe.withDefault model.grossUnitPrice <| Maybe.map NumberInput.fromFloat <| calculateGarfieldPrice model
}
suggestedGrossPrice netPrice percentage =
roundTo 2 <| netPrice * (1 + percentage) + 0.01
-- View stuff
@ -217,7 +228,7 @@ view { globals, state } = case state of
, Select.view SetLocation model.location
]
, div [ class "form-input" ]
[ label [ for "net-unit-price" ] [ text "Stückpreis (Netto)" ]
[ label [ for "net-unit-price" ] [ text "Einkaufspreis (Netto)" ]
, input
[ value <| NumberInput.show model.netUnitPrice
, onInput SetNetUnitPrice
@ -231,29 +242,36 @@ view { globals, state } = case state of
[ label [ for "tax-group" ] [ text "Steuergruppe" ]
, Select.view SetTaxGroup model.taxGroup
]
]
, fieldset []
[ legend [] [ text "Neuer Snackeintrag" ]
, div [ class "form-input" ]
[ label [ for "snack-name" ] [ text "Name" ]
, input [ value model.name, disabled True, id "snack-name" ] []
[ label [ for "gross-unit-price" ] [ text "Verkaufspreis (Brutto)" ]
, input
[ value <| NumberInput.show model.grossUnitPrice
, onInput SetGrossUnitPrice
, type_ "number"
, id "gross-unit-price"
, step "0.01"
]
[]
, viewSetSuggestedPriceButton model
]
, 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" ] []
]
viewSetSuggestedPriceButton model =
case NumberInput.get model.netUnitPrice of
Nothing ->
button [ disabled True ] [ text "Auf ? setzen" ]
Just netUnitPrice ->
let
grossUnitPrice = suggestedGrossPrice netUnitPrice (Select.get model.taxGroup).percentage
in
button
[ onClick <| SetGrossUnitPrice <| String.fromFloat grossUnitPrice
-- Prevent submitting the form
, type_ "button"
]
[ text <| "Auf " ++ String.fromFloat grossUnitPrice ++ " setzen"
]
searchResultHeaders =
tr []

View File

@ -11119,6 +11119,17 @@ var $author$project$Entry$searchResultDecoder = A3(
'item_barcode',
$elm$json$Json$Decode$string,
$elm$json$Json$Decode$succeed($author$project$Entry$SearchResult))))))))))));
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$suggestedGrossPrice = F2(
function (netPrice, percentage) {
return A2($author$project$Entry$roundTo, 2, (netPrice * (1 + percentage)) + 0.01);
});
var $author$project$NumberInput$update = F2(
function (str, model) {
return _Utils_update(
@ -11152,53 +11163,6 @@ var $author$project$Select$update = F2(
{selected: x});
}
});
var $author$project$NumberInput$get = function ($) {
return $.value;
};
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: A2(
$elm$core$Maybe$withDefault,
model.grossUnitPrice,
A2(
$elm$core$Maybe$map,
$author$project$NumberInput$fromFloat,
$author$project$Entry$calculateGarfieldPrice(model)))
});
};
var $author$project$Entry$updateState = F3(
function (msg, globals, state) {
if (state.$ === 'ItemSearch') {
@ -11251,53 +11215,53 @@ var $author$project$Entry$updateState = F3(
var taxGroup = _v2.a;
return _Utils_Tuple2(
$author$project$Entry$ItemEditor(
$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),
{
barcode: searchResult.barcode,
grossUnitPrice: $author$project$NumberInput$fromFloat(
A2($author$project$Entry$suggestedGrossPrice, searchResult.netUnitPrice, taxGroup.percentage)),
group: A4(
$author$project$Select$init,
A2(
$elm$core$Basics$composeR,
function ($) {
return $.name;
return $.id;
},
{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),
$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,
function ($) {
return $.name;
return $.id;
},
{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),
$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,
function ($) {
return $.description;
return $.id;
},
taxGroup,
globals.taxGroups)
})),
$elm$core$String$fromInt),
function ($) {
return $.description;
},
taxGroup,
globals.taxGroups)
}),
$elm$core$Platform$Cmd$none);
}
default:
@ -11338,12 +11302,11 @@ var $author$project$Entry$updateState = F3(
var str = msg.a;
return _Utils_Tuple2(
$author$project$Entry$ItemEditor(
$author$project$Entry$updateGrossUnitPrice(
_Utils_update(
model,
{
netUnitPrice: A2($author$project$NumberInput$update, str, model.netUnitPrice)
}))),
_Utils_update(
model,
{
netUnitPrice: A2($author$project$NumberInput$update, str, model.netUnitPrice)
})),
$elm$core$Platform$Cmd$none);
case 'SetGrossUnitPrice':
var str = msg.a;
@ -11379,12 +11342,11 @@ var $author$project$Entry$updateState = F3(
var key = msg.a;
return _Utils_Tuple2(
$author$project$Entry$ItemEditor(
$author$project$Entry$updateGrossUnitPrice(
_Utils_update(
model,
{
taxGroup: A2($author$project$Select$update, key, model.taxGroup)
}))),
_Utils_update(
model,
{
taxGroup: A2($author$project$Select$update, key, model.taxGroup)
})),
$elm$core$Platform$Cmd$none);
default:
return _Utils_Tuple2(state, $elm$core$Platform$Cmd$none);
@ -11405,6 +11367,9 @@ 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};
};
@ -11566,51 +11531,6 @@ 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};
};
@ -11712,6 +11632,47 @@ var $author$project$Entry$viewSearchResult = function (model) {
]))
]));
};
var $author$project$NumberInput$get = function ($) {
return $.value;
};
var $author$project$Select$get = function ($) {
return $.selected;
};
var $author$project$Entry$viewSetSuggestedPriceButton = function (model) {
var _v0 = $author$project$NumberInput$get(model.netUnitPrice);
if (_v0.$ === 'Nothing') {
return A2(
$elm$html$Html$button,
_List_fromArray(
[
$elm$html$Html$Attributes$disabled(true)
]),
_List_fromArray(
[
$elm$html$Html$text('Auf ? setzen')
]));
} else {
var netUnitPrice = _v0.a;
var grossUnitPrice = A2(
$author$project$Entry$suggestedGrossPrice,
netUnitPrice,
$author$project$Select$get(model.taxGroup).percentage);
return A2(
$elm$html$Html$button,
_List_fromArray(
[
$elm$html$Html$Events$onClick(
$author$project$Entry$SetGrossUnitPrice(
$elm$core$String$fromFloat(grossUnitPrice))),
$elm$html$Html$Attributes$type_('button')
]),
_List_fromArray(
[
$elm$html$Html$text(
'Auf ' + ($elm$core$String$fromFloat(grossUnitPrice) + ' setzen'))
]));
}
};
var $author$project$Entry$view = function (_v0) {
var globals = _v0.globals;
var state = _v0.state;
@ -11937,7 +11898,7 @@ var $author$project$Entry$view = function (_v0) {
]),
_List_fromArray(
[
$elm$html$Html$text('Stückpreis (Netto)')
$elm$html$Html$text('Einkaufspreis (Netto)')
])),
A2(
$elm$html$Html$input,
@ -11971,19 +11932,6 @@ var $author$project$Entry$view = function (_v0) {
$elm$html$Html$text('Steuergruppe')
])),
A2($author$project$Select$view, $author$project$Entry$SetTaxGroup, model.taxGroup)
]))
])),
A2(
$elm$html$Html$fieldset,
_List_Nil,
_List_fromArray(
[
A2(
$elm$html$Html$legend,
_List_Nil,
_List_fromArray(
[
$elm$html$Html$text('Neuer Snackeintrag')
])),
A2(
$elm$html$Html$div,
@ -11997,23 +11945,26 @@ var $author$project$Entry$view = function (_v0) {
$elm$html$Html$label,
_List_fromArray(
[
$elm$html$Html$Attributes$for('snack-name')
$elm$html$Html$Attributes$for('gross-unit-price')
]),
_List_fromArray(
[
$elm$html$Html$text('Name')
$elm$html$Html$text('Verkaufspreis (Brutto)')
])),
A2(
$elm$html$Html$input,
_List_fromArray(
[
$elm$html$Html$Attributes$value(model.name),
$elm$html$Html$Attributes$disabled(true),
$elm$html$Html$Attributes$id('snack-name')
$elm$html$Html$Attributes$value(
$author$project$NumberInput$show(model.grossUnitPrice)),
$elm$html$Html$Events$onInput($author$project$Entry$SetGrossUnitPrice),
$elm$html$Html$Attributes$type_('number'),
$elm$html$Html$Attributes$id('gross-unit-price'),
$elm$html$Html$Attributes$step('0.01')
]),
_List_Nil)
])),
$author$project$Entry$viewGrossUnitPriceInput(model)
_List_Nil),
$author$project$Entry$viewSetSuggestedPriceButton(model)
]))
]))
]));
}