module LoggedIn.Stat.View exposing ( view ) import Date exposing (Month) import Dict import Html exposing (..) import Html.Attributes exposing (..) import List.Extra as List import Time exposing (Time) import Chart.Api as Chart import LoggedData exposing (LoggedData) import LoggedIn.Stat.Model as Stat import LoggedIn.View.Format as Format import Model.Category exposing (CategoryId, Categories) import Model.Conf exposing (Conf) import Model.Payment as Payment exposing (Payments) import Model.PaymentCategory as PaymentCategory exposing (PaymentCategories) import Model.Translations exposing (Translations, getMessage, getParamMessage) import Msg exposing (Msg) import Utils.List as List import View.Date as Date import View.Plural exposing (plural) view : LoggedData -> Stat.Model -> Html Msg view loggedData { paymentsByMonthByCategory } = div [ class "stat withMargin" ] [ renderChart loggedData paymentsByMonthByCategory ] renderChart : LoggedData -> List ((Month, Int), List (CategoryId, Int)) -> Html msg renderChart { currentTime, paymentCategories, categories, conf, translations } paymentsByMonthByCategory = let monthPaymentMean = getMonthPaymentMean currentTime paymentsByMonthByCategory title = getParamMessage [ Format.price conf monthPaymentMean ] translations "ByMonthsAndMean" keys = paymentsByMonthByCategory |> List.map (\((month, year), _) -> Date.shortMonthAndYear month year translations) series = categories |> Dict.toList |> List.map (\(categoryId, category) -> { values = List.map (\(_, paymentsByCategory) -> paymentsByCategory |> List.find (\(c, _) -> c == categoryId) |> Maybe.map (toFloat << Tuple.second) |> Maybe.withDefault 0 ) paymentsByMonthByCategory , color = category.color , label = category.name } ) totalSerie = { values = List.transpose (List.map .values series) |> List.map List.sum , color = "black" , label = getMessage translations "Total" } in Chart.from keys (series ++ [totalSerie]) |> Chart.withSize { x = 2000, y = 900 } |> Chart.withTitle title |> Chart.withOrdinate 10 (Format.price conf << truncate) |> Chart.toHtml getMonthPaymentMean : Time -> List ((Month, Int), List (CategoryId, Int)) -> Int getMonthPaymentMean currentTime paymentsByMonthByCategory = paymentsByMonthByCategory |> List.filter (\((month, year), _) -> let currentDate = Date.fromTime currentTime in not (Date.month currentDate == month && Date.year currentDate == year) ) |> List.map (List.sum << List.map Tuple.second << Tuple.second) |> List.mean