Add support for billing date
[infodrom.org/service.infodrom.org] / src / InfoCon / buch / infocon
index 76e0b40..b0d6961 100755 (executable)
@@ -39,6 +39,7 @@ my $term = undef;
 my $opt_all = 0;
 my $opt_verbose = 0;
 my $opt_year = 0;
+my $opt_date = undef;
 my $opt_direction = undef;
 
 sub sdate
@@ -86,6 +87,31 @@ sub date_to_string
     return sprintf("%4d%02d%02d", $year,$mon,$day);
 }
 
+sub valid_isodate
+{
+    my $date = shift;
+    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
+
+    return sprintf('%04d-%02d-%02d', $year+1900, $mon+1, $mday) unless defined $date;
+
+    if ($date =~ /^(\d+)\.(\d+)\.?$/) {
+       if ($mon == 0 && $2 > 9) {
+           $date = sprintf('%04d-%02d-%02d', $year+1900-1, $2, $1);
+       } else {
+           $date = sprintf('%04d-%02d-%02d', $year+1900, $2, $1);
+       }
+    } elsif ($date =~ /^(\d+)\.(\d+)\.(\d+)?$/) {
+       if (length $3 == 2) {
+           $date = sprintf('20%02d-%02d-%02d', $3, $2, $1);
+       } else {
+           $date = sprintf('%04d-%02d-%02d', $3, $2, $1);
+       }
+    } elsif ($date !~ /^(\d+)-(\d+)-(\d+)$/) {
+       return undef;
+    }
+    return $date;
+}
+
 sub pay_invoice
 {
     my $nr = shift;
@@ -94,9 +120,14 @@ sub pay_invoice
     my $sth;
 
     if ($pay) {
-       $query  = "UPDATE sales SET paid=1,billing_date=now() WHERE nr = $nr";
+       my $date = valid_isodate $opt_date;
+
+       die "Invalid date\n" unless defined $date;
+
+       $query  = sprintf("UPDATE sales SET paid=1,billing_date=%s WHERE nr = %d",
+                         defined $date ? $dbh->quote($date) : 'now()', $nr);
     } else {
-       $query  = "UPDATE sales SET paid=0,billing_date=NULL WHERE nr = $nr";
+       $query  = sprintf("UPDATE sales SET paid=0,billing_date=NULL WHERE nr = %d", $nr);
     }
     $sth = $dbh->do($query);
 }
@@ -434,6 +465,10 @@ sub buchung_input
            'title' => 'bezahlt',
            'type' => 'boolean',
            'default' => 0},
+       'billing_date' => {
+           'title' => 'wann',
+           'type' => 'date',
+           'validate' => \&validate_date},
        'weiter' => {
            'title' => 'Weiter',
            'type' => 'boolean',
@@ -455,16 +490,19 @@ sub buchung_input
            if ($f eq 'tax_assigned' && $answers->{'tax_percent'} == 0) {
                $answers->{$f} = 0;
                next;
-           } elsif ($f eq 'paid') {
+           }
+           $ans = read_input($f, $fields->{$f});
+           $answers->{$f} = $ans;
+
+           if ($f eq 'paid') {
                if ($answers->{paid}) {
-                   my @now = localtime(time);
-                   $answers->{billing_date} = sprintf('%04d-%02d-%02d', $now[5]+1900, $now[4]+1, $now[3]);
+                   $fields->{'billing_date'}{'default'} = $answers->{'date'};
+                   $ans = read_input('billing_date', $fields->{'billing_date'});
+                   $answers->{billing_date} = $ans;
                } else {
                    $answers->{billing_date} = undef;
                }
            }
-           $ans = read_input($f, $fields->{$f});
-           $answers->{$f} = $ans;
        }
 
        $sth->execute(get_next_nr(),
@@ -476,7 +514,7 @@ sub buchung_input
                      $answers->{tax_percent},
                      $answers->{tax_assigned},
                      $answers->{price},
-                     $answers->{billing_date},
+                     defined $answers->{billing_date} ? date_to_string($answers->{billing_date}) : undef,
                      $answers->{paid});
 
        $weiter = $answers->{weiter};
@@ -509,6 +547,7 @@ sub usage
     print "  --buchung-input|-bi\n";
     print "  --buchung-unpaid\n";
     print "  --buchung-hidden\n";
+    print "  --date [yyyy-mm-dd|dd.mm.] (for --pay)\n";
     print "  --pay <nr> | --unpay <nr>\n";
     print "  --hide <nr> | --unhide <nr>\n";
     print "  --list-categories|-lc\n";
@@ -541,6 +580,7 @@ my %options = (
     'hide=s' => \$data{hide},
     'unhide=s' => \$data{unhide},
     'year=i' => \$opt_year,
+    'date=s' => \$opt_date,
     'direction|d=s' => \$opt_direction,
     'mailto:s' => \$data{mailto},
     'all' => \$opt_all,