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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
-- name: CreateClient :one
insert into client (name, email, billable_rate)
values (@name, @email, @billable_rate)
returning *;
-- name: FindClient :many
select c1.id, c1.name, c1.email, c1.billable_rate, c1.created_at from client c1 where c1.id = cast(@id as integer)
union all
select c2.id, c2.name, c2.email, c2.billable_rate, c2.created_at from client c2 where c2.name = @name;
-- name: CreateProject :one
insert into project (name, client_id, billable_rate)
values (@name, @client_id, @billable_rate)
returning *;
-- name: FindProject :many
select p1.id, p1.name, p1.client_id, p1.billable_rate, p1.created_at from project p1 where p1.id = cast(@id as integer)
union all
select p2.id, p2.name, p2.client_id, p2.billable_rate, p2.created_at from project p2 where p2.name = @name;
-- name: CreateTimeEntry :one
insert into time_entry (start_time, description, client_id, project_id, billable_rate)
values (
datetime('now', 'utc'),
@description,
@client_id,
@project_id,
coalesce(
@billable_rate,
(select p.billable_rate from project p where p.id = @project_id),
(select c.billable_rate from client c where c.id = @client_id)
)
)
returning *;
-- name: GetActiveTimeEntry :one
select * from time_entry
where end_time is null
order by start_time desc
limit 1;
-- name: StopTimeEntry :one
update time_entry
set end_time = datetime('now', 'utc')
where id = (
select id
from time_entry
where end_time is null
order by start_time desc
limit 1
)
returning *;
-- name: GetMostRecentTimeEntry :one
select * from time_entry
order by start_time desc
limit 1;
-- name: ListAllClients :many
select * from client
order by name;
-- name: ListAllProjects :many
select p.*, c.name as client_name from project p
join client c on p.client_id = c.id
order by c.name, p.name;
-- name: GetWeekSummaryByProject :many
select
p.id as project_id,
p.name as project_name,
c.id as client_id,
c.name as client_name,
cast(sum(
case
when te.end_time is null then
(julianday('now', 'utc') - julianday(te.start_time)) * 24 * 60 * 60
else
(julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60
end
) as integer) as total_seconds
from time_entry te
join client c on te.client_id = c.id
left join project p on te.project_id = p.id
where date(te.start_time) >= date('now', 'weekday 1', '-6 days')
and date(te.start_time) <= date('now')
group by p.id, p.name, c.id, c.name
order by c.name, p.name;
-- name: GetMonthSummaryByProject :many
select
p.id as project_id,
p.name as project_name,
c.id as client_id,
c.name as client_name,
cast(sum(
case
when te.end_time is null then
(julianday('now', 'utc') - julianday(te.start_time)) * 24 * 60 * 60
else
(julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60
end
) as integer) as total_seconds
from time_entry te
join client c on te.client_id = c.id
left join project p on te.project_id = p.id
where date(te.start_time) >= date('now', 'start of month')
and date(te.start_time) <= date('now')
group by p.id, p.name, c.id, c.name
order by c.name, p.name;
-- name: GetClientByName :one
select * from client where name = @name limit 1;
-- name: GetProjectByNameAndClient :one
select * from project where name = @name and client_id = @client_id limit 1;
-- name: CreateTimeEntryWithTimes :one
insert into time_entry (start_time, end_time, description, client_id, project_id, billable_rate)
values (
datetime(@start_time),
datetime(@end_time),
@description,
@client_id,
@project_id,
coalesce(
@billable_rate,
(select p.billable_rate from project p where p.id = @project_id),
(select c.billable_rate from client c where c.id = @client_id)
)
)
returning *;
-- name: GetInvoiceDataByClient :many
select
te.id as time_entry_id,
te.start_time,
te.end_time,
te.description,
te.billable_rate as entry_billable_rate,
c.id as client_id,
c.name as client_name,
c.billable_rate as client_billable_rate,
p.id as project_id,
p.name as project_name,
p.billable_rate as project_billable_rate,
cast(round((julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60) as integer) as duration_seconds,
case
when te.billable_rate is not null then 'entry'
when p.billable_rate is not null then 'project'
else 'client'
end as rate_source
from time_entry te
join client c on te.client_id = c.id
left join project p on te.project_id = p.id
where c.id = @client_id
and te.start_time >= @start_time
and te.start_time <= @end_time
and te.end_time is not null
order by te.start_time;
-- name: GetInvoiceDataByProject :many
select
te.id as time_entry_id,
te.start_time,
te.end_time,
te.description,
te.billable_rate as entry_billable_rate,
c.id as client_id,
c.name as client_name,
c.billable_rate as client_billable_rate,
p.id as project_id,
p.name as project_name,
p.billable_rate as project_billable_rate,
cast(
case
when te.end_time is null then
(julianday('now', 'utc') - julianday(te.start_time)) * 24 * 60 * 60
else
(julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60
end as integer
) as duration_seconds,
case
when te.billable_rate is not null then 'entry'
when p.billable_rate is not null then 'project'
else 'client'
end as rate_source
from time_entry te
join client c on te.client_id = c.id
join project p on te.project_id = p.id
where p.id = @project_id
and te.start_time >= @start_time
and te.start_time <= @end_time
and te.end_time is not null
order by te.start_time;
-- name: GetContractor :one
select * from contractor
order by id
limit 1;
-- name: CreateContractor :one
insert into contractor (name, label, email)
values (@name, @label, @email)
returning *;
-- name: UpdateContractor :one
update contractor
set name = @name, label = @label, email = @email
where id = (select id from contractor order by id limit 1)
returning *;
-- name: UpdateClient :one
update client
set name = @name, email = @email, billable_rate = @billable_rate
where id = @id
returning *;
-- name: UpdateProject :one
update project
set name = @name, billable_rate = @billable_rate
where id = @id
returning *;
-- name: GetHighestInvoiceNumber :one
select cast(coalesce(max(number), 0) as integer) as max_number
from invoice
where year = @year and month = @month;
-- name: CreateInvoice :one
insert into invoice (year, month, number, client_id, total_amount)
values (@year, @month, @number, @client_id, @total_amount)
returning *;
-- name: GetTimesheetDataByClient :many
select
te.id as time_entry_id,
te.start_time,
te.end_time,
te.description,
te.billable_rate as entry_billable_rate,
c.id as client_id,
c.name as client_name,
c.billable_rate as client_billable_rate,
p.id as project_id,
p.name as project_name,
p.billable_rate as project_billable_rate,
cast(round((julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60) as integer) as duration_seconds
from time_entry te
join client c on te.client_id = c.id
left join project p on te.project_id = p.id
where c.id = @client_id
and te.start_time >= @start_time
and te.start_time <= @end_time
and te.end_time is not null
order by te.start_time;
-- name: GetTimesheetDataByProject :many
select
te.id as time_entry_id,
te.start_time,
te.end_time,
te.description,
te.billable_rate as entry_billable_rate,
c.id as client_id,
c.name as client_name,
c.billable_rate as client_billable_rate,
p.id as project_id,
p.name as project_name,
p.billable_rate as project_billable_rate,
cast(
case
when te.end_time is null then
(julianday('now', 'utc') - julianday(te.start_time)) * 24 * 60 * 60
else
(julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60
end as integer
) as duration_seconds
from time_entry te
join client c on te.client_id = c.id
join project p on te.project_id = p.id
where p.id = @project_id
and te.start_time >= @start_time
and te.start_time <= @end_time
and te.end_time is not null
order by te.start_time;
-- name: GetTodaySummary :one
select
cast(sum(
case
when te.end_time is null then
(julianday('now', 'utc') - julianday(te.start_time)) * 24 * 60 * 60
else
(julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60
end
) as integer) as total_seconds
from time_entry te
where date(te.start_time) = date('now');
-- name: GetRecentTimeEntries :many
select * from time_entry
where start_time >= @start_time
order by start_time desc;
-- name: UpdateActiveTimerDescription :exec
update time_entry
set description = @description
where id = (
select id
from time_entry
where end_time is null
order by start_time desc
limit 1
);
-- name: RemoveTimeEntry :exec
delete from time_entry
where id = @entry_id;
|