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)) ) )