be9b66ca33c2d007720d750de640097fd651092c
[infodrom.org/service.infodrom.org] / src / InfoCon / stempel / stempel
1 #! /usr/bin/perl
2
3 #  Time Tracker
4 #  Copyright (c) 2007,8,13,14  Martin Schulze <joey@infodrom.org>
5 #
6 #  This program is free software; you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation; either version 2 of the License, or
9 #  (at your option) any later version.
10 #
11 #  This program is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with this program; if not, write to the Free Software
18 #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19
20 use strict;
21 use warnings;
22
23 use DBI;
24 use Getopt::Long;
25 use Term::ReadLine;
26
27 my $table = "stempel";
28 my $engine = "dbi:Pg:dbname=infocon";
29
30 my $dbh = DBI->connect($engine);
31 die "Access to database denied!\n" unless $dbh;
32 $dbh->do("SET DateStyle = 'ISO'");
33
34 sub min2hour
35 {
36     my $minutes = shift;
37
38     return sprintf('%02d:%02d', $minutes/60, $minutes%60);
39 }
40
41 sub is_open
42 {
43     my $query = q{SELECT count(*) AS count FROM stempel WHERE stop IS NULL};
44     my $sth = $dbh->prepare ($query);
45     if ($sth && $sth->execute > 0) {
46         my $row = $sth->fetchrow_hashref;
47         return $row->{count} > 0 ? 1 : 0;
48     }
49     return 0;
50 }
51
52 sub customerlist
53 {
54     my @res;
55
56     my $query = q{SELECT DISTINCT customer FROM stempel WHERE start > now() - interval '1 year' ORDER BY customer};
57     my $sth = $dbh->prepare ($query);
58     if ($sth && $sth->execute > 0) {
59         while ((my $row = $sth->fetchrow_hashref)) {
60             push @res, $row->{customer};
61         }
62     }
63     return \@res;
64 }
65
66 sub tasklist
67 {
68     my $customer = shift;
69     my @res;
70
71     my $query = q{SELECT DISTINCT task FROM stempel WHERE start > now() - interval '1 month' AND customer = ? ORDER BY task};
72     my $sth = $dbh->prepare($query);
73     if ($sth && $sth->execute($customer) > 0) {
74         while ((my $row = $sth->fetchrow_hashref)) {
75             push @res, $row->{task};
76         }
77     }
78     return \@res;
79 }
80
81 sub complete_customer
82 {
83     my ($customer, $text, $line, $start) = @_;
84
85     # return () unless exists $answers->{category} && length $answers->{category};
86
87     my $sql = sprintf("SELECT DISTINCT task FROM stempel WHERE start > now() - interval '1 month' AND customer = '%s' AND task LIKE '%s%%' ORDER BY task",
88                       $customer,
89                       $line);
90
91     my $sth = $dbh->prepare($sql);
92     $sth->execute;
93     my @complete;
94     while (my $row = $sth->fetchrow_hashref) {
95         $row->{task} = substr $row->{task}, $start if $start;
96         push @complete, $row->{task};
97     }
98
99     return @complete;
100 }
101
102 sub open_task
103 {
104     my $term = new Term::ReadLine '';
105     my $customers = customerlist;
106
107     my $attribs = $term->Attribs;
108     $attribs->{completion_entry_function} = $attribs->{list_completion_function};
109     $attribs->{completion_word} = $customers;
110
111     printf "[%s]\n", join (', ', @$customers);
112
113     my $customer = $term->readline ('Kunde: ');
114
115     if (defined $customer && length($customer) > 0) {
116         $customer =~ s/\s*$//;
117
118         my $tasks = tasklist $customer;
119
120         $term->addhistory($_) foreach (@$tasks);
121         $attribs->{completion_word} = $tasks;
122         $attribs->{completion_entry_function} = undef;
123         $term->{completion_function} = sub {return complete_customer $customer, @_};
124         while (1) {
125             my $task = $term->readline ('Aufgabe: ');
126             return unless length $task;
127             return if $task eq 'q';
128
129             if ($task eq '?') {
130                 printf "  %s\n", $_ foreach (@$tasks);
131                 next;
132             }
133
134             my $query = q{INSERT INTO stempel (start,customer,task) VALUES('now()',?,?)};
135             my $sth = $dbh->prepare($query);
136             $sth->execute($customer, $task);
137             last;
138         }
139     }
140 }
141
142 sub quarter
143 {
144     my $min = shift;
145
146     return 15 if $min < 15;
147
148     return $min - $min%15 if ($min%15 < 3);
149
150     return $min + 15-$min%15;
151 }
152
153 sub hdiff
154 {
155     my ($sh, $sm, $eh, $em) = @_;
156
157     if ($em >= $sm) {
158         return ($eh-$sh)*60 + $em-$sm;
159     } else {
160         return ($eh-$sh)*60 - ($sm-$em);
161     }
162 }
163
164 sub close_task
165 {
166     my ($d_sec,$d_min,$d_hour,$d_mday,$d_mon,$d_year,$d_wday,$d_isdst) = localtime();
167
168     my $query = q{SELECT oid,start FROM stempel WHERE stop IS NULL};
169     my $sth = $dbh->prepare ($query);
170     if ($sth && $sth->execute > 0) {
171         while ((my $row = $sth->fetchrow_hashref)) {
172             my @arr = split(/ /, $row->{start});
173             my @d = split(/-/, $arr[0]);
174
175             if ($d[0] != $d_year+1900 || $d[1] != $d_mon+1 || $d[2] != $d_mday) {
176                 printf "Task not started today, aborting.\n";
177                 next;
178             }
179
180             my @t = split(/:/, $arr[1]);
181             my $int = quarter(hdiff($t[0], $t[1], $d_hour, $d_min));
182
183             $query = sprintf('UPDATE stempel SET stop=now(),time=%d WHERE oid = %d',
184                              $int, $row->{oid});
185             $dbh->do ($query);
186             printf "Wrote %s.\n", min2hour($int);
187         }
188     }
189     exit(0);
190 }
191
192 sub delete_task
193 {
194     my $query = q{DELETE FROM stempel WHERE stop IS NULL};
195     $dbh->do ($query);
196
197     exit(0);
198 }
199
200 sub list_open
201 {
202     my $exit = shift;
203     my ($d_sec,$d_min,$d_hour,$d_mday,$d_mon,$d_year,$d_wday,$d_isdst) = localtime();
204     my $query = q{SELECT customer,start,task FROM stempel WHERE time IS NULL ORDER BY start,customer};
205
206     my $sth = $dbh->prepare ($query);
207     if ($sth && $sth->execute > 0) {
208         while ((my $row = $sth->fetchrow_hashref)) {
209             my @arr = split(/ /, $row->{start});
210             my $day = $arr[0];
211             my @d = split(/-/, $day);
212
213             if ($d[0] != $d_year+1900 || $d[1] != $d_mon+1 || $d[2] != $d_mday) {
214                 printf "Task not started today.\n";
215                 next if $exit eq 1;
216             }
217
218             my @t = split(/:/, $arr[1]);
219             my $time = sprintf('%02d:%02d', $t[0], $t[1]);
220             my $int = quarter(hdiff($t[0], $t[1], $d_hour, $d_min));
221
222             printf "%-15s  %s %s (%s)  %s\n", $row->{customer}, $day, $time, min2hour($int), $row->{task};
223         }
224     }
225     exit 0 if $exit;
226 }
227
228 sub list
229 {
230     my $month = shift;
231     my ($d_sec,$d_min,$d_hour,$d_mday,$d_mon,$d_year,$d_wday,$d_isdst) = localtime();
232     my $query = q{SELECT start,customer,time,task FROM stempel WHERE time IS NOT NULL };
233     
234     if ($month =~ /^(\d{4})-?(\d\d?)$/) {
235         my $pivot = $1 . '-' . $2 . '-01';
236         $query .= "AND start >= '$pivot' AND start <= '$pivot'::date + interval '1 month' ";
237     } elsif ($month =~ /^(\d{4})$/) {
238         my $pivot = $1 . '-01-01';
239         $query .= "AND start >= '$pivot' AND start <= '$pivot'::date + interval '1 year' ";
240     } elsif ($month =~ /^(\d\d?)$/) {
241         my $pivot = $d_year+1900 . '-' . $1 . '-01';
242         $query .= "AND start >= '$pivot' AND start <= '$pivot'::date + interval '1 month' ";
243     } elsif ($month !~ /^all$/) {
244         my $pivot = sprintf('%04d-%02d-01', $d_year+1900, $d_mon+1);
245         $query .= "AND start >= '$pivot' AND start <= '$pivot'::date + interval '1 month' ";
246     }
247     $query .= "ORDER BY customer,start";
248
249     my $sth = $dbh->prepare ($query);
250
251     if ($sth && $sth->execute > 0) {
252         while ((my $row = $sth->fetchrow_hashref)) {
253             if (defined $row->{time}) {
254                 my $day = (split(/ /, $row->{start}))[0];
255                 my $time = min2hour $row->{time};
256                 printf "%-15s  %s  %s  %s\n", $row->{customer}, $day, $time, $row->{task};
257             }
258         }
259     }
260     exit 0;
261 }
262
263 sub toggle_task
264 {
265     if (is_open) {
266         list_open 1;
267     } else {
268         open_task;
269     }
270 }
271
272 sub late_close_task
273 {
274     my $delta = shift;
275     my ($d_sec,$d_min,$d_hour,$d_mday,$d_mon,$d_year,$d_wday,$d_isdst) = localtime();
276
277     return unless is_open;
278
279     my $query = sprintf("UPDATE stempel SET stop=start + interval '%d minutes',time=%d WHERE stop IS NULL",
280                         $delta, quarter($delta));
281     $dbh->do($query);
282 }
283
284 sub move_task
285 {
286     my $minutes = shift;
287
288     return unless is_open;
289
290     my $query = sprintf("UPDATE stempel SET start = start - interval '%d minutes' WHERE stop IS NULL", $minutes);
291     $dbh->do($query);
292 }
293
294 sub alter_task
295 {
296     return unless is_open;
297
298     list_open;
299
300     my $term = new Term::ReadLine '';
301
302     my $task = $term->readline ('Aufgabe: ');
303
304     if (length($task) > 0) {
305         my $query = q{UPDATE stempel SET task=? WHERE stop IS NULL};
306         my $sth = $dbh->prepare($query);
307         $sth->execute($task);
308     }
309     exit 0;
310 }
311
312 sub help
313 {
314     print <<"END";
315 stempel  Copyright (c) 2007,8  Martin Schulze <joey\@infodrom.org>
316     --list [month] list month [default=current|all]
317     --back n       move start of current task back by n minutes
318     --open         list open
319     --close time   close open task
320     --help         this text
321     --end|-d       terminate task
322     --delete       delete open task
323     --task         alter task content
324 END
325     exit;
326 }
327
328 my $opt_close = undef;
329 my $opt_list = undef;
330 my $opt_back = undef;
331 my %options = ('list:s' => \$opt_list,
332                'back=i' => \$opt_back,
333                'open' => \&list_open,
334                'help' => \&help,
335                'close=i' => \$opt_close,
336                'delete' => \&delete_task,
337                'task|t' => \&alter_task,
338                'terminate|end|d' => \&close_task,
339                );
340 GetOptions %options;
341
342 if ($opt_close) {
343     late_close_task $opt_close;
344 } elsif (defined $opt_list) {
345     list $opt_list;
346 } elsif (defined $opt_back) {
347     move_task $opt_back;
348 } else {
349     toggle_task;
350 }