4 # Copyright (c) 2007,8,13,14 Martin Schulze <joey@infodrom.org>
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.
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.
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.
27 my $table = "stempel";
28 my $engine = "dbi:Pg:dbname=infocon";
30 my $dbh = DBI->connect($engine);
31 die "Access to database denied!\n" unless $dbh;
32 $dbh->do("SET DateStyle = 'ISO'");
38 return sprintf('%02d:%02d', $minutes/60, $minutes%60);
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;
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};
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};
83 my ($customer, $text, $line, $start) = @_;
85 # return () unless exists $answers->{category} && length $answers->{category};
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",
91 my $sth = $dbh->prepare($sql);
94 while (my $row = $sth->fetchrow_hashref) {
95 $row->{task} = substr $row->{task}, $start if $start;
96 push @complete, $row->{task};
104 my $term = new Term::ReadLine '';
105 my $customers = customerlist;
107 my $attribs = $term->Attribs;
108 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
109 $attribs->{completion_word} = $customers;
111 printf "[%s]\n", join (', ', @$customers);
113 my $customer = $term->readline ('Kunde: ');
115 if (defined $customer && length($customer) > 0) {
116 $customer =~ s/\s*$//;
118 my $tasks = tasklist $customer;
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, @_};
125 my $task = $term->readline ('Aufgabe: ');
126 return unless length $task;
127 return if $task eq 'q';
130 printf " %s\n", $_ foreach (@$tasks);
134 my $query = q{INSERT INTO stempel (start,customer,task) VALUES('now()',?,?)};
135 my $sth = $dbh->prepare($query);
136 $sth->execute($customer, $task);
146 return 15 if $min < 15;
148 return $min - $min%15 if ($min%15 < 3);
150 return $min + 15-$min%15;
155 my ($sh, $sm, $eh, $em) = @_;
158 return ($eh-$sh)*60 + $em-$sm;
160 return ($eh-$sh)*60 - ($sm-$em);
166 my ($d_sec,$d_min,$d_hour,$d_mday,$d_mon,$d_year,$d_wday,$d_isdst) = localtime();
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]);
175 if ($d[0] != $d_year+1900 || $d[1] != $d_mon+1 || $d[2] != $d_mday) {
176 printf "Task not started today, aborting.\n";
180 my @t = split(/:/, $arr[1]);
181 my $int = quarter(hdiff($t[0], $t[1], $d_hour, $d_min));
183 $query = sprintf('UPDATE stempel SET stop=now(),time=%d WHERE oid = %d',
186 printf "Wrote %s.\n", min2hour($int);
194 my $query = q{DELETE FROM stempel WHERE stop IS NULL};
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};
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});
211 my @d = split(/-/, $day);
213 if ($d[0] != $d_year+1900 || $d[1] != $d_mon+1 || $d[2] != $d_mday) {
214 printf "Task not started today.\n";
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));
222 printf "%-15s %s %s (%s) %s\n", $row->{customer}, $day, $time, min2hour($int), $row->{task};
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 };
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' ";
247 $query .= "ORDER BY customer,start";
249 my $sth = $dbh->prepare ($query);
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};
275 my ($d_sec,$d_min,$d_hour,$d_mday,$d_mon,$d_year,$d_wday,$d_isdst) = localtime();
277 return unless is_open;
279 my $query = sprintf("UPDATE stempel SET stop=start + interval '%d minutes',time=%d WHERE stop IS NULL",
280 $delta, quarter($delta));
288 return unless is_open;
290 my $query = sprintf("UPDATE stempel SET start = start - interval '%d minutes' WHERE stop IS NULL", $minutes);
296 return unless is_open;
300 my $term = new Term::ReadLine '';
302 my $task = $term->readline ('Aufgabe: ');
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);
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
319 --close time close open task
321 --end|-d terminate task
322 --delete delete open task
323 --task alter task content
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,
335 'close=i' => \$opt_close,
336 'delete' => \&delete_task,
337 'task|t' => \&alter_task,
338 'terminate|end|d' => \&close_task,
343 late_close_task $opt_close;
344 } elsif (defined $opt_list) {
346 } elsif (defined $opt_back) {