Wiederkehrende Rechnungen: Formatierung von Datumsdruckvariablen über freie Formatstrings
authorMoritz Bunkus <m.bunkus@linet-services.de>
Mon, 14 Oct 2013 14:18:48 +0000 (16:18 +0200)
committerMoritz Bunkus <m.bunkus@linet-services.de>
Thu, 3 Apr 2014 12:15:09 +0000 (14:15 +0200)
SL/BackgroundJob/CreatePeriodicInvoices.pm
SL/InstallationCheck.pm

index b16ab39..ccb0b27 100644 (file)
@@ -5,6 +5,7 @@ use strict;
 use parent qw(SL::BackgroundJob::Base);
 
 use Config::Std;
+use DateTime::Format::Strptime;
 use English qw(-no_match_vars);
 
 use SL::DB::AuthUser;
@@ -79,21 +80,22 @@ sub _generate_time_period_variables {
                            $::locale->text('January'), $::locale->text('February'), $::locale->text('March'),     $::locale->text('April'),   $::locale->text('May'),      $::locale->text('June'),
                            $::locale->text('July'),    $::locale->text('August'),   $::locale->text('September'), $::locale->text('October'), $::locale->text('November'), $::locale->text('December'));
 
-  my $vars = { current_quarter     => $period_start_date->quarter,
-               previous_quarter    => $period_start_date->clone->subtract(months => 3)->quarter,
-               next_quarter        => $period_start_date->clone->add(     months => 3)->quarter,
+  my $vars = {
+    current_quarter     => [ $period_start_date->clone->truncate(to => 'month'),                        sub { $_[0]->quarter } ],
+    previous_quarter    => [ $period_start_date->clone->truncate(to => 'month')->subtract(months => 3), sub { $_[0]->quarter } ],
+    next_quarter        => [ $period_start_date->clone->truncate(to => 'month')->add(     months => 3), sub { $_[0]->quarter } ],
 
-               current_month       => $period_start_date->month,
-               previous_month      => $period_start_date->clone->subtract(months => 1)->month,
-               next_month          => $period_start_date->clone->add(     months => 1)->month,
+    current_month       => [ $period_start_date->clone->truncate(to => 'month'),                        sub { $_[0]->month } ],
+    previous_month      => [ $period_start_date->clone->truncate(to => 'month')->subtract(months => 1), sub { $_[0]->month } ],
+    next_month          => [ $period_start_date->clone->truncate(to => 'month')->add(     months => 1), sub { $_[0]->month } ],
 
-               current_year        => $period_start_date->year,
-               previous_year       => $period_start_date->year - 1,
-               next_year           => $period_start_date->year + 1,
+    current_year        => [ $period_start_date->clone->truncate(to => 'year'),                         sub { $_[0]->year } ],
+    previous_year       => [ $period_start_date->clone->truncate(to => 'year')->subtract(years => 1),   sub { $_[0]->year } ],
+    next_year           => [ $period_start_date->clone->truncate(to => 'year')->add(     years => 1),   sub { $_[0]->year } ],
 
-               period_start_date   => $::locale->format_date(\%::myconfig, $period_start_date),
-               period_end_date     => $::locale->format_date(\%::myconfig, $period_end_date),
-             };
+    period_start_date   => [ $period_start_date->clone->truncate(to => 'month'), sub { $::locale->format_date(\%::myconfig, $_[0]) } ],
+    period_end_date     => [ $period_end_date  ->clone->truncate(to => 'month'), sub { $::locale->format_date(\%::myconfig, $_[0]) } ],
+  };
 
   map { $vars->{"${_}_month_long"} = $month_names[ $vars->{"${_}_month"} ] } qw(current previous next);
 
@@ -106,8 +108,23 @@ sub _replace_vars {
   my $sub    = shift;
   my $str    = $object->$sub;
 
-  my ($key, $value);
-  $str =~ s|<\%${key}\%>|$value|g while ($key, $value) = each %{ $vars };
+  $str =~ s{ <\% ([a-z0-9_]+) ( \s+ format \s*=\s* (.*?) \s* )? \%>}{
+    my ($key, $format) = ($1, $3);
+    if (!$vars->{$key}) {
+      '';
+
+    } elsif ($format) {
+      DateTime::Format::Strptime->new(
+        pattern     => $format,
+        locale      => 'de_DE',
+        time_zone   => 'local',
+      )->format_datetime($vars->{$key}->[0]);
+
+    } else {
+      $vars->{$1}->[1]->($vars->{$1}->[0]);
+    }
+  }eigx;
+
   $object->$sub($str);
 }
 
index a3aa509..499ef67 100644 (file)
@@ -21,6 +21,7 @@ BEGIN {
   { name => "Clone",                               url => "http://search.cpan.org/~rdf/",       debian => 'libclone-perl' },
   { name => "Config::Std",                         url => "http://search.cpan.org/~dconway/",   debian => 'libconfig-std-perl' },
   { name => "DateTime",                            url => "http://search.cpan.org/~drolsky/",   debian => 'libdatetime-perl' },
+  { name => "DateTime::Format::Strptime",          url => "http://search.cpan.org/~drolsky/",   debian => 'libdatetime-format-strptime-perl' },
   { name => "DBI",             version => '1.50',  url => "http://search.cpan.org/~timb/",      debian => 'libdbi-perl' },
   { name => "DBD::Pg",         version => '1.49',  url => "http://search.cpan.org/~dbdpg/",     debian => 'libdbd-pg-perl' },
   { name => "Email::Address",                      url => "http://search.cpan.org/~rjbs/",      debian => 'libemail-address-perl' },