X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FHelper%2FAttrDuration.pm;h=5bdd4bf7d146ecb516b48acf7e3878cd0bd0f395;hb=5067d7bd31514962af9730b33323b831d87164f8;hp=704c340ae1d0ff301e310caef20f08e1be8a037d;hpb=4180aaea33e9ff3bb35f3fa6cf91651a6225f7ad;p=kivitendo-erp.git diff --git a/SL/DB/Helper/AttrDuration.pm b/SL/DB/Helper/AttrDuration.pm index 704c340ae..5bdd4bf7d 100644 --- a/SL/DB/Helper/AttrDuration.pm +++ b/SL/DB/Helper/AttrDuration.pm @@ -3,7 +3,7 @@ package SL::DB::Helper::AttrDuration; use strict; use parent qw(Exporter); -our @EXPORT = qw(attr_duration); +our @EXPORT = qw(attr_duration attr_duration_minutes); use Carp; @@ -13,6 +13,12 @@ sub attr_duration { _make($package, $_) for @attributes; } +sub attr_duration_minutes { + my ($package, @attributes) = @_; + + _make_minutes($package, $_) for @attributes; +} + sub _make { my ($package, $attribute) = @_; @@ -75,6 +81,43 @@ sub _make { }; } +sub _make_minutes { + my ($package, $attribute) = @_; + + no strict 'refs'; + + *{ $package . '::' . $attribute . '_as_hours' } = sub { + my ($self, $value) = @_; + + $self->$attribute($value * 60 + ($self->$attribute % 60)) if @_ > 1; + return int(($self->$attribute // 0) / 60); + }; + + *{ $package . '::' . $attribute . '_as_minutes' } = sub { + my ($self, $value) = @_; + + $self->$attribute(int($self->$attribute) - (int($self->$attribute) % 60) + ($value // 0)) if @_ > 1; + return ($self->$attribute // 0) % 60; + }; + + *{ $package . '::' . $attribute . '_as_duration_string' } = sub { + my ($self, $value) = @_; + + if (@_ > 1) { + if (!defined($value) || ($value eq '')) { + $self->$attribute(undef); + } else { + croak $::locale->text("Invalid duration format") if $value !~ m{^(?:(\d*):)?(\d+)$}; + $self->$attribute(($1 // 0) * 60 + ($2 // 0)); + } + } + + my $as_hours = "${attribute}_as_hours"; + my $as_minutes = "${attribute}_as_minutes"; + return defined($self->$attribute) ? sprintf('%d:%02d', $self->$as_hours, $self->$as_minutes) : undef; + }; +} + 1; __END__ @@ -92,6 +135,7 @@ numeric columns # In a Rose model: use SL::DB::Helper::AttrDuration; __PACKAGE__->attr_duration('time_estimation'); + __PACKAGE__->attr_duration_minutes('hours'); # Read access: print "Minutes: " . $obj->time_estimation_as_minutes . " hours: " . $obj->time_estimation_as_hours . "\n"; @@ -104,11 +148,23 @@ numeric columns =head1 OVERVIEW -This is a helper for columns that store a duration as a numeric or -floating point number representing a number of hours. So the value -1.75 would stand for "1 hour, 45 minutes". +This is a helper for columns that store a duration in one of two formats: -The helper methods created are: +=over 2 + +=item 1. as a numeric or floating point number representing a number +of hours + +=item 2. as an integer presenting a number of minutes + +=back + +In the first case the value 1.75 would stand for "1 hour, 45 +minutes". In the second case the value 105 represents the same +duration. + +The helper methods created depend on the mode. Calling +C makes the following methods available: =over 4 @@ -160,6 +216,26 @@ handles this case correctly. =back +With C the following methods are available: + +=over 4 + +=item C + +Access only the minutes. Return values are in the range [0 - 59]. + +=item C + +Access only the hours. Returns an integer value. + +=item C + +Access the full value as a formatted string in the form C, +e.g. C<1:30> for the value 90 minutes. Parsing such a string is +supported, too. + +=back + =head1 FUNCTIONS =over 4 @@ -169,6 +245,11 @@ handles this case correctly. Package method. Call with the names of attributes for which the helper methods should be created. +=item C + +Package method. Call with the names of attributes for which the helper +methods should be created. + =back =head1 BUGS