aboutsummaryrefslogtreecommitdiff
path: root/src/client/LoggedIn/Stat/Model.elm
blob: bfc66f26ede0d7782c8e982ae87252f1949627ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module LoggedIn.Stat.Model exposing
  ( Model
  , init
  , getPaymentsByMonthByCategory
  )

import Date exposing (Month)
import List.Extra as List
import Time exposing (Time)

import Model.Category exposing (CategoryId)
import Model.Conf exposing (Conf)
import Model.Payment as Payment exposing (Payments)
import Model.PaymentCategory as PaymentCategory exposing (PaymentCategories)

type alias Model =
  { paymentsByMonthByCategory : List ((Month, Int), List (CategoryId, Int))
  }

init : Time -> PaymentCategories -> Payments -> Model
init currentTime paymentCategories payments =
  { paymentsByMonthByCategory = getPaymentsByMonthByCategory currentTime paymentCategories payments
  }

getPaymentsByMonthByCategory : Time -> PaymentCategories -> Payments -> List ((Month, Int), List (CategoryId, Int))
getPaymentsByMonthByCategory currentTime paymentCategories payments =
  Payment.punctual payments
    |> Payment.groupAndSortByMonth
    |> List.map (\(m, payments) ->
         ( m
         , PaymentCategory.groupPaymentsByCategory paymentCategories payments
             |> List.map (Tuple.mapSecond (List.sum << List.map .cost))
         )
       )