Pflichtenheft: durchsuchbare CVars als Spalten in Bericht anzeigen können
[kivitendo-erp.git] / SL / Helper / DateTime.pm
index 94c311f..138a8f3 100644 (file)
@@ -4,6 +4,11 @@ use strict;
 
 use SL::Util qw(_hashify);
 
+sub new_local {
+  my ($class, %params) = @_;
+  return $class->new(hour => 0, minute => 0, second => 0, time_zone => $::locale->get_local_time_zone, %params);
+}
+
 sub now_local {
   return shift->now(time_zone => $::locale->get_local_time_zone);
 }
@@ -36,6 +41,42 @@ sub from_lxoffice {
   goto &from_kivitendo;
 }
 
+sub add_business_duration {
+  my ($self, %params) = @_;
+
+  my $abs_days = abs $params{days};
+  my $neg      = $params{days} < 0;
+  my $bweek    = $params{businessweek} || 5;
+  my $weeks    = int ($abs_days / $bweek);
+  my $days     = $abs_days % $bweek;
+
+  if ($neg) {
+    $self->subtract(weeks => $weeks);
+    $self->add(days => 8 - $self->day_of_week) if $self->day_of_week > $bweek;
+    $self->subtract(days => $self->day_of_week > $days ? $days : $days + (7 - $bweek));
+  } else {
+    $self->add(weeks => $weeks);
+    $self->subtract(days => $self->day_of_week - $bweek) if $self->day_of_week > $bweek;
+    $self->add(days => $self->day_of_week + $days <= $bweek ? $days : $days + (7 - $bweek));
+  }
+
+  $self;
+}
+
+sub add_businessdays {
+  my ($self, %params) = @_;
+
+  $self->add_business_duration(%params);
+}
+
+sub subtract_businessdays {
+  my ($self, %params) = @_;
+
+  $params{days} *= -1;
+
+  $self->add_business_duration(%params);
+}
+
 1;
 
 __END__
@@ -50,6 +91,11 @@ SL::Helpers::DateTime - helper functions for L<DateTime>
 
 =over 4
 
+=item C<new_local %params>
+
+Returns the time given in C<%params> with the time zone set to the
+local time zone.
+
 =item C<now_local>
 
 Returns the current time with the time zone set to the local time zone.