Support month specification for task lists
[infodrom.org/service.infodrom.org] / src / InfoCon / stempel / stempel
index 31acc97..b5e0646 100755 (executable)
@@ -2,7 +2,7 @@
 
 #  Time Tracker
 #
-#  Copyright (c) 2007  Martin Schulze <joey@infodrom.org>
+#  Copyright (c) 2007,8  Martin Schulze <joey@infodrom.org>
 #
 #  This program is free software; you can redistribute it and/or modify
 #  it under the terms of the GNU General Public License as published by
@@ -171,10 +171,26 @@ sub list_open
     exit 0;
 }
 
-sub list_all
+sub list
 {
-    # my $query = q{SELECT customer,sum(time) FROM stempel GROUP BY customer ORDER BY customer};
-    my $query = q{SELECT start,customer,time,task FROM stempel WHERE time IS NOT NULL ORDER BY customer,start};
+    my $month = shift;
+    my ($d_sec,$d_min,$d_hour,$d_mday,$d_mon,$d_year,$d_wday,$d_isdst) = localtime();
+    my $query = q{SELECT start,customer,time,task FROM stempel WHERE time IS NOT NULL };
+    
+    if ($month =~ /^(\d{4})-?(\d\d?)$/) {
+       my $pivot = $1 . '-' . $2 . '-01';
+       $query .= "AND start >= '$pivot' AND start <= '$pivot'::date + interval '1 month' ";
+    } elsif ($month =~ /^(\d{4})$/) {
+       my $pivot = $1 . '-01-01';
+       $query .= "AND start >= '$pivot' AND start <= '$pivot'::date + interval '1 year' ";
+    } elsif ($month =~ /^(\d\d?)$/) {
+       my $pivot = $d_year+1900 . '-' . $1 . '-01';
+       $query .= "AND start >= '$pivot' AND start <= '$pivot'::date + interval '1 month' ";
+    } elsif ($month !~ /^all$/) {
+       my $pivot = sprintf('%04d-%02d-01', $d_year+1900, $d_mon+1);
+       $query .= "AND start >= '$pivot' AND start <= '$pivot'::date + interval '1 month' ";
+    }
+    $query .= "ORDER BY customer,start";
 
     my $sth = $dbh->prepare ($query);
 
@@ -199,11 +215,46 @@ sub toggle_task
     }
 }
 
+sub late_close_task
+{
+    my $delta = shift;
+    my ($d_sec,$d_min,$d_hour,$d_mday,$d_mon,$d_year,$d_wday,$d_isdst) = localtime();
+
+    return unless is_open;
 
-my %options = ('list' => \&list_all,
+    print $delta,$/;
+    my $query = sprintf("UPDATE stempel SET stop=start + interval '%d minutes',time=%d WHERE stop IS NULL",
+                       $delta, quarter($delta));
+    $dbh->do($query);
+}
+
+sub help
+{
+    print <<"END";
+stempel  Copyright (c) 2007,8  Martin Schulze <joey\@infodrom.org>
+    --list [month] list month [default=current|all]
+    --open         list open
+    --close time   close open task
+    --help         this text
+    -d             terminate task
+END
+    exit;
+}
+
+my $opt_close = undef;
+my $opt_list = undef;
+my %options = ('list:s' => \$opt_list,
               'open' => \&list_open,
+              'help' => \&help,
+              'close=i' => \$opt_close,
               'terminate|end|d' => \&close_task,
               );
 GetOptions %options;
 
-toggle_task;
+if ($opt_close) {
+    late_close_task $opt_close;
+} elsif (defined $opt_list) {
+    list $opt_list;
+} else {
+    toggle_task;
+}