5 # Copyright (c) 2007,8,13 Martin Schulze <joey@infodrom.org>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
28 my $table = "stempel";
29 my $engine = "dbi:Pg:dbname=infocon";
31 my $dbh = DBI->connect($engine);
32 die "Access to database denied!\n" unless $dbh;
33 $dbh->do("SET DateStyle = 'ISO'");
39 return sprintf('%02d:%02d', $minutes/60, $minutes%60);
44 my $query = q{SELECT count(*) AS count FROM stempel WHERE stop IS NULL};
45 my $sth = $dbh->prepare ($query);
46 if ($sth && $sth->execute > 0) {
47 my $row = $sth->fetchrow_hashref;
48 return $row->{count} > 0 ? 1 : 0;
57 my $query = q{SELECT DISTINCT customer FROM stempel WHERE start > now() - interval '1 year' ORDER BY customer};
58 my $sth = $dbh->prepare ($query);
59 if ($sth && $sth->execute > 0) {
60 while ((my $row = $sth->fetchrow_hashref)) {
61 push @res, $row->{customer};
69 my $term = new Term::ReadLine '';
70 my @customers = customerlist;
72 $term->addhistory($_) foreach (@customers);
74 my $attribs = $term->Attribs;
75 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
76 $attribs->{completion_word} = \@customers;
78 printf "[%s]\n", join (', ', @customers);
80 my $customer = $term->readline ('Kunde: ');
82 if (defined $customer && length($customer) > 0) {
83 $customer =~ s/\s*$//;
84 $attribs->{completion_word} = undef;
85 my $task = $term->readline ('Aufgabe: ');
86 if (length($task) > 0) {
87 my $query = q{INSERT INTO stempel (start,customer,task) VALUES('now()',?,?)};
88 my $sth = $dbh->prepare($query);
89 $sth->execute($customer, $task);
98 return 15 if $min < 15;
100 return $min - $min%15 if ($min%15 < 3);
102 return $min + 15-$min%15;
107 my ($sh, $sm, $eh, $em) = @_;
110 return ($eh-$sh)*60 + $em-$sm;
112 return ($eh-$sh)*60 - ($sm-$em);
118 my ($d_sec,$d_min,$d_hour,$d_mday,$d_mon,$d_year,$d_wday,$d_isdst) = localtime();
120 my $query = q{SELECT oid,start FROM stempel WHERE stop IS NULL};
121 my $sth = $dbh->prepare ($query);
122 if ($sth && $sth->execute > 0) {
123 while ((my $row = $sth->fetchrow_hashref)) {
124 my @arr = split(/ /, $row->{start});
125 my @d = split(/-/, $arr[0]);
127 if ($d[0] != $d_year+1900 || $d[1] != $d_mon+1 || $d[2] != $d_mday) {
128 printf "Task not started today, aborting.\n";
132 my @t = split(/:/, $arr[1]);
133 my $int = quarter(hdiff($t[0], $t[1], $d_hour, $d_min));
135 $query = sprintf('UPDATE stempel SET stop=now(),time=%d WHERE oid = %d',
138 printf "Wrote %s.\n", min2hour($int);
146 my $query = q{DELETE FROM stempel WHERE stop IS NULL};
155 my ($d_sec,$d_min,$d_hour,$d_mday,$d_mon,$d_year,$d_wday,$d_isdst) = localtime();
156 my $query = q{SELECT customer,start,task FROM stempel WHERE time IS NULL ORDER BY start,customer};
158 my $sth = $dbh->prepare ($query);
159 if ($sth && $sth->execute > 0) {
160 while ((my $row = $sth->fetchrow_hashref)) {
161 my @arr = split(/ /, $row->{start});
163 my @d = split(/-/, $day);
165 if ($d[0] != $d_year+1900 || $d[1] != $d_mon+1 || $d[2] != $d_mday) {
166 printf "Task not started today.\n";
170 my @t = split(/:/, $arr[1]);
171 my $time = sprintf('%02d:%02d', $t[0], $t[1]);
172 my $int = quarter(hdiff($t[0], $t[1], $d_hour, $d_min));
174 printf "%-15s %s %s (%s) %s\n", $row->{customer}, $day, $time, min2hour($int), $row->{task};
183 my ($d_sec,$d_min,$d_hour,$d_mday,$d_mon,$d_year,$d_wday,$d_isdst) = localtime();
184 my $query = q{SELECT start,customer,time,task FROM stempel WHERE time IS NOT NULL };
186 if ($month =~ /^(\d{4})-?(\d\d?)$/) {
187 my $pivot = $1 . '-' . $2 . '-01';
188 $query .= "AND start >= '$pivot' AND start <= '$pivot'::date + interval '1 month' ";
189 } elsif ($month =~ /^(\d{4})$/) {
190 my $pivot = $1 . '-01-01';
191 $query .= "AND start >= '$pivot' AND start <= '$pivot'::date + interval '1 year' ";
192 } elsif ($month =~ /^(\d\d?)$/) {
193 my $pivot = $d_year+1900 . '-' . $1 . '-01';
194 $query .= "AND start >= '$pivot' AND start <= '$pivot'::date + interval '1 month' ";
195 } elsif ($month !~ /^all$/) {
196 my $pivot = sprintf('%04d-%02d-01', $d_year+1900, $d_mon+1);
197 $query .= "AND start >= '$pivot' AND start <= '$pivot'::date + interval '1 month' ";
199 $query .= "ORDER BY customer,start";
201 my $sth = $dbh->prepare ($query);
203 if ($sth && $sth->execute > 0) {
204 while ((my $row = $sth->fetchrow_hashref)) {
205 if (defined $row->{time}) {
206 my $day = (split(/ /, $row->{start}))[0];
207 my $time = min2hour $row->{time};
208 printf "%-15s %s %s %s\n", $row->{customer}, $day, $time, $row->{task};
227 my ($d_sec,$d_min,$d_hour,$d_mday,$d_mon,$d_year,$d_wday,$d_isdst) = localtime();
229 return unless is_open;
231 my $query = sprintf("UPDATE stempel SET stop=start + interval '%d minutes',time=%d WHERE stop IS NULL",
232 $delta, quarter($delta));
240 return unless is_open;
242 my $query = sprintf("UPDATE stempel SET start = start - interval '%d minutes' WHERE stop IS NULL", $minutes);
248 return unless is_open;
252 my $term = new Term::ReadLine '';
254 my $task = $term->readline ('Aufgabe: ');
256 if (length($task) > 0) {
257 my $query = q{UPDATE stempel SET task=? WHERE stop IS NULL};
258 my $sth = $dbh->prepare($query);
259 $sth->execute($task);
267 stempel Copyright (c) 2007,8 Martin Schulze <joey\@infodrom.org>
268 --list [month] list month [default=current|all]
269 --back n move start of current task back by n minutes
271 --close time close open task
273 --end|-d terminate task
274 --delete delete open task
275 --task alter task content
280 my $opt_close = undef;
281 my $opt_list = undef;
282 my $opt_back = undef;
283 my %options = ('list:s' => \$opt_list,
284 'back=i' => \$opt_back,
285 'open' => \&list_open,
287 'close=i' => \$opt_close,
288 'delete' => \&delete_task,
289 'task|t' => \&alter_task,
290 'terminate|end|d' => \&close_task,
295 late_close_task $opt_close;
296 } elsif (defined $opt_list) {
298 } elsif (defined $opt_back) {