-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvoice.elm
204 lines (163 loc) · 4.61 KB
/
Invoice.elm
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
module Invoice exposing (Model, Msg, init, update, view)
import Person
import Service
import Expense
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.App as App
-- model
type alias Model =
{ currency : String
, date : String
, reference : String
, client : Person.Model
, you : Person.Model
, serviceId : Maybe Int
, services : List Service.Model
, expenseId : Maybe Int
, expenses : List Expense.Model
}
init : Model
init =
{ currency = ""
, date = "Today"
, reference = "1042753869"
, client = Person.init
, you = Person.init
, serviceId = Nothing
, services = []
, expenseId = Nothing
, expenses = []
}
-- update
type Msg
= AddService
| SaveService
| EditService Int
| SelectCurrency String
| Client Person.Msg
| You Person.Msg
| Service Service.Msg
| Expense Expense.Msg
update : Msg -> Model -> Model
update msg model =
case msg of
AddService ->
addService model
SaveService ->
saveService model
EditService id ->
{ model | serviceId = Just id }
SelectCurrency currency ->
{ model | currency = currency }
Client msg ->
{ model | client = Person.update msg model.client }
You msg ->
{ model | you = Person.update msg model.you }
_ ->
model
addService : Model -> Model
addService model =
let
newId =
List.length model.services
newService =
Service.init
in
case model.serviceId of
Just id ->
if id >= newId then
{ model | serviceId = Just newId }
else
model
Nothing ->
{ model | serviceId = Just newId, services = model.services ++ [ { newService | id = newId } ] }
editService : Model -> Int -> Model
editService model serviceId =
{ model | serviceId = Nothing }
saveService : Model -> Model
saveService model =
case model.serviceId of
Just id ->
editService model id
Nothing ->
addService model
-- view
view : Model -> Html Msg
view model =
div []
[ h2 [] [ text "To:" ]
, App.map Client (Person.view model.client)
, h2 [] [ text "From:" ]
, App.map You (Person.view model.you)
, p [] [ text ("Date: " ++ model.date) ]
, p [] [ text ("Reference: " ++ model.reference) ]
, p [] [ text "Hello, here is your invoice" ]
, table []
([ tr []
[ th []
[ text "Services" ]
, th
[]
[ text "Description" ]
, th
[]
[ text "Amount" ]
, th [] []
]
]
++ List.map (service model.serviceId) model.services
)
, p [] [ text ("Total: " ++ calc .amount model.services) ]
, button [ onClick AddService ] [ text "Add Service" ]
, select [ onInput SelectCurrency ]
[ option []
[ text "AUD" ]
, option
[]
[ text "USD" ]
, option
[]
[ text "GBP" ]
]
]
calc : (Service.Model -> number) -> List Service.Model -> String
calc func model =
model
|> List.map func
|> List.sum
|> toString
service : Maybe Int -> Service.Model -> Html Msg
service editId service =
case editId of
Just id ->
if service.id == id then
tr []
[ td []
[ input [ value service.title ] []
]
, td []
[ input [ value service.description ] []
]
, td []
[ input [ value (toString service.amount) ] []
]
, td []
[ button [ onClick (SaveService) ] [ text "Save" ]
]
]
else
serviceRow service
Nothing ->
serviceRow service
serviceRow : Service.Model -> Html Msg
serviceRow service =
tr []
[ td [] [ text service.title ]
, td [] [ text service.description ]
, td [] [ text (toString service.amount) ]
, td []
[ button [ onClick (EditService service.id) ] [ text "Edit" ]
]
]