diff options
author | Joris | 2015-08-29 13:30:09 +0200 |
---|---|---|
committer | Joris | 2015-08-29 13:30:09 +0200 |
commit | 6b466f616035c2fc03359d182c074f096d6b7f17 (patch) | |
tree | 47708f2e96614d71059f98c757d6a3fe88c8b923 /src/client/Model | |
parent | aa7f70d172be9ef322f9a0d19d1d9d9489f9fa75 (diff) |
Showing exceeding payers
Diffstat (limited to 'src/client/Model')
-rw-r--r-- | src/client/Model/Payers.elm | 57 | ||||
-rw-r--r-- | src/client/Model/View/PaymentView.elm | 7 |
2 files changed, 62 insertions, 2 deletions
diff --git a/src/client/Model/Payers.elm b/src/client/Model/Payers.elm new file mode 100644 index 0000000..6550eaa --- /dev/null +++ b/src/client/Model/Payers.elm @@ -0,0 +1,57 @@ +module Model.Payers + ( Payers + , ExceedingPayer + , payersDecoder + , updatePayers + , getOrderedExceedingPayers + ) where + +import Json.Decode as Json exposing (..) +import Dict exposing (..) +import List +import Maybe + +type alias Payers = Dict String Int + +payersDecoder : Decoder Payers +payersDecoder = Json.map Dict.fromList (list payerDecoder) + +payerDecoder : Decoder (String, Int) +payerDecoder = + object2 (,) + ("userName" := string) + ("totalPayment" := int) + +updatePayers : Payers -> String -> Int -> Payers +updatePayers payers userName amountDiff = + Dict.update + userName + (\mbAmount -> + case mbAmount of + Just amount -> Just (amount + amountDiff) + Nothing -> Nothing + ) + payers + +type alias ExceedingPayer = + { userName : String + , amount : Int + } + +getOrderedExceedingPayers : Payers -> List ExceedingPayer +getOrderedExceedingPayers payers = + let orderedPayers = + Dict.toList payers + |> List.map (\(userName, amount) -> ExceedingPayer userName amount) + |> List.sortBy .amount + maybeMinAmount = + List.head orderedPayers + |> Maybe.map .amount + in case maybeMinAmount of + Just minAmount -> + orderedPayers + |> List.map (\payer -> { payer | amount <- payer.amount - minAmount }) + |> List.filter (\payer -> payer.amount /= 0) + |> List.reverse + Nothing -> + [] diff --git a/src/client/Model/View/PaymentView.elm b/src/client/Model/View/PaymentView.elm index 2f2be46..19ad355 100644 --- a/src/client/Model/View/PaymentView.elm +++ b/src/client/Model/View/PaymentView.elm @@ -4,6 +4,7 @@ module Model.View.PaymentView ) where import Model.Payment exposing (Payments) +import Model.Payers exposing (Payers) import Model.View.Payment.Add exposing (..) import Model.View.Payment.Edition exposing (..) @@ -11,13 +12,15 @@ type alias PaymentView = { userName : String , add : AddPayment , payments : Payments + , payers : Payers , edition : Maybe Edition } -initPaymentView : String -> Payments -> PaymentView -initPaymentView userName payments = +initPaymentView : String -> Payments -> Payers -> PaymentView +initPaymentView userName payments payers = { userName = userName , add = initAddPayment , payments = payments + , payers = payers , edition = Nothing } |