aboutsummaryrefslogtreecommitdiff
path: root/src/client/elm/Model/Payment.elm
blob: f61ded8c424e873d05cf58091f95208c8cba7583 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
module Model.Payment exposing
  ( perPage
  , Payments
  , Payment
  , PaymentId
  , Frequency(..)
  , paymentsDecoder
  , paymentIdDecoder
  , find
  , edit
  , delete
  , totalPayments
  , punctual
  , monthly
  , groupAndSortByMonth
  , search
  , validateFrequency
  )

import Date exposing (..)
import Date.Extra.Core exposing (monthToInt, intToMonth)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Extra as Decode
import List

import Form.Validate as Validate exposing (Validation)
import Model.Date exposing (dateDecoder)
import Model.User exposing (UserId, userIdDecoder)

import Utils.List as List
import Utils.Search as Search

perPage : Int
perPage = 7

type alias Payments = List Payment

type alias Payment =
  { id : PaymentId
  , name : String
  , cost : Int
  , date : Date
  , userId : UserId
  , frequency : Frequency
  }

type alias PaymentId = Int

type Frequency = Punctual | Monthly

paymentsDecoder : Decoder Payments
paymentsDecoder = Decode.list paymentDecoder

paymentDecoder : Decoder Payment
paymentDecoder =
  Decode.map6 Payment
    (Decode.field "id" paymentIdDecoder)
    (Decode.field "name" Decode.string)
    (Decode.field "cost" Decode.int)
    (Decode.field "date" dateDecoder)
    (Decode.field "userId" userIdDecoder)
    (Decode.field "frequency" frequencyDecoder)

paymentIdDecoder : Decoder PaymentId
paymentIdDecoder = Decode.int

frequencyDecoder : Decoder Frequency
frequencyDecoder =
  let frequencyResult input =
        case input of
          "Punctual" -> Ok Punctual
          "Monthly" -> Ok Monthly
          _ -> Err ("Could not deduce Punctual nor Monthly from " ++ input)
  in  Decode.string |> Decode.andThen (Decode.fromResult << frequencyResult)

find : PaymentId -> Payments -> Maybe Payment
find paymentId payments =
  payments
    |> List.filter (\p -> p.id == paymentId)
    |> List.head

edit : Payment -> Payments -> Payments
edit payment payments = payment :: delete payment.id payments

delete : PaymentId -> Payments -> Payments
delete paymentId = List.filter (((/=) paymentId) << .id)

totalPayments : (Payment -> Bool) -> UserId -> Payments -> Int
totalPayments paymentFilter userId payments =
  payments
    |> List.filter (\payment ->
         paymentFilter payment
         && payment.userId == userId
       )
    |> List.map .cost
    |> List.sum

punctual : Payments -> Payments
punctual = List.filter ((==) Punctual << .frequency)

monthly : Payments -> Payments
monthly = List.filter ((==) Monthly << .frequency)

groupAndSortByMonth : Payments -> List ((Month, Int), Payments)
groupAndSortByMonth payments =
  payments
    |> List.groupBy (\payment -> (Date.year payment.date, monthToInt << Date.month <| payment.date))
    |> List.sortBy Tuple.first
    |> List.map (\((year, month), payments) -> ((intToMonth month, year), payments))
    |> List.reverse

search : String -> Frequency -> Payments -> Payments
search name frequency payments =
  payments
    |> List.filter ((==) frequency << .frequency)
    |> paymentSort frequency
    |> List.filter (searchSuccess name)

paymentSort : Frequency -> Payments -> Payments
paymentSort frequency =
  case frequency of
    Punctual -> List.reverse << List.sortBy (Date.toTime << .date)
    Monthly -> List.sortBy (String.toLower << .name)

searchSuccess : String -> Payment -> Bool
searchSuccess search { name, cost } =
  let searchSuccessWord word =
        (  String.contains (Search.format word) (Search.format name)
        || String.contains word (toString cost)
        )
  in  List.all searchSuccessWord (String.words search)

validateFrequency : Validation String Frequency
validateFrequency =
  Validate.customValidation Validate.string (\str ->
    if str == toString Punctual
      then
        Ok Punctual
      else
        if str == toString Monthly
          then Ok Monthly
          else Err (Validate.customError "InvalidFrequency")
  )