module Model.Payment exposing ( perPage , Payments , Payment , PaymentId , Frequency(..) , paymentsDecoder , paymentIdDecoder , deletePayment , totalPayments , punctual , monthly , groupAndSortByMonth , sortedFiltredPunctual ) import Date exposing (..) import Date.Extra.Core exposing (monthToInt, intToMonth) import Json.Decode as Json exposing ((:=)) import String import Model.User exposing (UserId, userIdDecoder) import Model.Date exposing (dateDecoder) import Utils.List as List perPage : Int perPage = 8 type alias Payments = List Payment type alias Payment = { id : PaymentId , creation : Date , name : String , cost : Int , userId : UserId , frequency : Frequency } type alias PaymentId = Int type Frequency = Punctual | Monthly paymentsDecoder : Json.Decoder Payments paymentsDecoder = Json.list paymentDecoder paymentDecoder : Json.Decoder Payment paymentDecoder = Json.object6 Payment ("id" := paymentIdDecoder) ("creation" := dateDecoder) ("name" := Json.string) ("cost" := Json.int) ("userId" := userIdDecoder) ("frequency" := frequencyDecoder) paymentIdDecoder : Json.Decoder PaymentId paymentIdDecoder = Json.int frequencyDecoder : Json.Decoder Frequency frequencyDecoder = Json.customDecoder Json.string (\input -> case input of "Punctual" -> Ok Punctual "Monthly" -> Ok Monthly _ -> Err ("Could not deduce Punctual nor Monthly from " ++ input) ) deletePayment : PaymentId -> Payments -> Payments deletePayment paymentId = List.filter (((/=) paymentId) << .id) totalPayments : (Payment -> Bool) -> UserId -> Payments -> Int totalPayments paymentFilter userId payments = payments |> List.filter (\payment -> paymentFilter payment && payment.userId == userId && payment.frequency == Punctual ) |> List.map .cost |> List.sum punctual : Payments -> Payments punctual = List.filter ((==) Punctual << .frequency) monthly : UserId -> Payments -> Payments monthly userId = List.filter (\p -> p.frequency == Monthly && p.userId == userId) groupAndSortByMonth : Payments -> List ((Month, Int), Payments) groupAndSortByMonth payments = payments |> List.groupBy (\payment -> (Date.year payment.creation, monthToInt << Date.month <| payment.creation)) |> List.sortBy fst |> List.map (\((year, month), payments) -> ((intToMonth month, year), payments)) |> List.reverse sortedFiltredPunctual : String -> Payments -> Payments sortedFiltredPunctual search payments = punctual payments |> List.sortBy (Date.toTime << .creation) |> List.filter (searchSuccess search) |> List.reverse searchSuccess : String -> Payment -> Bool searchSuccess text { name } = (String.toLower text) `String.contains` (String.toLower name)