From 5c110716cfda6e616a795edd12f2012b132dca9f Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 2 Apr 2017 17:51:12 +0200 Subject: Add a chart on payments by month by categories --- src/client/Model/Income.elm | 11 +++++------ src/client/Model/Payer.elm | 1 - src/client/Model/Payment.elm | 7 +++---- src/client/Model/PaymentCategory.elm | 25 +++++++++++++++++++++---- 4 files changed, 29 insertions(+), 15 deletions(-) (limited to 'src/client/Model') diff --git a/src/client/Model/Income.elm b/src/client/Model/Income.elm index 34578c6..aa5f05f 100644 --- a/src/client/Model/Income.elm +++ b/src/client/Model/Income.elm @@ -9,17 +9,16 @@ module Model.Income exposing , cumulativeIncomesSince ) +import Dict exposing (Dict) import Json.Decode as Decode exposing (Decoder) -import Utils.Json as Json -import Time exposing (Time, hour) import List exposing (..) -import Dict exposing (Dict) +import Maybe.Extra as Maybe +import Time exposing (Time, hour) +import Utils.Json as Json import Model.Date exposing (timeDecoder) import Model.User exposing (UserId, userIdDecoder) -import Utils.Maybe as Maybe - type alias Incomes = Dict IncomeId Income type alias IncomeId = Int @@ -46,7 +45,7 @@ incomeDefinedForAll userIds incomes = let userIncomes = List.map (\userId -> List.filter ((==) userId << .userId) << Dict.values <| incomes) userIds firstIncomes = map (head << sortBy .time) userIncomes in if all Maybe.isJust firstIncomes - then head << reverse << List.sort << map .time << Maybe.cat <| firstIncomes + then head << reverse << List.sort << map .time << Maybe.values <| firstIncomes else Nothing userCumulativeIncomeSince : Time -> Time -> Incomes -> UserId -> Int diff --git a/src/client/Model/Payer.elm b/src/client/Model/Payer.elm index 1663273..4d9190e 100644 --- a/src/client/Model/Payer.elm +++ b/src/client/Model/Payer.elm @@ -17,7 +17,6 @@ import Model.User exposing (Users, UserId, userIdDecoder) import Model.Income exposing (..) import Utils.Dict exposing (mapValues) -import Utils.Maybe exposing (isJust) type alias Payers = Dict UserId Payer diff --git a/src/client/Model/Payment.elm b/src/client/Model/Payment.elm index 2412ab9..204f9f5 100644 --- a/src/client/Model/Payment.elm +++ b/src/client/Model/Payment.elm @@ -20,10 +20,11 @@ import Date.Extra.Core exposing (monthToInt, intToMonth) import Json.Decode as Decode exposing (Decoder) import Json.Decode.Extra as Decode import List +import List.Extra as List import Form.Validate as Validate exposing (Validation) -import Model.Frequency as Frequency exposing (Frequency(..)) import Model.Date exposing (dateDecoder) +import Model.Frequency as Frequency exposing (Frequency(..)) import Model.User exposing (UserId, userIdDecoder) import Utils.List as List import Utils.Search as Search @@ -63,8 +64,7 @@ paymentIdDecoder = Decode.int find : PaymentId -> Payments -> Maybe Payment find paymentId payments = payments - |> List.filter (\p -> p.id == paymentId) - |> List.head + |> List.find (\p -> p.id == paymentId) edit : Payment -> Payments -> Payments edit payment payments = payment :: delete payment.id payments @@ -94,7 +94,6 @@ groupAndSortByMonth payments = |> List.groupBy (\payment -> (Date.year payment.date, monthToInt << Date.month <| payment.date)) |> List.sortBy Tuple.first |> List.map (\((year, month), payments) -> ((intToMonth month, year), payments)) - |> List.reverse search : String -> Frequency -> Payments -> Payments search name frequency payments = diff --git a/src/client/Model/PaymentCategory.elm b/src/client/Model/PaymentCategory.elm index bb6c152..a4fceb1 100644 --- a/src/client/Model/PaymentCategory.elm +++ b/src/client/Model/PaymentCategory.elm @@ -2,15 +2,20 @@ module Model.PaymentCategory exposing ( PaymentCategories , paymentCategoriesDecoder , search + , groupPaymentsByCategory , isCategoryUnused , save ) import Dict exposing (Dict) import Json.Decode as Decode exposing (Decoder) +import List.Extra as List +import Maybe.Extra as Maybe import Model.Category exposing (CategoryId, categoryIdDecoder) +import Model.Payment exposing (Payments) import Utils.Json as Json +import Utils.List as List import Utils.Search as Search type alias PaymentCategories = List PaymentCategory @@ -26,18 +31,30 @@ paymentCategoriesDecoder = (Decode.field "name" Decode.string) (Decode.field "category" categoryIdDecoder) +groupPaymentsByCategory : PaymentCategories -> Payments -> List (CategoryId, Payments) +groupPaymentsByCategory paymentCategories payments = + payments + |> List.groupBy (\payment -> + search payment.name paymentCategories + |> Maybe.withDefault -1 + ) + |> List.filterMap (\(category, payments) -> + case category of + -1 -> Nothing + _ -> Just (category, payments) + ) + search : String -> PaymentCategories -> Maybe CategoryId search paymentName paymentCategories = paymentCategories - |> List.filter (\pc -> Search.format pc.name == Search.format paymentName) - |> List.head + |> List.find (\pc -> Search.format pc.name == Search.format paymentName) |> Maybe.map .category isCategoryUnused : CategoryId -> PaymentCategories -> Bool isCategoryUnused category paymentCategories = paymentCategories - |> List.filter ((==) category << .category) - |> List.isEmpty + |> List.find ((==) category << .category) + |> Maybe.isNothing save : String -> CategoryId -> PaymentCategories -> PaymentCategories save name category paymentCategories = -- cgit v1.2.3