aboutsummaryrefslogtreecommitdiff
path: root/src/client/Model/PaymentCategory.elm
diff options
context:
space:
mode:
authorJoris2017-04-02 17:51:12 +0200
committerJoris2017-04-02 21:07:08 +0200
commit5c110716cfda6e616a795edd12f2012b132dca9f (patch)
tree71c3d04780302edf0648bec1cd914757cdbb2883 /src/client/Model/PaymentCategory.elm
parent64ff4707fdcd81c27c6be9903c3c82bc543ef016 (diff)
downloadbudget-5c110716cfda6e616a795edd12f2012b132dca9f.tar.gz
budget-5c110716cfda6e616a795edd12f2012b132dca9f.tar.bz2
budget-5c110716cfda6e616a795edd12f2012b132dca9f.zip
Add a chart on payments by month by categories
Diffstat (limited to 'src/client/Model/PaymentCategory.elm')
-rw-r--r--src/client/Model/PaymentCategory.elm25
1 files changed, 21 insertions, 4 deletions
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 =